A simpler, centralized INLINE.
[bpt/emacs.git] / src / xterm.c
1 /* X Communication module for terminals which understand the X protocol.
2
3 Copyright (C) 1989, 1993-2013 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
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.
11
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.
16
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/>. */
19
20 /* New display code by Gerd Moellmann <gerd@gnu.org>. */
21 /* Xt features made by Fred Pierresteguy. */
22
23 #include <config.h>
24 #include <stdio.h>
25
26 #include "lisp.h"
27 #include "blockinput.h"
28 #include "syssignal.h"
29
30 /* This may include sys/types.h, and that somehow loses
31 if this is not done before the other system files. */
32 #include "xterm.h"
33 #include <X11/cursorfont.h>
34
35 /* Load sys/types.h if not already loaded.
36 In some systems loading it twice is suicidal. */
37 #ifndef makedev
38 #include <sys/types.h>
39 #endif /* makedev */
40
41 #include <sys/ioctl.h>
42
43 #include "systime.h"
44
45 #include <fcntl.h>
46 #include <errno.h>
47 #include <sys/stat.h>
48 /* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
49 /* #include <sys/param.h> */
50
51 #include "charset.h"
52 #include "character.h"
53 #include "coding.h"
54 #include "frame.h"
55 #include "dispextern.h"
56 #include "fontset.h"
57 #include "termhooks.h"
58 #include "termopts.h"
59 #include "termchar.h"
60 #include "emacs-icon.h"
61 #include "disptab.h"
62 #include "buffer.h"
63 #include "window.h"
64 #include "keyboard.h"
65 #include "intervals.h"
66 #include "process.h"
67 #include "atimer.h"
68 #include "keymap.h"
69 #include "font.h"
70 #include "xsettings.h"
71 #include "xgselect.h"
72 #include "sysselect.h"
73
74 #ifdef USE_X_TOOLKIT
75 #include <X11/Shell.h>
76 #endif
77
78 #include <unistd.h>
79
80 #ifdef USE_GTK
81 #include "gtkutil.h"
82 #ifdef HAVE_GTK3
83 #include <X11/Xproto.h>
84 #endif
85 #endif
86
87 #if defined (USE_LUCID) || defined (USE_MOTIF)
88 #include "../lwlib/xlwmenu.h"
89 #endif
90
91 #ifdef USE_X_TOOLKIT
92 #if !defined (NO_EDITRES)
93 #define HACK_EDITRES
94 extern void _XEditResCheckMessages (Widget, XtPointer, XEvent *, Boolean *);
95 #endif /* not NO_EDITRES */
96
97 /* Include toolkit specific headers for the scroll bar widget. */
98
99 #ifdef USE_TOOLKIT_SCROLL_BARS
100 #if defined USE_MOTIF
101 #include <Xm/Xm.h> /* for LESSTIF_VERSION */
102 #include <Xm/ScrollBar.h>
103 #else /* !USE_MOTIF i.e. use Xaw */
104
105 #ifdef HAVE_XAW3D
106 #include <X11/Xaw3d/Simple.h>
107 #include <X11/Xaw3d/Scrollbar.h>
108 #include <X11/Xaw3d/ThreeD.h>
109 #else /* !HAVE_XAW3D */
110 #include <X11/Xaw/Simple.h>
111 #include <X11/Xaw/Scrollbar.h>
112 #endif /* !HAVE_XAW3D */
113 #ifndef XtNpickTop
114 #define XtNpickTop "pickTop"
115 #endif /* !XtNpickTop */
116 #endif /* !USE_MOTIF */
117 #endif /* USE_TOOLKIT_SCROLL_BARS */
118
119 #endif /* USE_X_TOOLKIT */
120
121 #ifdef USE_X_TOOLKIT
122 #include "widget.h"
123 #ifndef XtNinitialState
124 #define XtNinitialState "initialState"
125 #endif
126 #endif
127
128 #include "bitmaps/gray.xbm"
129
130 #ifdef HAVE_XKB
131 #include <X11/XKBlib.h>
132 #endif
133
134 /* Default to using XIM if available. */
135 #ifdef USE_XIM
136 int use_xim = 1;
137 #else
138 int use_xim = 0; /* configure --without-xim */
139 #endif
140
141 /* Non-zero means that a HELP_EVENT has been generated since Emacs
142 start. */
143
144 static bool any_help_event_p;
145
146 /* This is a chain of structures for all the X displays currently in
147 use. */
148
149 struct x_display_info *x_display_list;
150
151 /* This is a list of cons cells, each of the form (NAME
152 . FONT-LIST-CACHE), one for each element of x_display_list and in
153 the same order. NAME is the name of the frame. FONT-LIST-CACHE
154 records previous values returned by x-list-fonts. */
155
156 Lisp_Object x_display_name_list;
157
158 #ifdef USE_X_TOOLKIT
159
160 /* The application context for Xt use. */
161 XtAppContext Xt_app_con;
162 static String Xt_default_resources[] = {0};
163
164 /* Non-zero means user is interacting with a toolkit scroll bar. */
165 static bool toolkit_scroll_bar_interaction;
166
167 #endif /* USE_X_TOOLKIT */
168
169 /* Non-zero timeout value means ignore next mouse click if it arrives
170 before that timeout elapses (i.e. as part of the same sequence of
171 events resulting from clicking on a frame to select it). */
172
173 static Time ignore_next_mouse_click_timeout;
174
175 /* Incremented by XTread_socket whenever it really tries to read
176 events. */
177
178 static int volatile input_signal_count;
179
180 /* Used locally within XTread_socket. */
181
182 static int x_noop_count;
183
184 static Lisp_Object Qalt, Qhyper, Qmeta, Qsuper, Qmodifier_value;
185
186 static Lisp_Object Qvendor_specific_keysyms;
187 static Lisp_Object Qlatin_1;
188
189 #ifdef USE_GTK
190 /* The name of the Emacs icon file. */
191 static Lisp_Object xg_default_icon_file;
192
193 /* Used in gtkutil.c. */
194 Lisp_Object Qx_gtk_map_stock;
195 #endif
196
197 /* Some functions take this as char *, not const char *. */
198 static char emacs_class[] = EMACS_CLASS;
199
200 enum xembed_info
201 {
202 XEMBED_MAPPED = 1 << 0
203 };
204
205 enum xembed_message
206 {
207 XEMBED_EMBEDDED_NOTIFY = 0,
208 XEMBED_WINDOW_ACTIVATE = 1,
209 XEMBED_WINDOW_DEACTIVATE = 2,
210 XEMBED_REQUEST_FOCUS = 3,
211 XEMBED_FOCUS_IN = 4,
212 XEMBED_FOCUS_OUT = 5,
213 XEMBED_FOCUS_NEXT = 6,
214 XEMBED_FOCUS_PREV = 7,
215
216 XEMBED_MODALITY_ON = 10,
217 XEMBED_MODALITY_OFF = 11,
218 XEMBED_REGISTER_ACCELERATOR = 12,
219 XEMBED_UNREGISTER_ACCELERATOR = 13,
220 XEMBED_ACTIVATE_ACCELERATOR = 14
221 };
222
223 static bool x_alloc_nearest_color_1 (Display *, Colormap, XColor *);
224 static void x_set_window_size_1 (struct frame *, int, int, int);
225 static void x_raise_frame (struct frame *);
226 static void x_lower_frame (struct frame *);
227 static const XColor *x_color_cells (Display *, int *);
228 static int x_io_error_quitter (Display *);
229 static struct terminal *x_create_terminal (struct x_display_info *);
230 void x_delete_terminal (struct terminal *);
231 static void x_update_end (struct frame *);
232 static void XTframe_up_to_date (struct frame *);
233 static void x_clear_frame (struct frame *);
234 static _Noreturn void x_ins_del_lines (struct frame *, int, int);
235 static void frame_highlight (struct frame *);
236 static void frame_unhighlight (struct frame *);
237 static void x_new_focus_frame (struct x_display_info *, struct frame *);
238 static void x_focus_changed (int, int, struct x_display_info *,
239 struct frame *, struct input_event *);
240 static void XTframe_rehighlight (struct frame *);
241 static void x_frame_rehighlight (struct x_display_info *);
242 static void x_draw_hollow_cursor (struct window *, struct glyph_row *);
243 static void x_draw_bar_cursor (struct window *, struct glyph_row *, int,
244 enum text_cursor_kinds);
245
246 static void x_clip_to_row (struct window *, struct glyph_row *,
247 enum glyph_row_area, GC);
248 static void x_flush (struct frame *f);
249 static void x_update_begin (struct frame *);
250 static void x_update_window_begin (struct window *);
251 static struct scroll_bar *x_window_to_scroll_bar (Display *, Window);
252 static void x_scroll_bar_report_motion (struct frame **, Lisp_Object *,
253 enum scroll_bar_part *,
254 Lisp_Object *, Lisp_Object *,
255 Time *);
256 static int x_handle_net_wm_state (struct frame *, const XPropertyEvent *);
257 static void x_check_fullscreen (struct frame *);
258 static void x_check_expected_move (struct frame *, int, int);
259 static void x_sync_with_move (struct frame *, int, int, int);
260 static int handle_one_xevent (struct x_display_info *,
261 const XEvent *, int *,
262 struct input_event *);
263 #ifdef USE_GTK
264 static int x_dispatch_event (XEvent *, Display *);
265 #endif
266 /* Don't declare this _Noreturn because we want no
267 interference with debugging failing X calls. */
268 static void x_connection_closed (Display *, const char *);
269 static void x_wm_set_window_state (struct frame *, int);
270 static void x_wm_set_icon_pixmap (struct frame *, ptrdiff_t);
271 static void x_initialize (void);
272
273
274 /* Flush display of frame F. */
275
276 static void
277 x_flush (struct frame *f)
278 {
279 eassert (f && FRAME_X_P (f));
280 /* Don't call XFlush when it is not safe to redisplay; the X
281 connection may be broken. */
282 if (!NILP (Vinhibit_redisplay))
283 return;
284
285 block_input ();
286 XFlush (FRAME_X_DISPLAY (f));
287 unblock_input ();
288 }
289
290
291 /* Remove calls to XFlush by defining XFlush to an empty replacement.
292 Calls to XFlush should be unnecessary because the X output buffer
293 is flushed automatically as needed by calls to XPending,
294 XNextEvent, or XWindowEvent according to the XFlush man page.
295 XTread_socket calls XPending. Removing XFlush improves
296 performance. */
297
298 #define XFlush(DISPLAY) (void) 0
299
300 \f
301 /***********************************************************************
302 Debugging
303 ***********************************************************************/
304
305 #if 0
306
307 /* This is a function useful for recording debugging information about
308 the sequence of occurrences in this file. */
309
310 struct record
311 {
312 char *locus;
313 int type;
314 };
315
316 struct record event_record[100];
317
318 int event_record_index;
319
320 void
321 record_event (char *locus, int type)
322 {
323 if (event_record_index == sizeof (event_record) / sizeof (struct record))
324 event_record_index = 0;
325
326 event_record[event_record_index].locus = locus;
327 event_record[event_record_index].type = type;
328 event_record_index++;
329 }
330
331 #endif /* 0 */
332
333
334 \f
335 /* Return the struct x_display_info corresponding to DPY. */
336
337 struct x_display_info *
338 x_display_info_for_display (Display *dpy)
339 {
340 struct x_display_info *dpyinfo;
341
342 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
343 if (dpyinfo->display == dpy)
344 return dpyinfo;
345
346 return 0;
347 }
348
349 static Window
350 x_find_topmost_parent (struct frame *f)
351 {
352 struct x_output *x = f->output_data.x;
353 Window win = None, wi = x->parent_desc;
354 Display *dpy = FRAME_X_DISPLAY (f);
355
356 while (wi != FRAME_DISPLAY_INFO (f)->root_window)
357 {
358 Window root;
359 Window *children;
360 unsigned int nchildren;
361
362 win = wi;
363 XQueryTree (dpy, win, &root, &wi, &children, &nchildren);
364 XFree (children);
365 }
366
367 return win;
368 }
369
370 #define OPAQUE 0xffffffff
371
372 void
373 x_set_frame_alpha (struct frame *f)
374 {
375 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
376 Display *dpy = FRAME_X_DISPLAY (f);
377 Window win = FRAME_OUTER_WINDOW (f);
378 double alpha = 1.0;
379 double alpha_min = 1.0;
380 unsigned long opac;
381 Window parent;
382
383 if (dpyinfo->x_highlight_frame == f)
384 alpha = f->alpha[0];
385 else
386 alpha = f->alpha[1];
387
388 if (FLOATP (Vframe_alpha_lower_limit))
389 alpha_min = XFLOAT_DATA (Vframe_alpha_lower_limit);
390 else if (INTEGERP (Vframe_alpha_lower_limit))
391 alpha_min = (XINT (Vframe_alpha_lower_limit)) / 100.0;
392
393 if (alpha < 0.0)
394 return;
395 else if (alpha > 1.0)
396 alpha = 1.0;
397 else if (0.0 <= alpha && alpha < alpha_min && alpha_min <= 1.0)
398 alpha = alpha_min;
399
400 opac = alpha * OPAQUE;
401
402 x_catch_errors (dpy);
403
404 /* If there is a parent from the window manager, put the property there
405 also, to work around broken window managers that fail to do that.
406 Do this unconditionally as this function is called on reparent when
407 alpha has not changed on the frame. */
408
409 parent = x_find_topmost_parent (f);
410 if (parent != None)
411 XChangeProperty (dpy, parent, dpyinfo->Xatom_net_wm_window_opacity,
412 XA_CARDINAL, 32, PropModeReplace,
413 (unsigned char *) &opac, 1L);
414
415 /* return unless necessary */
416 {
417 unsigned char *data;
418 Atom actual;
419 int rc, format;
420 unsigned long n, left;
421
422 rc = XGetWindowProperty (dpy, win, dpyinfo->Xatom_net_wm_window_opacity,
423 0L, 1L, False, XA_CARDINAL,
424 &actual, &format, &n, &left,
425 &data);
426
427 if (rc == Success && actual != None)
428 {
429 unsigned long value = *(unsigned long *)data;
430 XFree (data);
431 if (value == opac)
432 {
433 x_uncatch_errors ();
434 return;
435 }
436 }
437 }
438
439 XChangeProperty (dpy, win, dpyinfo->Xatom_net_wm_window_opacity,
440 XA_CARDINAL, 32, PropModeReplace,
441 (unsigned char *) &opac, 1L);
442 x_uncatch_errors ();
443 }
444
445 int
446 x_display_pixel_height (struct x_display_info *dpyinfo)
447 {
448 return HeightOfScreen (dpyinfo->screen);
449 }
450
451 int
452 x_display_pixel_width (struct x_display_info *dpyinfo)
453 {
454 return WidthOfScreen (dpyinfo->screen);
455 }
456
457 \f
458 /***********************************************************************
459 Starting and ending an update
460 ***********************************************************************/
461
462 /* Start an update of frame F. This function is installed as a hook
463 for update_begin, i.e. it is called when update_begin is called.
464 This function is called prior to calls to x_update_window_begin for
465 each window being updated. Currently, there is nothing to do here
466 because all interesting stuff is done on a window basis. */
467
468 static void
469 x_update_begin (struct frame *f)
470 {
471 /* Nothing to do. */
472 }
473
474
475 /* Start update of window W. */
476
477 static void
478 x_update_window_begin (struct window *w)
479 {
480 struct frame *f = XFRAME (WINDOW_FRAME (w));
481 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
482
483 w->output_cursor = w->cursor;
484
485 block_input ();
486
487 if (f == hlinfo->mouse_face_mouse_frame)
488 {
489 /* Don't do highlighting for mouse motion during the update. */
490 hlinfo->mouse_face_defer = 1;
491
492 /* If F needs to be redrawn, simply forget about any prior mouse
493 highlighting. */
494 if (FRAME_GARBAGED_P (f))
495 hlinfo->mouse_face_window = Qnil;
496 }
497
498 unblock_input ();
499 }
500
501
502 /* Draw a vertical window border from (x,y0) to (x,y1) */
503
504 static void
505 x_draw_vertical_window_border (struct window *w, int x, int y0, int y1)
506 {
507 struct frame *f = XFRAME (WINDOW_FRAME (w));
508 struct face *face;
509
510 face = FACE_FROM_ID (f, VERTICAL_BORDER_FACE_ID);
511 if (face)
512 XSetForeground (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
513 face->foreground);
514
515 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
516 f->output_data.x->normal_gc, x, y0, x, y1);
517 }
518
519 /* End update of window W.
520
521 Draw vertical borders between horizontally adjacent windows, and
522 display W's cursor if CURSOR_ON_P is non-zero.
523
524 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
525 glyphs in mouse-face were overwritten. In that case we have to
526 make sure that the mouse-highlight is properly redrawn.
527
528 W may be a menu bar pseudo-window in case we don't have X toolkit
529 support. Such windows don't have a cursor, so don't display it
530 here. */
531
532 static void
533 x_update_window_end (struct window *w, bool cursor_on_p,
534 bool mouse_face_overwritten_p)
535 {
536 if (!w->pseudo_window_p)
537 {
538 block_input ();
539
540 if (cursor_on_p)
541 display_and_set_cursor (w, 1,
542 w->output_cursor.hpos, w->output_cursor.vpos,
543 w->output_cursor.x, w->output_cursor.y);
544
545 if (draw_window_fringes (w, 1))
546 x_draw_vertical_border (w);
547
548 unblock_input ();
549 }
550
551 /* If a row with mouse-face was overwritten, arrange for
552 XTframe_up_to_date to redisplay the mouse highlight. */
553 if (mouse_face_overwritten_p)
554 reset_mouse_highlight (MOUSE_HL_INFO (XFRAME (w->frame)));
555 }
556
557
558 /* End update of frame F. This function is installed as a hook in
559 update_end. */
560
561 static void
562 x_update_end (struct frame *f)
563 {
564 /* Mouse highlight may be displayed again. */
565 MOUSE_HL_INFO (f)->mouse_face_defer = 0;
566
567 #ifndef XFlush
568 block_input ();
569 XFlush (FRAME_X_DISPLAY (f));
570 unblock_input ();
571 #endif
572 }
573
574
575 /* This function is called from various places in xdisp.c
576 whenever a complete update has been performed. */
577
578 static void
579 XTframe_up_to_date (struct frame *f)
580 {
581 if (FRAME_X_P (f))
582 FRAME_MOUSE_UPDATE (f);
583 }
584
585
586 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
587 arrow bitmaps, or clear the fringes if no bitmaps are required
588 before DESIRED_ROW is made current. This function is called from
589 update_window_line only if it is known that there are differences
590 between bitmaps to be drawn between current row and DESIRED_ROW. */
591
592 static void
593 x_after_update_window_line (struct window *w, struct glyph_row *desired_row)
594 {
595 struct frame *f;
596 int width, height;
597
598 eassert (w);
599
600 if (!desired_row->mode_line_p && !w->pseudo_window_p)
601 desired_row->redraw_fringe_bitmaps_p = 1;
602
603 /* When a window has disappeared, make sure that no rest of
604 full-width rows stays visible in the internal border. Could
605 check here if updated window is the leftmost/rightmost window,
606 but I guess it's not worth doing since vertically split windows
607 are almost never used, internal border is rarely set, and the
608 overhead is very small. */
609 if (windows_or_buffers_changed
610 && desired_row->full_width_p
611 && (f = XFRAME (w->frame),
612 width = FRAME_INTERNAL_BORDER_WIDTH (f),
613 width != 0)
614 && (height = desired_row->visible_height,
615 height > 0))
616 {
617 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
618
619 block_input ();
620 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
621 0, y, width, height);
622 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
623 FRAME_PIXEL_WIDTH (f) - width,
624 y, width, height);
625 unblock_input ();
626 }
627 }
628
629 static void
630 x_draw_fringe_bitmap (struct window *w, struct glyph_row *row, struct draw_fringe_bitmap_params *p)
631 {
632 struct frame *f = XFRAME (WINDOW_FRAME (w));
633 Display *display = FRAME_X_DISPLAY (f);
634 Window window = FRAME_X_WINDOW (f);
635 GC gc = f->output_data.x->normal_gc;
636 struct face *face = p->face;
637
638 /* Must clip because of partially visible lines. */
639 x_clip_to_row (w, row, ANY_AREA, gc);
640
641 if (!p->overlay_p)
642 {
643 int bx = p->bx, by = p->by, nx = p->nx, ny = p->ny;
644
645 /* In case the same realized face is used for fringes and
646 for something displayed in the text (e.g. face `region' on
647 mono-displays, the fill style may have been changed to
648 FillSolid in x_draw_glyph_string_background. */
649 if (face->stipple)
650 XSetFillStyle (display, face->gc, FillOpaqueStippled);
651 else
652 XSetForeground (display, face->gc, face->background);
653
654 #ifdef USE_TOOLKIT_SCROLL_BARS
655 /* If the fringe is adjacent to the left (right) scroll bar of a
656 leftmost (rightmost, respectively) window, then extend its
657 background to the gap between the fringe and the bar. */
658 if ((WINDOW_LEFTMOST_P (w)
659 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
660 || (WINDOW_RIGHTMOST_P (w)
661 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w)))
662 {
663 int sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
664
665 if (sb_width > 0)
666 {
667 int bar_area_x = WINDOW_SCROLL_BAR_AREA_X (w);
668 int bar_area_width = (WINDOW_CONFIG_SCROLL_BAR_COLS (w)
669 * FRAME_COLUMN_WIDTH (f));
670
671 if (bx < 0)
672 {
673 /* Bitmap fills the fringe. */
674 if (bar_area_x + bar_area_width == p->x)
675 bx = bar_area_x + sb_width;
676 else if (p->x + p->wd == bar_area_x)
677 bx = bar_area_x;
678 if (bx >= 0)
679 {
680 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
681
682 nx = bar_area_width - sb_width;
683 by = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
684 row->y));
685 ny = row->visible_height;
686 }
687 }
688 else
689 {
690 if (bar_area_x + bar_area_width == bx)
691 {
692 bx = bar_area_x + sb_width;
693 nx += bar_area_width - sb_width;
694 }
695 else if (bx + nx == bar_area_x)
696 nx += bar_area_width - sb_width;
697 }
698 }
699 }
700 #endif
701 if (bx >= 0 && nx > 0)
702 XFillRectangle (display, window, face->gc, bx, by, nx, ny);
703
704 if (!face->stipple)
705 XSetForeground (display, face->gc, face->foreground);
706 }
707
708 if (p->which)
709 {
710 char *bits;
711 Pixmap pixmap, clipmask = (Pixmap) 0;
712 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
713 XGCValues gcv;
714
715 if (p->wd > 8)
716 bits = (char *) (p->bits + p->dh);
717 else
718 bits = (char *) p->bits + p->dh;
719
720 /* Draw the bitmap. I believe these small pixmaps can be cached
721 by the server. */
722 pixmap = XCreatePixmapFromBitmapData (display, window, bits, p->wd, p->h,
723 (p->cursor_p
724 ? (p->overlay_p ? face->background
725 : f->output_data.x->cursor_pixel)
726 : face->foreground),
727 face->background, depth);
728
729 if (p->overlay_p)
730 {
731 clipmask = XCreatePixmapFromBitmapData (display,
732 FRAME_DISPLAY_INFO (f)->root_window,
733 bits, p->wd, p->h,
734 1, 0, 1);
735 gcv.clip_mask = clipmask;
736 gcv.clip_x_origin = p->x;
737 gcv.clip_y_origin = p->y;
738 XChangeGC (display, gc, GCClipMask | GCClipXOrigin | GCClipYOrigin, &gcv);
739 }
740
741 XCopyArea (display, pixmap, window, gc, 0, 0,
742 p->wd, p->h, p->x, p->y);
743 XFreePixmap (display, pixmap);
744
745 if (p->overlay_p)
746 {
747 gcv.clip_mask = (Pixmap) 0;
748 XChangeGC (display, gc, GCClipMask, &gcv);
749 XFreePixmap (display, clipmask);
750 }
751 }
752
753 XSetClipMask (display, gc, None);
754 }
755
756 /***********************************************************************
757 Glyph display
758 ***********************************************************************/
759
760
761
762 static void x_set_glyph_string_clipping (struct glyph_string *);
763 static void x_set_glyph_string_gc (struct glyph_string *);
764 static void x_draw_glyph_string_foreground (struct glyph_string *);
765 static void x_draw_composite_glyph_string_foreground (struct glyph_string *);
766 static void x_draw_glyph_string_box (struct glyph_string *);
767 static void x_draw_glyph_string (struct glyph_string *);
768 static _Noreturn void x_delete_glyphs (struct frame *, int);
769 static void x_compute_glyph_string_overhangs (struct glyph_string *);
770 static void x_set_cursor_gc (struct glyph_string *);
771 static void x_set_mode_line_face_gc (struct glyph_string *);
772 static void x_set_mouse_face_gc (struct glyph_string *);
773 static bool x_alloc_lighter_color (struct frame *, Display *, Colormap,
774 unsigned long *, double, int);
775 static void x_setup_relief_color (struct frame *, struct relief *,
776 double, int, unsigned long);
777 static void x_setup_relief_colors (struct glyph_string *);
778 static void x_draw_image_glyph_string (struct glyph_string *);
779 static void x_draw_image_relief (struct glyph_string *);
780 static void x_draw_image_foreground (struct glyph_string *);
781 static void x_draw_image_foreground_1 (struct glyph_string *, Pixmap);
782 static void x_clear_glyph_string_rect (struct glyph_string *, int,
783 int, int, int);
784 static void x_draw_relief_rect (struct frame *, int, int, int, int,
785 int, int, int, int, int, int,
786 XRectangle *);
787 static void x_draw_box_rect (struct glyph_string *, int, int, int, int,
788 int, int, int, XRectangle *);
789 static void x_scroll_bar_clear (struct frame *);
790
791 #ifdef GLYPH_DEBUG
792 static void x_check_font (struct frame *, struct font *);
793 #endif
794
795
796 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
797 face. */
798
799 static void
800 x_set_cursor_gc (struct glyph_string *s)
801 {
802 if (s->font == FRAME_FONT (s->f)
803 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
804 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
805 && !s->cmp)
806 s->gc = s->f->output_data.x->cursor_gc;
807 else
808 {
809 /* Cursor on non-default face: must merge. */
810 XGCValues xgcv;
811 unsigned long mask;
812
813 xgcv.background = s->f->output_data.x->cursor_pixel;
814 xgcv.foreground = s->face->background;
815
816 /* If the glyph would be invisible, try a different foreground. */
817 if (xgcv.foreground == xgcv.background)
818 xgcv.foreground = s->face->foreground;
819 if (xgcv.foreground == xgcv.background)
820 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
821 if (xgcv.foreground == xgcv.background)
822 xgcv.foreground = s->face->foreground;
823
824 /* Make sure the cursor is distinct from text in this face. */
825 if (xgcv.background == s->face->background
826 && xgcv.foreground == s->face->foreground)
827 {
828 xgcv.background = s->face->foreground;
829 xgcv.foreground = s->face->background;
830 }
831
832 IF_DEBUG (x_check_font (s->f, s->font));
833 xgcv.graphics_exposures = False;
834 mask = GCForeground | GCBackground | GCGraphicsExposures;
835
836 if (FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc)
837 XChangeGC (s->display, FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc,
838 mask, &xgcv);
839 else
840 FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc
841 = XCreateGC (s->display, s->window, mask, &xgcv);
842
843 s->gc = FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc;
844 }
845 }
846
847
848 /* Set up S->gc of glyph string S for drawing text in mouse face. */
849
850 static void
851 x_set_mouse_face_gc (struct glyph_string *s)
852 {
853 int face_id;
854 struct face *face;
855
856 /* What face has to be used last for the mouse face? */
857 face_id = MOUSE_HL_INFO (s->f)->mouse_face_face_id;
858 face = FACE_FROM_ID (s->f, face_id);
859 if (face == NULL)
860 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
861
862 if (s->first_glyph->type == CHAR_GLYPH)
863 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch, -1, Qnil);
864 else
865 face_id = FACE_FOR_CHAR (s->f, face, 0, -1, Qnil);
866 s->face = FACE_FROM_ID (s->f, face_id);
867 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
868
869 if (s->font == s->face->font)
870 s->gc = s->face->gc;
871 else
872 {
873 /* Otherwise construct scratch_cursor_gc with values from FACE
874 except for FONT. */
875 XGCValues xgcv;
876 unsigned long mask;
877
878 xgcv.background = s->face->background;
879 xgcv.foreground = s->face->foreground;
880 xgcv.graphics_exposures = False;
881 mask = GCForeground | GCBackground | GCGraphicsExposures;
882
883 if (FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc)
884 XChangeGC (s->display, FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc,
885 mask, &xgcv);
886 else
887 FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc
888 = XCreateGC (s->display, s->window, mask, &xgcv);
889
890 s->gc = FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc;
891
892 }
893 eassert (s->gc != 0);
894 }
895
896
897 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
898 Faces to use in the mode line have already been computed when the
899 matrix was built, so there isn't much to do, here. */
900
901 static void
902 x_set_mode_line_face_gc (struct glyph_string *s)
903 {
904 s->gc = s->face->gc;
905 }
906
907
908 /* Set S->gc of glyph string S for drawing that glyph string. Set
909 S->stippled_p to a non-zero value if the face of S has a stipple
910 pattern. */
911
912 static void
913 x_set_glyph_string_gc (struct glyph_string *s)
914 {
915 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
916
917 if (s->hl == DRAW_NORMAL_TEXT)
918 {
919 s->gc = s->face->gc;
920 s->stippled_p = s->face->stipple != 0;
921 }
922 else if (s->hl == DRAW_INVERSE_VIDEO)
923 {
924 x_set_mode_line_face_gc (s);
925 s->stippled_p = s->face->stipple != 0;
926 }
927 else if (s->hl == DRAW_CURSOR)
928 {
929 x_set_cursor_gc (s);
930 s->stippled_p = 0;
931 }
932 else if (s->hl == DRAW_MOUSE_FACE)
933 {
934 x_set_mouse_face_gc (s);
935 s->stippled_p = s->face->stipple != 0;
936 }
937 else if (s->hl == DRAW_IMAGE_RAISED
938 || s->hl == DRAW_IMAGE_SUNKEN)
939 {
940 s->gc = s->face->gc;
941 s->stippled_p = s->face->stipple != 0;
942 }
943 else
944 {
945 s->gc = s->face->gc;
946 s->stippled_p = s->face->stipple != 0;
947 }
948
949 /* GC must have been set. */
950 eassert (s->gc != 0);
951 }
952
953
954 /* Set clipping for output of glyph string S. S may be part of a mode
955 line or menu if we don't have X toolkit support. */
956
957 static void
958 x_set_glyph_string_clipping (struct glyph_string *s)
959 {
960 XRectangle *r = s->clip;
961 int n = get_glyph_string_clip_rects (s, r, 2);
962
963 if (n > 0)
964 XSetClipRectangles (s->display, s->gc, 0, 0, r, n, Unsorted);
965 s->num_clips = n;
966 }
967
968
969 /* Set SRC's clipping for output of glyph string DST. This is called
970 when we are drawing DST's left_overhang or right_overhang only in
971 the area of SRC. */
972
973 static void
974 x_set_glyph_string_clipping_exactly (struct glyph_string *src, struct glyph_string *dst)
975 {
976 XRectangle r;
977
978 r.x = src->x;
979 r.width = src->width;
980 r.y = src->y;
981 r.height = src->height;
982 dst->clip[0] = r;
983 dst->num_clips = 1;
984 XSetClipRectangles (dst->display, dst->gc, 0, 0, &r, 1, Unsorted);
985 }
986
987
988 /* RIF:
989 Compute left and right overhang of glyph string S. */
990
991 static void
992 x_compute_glyph_string_overhangs (struct glyph_string *s)
993 {
994 if (s->cmp == NULL
995 && (s->first_glyph->type == CHAR_GLYPH
996 || s->first_glyph->type == COMPOSITE_GLYPH))
997 {
998 struct font_metrics metrics;
999
1000 if (s->first_glyph->type == CHAR_GLYPH)
1001 {
1002 unsigned *code = alloca (sizeof (unsigned) * s->nchars);
1003 struct font *font = s->font;
1004 int i;
1005
1006 for (i = 0; i < s->nchars; i++)
1007 code[i] = (s->char2b[i].byte1 << 8) | s->char2b[i].byte2;
1008 font->driver->text_extents (font, code, s->nchars, &metrics);
1009 }
1010 else
1011 {
1012 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1013
1014 composition_gstring_width (gstring, s->cmp_from, s->cmp_to, &metrics);
1015 }
1016 s->right_overhang = (metrics.rbearing > metrics.width
1017 ? metrics.rbearing - metrics.width : 0);
1018 s->left_overhang = metrics.lbearing < 0 ? - metrics.lbearing : 0;
1019 }
1020 else if (s->cmp)
1021 {
1022 s->right_overhang = s->cmp->rbearing - s->cmp->pixel_width;
1023 s->left_overhang = - s->cmp->lbearing;
1024 }
1025 }
1026
1027
1028 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
1029
1030 static void
1031 x_clear_glyph_string_rect (struct glyph_string *s, int x, int y, int w, int h)
1032 {
1033 XGCValues xgcv;
1034 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
1035 XSetForeground (s->display, s->gc, xgcv.background);
1036 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
1037 XSetForeground (s->display, s->gc, xgcv.foreground);
1038 }
1039
1040
1041 /* Draw the background of glyph_string S. If S->background_filled_p
1042 is non-zero don't draw it. FORCE_P non-zero means draw the
1043 background even if it wouldn't be drawn normally. This is used
1044 when a string preceding S draws into the background of S, or S
1045 contains the first component of a composition. */
1046
1047 static void
1048 x_draw_glyph_string_background (struct glyph_string *s, bool force_p)
1049 {
1050 /* Nothing to do if background has already been drawn or if it
1051 shouldn't be drawn in the first place. */
1052 if (!s->background_filled_p)
1053 {
1054 int box_line_width = max (s->face->box_line_width, 0);
1055
1056 if (s->stippled_p)
1057 {
1058 /* Fill background with a stipple pattern. */
1059 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
1060 XFillRectangle (s->display, s->window, s->gc, s->x,
1061 s->y + box_line_width,
1062 s->background_width,
1063 s->height - 2 * box_line_width);
1064 XSetFillStyle (s->display, s->gc, FillSolid);
1065 s->background_filled_p = 1;
1066 }
1067 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
1068 || s->font_not_found_p
1069 || s->extends_to_end_of_line_p
1070 || force_p)
1071 {
1072 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
1073 s->background_width,
1074 s->height - 2 * box_line_width);
1075 s->background_filled_p = 1;
1076 }
1077 }
1078 }
1079
1080
1081 /* Draw the foreground of glyph string S. */
1082
1083 static void
1084 x_draw_glyph_string_foreground (struct glyph_string *s)
1085 {
1086 int i, x;
1087
1088 /* If first glyph of S has a left box line, start drawing the text
1089 of S to the right of that box line. */
1090 if (s->face->box != FACE_NO_BOX
1091 && s->first_glyph->left_box_line_p)
1092 x = s->x + eabs (s->face->box_line_width);
1093 else
1094 x = s->x;
1095
1096 /* Draw characters of S as rectangles if S's font could not be
1097 loaded. */
1098 if (s->font_not_found_p)
1099 {
1100 for (i = 0; i < s->nchars; ++i)
1101 {
1102 struct glyph *g = s->first_glyph + i;
1103 XDrawRectangle (s->display, s->window,
1104 s->gc, x, s->y, g->pixel_width - 1,
1105 s->height - 1);
1106 x += g->pixel_width;
1107 }
1108 }
1109 else
1110 {
1111 struct font *font = s->font;
1112 int boff = font->baseline_offset;
1113 int y;
1114
1115 if (font->vertical_centering)
1116 boff = VCENTER_BASELINE_OFFSET (font, s->f) - boff;
1117
1118 y = s->ybase - boff;
1119 if (s->for_overlaps
1120 || (s->background_filled_p && s->hl != DRAW_CURSOR))
1121 font->driver->draw (s, 0, s->nchars, x, y, 0);
1122 else
1123 font->driver->draw (s, 0, s->nchars, x, y, 1);
1124 if (s->face->overstrike)
1125 font->driver->draw (s, 0, s->nchars, x + 1, y, 0);
1126 }
1127 }
1128
1129 /* Draw the foreground of composite glyph string S. */
1130
1131 static void
1132 x_draw_composite_glyph_string_foreground (struct glyph_string *s)
1133 {
1134 int i, j, x;
1135 struct font *font = s->font;
1136
1137 /* If first glyph of S has a left box line, start drawing the text
1138 of S to the right of that box line. */
1139 if (s->face && s->face->box != FACE_NO_BOX
1140 && s->first_glyph->left_box_line_p)
1141 x = s->x + eabs (s->face->box_line_width);
1142 else
1143 x = s->x;
1144
1145 /* S is a glyph string for a composition. S->cmp_from is the index
1146 of the first character drawn for glyphs of this composition.
1147 S->cmp_from == 0 means we are drawing the very first character of
1148 this composition. */
1149
1150 /* Draw a rectangle for the composition if the font for the very
1151 first character of the composition could not be loaded. */
1152 if (s->font_not_found_p)
1153 {
1154 if (s->cmp_from == 0)
1155 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
1156 s->width - 1, s->height - 1);
1157 }
1158 else if (! s->first_glyph->u.cmp.automatic)
1159 {
1160 int y = s->ybase;
1161
1162 for (i = 0, j = s->cmp_from; i < s->nchars; i++, j++)
1163 /* TAB in a composition means display glyphs with padding
1164 space on the left or right. */
1165 if (COMPOSITION_GLYPH (s->cmp, j) != '\t')
1166 {
1167 int xx = x + s->cmp->offsets[j * 2];
1168 int yy = y - s->cmp->offsets[j * 2 + 1];
1169
1170 font->driver->draw (s, j, j + 1, xx, yy, 0);
1171 if (s->face->overstrike)
1172 font->driver->draw (s, j, j + 1, xx + 1, yy, 0);
1173 }
1174 }
1175 else
1176 {
1177 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1178 Lisp_Object glyph;
1179 int y = s->ybase;
1180 int width = 0;
1181
1182 for (i = j = s->cmp_from; i < s->cmp_to; i++)
1183 {
1184 glyph = LGSTRING_GLYPH (gstring, i);
1185 if (NILP (LGLYPH_ADJUSTMENT (glyph)))
1186 width += LGLYPH_WIDTH (glyph);
1187 else
1188 {
1189 int xoff, yoff, wadjust;
1190
1191 if (j < i)
1192 {
1193 font->driver->draw (s, j, i, x, y, 0);
1194 if (s->face->overstrike)
1195 font->driver->draw (s, j, i, x + 1, y, 0);
1196 x += width;
1197 }
1198 xoff = LGLYPH_XOFF (glyph);
1199 yoff = LGLYPH_YOFF (glyph);
1200 wadjust = LGLYPH_WADJUST (glyph);
1201 font->driver->draw (s, i, i + 1, x + xoff, y + yoff, 0);
1202 if (s->face->overstrike)
1203 font->driver->draw (s, i, i + 1, x + xoff + 1, y + yoff, 0);
1204 x += wadjust;
1205 j = i + 1;
1206 width = 0;
1207 }
1208 }
1209 if (j < i)
1210 {
1211 font->driver->draw (s, j, i, x, y, 0);
1212 if (s->face->overstrike)
1213 font->driver->draw (s, j, i, x + 1, y, 0);
1214 }
1215 }
1216 }
1217
1218
1219 /* Draw the foreground of glyph string S for glyphless characters. */
1220
1221 static void
1222 x_draw_glyphless_glyph_string_foreground (struct glyph_string *s)
1223 {
1224 struct glyph *glyph = s->first_glyph;
1225 XChar2b char2b[8];
1226 int x, i, j;
1227
1228 /* If first glyph of S has a left box line, start drawing the text
1229 of S to the right of that box line. */
1230 if (s->face && s->face->box != FACE_NO_BOX
1231 && s->first_glyph->left_box_line_p)
1232 x = s->x + eabs (s->face->box_line_width);
1233 else
1234 x = s->x;
1235
1236 s->char2b = char2b;
1237
1238 for (i = 0; i < s->nchars; i++, glyph++)
1239 {
1240 char buf[7], *str = NULL;
1241 int len = glyph->u.glyphless.len;
1242
1243 if (glyph->u.glyphless.method == GLYPHLESS_DISPLAY_ACRONYM)
1244 {
1245 if (len > 0
1246 && CHAR_TABLE_P (Vglyphless_char_display)
1247 && (CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display))
1248 >= 1))
1249 {
1250 Lisp_Object acronym
1251 = (! glyph->u.glyphless.for_no_font
1252 ? CHAR_TABLE_REF (Vglyphless_char_display,
1253 glyph->u.glyphless.ch)
1254 : XCHAR_TABLE (Vglyphless_char_display)->extras[0]);
1255 if (STRINGP (acronym))
1256 str = SSDATA (acronym);
1257 }
1258 }
1259 else if (glyph->u.glyphless.method == GLYPHLESS_DISPLAY_HEX_CODE)
1260 {
1261 sprintf (buf, "%0*X",
1262 glyph->u.glyphless.ch < 0x10000 ? 4 : 6,
1263 glyph->u.glyphless.ch);
1264 str = buf;
1265 }
1266
1267 if (str)
1268 {
1269 int upper_len = (len + 1) / 2;
1270 unsigned code;
1271
1272 /* It is assured that all LEN characters in STR is ASCII. */
1273 for (j = 0; j < len; j++)
1274 {
1275 code = s->font->driver->encode_char (s->font, str[j]);
1276 STORE_XCHAR2B (char2b + j, code >> 8, code & 0xFF);
1277 }
1278 s->font->driver->draw (s, 0, upper_len,
1279 x + glyph->slice.glyphless.upper_xoff,
1280 s->ybase + glyph->slice.glyphless.upper_yoff,
1281 0);
1282 s->font->driver->draw (s, upper_len, len,
1283 x + glyph->slice.glyphless.lower_xoff,
1284 s->ybase + glyph->slice.glyphless.lower_yoff,
1285 0);
1286 }
1287 if (glyph->u.glyphless.method != GLYPHLESS_DISPLAY_THIN_SPACE)
1288 XDrawRectangle (s->display, s->window, s->gc,
1289 x, s->ybase - glyph->ascent,
1290 glyph->pixel_width - 1,
1291 glyph->ascent + glyph->descent - 1);
1292 x += glyph->pixel_width;
1293 }
1294 }
1295
1296 #ifdef USE_X_TOOLKIT
1297
1298 #ifdef USE_LUCID
1299
1300 /* Return the frame on which widget WIDGET is used.. Abort if frame
1301 cannot be determined. */
1302
1303 static struct frame *
1304 x_frame_of_widget (Widget widget)
1305 {
1306 struct x_display_info *dpyinfo;
1307 Lisp_Object tail, frame;
1308 struct frame *f;
1309
1310 dpyinfo = x_display_info_for_display (XtDisplay (widget));
1311
1312 /* Find the top-level shell of the widget. Note that this function
1313 can be called when the widget is not yet realized, so XtWindow
1314 (widget) == 0. That's the reason we can't simply use
1315 x_any_window_to_frame. */
1316 while (!XtIsTopLevelShell (widget))
1317 widget = XtParent (widget);
1318
1319 /* Look for a frame with that top-level widget. Allocate the color
1320 on that frame to get the right gamma correction value. */
1321 FOR_EACH_FRAME (tail, frame)
1322 {
1323 f = XFRAME (frame);
1324 if (FRAME_X_P (f)
1325 && f->output_data.nothing != 1
1326 && FRAME_DISPLAY_INFO (f) == dpyinfo
1327 && f->output_data.x->widget == widget)
1328 return f;
1329 }
1330 emacs_abort ();
1331 }
1332
1333 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1334 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1335 If this produces the same color as PIXEL, try a color where all RGB
1336 values have DELTA added. Return the allocated color in *PIXEL.
1337 DISPLAY is the X display, CMAP is the colormap to operate on.
1338 Value is true if successful. */
1339
1340 bool
1341 x_alloc_lighter_color_for_widget (Widget widget, Display *display, Colormap cmap,
1342 unsigned long *pixel, double factor, int delta)
1343 {
1344 struct frame *f = x_frame_of_widget (widget);
1345 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
1346 }
1347
1348 #endif /* USE_LUCID */
1349
1350
1351 /* Structure specifying which arguments should be passed by Xt to
1352 cvt_string_to_pixel. We want the widget's screen and colormap. */
1353
1354 static XtConvertArgRec cvt_string_to_pixel_args[] =
1355 {
1356 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.screen),
1357 sizeof (Screen *)},
1358 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.colormap),
1359 sizeof (Colormap)}
1360 };
1361
1362
1363 /* The address of this variable is returned by
1364 cvt_string_to_pixel. */
1365
1366 static Pixel cvt_string_to_pixel_value;
1367
1368
1369 /* Convert a color name to a pixel color.
1370
1371 DPY is the display we are working on.
1372
1373 ARGS is an array of *NARGS XrmValue structures holding additional
1374 information about the widget for which the conversion takes place.
1375 The contents of this array are determined by the specification
1376 in cvt_string_to_pixel_args.
1377
1378 FROM is a pointer to an XrmValue which points to the color name to
1379 convert. TO is an XrmValue in which to return the pixel color.
1380
1381 CLOSURE_RET is a pointer to user-data, in which we record if
1382 we allocated the color or not.
1383
1384 Value is True if successful, False otherwise. */
1385
1386 static Boolean
1387 cvt_string_to_pixel (Display *dpy, XrmValue *args, Cardinal *nargs,
1388 XrmValue *from, XrmValue *to,
1389 XtPointer *closure_ret)
1390 {
1391 Screen *screen;
1392 Colormap cmap;
1393 Pixel pixel;
1394 String color_name;
1395 XColor color;
1396
1397 if (*nargs != 2)
1398 {
1399 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1400 "wrongParameters", "cvt_string_to_pixel",
1401 "XtToolkitError",
1402 "Screen and colormap args required", NULL, NULL);
1403 return False;
1404 }
1405
1406 screen = *(Screen **) args[0].addr;
1407 cmap = *(Colormap *) args[1].addr;
1408 color_name = (String) from->addr;
1409
1410 if (strcmp (color_name, XtDefaultBackground) == 0)
1411 {
1412 *closure_ret = (XtPointer) False;
1413 pixel = WhitePixelOfScreen (screen);
1414 }
1415 else if (strcmp (color_name, XtDefaultForeground) == 0)
1416 {
1417 *closure_ret = (XtPointer) False;
1418 pixel = BlackPixelOfScreen (screen);
1419 }
1420 else if (XParseColor (dpy, cmap, color_name, &color)
1421 && x_alloc_nearest_color_1 (dpy, cmap, &color))
1422 {
1423 pixel = color.pixel;
1424 *closure_ret = (XtPointer) True;
1425 }
1426 else
1427 {
1428 String params[1];
1429 Cardinal nparams = 1;
1430
1431 params[0] = color_name;
1432 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1433 "badValue", "cvt_string_to_pixel",
1434 "XtToolkitError", "Invalid color `%s'",
1435 params, &nparams);
1436 return False;
1437 }
1438
1439 if (to->addr != NULL)
1440 {
1441 if (to->size < sizeof (Pixel))
1442 {
1443 to->size = sizeof (Pixel);
1444 return False;
1445 }
1446
1447 *(Pixel *) to->addr = pixel;
1448 }
1449 else
1450 {
1451 cvt_string_to_pixel_value = pixel;
1452 to->addr = (XtPointer) &cvt_string_to_pixel_value;
1453 }
1454
1455 to->size = sizeof (Pixel);
1456 return True;
1457 }
1458
1459
1460 /* Free a pixel color which was previously allocated via
1461 cvt_string_to_pixel. This is registered as the destructor
1462 for this type of resource via XtSetTypeConverter.
1463
1464 APP is the application context in which we work.
1465
1466 TO is a pointer to an XrmValue holding the color to free.
1467 CLOSURE is the value we stored in CLOSURE_RET for this color
1468 in cvt_string_to_pixel.
1469
1470 ARGS and NARGS are like for cvt_string_to_pixel. */
1471
1472 static void
1473 cvt_pixel_dtor (XtAppContext app, XrmValuePtr to, XtPointer closure, XrmValuePtr args,
1474 Cardinal *nargs)
1475 {
1476 if (*nargs != 2)
1477 {
1478 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
1479 "XtToolkitError",
1480 "Screen and colormap arguments required",
1481 NULL, NULL);
1482 }
1483 else if (closure != NULL)
1484 {
1485 /* We did allocate the pixel, so free it. */
1486 Screen *screen = *(Screen **) args[0].addr;
1487 Colormap cmap = *(Colormap *) args[1].addr;
1488 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
1489 (Pixel *) to->addr, 1);
1490 }
1491 }
1492
1493
1494 #endif /* USE_X_TOOLKIT */
1495
1496
1497 /* Value is an array of XColor structures for the contents of the
1498 color map of display DPY. Set *NCELLS to the size of the array.
1499 Note that this probably shouldn't be called for large color maps,
1500 say a 24-bit TrueColor map. */
1501
1502 static const XColor *
1503 x_color_cells (Display *dpy, int *ncells)
1504 {
1505 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1506
1507 if (dpyinfo->color_cells == NULL)
1508 {
1509 Screen *screen = dpyinfo->screen;
1510 int ncolor_cells = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
1511 int i;
1512
1513 dpyinfo->color_cells = xnmalloc (ncolor_cells,
1514 sizeof *dpyinfo->color_cells);
1515 dpyinfo->ncolor_cells = ncolor_cells;
1516
1517 for (i = 0; i < ncolor_cells; ++i)
1518 dpyinfo->color_cells[i].pixel = i;
1519
1520 XQueryColors (dpy, dpyinfo->cmap,
1521 dpyinfo->color_cells, ncolor_cells);
1522 }
1523
1524 *ncells = dpyinfo->ncolor_cells;
1525 return dpyinfo->color_cells;
1526 }
1527
1528
1529 /* On frame F, translate pixel colors to RGB values for the NCOLORS
1530 colors in COLORS. Use cached information, if available. */
1531
1532 void
1533 x_query_colors (struct frame *f, XColor *colors, int ncolors)
1534 {
1535 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
1536
1537 if (dpyinfo->color_cells)
1538 {
1539 int i;
1540 for (i = 0; i < ncolors; ++i)
1541 {
1542 unsigned long pixel = colors[i].pixel;
1543 eassert (pixel < dpyinfo->ncolor_cells);
1544 eassert (dpyinfo->color_cells[pixel].pixel == pixel);
1545 colors[i] = dpyinfo->color_cells[pixel];
1546 }
1547 }
1548 else
1549 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
1550 }
1551
1552
1553 /* On frame F, translate pixel color to RGB values for the color in
1554 COLOR. Use cached information, if available. */
1555
1556 void
1557 x_query_color (struct frame *f, XColor *color)
1558 {
1559 x_query_colors (f, color, 1);
1560 }
1561
1562
1563 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
1564 exact match can't be allocated, try the nearest color available.
1565 Value is true if successful. Set *COLOR to the color
1566 allocated. */
1567
1568 static bool
1569 x_alloc_nearest_color_1 (Display *dpy, Colormap cmap, XColor *color)
1570 {
1571 bool rc;
1572
1573 rc = XAllocColor (dpy, cmap, color) != 0;
1574 if (rc == 0)
1575 {
1576 /* If we got to this point, the colormap is full, so we're going
1577 to try to get the next closest color. The algorithm used is
1578 a least-squares matching, which is what X uses for closest
1579 color matching with StaticColor visuals. */
1580 int nearest, i;
1581 int max_color_delta = 255;
1582 int max_delta = 3 * max_color_delta;
1583 int nearest_delta = max_delta + 1;
1584 int ncells;
1585 const XColor *cells = x_color_cells (dpy, &ncells);
1586
1587 for (nearest = i = 0; i < ncells; ++i)
1588 {
1589 int dred = (color->red >> 8) - (cells[i].red >> 8);
1590 int dgreen = (color->green >> 8) - (cells[i].green >> 8);
1591 int dblue = (color->blue >> 8) - (cells[i].blue >> 8);
1592 int delta = dred * dred + dgreen * dgreen + dblue * dblue;
1593
1594 if (delta < nearest_delta)
1595 {
1596 nearest = i;
1597 nearest_delta = delta;
1598 }
1599 }
1600
1601 color->red = cells[nearest].red;
1602 color->green = cells[nearest].green;
1603 color->blue = cells[nearest].blue;
1604 rc = XAllocColor (dpy, cmap, color) != 0;
1605 }
1606 else
1607 {
1608 /* If allocation succeeded, and the allocated pixel color is not
1609 equal to a cached pixel color recorded earlier, there was a
1610 change in the colormap, so clear the color cache. */
1611 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1612 XColor *cached_color;
1613
1614 if (dpyinfo->color_cells
1615 && (cached_color = &dpyinfo->color_cells[color->pixel],
1616 (cached_color->red != color->red
1617 || cached_color->blue != color->blue
1618 || cached_color->green != color->green)))
1619 {
1620 xfree (dpyinfo->color_cells);
1621 dpyinfo->color_cells = NULL;
1622 dpyinfo->ncolor_cells = 0;
1623 }
1624 }
1625
1626 #ifdef DEBUG_X_COLORS
1627 if (rc)
1628 register_color (color->pixel);
1629 #endif /* DEBUG_X_COLORS */
1630
1631 return rc;
1632 }
1633
1634
1635 /* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
1636 exact match can't be allocated, try the nearest color available.
1637 Value is true if successful. Set *COLOR to the color
1638 allocated. */
1639
1640 bool
1641 x_alloc_nearest_color (struct frame *f, Colormap cmap, XColor *color)
1642 {
1643 gamma_correct (f, color);
1644 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
1645 }
1646
1647
1648 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
1649 It's necessary to do this instead of just using PIXEL directly to
1650 get color reference counts right. */
1651
1652 unsigned long
1653 x_copy_color (struct frame *f, long unsigned int pixel)
1654 {
1655 XColor color;
1656
1657 color.pixel = pixel;
1658 block_input ();
1659 x_query_color (f, &color);
1660 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
1661 unblock_input ();
1662 #ifdef DEBUG_X_COLORS
1663 register_color (pixel);
1664 #endif
1665 return color.pixel;
1666 }
1667
1668
1669 /* Brightness beyond which a color won't have its highlight brightness
1670 boosted.
1671
1672 Nominally, highlight colors for `3d' faces are calculated by
1673 brightening an object's color by a constant scale factor, but this
1674 doesn't yield good results for dark colors, so for colors who's
1675 brightness is less than this value (on a scale of 0-65535) have an
1676 use an additional additive factor.
1677
1678 The value here is set so that the default menu-bar/mode-line color
1679 (grey75) will not have its highlights changed at all. */
1680 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
1681
1682
1683 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1684 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1685 If this produces the same color as PIXEL, try a color where all RGB
1686 values have DELTA added. Return the allocated color in *PIXEL.
1687 DISPLAY is the X display, CMAP is the colormap to operate on.
1688 Value is non-zero if successful. */
1689
1690 static bool
1691 x_alloc_lighter_color (struct frame *f, Display *display, Colormap cmap, long unsigned int *pixel, double factor, int delta)
1692 {
1693 XColor color, new;
1694 long bright;
1695 bool success_p;
1696
1697 /* Get RGB color values. */
1698 color.pixel = *pixel;
1699 x_query_color (f, &color);
1700
1701 /* Change RGB values by specified FACTOR. Avoid overflow! */
1702 eassert (factor >= 0);
1703 new.red = min (0xffff, factor * color.red);
1704 new.green = min (0xffff, factor * color.green);
1705 new.blue = min (0xffff, factor * color.blue);
1706
1707 /* Calculate brightness of COLOR. */
1708 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
1709
1710 /* We only boost colors that are darker than
1711 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
1712 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
1713 /* Make an additive adjustment to NEW, because it's dark enough so
1714 that scaling by FACTOR alone isn't enough. */
1715 {
1716 /* How far below the limit this color is (0 - 1, 1 being darker). */
1717 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
1718 /* The additive adjustment. */
1719 int min_delta = delta * dimness * factor / 2;
1720
1721 if (factor < 1)
1722 {
1723 new.red = max (0, new.red - min_delta);
1724 new.green = max (0, new.green - min_delta);
1725 new.blue = max (0, new.blue - min_delta);
1726 }
1727 else
1728 {
1729 new.red = min (0xffff, min_delta + new.red);
1730 new.green = min (0xffff, min_delta + new.green);
1731 new.blue = min (0xffff, min_delta + new.blue);
1732 }
1733 }
1734
1735 /* Try to allocate the color. */
1736 success_p = x_alloc_nearest_color (f, cmap, &new);
1737 if (success_p)
1738 {
1739 if (new.pixel == *pixel)
1740 {
1741 /* If we end up with the same color as before, try adding
1742 delta to the RGB values. */
1743 x_free_colors (f, &new.pixel, 1);
1744
1745 new.red = min (0xffff, delta + color.red);
1746 new.green = min (0xffff, delta + color.green);
1747 new.blue = min (0xffff, delta + color.blue);
1748 success_p = x_alloc_nearest_color (f, cmap, &new);
1749 }
1750 else
1751 success_p = 1;
1752 *pixel = new.pixel;
1753 }
1754
1755 return success_p;
1756 }
1757
1758
1759 /* Set up the foreground color for drawing relief lines of glyph
1760 string S. RELIEF is a pointer to a struct relief containing the GC
1761 with which lines will be drawn. Use a color that is FACTOR or
1762 DELTA lighter or darker than the relief's background which is found
1763 in S->f->output_data.x->relief_background. If such a color cannot
1764 be allocated, use DEFAULT_PIXEL, instead. */
1765
1766 static void
1767 x_setup_relief_color (struct frame *f, struct relief *relief, double factor, int delta, long unsigned int default_pixel)
1768 {
1769 XGCValues xgcv;
1770 struct x_output *di = f->output_data.x;
1771 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
1772 unsigned long pixel;
1773 unsigned long background = di->relief_background;
1774 Colormap cmap = FRAME_X_COLORMAP (f);
1775 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
1776 Display *dpy = FRAME_X_DISPLAY (f);
1777
1778 xgcv.graphics_exposures = False;
1779 xgcv.line_width = 1;
1780
1781 /* Free previously allocated color. The color cell will be reused
1782 when it has been freed as many times as it was allocated, so this
1783 doesn't affect faces using the same colors. */
1784 if (relief->gc
1785 && relief->allocated_p)
1786 {
1787 x_free_colors (f, &relief->pixel, 1);
1788 relief->allocated_p = 0;
1789 }
1790
1791 /* Allocate new color. */
1792 xgcv.foreground = default_pixel;
1793 pixel = background;
1794 if (dpyinfo->n_planes != 1
1795 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
1796 {
1797 relief->allocated_p = 1;
1798 xgcv.foreground = relief->pixel = pixel;
1799 }
1800
1801 if (relief->gc == 0)
1802 {
1803 xgcv.stipple = dpyinfo->gray;
1804 mask |= GCStipple;
1805 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
1806 }
1807 else
1808 XChangeGC (dpy, relief->gc, mask, &xgcv);
1809 }
1810
1811
1812 /* Set up colors for the relief lines around glyph string S. */
1813
1814 static void
1815 x_setup_relief_colors (struct glyph_string *s)
1816 {
1817 struct x_output *di = s->f->output_data.x;
1818 unsigned long color;
1819
1820 if (s->face->use_box_color_for_shadows_p)
1821 color = s->face->box_color;
1822 else if (s->first_glyph->type == IMAGE_GLYPH
1823 && s->img->pixmap
1824 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
1825 color = IMAGE_BACKGROUND (s->img, s->f, 0);
1826 else
1827 {
1828 XGCValues xgcv;
1829
1830 /* Get the background color of the face. */
1831 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
1832 color = xgcv.background;
1833 }
1834
1835 if (di->white_relief.gc == 0
1836 || color != di->relief_background)
1837 {
1838 di->relief_background = color;
1839 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
1840 WHITE_PIX_DEFAULT (s->f));
1841 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
1842 BLACK_PIX_DEFAULT (s->f));
1843 }
1844 }
1845
1846
1847 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
1848 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
1849 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
1850 relief. LEFT_P non-zero means draw a relief on the left side of
1851 the rectangle. RIGHT_P non-zero means draw a relief on the right
1852 side of the rectangle. CLIP_RECT is the clipping rectangle to use
1853 when drawing. */
1854
1855 static void
1856 x_draw_relief_rect (struct frame *f,
1857 int left_x, int top_y, int right_x, int bottom_y, int width,
1858 int raised_p, int top_p, int bot_p, int left_p, int right_p,
1859 XRectangle *clip_rect)
1860 {
1861 Display *dpy = FRAME_X_DISPLAY (f);
1862 Window window = FRAME_X_WINDOW (f);
1863 int i;
1864 GC gc;
1865
1866 if (raised_p)
1867 gc = f->output_data.x->white_relief.gc;
1868 else
1869 gc = f->output_data.x->black_relief.gc;
1870 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
1871
1872 /* This code is more complicated than it has to be, because of two
1873 minor hacks to make the boxes look nicer: (i) if width > 1, draw
1874 the outermost line using the black relief. (ii) Omit the four
1875 corner pixels. */
1876
1877 /* Top. */
1878 if (top_p)
1879 {
1880 if (width == 1)
1881 XDrawLine (dpy, window, gc,
1882 left_x + (left_p ? 1 : 0), top_y,
1883 right_x + (right_p ? 0 : 1), top_y);
1884
1885 for (i = 1; i < width; ++i)
1886 XDrawLine (dpy, window, gc,
1887 left_x + i * left_p, top_y + i,
1888 right_x + 1 - i * right_p, top_y + i);
1889 }
1890
1891 /* Left. */
1892 if (left_p)
1893 {
1894 if (width == 1)
1895 XDrawLine (dpy, window, gc, left_x, top_y + 1, left_x, bottom_y);
1896
1897 XClearArea (dpy, window, left_x, top_y, 1, 1, False);
1898 XClearArea (dpy, window, left_x, bottom_y, 1, 1, False);
1899
1900 for (i = (width > 1 ? 1 : 0); i < width; ++i)
1901 XDrawLine (dpy, window, gc,
1902 left_x + i, top_y + (i + 1) * top_p,
1903 left_x + i, bottom_y + 1 - (i + 1) * bot_p);
1904 }
1905
1906 XSetClipMask (dpy, gc, None);
1907 if (raised_p)
1908 gc = f->output_data.x->black_relief.gc;
1909 else
1910 gc = f->output_data.x->white_relief.gc;
1911 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
1912
1913 if (width > 1)
1914 {
1915 /* Outermost top line. */
1916 if (top_p)
1917 XDrawLine (dpy, window, gc,
1918 left_x + (left_p ? 1 : 0), top_y,
1919 right_x + (right_p ? 0 : 1), top_y);
1920
1921 /* Outermost left line. */
1922 if (left_p)
1923 XDrawLine (dpy, window, gc, left_x, top_y + 1, left_x, bottom_y);
1924 }
1925
1926 /* Bottom. */
1927 if (bot_p)
1928 {
1929 XDrawLine (dpy, window, gc,
1930 left_x + (left_p ? 1 : 0), bottom_y,
1931 right_x + (right_p ? 0 : 1), bottom_y);
1932 for (i = 1; i < width; ++i)
1933 XDrawLine (dpy, window, gc,
1934 left_x + i * left_p, bottom_y - i,
1935 right_x + 1 - i * right_p, bottom_y - i);
1936 }
1937
1938 /* Right. */
1939 if (right_p)
1940 {
1941 XClearArea (dpy, window, right_x, top_y, 1, 1, False);
1942 XClearArea (dpy, window, right_x, bottom_y, 1, 1, False);
1943 for (i = 0; i < width; ++i)
1944 XDrawLine (dpy, window, gc,
1945 right_x - i, top_y + (i + 1) * top_p,
1946 right_x - i, bottom_y + 1 - (i + 1) * bot_p);
1947 }
1948
1949 XSetClipMask (dpy, gc, None);
1950 }
1951
1952
1953 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
1954 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
1955 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
1956 left side of the rectangle. RIGHT_P non-zero means draw a line
1957 on the right side of the rectangle. CLIP_RECT is the clipping
1958 rectangle to use when drawing. */
1959
1960 static void
1961 x_draw_box_rect (struct glyph_string *s,
1962 int left_x, int top_y, int right_x, int bottom_y, int width,
1963 int left_p, int right_p, XRectangle *clip_rect)
1964 {
1965 XGCValues xgcv;
1966
1967 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
1968 XSetForeground (s->display, s->gc, s->face->box_color);
1969 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
1970
1971 /* Top. */
1972 XFillRectangle (s->display, s->window, s->gc,
1973 left_x, top_y, right_x - left_x + 1, width);
1974
1975 /* Left. */
1976 if (left_p)
1977 XFillRectangle (s->display, s->window, s->gc,
1978 left_x, top_y, width, bottom_y - top_y + 1);
1979
1980 /* Bottom. */
1981 XFillRectangle (s->display, s->window, s->gc,
1982 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
1983
1984 /* Right. */
1985 if (right_p)
1986 XFillRectangle (s->display, s->window, s->gc,
1987 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
1988
1989 XSetForeground (s->display, s->gc, xgcv.foreground);
1990 XSetClipMask (s->display, s->gc, None);
1991 }
1992
1993
1994 /* Draw a box around glyph string S. */
1995
1996 static void
1997 x_draw_glyph_string_box (struct glyph_string *s)
1998 {
1999 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
2000 int left_p, right_p;
2001 struct glyph *last_glyph;
2002 XRectangle clip_rect;
2003
2004 last_x = ((s->row->full_width_p && !s->w->pseudo_window_p)
2005 ? WINDOW_RIGHT_EDGE_X (s->w)
2006 : window_box_right (s->w, s->area));
2007
2008 /* The glyph that may have a right box line. */
2009 last_glyph = (s->cmp || s->img
2010 ? s->first_glyph
2011 : s->first_glyph + s->nchars - 1);
2012
2013 width = eabs (s->face->box_line_width);
2014 raised_p = s->face->box == FACE_RAISED_BOX;
2015 left_x = s->x;
2016 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
2017 ? last_x - 1
2018 : min (last_x, s->x + s->background_width) - 1);
2019 top_y = s->y;
2020 bottom_y = top_y + s->height - 1;
2021
2022 left_p = (s->first_glyph->left_box_line_p
2023 || (s->hl == DRAW_MOUSE_FACE
2024 && (s->prev == NULL
2025 || s->prev->hl != s->hl)));
2026 right_p = (last_glyph->right_box_line_p
2027 || (s->hl == DRAW_MOUSE_FACE
2028 && (s->next == NULL
2029 || s->next->hl != s->hl)));
2030
2031 get_glyph_string_clip_rect (s, &clip_rect);
2032
2033 if (s->face->box == FACE_SIMPLE_BOX)
2034 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2035 left_p, right_p, &clip_rect);
2036 else
2037 {
2038 x_setup_relief_colors (s);
2039 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
2040 width, raised_p, 1, 1, left_p, right_p, &clip_rect);
2041 }
2042 }
2043
2044
2045 /* Draw foreground of image glyph string S. */
2046
2047 static void
2048 x_draw_image_foreground (struct glyph_string *s)
2049 {
2050 int x = s->x;
2051 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2052
2053 /* If first glyph of S has a left box line, start drawing it to the
2054 right of that line. */
2055 if (s->face->box != FACE_NO_BOX
2056 && s->first_glyph->left_box_line_p
2057 && s->slice.x == 0)
2058 x += eabs (s->face->box_line_width);
2059
2060 /* If there is a margin around the image, adjust x- and y-position
2061 by that margin. */
2062 if (s->slice.x == 0)
2063 x += s->img->hmargin;
2064 if (s->slice.y == 0)
2065 y += s->img->vmargin;
2066
2067 if (s->img->pixmap)
2068 {
2069 if (s->img->mask)
2070 {
2071 /* We can't set both a clip mask and use XSetClipRectangles
2072 because the latter also sets a clip mask. We also can't
2073 trust on the shape extension to be available
2074 (XShapeCombineRegion). So, compute the rectangle to draw
2075 manually. */
2076 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2077 | GCFunction);
2078 XGCValues xgcv;
2079 XRectangle clip_rect, image_rect, r;
2080
2081 xgcv.clip_mask = s->img->mask;
2082 xgcv.clip_x_origin = x;
2083 xgcv.clip_y_origin = y;
2084 xgcv.function = GXcopy;
2085 XChangeGC (s->display, s->gc, mask, &xgcv);
2086
2087 get_glyph_string_clip_rect (s, &clip_rect);
2088 image_rect.x = x;
2089 image_rect.y = y;
2090 image_rect.width = s->slice.width;
2091 image_rect.height = s->slice.height;
2092 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2093 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2094 s->slice.x + r.x - x, s->slice.y + r.y - y,
2095 r.width, r.height, r.x, r.y);
2096 }
2097 else
2098 {
2099 XRectangle clip_rect, image_rect, r;
2100
2101 get_glyph_string_clip_rect (s, &clip_rect);
2102 image_rect.x = x;
2103 image_rect.y = y;
2104 image_rect.width = s->slice.width;
2105 image_rect.height = s->slice.height;
2106 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2107 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2108 s->slice.x + r.x - x, s->slice.y + r.y - y,
2109 r.width, r.height, r.x, r.y);
2110
2111 /* When the image has a mask, we can expect that at
2112 least part of a mouse highlight or a block cursor will
2113 be visible. If the image doesn't have a mask, make
2114 a block cursor visible by drawing a rectangle around
2115 the image. I believe it's looking better if we do
2116 nothing here for mouse-face. */
2117 if (s->hl == DRAW_CURSOR)
2118 {
2119 int relief = eabs (s->img->relief);
2120 XDrawRectangle (s->display, s->window, s->gc,
2121 x - relief, y - relief,
2122 s->slice.width + relief*2 - 1,
2123 s->slice.height + relief*2 - 1);
2124 }
2125 }
2126 }
2127 else
2128 /* Draw a rectangle if image could not be loaded. */
2129 XDrawRectangle (s->display, s->window, s->gc, x, y,
2130 s->slice.width - 1, s->slice.height - 1);
2131 }
2132
2133
2134 /* Draw a relief around the image glyph string S. */
2135
2136 static void
2137 x_draw_image_relief (struct glyph_string *s)
2138 {
2139 int x1, y1, thick, raised_p, top_p, bot_p, left_p, right_p;
2140 XRectangle r;
2141 int x = s->x;
2142 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2143
2144 /* If first glyph of S has a left box line, start drawing it to the
2145 right of that line. */
2146 if (s->face->box != FACE_NO_BOX
2147 && s->first_glyph->left_box_line_p
2148 && s->slice.x == 0)
2149 x += eabs (s->face->box_line_width);
2150
2151 /* If there is a margin around the image, adjust x- and y-position
2152 by that margin. */
2153 if (s->slice.x == 0)
2154 x += s->img->hmargin;
2155 if (s->slice.y == 0)
2156 y += s->img->vmargin;
2157
2158 if (s->hl == DRAW_IMAGE_SUNKEN
2159 || s->hl == DRAW_IMAGE_RAISED)
2160 {
2161 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
2162 raised_p = s->hl == DRAW_IMAGE_RAISED;
2163 }
2164 else
2165 {
2166 thick = eabs (s->img->relief);
2167 raised_p = s->img->relief > 0;
2168 }
2169
2170 x1 = x + s->slice.width - 1;
2171 y1 = y + s->slice.height - 1;
2172 top_p = bot_p = left_p = right_p = 0;
2173
2174 if (s->slice.x == 0)
2175 x -= thick, left_p = 1;
2176 if (s->slice.y == 0)
2177 y -= thick, top_p = 1;
2178 if (s->slice.x + s->slice.width == s->img->width)
2179 x1 += thick, right_p = 1;
2180 if (s->slice.y + s->slice.height == s->img->height)
2181 y1 += thick, bot_p = 1;
2182
2183 x_setup_relief_colors (s);
2184 get_glyph_string_clip_rect (s, &r);
2185 x_draw_relief_rect (s->f, x, y, x1, y1, thick, raised_p,
2186 top_p, bot_p, left_p, right_p, &r);
2187 }
2188
2189
2190 /* Draw the foreground of image glyph string S to PIXMAP. */
2191
2192 static void
2193 x_draw_image_foreground_1 (struct glyph_string *s, Pixmap pixmap)
2194 {
2195 int x = 0;
2196 int y = s->ybase - s->y - image_ascent (s->img, s->face, &s->slice);
2197
2198 /* If first glyph of S has a left box line, start drawing it to the
2199 right of that line. */
2200 if (s->face->box != FACE_NO_BOX
2201 && s->first_glyph->left_box_line_p
2202 && s->slice.x == 0)
2203 x += eabs (s->face->box_line_width);
2204
2205 /* If there is a margin around the image, adjust x- and y-position
2206 by that margin. */
2207 if (s->slice.x == 0)
2208 x += s->img->hmargin;
2209 if (s->slice.y == 0)
2210 y += s->img->vmargin;
2211
2212 if (s->img->pixmap)
2213 {
2214 if (s->img->mask)
2215 {
2216 /* We can't set both a clip mask and use XSetClipRectangles
2217 because the latter also sets a clip mask. We also can't
2218 trust on the shape extension to be available
2219 (XShapeCombineRegion). So, compute the rectangle to draw
2220 manually. */
2221 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2222 | GCFunction);
2223 XGCValues xgcv;
2224
2225 xgcv.clip_mask = s->img->mask;
2226 xgcv.clip_x_origin = x - s->slice.x;
2227 xgcv.clip_y_origin = y - s->slice.y;
2228 xgcv.function = GXcopy;
2229 XChangeGC (s->display, s->gc, mask, &xgcv);
2230
2231 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2232 s->slice.x, s->slice.y,
2233 s->slice.width, s->slice.height, x, y);
2234 XSetClipMask (s->display, s->gc, None);
2235 }
2236 else
2237 {
2238 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2239 s->slice.x, s->slice.y,
2240 s->slice.width, s->slice.height, x, y);
2241
2242 /* When the image has a mask, we can expect that at
2243 least part of a mouse highlight or a block cursor will
2244 be visible. If the image doesn't have a mask, make
2245 a block cursor visible by drawing a rectangle around
2246 the image. I believe it's looking better if we do
2247 nothing here for mouse-face. */
2248 if (s->hl == DRAW_CURSOR)
2249 {
2250 int r = eabs (s->img->relief);
2251 XDrawRectangle (s->display, s->window, s->gc, x - r, y - r,
2252 s->slice.width + r*2 - 1,
2253 s->slice.height + r*2 - 1);
2254 }
2255 }
2256 }
2257 else
2258 /* Draw a rectangle if image could not be loaded. */
2259 XDrawRectangle (s->display, pixmap, s->gc, x, y,
2260 s->slice.width - 1, s->slice.height - 1);
2261 }
2262
2263
2264 /* Draw part of the background of glyph string S. X, Y, W, and H
2265 give the rectangle to draw. */
2266
2267 static void
2268 x_draw_glyph_string_bg_rect (struct glyph_string *s, int x, int y, int w, int h)
2269 {
2270 if (s->stippled_p)
2271 {
2272 /* Fill background with a stipple pattern. */
2273 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2274 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
2275 XSetFillStyle (s->display, s->gc, FillSolid);
2276 }
2277 else
2278 x_clear_glyph_string_rect (s, x, y, w, h);
2279 }
2280
2281
2282 /* Draw image glyph string S.
2283
2284 s->y
2285 s->x +-------------------------
2286 | s->face->box
2287 |
2288 | +-------------------------
2289 | | s->img->margin
2290 | |
2291 | | +-------------------
2292 | | | the image
2293
2294 */
2295
2296 static void
2297 x_draw_image_glyph_string (struct glyph_string *s)
2298 {
2299 int box_line_hwidth = eabs (s->face->box_line_width);
2300 int box_line_vwidth = max (s->face->box_line_width, 0);
2301 int height;
2302 Pixmap pixmap = None;
2303
2304 height = s->height;
2305 if (s->slice.y == 0)
2306 height -= box_line_vwidth;
2307 if (s->slice.y + s->slice.height >= s->img->height)
2308 height -= box_line_vwidth;
2309
2310 /* Fill background with face under the image. Do it only if row is
2311 taller than image or if image has a clip mask to reduce
2312 flickering. */
2313 s->stippled_p = s->face->stipple != 0;
2314 if (height > s->slice.height
2315 || s->img->hmargin
2316 || s->img->vmargin
2317 || s->img->mask
2318 || s->img->pixmap == 0
2319 || s->width != s->background_width)
2320 {
2321 if (s->img->mask)
2322 {
2323 /* Create a pixmap as large as the glyph string. Fill it
2324 with the background color. Copy the image to it, using
2325 its mask. Copy the temporary pixmap to the display. */
2326 Screen *screen = FRAME_X_SCREEN (s->f);
2327 int depth = DefaultDepthOfScreen (screen);
2328
2329 /* Create a pixmap as large as the glyph string. */
2330 pixmap = XCreatePixmap (s->display, s->window,
2331 s->background_width,
2332 s->height, depth);
2333
2334 /* Don't clip in the following because we're working on the
2335 pixmap. */
2336 XSetClipMask (s->display, s->gc, None);
2337
2338 /* Fill the pixmap with the background color/stipple. */
2339 if (s->stippled_p)
2340 {
2341 /* Fill background with a stipple pattern. */
2342 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2343 XSetTSOrigin (s->display, s->gc, - s->x, - s->y);
2344 XFillRectangle (s->display, pixmap, s->gc,
2345 0, 0, s->background_width, s->height);
2346 XSetFillStyle (s->display, s->gc, FillSolid);
2347 XSetTSOrigin (s->display, s->gc, 0, 0);
2348 }
2349 else
2350 {
2351 XGCValues xgcv;
2352 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
2353 &xgcv);
2354 XSetForeground (s->display, s->gc, xgcv.background);
2355 XFillRectangle (s->display, pixmap, s->gc,
2356 0, 0, s->background_width, s->height);
2357 XSetForeground (s->display, s->gc, xgcv.foreground);
2358 }
2359 }
2360 else
2361 {
2362 int x = s->x;
2363 int y = s->y;
2364
2365 if (s->first_glyph->left_box_line_p
2366 && s->slice.x == 0)
2367 x += box_line_hwidth;
2368
2369 if (s->slice.y == 0)
2370 y += box_line_vwidth;
2371
2372 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
2373 }
2374
2375 s->background_filled_p = 1;
2376 }
2377
2378 /* Draw the foreground. */
2379 if (pixmap != None)
2380 {
2381 x_draw_image_foreground_1 (s, pixmap);
2382 x_set_glyph_string_clipping (s);
2383 XCopyArea (s->display, pixmap, s->window, s->gc,
2384 0, 0, s->background_width, s->height, s->x, s->y);
2385 XFreePixmap (s->display, pixmap);
2386 }
2387 else
2388 x_draw_image_foreground (s);
2389
2390 /* If we must draw a relief around the image, do it. */
2391 if (s->img->relief
2392 || s->hl == DRAW_IMAGE_RAISED
2393 || s->hl == DRAW_IMAGE_SUNKEN)
2394 x_draw_image_relief (s);
2395 }
2396
2397
2398 /* Draw stretch glyph string S. */
2399
2400 static void
2401 x_draw_stretch_glyph_string (struct glyph_string *s)
2402 {
2403 eassert (s->first_glyph->type == STRETCH_GLYPH);
2404
2405 if (s->hl == DRAW_CURSOR
2406 && !x_stretch_cursor_p)
2407 {
2408 /* If `x-stretch-cursor' is nil, don't draw a block cursor as
2409 wide as the stretch glyph. */
2410 int width, background_width = s->background_width;
2411 int x = s->x;
2412
2413 if (!s->row->reversed_p)
2414 {
2415 int left_x = window_box_left_offset (s->w, TEXT_AREA);
2416
2417 if (x < left_x)
2418 {
2419 background_width -= left_x - x;
2420 x = left_x;
2421 }
2422 }
2423 else
2424 {
2425 /* In R2L rows, draw the cursor on the right edge of the
2426 stretch glyph. */
2427 int right_x = window_box_right_offset (s->w, TEXT_AREA);
2428
2429 if (x + background_width > right_x)
2430 background_width -= x - right_x;
2431 x += background_width;
2432 }
2433 width = min (FRAME_COLUMN_WIDTH (s->f), background_width);
2434 if (s->row->reversed_p)
2435 x -= width;
2436
2437 /* Draw cursor. */
2438 x_draw_glyph_string_bg_rect (s, x, s->y, width, s->height);
2439
2440 /* Clear rest using the GC of the original non-cursor face. */
2441 if (width < background_width)
2442 {
2443 int y = s->y;
2444 int w = background_width - width, h = s->height;
2445 XRectangle r;
2446 GC gc;
2447
2448 if (!s->row->reversed_p)
2449 x += width;
2450 else
2451 x = s->x;
2452 if (s->row->mouse_face_p
2453 && cursor_in_mouse_face_p (s->w))
2454 {
2455 x_set_mouse_face_gc (s);
2456 gc = s->gc;
2457 }
2458 else
2459 gc = s->face->gc;
2460
2461 get_glyph_string_clip_rect (s, &r);
2462 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
2463
2464 if (s->face->stipple)
2465 {
2466 /* Fill background with a stipple pattern. */
2467 XSetFillStyle (s->display, gc, FillOpaqueStippled);
2468 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2469 XSetFillStyle (s->display, gc, FillSolid);
2470 }
2471 else
2472 {
2473 XGCValues xgcv;
2474 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
2475 XSetForeground (s->display, gc, xgcv.background);
2476 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2477 XSetForeground (s->display, gc, xgcv.foreground);
2478 }
2479 }
2480 }
2481 else if (!s->background_filled_p)
2482 {
2483 int background_width = s->background_width;
2484 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2485
2486 /* Don't draw into left margin, fringe or scrollbar area
2487 except for header line and mode line. */
2488 if (x < left_x && !s->row->mode_line_p)
2489 {
2490 background_width -= left_x - x;
2491 x = left_x;
2492 }
2493 if (background_width > 0)
2494 x_draw_glyph_string_bg_rect (s, x, s->y, background_width, s->height);
2495 }
2496
2497 s->background_filled_p = 1;
2498 }
2499
2500 /*
2501 Draw a wavy line under S. The wave fills wave_height pixels from y0.
2502
2503 x0 wave_length = 2
2504 --
2505 y0 * * * * *
2506 |* * * * * * * * *
2507 wave_height = 3 | * * * *
2508
2509 */
2510
2511 static void
2512 x_draw_underwave (struct glyph_string *s)
2513 {
2514 int wave_height = 3, wave_length = 2;
2515 int dx, dy, x0, y0, width, x1, y1, x2, y2, odd, xmax;
2516 XRectangle wave_clip, string_clip, final_clip;
2517
2518 dx = wave_length;
2519 dy = wave_height - 1;
2520 x0 = s->x;
2521 y0 = s->ybase - wave_height + 3;
2522 width = s->width;
2523 xmax = x0 + width;
2524
2525 /* Find and set clipping rectangle */
2526
2527 wave_clip.x = x0;
2528 wave_clip.y = y0;
2529 wave_clip.width = width;
2530 wave_clip.height = wave_height;
2531 get_glyph_string_clip_rect (s, &string_clip);
2532
2533 if (!x_intersect_rectangles (&wave_clip, &string_clip, &final_clip))
2534 return;
2535
2536 XSetClipRectangles (s->display, s->gc, 0, 0, &final_clip, 1, Unsorted);
2537
2538 /* Draw the waves */
2539
2540 x1 = x0 - (x0 % dx);
2541 x2 = x1 + dx;
2542 odd = (x1/dx) % 2;
2543 y1 = y2 = y0;
2544
2545 if (odd)
2546 y1 += dy;
2547 else
2548 y2 += dy;
2549
2550 if (INT_MAX - dx < xmax)
2551 emacs_abort ();
2552
2553 while (x1 <= xmax)
2554 {
2555 XDrawLine (s->display, s->window, s->gc, x1, y1, x2, y2);
2556 x1 = x2, y1 = y2;
2557 x2 += dx, y2 = y0 + odd*dy;
2558 odd = !odd;
2559 }
2560
2561 /* Restore previous clipping rectangle(s) */
2562 XSetClipRectangles (s->display, s->gc, 0, 0, s->clip, s->num_clips, Unsorted);
2563 }
2564
2565
2566 /* Draw glyph string S. */
2567
2568 static void
2569 x_draw_glyph_string (struct glyph_string *s)
2570 {
2571 bool relief_drawn_p = 0;
2572
2573 /* If S draws into the background of its successors, draw the
2574 background of the successors first so that S can draw into it.
2575 This makes S->next use XDrawString instead of XDrawImageString. */
2576 if (s->next && s->right_overhang && !s->for_overlaps)
2577 {
2578 int width;
2579 struct glyph_string *next;
2580
2581 for (width = 0, next = s->next;
2582 next && width < s->right_overhang;
2583 width += next->width, next = next->next)
2584 if (next->first_glyph->type != IMAGE_GLYPH)
2585 {
2586 x_set_glyph_string_gc (next);
2587 x_set_glyph_string_clipping (next);
2588 if (next->first_glyph->type == STRETCH_GLYPH)
2589 x_draw_stretch_glyph_string (next);
2590 else
2591 x_draw_glyph_string_background (next, 1);
2592 next->num_clips = 0;
2593 }
2594 }
2595
2596 /* Set up S->gc, set clipping and draw S. */
2597 x_set_glyph_string_gc (s);
2598
2599 /* Draw relief (if any) in advance for char/composition so that the
2600 glyph string can be drawn over it. */
2601 if (!s->for_overlaps
2602 && s->face->box != FACE_NO_BOX
2603 && (s->first_glyph->type == CHAR_GLYPH
2604 || s->first_glyph->type == COMPOSITE_GLYPH))
2605
2606 {
2607 x_set_glyph_string_clipping (s);
2608 x_draw_glyph_string_background (s, 1);
2609 x_draw_glyph_string_box (s);
2610 x_set_glyph_string_clipping (s);
2611 relief_drawn_p = 1;
2612 }
2613 else if (!s->clip_head /* draw_glyphs didn't specify a clip mask. */
2614 && !s->clip_tail
2615 && ((s->prev && s->prev->hl != s->hl && s->left_overhang)
2616 || (s->next && s->next->hl != s->hl && s->right_overhang)))
2617 /* We must clip just this glyph. left_overhang part has already
2618 drawn when s->prev was drawn, and right_overhang part will be
2619 drawn later when s->next is drawn. */
2620 x_set_glyph_string_clipping_exactly (s, s);
2621 else
2622 x_set_glyph_string_clipping (s);
2623
2624 switch (s->first_glyph->type)
2625 {
2626 case IMAGE_GLYPH:
2627 x_draw_image_glyph_string (s);
2628 break;
2629
2630 case STRETCH_GLYPH:
2631 x_draw_stretch_glyph_string (s);
2632 break;
2633
2634 case CHAR_GLYPH:
2635 if (s->for_overlaps)
2636 s->background_filled_p = 1;
2637 else
2638 x_draw_glyph_string_background (s, 0);
2639 x_draw_glyph_string_foreground (s);
2640 break;
2641
2642 case COMPOSITE_GLYPH:
2643 if (s->for_overlaps || (s->cmp_from > 0
2644 && ! s->first_glyph->u.cmp.automatic))
2645 s->background_filled_p = 1;
2646 else
2647 x_draw_glyph_string_background (s, 1);
2648 x_draw_composite_glyph_string_foreground (s);
2649 break;
2650
2651 case GLYPHLESS_GLYPH:
2652 if (s->for_overlaps)
2653 s->background_filled_p = 1;
2654 else
2655 x_draw_glyph_string_background (s, 1);
2656 x_draw_glyphless_glyph_string_foreground (s);
2657 break;
2658
2659 default:
2660 emacs_abort ();
2661 }
2662
2663 if (!s->for_overlaps)
2664 {
2665 /* Draw underline. */
2666 if (s->face->underline_p)
2667 {
2668 if (s->face->underline_type == FACE_UNDER_WAVE)
2669 {
2670 if (s->face->underline_defaulted_p)
2671 x_draw_underwave (s);
2672 else
2673 {
2674 XGCValues xgcv;
2675 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2676 XSetForeground (s->display, s->gc, s->face->underline_color);
2677 x_draw_underwave (s);
2678 XSetForeground (s->display, s->gc, xgcv.foreground);
2679 }
2680 }
2681 else if (s->face->underline_type == FACE_UNDER_LINE)
2682 {
2683 unsigned long thickness, position;
2684 int y;
2685
2686 if (s->prev && s->prev->face->underline_p
2687 && s->prev->face->underline_type == FACE_UNDER_LINE)
2688 {
2689 /* We use the same underline style as the previous one. */
2690 thickness = s->prev->underline_thickness;
2691 position = s->prev->underline_position;
2692 }
2693 else
2694 {
2695 /* Get the underline thickness. Default is 1 pixel. */
2696 if (s->font && s->font->underline_thickness > 0)
2697 thickness = s->font->underline_thickness;
2698 else
2699 thickness = 1;
2700 if (x_underline_at_descent_line)
2701 position = (s->height - thickness) - (s->ybase - s->y);
2702 else
2703 {
2704 /* Get the underline position. This is the recommended
2705 vertical offset in pixels from the baseline to the top of
2706 the underline. This is a signed value according to the
2707 specs, and its default is
2708
2709 ROUND ((maximum descent) / 2), with
2710 ROUND(x) = floor (x + 0.5) */
2711
2712 if (x_use_underline_position_properties
2713 && s->font && s->font->underline_position >= 0)
2714 position = s->font->underline_position;
2715 else if (s->font)
2716 position = (s->font->descent + 1) / 2;
2717 else
2718 position = underline_minimum_offset;
2719 }
2720 position = max (position, underline_minimum_offset);
2721 }
2722 /* Check the sanity of thickness and position. We should
2723 avoid drawing underline out of the current line area. */
2724 if (s->y + s->height <= s->ybase + position)
2725 position = (s->height - 1) - (s->ybase - s->y);
2726 if (s->y + s->height < s->ybase + position + thickness)
2727 thickness = (s->y + s->height) - (s->ybase + position);
2728 s->underline_thickness = thickness;
2729 s->underline_position = position;
2730 y = s->ybase + position;
2731 if (s->face->underline_defaulted_p)
2732 XFillRectangle (s->display, s->window, s->gc,
2733 s->x, y, s->width, thickness);
2734 else
2735 {
2736 XGCValues xgcv;
2737 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2738 XSetForeground (s->display, s->gc, s->face->underline_color);
2739 XFillRectangle (s->display, s->window, s->gc,
2740 s->x, y, s->width, thickness);
2741 XSetForeground (s->display, s->gc, xgcv.foreground);
2742 }
2743 }
2744 }
2745 /* Draw overline. */
2746 if (s->face->overline_p)
2747 {
2748 unsigned long dy = 0, h = 1;
2749
2750 if (s->face->overline_color_defaulted_p)
2751 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2752 s->width, h);
2753 else
2754 {
2755 XGCValues xgcv;
2756 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2757 XSetForeground (s->display, s->gc, s->face->overline_color);
2758 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2759 s->width, h);
2760 XSetForeground (s->display, s->gc, xgcv.foreground);
2761 }
2762 }
2763
2764 /* Draw strike-through. */
2765 if (s->face->strike_through_p)
2766 {
2767 unsigned long h = 1;
2768 unsigned long dy = (s->height - h) / 2;
2769
2770 if (s->face->strike_through_color_defaulted_p)
2771 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2772 s->width, h);
2773 else
2774 {
2775 XGCValues xgcv;
2776 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2777 XSetForeground (s->display, s->gc, s->face->strike_through_color);
2778 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2779 s->width, h);
2780 XSetForeground (s->display, s->gc, xgcv.foreground);
2781 }
2782 }
2783
2784 /* Draw relief if not yet drawn. */
2785 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
2786 x_draw_glyph_string_box (s);
2787
2788 if (s->prev)
2789 {
2790 struct glyph_string *prev;
2791
2792 for (prev = s->prev; prev; prev = prev->prev)
2793 if (prev->hl != s->hl
2794 && prev->x + prev->width + prev->right_overhang > s->x)
2795 {
2796 /* As prev was drawn while clipped to its own area, we
2797 must draw the right_overhang part using s->hl now. */
2798 enum draw_glyphs_face save = prev->hl;
2799
2800 prev->hl = s->hl;
2801 x_set_glyph_string_gc (prev);
2802 x_set_glyph_string_clipping_exactly (s, prev);
2803 if (prev->first_glyph->type == CHAR_GLYPH)
2804 x_draw_glyph_string_foreground (prev);
2805 else
2806 x_draw_composite_glyph_string_foreground (prev);
2807 XSetClipMask (prev->display, prev->gc, None);
2808 prev->hl = save;
2809 prev->num_clips = 0;
2810 }
2811 }
2812
2813 if (s->next)
2814 {
2815 struct glyph_string *next;
2816
2817 for (next = s->next; next; next = next->next)
2818 if (next->hl != s->hl
2819 && next->x - next->left_overhang < s->x + s->width)
2820 {
2821 /* As next will be drawn while clipped to its own area,
2822 we must draw the left_overhang part using s->hl now. */
2823 enum draw_glyphs_face save = next->hl;
2824
2825 next->hl = s->hl;
2826 x_set_glyph_string_gc (next);
2827 x_set_glyph_string_clipping_exactly (s, next);
2828 if (next->first_glyph->type == CHAR_GLYPH)
2829 x_draw_glyph_string_foreground (next);
2830 else
2831 x_draw_composite_glyph_string_foreground (next);
2832 XSetClipMask (next->display, next->gc, None);
2833 next->hl = save;
2834 next->num_clips = 0;
2835 next->clip_head = s->next;
2836 }
2837 }
2838 }
2839
2840 /* Reset clipping. */
2841 XSetClipMask (s->display, s->gc, None);
2842 s->num_clips = 0;
2843 }
2844
2845 /* Shift display to make room for inserted glyphs. */
2846
2847 static void
2848 x_shift_glyphs_for_insert (struct frame *f, int x, int y, int width, int height, int shift_by)
2849 {
2850 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
2851 f->output_data.x->normal_gc,
2852 x, y, width, height,
2853 x + shift_by, y);
2854 }
2855
2856 /* Delete N glyphs at the nominal cursor position. Not implemented
2857 for X frames. */
2858
2859 static void
2860 x_delete_glyphs (struct frame *f, register int n)
2861 {
2862 emacs_abort ();
2863 }
2864
2865
2866 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
2867 If they are <= 0, this is probably an error. */
2868
2869 void
2870 x_clear_area (Display *dpy, Window window, int x, int y, int width, int height)
2871 {
2872 eassert (width > 0 && height > 0);
2873 XClearArea (dpy, window, x, y, width, height, False);
2874 }
2875
2876
2877 /* Clear an entire frame. */
2878
2879 static void
2880 x_clear_frame (struct frame *f)
2881 {
2882 /* Clearing the frame will erase any cursor, so mark them all as no
2883 longer visible. */
2884 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
2885
2886 block_input ();
2887
2888 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
2889
2890 /* We have to clear the scroll bars. If we have changed colors or
2891 something like that, then they should be notified. */
2892 x_scroll_bar_clear (f);
2893
2894 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
2895 /* Make sure scroll bars are redrawn. As they aren't redrawn by
2896 redisplay, do it here. */
2897 if (FRAME_GTK_WIDGET (f))
2898 gtk_widget_queue_draw (FRAME_GTK_WIDGET (f));
2899 #endif
2900
2901 XFlush (FRAME_X_DISPLAY (f));
2902
2903 unblock_input ();
2904 }
2905
2906
2907 \f
2908 /* Invert the middle quarter of the frame for .15 sec. */
2909
2910 static void
2911 XTflash (struct frame *f)
2912 {
2913 block_input ();
2914
2915 {
2916 #ifdef USE_GTK
2917 /* Use Gdk routines to draw. This way, we won't draw over scroll bars
2918 when the scroll bars and the edit widget share the same X window. */
2919 GdkWindow *window = gtk_widget_get_window (FRAME_GTK_WIDGET (f));
2920 #ifdef HAVE_GTK3
2921 cairo_t *cr = gdk_cairo_create (window);
2922 cairo_set_source_rgb (cr, 1, 1, 1);
2923 cairo_set_operator (cr, CAIRO_OPERATOR_DIFFERENCE);
2924 #define XFillRectangle(d, win, gc, x, y, w, h) \
2925 do { \
2926 cairo_rectangle (cr, x, y, w, h); \
2927 cairo_fill (cr); \
2928 } \
2929 while (0)
2930 #else /* ! HAVE_GTK3 */
2931 GdkGCValues vals;
2932 GdkGC *gc;
2933 vals.foreground.pixel = (FRAME_FOREGROUND_PIXEL (f)
2934 ^ FRAME_BACKGROUND_PIXEL (f));
2935 vals.function = GDK_XOR;
2936 gc = gdk_gc_new_with_values (window,
2937 &vals, GDK_GC_FUNCTION | GDK_GC_FOREGROUND);
2938 #define XFillRectangle(d, win, gc, x, y, w, h) \
2939 gdk_draw_rectangle (window, gc, TRUE, x, y, w, h)
2940 #endif /* ! HAVE_GTK3 */
2941 #else /* ! USE_GTK */
2942 GC gc;
2943
2944 /* Create a GC that will use the GXxor function to flip foreground
2945 pixels into background pixels. */
2946 {
2947 XGCValues values;
2948
2949 values.function = GXxor;
2950 values.foreground = (FRAME_FOREGROUND_PIXEL (f)
2951 ^ FRAME_BACKGROUND_PIXEL (f));
2952
2953 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2954 GCFunction | GCForeground, &values);
2955 }
2956 #endif
2957 {
2958 /* Get the height not including a menu bar widget. */
2959 int height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, FRAME_LINES (f));
2960 /* Height of each line to flash. */
2961 int flash_height = FRAME_LINE_HEIGHT (f);
2962 /* These will be the left and right margins of the rectangles. */
2963 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
2964 int flash_right = FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
2965 int width = flash_right - flash_left;
2966
2967 /* If window is tall, flash top and bottom line. */
2968 if (height > 3 * FRAME_LINE_HEIGHT (f))
2969 {
2970 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2971 flash_left,
2972 (FRAME_INTERNAL_BORDER_WIDTH (f)
2973 + FRAME_TOP_MARGIN_HEIGHT (f)),
2974 width, flash_height);
2975 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2976 flash_left,
2977 (height - flash_height
2978 - FRAME_INTERNAL_BORDER_WIDTH (f)),
2979 width, flash_height);
2980
2981 }
2982 else
2983 /* If it is short, flash it all. */
2984 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2985 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
2986 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
2987
2988 x_flush (f);
2989
2990 {
2991 struct timespec delay = make_timespec (0, 150 * 1000 * 1000);
2992 struct timespec wakeup = timespec_add (current_timespec (), delay);
2993
2994 /* Keep waiting until past the time wakeup or any input gets
2995 available. */
2996 while (! detect_input_pending ())
2997 {
2998 struct timespec current = current_timespec ();
2999 struct timespec timeout;
3000
3001 /* Break if result would not be positive. */
3002 if (timespec_cmp (wakeup, current) <= 0)
3003 break;
3004
3005 /* How long `select' should wait. */
3006 timeout = make_timespec (0, 10 * 1000 * 1000);
3007
3008 /* Try to wait that long--but we might wake up sooner. */
3009 pselect (0, NULL, NULL, NULL, &timeout, NULL);
3010 }
3011 }
3012
3013 /* If window is tall, flash top and bottom line. */
3014 if (height > 3 * FRAME_LINE_HEIGHT (f))
3015 {
3016 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3017 flash_left,
3018 (FRAME_INTERNAL_BORDER_WIDTH (f)
3019 + FRAME_TOP_MARGIN_HEIGHT (f)),
3020 width, flash_height);
3021 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3022 flash_left,
3023 (height - flash_height
3024 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3025 width, flash_height);
3026 }
3027 else
3028 /* If it is short, flash it all. */
3029 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3030 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3031 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3032
3033 #ifdef USE_GTK
3034 #ifdef HAVE_GTK3
3035 cairo_destroy (cr);
3036 #else
3037 g_object_unref (G_OBJECT (gc));
3038 #endif
3039 #undef XFillRectangle
3040 #else
3041 XFreeGC (FRAME_X_DISPLAY (f), gc);
3042 #endif
3043 x_flush (f);
3044 }
3045 }
3046
3047 unblock_input ();
3048 }
3049
3050
3051 static void
3052 XTtoggle_invisible_pointer (struct frame *f, int invisible)
3053 {
3054 block_input ();
3055 if (invisible)
3056 {
3057 if (FRAME_DISPLAY_INFO (f)->invisible_cursor != 0)
3058 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3059 FRAME_DISPLAY_INFO (f)->invisible_cursor);
3060 }
3061 else
3062 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3063 f->output_data.x->current_cursor);
3064 f->pointer_invisible = invisible;
3065 unblock_input ();
3066 }
3067
3068
3069 /* Make audible bell. */
3070
3071 static void
3072 XTring_bell (struct frame *f)
3073 {
3074 if (FRAME_X_DISPLAY (f))
3075 {
3076 if (visible_bell)
3077 XTflash (f);
3078 else
3079 {
3080 block_input ();
3081 #ifdef HAVE_XKB
3082 XkbBell (FRAME_X_DISPLAY (f), None, 0, None);
3083 #else
3084 XBell (FRAME_X_DISPLAY (f), 0);
3085 #endif
3086 XFlush (FRAME_X_DISPLAY (f));
3087 unblock_input ();
3088 }
3089 }
3090 }
3091
3092 /***********************************************************************
3093 Line Dance
3094 ***********************************************************************/
3095
3096 /* Perform an insert-lines or delete-lines operation, inserting N
3097 lines or deleting -N lines at vertical position VPOS. */
3098
3099 static void
3100 x_ins_del_lines (struct frame *f, int vpos, int n)
3101 {
3102 emacs_abort ();
3103 }
3104
3105
3106 /* Scroll part of the display as described by RUN. */
3107
3108 static void
3109 x_scroll_run (struct window *w, struct run *run)
3110 {
3111 struct frame *f = XFRAME (w->frame);
3112 int x, y, width, height, from_y, to_y, bottom_y;
3113
3114 /* Get frame-relative bounding box of the text display area of W,
3115 without mode lines. Include in this box the left and right
3116 fringe of W. */
3117 window_box (w, ANY_AREA, &x, &y, &width, &height);
3118
3119 #ifdef USE_TOOLKIT_SCROLL_BARS
3120 /* If the fringe is adjacent to the left (right) scroll bar of a
3121 leftmost (rightmost, respectively) window, then extend its
3122 background to the gap between the fringe and the bar. */
3123 if ((WINDOW_LEFTMOST_P (w)
3124 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
3125 || (WINDOW_RIGHTMOST_P (w)
3126 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w)))
3127 {
3128 int sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
3129
3130 if (sb_width > 0)
3131 {
3132 int bar_area_x = WINDOW_SCROLL_BAR_AREA_X (w);
3133 int bar_area_width = (WINDOW_CONFIG_SCROLL_BAR_COLS (w)
3134 * FRAME_COLUMN_WIDTH (f));
3135
3136 if (bar_area_x + bar_area_width == x)
3137 {
3138 x = bar_area_x + sb_width;
3139 width += bar_area_width - sb_width;
3140 }
3141 else if (x + width == bar_area_x)
3142 width += bar_area_width - sb_width;
3143 }
3144 }
3145 #endif
3146
3147 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
3148 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
3149 bottom_y = y + height;
3150
3151 if (to_y < from_y)
3152 {
3153 /* Scrolling up. Make sure we don't copy part of the mode
3154 line at the bottom. */
3155 if (from_y + run->height > bottom_y)
3156 height = bottom_y - from_y;
3157 else
3158 height = run->height;
3159 }
3160 else
3161 {
3162 /* Scrolling down. Make sure we don't copy over the mode line.
3163 at the bottom. */
3164 if (to_y + run->height > bottom_y)
3165 height = bottom_y - to_y;
3166 else
3167 height = run->height;
3168 }
3169
3170 block_input ();
3171
3172 /* Cursor off. Will be switched on again in x_update_window_end. */
3173 x_clear_cursor (w);
3174
3175 XCopyArea (FRAME_X_DISPLAY (f),
3176 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
3177 f->output_data.x->normal_gc,
3178 x, from_y,
3179 width, height,
3180 x, to_y);
3181
3182 unblock_input ();
3183 }
3184
3185
3186 \f
3187 /***********************************************************************
3188 Exposure Events
3189 ***********************************************************************/
3190
3191 \f
3192 static void
3193 frame_highlight (struct frame *f)
3194 {
3195 /* We used to only do this if Vx_no_window_manager was non-nil, but
3196 the ICCCM (section 4.1.6) says that the window's border pixmap
3197 and border pixel are window attributes which are "private to the
3198 client", so we can always change it to whatever we want. */
3199 block_input ();
3200 /* I recently started to get errors in this XSetWindowBorder, depending on
3201 the window-manager in use, tho something more is at play since I've been
3202 using that same window-manager binary for ever. Let's not crash just
3203 because of this (bug#9310). */
3204 x_catch_errors (FRAME_X_DISPLAY (f));
3205 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3206 f->output_data.x->border_pixel);
3207 x_uncatch_errors ();
3208 unblock_input ();
3209 x_update_cursor (f, 1);
3210 x_set_frame_alpha (f);
3211 }
3212
3213 static void
3214 frame_unhighlight (struct frame *f)
3215 {
3216 /* We used to only do this if Vx_no_window_manager was non-nil, but
3217 the ICCCM (section 4.1.6) says that the window's border pixmap
3218 and border pixel are window attributes which are "private to the
3219 client", so we can always change it to whatever we want. */
3220 block_input ();
3221 /* Same as above for XSetWindowBorder (bug#9310). */
3222 x_catch_errors (FRAME_X_DISPLAY (f));
3223 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3224 f->output_data.x->border_tile);
3225 x_uncatch_errors ();
3226 unblock_input ();
3227 x_update_cursor (f, 1);
3228 x_set_frame_alpha (f);
3229 }
3230
3231 /* The focus has changed. Update the frames as necessary to reflect
3232 the new situation. Note that we can't change the selected frame
3233 here, because the Lisp code we are interrupting might become confused.
3234 Each event gets marked with the frame in which it occurred, so the
3235 Lisp code can tell when the switch took place by examining the events. */
3236
3237 static void
3238 x_new_focus_frame (struct x_display_info *dpyinfo, struct frame *frame)
3239 {
3240 struct frame *old_focus = dpyinfo->x_focus_frame;
3241
3242 if (frame != dpyinfo->x_focus_frame)
3243 {
3244 /* Set this before calling other routines, so that they see
3245 the correct value of x_focus_frame. */
3246 dpyinfo->x_focus_frame = frame;
3247
3248 if (old_focus && old_focus->auto_lower)
3249 x_lower_frame (old_focus);
3250
3251 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
3252 dpyinfo->x_pending_autoraise_frame = dpyinfo->x_focus_frame;
3253 else
3254 dpyinfo->x_pending_autoraise_frame = NULL;
3255 }
3256
3257 x_frame_rehighlight (dpyinfo);
3258 }
3259
3260 /* Handle FocusIn and FocusOut state changes for FRAME.
3261 If FRAME has focus and there exists more than one frame, puts
3262 a FOCUS_IN_EVENT into *BUFP. */
3263
3264 static void
3265 x_focus_changed (int type, int state, struct x_display_info *dpyinfo, struct frame *frame, struct input_event *bufp)
3266 {
3267 if (type == FocusIn)
3268 {
3269 if (dpyinfo->x_focus_event_frame != frame)
3270 {
3271 x_new_focus_frame (dpyinfo, frame);
3272 dpyinfo->x_focus_event_frame = frame;
3273
3274 /* Don't stop displaying the initial startup message
3275 for a switch-frame event we don't need. */
3276 /* When run as a daemon, Vterminal_frame is always NIL. */
3277 bufp->arg = (((NILP (Vterminal_frame) || EQ (Fdaemonp (), Qt))
3278 && CONSP (Vframe_list)
3279 && !NILP (XCDR (Vframe_list)))
3280 ? Qt : Qnil);
3281 bufp->kind = FOCUS_IN_EVENT;
3282 XSETFRAME (bufp->frame_or_window, frame);
3283 }
3284
3285 frame->output_data.x->focus_state |= state;
3286
3287 #ifdef HAVE_X_I18N
3288 if (FRAME_XIC (frame))
3289 XSetICFocus (FRAME_XIC (frame));
3290 #endif
3291 }
3292 else if (type == FocusOut)
3293 {
3294 frame->output_data.x->focus_state &= ~state;
3295
3296 if (dpyinfo->x_focus_event_frame == frame)
3297 {
3298 dpyinfo->x_focus_event_frame = 0;
3299 x_new_focus_frame (dpyinfo, 0);
3300
3301 bufp->kind = FOCUS_OUT_EVENT;
3302 XSETFRAME (bufp->frame_or_window, frame);
3303 }
3304
3305 #ifdef HAVE_X_I18N
3306 if (FRAME_XIC (frame))
3307 XUnsetICFocus (FRAME_XIC (frame));
3308 #endif
3309 if (frame->pointer_invisible)
3310 XTtoggle_invisible_pointer (frame, 0);
3311 }
3312 }
3313
3314 /* Return the Emacs frame-object corresponding to an X window.
3315 It could be the frame's main window or an icon window. */
3316
3317 static struct frame *
3318 x_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
3319 {
3320 Lisp_Object tail, frame;
3321 struct frame *f;
3322
3323 if (wdesc == None)
3324 return NULL;
3325
3326 FOR_EACH_FRAME (tail, frame)
3327 {
3328 f = XFRAME (frame);
3329 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
3330 continue;
3331 if (f->output_data.x->hourglass_window == wdesc)
3332 return f;
3333 #ifdef USE_X_TOOLKIT
3334 if ((f->output_data.x->edit_widget
3335 && XtWindow (f->output_data.x->edit_widget) == wdesc)
3336 /* A tooltip frame? */
3337 || (!f->output_data.x->edit_widget
3338 && FRAME_X_WINDOW (f) == wdesc)
3339 || f->output_data.x->icon_desc == wdesc)
3340 return f;
3341 #else /* not USE_X_TOOLKIT */
3342 #ifdef USE_GTK
3343 if (f->output_data.x->edit_widget)
3344 {
3345 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
3346 struct x_output *x = f->output_data.x;
3347 if (gwdesc != 0 && gwdesc == x->edit_widget)
3348 return f;
3349 }
3350 #endif /* USE_GTK */
3351 if (FRAME_X_WINDOW (f) == wdesc
3352 || f->output_data.x->icon_desc == wdesc)
3353 return f;
3354 #endif /* not USE_X_TOOLKIT */
3355 }
3356 return 0;
3357 }
3358
3359 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
3360
3361 /* Like x_window_to_frame but also compares the window with the widget's
3362 windows. */
3363
3364 static struct frame *
3365 x_any_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
3366 {
3367 Lisp_Object tail, frame;
3368 struct frame *f, *found = NULL;
3369 struct x_output *x;
3370
3371 if (wdesc == None)
3372 return NULL;
3373
3374 FOR_EACH_FRAME (tail, frame)
3375 {
3376 if (found)
3377 break;
3378 f = XFRAME (frame);
3379 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo)
3380 {
3381 /* This frame matches if the window is any of its widgets. */
3382 x = f->output_data.x;
3383 if (x->hourglass_window == wdesc)
3384 found = f;
3385 else if (x->widget)
3386 {
3387 #ifdef USE_GTK
3388 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
3389 if (gwdesc != 0
3390 && gtk_widget_get_toplevel (gwdesc) == x->widget)
3391 found = f;
3392 #else
3393 if (wdesc == XtWindow (x->widget)
3394 || wdesc == XtWindow (x->column_widget)
3395 || wdesc == XtWindow (x->edit_widget))
3396 found = f;
3397 /* Match if the window is this frame's menubar. */
3398 else if (lw_window_is_in_menubar (wdesc, x->menubar_widget))
3399 found = f;
3400 #endif
3401 }
3402 else if (FRAME_X_WINDOW (f) == wdesc)
3403 /* A tooltip frame. */
3404 found = f;
3405 }
3406 }
3407
3408 return found;
3409 }
3410
3411 /* Likewise, but consider only the menu bar widget. */
3412
3413 static struct frame *
3414 x_menubar_window_to_frame (struct x_display_info *dpyinfo,
3415 const XEvent *event)
3416 {
3417 Window wdesc = event->xany.window;
3418 Lisp_Object tail, frame;
3419 struct frame *f;
3420 struct x_output *x;
3421
3422 if (wdesc == None)
3423 return NULL;
3424
3425 FOR_EACH_FRAME (tail, frame)
3426 {
3427 f = XFRAME (frame);
3428 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
3429 continue;
3430 x = f->output_data.x;
3431 #ifdef USE_GTK
3432 if (x->menubar_widget && xg_event_is_for_menubar (f, event))
3433 return f;
3434 #else
3435 /* Match if the window is this frame's menubar. */
3436 if (x->menubar_widget
3437 && lw_window_is_in_menubar (wdesc, x->menubar_widget))
3438 return f;
3439 #endif
3440 }
3441 return 0;
3442 }
3443
3444 /* Return the frame whose principal (outermost) window is WDESC.
3445 If WDESC is some other (smaller) window, we return 0. */
3446
3447 struct frame *
3448 x_top_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
3449 {
3450 Lisp_Object tail, frame;
3451 struct frame *f;
3452 struct x_output *x;
3453
3454 if (wdesc == None)
3455 return NULL;
3456
3457 FOR_EACH_FRAME (tail, frame)
3458 {
3459 f = XFRAME (frame);
3460 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
3461 continue;
3462 x = f->output_data.x;
3463
3464 if (x->widget)
3465 {
3466 /* This frame matches if the window is its topmost widget. */
3467 #ifdef USE_GTK
3468 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
3469 if (gwdesc == x->widget)
3470 return f;
3471 #else
3472 if (wdesc == XtWindow (x->widget))
3473 return f;
3474 #endif
3475 }
3476 else if (FRAME_X_WINDOW (f) == wdesc)
3477 /* Tooltip frame. */
3478 return f;
3479 }
3480 return 0;
3481 }
3482
3483 #else /* !USE_X_TOOLKIT && !USE_GTK */
3484
3485 #define x_any_window_to_frame(d, i) x_window_to_frame (d, i)
3486 #define x_top_window_to_frame(d, i) x_window_to_frame (d, i)
3487
3488 #endif /* USE_X_TOOLKIT || USE_GTK */
3489
3490 /* The focus may have changed. Figure out if it is a real focus change,
3491 by checking both FocusIn/Out and Enter/LeaveNotify events.
3492
3493 Returns FOCUS_IN_EVENT event in *BUFP. */
3494
3495 static void
3496 x_detect_focus_change (struct x_display_info *dpyinfo, struct frame *frame,
3497 const XEvent *event, struct input_event *bufp)
3498 {
3499 if (!frame)
3500 return;
3501
3502 switch (event->type)
3503 {
3504 case EnterNotify:
3505 case LeaveNotify:
3506 {
3507 struct frame *focus_frame = dpyinfo->x_focus_event_frame;
3508 int focus_state
3509 = focus_frame ? focus_frame->output_data.x->focus_state : 0;
3510
3511 if (event->xcrossing.detail != NotifyInferior
3512 && event->xcrossing.focus
3513 && ! (focus_state & FOCUS_EXPLICIT))
3514 x_focus_changed ((event->type == EnterNotify ? FocusIn : FocusOut),
3515 FOCUS_IMPLICIT,
3516 dpyinfo, frame, bufp);
3517 }
3518 break;
3519
3520 case FocusIn:
3521 case FocusOut:
3522 x_focus_changed (event->type,
3523 (event->xfocus.detail == NotifyPointer ?
3524 FOCUS_IMPLICIT : FOCUS_EXPLICIT),
3525 dpyinfo, frame, bufp);
3526 break;
3527
3528 case ClientMessage:
3529 if (event->xclient.message_type == dpyinfo->Xatom_XEMBED)
3530 {
3531 enum xembed_message msg = event->xclient.data.l[1];
3532 x_focus_changed ((msg == XEMBED_FOCUS_IN ? FocusIn : FocusOut),
3533 FOCUS_EXPLICIT, dpyinfo, frame, bufp);
3534 }
3535 break;
3536 }
3537 }
3538
3539
3540 #if defined HAVE_MENUS && !defined USE_X_TOOLKIT && !defined USE_GTK
3541 /* Handle an event saying the mouse has moved out of an Emacs frame. */
3542
3543 void
3544 x_mouse_leave (struct x_display_info *dpyinfo)
3545 {
3546 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
3547 }
3548 #endif
3549
3550 /* The focus has changed, or we have redirected a frame's focus to
3551 another frame (this happens when a frame uses a surrogate
3552 mini-buffer frame). Shift the highlight as appropriate.
3553
3554 The FRAME argument doesn't necessarily have anything to do with which
3555 frame is being highlighted or un-highlighted; we only use it to find
3556 the appropriate X display info. */
3557
3558 static void
3559 XTframe_rehighlight (struct frame *frame)
3560 {
3561 x_frame_rehighlight (FRAME_DISPLAY_INFO (frame));
3562 }
3563
3564 static void
3565 x_frame_rehighlight (struct x_display_info *dpyinfo)
3566 {
3567 struct frame *old_highlight = dpyinfo->x_highlight_frame;
3568
3569 if (dpyinfo->x_focus_frame)
3570 {
3571 dpyinfo->x_highlight_frame
3572 = ((FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
3573 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
3574 : dpyinfo->x_focus_frame);
3575 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
3576 {
3577 fset_focus_frame (dpyinfo->x_focus_frame, Qnil);
3578 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
3579 }
3580 }
3581 else
3582 dpyinfo->x_highlight_frame = 0;
3583
3584 if (dpyinfo->x_highlight_frame != old_highlight)
3585 {
3586 if (old_highlight)
3587 frame_unhighlight (old_highlight);
3588 if (dpyinfo->x_highlight_frame)
3589 frame_highlight (dpyinfo->x_highlight_frame);
3590 }
3591 }
3592
3593
3594 \f
3595 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
3596
3597 /* Initialize mode_switch_bit and modifier_meaning. */
3598 static void
3599 x_find_modifier_meanings (struct x_display_info *dpyinfo)
3600 {
3601 int min_code, max_code;
3602 KeySym *syms;
3603 int syms_per_code;
3604 XModifierKeymap *mods;
3605
3606 dpyinfo->meta_mod_mask = 0;
3607 dpyinfo->shift_lock_mask = 0;
3608 dpyinfo->alt_mod_mask = 0;
3609 dpyinfo->super_mod_mask = 0;
3610 dpyinfo->hyper_mod_mask = 0;
3611
3612 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
3613
3614 syms = XGetKeyboardMapping (dpyinfo->display,
3615 min_code, max_code - min_code + 1,
3616 &syms_per_code);
3617 mods = XGetModifierMapping (dpyinfo->display);
3618
3619 /* Scan the modifier table to see which modifier bits the Meta and
3620 Alt keysyms are on. */
3621 {
3622 int row, col; /* The row and column in the modifier table. */
3623 int found_alt_or_meta;
3624
3625 for (row = 3; row < 8; row++)
3626 {
3627 found_alt_or_meta = 0;
3628 for (col = 0; col < mods->max_keypermod; col++)
3629 {
3630 KeyCode code = mods->modifiermap[(row * mods->max_keypermod) + col];
3631
3632 /* Zeroes are used for filler. Skip them. */
3633 if (code == 0)
3634 continue;
3635
3636 /* Are any of this keycode's keysyms a meta key? */
3637 {
3638 int code_col;
3639
3640 for (code_col = 0; code_col < syms_per_code; code_col++)
3641 {
3642 int sym = syms[((code - min_code) * syms_per_code) + code_col];
3643
3644 switch (sym)
3645 {
3646 case XK_Meta_L:
3647 case XK_Meta_R:
3648 found_alt_or_meta = 1;
3649 dpyinfo->meta_mod_mask |= (1 << row);
3650 break;
3651
3652 case XK_Alt_L:
3653 case XK_Alt_R:
3654 found_alt_or_meta = 1;
3655 dpyinfo->alt_mod_mask |= (1 << row);
3656 break;
3657
3658 case XK_Hyper_L:
3659 case XK_Hyper_R:
3660 if (!found_alt_or_meta)
3661 dpyinfo->hyper_mod_mask |= (1 << row);
3662 code_col = syms_per_code;
3663 col = mods->max_keypermod;
3664 break;
3665
3666 case XK_Super_L:
3667 case XK_Super_R:
3668 if (!found_alt_or_meta)
3669 dpyinfo->super_mod_mask |= (1 << row);
3670 code_col = syms_per_code;
3671 col = mods->max_keypermod;
3672 break;
3673
3674 case XK_Shift_Lock:
3675 /* Ignore this if it's not on the lock modifier. */
3676 if (!found_alt_or_meta && ((1 << row) == LockMask))
3677 dpyinfo->shift_lock_mask = LockMask;
3678 code_col = syms_per_code;
3679 col = mods->max_keypermod;
3680 break;
3681 }
3682 }
3683 }
3684 }
3685 }
3686 }
3687
3688 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
3689 if (! dpyinfo->meta_mod_mask)
3690 {
3691 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
3692 dpyinfo->alt_mod_mask = 0;
3693 }
3694
3695 /* If some keys are both alt and meta,
3696 make them just meta, not alt. */
3697 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
3698 {
3699 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
3700 }
3701
3702 XFree (syms);
3703 XFreeModifiermap (mods);
3704 }
3705
3706 /* Convert between the modifier bits X uses and the modifier bits
3707 Emacs uses. */
3708
3709 int
3710 x_x_to_emacs_modifiers (struct x_display_info *dpyinfo, int state)
3711 {
3712 int mod_meta = meta_modifier;
3713 int mod_alt = alt_modifier;
3714 int mod_hyper = hyper_modifier;
3715 int mod_super = super_modifier;
3716 Lisp_Object tem;
3717
3718 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3719 if (INTEGERP (tem)) mod_alt = XINT (tem) & INT_MAX;
3720 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3721 if (INTEGERP (tem)) mod_meta = XINT (tem) & INT_MAX;
3722 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3723 if (INTEGERP (tem)) mod_hyper = XINT (tem) & INT_MAX;
3724 tem = Fget (Vx_super_keysym, Qmodifier_value);
3725 if (INTEGERP (tem)) mod_super = XINT (tem) & INT_MAX;
3726
3727 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
3728 | ((state & ControlMask) ? ctrl_modifier : 0)
3729 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
3730 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
3731 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
3732 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
3733 }
3734
3735 static int
3736 x_emacs_to_x_modifiers (struct x_display_info *dpyinfo, EMACS_INT state)
3737 {
3738 EMACS_INT mod_meta = meta_modifier;
3739 EMACS_INT mod_alt = alt_modifier;
3740 EMACS_INT mod_hyper = hyper_modifier;
3741 EMACS_INT mod_super = super_modifier;
3742
3743 Lisp_Object tem;
3744
3745 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3746 if (INTEGERP (tem)) mod_alt = XINT (tem);
3747 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3748 if (INTEGERP (tem)) mod_meta = XINT (tem);
3749 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3750 if (INTEGERP (tem)) mod_hyper = XINT (tem);
3751 tem = Fget (Vx_super_keysym, Qmodifier_value);
3752 if (INTEGERP (tem)) mod_super = XINT (tem);
3753
3754
3755 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
3756 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
3757 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
3758 | ((state & shift_modifier) ? ShiftMask : 0)
3759 | ((state & ctrl_modifier) ? ControlMask : 0)
3760 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
3761 }
3762
3763 /* Convert a keysym to its name. */
3764
3765 char *
3766 x_get_keysym_name (int keysym)
3767 {
3768 char *value;
3769
3770 block_input ();
3771 value = XKeysymToString (keysym);
3772 unblock_input ();
3773
3774 return value;
3775 }
3776
3777 /* Mouse clicks and mouse movement. Rah.
3778
3779 Formerly, we used PointerMotionHintMask (in standard_event_mask)
3780 so that we would have to call XQueryPointer after each MotionNotify
3781 event to ask for another such event. However, this made mouse tracking
3782 slow, and there was a bug that made it eventually stop.
3783
3784 Simply asking for MotionNotify all the time seems to work better.
3785
3786 In order to avoid asking for motion events and then throwing most
3787 of them away or busy-polling the server for mouse positions, we ask
3788 the server for pointer motion hints. This means that we get only
3789 one event per group of mouse movements. "Groups" are delimited by
3790 other kinds of events (focus changes and button clicks, for
3791 example), or by XQueryPointer calls; when one of these happens, we
3792 get another MotionNotify event the next time the mouse moves. This
3793 is at least as efficient as getting motion events when mouse
3794 tracking is on, and I suspect only negligibly worse when tracking
3795 is off. */
3796
3797 /* Prepare a mouse-event in *RESULT for placement in the input queue.
3798
3799 If the event is a button press, then note that we have grabbed
3800 the mouse. */
3801
3802 static Lisp_Object
3803 construct_mouse_click (struct input_event *result,
3804 const XButtonEvent *event,
3805 struct frame *f)
3806 {
3807 /* Make the event type NO_EVENT; we'll change that when we decide
3808 otherwise. */
3809 result->kind = MOUSE_CLICK_EVENT;
3810 result->code = event->button - Button1;
3811 result->timestamp = event->time;
3812 result->modifiers = (x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO (f),
3813 event->state)
3814 | (event->type == ButtonRelease
3815 ? up_modifier
3816 : down_modifier));
3817
3818 XSETINT (result->x, event->x);
3819 XSETINT (result->y, event->y);
3820 XSETFRAME (result->frame_or_window, f);
3821 result->arg = Qnil;
3822 return Qnil;
3823 }
3824
3825 /* Function to report a mouse movement to the mainstream Emacs code.
3826 The input handler calls this.
3827
3828 We have received a mouse movement event, which is given in *event.
3829 If the mouse is over a different glyph than it was last time, tell
3830 the mainstream emacs code by setting mouse_moved. If not, ask for
3831 another motion event, so we can check again the next time it moves. */
3832
3833 static int
3834 note_mouse_movement (struct frame *frame, const XMotionEvent *event)
3835 {
3836 XRectangle *r;
3837 struct x_display_info *dpyinfo;
3838
3839 if (!FRAME_X_OUTPUT (frame))
3840 return 0;
3841
3842 dpyinfo = FRAME_DISPLAY_INFO (frame);
3843 dpyinfo->last_mouse_movement_time = event->time;
3844 dpyinfo->last_mouse_motion_frame = frame;
3845 dpyinfo->last_mouse_motion_x = event->x;
3846 dpyinfo->last_mouse_motion_y = event->y;
3847
3848 if (event->window != FRAME_X_WINDOW (frame))
3849 {
3850 frame->mouse_moved = 1;
3851 dpyinfo->last_mouse_scroll_bar = NULL;
3852 note_mouse_highlight (frame, -1, -1);
3853 dpyinfo->last_mouse_glyph_frame = NULL;
3854 return 1;
3855 }
3856
3857
3858 /* Has the mouse moved off the glyph it was on at the last sighting? */
3859 r = &dpyinfo->last_mouse_glyph;
3860 if (frame != dpyinfo->last_mouse_glyph_frame
3861 || event->x < r->x || event->x >= r->x + r->width
3862 || event->y < r->y || event->y >= r->y + r->height)
3863 {
3864 frame->mouse_moved = 1;
3865 dpyinfo->last_mouse_scroll_bar = NULL;
3866 note_mouse_highlight (frame, event->x, event->y);
3867 /* Remember which glyph we're now on. */
3868 remember_mouse_glyph (frame, event->x, event->y, r);
3869 dpyinfo->last_mouse_glyph_frame = frame;
3870 return 1;
3871 }
3872
3873 return 0;
3874 }
3875
3876 /* Return the current position of the mouse.
3877 *FP should be a frame which indicates which display to ask about.
3878
3879 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
3880 and *PART to the frame, window, and scroll bar part that the mouse
3881 is over. Set *X and *Y to the portion and whole of the mouse's
3882 position on the scroll bar.
3883
3884 If the mouse movement started elsewhere, set *FP to the frame the
3885 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
3886 the mouse is over.
3887
3888 Set *TIMESTAMP to the server time-stamp for the time at which the mouse
3889 was at this position.
3890
3891 Don't store anything if we don't have a valid set of values to report.
3892
3893 This clears the mouse_moved flag, so we can wait for the next mouse
3894 movement. */
3895
3896 static void
3897 XTmouse_position (struct frame **fp, int insist, Lisp_Object *bar_window,
3898 enum scroll_bar_part *part, Lisp_Object *x, Lisp_Object *y,
3899 Time *timestamp)
3900 {
3901 struct frame *f1;
3902 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
3903
3904 block_input ();
3905
3906 if (dpyinfo->last_mouse_scroll_bar && insist == 0)
3907 x_scroll_bar_report_motion (fp, bar_window, part, x, y, timestamp);
3908 else
3909 {
3910 Window root;
3911 int root_x, root_y;
3912
3913 Window dummy_window;
3914 int dummy;
3915
3916 Lisp_Object frame, tail;
3917
3918 /* Clear the mouse-moved flag for every frame on this display. */
3919 FOR_EACH_FRAME (tail, frame)
3920 if (FRAME_X_P (XFRAME (frame))
3921 && FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
3922 XFRAME (frame)->mouse_moved = 0;
3923
3924 dpyinfo->last_mouse_scroll_bar = NULL;
3925
3926 /* Figure out which root window we're on. */
3927 XQueryPointer (FRAME_X_DISPLAY (*fp),
3928 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
3929
3930 /* The root window which contains the pointer. */
3931 &root,
3932
3933 /* Trash which we can't trust if the pointer is on
3934 a different screen. */
3935 &dummy_window,
3936
3937 /* The position on that root window. */
3938 &root_x, &root_y,
3939
3940 /* More trash we can't trust. */
3941 &dummy, &dummy,
3942
3943 /* Modifier keys and pointer buttons, about which
3944 we don't care. */
3945 (unsigned int *) &dummy);
3946
3947 /* Now we have a position on the root; find the innermost window
3948 containing the pointer. */
3949 {
3950 Window win, child;
3951 int win_x, win_y;
3952 int parent_x = 0, parent_y = 0;
3953
3954 win = root;
3955
3956 /* XTranslateCoordinates can get errors if the window
3957 structure is changing at the same time this function
3958 is running. So at least we must not crash from them. */
3959
3960 x_catch_errors (FRAME_X_DISPLAY (*fp));
3961
3962 if (x_mouse_grabbed (dpyinfo))
3963 {
3964 /* If mouse was grabbed on a frame, give coords for that frame
3965 even if the mouse is now outside it. */
3966 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
3967
3968 /* From-window. */
3969 root,
3970
3971 /* To-window. */
3972 FRAME_X_WINDOW (dpyinfo->last_mouse_frame),
3973
3974 /* From-position, to-position. */
3975 root_x, root_y, &win_x, &win_y,
3976
3977 /* Child of win. */
3978 &child);
3979 f1 = dpyinfo->last_mouse_frame;
3980 }
3981 else
3982 {
3983 while (1)
3984 {
3985 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
3986
3987 /* From-window, to-window. */
3988 root, win,
3989
3990 /* From-position, to-position. */
3991 root_x, root_y, &win_x, &win_y,
3992
3993 /* Child of win. */
3994 &child);
3995
3996 if (child == None || child == win)
3997 break;
3998 #ifdef USE_GTK
3999 /* We don't wan't to know the innermost window. We
4000 want the edit window. For non-Gtk+ the innermost
4001 window is the edit window. For Gtk+ it might not
4002 be. It might be the tool bar for example. */
4003 if (x_window_to_frame (dpyinfo, win))
4004 break;
4005 #endif
4006 win = child;
4007 parent_x = win_x;
4008 parent_y = win_y;
4009 }
4010
4011 /* Now we know that:
4012 win is the innermost window containing the pointer
4013 (XTC says it has no child containing the pointer),
4014 win_x and win_y are the pointer's position in it
4015 (XTC did this the last time through), and
4016 parent_x and parent_y are the pointer's position in win's parent.
4017 (They are what win_x and win_y were when win was child.
4018 If win is the root window, it has no parent, and
4019 parent_{x,y} are invalid, but that's okay, because we'll
4020 never use them in that case.) */
4021
4022 #ifdef USE_GTK
4023 /* We don't wan't to know the innermost window. We
4024 want the edit window. */
4025 f1 = x_window_to_frame (dpyinfo, win);
4026 #else
4027 /* Is win one of our frames? */
4028 f1 = x_any_window_to_frame (dpyinfo, win);
4029 #endif
4030
4031 #ifdef USE_X_TOOLKIT
4032 /* If we end up with the menu bar window, say it's not
4033 on the frame. */
4034 if (f1 != NULL
4035 && f1->output_data.x->menubar_widget
4036 && win == XtWindow (f1->output_data.x->menubar_widget))
4037 f1 = NULL;
4038 #endif /* USE_X_TOOLKIT */
4039 }
4040
4041 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
4042 f1 = 0;
4043
4044 x_uncatch_errors ();
4045
4046 /* If not, is it one of our scroll bars? */
4047 if (! f1)
4048 {
4049 struct scroll_bar *bar;
4050
4051 bar = x_window_to_scroll_bar (FRAME_X_DISPLAY (*fp), win);
4052
4053 if (bar)
4054 {
4055 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4056 win_x = parent_x;
4057 win_y = parent_y;
4058 }
4059 }
4060
4061 if (f1 == 0 && insist > 0)
4062 f1 = SELECTED_FRAME ();
4063
4064 if (f1)
4065 {
4066 /* Ok, we found a frame. Store all the values.
4067 last_mouse_glyph is a rectangle used to reduce the
4068 generation of mouse events. To not miss any motion
4069 events, we must divide the frame into rectangles of the
4070 size of the smallest character that could be displayed
4071 on it, i.e. into the same rectangles that matrices on
4072 the frame are divided into. */
4073
4074 /* FIXME: what if F1 is not an X frame? */
4075 dpyinfo = FRAME_DISPLAY_INFO (f1);
4076 remember_mouse_glyph (f1, win_x, win_y, &dpyinfo->last_mouse_glyph);
4077 dpyinfo->last_mouse_glyph_frame = f1;
4078
4079 *bar_window = Qnil;
4080 *part = 0;
4081 *fp = f1;
4082 XSETINT (*x, win_x);
4083 XSETINT (*y, win_y);
4084 *timestamp = dpyinfo->last_mouse_movement_time;
4085 }
4086 }
4087 }
4088
4089 unblock_input ();
4090 }
4091
4092
4093 \f
4094 /***********************************************************************
4095 Scroll bars
4096 ***********************************************************************/
4097
4098 /* Scroll bar support. */
4099
4100 /* Given an X window ID and a DISPLAY, find the struct scroll_bar which
4101 manages it.
4102 This can be called in GC, so we have to make sure to strip off mark
4103 bits. */
4104
4105 static struct scroll_bar *
4106 x_window_to_scroll_bar (Display *display, Window window_id)
4107 {
4108 Lisp_Object tail, frame;
4109
4110 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
4111 window_id = (Window) xg_get_scroll_id_for_window (display, window_id);
4112 #endif /* USE_GTK && USE_TOOLKIT_SCROLL_BARS */
4113
4114 FOR_EACH_FRAME (tail, frame)
4115 {
4116 Lisp_Object bar, condemned;
4117
4118 if (! FRAME_X_P (XFRAME (frame)))
4119 continue;
4120
4121 /* Scan this frame's scroll bar list for a scroll bar with the
4122 right window ID. */
4123 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
4124 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
4125 /* This trick allows us to search both the ordinary and
4126 condemned scroll bar lists with one loop. */
4127 ! NILP (bar) || (bar = condemned,
4128 condemned = Qnil,
4129 ! NILP (bar));
4130 bar = XSCROLL_BAR (bar)->next)
4131 if (XSCROLL_BAR (bar)->x_window == window_id &&
4132 FRAME_X_DISPLAY (XFRAME (frame)) == display)
4133 return XSCROLL_BAR (bar);
4134 }
4135
4136 return NULL;
4137 }
4138
4139
4140 #if defined USE_LUCID
4141
4142 /* Return the Lucid menu bar WINDOW is part of. Return null
4143 if WINDOW is not part of a menu bar. */
4144
4145 static Widget
4146 x_window_to_menu_bar (Window window)
4147 {
4148 Lisp_Object tail, frame;
4149
4150 FOR_EACH_FRAME (tail, frame)
4151 if (FRAME_X_P (XFRAME (frame)))
4152 {
4153 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
4154
4155 if (menu_bar && xlwmenu_window_p (menu_bar, window))
4156 return menu_bar;
4157 }
4158 return NULL;
4159 }
4160
4161 #endif /* USE_LUCID */
4162
4163 \f
4164 /************************************************************************
4165 Toolkit scroll bars
4166 ************************************************************************/
4167
4168 #ifdef USE_TOOLKIT_SCROLL_BARS
4169
4170 static void x_send_scroll_bar_event (Lisp_Object, int, int, int);
4171
4172 /* Lisp window being scrolled. Set when starting to interact with
4173 a toolkit scroll bar, reset to nil when ending the interaction. */
4174
4175 static Lisp_Object window_being_scrolled;
4176
4177 /* Whether this is an Xaw with arrow-scrollbars. This should imply
4178 that movements of 1/20 of the screen size are mapped to up/down. */
4179
4180 #ifndef USE_GTK
4181 /* Id of action hook installed for scroll bars. */
4182
4183 static XtActionHookId action_hook_id;
4184
4185 static Boolean xaw3d_arrow_scroll;
4186
4187 /* Whether the drag scrolling maintains the mouse at the top of the
4188 thumb. If not, resizing the thumb needs to be done more carefully
4189 to avoid jerkiness. */
4190
4191 static Boolean xaw3d_pick_top;
4192
4193 /* Action hook installed via XtAppAddActionHook when toolkit scroll
4194 bars are used.. The hook is responsible for detecting when
4195 the user ends an interaction with the scroll bar, and generates
4196 a `end-scroll' SCROLL_BAR_CLICK_EVENT' event if so. */
4197
4198 static void
4199 xt_action_hook (Widget widget, XtPointer client_data, String action_name,
4200 XEvent *event, String *params, Cardinal *num_params)
4201 {
4202 int scroll_bar_p;
4203 const char *end_action;
4204
4205 #ifdef USE_MOTIF
4206 scroll_bar_p = XmIsScrollBar (widget);
4207 end_action = "Release";
4208 #else /* !USE_MOTIF i.e. use Xaw */
4209 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
4210 end_action = "EndScroll";
4211 #endif /* USE_MOTIF */
4212
4213 if (scroll_bar_p
4214 && strcmp (action_name, end_action) == 0
4215 && WINDOWP (window_being_scrolled))
4216 {
4217 struct window *w;
4218 struct scroll_bar *bar;
4219
4220 x_send_scroll_bar_event (window_being_scrolled,
4221 scroll_bar_end_scroll, 0, 0);
4222 w = XWINDOW (window_being_scrolled);
4223 bar = XSCROLL_BAR (w->vertical_scroll_bar);
4224
4225 if (bar->dragging != -1)
4226 {
4227 bar->dragging = -1;
4228 /* The thumb size is incorrect while dragging: fix it. */
4229 set_vertical_scroll_bar (w);
4230 }
4231 window_being_scrolled = Qnil;
4232 #if defined (USE_LUCID)
4233 bar->last_seen_part = scroll_bar_nowhere;
4234 #endif
4235 /* Xt timeouts no longer needed. */
4236 toolkit_scroll_bar_interaction = 0;
4237 }
4238 }
4239 #endif /* not USE_GTK */
4240
4241 /* Send a client message with message type Xatom_Scrollbar for a
4242 scroll action to the frame of WINDOW. PART is a value identifying
4243 the part of the scroll bar that was clicked on. PORTION is the
4244 amount to scroll of a whole of WHOLE. */
4245
4246 static void
4247 x_send_scroll_bar_event (Lisp_Object window, int part, int portion, int whole)
4248 {
4249 XEvent event;
4250 XClientMessageEvent *ev = &event.xclient;
4251 struct window *w = XWINDOW (window);
4252 struct frame *f = XFRAME (w->frame);
4253
4254 block_input ();
4255
4256 /* Construct a ClientMessage event to send to the frame. */
4257 ev->type = ClientMessage;
4258 ev->message_type = FRAME_DISPLAY_INFO (f)->Xatom_Scrollbar;
4259 ev->display = FRAME_X_DISPLAY (f);
4260 ev->window = FRAME_X_WINDOW (f);
4261 ev->format = 32;
4262
4263 /* 32-bit X client on a 64-bit X server can pass window pointer
4264 as is. 64-bit client on a 32-bit X server is in trouble
4265 because pointer does not fit and will be truncated while
4266 passing through the server. So we should use two slots
4267 and hope that X12 will resolve such an issues someday. */
4268
4269 if (BITS_PER_LONG > 32)
4270 {
4271 union {
4272 int i[2];
4273 void *v;
4274 } val;
4275 val.v = w;
4276 ev->data.l[0] = val.i[0];
4277 ev->data.l[1] = val.i[1];
4278 }
4279 else
4280 {
4281 ev->data.l[0] = 0;
4282 ev->data.l[1] = (long) w;
4283 }
4284 ev->data.l[2] = part;
4285 ev->data.l[3] = portion;
4286 ev->data.l[4] = whole;
4287
4288 /* Make Xt timeouts work while the scroll bar is active. */
4289 #ifdef USE_X_TOOLKIT
4290 toolkit_scroll_bar_interaction = 1;
4291 x_activate_timeout_atimer ();
4292 #endif
4293
4294 /* Setting the event mask to zero means that the message will
4295 be sent to the client that created the window, and if that
4296 window no longer exists, no event will be sent. */
4297 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
4298 unblock_input ();
4299 }
4300
4301
4302 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
4303 in *IEVENT. */
4304
4305 static void
4306 x_scroll_bar_to_input_event (const XEvent *event,
4307 struct input_event *ievent)
4308 {
4309 const XClientMessageEvent *ev = &event->xclient;
4310 Lisp_Object window;
4311 struct window *w;
4312
4313 /* See the comment in the function above. */
4314
4315 if (BITS_PER_LONG > 32)
4316 {
4317 union {
4318 int i[2];
4319 void *v;
4320 } val;
4321 val.i[0] = ev->data.l[0];
4322 val.i[1] = ev->data.l[1];
4323 w = val.v;
4324 }
4325 else
4326 w = (void *) ev->data.l[1];
4327
4328 XSETWINDOW (window, w);
4329
4330 ievent->kind = SCROLL_BAR_CLICK_EVENT;
4331 ievent->frame_or_window = window;
4332 ievent->arg = Qnil;
4333 #ifdef USE_GTK
4334 ievent->timestamp = CurrentTime;
4335 #else
4336 ievent->timestamp =
4337 XtLastTimestampProcessed (FRAME_X_DISPLAY (XFRAME (w->frame)));
4338 #endif
4339 ievent->code = 0;
4340 ievent->part = ev->data.l[2];
4341 ievent->x = make_number (ev->data.l[3]);
4342 ievent->y = make_number (ev->data.l[4]);
4343 ievent->modifiers = 0;
4344 }
4345
4346
4347 #ifdef USE_MOTIF
4348
4349 /* Minimum and maximum values used for Motif scroll bars. */
4350
4351 #define XM_SB_MAX 10000000
4352
4353
4354 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
4355 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
4356 CALL_DATA is a pointer to a XmScrollBarCallbackStruct. */
4357
4358 static void
4359 xm_scroll_callback (Widget widget, XtPointer client_data, XtPointer call_data)
4360 {
4361 struct scroll_bar *bar = client_data;
4362 XmScrollBarCallbackStruct *cs = call_data;
4363 int part = -1, whole = 0, portion = 0;
4364
4365 switch (cs->reason)
4366 {
4367 case XmCR_DECREMENT:
4368 bar->dragging = -1;
4369 part = scroll_bar_up_arrow;
4370 break;
4371
4372 case XmCR_INCREMENT:
4373 bar->dragging = -1;
4374 part = scroll_bar_down_arrow;
4375 break;
4376
4377 case XmCR_PAGE_DECREMENT:
4378 bar->dragging = -1;
4379 part = scroll_bar_above_handle;
4380 break;
4381
4382 case XmCR_PAGE_INCREMENT:
4383 bar->dragging = -1;
4384 part = scroll_bar_below_handle;
4385 break;
4386
4387 case XmCR_TO_TOP:
4388 bar->dragging = -1;
4389 part = scroll_bar_to_top;
4390 break;
4391
4392 case XmCR_TO_BOTTOM:
4393 bar->dragging = -1;
4394 part = scroll_bar_to_bottom;
4395 break;
4396
4397 case XmCR_DRAG:
4398 {
4399 int slider_size;
4400
4401 /* Get the slider size. */
4402 block_input ();
4403 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
4404 unblock_input ();
4405
4406 whole = XM_SB_MAX - slider_size;
4407 portion = min (cs->value, whole);
4408 part = scroll_bar_handle;
4409 bar->dragging = cs->value;
4410 }
4411 break;
4412
4413 case XmCR_VALUE_CHANGED:
4414 break;
4415 };
4416
4417 if (part >= 0)
4418 {
4419 window_being_scrolled = bar->window;
4420 x_send_scroll_bar_event (bar->window, part, portion, whole);
4421 }
4422 }
4423
4424 #elif defined USE_GTK
4425
4426 /* Scroll bar callback for GTK scroll bars. WIDGET is the scroll
4427 bar widget. DATA is a pointer to the scroll_bar structure. */
4428
4429 static gboolean
4430 xg_scroll_callback (GtkRange *range,
4431 GtkScrollType scroll,
4432 gdouble value,
4433 gpointer user_data)
4434 {
4435 struct scroll_bar *bar = user_data;
4436 gdouble position;
4437 int part = -1, whole = 0, portion = 0;
4438 GtkAdjustment *adj = GTK_ADJUSTMENT (gtk_range_get_adjustment (range));
4439 struct frame *f = g_object_get_data (G_OBJECT (range), XG_FRAME_DATA);
4440
4441 if (xg_ignore_gtk_scrollbar) return FALSE;
4442 position = gtk_adjustment_get_value (adj);
4443
4444
4445 switch (scroll)
4446 {
4447 case GTK_SCROLL_JUMP:
4448 /* Buttons 1 2 or 3 must be grabbed. */
4449 if (FRAME_DISPLAY_INFO (f)->grabbed != 0
4450 && FRAME_DISPLAY_INFO (f)->grabbed < (1 << 4))
4451 {
4452 part = scroll_bar_handle;
4453 whole = gtk_adjustment_get_upper (adj) -
4454 gtk_adjustment_get_page_size (adj);
4455 portion = min ((int)position, whole);
4456 bar->dragging = portion;
4457 }
4458 break;
4459 case GTK_SCROLL_STEP_BACKWARD:
4460 part = scroll_bar_up_arrow;
4461 bar->dragging = -1;
4462 break;
4463 case GTK_SCROLL_STEP_FORWARD:
4464 part = scroll_bar_down_arrow;
4465 bar->dragging = -1;
4466 break;
4467 case GTK_SCROLL_PAGE_BACKWARD:
4468 part = scroll_bar_above_handle;
4469 bar->dragging = -1;
4470 break;
4471 case GTK_SCROLL_PAGE_FORWARD:
4472 part = scroll_bar_below_handle;
4473 bar->dragging = -1;
4474 break;
4475 }
4476
4477 if (part >= 0)
4478 {
4479 window_being_scrolled = bar->window;
4480 x_send_scroll_bar_event (bar->window, part, portion, whole);
4481 }
4482
4483 return FALSE;
4484 }
4485
4486 /* Callback for button release. Sets dragging to -1 when dragging is done. */
4487
4488 static gboolean
4489 xg_end_scroll_callback (GtkWidget *widget,
4490 GdkEventButton *event,
4491 gpointer user_data)
4492 {
4493 struct scroll_bar *bar = user_data;
4494 bar->dragging = -1;
4495 if (WINDOWP (window_being_scrolled))
4496 {
4497 x_send_scroll_bar_event (window_being_scrolled,
4498 scroll_bar_end_scroll, 0, 0);
4499 window_being_scrolled = Qnil;
4500 }
4501
4502 return FALSE;
4503 }
4504
4505
4506 #else /* not USE_GTK and not USE_MOTIF */
4507
4508 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
4509 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
4510 scroll bar struct. CALL_DATA is a pointer to a float saying where
4511 the thumb is. */
4512
4513 static void
4514 xaw_jump_callback (Widget widget, XtPointer client_data, XtPointer call_data)
4515 {
4516 struct scroll_bar *bar = client_data;
4517 float *top_addr = call_data;
4518 float top = *top_addr;
4519 float shown;
4520 int whole, portion, height;
4521 enum scroll_bar_part part;
4522
4523 /* Get the size of the thumb, a value between 0 and 1. */
4524 block_input ();
4525 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
4526 unblock_input ();
4527
4528 whole = 10000000;
4529 portion = shown < 1 ? top * whole : 0;
4530
4531 if (shown < 1 && (eabs (top + shown - 1) < 1.0f / height))
4532 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
4533 the bottom, so we force the scrolling whenever we see that we're
4534 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
4535 we try to ensure that we always stay two pixels away from the
4536 bottom). */
4537 part = scroll_bar_down_arrow;
4538 else
4539 part = scroll_bar_handle;
4540
4541 window_being_scrolled = bar->window;
4542 bar->dragging = portion;
4543 bar->last_seen_part = part;
4544 x_send_scroll_bar_event (bar->window, part, portion, whole);
4545 }
4546
4547
4548 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
4549 i.e. line or page up or down. WIDGET is the Xaw scroll bar
4550 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
4551 the scroll bar. CALL_DATA is an integer specifying the action that
4552 has taken place. Its magnitude is in the range 0..height of the
4553 scroll bar. Negative values mean scroll towards buffer start.
4554 Values < height of scroll bar mean line-wise movement. */
4555
4556 static void
4557 xaw_scroll_callback (Widget widget, XtPointer client_data, XtPointer call_data)
4558 {
4559 struct scroll_bar *bar = client_data;
4560 /* The position really is stored cast to a pointer. */
4561 int position = (intptr_t) call_data;
4562 Dimension height;
4563 enum scroll_bar_part part;
4564
4565 /* Get the height of the scroll bar. */
4566 block_input ();
4567 XtVaGetValues (widget, XtNheight, &height, NULL);
4568 unblock_input ();
4569
4570 if (eabs (position) >= height)
4571 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
4572
4573 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
4574 it maps line-movement to call_data = max(5, height/20). */
4575 else if (xaw3d_arrow_scroll && eabs (position) <= max (5, height / 20))
4576 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
4577 else
4578 part = scroll_bar_move_ratio;
4579
4580 window_being_scrolled = bar->window;
4581 bar->dragging = -1;
4582 bar->last_seen_part = part;
4583 x_send_scroll_bar_event (bar->window, part, position, height);
4584 }
4585
4586 #endif /* not USE_GTK and not USE_MOTIF */
4587
4588 #define SCROLL_BAR_NAME "verticalScrollBar"
4589
4590 /* Create the widget for scroll bar BAR on frame F. Record the widget
4591 and X window of the scroll bar in BAR. */
4592
4593 #ifdef USE_GTK
4594 static void
4595 x_create_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
4596 {
4597 const char *scroll_bar_name = SCROLL_BAR_NAME;
4598
4599 block_input ();
4600 xg_create_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
4601 G_CALLBACK (xg_end_scroll_callback),
4602 scroll_bar_name);
4603 unblock_input ();
4604 }
4605
4606 #else /* not USE_GTK */
4607
4608 static void
4609 x_create_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
4610 {
4611 Window xwindow;
4612 Widget widget;
4613 Arg av[20];
4614 int ac = 0;
4615 const char *scroll_bar_name = SCROLL_BAR_NAME;
4616 unsigned long pixel;
4617
4618 block_input ();
4619
4620 #ifdef USE_MOTIF
4621 /* Set resources. Create the widget. */
4622 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4623 XtSetArg (av[ac], XmNminimum, 0); ++ac;
4624 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
4625 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
4626 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
4627 XtSetArg (av[ac], XmNincrement, 1); ++ac;
4628 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
4629
4630 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4631 if (pixel != -1)
4632 {
4633 XtSetArg (av[ac], XmNforeground, pixel);
4634 ++ac;
4635 }
4636
4637 pixel = f->output_data.x->scroll_bar_background_pixel;
4638 if (pixel != -1)
4639 {
4640 XtSetArg (av[ac], XmNbackground, pixel);
4641 ++ac;
4642 }
4643
4644 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
4645 (char *) scroll_bar_name, av, ac);
4646
4647 /* Add one callback for everything that can happen. */
4648 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
4649 (XtPointer) bar);
4650 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
4651 (XtPointer) bar);
4652 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
4653 (XtPointer) bar);
4654 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
4655 (XtPointer) bar);
4656 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
4657 (XtPointer) bar);
4658 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
4659 (XtPointer) bar);
4660 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
4661 (XtPointer) bar);
4662
4663 /* Realize the widget. Only after that is the X window created. */
4664 XtRealizeWidget (widget);
4665
4666 /* Set the cursor to an arrow. I didn't find a resource to do that.
4667 And I'm wondering why it hasn't an arrow cursor by default. */
4668 XDefineCursor (XtDisplay (widget), XtWindow (widget),
4669 f->output_data.x->nontext_cursor);
4670
4671 #else /* !USE_MOTIF i.e. use Xaw */
4672
4673 /* Set resources. Create the widget. The background of the
4674 Xaw3d scroll bar widget is a little bit light for my taste.
4675 We don't alter it here to let users change it according
4676 to their taste with `emacs*verticalScrollBar.background: xxx'. */
4677 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4678 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
4679 /* For smoother scrolling with Xaw3d -sm */
4680 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
4681
4682 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4683 if (pixel != -1)
4684 {
4685 XtSetArg (av[ac], XtNforeground, pixel);
4686 ++ac;
4687 }
4688
4689 pixel = f->output_data.x->scroll_bar_background_pixel;
4690 if (pixel != -1)
4691 {
4692 XtSetArg (av[ac], XtNbackground, pixel);
4693 ++ac;
4694 }
4695
4696 /* Top/bottom shadow colors. */
4697
4698 /* Allocate them, if necessary. */
4699 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
4700 {
4701 pixel = f->output_data.x->scroll_bar_background_pixel;
4702 if (pixel != -1)
4703 {
4704 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
4705 FRAME_X_COLORMAP (f),
4706 &pixel, 1.2, 0x8000))
4707 pixel = -1;
4708 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
4709 }
4710 }
4711 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4712 {
4713 pixel = f->output_data.x->scroll_bar_background_pixel;
4714 if (pixel != -1)
4715 {
4716 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
4717 FRAME_X_COLORMAP (f),
4718 &pixel, 0.6, 0x4000))
4719 pixel = -1;
4720 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
4721 }
4722 }
4723
4724 #ifdef XtNbeNiceToColormap
4725 /* Tell the toolkit about them. */
4726 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
4727 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4728 /* We tried to allocate a color for the top/bottom shadow, and
4729 failed, so tell Xaw3d to use dithering instead. */
4730 /* But only if we have a small colormap. Xaw3d can allocate nice
4731 colors itself. */
4732 {
4733 XtSetArg (av[ac], XtNbeNiceToColormap,
4734 DefaultDepthOfScreen (FRAME_X_SCREEN (f)) < 16);
4735 ++ac;
4736 }
4737 else
4738 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
4739 be more consistent with other emacs 3d colors, and since Xaw3d is
4740 not good at dealing with allocation failure. */
4741 {
4742 /* This tells Xaw3d to use real colors instead of dithering for
4743 the shadows. */
4744 XtSetArg (av[ac], XtNbeNiceToColormap, False);
4745 ++ac;
4746
4747 /* Specify the colors. */
4748 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
4749 if (pixel != -1)
4750 {
4751 XtSetArg (av[ac], XtNtopShadowPixel, pixel);
4752 ++ac;
4753 }
4754 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
4755 if (pixel != -1)
4756 {
4757 XtSetArg (av[ac], XtNbottomShadowPixel, pixel);
4758 ++ac;
4759 }
4760 }
4761 #endif
4762
4763 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
4764 f->output_data.x->edit_widget, av, ac);
4765
4766 {
4767 char const *initial = "";
4768 char const *val = initial;
4769 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
4770 #ifdef XtNarrowScrollbars
4771 XtNarrowScrollbars, (XtPointer) &xaw3d_arrow_scroll,
4772 #endif
4773 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
4774 if (xaw3d_arrow_scroll || val == initial)
4775 { /* ARROW_SCROLL */
4776 xaw3d_arrow_scroll = True;
4777 /* Isn't that just a personal preference ? --Stef */
4778 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
4779 }
4780 }
4781
4782 /* Define callbacks. */
4783 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
4784 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
4785 (XtPointer) bar);
4786
4787 /* Realize the widget. Only after that is the X window created. */
4788 XtRealizeWidget (widget);
4789
4790 #endif /* !USE_MOTIF */
4791
4792 /* Install an action hook that lets us detect when the user
4793 finishes interacting with a scroll bar. */
4794 if (action_hook_id == 0)
4795 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
4796
4797 /* Remember X window and widget in the scroll bar vector. */
4798 SET_SCROLL_BAR_X_WIDGET (bar, widget);
4799 xwindow = XtWindow (widget);
4800 bar->x_window = xwindow;
4801
4802 unblock_input ();
4803 }
4804 #endif /* not USE_GTK */
4805
4806
4807 /* Set the thumb size and position of scroll bar BAR. We are currently
4808 displaying PORTION out of a whole WHOLE, and our position POSITION. */
4809
4810 #ifdef USE_GTK
4811 static void
4812 x_set_toolkit_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position, int whole)
4813 {
4814 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
4815 }
4816
4817 #else /* not USE_GTK */
4818 static void
4819 x_set_toolkit_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position,
4820 int whole)
4821 {
4822 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4823 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
4824 float top, shown;
4825
4826 block_input ();
4827
4828 #ifdef USE_MOTIF
4829
4830 if (scroll_bar_adjust_thumb_portion_p)
4831 {
4832 /* We use an estimate of 30 chars per line rather than the real
4833 `portion' value. This has the disadvantage that the thumb size
4834 is not very representative, but it makes our life a lot easier.
4835 Otherwise, we have to constantly adjust the thumb size, which
4836 we can't always do quickly enough: while dragging, the size of
4837 the thumb might prevent the user from dragging the thumb all the
4838 way to the end. but Motif and some versions of Xaw3d don't allow
4839 updating the thumb size while dragging. Also, even if we can update
4840 its size, the update will often happen too late.
4841 If you don't believe it, check out revision 1.650 of xterm.c to see
4842 what hoops we were going through and the still poor behavior we got. */
4843 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
4844 /* When the thumb is at the bottom, position == whole.
4845 So we need to increase `whole' to make space for the thumb. */
4846 whole += portion;
4847 }
4848
4849 if (whole <= 0)
4850 top = 0, shown = 1;
4851 else
4852 {
4853 top = (float) position / whole;
4854 shown = (float) portion / whole;
4855 }
4856
4857 if (bar->dragging == -1)
4858 {
4859 int size, value;
4860
4861 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
4862 is the scroll bar's maximum and MIN is the scroll bar's minimum
4863 value. */
4864 size = clip_to_bounds (1, shown * XM_SB_MAX, XM_SB_MAX);
4865
4866 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
4867 value = top * XM_SB_MAX;
4868 value = min (value, XM_SB_MAX - size);
4869
4870 XmScrollBarSetValues (widget, value, size, 0, 0, False);
4871 }
4872 #else /* !USE_MOTIF i.e. use Xaw */
4873
4874 if (whole == 0)
4875 top = 0, shown = 1;
4876 else
4877 {
4878 top = (float) position / whole;
4879 shown = (float) portion / whole;
4880 }
4881
4882 {
4883 float old_top, old_shown;
4884 Dimension height;
4885 XtVaGetValues (widget,
4886 XtNtopOfThumb, &old_top,
4887 XtNshown, &old_shown,
4888 XtNheight, &height,
4889 NULL);
4890
4891 /* Massage the top+shown values. */
4892 if (bar->dragging == -1 || bar->last_seen_part == scroll_bar_down_arrow)
4893 top = max (0, min (1, top));
4894 else
4895 top = old_top;
4896 #if ! defined (HAVE_XAW3D)
4897 /* With Xaw, 'top' values too closer to 1.0 may
4898 cause the thumb to disappear. Fix that. */
4899 top = min (top, 0.99f);
4900 #endif
4901 /* Keep two pixels available for moving the thumb down. */
4902 shown = max (0, min (1 - top - (2.0f / height), shown));
4903 #if ! defined (HAVE_XAW3D)
4904 /* Likewise with too small 'shown'. */
4905 shown = max (shown, 0.01f);
4906 #endif
4907
4908 /* If the call to XawScrollbarSetThumb below doesn't seem to
4909 work, check that 'NARROWPROTO' is defined in src/config.h.
4910 If this is not so, most likely you need to fix configure. */
4911 if (top != old_top || shown != old_shown)
4912 {
4913 if (bar->dragging == -1)
4914 XawScrollbarSetThumb (widget, top, shown);
4915 else
4916 {
4917 /* Try to make the scrolling a tad smoother. */
4918 if (!xaw3d_pick_top)
4919 shown = min (shown, old_shown);
4920
4921 XawScrollbarSetThumb (widget, top, shown);
4922 }
4923 }
4924 }
4925 #endif /* !USE_MOTIF */
4926
4927 unblock_input ();
4928 }
4929 #endif /* not USE_GTK */
4930
4931 #endif /* USE_TOOLKIT_SCROLL_BARS */
4932
4933
4934 \f
4935 /************************************************************************
4936 Scroll bars, general
4937 ************************************************************************/
4938
4939 /* Create a scroll bar and return the scroll bar vector for it. W is
4940 the Emacs window on which to create the scroll bar. TOP, LEFT,
4941 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
4942 scroll bar. */
4943
4944 static struct scroll_bar *
4945 x_scroll_bar_create (struct window *w, int top, int left, int width, int height)
4946 {
4947 struct frame *f = XFRAME (w->frame);
4948 struct scroll_bar *bar
4949 = ALLOCATE_PSEUDOVECTOR (struct scroll_bar, x_window, PVEC_OTHER);
4950 Lisp_Object barobj;
4951
4952 block_input ();
4953
4954 #ifdef USE_TOOLKIT_SCROLL_BARS
4955 x_create_toolkit_scroll_bar (f, bar);
4956 #else /* not USE_TOOLKIT_SCROLL_BARS */
4957 {
4958 XSetWindowAttributes a;
4959 unsigned long mask;
4960 Window window;
4961
4962 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
4963 if (a.background_pixel == -1)
4964 a.background_pixel = FRAME_BACKGROUND_PIXEL (f);
4965
4966 a.event_mask = (ButtonPressMask | ButtonReleaseMask
4967 | ButtonMotionMask | PointerMotionHintMask
4968 | ExposureMask);
4969 a.cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
4970
4971 mask = (CWBackPixel | CWEventMask | CWCursor);
4972
4973 /* Clear the area of W that will serve as a scroll bar. This is
4974 for the case that a window has been split horizontally. In
4975 this case, no clear_frame is generated to reduce flickering. */
4976 if (width > 0 && height > 0)
4977 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4978 left, top, width, window_box_height (w));
4979
4980 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4981 /* Position and size of scroll bar. */
4982 left, top, width, height,
4983 /* Border width, depth, class, and visual. */
4984 0,
4985 CopyFromParent,
4986 CopyFromParent,
4987 CopyFromParent,
4988 /* Attributes. */
4989 mask, &a);
4990 bar->x_window = window;
4991 }
4992 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4993
4994 XSETWINDOW (bar->window, w);
4995 bar->top = top;
4996 bar->left = left;
4997 bar->width = width;
4998 bar->height = height;
4999 bar->start = 0;
5000 bar->end = 0;
5001 bar->dragging = -1;
5002 bar->fringe_extended_p = 0;
5003 #if defined (USE_TOOLKIT_SCROLL_BARS) && defined (USE_LUCID)
5004 bar->last_seen_part = scroll_bar_nowhere;
5005 #endif
5006
5007 /* Add bar to its frame's list of scroll bars. */
5008 bar->next = FRAME_SCROLL_BARS (f);
5009 bar->prev = Qnil;
5010 XSETVECTOR (barobj, bar);
5011 fset_scroll_bars (f, barobj);
5012 if (!NILP (bar->next))
5013 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
5014
5015 /* Map the window/widget. */
5016 #ifdef USE_TOOLKIT_SCROLL_BARS
5017 {
5018 #ifdef USE_GTK
5019 xg_update_scrollbar_pos (f, bar->x_window, top,
5020 left,width, max (height, 1));
5021 #else /* not USE_GTK */
5022 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
5023 XtConfigureWidget (scroll_bar, left, top, width, max (height, 1), 0);
5024 XtMapWidget (scroll_bar);
5025 #endif /* not USE_GTK */
5026 }
5027 #else /* not USE_TOOLKIT_SCROLL_BARS */
5028 XMapRaised (FRAME_X_DISPLAY (f), bar->x_window);
5029 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5030
5031 unblock_input ();
5032 return bar;
5033 }
5034
5035
5036 #ifndef USE_TOOLKIT_SCROLL_BARS
5037
5038 /* Draw BAR's handle in the proper position.
5039
5040 If the handle is already drawn from START to END, don't bother
5041 redrawing it, unless REBUILD is non-zero; in that case, always
5042 redraw it. (REBUILD is handy for drawing the handle after expose
5043 events.)
5044
5045 Normally, we want to constrain the start and end of the handle to
5046 fit inside its rectangle, but if the user is dragging the scroll
5047 bar handle, we want to let them drag it down all the way, so that
5048 the bar's top is as far down as it goes; otherwise, there's no way
5049 to move to the very end of the buffer. */
5050
5051 static void
5052 x_scroll_bar_set_handle (struct scroll_bar *bar, int start, int end, int rebuild)
5053 {
5054 bool dragging = bar->dragging != -1;
5055 Window w = bar->x_window;
5056 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5057 GC gc = f->output_data.x->normal_gc;
5058
5059 /* If the display is already accurate, do nothing. */
5060 if (! rebuild
5061 && start == bar->start
5062 && end == bar->end)
5063 return;
5064
5065 block_input ();
5066
5067 {
5068 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, bar->width);
5069 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, bar->height);
5070 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
5071
5072 /* Make sure the values are reasonable, and try to preserve
5073 the distance between start and end. */
5074 {
5075 int length = end - start;
5076
5077 if (start < 0)
5078 start = 0;
5079 else if (start > top_range)
5080 start = top_range;
5081 end = start + length;
5082
5083 if (end < start)
5084 end = start;
5085 else if (end > top_range && ! dragging)
5086 end = top_range;
5087 }
5088
5089 /* Store the adjusted setting in the scroll bar. */
5090 bar->start = start;
5091 bar->end = end;
5092
5093 /* Clip the end position, just for display. */
5094 if (end > top_range)
5095 end = top_range;
5096
5097 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
5098 below top positions, to make sure the handle is always at least
5099 that many pixels tall. */
5100 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
5101
5102 /* Draw the empty space above the handle. Note that we can't clear
5103 zero-height areas; that means "clear to end of window." */
5104 if (start > 0)
5105 x_clear_area (FRAME_X_DISPLAY (f), w,
5106 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5107 VERTICAL_SCROLL_BAR_TOP_BORDER,
5108 inside_width, start);
5109
5110 /* Change to proper foreground color if one is specified. */
5111 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5112 XSetForeground (FRAME_X_DISPLAY (f), gc,
5113 f->output_data.x->scroll_bar_foreground_pixel);
5114
5115 /* Draw the handle itself. */
5116 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
5117 /* x, y, width, height */
5118 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5119 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
5120 inside_width, end - start);
5121
5122 /* Restore the foreground color of the GC if we changed it above. */
5123 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5124 XSetForeground (FRAME_X_DISPLAY (f), gc,
5125 FRAME_FOREGROUND_PIXEL (f));
5126
5127 /* Draw the empty space below the handle. Note that we can't
5128 clear zero-height areas; that means "clear to end of window." */
5129 if (end < inside_height)
5130 x_clear_area (FRAME_X_DISPLAY (f), w,
5131 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5132 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
5133 inside_width, inside_height - end);
5134 }
5135
5136 unblock_input ();
5137 }
5138
5139 #endif /* !USE_TOOLKIT_SCROLL_BARS */
5140
5141 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
5142 nil. */
5143
5144 static void
5145 x_scroll_bar_remove (struct scroll_bar *bar)
5146 {
5147 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5148 block_input ();
5149
5150 #ifdef USE_TOOLKIT_SCROLL_BARS
5151 #ifdef USE_GTK
5152 xg_remove_scroll_bar (f, bar->x_window);
5153 #else /* not USE_GTK */
5154 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
5155 #endif /* not USE_GTK */
5156 #else
5157 XDestroyWindow (FRAME_X_DISPLAY (f), bar->x_window);
5158 #endif
5159
5160 /* Dissociate this scroll bar from its window. */
5161 wset_vertical_scroll_bar (XWINDOW (bar->window), Qnil);
5162
5163 unblock_input ();
5164 }
5165
5166
5167 /* Set the handle of the vertical scroll bar for WINDOW to indicate
5168 that we are displaying PORTION characters out of a total of WHOLE
5169 characters, starting at POSITION. If WINDOW has no scroll bar,
5170 create one. */
5171
5172 static void
5173 XTset_vertical_scroll_bar (struct window *w, int portion, int whole, int position)
5174 {
5175 struct frame *f = XFRAME (w->frame);
5176 Lisp_Object barobj;
5177 struct scroll_bar *bar;
5178 int top, height, left, sb_left, width, sb_width;
5179 int window_y, window_height;
5180 #ifdef USE_TOOLKIT_SCROLL_BARS
5181 bool fringe_extended_p;
5182 #endif
5183
5184 /* Get window dimensions. */
5185 window_box (w, ANY_AREA, 0, &window_y, 0, &window_height);
5186 top = window_y;
5187 width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
5188 height = window_height;
5189
5190 /* Compute the left edge of the scroll bar area. */
5191 left = WINDOW_SCROLL_BAR_AREA_X (w);
5192
5193 /* Compute the width of the scroll bar which might be less than
5194 the width of the area reserved for the scroll bar. */
5195 if (WINDOW_CONFIG_SCROLL_BAR_WIDTH (w) > 0)
5196 sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
5197 else
5198 sb_width = width;
5199
5200 /* Compute the left edge of the scroll bar. */
5201 #ifdef USE_TOOLKIT_SCROLL_BARS
5202 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
5203 sb_left = left + (WINDOW_RIGHTMOST_P (w) ? width - sb_width : 0);
5204 else
5205 sb_left = left + (WINDOW_LEFTMOST_P (w) ? 0 : width - sb_width);
5206 #else
5207 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
5208 sb_left = left + width - sb_width;
5209 else
5210 sb_left = left;
5211 #endif
5212
5213 #ifdef USE_TOOLKIT_SCROLL_BARS
5214 fringe_extended_p = WINDOW_FRINGE_EXTENDED_P (w);
5215 #endif
5216
5217 /* Does the scroll bar exist yet? */
5218 if (NILP (w->vertical_scroll_bar))
5219 {
5220 if (width > 0 && height > 0)
5221 {
5222 block_input ();
5223 #ifdef USE_TOOLKIT_SCROLL_BARS
5224 if (fringe_extended_p)
5225 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5226 sb_left, top, sb_width, height);
5227 else
5228 #endif
5229 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5230 left, top, width, height);
5231 unblock_input ();
5232 }
5233
5234 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
5235 }
5236 else
5237 {
5238 /* It may just need to be moved and resized. */
5239 unsigned int mask = 0;
5240
5241 bar = XSCROLL_BAR (w->vertical_scroll_bar);
5242
5243 block_input ();
5244
5245 if (sb_left != bar->left)
5246 mask |= CWX;
5247 if (top != bar->top)
5248 mask |= CWY;
5249 if (sb_width != bar->width)
5250 mask |= CWWidth;
5251 if (height != bar->height)
5252 mask |= CWHeight;
5253
5254 #ifdef USE_TOOLKIT_SCROLL_BARS
5255
5256 /* Move/size the scroll bar widget. */
5257 if (mask || bar->fringe_extended_p != fringe_extended_p)
5258 {
5259 /* Since toolkit scroll bars are smaller than the space reserved
5260 for them on the frame, we have to clear "under" them. */
5261 if (width > 0 && height > 0)
5262 {
5263 if (fringe_extended_p)
5264 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5265 sb_left, top, sb_width, height);
5266 else
5267 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5268 left, top, width, height);
5269 }
5270 #ifdef USE_GTK
5271 xg_update_scrollbar_pos (f, bar->x_window, top,
5272 sb_left, sb_width, max (height, 1));
5273 #else /* not USE_GTK */
5274 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
5275 sb_left, top, sb_width, max (height, 1), 0);
5276 #endif /* not USE_GTK */
5277 }
5278 #else /* not USE_TOOLKIT_SCROLL_BARS */
5279
5280 /* Clear areas not covered by the scroll bar because it's not as
5281 wide as the area reserved for it. This makes sure a
5282 previous mode line display is cleared after C-x 2 C-x 1, for
5283 example. */
5284 {
5285 int area_width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
5286 int rest = area_width - sb_width;
5287 if (rest > 0 && height > 0)
5288 {
5289 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
5290 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5291 left + area_width - rest, top, rest, height);
5292 else
5293 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5294 left, top, rest, height);
5295 }
5296 }
5297
5298 /* Move/size the scroll bar window. */
5299 if (mask)
5300 {
5301 XWindowChanges wc;
5302
5303 wc.x = sb_left;
5304 wc.y = top;
5305 wc.width = sb_width;
5306 wc.height = height;
5307 XConfigureWindow (FRAME_X_DISPLAY (f), bar->x_window,
5308 mask, &wc);
5309 }
5310
5311 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5312
5313 /* Remember new settings. */
5314 bar->left = sb_left;
5315 bar->top = top;
5316 bar->width = sb_width;
5317 bar->height = height;
5318
5319 unblock_input ();
5320 }
5321
5322 #ifdef USE_TOOLKIT_SCROLL_BARS
5323 bar->fringe_extended_p = fringe_extended_p;
5324
5325 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
5326 #else /* not USE_TOOLKIT_SCROLL_BARS */
5327 /* Set the scroll bar's current state, unless we're currently being
5328 dragged. */
5329 if (bar->dragging == -1)
5330 {
5331 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
5332
5333 if (whole == 0)
5334 x_scroll_bar_set_handle (bar, 0, top_range, 0);
5335 else
5336 {
5337 int start = ((double) position * top_range) / whole;
5338 int end = ((double) (position + portion) * top_range) / whole;
5339 x_scroll_bar_set_handle (bar, start, end, 0);
5340 }
5341 }
5342 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5343
5344 XSETVECTOR (barobj, bar);
5345 wset_vertical_scroll_bar (w, barobj);
5346 }
5347
5348
5349 /* The following three hooks are used when we're doing a thorough
5350 redisplay of the frame. We don't explicitly know which scroll bars
5351 are going to be deleted, because keeping track of when windows go
5352 away is a real pain - "Can you say set-window-configuration, boys
5353 and girls?" Instead, we just assert at the beginning of redisplay
5354 that *all* scroll bars are to be removed, and then save a scroll bar
5355 from the fiery pit when we actually redisplay its window. */
5356
5357 /* Arrange for all scroll bars on FRAME to be removed at the next call
5358 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
5359 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
5360
5361 static void
5362 XTcondemn_scroll_bars (struct frame *frame)
5363 {
5364 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
5365 while (! NILP (FRAME_SCROLL_BARS (frame)))
5366 {
5367 Lisp_Object bar;
5368 bar = FRAME_SCROLL_BARS (frame);
5369 fset_scroll_bars (frame, XSCROLL_BAR (bar)->next);
5370 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
5371 XSCROLL_BAR (bar)->prev = Qnil;
5372 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
5373 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
5374 fset_condemned_scroll_bars (frame, bar);
5375 }
5376 }
5377
5378
5379 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
5380 Note that WINDOW isn't necessarily condemned at all. */
5381
5382 static void
5383 XTredeem_scroll_bar (struct window *window)
5384 {
5385 struct scroll_bar *bar;
5386 struct frame *f;
5387 Lisp_Object barobj;
5388
5389 /* We can't redeem this window's scroll bar if it doesn't have one. */
5390 if (NILP (window->vertical_scroll_bar))
5391 emacs_abort ();
5392
5393 bar = XSCROLL_BAR (window->vertical_scroll_bar);
5394
5395 /* Unlink it from the condemned list. */
5396 f = XFRAME (WINDOW_FRAME (window));
5397 if (NILP (bar->prev))
5398 {
5399 /* If the prev pointer is nil, it must be the first in one of
5400 the lists. */
5401 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
5402 /* It's not condemned. Everything's fine. */
5403 return;
5404 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
5405 window->vertical_scroll_bar))
5406 fset_condemned_scroll_bars (f, bar->next);
5407 else
5408 /* If its prev pointer is nil, it must be at the front of
5409 one or the other! */
5410 emacs_abort ();
5411 }
5412 else
5413 XSCROLL_BAR (bar->prev)->next = bar->next;
5414
5415 if (! NILP (bar->next))
5416 XSCROLL_BAR (bar->next)->prev = bar->prev;
5417
5418 bar->next = FRAME_SCROLL_BARS (f);
5419 bar->prev = Qnil;
5420 XSETVECTOR (barobj, bar);
5421 fset_scroll_bars (f, barobj);
5422 if (! NILP (bar->next))
5423 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
5424 }
5425
5426 /* Remove all scroll bars on FRAME that haven't been saved since the
5427 last call to `*condemn_scroll_bars_hook'. */
5428
5429 static void
5430 XTjudge_scroll_bars (struct frame *f)
5431 {
5432 Lisp_Object bar, next;
5433
5434 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
5435
5436 /* Clear out the condemned list now so we won't try to process any
5437 more events on the hapless scroll bars. */
5438 fset_condemned_scroll_bars (f, Qnil);
5439
5440 for (; ! NILP (bar); bar = next)
5441 {
5442 struct scroll_bar *b = XSCROLL_BAR (bar);
5443
5444 x_scroll_bar_remove (b);
5445
5446 next = b->next;
5447 b->next = b->prev = Qnil;
5448 }
5449
5450 /* Now there should be no references to the condemned scroll bars,
5451 and they should get garbage-collected. */
5452 }
5453
5454
5455 #ifndef USE_TOOLKIT_SCROLL_BARS
5456 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
5457 is a no-op when using toolkit scroll bars.
5458
5459 This may be called from a signal handler, so we have to ignore GC
5460 mark bits. */
5461
5462 static void
5463 x_scroll_bar_expose (struct scroll_bar *bar, const XEvent *event)
5464 {
5465 Window w = bar->x_window;
5466 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5467 GC gc = f->output_data.x->normal_gc;
5468
5469 block_input ();
5470
5471 x_scroll_bar_set_handle (bar, bar->start, bar->end, 1);
5472
5473 /* Switch to scroll bar foreground color. */
5474 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5475 XSetForeground (FRAME_X_DISPLAY (f), gc,
5476 f->output_data.x->scroll_bar_foreground_pixel);
5477
5478 /* Draw a one-pixel border just inside the edges of the scroll bar. */
5479 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
5480 /* x, y, width, height */
5481 0, 0, bar->width - 1, bar->height - 1);
5482
5483 /* Restore the foreground color of the GC if we changed it above. */
5484 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5485 XSetForeground (FRAME_X_DISPLAY (f), gc,
5486 FRAME_FOREGROUND_PIXEL (f));
5487
5488 unblock_input ();
5489
5490 }
5491 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5492
5493 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
5494 is set to something other than NO_EVENT, it is enqueued.
5495
5496 This may be called from a signal handler, so we have to ignore GC
5497 mark bits. */
5498
5499
5500 static void
5501 x_scroll_bar_handle_click (struct scroll_bar *bar,
5502 const XEvent *event,
5503 struct input_event *emacs_event)
5504 {
5505 if (! WINDOWP (bar->window))
5506 emacs_abort ();
5507
5508 emacs_event->kind = SCROLL_BAR_CLICK_EVENT;
5509 emacs_event->code = event->xbutton.button - Button1;
5510 emacs_event->modifiers
5511 = (x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO
5512 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
5513 event->xbutton.state)
5514 | (event->type == ButtonRelease
5515 ? up_modifier
5516 : down_modifier));
5517 emacs_event->frame_or_window = bar->window;
5518 emacs_event->arg = Qnil;
5519 emacs_event->timestamp = event->xbutton.time;
5520 {
5521 int top_range
5522 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
5523 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
5524
5525 if (y < 0) y = 0;
5526 if (y > top_range) y = top_range;
5527
5528 if (y < bar->start)
5529 emacs_event->part = scroll_bar_above_handle;
5530 else if (y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5531 emacs_event->part = scroll_bar_handle;
5532 else
5533 emacs_event->part = scroll_bar_below_handle;
5534
5535 #ifndef USE_TOOLKIT_SCROLL_BARS
5536 /* If the user has released the handle, set it to its final position. */
5537 if (event->type == ButtonRelease && bar->dragging != -1)
5538 {
5539 int new_start = y - bar->dragging;
5540 int new_end = new_start + bar->end - bar->start;
5541
5542 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5543 bar->dragging = -1;
5544 }
5545 #endif
5546
5547 XSETINT (emacs_event->x, y);
5548 XSETINT (emacs_event->y, top_range);
5549 }
5550 }
5551
5552 #ifndef USE_TOOLKIT_SCROLL_BARS
5553
5554 /* Handle some mouse motion while someone is dragging the scroll bar.
5555
5556 This may be called from a signal handler, so we have to ignore GC
5557 mark bits. */
5558
5559 static void
5560 x_scroll_bar_note_movement (struct scroll_bar *bar,
5561 const XMotionEvent *event)
5562 {
5563 struct frame *f = XFRAME (XWINDOW (bar->window)->frame);
5564 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
5565
5566 dpyinfo->last_mouse_movement_time = event->time;
5567 dpyinfo->last_mouse_scroll_bar = bar;
5568 f->mouse_moved = 1;
5569
5570 /* If we're dragging the bar, display it. */
5571 if (bar->dragging != -1)
5572 {
5573 /* Where should the handle be now? */
5574 int new_start = event->y - bar->dragging;
5575
5576 if (new_start != bar->start)
5577 {
5578 int new_end = new_start + bar->end - bar->start;
5579
5580 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5581 }
5582 }
5583 }
5584
5585 #endif /* !USE_TOOLKIT_SCROLL_BARS */
5586
5587 /* Return information to the user about the current position of the mouse
5588 on the scroll bar. */
5589
5590 static void
5591 x_scroll_bar_report_motion (struct frame **fp, Lisp_Object *bar_window,
5592 enum scroll_bar_part *part, Lisp_Object *x,
5593 Lisp_Object *y, Time *timestamp)
5594 {
5595 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
5596 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
5597 Window w = bar->x_window;
5598 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5599 int win_x, win_y;
5600 Window dummy_window;
5601 int dummy_coord;
5602 unsigned int dummy_mask;
5603
5604 block_input ();
5605
5606 /* Get the mouse's position relative to the scroll bar window, and
5607 report that. */
5608 if (XQueryPointer (FRAME_X_DISPLAY (f), w,
5609
5610 /* Root, child, root x and root y. */
5611 &dummy_window, &dummy_window,
5612 &dummy_coord, &dummy_coord,
5613
5614 /* Position relative to scroll bar. */
5615 &win_x, &win_y,
5616
5617 /* Mouse buttons and modifier keys. */
5618 &dummy_mask))
5619 {
5620 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
5621
5622 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
5623
5624 if (bar->dragging != -1)
5625 win_y -= bar->dragging;
5626
5627 if (win_y < 0)
5628 win_y = 0;
5629 if (win_y > top_range)
5630 win_y = top_range;
5631
5632 *fp = f;
5633 *bar_window = bar->window;
5634
5635 if (bar->dragging != -1)
5636 *part = scroll_bar_handle;
5637 else if (win_y < bar->start)
5638 *part = scroll_bar_above_handle;
5639 else if (win_y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5640 *part = scroll_bar_handle;
5641 else
5642 *part = scroll_bar_below_handle;
5643
5644 XSETINT (*x, win_y);
5645 XSETINT (*y, top_range);
5646
5647 f->mouse_moved = 0;
5648 dpyinfo->last_mouse_scroll_bar = NULL;
5649 *timestamp = dpyinfo->last_mouse_movement_time;
5650 }
5651
5652 unblock_input ();
5653 }
5654
5655
5656 /* The screen has been cleared so we may have changed foreground or
5657 background colors, and the scroll bars may need to be redrawn.
5658 Clear out the scroll bars, and ask for expose events, so we can
5659 redraw them. */
5660
5661 static void
5662 x_scroll_bar_clear (struct frame *f)
5663 {
5664 #ifndef USE_TOOLKIT_SCROLL_BARS
5665 Lisp_Object bar;
5666
5667 /* We can have scroll bars even if this is 0,
5668 if we just turned off scroll bar mode.
5669 But in that case we should not clear them. */
5670 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5671 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
5672 bar = XSCROLL_BAR (bar)->next)
5673 XClearArea (FRAME_X_DISPLAY (f),
5674 XSCROLL_BAR (bar)->x_window,
5675 0, 0, 0, 0, True);
5676 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5677 }
5678
5679 #ifdef ENABLE_CHECKING
5680
5681 /* Record the last 100 characters stored
5682 to help debug the loss-of-chars-during-GC problem. */
5683
5684 static int temp_index;
5685 static short temp_buffer[100];
5686
5687 #define STORE_KEYSYM_FOR_DEBUG(keysym) \
5688 if (temp_index == sizeof temp_buffer / sizeof (short)) \
5689 temp_index = 0; \
5690 temp_buffer[temp_index++] = (keysym)
5691
5692 #else /* not ENABLE_CHECKING */
5693
5694 #define STORE_KEYSYM_FOR_DEBUG(keysym) ((void)0)
5695
5696 #endif /* ENABLE_CHECKING */
5697
5698 /* Set this to nonzero to fake an "X I/O error"
5699 on a particular display. */
5700
5701 static struct x_display_info *XTread_socket_fake_io_error;
5702
5703 /* When we find no input here, we occasionally do a no-op command
5704 to verify that the X server is still running and we can still talk with it.
5705 We try all the open displays, one by one.
5706 This variable is used for cycling thru the displays. */
5707
5708 static struct x_display_info *next_noop_dpyinfo;
5709
5710 enum
5711 {
5712 X_EVENT_NORMAL,
5713 X_EVENT_GOTO_OUT,
5714 X_EVENT_DROP
5715 };
5716
5717 /* Filter events for the current X input method.
5718 DPYINFO is the display this event is for.
5719 EVENT is the X event to filter.
5720
5721 Returns non-zero if the event was filtered, caller shall not process
5722 this event further.
5723 Returns zero if event is wasn't filtered. */
5724
5725 #ifdef HAVE_X_I18N
5726 static int
5727 x_filter_event (struct x_display_info *dpyinfo, XEvent *event)
5728 {
5729 /* XFilterEvent returns non-zero if the input method has
5730 consumed the event. We pass the frame's X window to
5731 XFilterEvent because that's the one for which the IC
5732 was created. */
5733
5734 struct frame *f1 = x_any_window_to_frame (dpyinfo,
5735 event->xclient.window);
5736
5737 return XFilterEvent (event, f1 ? FRAME_X_WINDOW (f1) : None);
5738 }
5739 #endif
5740
5741 #ifdef USE_GTK
5742 static int current_count;
5743 static int current_finish;
5744 static struct input_event *current_hold_quit;
5745
5746 /* This is the filter function invoked by the GTK event loop.
5747 It is invoked before the XEvent is translated to a GdkEvent,
5748 so we have a chance to act on the event before GTK. */
5749 static GdkFilterReturn
5750 event_handler_gdk (GdkXEvent *gxev, GdkEvent *ev, gpointer data)
5751 {
5752 XEvent *xev = (XEvent *) gxev;
5753
5754 block_input ();
5755 if (current_count >= 0)
5756 {
5757 struct x_display_info *dpyinfo;
5758
5759 dpyinfo = x_display_info_for_display (xev->xany.display);
5760
5761 #ifdef HAVE_X_I18N
5762 /* Filter events for the current X input method.
5763 GTK calls XFilterEvent but not for key press and release,
5764 so we do it here. */
5765 if ((xev->type == KeyPress || xev->type == KeyRelease)
5766 && dpyinfo
5767 && x_filter_event (dpyinfo, xev))
5768 {
5769 unblock_input ();
5770 return GDK_FILTER_REMOVE;
5771 }
5772 #endif
5773
5774 if (! dpyinfo)
5775 current_finish = X_EVENT_NORMAL;
5776 else
5777 current_count +=
5778 handle_one_xevent (dpyinfo, xev, &current_finish,
5779 current_hold_quit);
5780 }
5781 else
5782 current_finish = x_dispatch_event (xev, xev->xany.display);
5783
5784 unblock_input ();
5785
5786 if (current_finish == X_EVENT_GOTO_OUT || current_finish == X_EVENT_DROP)
5787 return GDK_FILTER_REMOVE;
5788
5789 return GDK_FILTER_CONTINUE;
5790 }
5791 #endif /* USE_GTK */
5792
5793
5794 static void xembed_send_message (struct frame *f, Time,
5795 enum xembed_message,
5796 long detail, long data1, long data2);
5797
5798 /* Handles the XEvent EVENT on display DPYINFO.
5799
5800 *FINISH is X_EVENT_GOTO_OUT if caller should stop reading events.
5801 *FINISH is zero if caller should continue reading events.
5802 *FINISH is X_EVENT_DROP if event should not be passed to the toolkit.
5803 *EVENT is unchanged unless we're processing KeyPress event.
5804
5805 We return the number of characters stored into the buffer. */
5806
5807 static int
5808 handle_one_xevent (struct x_display_info *dpyinfo,
5809 const XEvent *event,
5810 int *finish, struct input_event *hold_quit)
5811 {
5812 union {
5813 struct input_event ie;
5814 struct selection_input_event sie;
5815 } inev;
5816 int count = 0;
5817 int do_help = 0;
5818 ptrdiff_t nbytes = 0;
5819 struct frame *any, *f = NULL;
5820 struct coding_system coding;
5821 Mouse_HLInfo *hlinfo = &dpyinfo->mouse_highlight;
5822 /* This holds the state XLookupString needs to implement dead keys
5823 and other tricks known as "compose processing". _X Window System_
5824 says that a portable program can't use this, but Stephen Gildea assures
5825 me that letting the compiler initialize it to zeros will work okay. */
5826 static XComposeStatus compose_status;
5827
5828 USE_SAFE_ALLOCA;
5829
5830 *finish = X_EVENT_NORMAL;
5831
5832 EVENT_INIT (inev.ie);
5833 inev.ie.kind = NO_EVENT;
5834 inev.ie.arg = Qnil;
5835
5836 any = x_any_window_to_frame (dpyinfo, event->xany.window);
5837
5838 if (any && any->wait_event_type == event->type)
5839 any->wait_event_type = 0; /* Indicates we got it. */
5840
5841 switch (event->type)
5842 {
5843 case ClientMessage:
5844 {
5845 if (event->xclient.message_type == dpyinfo->Xatom_wm_protocols
5846 && event->xclient.format == 32)
5847 {
5848 if (event->xclient.data.l[0] == dpyinfo->Xatom_wm_take_focus)
5849 {
5850 /* Use the value returned by x_any_window_to_frame
5851 because this could be the shell widget window
5852 if the frame has no title bar. */
5853 f = any;
5854 #ifdef HAVE_X_I18N
5855 /* Not quite sure this is needed -pd */
5856 if (f && FRAME_XIC (f))
5857 XSetICFocus (FRAME_XIC (f));
5858 #endif
5859 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
5860 instructs the WM to set the input focus automatically for
5861 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
5862 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
5863 it has set the focus. So, XSetInputFocus below is not
5864 needed.
5865
5866 The call to XSetInputFocus below has also caused trouble. In
5867 cases where the XSetInputFocus done by the WM and the one
5868 below are temporally close (on a fast machine), the call
5869 below can generate additional FocusIn events which confuse
5870 Emacs. */
5871
5872 /* Since we set WM_TAKE_FOCUS, we must call
5873 XSetInputFocus explicitly. But not if f is null,
5874 since that might be an event for a deleted frame. */
5875 if (f)
5876 {
5877 Display *d = event->xclient.display;
5878 /* Catch and ignore errors, in case window has been
5879 iconified by a window manager such as GWM. */
5880 x_catch_errors (d);
5881 XSetInputFocus (d, event->xclient.window,
5882 /* The ICCCM says this is
5883 the only valid choice. */
5884 RevertToParent,
5885 event->xclient.data.l[1]);
5886 /* This is needed to detect the error
5887 if there is an error. */
5888 XSync (d, False);
5889 x_uncatch_errors ();
5890 }
5891 /* Not certain about handling scroll bars here */
5892 #endif /* 0 */
5893 goto done;
5894 }
5895
5896 if (event->xclient.data.l[0] == dpyinfo->Xatom_wm_save_yourself)
5897 {
5898 /* Save state modify the WM_COMMAND property to
5899 something which can reinstate us. This notifies
5900 the session manager, who's looking for such a
5901 PropertyNotify. Can restart processing when
5902 a keyboard or mouse event arrives. */
5903 /* If we have a session manager, don't set this.
5904 KDE will then start two Emacsen, one for the
5905 session manager and one for this. */
5906 #ifdef HAVE_X_SM
5907 if (! x_session_have_connection ())
5908 #endif
5909 {
5910 f = x_top_window_to_frame (dpyinfo,
5911 event->xclient.window);
5912 /* This is just so we only give real data once
5913 for a single Emacs process. */
5914 if (f == SELECTED_FRAME ())
5915 XSetCommand (FRAME_X_DISPLAY (f),
5916 event->xclient.window,
5917 initial_argv, initial_argc);
5918 else if (f)
5919 XSetCommand (FRAME_X_DISPLAY (f),
5920 event->xclient.window,
5921 0, 0);
5922 }
5923 goto done;
5924 }
5925
5926 if (event->xclient.data.l[0] == dpyinfo->Xatom_wm_delete_window)
5927 {
5928 f = any;
5929 if (!f)
5930 goto OTHER; /* May be a dialog that is to be removed */
5931
5932 inev.ie.kind = DELETE_WINDOW_EVENT;
5933 XSETFRAME (inev.ie.frame_or_window, f);
5934 goto done;
5935 }
5936
5937 goto done;
5938 }
5939
5940 if (event->xclient.message_type == dpyinfo->Xatom_wm_configure_denied)
5941 goto done;
5942
5943 if (event->xclient.message_type == dpyinfo->Xatom_wm_window_moved)
5944 {
5945 int new_x, new_y;
5946 f = x_window_to_frame (dpyinfo, event->xclient.window);
5947
5948 new_x = event->xclient.data.s[0];
5949 new_y = event->xclient.data.s[1];
5950
5951 if (f)
5952 {
5953 f->left_pos = new_x;
5954 f->top_pos = new_y;
5955 }
5956 goto done;
5957 }
5958
5959 #ifdef HACK_EDITRES
5960 if (event->xclient.message_type == dpyinfo->Xatom_editres)
5961 {
5962 f = any;
5963 if (f)
5964 _XEditResCheckMessages (f->output_data.x->widget,
5965 NULL, (XEvent *) event, NULL);
5966 goto done;
5967 }
5968 #endif /* HACK_EDITRES */
5969
5970 if (event->xclient.message_type == dpyinfo->Xatom_DONE
5971 || event->xclient.message_type == dpyinfo->Xatom_PAGE)
5972 {
5973 /* Ghostview job completed. Kill it. We could
5974 reply with "Next" if we received "Page", but we
5975 currently never do because we are interested in
5976 images, only, which should have 1 page. */
5977 Pixmap pixmap = (Pixmap) event->xclient.data.l[1];
5978 f = x_window_to_frame (dpyinfo, event->xclient.window);
5979 if (!f)
5980 goto OTHER;
5981 x_kill_gs_process (pixmap, f);
5982 expose_frame (f, 0, 0, 0, 0);
5983 goto done;
5984 }
5985
5986 #ifdef USE_TOOLKIT_SCROLL_BARS
5987 /* Scroll bar callbacks send a ClientMessage from which
5988 we construct an input_event. */
5989 if (event->xclient.message_type == dpyinfo->Xatom_Scrollbar)
5990 {
5991 x_scroll_bar_to_input_event (event, &inev.ie);
5992 *finish = X_EVENT_GOTO_OUT;
5993 goto done;
5994 }
5995 #endif /* USE_TOOLKIT_SCROLL_BARS */
5996
5997 /* XEmbed messages from the embedder (if any). */
5998 if (event->xclient.message_type == dpyinfo->Xatom_XEMBED)
5999 {
6000 enum xembed_message msg = event->xclient.data.l[1];
6001 if (msg == XEMBED_FOCUS_IN || msg == XEMBED_FOCUS_OUT)
6002 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
6003
6004 *finish = X_EVENT_GOTO_OUT;
6005 goto done;
6006 }
6007
6008 xft_settings_event (dpyinfo, event);
6009
6010 f = any;
6011 if (!f)
6012 goto OTHER;
6013 if (x_handle_dnd_message (f, &event->xclient, dpyinfo, &inev.ie))
6014 *finish = X_EVENT_DROP;
6015 }
6016 break;
6017
6018 case SelectionNotify:
6019 dpyinfo->last_user_time = event->xselection.time;
6020 #ifdef USE_X_TOOLKIT
6021 if (! x_window_to_frame (dpyinfo, event->xselection.requestor))
6022 goto OTHER;
6023 #endif /* not USE_X_TOOLKIT */
6024 x_handle_selection_notify (&event->xselection);
6025 break;
6026
6027 case SelectionClear: /* Someone has grabbed ownership. */
6028 dpyinfo->last_user_time = event->xselectionclear.time;
6029 #ifdef USE_X_TOOLKIT
6030 if (! x_window_to_frame (dpyinfo, event->xselectionclear.window))
6031 goto OTHER;
6032 #endif /* USE_X_TOOLKIT */
6033 {
6034 const XSelectionClearEvent *eventp = &event->xselectionclear;
6035
6036 inev.ie.kind = SELECTION_CLEAR_EVENT;
6037 SELECTION_EVENT_DISPLAY (&inev.sie) = eventp->display;
6038 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
6039 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
6040 }
6041 break;
6042
6043 case SelectionRequest: /* Someone wants our selection. */
6044 dpyinfo->last_user_time = event->xselectionrequest.time;
6045 #ifdef USE_X_TOOLKIT
6046 if (!x_window_to_frame (dpyinfo, event->xselectionrequest.owner))
6047 goto OTHER;
6048 #endif /* USE_X_TOOLKIT */
6049 {
6050 const XSelectionRequestEvent *eventp = &event->xselectionrequest;
6051
6052 inev.ie.kind = SELECTION_REQUEST_EVENT;
6053 SELECTION_EVENT_DISPLAY (&inev.sie) = eventp->display;
6054 SELECTION_EVENT_REQUESTOR (&inev.sie) = eventp->requestor;
6055 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
6056 SELECTION_EVENT_TARGET (&inev.sie) = eventp->target;
6057 SELECTION_EVENT_PROPERTY (&inev.sie) = eventp->property;
6058 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
6059 }
6060 break;
6061
6062 case PropertyNotify:
6063 dpyinfo->last_user_time = event->xproperty.time;
6064 f = x_top_window_to_frame (dpyinfo, event->xproperty.window);
6065 if (f && event->xproperty.atom == dpyinfo->Xatom_net_wm_state)
6066 if (x_handle_net_wm_state (f, &event->xproperty)
6067 && FRAME_ICONIFIED_P (f)
6068 && f->output_data.x->net_wm_state_hidden_seen)
6069 {
6070 /* Gnome shell does not iconify us when C-z is pressed.
6071 It hides the frame. So if our state says we aren't
6072 hidden anymore, treat it as deiconified. */
6073 SET_FRAME_VISIBLE (f, 1);
6074 SET_FRAME_ICONIFIED (f, 0);
6075 f->output_data.x->has_been_visible = 1;
6076 f->output_data.x->net_wm_state_hidden_seen = 0;
6077 inev.ie.kind = DEICONIFY_EVENT;
6078 XSETFRAME (inev.ie.frame_or_window, f);
6079 }
6080
6081 x_handle_property_notify (&event->xproperty);
6082 xft_settings_event (dpyinfo, event);
6083 goto OTHER;
6084
6085 case ReparentNotify:
6086 f = x_top_window_to_frame (dpyinfo, event->xreparent.window);
6087 if (f)
6088 {
6089 f->output_data.x->parent_desc = event->xreparent.parent;
6090 x_real_positions (f, &f->left_pos, &f->top_pos);
6091
6092 /* Perhaps reparented due to a WM restart. Reset this. */
6093 FRAME_DISPLAY_INFO (f)->wm_type = X_WMTYPE_UNKNOWN;
6094 FRAME_DISPLAY_INFO (f)->net_supported_window = 0;
6095
6096 x_set_frame_alpha (f);
6097 }
6098 goto OTHER;
6099
6100 case Expose:
6101 f = x_window_to_frame (dpyinfo, event->xexpose.window);
6102 if (f)
6103 {
6104 if (!FRAME_VISIBLE_P (f))
6105 {
6106 SET_FRAME_VISIBLE (f, 1);
6107 SET_FRAME_ICONIFIED (f, 0);
6108 f->output_data.x->has_been_visible = 1;
6109 SET_FRAME_GARBAGED (f);
6110 }
6111 else
6112 {
6113 #ifdef USE_GTK
6114 /* This seems to be needed for GTK 2.6 and later, see
6115 http://debbugs.gnu.org/cgi/bugreport.cgi?bug=15398. */
6116 x_clear_area (event->xexpose.display,
6117 event->xexpose.window,
6118 event->xexpose.x, event->xexpose.y,
6119 event->xexpose.width, event->xexpose.height);
6120 #endif
6121 expose_frame (f, event->xexpose.x, event->xexpose.y,
6122 event->xexpose.width, event->xexpose.height);
6123 }
6124 }
6125 else
6126 {
6127 #ifndef USE_TOOLKIT_SCROLL_BARS
6128 struct scroll_bar *bar;
6129 #endif
6130 #if defined USE_LUCID
6131 /* Submenus of the Lucid menu bar aren't widgets
6132 themselves, so there's no way to dispatch events
6133 to them. Recognize this case separately. */
6134 {
6135 Widget widget = x_window_to_menu_bar (event->xexpose.window);
6136 if (widget)
6137 xlwmenu_redisplay (widget);
6138 }
6139 #endif /* USE_LUCID */
6140
6141 #ifdef USE_TOOLKIT_SCROLL_BARS
6142 /* Dispatch event to the widget. */
6143 goto OTHER;
6144 #else /* not USE_TOOLKIT_SCROLL_BARS */
6145 bar = x_window_to_scroll_bar (event->xexpose.display,
6146 event->xexpose.window);
6147
6148 if (bar)
6149 x_scroll_bar_expose (bar, event);
6150 #ifdef USE_X_TOOLKIT
6151 else
6152 goto OTHER;
6153 #endif /* USE_X_TOOLKIT */
6154 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6155 }
6156 break;
6157
6158 case GraphicsExpose: /* This occurs when an XCopyArea's
6159 source area was obscured or not
6160 available. */
6161 f = x_window_to_frame (dpyinfo, event->xgraphicsexpose.drawable);
6162 if (f)
6163 expose_frame (f, event->xgraphicsexpose.x,
6164 event->xgraphicsexpose.y,
6165 event->xgraphicsexpose.width,
6166 event->xgraphicsexpose.height);
6167 #ifdef USE_X_TOOLKIT
6168 else
6169 goto OTHER;
6170 #endif /* USE_X_TOOLKIT */
6171 break;
6172
6173 case NoExpose: /* This occurs when an XCopyArea's
6174 source area was completely
6175 available. */
6176 break;
6177
6178 case UnmapNotify:
6179 /* Redo the mouse-highlight after the tooltip has gone. */
6180 if (event->xunmap.window == tip_window)
6181 {
6182 tip_window = 0;
6183 x_redo_mouse_highlight (dpyinfo);
6184 }
6185
6186 f = x_top_window_to_frame (dpyinfo, event->xunmap.window);
6187 if (f) /* F may no longer exist if
6188 the frame was deleted. */
6189 {
6190 bool visible = FRAME_VISIBLE_P (f);
6191 /* While a frame is unmapped, display generation is
6192 disabled; you don't want to spend time updating a
6193 display that won't ever be seen. */
6194 SET_FRAME_VISIBLE (f, 0);
6195 /* We can't distinguish, from the event, whether the window
6196 has become iconified or invisible. So assume, if it
6197 was previously visible, than now it is iconified.
6198 But x_make_frame_invisible clears both
6199 the visible flag and the iconified flag;
6200 and that way, we know the window is not iconified now. */
6201 if (visible || FRAME_ICONIFIED_P (f))
6202 {
6203 SET_FRAME_ICONIFIED (f, 1);
6204 inev.ie.kind = ICONIFY_EVENT;
6205 XSETFRAME (inev.ie.frame_or_window, f);
6206 }
6207 }
6208 goto OTHER;
6209
6210 case MapNotify:
6211 if (event->xmap.window == tip_window)
6212 /* The tooltip has been drawn already. Avoid
6213 the SET_FRAME_GARBAGED below. */
6214 goto OTHER;
6215
6216 /* We use x_top_window_to_frame because map events can
6217 come for sub-windows and they don't mean that the
6218 frame is visible. */
6219 f = x_top_window_to_frame (dpyinfo, event->xmap.window);
6220 if (f)
6221 {
6222 bool iconified = FRAME_ICONIFIED_P (f);
6223 /* wait_reading_process_output will notice this and update
6224 the frame's display structures.
6225 If we where iconified, we should not set garbaged,
6226 because that stops redrawing on Expose events. This looks
6227 bad if we are called from a recursive event loop
6228 (x_dispatch_event), for example when a dialog is up. */
6229 if (!iconified)
6230 SET_FRAME_GARBAGED (f);
6231
6232 /* Check if fullscreen was specified before we where mapped the
6233 first time, i.e. from the command line. */
6234 if (!f->output_data.x->has_been_visible)
6235 x_check_fullscreen (f);
6236
6237 SET_FRAME_VISIBLE (f, 1);
6238 SET_FRAME_ICONIFIED (f, 0);
6239 f->output_data.x->has_been_visible = 1;
6240
6241 if (iconified)
6242 {
6243 inev.ie.kind = DEICONIFY_EVENT;
6244 XSETFRAME (inev.ie.frame_or_window, f);
6245 }
6246 else if (! NILP (Vframe_list) && ! NILP (XCDR (Vframe_list)))
6247 /* Force a redisplay sooner or later to update the
6248 frame titles in case this is the second frame. */
6249 record_asynch_buffer_change ();
6250
6251 #ifdef USE_GTK
6252 xg_frame_resized (f, -1, -1);
6253 #endif
6254 }
6255 goto OTHER;
6256
6257 case KeyPress:
6258
6259 dpyinfo->last_user_time = event->xkey.time;
6260 ignore_next_mouse_click_timeout = 0;
6261
6262 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6263 /* Dispatch KeyPress events when in menu. */
6264 if (popup_activated ())
6265 goto OTHER;
6266 #endif
6267
6268 f = any;
6269
6270 #if ! defined (USE_GTK)
6271 /* If mouse-highlight is an integer, input clears out
6272 mouse highlighting. */
6273 if (!hlinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
6274 && (f == 0
6275 || !EQ (f->tool_bar_window, hlinfo->mouse_face_window)))
6276 {
6277 clear_mouse_face (hlinfo);
6278 hlinfo->mouse_face_hidden = 1;
6279 }
6280 #endif
6281
6282 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
6283 if (f == 0)
6284 {
6285 /* Scroll bars consume key events, but we want
6286 the keys to go to the scroll bar's frame. */
6287 Widget widget = XtWindowToWidget (dpyinfo->display,
6288 event->xkey.window);
6289 if (widget && XmIsScrollBar (widget))
6290 {
6291 widget = XtParent (widget);
6292 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
6293 }
6294 }
6295 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
6296
6297 if (f != 0)
6298 {
6299 KeySym keysym, orig_keysym;
6300 /* al%imercury@uunet.uu.net says that making this 81
6301 instead of 80 fixed a bug whereby meta chars made
6302 his Emacs hang.
6303
6304 It seems that some version of XmbLookupString has
6305 a bug of not returning XBufferOverflow in
6306 status_return even if the input is too long to
6307 fit in 81 bytes. So, we must prepare sufficient
6308 bytes for copy_buffer. 513 bytes (256 chars for
6309 two-byte character set) seems to be a fairly good
6310 approximation. -- 2000.8.10 handa@etl.go.jp */
6311 unsigned char copy_buffer[513];
6312 unsigned char *copy_bufptr = copy_buffer;
6313 int copy_bufsiz = sizeof (copy_buffer);
6314 int modifiers;
6315 Lisp_Object coding_system = Qlatin_1;
6316 Lisp_Object c;
6317 /* Event will be modified. */
6318 XKeyEvent xkey = event->xkey;
6319
6320 #ifdef USE_GTK
6321 /* Don't pass keys to GTK. A Tab will shift focus to the
6322 tool bar in GTK 2.4. Keys will still go to menus and
6323 dialogs because in that case popup_activated is TRUE
6324 (see above). */
6325 *finish = X_EVENT_DROP;
6326 #endif
6327
6328 xkey.state |= x_emacs_to_x_modifiers (FRAME_DISPLAY_INFO (f),
6329 extra_keyboard_modifiers);
6330 modifiers = xkey.state;
6331
6332 /* This will have to go some day... */
6333
6334 /* make_lispy_event turns chars into control chars.
6335 Don't do it here because XLookupString is too eager. */
6336 xkey.state &= ~ControlMask;
6337 xkey.state &= ~(dpyinfo->meta_mod_mask
6338 | dpyinfo->super_mod_mask
6339 | dpyinfo->hyper_mod_mask
6340 | dpyinfo->alt_mod_mask);
6341
6342 /* In case Meta is ComposeCharacter,
6343 clear its status. According to Markus Ehrnsperger
6344 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
6345 this enables ComposeCharacter to work whether or
6346 not it is combined with Meta. */
6347 if (modifiers & dpyinfo->meta_mod_mask)
6348 memset (&compose_status, 0, sizeof (compose_status));
6349
6350 #ifdef HAVE_X_I18N
6351 if (FRAME_XIC (f))
6352 {
6353 Status status_return;
6354
6355 coding_system = Vlocale_coding_system;
6356 nbytes = XmbLookupString (FRAME_XIC (f),
6357 &xkey, (char *) copy_bufptr,
6358 copy_bufsiz, &keysym,
6359 &status_return);
6360 if (status_return == XBufferOverflow)
6361 {
6362 copy_bufsiz = nbytes + 1;
6363 copy_bufptr = alloca (copy_bufsiz);
6364 nbytes = XmbLookupString (FRAME_XIC (f),
6365 &xkey, (char *) copy_bufptr,
6366 copy_bufsiz, &keysym,
6367 &status_return);
6368 }
6369 /* Xutf8LookupString is a new but already deprecated interface. -stef */
6370 if (status_return == XLookupNone)
6371 break;
6372 else if (status_return == XLookupChars)
6373 {
6374 keysym = NoSymbol;
6375 modifiers = 0;
6376 }
6377 else if (status_return != XLookupKeySym
6378 && status_return != XLookupBoth)
6379 emacs_abort ();
6380 }
6381 else
6382 nbytes = XLookupString (&xkey, (char *) copy_bufptr,
6383 copy_bufsiz, &keysym,
6384 &compose_status);
6385 #else
6386 nbytes = XLookupString (&xkey, (char *) copy_bufptr,
6387 copy_bufsiz, &keysym,
6388 &compose_status);
6389 #endif
6390
6391 /* If not using XIM/XIC, and a compose sequence is in progress,
6392 we break here. Otherwise, chars_matched is always 0. */
6393 if (compose_status.chars_matched > 0 && nbytes == 0)
6394 break;
6395
6396 memset (&compose_status, 0, sizeof (compose_status));
6397 orig_keysym = keysym;
6398
6399 /* Common for all keysym input events. */
6400 XSETFRAME (inev.ie.frame_or_window, f);
6401 inev.ie.modifiers
6402 = x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO (f), modifiers);
6403 inev.ie.timestamp = xkey.time;
6404
6405 /* First deal with keysyms which have defined
6406 translations to characters. */
6407 if (keysym >= 32 && keysym < 128)
6408 /* Avoid explicitly decoding each ASCII character. */
6409 {
6410 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
6411 inev.ie.code = keysym;
6412 goto done_keysym;
6413 }
6414
6415 /* Keysyms directly mapped to Unicode characters. */
6416 if (keysym >= 0x01000000 && keysym <= 0x0110FFFF)
6417 {
6418 if (keysym < 0x01000080)
6419 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
6420 else
6421 inev.ie.kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
6422 inev.ie.code = keysym & 0xFFFFFF;
6423 goto done_keysym;
6424 }
6425
6426 /* Now non-ASCII. */
6427 if (HASH_TABLE_P (Vx_keysym_table)
6428 && (c = Fgethash (make_number (keysym),
6429 Vx_keysym_table,
6430 Qnil),
6431 NATNUMP (c)))
6432 {
6433 inev.ie.kind = (SINGLE_BYTE_CHAR_P (XFASTINT (c))
6434 ? ASCII_KEYSTROKE_EVENT
6435 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6436 inev.ie.code = XFASTINT (c);
6437 goto done_keysym;
6438 }
6439
6440 /* Random non-modifier sorts of keysyms. */
6441 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
6442 || keysym == XK_Delete
6443 #ifdef XK_ISO_Left_Tab
6444 || (keysym >= XK_ISO_Left_Tab
6445 && keysym <= XK_ISO_Enter)
6446 #endif
6447 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
6448 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
6449 #ifdef HPUX
6450 /* This recognizes the "extended function
6451 keys". It seems there's no cleaner way.
6452 Test IsModifierKey to avoid handling
6453 mode_switch incorrectly. */
6454 || (XK_Select <= keysym && keysym < XK_KP_Space)
6455 #endif
6456 #ifdef XK_dead_circumflex
6457 || orig_keysym == XK_dead_circumflex
6458 #endif
6459 #ifdef XK_dead_grave
6460 || orig_keysym == XK_dead_grave
6461 #endif
6462 #ifdef XK_dead_tilde
6463 || orig_keysym == XK_dead_tilde
6464 #endif
6465 #ifdef XK_dead_diaeresis
6466 || orig_keysym == XK_dead_diaeresis
6467 #endif
6468 #ifdef XK_dead_macron
6469 || orig_keysym == XK_dead_macron
6470 #endif
6471 #ifdef XK_dead_degree
6472 || orig_keysym == XK_dead_degree
6473 #endif
6474 #ifdef XK_dead_acute
6475 || orig_keysym == XK_dead_acute
6476 #endif
6477 #ifdef XK_dead_cedilla
6478 || orig_keysym == XK_dead_cedilla
6479 #endif
6480 #ifdef XK_dead_breve
6481 || orig_keysym == XK_dead_breve
6482 #endif
6483 #ifdef XK_dead_ogonek
6484 || orig_keysym == XK_dead_ogonek
6485 #endif
6486 #ifdef XK_dead_caron
6487 || orig_keysym == XK_dead_caron
6488 #endif
6489 #ifdef XK_dead_doubleacute
6490 || orig_keysym == XK_dead_doubleacute
6491 #endif
6492 #ifdef XK_dead_abovedot
6493 || orig_keysym == XK_dead_abovedot
6494 #endif
6495 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
6496 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
6497 /* Any "vendor-specific" key is ok. */
6498 || (orig_keysym & (1 << 28))
6499 || (keysym != NoSymbol && nbytes == 0))
6500 && ! (IsModifierKey (orig_keysym)
6501 /* The symbols from XK_ISO_Lock
6502 to XK_ISO_Last_Group_Lock
6503 don't have real modifiers but
6504 should be treated similarly to
6505 Mode_switch by Emacs. */
6506 #if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
6507 || (XK_ISO_Lock <= orig_keysym
6508 && orig_keysym <= XK_ISO_Last_Group_Lock)
6509 #endif
6510 ))
6511 {
6512 STORE_KEYSYM_FOR_DEBUG (keysym);
6513 /* make_lispy_event will convert this to a symbolic
6514 key. */
6515 inev.ie.kind = NON_ASCII_KEYSTROKE_EVENT;
6516 inev.ie.code = keysym;
6517 goto done_keysym;
6518 }
6519
6520 { /* Raw bytes, not keysym. */
6521 ptrdiff_t i;
6522 int nchars, len;
6523
6524 for (i = 0, nchars = 0; i < nbytes; i++)
6525 {
6526 if (ASCII_BYTE_P (copy_bufptr[i]))
6527 nchars++;
6528 STORE_KEYSYM_FOR_DEBUG (copy_bufptr[i]);
6529 }
6530
6531 if (nchars < nbytes)
6532 {
6533 /* Decode the input data. */
6534
6535 /* The input should be decoded with `coding_system'
6536 which depends on which X*LookupString function
6537 we used just above and the locale. */
6538 setup_coding_system (coding_system, &coding);
6539 coding.src_multibyte = 0;
6540 coding.dst_multibyte = 1;
6541 /* The input is converted to events, thus we can't
6542 handle composition. Anyway, there's no XIM that
6543 gives us composition information. */
6544 coding.common_flags &= ~CODING_ANNOTATION_MASK;
6545
6546 SAFE_NALLOCA (coding.destination, MAX_MULTIBYTE_LENGTH,
6547 nbytes);
6548 coding.dst_bytes = MAX_MULTIBYTE_LENGTH * nbytes;
6549 coding.mode |= CODING_MODE_LAST_BLOCK;
6550 decode_coding_c_string (&coding, copy_bufptr, nbytes, Qnil);
6551 nbytes = coding.produced;
6552 nchars = coding.produced_char;
6553 copy_bufptr = coding.destination;
6554 }
6555
6556 /* Convert the input data to a sequence of
6557 character events. */
6558 for (i = 0; i < nbytes; i += len)
6559 {
6560 int ch;
6561 if (nchars == nbytes)
6562 ch = copy_bufptr[i], len = 1;
6563 else
6564 ch = STRING_CHAR_AND_LENGTH (copy_bufptr + i, len);
6565 inev.ie.kind = (SINGLE_BYTE_CHAR_P (ch)
6566 ? ASCII_KEYSTROKE_EVENT
6567 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6568 inev.ie.code = ch;
6569 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
6570 }
6571
6572 count += nchars;
6573
6574 inev.ie.kind = NO_EVENT; /* Already stored above. */
6575
6576 if (keysym == NoSymbol)
6577 break;
6578 }
6579 /* FIXME: check side effects and remove this. */
6580 ((XEvent *) event)->xkey = xkey;
6581 }
6582 done_keysym:
6583 #ifdef HAVE_X_I18N
6584 /* Don't dispatch this event since XtDispatchEvent calls
6585 XFilterEvent, and two calls in a row may freeze the
6586 client. */
6587 break;
6588 #else
6589 goto OTHER;
6590 #endif
6591
6592 case KeyRelease:
6593 dpyinfo->last_user_time = event->xkey.time;
6594 #ifdef HAVE_X_I18N
6595 /* Don't dispatch this event since XtDispatchEvent calls
6596 XFilterEvent, and two calls in a row may freeze the
6597 client. */
6598 break;
6599 #else
6600 goto OTHER;
6601 #endif
6602
6603 case EnterNotify:
6604 dpyinfo->last_user_time = event->xcrossing.time;
6605 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
6606
6607 f = any;
6608
6609 if (f && x_mouse_click_focus_ignore_position)
6610 ignore_next_mouse_click_timeout = event->xmotion.time + 200;
6611
6612 /* EnterNotify counts as mouse movement,
6613 so update things that depend on mouse position. */
6614 if (f && !f->output_data.x->hourglass_p)
6615 note_mouse_movement (f, &event->xmotion);
6616 #ifdef USE_GTK
6617 /* We may get an EnterNotify on the buttons in the toolbar. In that
6618 case we moved out of any highlighted area and need to note this. */
6619 if (!f && dpyinfo->last_mouse_glyph_frame)
6620 note_mouse_movement (dpyinfo->last_mouse_glyph_frame, &event->xmotion);
6621 #endif
6622 goto OTHER;
6623
6624 case FocusIn:
6625 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
6626 goto OTHER;
6627
6628 case LeaveNotify:
6629 dpyinfo->last_user_time = event->xcrossing.time;
6630 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
6631
6632 f = x_top_window_to_frame (dpyinfo, event->xcrossing.window);
6633 if (f)
6634 {
6635 if (f == hlinfo->mouse_face_mouse_frame)
6636 {
6637 /* If we move outside the frame, then we're
6638 certainly no longer on any text in the frame. */
6639 clear_mouse_face (hlinfo);
6640 hlinfo->mouse_face_mouse_frame = 0;
6641 }
6642
6643 /* Generate a nil HELP_EVENT to cancel a help-echo.
6644 Do it only if there's something to cancel.
6645 Otherwise, the startup message is cleared when
6646 the mouse leaves the frame. */
6647 if (any_help_event_p)
6648 do_help = -1;
6649 }
6650 #ifdef USE_GTK
6651 /* See comment in EnterNotify above */
6652 else if (dpyinfo->last_mouse_glyph_frame)
6653 note_mouse_movement (dpyinfo->last_mouse_glyph_frame, &event->xmotion);
6654 #endif
6655 goto OTHER;
6656
6657 case FocusOut:
6658 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
6659 goto OTHER;
6660
6661 case MotionNotify:
6662 {
6663 dpyinfo->last_user_time = event->xmotion.time;
6664 previous_help_echo_string = help_echo_string;
6665 help_echo_string = Qnil;
6666
6667 f = (x_mouse_grabbed (dpyinfo) ? dpyinfo->last_mouse_frame
6668 : x_window_to_frame (dpyinfo, event->xmotion.window));
6669
6670 if (hlinfo->mouse_face_hidden)
6671 {
6672 hlinfo->mouse_face_hidden = 0;
6673 clear_mouse_face (hlinfo);
6674 }
6675
6676 #ifdef USE_GTK
6677 if (f && xg_event_is_for_scrollbar (f, event))
6678 f = 0;
6679 #endif
6680 if (f)
6681 {
6682
6683 /* Generate SELECT_WINDOW_EVENTs when needed.
6684 Don't let popup menus influence things (bug#1261). */
6685 if (!NILP (Vmouse_autoselect_window) && !popup_activated ())
6686 {
6687 static Lisp_Object last_mouse_window;
6688 Lisp_Object window = window_from_coordinates
6689 (f, event->xmotion.x, event->xmotion.y, 0, 0);
6690
6691 /* Window will be selected only when it is not selected now and
6692 last mouse movement event was not in it. Minibuffer window
6693 will be selected only when it is active. */
6694 if (WINDOWP (window)
6695 && !EQ (window, last_mouse_window)
6696 && !EQ (window, selected_window)
6697 /* For click-to-focus window managers
6698 create event iff we don't leave the
6699 selected frame. */
6700 && (focus_follows_mouse
6701 || (EQ (XWINDOW (window)->frame,
6702 XWINDOW (selected_window)->frame))))
6703 {
6704 inev.ie.kind = SELECT_WINDOW_EVENT;
6705 inev.ie.frame_or_window = window;
6706 }
6707 /* Remember the last window where we saw the mouse. */
6708 last_mouse_window = window;
6709 }
6710 if (!note_mouse_movement (f, &event->xmotion))
6711 help_echo_string = previous_help_echo_string;
6712 }
6713 else
6714 {
6715 #ifndef USE_TOOLKIT_SCROLL_BARS
6716 struct scroll_bar *bar
6717 = x_window_to_scroll_bar (event->xmotion.display,
6718 event->xmotion.window);
6719
6720 if (bar)
6721 x_scroll_bar_note_movement (bar, &event->xmotion);
6722 #endif /* USE_TOOLKIT_SCROLL_BARS */
6723
6724 /* If we move outside the frame, then we're
6725 certainly no longer on any text in the frame. */
6726 clear_mouse_face (hlinfo);
6727 }
6728
6729 /* If the contents of the global variable help_echo_string
6730 has changed, generate a HELP_EVENT. */
6731 if (!NILP (help_echo_string)
6732 || !NILP (previous_help_echo_string))
6733 do_help = 1;
6734 goto OTHER;
6735 }
6736
6737 case ConfigureNotify:
6738 f = x_top_window_to_frame (dpyinfo, event->xconfigure.window);
6739 #ifdef USE_GTK
6740 if (!f
6741 && (f = any)
6742 && event->xconfigure.window == FRAME_X_WINDOW (f))
6743 {
6744 xg_frame_resized (f, event->xconfigure.width,
6745 event->xconfigure.height);
6746 f = 0;
6747 }
6748 #endif
6749 if (f)
6750 {
6751 #ifndef USE_X_TOOLKIT
6752 #ifndef USE_GTK
6753 int rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, event->xconfigure.height);
6754 int columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, event->xconfigure.width);
6755
6756 /* In the toolkit version, change_frame_size
6757 is called by the code that handles resizing
6758 of the EmacsFrame widget. */
6759
6760 /* Even if the number of character rows and columns has
6761 not changed, the font size may have changed, so we need
6762 to check the pixel dimensions as well. */
6763 if (columns != FRAME_COLS (f)
6764 || rows != FRAME_LINES (f)
6765 || event->xconfigure.width != FRAME_PIXEL_WIDTH (f)
6766 || event->xconfigure.height != FRAME_PIXEL_HEIGHT (f))
6767 {
6768 change_frame_size (f, rows, columns, 0, 1, 0);
6769 SET_FRAME_GARBAGED (f);
6770 cancel_mouse_face (f);
6771 }
6772
6773 FRAME_PIXEL_WIDTH (f) = event->xconfigure.width;
6774 FRAME_PIXEL_HEIGHT (f) = event->xconfigure.height;
6775 #endif /* not USE_GTK */
6776 #endif
6777
6778 #ifdef USE_GTK
6779 /* GTK creates windows but doesn't map them.
6780 Only get real positions when mapped. */
6781 if (FRAME_GTK_OUTER_WIDGET (f)
6782 && gtk_widget_get_mapped (FRAME_GTK_OUTER_WIDGET (f)))
6783 #endif
6784 x_real_positions (f, &f->left_pos, &f->top_pos);
6785
6786 #ifdef HAVE_X_I18N
6787 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
6788 xic_set_statusarea (f);
6789 #endif
6790
6791 }
6792 goto OTHER;
6793
6794 case ButtonRelease:
6795 case ButtonPress:
6796 {
6797 /* If we decide we want to generate an event to be seen
6798 by the rest of Emacs, we put it here. */
6799 bool tool_bar_p = 0;
6800
6801 memset (&compose_status, 0, sizeof (compose_status));
6802 dpyinfo->last_mouse_glyph_frame = NULL;
6803 dpyinfo->last_user_time = event->xbutton.time;
6804
6805 f = (x_mouse_grabbed (dpyinfo) ? dpyinfo->last_mouse_frame
6806 : x_window_to_frame (dpyinfo, event->xbutton.window));
6807
6808 #ifdef USE_GTK
6809 if (f && xg_event_is_for_scrollbar (f, event))
6810 f = 0;
6811 #endif
6812 if (f)
6813 {
6814 #if ! defined (USE_GTK)
6815 /* Is this in the tool-bar? */
6816 if (WINDOWP (f->tool_bar_window)
6817 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
6818 {
6819 Lisp_Object window;
6820 int x = event->xbutton.x;
6821 int y = event->xbutton.y;
6822
6823 window = window_from_coordinates (f, x, y, 0, 1);
6824 tool_bar_p = EQ (window, f->tool_bar_window);
6825
6826 if (tool_bar_p && event->xbutton.button < 4)
6827 handle_tool_bar_click
6828 (f, x, y, event->xbutton.type == ButtonPress,
6829 x_x_to_emacs_modifiers (dpyinfo, event->xbutton.state));
6830 }
6831 #endif /* !USE_GTK */
6832
6833 if (!tool_bar_p)
6834 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6835 if (! popup_activated ())
6836 #endif
6837 {
6838 if (ignore_next_mouse_click_timeout)
6839 {
6840 if (event->type == ButtonPress
6841 && event->xbutton.time > ignore_next_mouse_click_timeout)
6842 {
6843 ignore_next_mouse_click_timeout = 0;
6844 construct_mouse_click (&inev.ie, &event->xbutton, f);
6845 }
6846 if (event->type == ButtonRelease)
6847 ignore_next_mouse_click_timeout = 0;
6848 }
6849 else
6850 construct_mouse_click (&inev.ie, &event->xbutton, f);
6851 }
6852 if (FRAME_X_EMBEDDED_P (f))
6853 xembed_send_message (f, event->xbutton.time,
6854 XEMBED_REQUEST_FOCUS, 0, 0, 0);
6855 }
6856 else
6857 {
6858 struct scroll_bar *bar
6859 = x_window_to_scroll_bar (event->xbutton.display,
6860 event->xbutton.window);
6861
6862 #ifdef USE_TOOLKIT_SCROLL_BARS
6863 /* Make the "Ctrl-Mouse-2 splits window" work for toolkit
6864 scroll bars. */
6865 if (bar && event->xbutton.state & ControlMask)
6866 {
6867 x_scroll_bar_handle_click (bar, event, &inev.ie);
6868 *finish = X_EVENT_DROP;
6869 }
6870 #else /* not USE_TOOLKIT_SCROLL_BARS */
6871 if (bar)
6872 x_scroll_bar_handle_click (bar, event, &inev.ie);
6873 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6874 }
6875
6876 if (event->type == ButtonPress)
6877 {
6878 dpyinfo->grabbed |= (1 << event->xbutton.button);
6879 dpyinfo->last_mouse_frame = f;
6880
6881 if (!tool_bar_p)
6882 last_tool_bar_item = -1;
6883 }
6884 else
6885 dpyinfo->grabbed &= ~(1 << event->xbutton.button);
6886
6887 /* Ignore any mouse motion that happened before this event;
6888 any subsequent mouse-movement Emacs events should reflect
6889 only motion after the ButtonPress/Release. */
6890 if (f != 0)
6891 f->mouse_moved = 0;
6892
6893 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6894 f = x_menubar_window_to_frame (dpyinfo, event);
6895 /* For a down-event in the menu bar,
6896 don't pass it to Xt right now.
6897 Instead, save it away
6898 and we will pass it to Xt from kbd_buffer_get_event.
6899 That way, we can run some Lisp code first. */
6900 if (! popup_activated ()
6901 #ifdef USE_GTK
6902 /* Gtk+ menus only react to the first three buttons. */
6903 && event->xbutton.button < 3
6904 #endif
6905 && f && event->type == ButtonPress
6906 /* Verify the event is really within the menu bar
6907 and not just sent to it due to grabbing. */
6908 && event->xbutton.x >= 0
6909 && event->xbutton.x < FRAME_PIXEL_WIDTH (f)
6910 && event->xbutton.y >= 0
6911 && event->xbutton.y < f->output_data.x->menubar_height
6912 && event->xbutton.same_screen)
6913 {
6914 if (!f->output_data.x->saved_menu_event)
6915 f->output_data.x->saved_menu_event = xmalloc (sizeof *event);
6916 *f->output_data.x->saved_menu_event = *event;
6917 inev.ie.kind = MENU_BAR_ACTIVATE_EVENT;
6918 XSETFRAME (inev.ie.frame_or_window, f);
6919 *finish = X_EVENT_DROP;
6920 }
6921 else
6922 goto OTHER;
6923 #endif /* USE_X_TOOLKIT || USE_GTK */
6924 }
6925 break;
6926
6927 case CirculateNotify:
6928 goto OTHER;
6929
6930 case CirculateRequest:
6931 goto OTHER;
6932
6933 case VisibilityNotify:
6934 goto OTHER;
6935
6936 case MappingNotify:
6937 /* Someone has changed the keyboard mapping - update the
6938 local cache. */
6939 switch (event->xmapping.request)
6940 {
6941 case MappingModifier:
6942 x_find_modifier_meanings (dpyinfo);
6943 /* This is meant to fall through. */
6944 case MappingKeyboard:
6945 XRefreshKeyboardMapping ((XMappingEvent *) &event->xmapping);
6946 }
6947 goto OTHER;
6948
6949 case DestroyNotify:
6950 xft_settings_event (dpyinfo, event);
6951 break;
6952
6953 default:
6954 OTHER:
6955 #ifdef USE_X_TOOLKIT
6956 block_input ();
6957 if (*finish != X_EVENT_DROP)
6958 XtDispatchEvent ((XEvent *) event);
6959 unblock_input ();
6960 #endif /* USE_X_TOOLKIT */
6961 break;
6962 }
6963
6964 done:
6965 if (inev.ie.kind != NO_EVENT)
6966 {
6967 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
6968 count++;
6969 }
6970
6971 if (do_help
6972 && !(hold_quit && hold_quit->kind != NO_EVENT))
6973 {
6974 Lisp_Object frame;
6975
6976 if (f)
6977 XSETFRAME (frame, f);
6978 else
6979 frame = Qnil;
6980
6981 if (do_help > 0)
6982 {
6983 any_help_event_p = 1;
6984 gen_help_event (help_echo_string, frame, help_echo_window,
6985 help_echo_object, help_echo_pos);
6986 }
6987 else
6988 {
6989 help_echo_string = Qnil;
6990 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
6991 }
6992 count++;
6993 }
6994
6995 SAFE_FREE ();
6996 return count;
6997 }
6998
6999 #if defined USE_GTK || defined USE_X_TOOLKIT
7000
7001 /* Handles the XEvent EVENT on display DISPLAY.
7002 This is used for event loops outside the normal event handling,
7003 i.e. looping while a popup menu or a dialog is posted.
7004
7005 Returns the value handle_one_xevent sets in the finish argument. */
7006 int
7007 x_dispatch_event (XEvent *event, Display *display)
7008 {
7009 struct x_display_info *dpyinfo;
7010 int finish = X_EVENT_NORMAL;
7011
7012 dpyinfo = x_display_info_for_display (display);
7013
7014 if (dpyinfo)
7015 handle_one_xevent (dpyinfo, event, &finish, 0);
7016
7017 return finish;
7018 }
7019 #endif
7020
7021
7022 /* Read events coming from the X server.
7023 Return as soon as there are no more events to be read.
7024
7025 Return the number of characters stored into the buffer,
7026 thus pretending to be `read' (except the characters we store
7027 in the keyboard buffer can be multibyte, so are not necessarily
7028 C chars). */
7029
7030 static int
7031 XTread_socket (struct terminal *terminal, struct input_event *hold_quit)
7032 {
7033 int count = 0;
7034 int event_found = 0;
7035 struct x_display_info *dpyinfo = terminal->display_info.x;
7036
7037 block_input ();
7038
7039 /* So people can tell when we have read the available input. */
7040 input_signal_count++;
7041
7042 /* For debugging, this gives a way to fake an I/O error. */
7043 if (dpyinfo == XTread_socket_fake_io_error)
7044 {
7045 XTread_socket_fake_io_error = 0;
7046 x_io_error_quitter (dpyinfo->display);
7047 }
7048
7049 #ifndef USE_GTK
7050 while (XPending (dpyinfo->display))
7051 {
7052 int finish;
7053 XEvent event;
7054
7055 XNextEvent (dpyinfo->display, &event);
7056
7057 #ifdef HAVE_X_I18N
7058 /* Filter events for the current X input method. */
7059 if (x_filter_event (dpyinfo, &event))
7060 continue;
7061 #endif
7062 event_found = 1;
7063
7064 count += handle_one_xevent (dpyinfo, &event, &finish, hold_quit);
7065
7066 if (finish == X_EVENT_GOTO_OUT)
7067 break;
7068 }
7069
7070 #else /* USE_GTK */
7071
7072 /* For GTK we must use the GTK event loop. But XEvents gets passed
7073 to our filter function above, and then to the big event switch.
7074 We use a bunch of globals to communicate with our filter function,
7075 that is kind of ugly, but it works.
7076
7077 There is no way to do one display at the time, GTK just does events
7078 from all displays. */
7079
7080 while (gtk_events_pending ())
7081 {
7082 current_count = count;
7083 current_hold_quit = hold_quit;
7084
7085 gtk_main_iteration ();
7086
7087 count = current_count;
7088 current_count = -1;
7089 current_hold_quit = 0;
7090
7091 if (current_finish == X_EVENT_GOTO_OUT)
7092 break;
7093 }
7094 #endif /* USE_GTK */
7095
7096 /* On some systems, an X bug causes Emacs to get no more events
7097 when the window is destroyed. Detect that. (1994.) */
7098 if (! event_found)
7099 {
7100 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
7101 One XNOOP in 100 loops will make Emacs terminate.
7102 B. Bretthauer, 1994 */
7103 x_noop_count++;
7104 if (x_noop_count >= 100)
7105 {
7106 x_noop_count=0;
7107
7108 if (next_noop_dpyinfo == 0)
7109 next_noop_dpyinfo = x_display_list;
7110
7111 XNoOp (next_noop_dpyinfo->display);
7112
7113 /* Each time we get here, cycle through the displays now open. */
7114 next_noop_dpyinfo = next_noop_dpyinfo->next;
7115 }
7116 }
7117
7118 /* If the focus was just given to an auto-raising frame,
7119 raise it now. FIXME: handle more than one such frame. */
7120 if (dpyinfo->x_pending_autoraise_frame)
7121 {
7122 x_raise_frame (dpyinfo->x_pending_autoraise_frame);
7123 dpyinfo->x_pending_autoraise_frame = NULL;
7124 }
7125
7126 unblock_input ();
7127
7128 return count;
7129 }
7130
7131
7132
7133 \f
7134 /***********************************************************************
7135 Text Cursor
7136 ***********************************************************************/
7137
7138 /* Set clipping for output in glyph row ROW. W is the window in which
7139 we operate. GC is the graphics context to set clipping in.
7140
7141 ROW may be a text row or, e.g., a mode line. Text rows must be
7142 clipped to the interior of the window dedicated to text display,
7143 mode lines must be clipped to the whole window. */
7144
7145 static void
7146 x_clip_to_row (struct window *w, struct glyph_row *row,
7147 enum glyph_row_area area, GC gc)
7148 {
7149 struct frame *f = XFRAME (WINDOW_FRAME (w));
7150 XRectangle clip_rect;
7151 int window_x, window_y, window_width;
7152
7153 window_box (w, area, &window_x, &window_y, &window_width, 0);
7154
7155 clip_rect.x = window_x;
7156 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, row->y));
7157 clip_rect.y = max (clip_rect.y, window_y);
7158 clip_rect.width = window_width;
7159 clip_rect.height = row->visible_height;
7160
7161 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
7162 }
7163
7164
7165 /* Draw a hollow box cursor on window W in glyph row ROW. */
7166
7167 static void
7168 x_draw_hollow_cursor (struct window *w, struct glyph_row *row)
7169 {
7170 struct frame *f = XFRAME (WINDOW_FRAME (w));
7171 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
7172 Display *dpy = FRAME_X_DISPLAY (f);
7173 int x, y, wd, h;
7174 XGCValues xgcv;
7175 struct glyph *cursor_glyph;
7176 GC gc;
7177
7178 /* Get the glyph the cursor is on. If we can't tell because
7179 the current matrix is invalid or such, give up. */
7180 cursor_glyph = get_phys_cursor_glyph (w);
7181 if (cursor_glyph == NULL)
7182 return;
7183
7184 /* Compute frame-relative coordinates for phys cursor. */
7185 get_phys_cursor_geometry (w, row, cursor_glyph, &x, &y, &h);
7186 wd = w->phys_cursor_width;
7187
7188 /* The foreground of cursor_gc is typically the same as the normal
7189 background color, which can cause the cursor box to be invisible. */
7190 xgcv.foreground = f->output_data.x->cursor_pixel;
7191 if (dpyinfo->scratch_cursor_gc)
7192 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
7193 else
7194 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
7195 GCForeground, &xgcv);
7196 gc = dpyinfo->scratch_cursor_gc;
7197
7198 /* Set clipping, draw the rectangle, and reset clipping again. */
7199 x_clip_to_row (w, row, TEXT_AREA, gc);
7200 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h - 1);
7201 XSetClipMask (dpy, gc, None);
7202 }
7203
7204
7205 /* Draw a bar cursor on window W in glyph row ROW.
7206
7207 Implementation note: One would like to draw a bar cursor with an
7208 angle equal to the one given by the font property XA_ITALIC_ANGLE.
7209 Unfortunately, I didn't find a font yet that has this property set.
7210 --gerd. */
7211
7212 static void
7213 x_draw_bar_cursor (struct window *w, struct glyph_row *row, int width, enum text_cursor_kinds kind)
7214 {
7215 struct frame *f = XFRAME (w->frame);
7216 struct glyph *cursor_glyph;
7217
7218 /* If cursor is out of bounds, don't draw garbage. This can happen
7219 in mini-buffer windows when switching between echo area glyphs
7220 and mini-buffer. */
7221 cursor_glyph = get_phys_cursor_glyph (w);
7222 if (cursor_glyph == NULL)
7223 return;
7224
7225 /* If on an image, draw like a normal cursor. That's usually better
7226 visible than drawing a bar, esp. if the image is large so that
7227 the bar might not be in the window. */
7228 if (cursor_glyph->type == IMAGE_GLYPH)
7229 {
7230 struct glyph_row *r;
7231 r = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
7232 draw_phys_cursor_glyph (w, r, DRAW_CURSOR);
7233 }
7234 else
7235 {
7236 Display *dpy = FRAME_X_DISPLAY (f);
7237 Window window = FRAME_X_WINDOW (f);
7238 GC gc = FRAME_DISPLAY_INFO (f)->scratch_cursor_gc;
7239 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
7240 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
7241 XGCValues xgcv;
7242
7243 /* If the glyph's background equals the color we normally draw
7244 the bars cursor in, the bar cursor in its normal color is
7245 invisible. Use the glyph's foreground color instead in this
7246 case, on the assumption that the glyph's colors are chosen so
7247 that the glyph is legible. */
7248 if (face->background == f->output_data.x->cursor_pixel)
7249 xgcv.background = xgcv.foreground = face->foreground;
7250 else
7251 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
7252 xgcv.graphics_exposures = 0;
7253
7254 if (gc)
7255 XChangeGC (dpy, gc, mask, &xgcv);
7256 else
7257 {
7258 gc = XCreateGC (dpy, window, mask, &xgcv);
7259 FRAME_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
7260 }
7261
7262 x_clip_to_row (w, row, TEXT_AREA, gc);
7263
7264 if (kind == BAR_CURSOR)
7265 {
7266 int x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
7267
7268 if (width < 0)
7269 width = FRAME_CURSOR_WIDTH (f);
7270 width = min (cursor_glyph->pixel_width, width);
7271
7272 w->phys_cursor_width = width;
7273
7274 /* If the character under cursor is R2L, draw the bar cursor
7275 on the right of its glyph, rather than on the left. */
7276 if ((cursor_glyph->resolved_level & 1) != 0)
7277 x += cursor_glyph->pixel_width - width;
7278
7279 XFillRectangle (dpy, window, gc, x,
7280 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
7281 width, row->height);
7282 }
7283 else
7284 {
7285 int dummy_x, dummy_y, dummy_h;
7286
7287 if (width < 0)
7288 width = row->height;
7289
7290 width = min (row->height, width);
7291
7292 get_phys_cursor_geometry (w, row, cursor_glyph, &dummy_x,
7293 &dummy_y, &dummy_h);
7294
7295 XFillRectangle (dpy, window, gc,
7296 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7297 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
7298 row->height - width),
7299 w->phys_cursor_width, width);
7300 }
7301
7302 XSetClipMask (dpy, gc, None);
7303 }
7304 }
7305
7306
7307 /* RIF: Define cursor CURSOR on frame F. */
7308
7309 static void
7310 x_define_frame_cursor (struct frame *f, Cursor cursor)
7311 {
7312 if (!f->pointer_invisible
7313 && f->output_data.x->current_cursor != cursor)
7314 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
7315 f->output_data.x->current_cursor = cursor;
7316 }
7317
7318
7319 /* RIF: Clear area on frame F. */
7320
7321 static void
7322 x_clear_frame_area (struct frame *f, int x, int y, int width, int height)
7323 {
7324 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), x, y, width, height);
7325 #ifdef USE_GTK
7326 /* Must queue a redraw, because scroll bars might have been cleared. */
7327 if (FRAME_GTK_WIDGET (f))
7328 gtk_widget_queue_draw (FRAME_GTK_WIDGET (f));
7329 #endif
7330 }
7331
7332
7333 /* RIF: Draw cursor on window W. */
7334
7335 static void
7336 x_draw_window_cursor (struct window *w, struct glyph_row *glyph_row, int x,
7337 int y, enum text_cursor_kinds cursor_type,
7338 int cursor_width, bool on_p, bool active_p)
7339 {
7340 struct frame *f = XFRAME (WINDOW_FRAME (w));
7341
7342 if (on_p)
7343 {
7344 w->phys_cursor_type = cursor_type;
7345 w->phys_cursor_on_p = 1;
7346
7347 if (glyph_row->exact_window_width_line_p
7348 && (glyph_row->reversed_p
7349 ? (w->phys_cursor.hpos < 0)
7350 : (w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])))
7351 {
7352 glyph_row->cursor_in_fringe_p = 1;
7353 draw_fringe_bitmap (w, glyph_row, glyph_row->reversed_p);
7354 }
7355 else
7356 {
7357 switch (cursor_type)
7358 {
7359 case HOLLOW_BOX_CURSOR:
7360 x_draw_hollow_cursor (w, glyph_row);
7361 break;
7362
7363 case FILLED_BOX_CURSOR:
7364 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
7365 break;
7366
7367 case BAR_CURSOR:
7368 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
7369 break;
7370
7371 case HBAR_CURSOR:
7372 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
7373 break;
7374
7375 case NO_CURSOR:
7376 w->phys_cursor_width = 0;
7377 break;
7378
7379 default:
7380 emacs_abort ();
7381 }
7382 }
7383
7384 #ifdef HAVE_X_I18N
7385 if (w == XWINDOW (f->selected_window))
7386 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
7387 xic_set_preeditarea (w, x, y);
7388 #endif
7389 }
7390
7391 XFlush (FRAME_X_DISPLAY (f));
7392 }
7393
7394 \f
7395 /* Icons. */
7396
7397 /* Make the x-window of frame F use the gnu icon bitmap. */
7398
7399 int
7400 x_bitmap_icon (struct frame *f, Lisp_Object file)
7401 {
7402 ptrdiff_t bitmap_id;
7403
7404 if (FRAME_X_WINDOW (f) == 0)
7405 return 1;
7406
7407 /* Free up our existing icon bitmap and mask if any. */
7408 if (f->output_data.x->icon_bitmap > 0)
7409 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7410 f->output_data.x->icon_bitmap = 0;
7411
7412 if (STRINGP (file))
7413 {
7414 #ifdef USE_GTK
7415 /* Use gtk_window_set_icon_from_file () if available,
7416 It's not restricted to bitmaps */
7417 if (xg_set_icon (f, file))
7418 return 0;
7419 #endif /* USE_GTK */
7420 bitmap_id = x_create_bitmap_from_file (f, file);
7421 x_create_bitmap_mask (f, bitmap_id);
7422 }
7423 else
7424 {
7425 /* Create the GNU bitmap and mask if necessary. */
7426 if (FRAME_DISPLAY_INFO (f)->icon_bitmap_id < 0)
7427 {
7428 ptrdiff_t rc = -1;
7429
7430 #ifdef USE_GTK
7431
7432 if (xg_set_icon (f, xg_default_icon_file)
7433 || xg_set_icon_from_xpm_data (f, gnu_xpm_bits))
7434 return 0;
7435
7436 #elif defined (HAVE_XPM) && defined (HAVE_X_WINDOWS)
7437
7438 rc = x_create_bitmap_from_xpm_data (f, gnu_xpm_bits);
7439 if (rc != -1)
7440 FRAME_DISPLAY_INFO (f)->icon_bitmap_id = rc;
7441
7442 #endif
7443
7444 /* If all else fails, use the (black and white) xbm image. */
7445 if (rc == -1)
7446 {
7447 rc = x_create_bitmap_from_data (f, (char *) gnu_xbm_bits,
7448 gnu_xbm_width, gnu_xbm_height);
7449 if (rc == -1)
7450 return 1;
7451
7452 FRAME_DISPLAY_INFO (f)->icon_bitmap_id = rc;
7453 x_create_bitmap_mask (f, FRAME_DISPLAY_INFO (f)->icon_bitmap_id);
7454 }
7455 }
7456
7457 /* The first time we create the GNU bitmap and mask,
7458 this increments the ref-count one extra time.
7459 As a result, the GNU bitmap and mask are never freed.
7460 That way, we don't have to worry about allocating it again. */
7461 x_reference_bitmap (f, FRAME_DISPLAY_INFO (f)->icon_bitmap_id);
7462
7463 bitmap_id = FRAME_DISPLAY_INFO (f)->icon_bitmap_id;
7464 }
7465
7466 x_wm_set_icon_pixmap (f, bitmap_id);
7467 f->output_data.x->icon_bitmap = bitmap_id;
7468
7469 return 0;
7470 }
7471
7472
7473 /* Make the x-window of frame F use a rectangle with text.
7474 Use ICON_NAME as the text. */
7475
7476 int
7477 x_text_icon (struct frame *f, const char *icon_name)
7478 {
7479 if (FRAME_X_WINDOW (f) == 0)
7480 return 1;
7481
7482 {
7483 XTextProperty text;
7484 text.value = (unsigned char *) icon_name;
7485 text.encoding = XA_STRING;
7486 text.format = 8;
7487 text.nitems = strlen (icon_name);
7488 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
7489 }
7490
7491 if (f->output_data.x->icon_bitmap > 0)
7492 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7493 f->output_data.x->icon_bitmap = 0;
7494 x_wm_set_icon_pixmap (f, 0);
7495
7496 return 0;
7497 }
7498 \f
7499 #define X_ERROR_MESSAGE_SIZE 200
7500
7501 /* If non-nil, this should be a string.
7502 It means catch X errors and store the error message in this string.
7503
7504 The reason we use a stack is that x_catch_error/x_uncatch_error can
7505 be called from a signal handler.
7506 */
7507
7508 struct x_error_message_stack {
7509 char string[X_ERROR_MESSAGE_SIZE];
7510 Display *dpy;
7511 struct x_error_message_stack *prev;
7512 };
7513 static struct x_error_message_stack *x_error_message;
7514
7515 /* An X error handler which stores the error message in
7516 *x_error_message. This is called from x_error_handler if
7517 x_catch_errors is in effect. */
7518
7519 static void
7520 x_error_catcher (Display *display, XErrorEvent *event)
7521 {
7522 XGetErrorText (display, event->error_code,
7523 x_error_message->string,
7524 X_ERROR_MESSAGE_SIZE);
7525 }
7526
7527 /* Begin trapping X errors for display DPY. Actually we trap X errors
7528 for all displays, but DPY should be the display you are actually
7529 operating on.
7530
7531 After calling this function, X protocol errors no longer cause
7532 Emacs to exit; instead, they are recorded in the string
7533 stored in *x_error_message.
7534
7535 Calling x_check_errors signals an Emacs error if an X error has
7536 occurred since the last call to x_catch_errors or x_check_errors.
7537
7538 Calling x_uncatch_errors resumes the normal error handling. */
7539
7540 void
7541 x_catch_errors (Display *dpy)
7542 {
7543 struct x_error_message_stack *data = xmalloc (sizeof *data);
7544
7545 /* Make sure any errors from previous requests have been dealt with. */
7546 XSync (dpy, False);
7547
7548 data->dpy = dpy;
7549 data->string[0] = 0;
7550 data->prev = x_error_message;
7551 x_error_message = data;
7552 }
7553
7554 /* Undo the last x_catch_errors call.
7555 DPY should be the display that was passed to x_catch_errors. */
7556
7557 void
7558 x_uncatch_errors (void)
7559 {
7560 struct x_error_message_stack *tmp;
7561
7562 block_input ();
7563
7564 /* The display may have been closed before this function is called.
7565 Check if it is still open before calling XSync. */
7566 if (x_display_info_for_display (x_error_message->dpy) != 0)
7567 XSync (x_error_message->dpy, False);
7568
7569 tmp = x_error_message;
7570 x_error_message = x_error_message->prev;
7571 xfree (tmp);
7572 unblock_input ();
7573 }
7574
7575 /* If any X protocol errors have arrived since the last call to
7576 x_catch_errors or x_check_errors, signal an Emacs error using
7577 sprintf (a buffer, FORMAT, the x error message text) as the text. */
7578
7579 void
7580 x_check_errors (Display *dpy, const char *format)
7581 {
7582 /* Make sure to catch any errors incurred so far. */
7583 XSync (dpy, False);
7584
7585 if (x_error_message->string[0])
7586 {
7587 char string[X_ERROR_MESSAGE_SIZE];
7588 memcpy (string, x_error_message->string, X_ERROR_MESSAGE_SIZE);
7589 x_uncatch_errors ();
7590 error (format, string);
7591 }
7592 }
7593
7594 /* Nonzero if we had any X protocol errors
7595 since we did x_catch_errors on DPY. */
7596
7597 bool
7598 x_had_errors_p (Display *dpy)
7599 {
7600 /* Make sure to catch any errors incurred so far. */
7601 XSync (dpy, False);
7602
7603 return x_error_message->string[0] != 0;
7604 }
7605
7606 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
7607
7608 void
7609 x_clear_errors (Display *dpy)
7610 {
7611 x_error_message->string[0] = 0;
7612 }
7613
7614 #if 0 /* See comment in unwind_to_catch why calling this is a bad
7615 * idea. --lorentey */
7616 /* Close off all unclosed x_catch_errors calls. */
7617
7618 void
7619 x_fully_uncatch_errors (void)
7620 {
7621 while (x_error_message)
7622 x_uncatch_errors ();
7623 }
7624 #endif
7625
7626 #if 0
7627 static unsigned int x_wire_count;
7628 x_trace_wire (void)
7629 {
7630 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
7631 }
7632 #endif /* ! 0 */
7633
7634 \f
7635 /************************************************************************
7636 Handling X errors
7637 ************************************************************************/
7638
7639 /* Error message passed to x_connection_closed. */
7640
7641 static char *error_msg;
7642
7643 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
7644 the text of an error message that lead to the connection loss. */
7645
7646 static void
7647 x_connection_closed (Display *dpy, const char *error_message)
7648 {
7649 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
7650 Lisp_Object frame, tail;
7651 ptrdiff_t idx = SPECPDL_INDEX ();
7652
7653 error_msg = alloca (strlen (error_message) + 1);
7654 strcpy (error_msg, error_message);
7655
7656 /* Inhibit redisplay while frames are being deleted. */
7657 specbind (Qinhibit_redisplay, Qt);
7658
7659 if (dpyinfo)
7660 {
7661 /* Protect display from being closed when we delete the last
7662 frame on it. */
7663 dpyinfo->reference_count++;
7664 dpyinfo->terminal->reference_count++;
7665 }
7666
7667 /* First delete frames whose mini-buffers are on frames
7668 that are on the dead display. */
7669 FOR_EACH_FRAME (tail, frame)
7670 {
7671 Lisp_Object minibuf_frame;
7672 minibuf_frame
7673 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
7674 if (FRAME_X_P (XFRAME (frame))
7675 && FRAME_X_P (XFRAME (minibuf_frame))
7676 && ! EQ (frame, minibuf_frame)
7677 && FRAME_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7678 delete_frame (frame, Qnoelisp);
7679 }
7680
7681 /* Now delete all remaining frames on the dead display.
7682 We are now sure none of these is used as the mini-buffer
7683 for another frame that we need to delete. */
7684 FOR_EACH_FRAME (tail, frame)
7685 if (FRAME_X_P (XFRAME (frame))
7686 && FRAME_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
7687 {
7688 /* Set this to t so that delete_frame won't get confused
7689 trying to find a replacement. */
7690 kset_default_minibuffer_frame (FRAME_KBOARD (XFRAME (frame)), Qt);
7691 delete_frame (frame, Qnoelisp);
7692 }
7693
7694 /* If DPYINFO is null, this means we didn't open the display in the
7695 first place, so don't try to close it. */
7696 if (dpyinfo)
7697 {
7698 /* We can not call XtCloseDisplay here because it calls XSync.
7699 XSync inside the error handler apparently hangs Emacs. On
7700 current Xt versions, this isn't needed either. */
7701 #ifdef USE_GTK
7702 /* A long-standing GTK bug prevents proper disconnect handling
7703 (https://bugzilla.gnome.org/show_bug.cgi?id=85715). Once,
7704 the resulting Glib error message loop filled a user's disk.
7705 To avoid this, kill Emacs unconditionally on disconnect. */
7706 shut_down_emacs (0, Qnil);
7707 fprintf (stderr, "%s\n\
7708 When compiled with GTK, Emacs cannot recover from X disconnects.\n\
7709 This is a GTK bug: https://bugzilla.gnome.org/show_bug.cgi?id=85715\n\
7710 For details, see etc/PROBLEMS.\n",
7711 error_msg);
7712 emacs_abort ();
7713 #endif /* USE_GTK */
7714
7715 /* Indicate that this display is dead. */
7716 dpyinfo->display = 0;
7717
7718 dpyinfo->reference_count--;
7719 dpyinfo->terminal->reference_count--;
7720 if (dpyinfo->reference_count != 0)
7721 /* We have just closed all frames on this display. */
7722 emacs_abort ();
7723
7724 {
7725 Lisp_Object tmp;
7726 XSETTERMINAL (tmp, dpyinfo->terminal);
7727 Fdelete_terminal (tmp, Qnoelisp);
7728 }
7729 }
7730
7731 if (terminal_list == 0)
7732 {
7733 fprintf (stderr, "%s\n", error_msg);
7734 Fkill_emacs (make_number (70));
7735 /* NOTREACHED */
7736 }
7737
7738 totally_unblock_input ();
7739
7740 unbind_to (idx, Qnil);
7741 clear_waiting_for_input ();
7742
7743 /* Tell GCC not to suggest attribute 'noreturn' for this function. */
7744 IF_LINT (if (! terminal_list) return; )
7745
7746 /* Here, we absolutely have to use a non-local exit (e.g. signal, throw,
7747 longjmp), because returning from this function would get us back into
7748 Xlib's code which will directly call `exit'. */
7749 error ("%s", error_msg);
7750 }
7751
7752 /* We specifically use it before defining it, so that gcc doesn't inline it,
7753 otherwise gdb doesn't know how to properly put a breakpoint on it. */
7754 static void x_error_quitter (Display *, XErrorEvent *);
7755
7756 /* This is the first-level handler for X protocol errors.
7757 It calls x_error_quitter or x_error_catcher. */
7758
7759 static int
7760 x_error_handler (Display *display, XErrorEvent *event)
7761 {
7762 #if defined USE_GTK && defined HAVE_GTK3
7763 if ((event->error_code == BadMatch || event->error_code == BadWindow)
7764 && event->request_code == X_SetInputFocus)
7765 {
7766 return 0;
7767 }
7768 #endif
7769
7770 if (x_error_message)
7771 x_error_catcher (display, event);
7772 else
7773 x_error_quitter (display, event);
7774 return 0;
7775 }
7776
7777 /* This is the usual handler for X protocol errors.
7778 It kills all frames on the display that we got the error for.
7779 If that was the only one, it prints an error message and kills Emacs. */
7780
7781 /* .gdbinit puts a breakpoint here, so make sure it is not inlined. */
7782
7783 /* On older GCC versions, just putting x_error_quitter
7784 after x_error_handler prevents inlining into the former. */
7785
7786 static void NO_INLINE
7787 x_error_quitter (Display *display, XErrorEvent *event)
7788 {
7789 char buf[256], buf1[356];
7790
7791 /* Ignore BadName errors. They can happen because of fonts
7792 or colors that are not defined. */
7793
7794 if (event->error_code == BadName)
7795 return;
7796
7797 /* Note that there is no real way portable across R3/R4 to get the
7798 original error handler. */
7799
7800 XGetErrorText (display, event->error_code, buf, sizeof (buf));
7801 sprintf (buf1, "X protocol error: %s on protocol request %d",
7802 buf, event->request_code);
7803 x_connection_closed (display, buf1);
7804 }
7805
7806
7807 /* This is the handler for X IO errors, always.
7808 It kills all frames on the display that we lost touch with.
7809 If that was the only one, it prints an error message and kills Emacs. */
7810
7811 static int
7812 x_io_error_quitter (Display *display)
7813 {
7814 char buf[256];
7815
7816 snprintf (buf, sizeof buf, "Connection lost to X server `%s'",
7817 DisplayString (display));
7818 x_connection_closed (display, buf);
7819 return 0;
7820 }
7821 \f
7822 /* Changing the font of the frame. */
7823
7824 /* Give frame F the font FONT-OBJECT as its default font. The return
7825 value is FONT-OBJECT. FONTSET is an ID of the fontset for the
7826 frame. If it is negative, generate a new fontset from
7827 FONT-OBJECT. */
7828
7829 Lisp_Object
7830 x_new_font (struct frame *f, Lisp_Object font_object, int fontset)
7831 {
7832 struct font *font = XFONT_OBJECT (font_object);
7833
7834 if (fontset < 0)
7835 fontset = fontset_from_font (font_object);
7836 FRAME_FONTSET (f) = fontset;
7837 if (FRAME_FONT (f) == font)
7838 /* This font is already set in frame F. There's nothing more to
7839 do. */
7840 return font_object;
7841
7842 FRAME_FONT (f) = font;
7843 FRAME_BASELINE_OFFSET (f) = font->baseline_offset;
7844 FRAME_COLUMN_WIDTH (f) = font->average_width;
7845 FRAME_LINE_HEIGHT (f) = FONT_HEIGHT (font);
7846
7847 compute_fringe_widths (f, 1);
7848
7849 /* Compute the scroll bar width in character columns. */
7850 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
7851 {
7852 int wid = FRAME_COLUMN_WIDTH (f);
7853 FRAME_CONFIG_SCROLL_BAR_COLS (f)
7854 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + wid-1) / wid;
7855 }
7856 else
7857 {
7858 int wid = FRAME_COLUMN_WIDTH (f);
7859 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
7860 }
7861
7862 if (FRAME_X_WINDOW (f) != 0)
7863 {
7864 /* Don't change the size of a tip frame; there's no point in
7865 doing it because it's done in Fx_show_tip, and it leads to
7866 problems because the tip frame has no widget. */
7867 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
7868 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
7869 }
7870
7871 #ifdef HAVE_X_I18N
7872 if (FRAME_XIC (f)
7873 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
7874 {
7875 block_input ();
7876 xic_set_xfontset (f, SSDATA (fontset_ascii (fontset)));
7877 unblock_input ();
7878 }
7879 #endif
7880
7881 return font_object;
7882 }
7883
7884 \f
7885 /***********************************************************************
7886 X Input Methods
7887 ***********************************************************************/
7888
7889 #ifdef HAVE_X_I18N
7890
7891 #ifdef HAVE_X11R6
7892
7893 /* XIM destroy callback function, which is called whenever the
7894 connection to input method XIM dies. CLIENT_DATA contains a
7895 pointer to the x_display_info structure corresponding to XIM. */
7896
7897 static void
7898 xim_destroy_callback (XIM xim, XPointer client_data, XPointer call_data)
7899 {
7900 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
7901 Lisp_Object frame, tail;
7902
7903 block_input ();
7904
7905 /* No need to call XDestroyIC.. */
7906 FOR_EACH_FRAME (tail, frame)
7907 {
7908 struct frame *f = XFRAME (frame);
7909 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo)
7910 {
7911 FRAME_XIC (f) = NULL;
7912 xic_free_xfontset (f);
7913 }
7914 }
7915
7916 /* No need to call XCloseIM. */
7917 dpyinfo->xim = NULL;
7918 XFree (dpyinfo->xim_styles);
7919 unblock_input ();
7920 }
7921
7922 #endif /* HAVE_X11R6 */
7923
7924 #ifdef HAVE_X11R6
7925 /* This isn't prototyped in OSF 5.0 or 5.1a. */
7926 extern char *XSetIMValues (XIM, ...);
7927 #endif
7928
7929 /* Open the connection to the XIM server on display DPYINFO.
7930 RESOURCE_NAME is the resource name Emacs uses. */
7931
7932 static void
7933 xim_open_dpy (struct x_display_info *dpyinfo, char *resource_name)
7934 {
7935 XIM xim;
7936
7937 #ifdef HAVE_XIM
7938 if (use_xim)
7939 {
7940 if (dpyinfo->xim)
7941 XCloseIM (dpyinfo->xim);
7942 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name,
7943 emacs_class);
7944 dpyinfo->xim = xim;
7945
7946 if (xim)
7947 {
7948 #ifdef HAVE_X11R6
7949 XIMCallback destroy;
7950 #endif
7951
7952 /* Get supported styles and XIM values. */
7953 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
7954
7955 #ifdef HAVE_X11R6
7956 destroy.callback = xim_destroy_callback;
7957 destroy.client_data = (XPointer)dpyinfo;
7958 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
7959 #endif
7960 }
7961 }
7962
7963 else
7964 #endif /* HAVE_XIM */
7965 dpyinfo->xim = NULL;
7966 }
7967
7968
7969 #ifdef HAVE_X11R6_XIM
7970
7971 /* XIM instantiate callback function, which is called whenever an XIM
7972 server is available. DISPLAY is the display of the XIM.
7973 CLIENT_DATA contains a pointer to an xim_inst_t structure created
7974 when the callback was registered. */
7975
7976 static void
7977 xim_instantiate_callback (Display *display, XPointer client_data, XPointer call_data)
7978 {
7979 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
7980 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
7981
7982 /* We don't support multiple XIM connections. */
7983 if (dpyinfo->xim)
7984 return;
7985
7986 xim_open_dpy (dpyinfo, xim_inst->resource_name);
7987
7988 /* Create XIC for the existing frames on the same display, as long
7989 as they have no XIC. */
7990 if (dpyinfo->xim && dpyinfo->reference_count > 0)
7991 {
7992 Lisp_Object tail, frame;
7993
7994 block_input ();
7995 FOR_EACH_FRAME (tail, frame)
7996 {
7997 struct frame *f = XFRAME (frame);
7998
7999 if (FRAME_X_P (f)
8000 && FRAME_DISPLAY_INFO (f) == xim_inst->dpyinfo)
8001 if (FRAME_XIC (f) == NULL)
8002 {
8003 create_frame_xic (f);
8004 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
8005 xic_set_statusarea (f);
8006 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
8007 {
8008 struct window *w = XWINDOW (f->selected_window);
8009 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
8010 }
8011 }
8012 }
8013
8014 unblock_input ();
8015 }
8016 }
8017
8018 #endif /* HAVE_X11R6_XIM */
8019
8020
8021 /* Open a connection to the XIM server on display DPYINFO.
8022 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
8023 connection only at the first time. On X11R6, open the connection
8024 in the XIM instantiate callback function. */
8025
8026 static void
8027 xim_initialize (struct x_display_info *dpyinfo, char *resource_name)
8028 {
8029 dpyinfo->xim = NULL;
8030 #ifdef HAVE_XIM
8031 if (use_xim)
8032 {
8033 #ifdef HAVE_X11R6_XIM
8034 struct xim_inst_t *xim_inst = xmalloc (sizeof *xim_inst);
8035
8036 dpyinfo->xim_callback_data = xim_inst;
8037 xim_inst->dpyinfo = dpyinfo;
8038 xim_inst->resource_name = xstrdup (resource_name);
8039 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8040 resource_name, emacs_class,
8041 xim_instantiate_callback,
8042 /* This is XPointer in XFree86
8043 but (XPointer *) on Tru64, at
8044 least, hence the configure test. */
8045 (XRegisterIMInstantiateCallback_arg6) xim_inst);
8046 #else /* not HAVE_X11R6_XIM */
8047 xim_open_dpy (dpyinfo, resource_name);
8048 #endif /* not HAVE_X11R6_XIM */
8049 }
8050 #endif /* HAVE_XIM */
8051 }
8052
8053
8054 /* Close the connection to the XIM server on display DPYINFO. */
8055
8056 static void
8057 xim_close_dpy (struct x_display_info *dpyinfo)
8058 {
8059 #ifdef HAVE_XIM
8060 if (use_xim)
8061 {
8062 #ifdef HAVE_X11R6_XIM
8063 if (dpyinfo->display)
8064 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8065 NULL, emacs_class,
8066 xim_instantiate_callback, NULL);
8067 xfree (dpyinfo->xim_callback_data->resource_name);
8068 xfree (dpyinfo->xim_callback_data);
8069 #endif /* HAVE_X11R6_XIM */
8070 if (dpyinfo->display)
8071 XCloseIM (dpyinfo->xim);
8072 dpyinfo->xim = NULL;
8073 XFree (dpyinfo->xim_styles);
8074 }
8075 #endif /* HAVE_XIM */
8076 }
8077
8078 #endif /* not HAVE_X11R6_XIM */
8079
8080
8081 \f
8082 /* Calculate the absolute position in frame F
8083 from its current recorded position values and gravity. */
8084
8085 static void
8086 x_calc_absolute_position (struct frame *f)
8087 {
8088 int flags = f->size_hint_flags;
8089
8090 /* We have nothing to do if the current position
8091 is already for the top-left corner. */
8092 if (! ((flags & XNegative) || (flags & YNegative)))
8093 return;
8094
8095 /* Treat negative positions as relative to the leftmost bottommost
8096 position that fits on the screen. */
8097 if (flags & XNegative)
8098 f->left_pos = x_display_pixel_width (FRAME_DISPLAY_INFO (f))
8099 - FRAME_PIXEL_WIDTH (f) + f->left_pos;
8100
8101 {
8102 int height = FRAME_PIXEL_HEIGHT (f);
8103
8104 #if defined USE_X_TOOLKIT && defined USE_MOTIF
8105 /* Something is fishy here. When using Motif, starting Emacs with
8106 `-g -0-0', the frame appears too low by a few pixels.
8107
8108 This seems to be so because initially, while Emacs is starting,
8109 the column widget's height and the frame's pixel height are
8110 different. The column widget's height is the right one. In
8111 later invocations, when Emacs is up, the frame's pixel height
8112 is right, though.
8113
8114 It's not obvious where the initial small difference comes from.
8115 2000-12-01, gerd. */
8116
8117 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
8118 #endif
8119
8120 if (flags & YNegative)
8121 f->top_pos = x_display_pixel_height (FRAME_DISPLAY_INFO (f))
8122 - height + f->top_pos;
8123 }
8124
8125 /* The left_pos and top_pos
8126 are now relative to the top and left screen edges,
8127 so the flags should correspond. */
8128 f->size_hint_flags &= ~ (XNegative | YNegative);
8129 }
8130
8131 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
8132 to really change the position, and 0 when calling from
8133 x_make_frame_visible (in that case, XOFF and YOFF are the current
8134 position values). It is -1 when calling from x_set_frame_parameters,
8135 which means, do adjust for borders but don't change the gravity. */
8136
8137 void
8138 x_set_offset (struct frame *f, register int xoff, register int yoff, int change_gravity)
8139 {
8140 int modified_top, modified_left;
8141
8142 if (change_gravity > 0)
8143 {
8144 f->top_pos = yoff;
8145 f->left_pos = xoff;
8146 f->size_hint_flags &= ~ (XNegative | YNegative);
8147 if (xoff < 0)
8148 f->size_hint_flags |= XNegative;
8149 if (yoff < 0)
8150 f->size_hint_flags |= YNegative;
8151 f->win_gravity = NorthWestGravity;
8152 }
8153 x_calc_absolute_position (f);
8154
8155 block_input ();
8156 x_wm_set_size_hint (f, (long) 0, 0);
8157
8158 modified_left = f->left_pos;
8159 modified_top = f->top_pos;
8160
8161 if (change_gravity != 0 && FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A)
8162 {
8163 /* Some WMs (twm, wmaker at least) has an offset that is smaller
8164 than the WM decorations. So we use the calculated offset instead
8165 of the WM decoration sizes here (x/y_pixels_outer_diff). */
8166 modified_left += FRAME_X_OUTPUT (f)->move_offset_left;
8167 modified_top += FRAME_X_OUTPUT (f)->move_offset_top;
8168 }
8169
8170 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8171 modified_left, modified_top);
8172
8173 x_sync_with_move (f, f->left_pos, f->top_pos,
8174 FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
8175 ? 1 : 0);
8176
8177 /* change_gravity is non-zero when this function is called from Lisp to
8178 programmatically move a frame. In that case, we call
8179 x_check_expected_move to discover if we have a "Type A" or "Type B"
8180 window manager, and, for a "Type A" window manager, adjust the position
8181 of the frame.
8182
8183 We call x_check_expected_move if a programmatic move occurred, and
8184 either the window manager type (A/B) is unknown or it is Type A but we
8185 need to compute the top/left offset adjustment for this frame. */
8186
8187 if (change_gravity != 0 &&
8188 (FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
8189 || (FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A
8190 && (FRAME_X_OUTPUT (f)->move_offset_left == 0
8191 && FRAME_X_OUTPUT (f)->move_offset_top == 0))))
8192 x_check_expected_move (f, modified_left, modified_top);
8193
8194 unblock_input ();
8195 }
8196
8197 /* Return non-zero if _NET_SUPPORTING_WM_CHECK window exists and _NET_SUPPORTED
8198 on the root window for frame F contains ATOMNAME.
8199 This is how a WM check shall be done according to the Window Manager
8200 Specification/Extended Window Manager Hints at
8201 http://freedesktop.org/wiki/Specifications/wm-spec. */
8202
8203 static int
8204 wm_supports (struct frame *f, Atom want_atom)
8205 {
8206 Atom actual_type;
8207 unsigned long actual_size, bytes_remaining;
8208 int i, rc, actual_format;
8209 Window wmcheck_window;
8210 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
8211 Window target_window = dpyinfo->root_window;
8212 long max_len = 65536;
8213 Display *dpy = FRAME_X_DISPLAY (f);
8214 unsigned char *tmp_data = NULL;
8215 Atom target_type = XA_WINDOW;
8216
8217 block_input ();
8218
8219 x_catch_errors (dpy);
8220 rc = XGetWindowProperty (dpy, target_window,
8221 dpyinfo->Xatom_net_supporting_wm_check,
8222 0, max_len, False, target_type,
8223 &actual_type, &actual_format, &actual_size,
8224 &bytes_remaining, &tmp_data);
8225
8226 if (rc != Success || actual_type != XA_WINDOW || x_had_errors_p (dpy))
8227 {
8228 if (tmp_data) XFree (tmp_data);
8229 x_uncatch_errors ();
8230 unblock_input ();
8231 return 0;
8232 }
8233
8234 wmcheck_window = *(Window *) tmp_data;
8235 XFree (tmp_data);
8236
8237 /* Check if window exists. */
8238 XSelectInput (dpy, wmcheck_window, StructureNotifyMask);
8239 x_sync (f);
8240 if (x_had_errors_p (dpy))
8241 {
8242 x_uncatch_errors ();
8243 unblock_input ();
8244 return 0;
8245 }
8246
8247 if (dpyinfo->net_supported_window != wmcheck_window)
8248 {
8249 /* Window changed, reload atoms */
8250 if (dpyinfo->net_supported_atoms != NULL)
8251 XFree (dpyinfo->net_supported_atoms);
8252 dpyinfo->net_supported_atoms = NULL;
8253 dpyinfo->nr_net_supported_atoms = 0;
8254 dpyinfo->net_supported_window = 0;
8255
8256 target_type = XA_ATOM;
8257 tmp_data = NULL;
8258 rc = XGetWindowProperty (dpy, target_window,
8259 dpyinfo->Xatom_net_supported,
8260 0, max_len, False, target_type,
8261 &actual_type, &actual_format, &actual_size,
8262 &bytes_remaining, &tmp_data);
8263
8264 if (rc != Success || actual_type != XA_ATOM || x_had_errors_p (dpy))
8265 {
8266 if (tmp_data) XFree (tmp_data);
8267 x_uncatch_errors ();
8268 unblock_input ();
8269 return 0;
8270 }
8271
8272 dpyinfo->net_supported_atoms = (Atom *)tmp_data;
8273 dpyinfo->nr_net_supported_atoms = actual_size;
8274 dpyinfo->net_supported_window = wmcheck_window;
8275 }
8276
8277 rc = 0;
8278
8279 for (i = 0; rc == 0 && i < dpyinfo->nr_net_supported_atoms; ++i)
8280 rc = dpyinfo->net_supported_atoms[i] == want_atom;
8281
8282 x_uncatch_errors ();
8283 unblock_input ();
8284
8285 return rc;
8286 }
8287
8288 static void
8289 set_wm_state (Lisp_Object frame, int add, Atom atom, Atom value)
8290 {
8291 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (XFRAME (frame));
8292
8293 x_send_client_event (frame, make_number (0), frame,
8294 dpyinfo->Xatom_net_wm_state,
8295 make_number (32),
8296 /* 1 = add, 0 = remove */
8297 Fcons
8298 (make_number (add ? 1 : 0),
8299 Fcons
8300 (make_fixnum_or_float (atom),
8301 (value != 0
8302 ? list1 (make_fixnum_or_float (value))
8303 : Qnil))));
8304 }
8305
8306 void
8307 x_set_sticky (struct frame *f, Lisp_Object new_value, Lisp_Object old_value)
8308 {
8309 Lisp_Object frame;
8310 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
8311
8312 XSETFRAME (frame, f);
8313
8314 set_wm_state (frame, NILP (new_value) ? 0 : 1,
8315 dpyinfo->Xatom_net_wm_state_sticky, None);
8316 }
8317
8318 /* Return the current _NET_WM_STATE.
8319 SIZE_STATE is set to one of the FULLSCREEN_* values.
8320 STICKY is set to 1 if the sticky state is set, 0 if not.
8321
8322 Return non-zero if we are not hidden, zero if we are. */
8323
8324 static int
8325 get_current_wm_state (struct frame *f,
8326 Window window,
8327 int *size_state,
8328 int *sticky)
8329 {
8330 Atom actual_type;
8331 unsigned long actual_size, bytes_remaining;
8332 int i, rc, actual_format, is_hidden = 0;
8333 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
8334 long max_len = 65536;
8335 Display *dpy = FRAME_X_DISPLAY (f);
8336 unsigned char *tmp_data = NULL;
8337 Atom target_type = XA_ATOM;
8338
8339 *sticky = 0;
8340 *size_state = FULLSCREEN_NONE;
8341
8342 block_input ();
8343 x_catch_errors (dpy);
8344 rc = XGetWindowProperty (dpy, window, dpyinfo->Xatom_net_wm_state,
8345 0, max_len, False, target_type,
8346 &actual_type, &actual_format, &actual_size,
8347 &bytes_remaining, &tmp_data);
8348
8349 if (rc != Success || actual_type != target_type || x_had_errors_p (dpy))
8350 {
8351 if (tmp_data) XFree (tmp_data);
8352 x_uncatch_errors ();
8353 unblock_input ();
8354 return !FRAME_ICONIFIED_P (f);
8355 }
8356
8357 x_uncatch_errors ();
8358
8359 for (i = 0; i < actual_size; ++i)
8360 {
8361 Atom a = ((Atom*)tmp_data)[i];
8362 if (a == dpyinfo->Xatom_net_wm_state_hidden)
8363 {
8364 is_hidden = 1;
8365 f->output_data.x->net_wm_state_hidden_seen = 1;
8366 }
8367 else if (a == dpyinfo->Xatom_net_wm_state_maximized_horz)
8368 {
8369 if (*size_state == FULLSCREEN_HEIGHT)
8370 *size_state = FULLSCREEN_MAXIMIZED;
8371 else
8372 *size_state = FULLSCREEN_WIDTH;
8373 }
8374 else if (a == dpyinfo->Xatom_net_wm_state_maximized_vert)
8375 {
8376 if (*size_state == FULLSCREEN_WIDTH)
8377 *size_state = FULLSCREEN_MAXIMIZED;
8378 else
8379 *size_state = FULLSCREEN_HEIGHT;
8380 }
8381 else if (a == dpyinfo->Xatom_net_wm_state_fullscreen)
8382 *size_state = FULLSCREEN_BOTH;
8383 else if (a == dpyinfo->Xatom_net_wm_state_sticky)
8384 *sticky = 1;
8385 }
8386
8387 if (tmp_data) XFree (tmp_data);
8388 unblock_input ();
8389 return ! is_hidden;
8390 }
8391
8392 /* Do fullscreen as specified in extended window manager hints */
8393
8394 static int
8395 do_ewmh_fullscreen (struct frame *f)
8396 {
8397 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
8398 int have_net_atom = wm_supports (f, dpyinfo->Xatom_net_wm_state);
8399 int cur, dummy;
8400
8401 (void)get_current_wm_state (f, FRAME_OUTER_WINDOW (f), &cur, &dummy);
8402
8403 /* Some window managers don't say they support _NET_WM_STATE, but they do say
8404 they support _NET_WM_STATE_FULLSCREEN. Try that also. */
8405 if (!have_net_atom)
8406 have_net_atom = wm_supports (f, dpyinfo->Xatom_net_wm_state_fullscreen);
8407
8408 if (have_net_atom && cur != f->want_fullscreen)
8409 {
8410 Lisp_Object frame;
8411
8412 XSETFRAME (frame, f);
8413
8414 /* Keep number of calls to set_wm_state as low as possible.
8415 Some window managers, or possible Gtk+, hangs when too many
8416 are sent at once. */
8417 switch (f->want_fullscreen)
8418 {
8419 case FULLSCREEN_BOTH:
8420 if (cur == FULLSCREEN_WIDTH || cur == FULLSCREEN_MAXIMIZED
8421 || cur == FULLSCREEN_HEIGHT)
8422 set_wm_state (frame, 0, dpyinfo->Xatom_net_wm_state_maximized_horz,
8423 dpyinfo->Xatom_net_wm_state_maximized_vert);
8424 set_wm_state (frame, 1, dpyinfo->Xatom_net_wm_state_fullscreen, None);
8425 break;
8426 case FULLSCREEN_WIDTH:
8427 if (cur == FULLSCREEN_BOTH || cur == FULLSCREEN_HEIGHT
8428 || cur == FULLSCREEN_MAXIMIZED)
8429 set_wm_state (frame, 0, dpyinfo->Xatom_net_wm_state_fullscreen,
8430 dpyinfo->Xatom_net_wm_state_maximized_vert);
8431 if (cur != FULLSCREEN_MAXIMIZED)
8432 set_wm_state (frame, 1, dpyinfo->Xatom_net_wm_state_maximized_horz, None);
8433 break;
8434 case FULLSCREEN_HEIGHT:
8435 if (cur == FULLSCREEN_BOTH || cur == FULLSCREEN_WIDTH
8436 || cur == FULLSCREEN_MAXIMIZED)
8437 set_wm_state (frame, 0, dpyinfo->Xatom_net_wm_state_fullscreen,
8438 dpyinfo->Xatom_net_wm_state_maximized_horz);
8439 if (cur != FULLSCREEN_MAXIMIZED)
8440 set_wm_state (frame, 1, dpyinfo->Xatom_net_wm_state_maximized_vert, None);
8441 break;
8442 case FULLSCREEN_MAXIMIZED:
8443 if (cur == FULLSCREEN_BOTH)
8444 set_wm_state (frame, 0, dpyinfo->Xatom_net_wm_state_fullscreen, None);
8445 set_wm_state (frame, 1, dpyinfo->Xatom_net_wm_state_maximized_horz,
8446 dpyinfo->Xatom_net_wm_state_maximized_vert);
8447 break;
8448 case FULLSCREEN_NONE:
8449 if (cur == FULLSCREEN_BOTH)
8450 set_wm_state (frame, 0, dpyinfo->Xatom_net_wm_state_fullscreen, None);
8451 else
8452 set_wm_state (frame, 0, dpyinfo->Xatom_net_wm_state_maximized_horz,
8453 dpyinfo->Xatom_net_wm_state_maximized_vert);
8454 }
8455
8456 f->want_fullscreen = FULLSCREEN_NONE;
8457
8458 }
8459
8460 return have_net_atom;
8461 }
8462
8463 static void
8464 XTfullscreen_hook (struct frame *f)
8465 {
8466 if (FRAME_VISIBLE_P (f))
8467 {
8468 block_input ();
8469 x_check_fullscreen (f);
8470 x_sync (f);
8471 unblock_input ();
8472 }
8473 }
8474
8475
8476 static int
8477 x_handle_net_wm_state (struct frame *f, const XPropertyEvent *event)
8478 {
8479 int value = FULLSCREEN_NONE;
8480 Lisp_Object lval;
8481 int sticky = 0;
8482 int not_hidden = get_current_wm_state (f, event->window, &value, &sticky);
8483
8484 lval = Qnil;
8485 switch (value)
8486 {
8487 case FULLSCREEN_WIDTH:
8488 lval = Qfullwidth;
8489 break;
8490 case FULLSCREEN_HEIGHT:
8491 lval = Qfullheight;
8492 break;
8493 case FULLSCREEN_BOTH:
8494 lval = Qfullboth;
8495 break;
8496 case FULLSCREEN_MAXIMIZED:
8497 lval = Qmaximized;
8498 break;
8499 }
8500
8501 store_frame_param (f, Qfullscreen, lval);
8502 store_frame_param (f, Qsticky, sticky ? Qt : Qnil);
8503
8504 return not_hidden;
8505 }
8506
8507 /* Check if we need to resize the frame due to a fullscreen request.
8508 If so needed, resize the frame. */
8509 static void
8510 x_check_fullscreen (struct frame *f)
8511 {
8512 if (do_ewmh_fullscreen (f))
8513 return;
8514
8515 if (f->output_data.x->parent_desc != FRAME_DISPLAY_INFO (f)->root_window)
8516 return; /* Only fullscreen without WM or with EWM hints (above). */
8517
8518 /* Setting fullscreen to nil doesn't do anything. We could save the
8519 last non-fullscreen size and restore it, but it seems like a
8520 lot of work for this unusual case (no window manager running). */
8521
8522 if (f->want_fullscreen != FULLSCREEN_NONE)
8523 {
8524 int width = FRAME_PIXEL_WIDTH (f), height = FRAME_PIXEL_HEIGHT (f);
8525 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
8526
8527 switch (f->want_fullscreen)
8528 {
8529 /* No difference between these two when there is no WM */
8530 case FULLSCREEN_BOTH:
8531 case FULLSCREEN_MAXIMIZED:
8532 width = x_display_pixel_width (dpyinfo);
8533 height = x_display_pixel_height (dpyinfo);
8534 break;
8535 case FULLSCREEN_WIDTH:
8536 width = x_display_pixel_width (dpyinfo);
8537 break;
8538 case FULLSCREEN_HEIGHT:
8539 height = x_display_pixel_height (dpyinfo);
8540 }
8541
8542 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8543 width, height);
8544 }
8545 }
8546
8547 /* This function is called by x_set_offset to determine whether the window
8548 manager interfered with the positioning of the frame. Type A window
8549 managers position the surrounding window manager decorations a small
8550 amount above and left of the user-supplied position. Type B window
8551 managers position the surrounding window manager decorations at the
8552 user-specified position. If we detect a Type A window manager, we
8553 compensate by moving the window right and down by the proper amount. */
8554
8555 static void
8556 x_check_expected_move (struct frame *f, int expected_left, int expected_top)
8557 {
8558 int current_left = 0, current_top = 0;
8559
8560 /* x_real_positions returns the left and top offsets of the outermost
8561 window manager window around the frame. */
8562
8563 x_real_positions (f, &current_left, &current_top);
8564
8565 if (current_left != expected_left || current_top != expected_top)
8566 {
8567 /* It's a "Type A" window manager. */
8568
8569 int adjusted_left;
8570 int adjusted_top;
8571
8572 FRAME_DISPLAY_INFO (f)->wm_type = X_WMTYPE_A;
8573 FRAME_X_OUTPUT (f)->move_offset_left = expected_left - current_left;
8574 FRAME_X_OUTPUT (f)->move_offset_top = expected_top - current_top;
8575
8576 /* Now fix the mispositioned frame's location. */
8577
8578 adjusted_left = expected_left + FRAME_X_OUTPUT (f)->move_offset_left;
8579 adjusted_top = expected_top + FRAME_X_OUTPUT (f)->move_offset_top;
8580
8581 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8582 adjusted_left, adjusted_top);
8583
8584 x_sync_with_move (f, expected_left, expected_top, 0);
8585 }
8586 else
8587 /* It's a "Type B" window manager. We don't have to adjust the
8588 frame's position. */
8589
8590 FRAME_DISPLAY_INFO (f)->wm_type = X_WMTYPE_B;
8591 }
8592
8593
8594 /* Wait for XGetGeometry to return up-to-date position information for a
8595 recently-moved frame. Call this immediately after calling XMoveWindow.
8596 If FUZZY is non-zero, then LEFT and TOP are just estimates of where the
8597 frame has been moved to, so we use a fuzzy position comparison instead
8598 of an exact comparison. */
8599
8600 static void
8601 x_sync_with_move (struct frame *f, int left, int top, int fuzzy)
8602 {
8603 int count = 0;
8604
8605 while (count++ < 50)
8606 {
8607 int current_left = 0, current_top = 0;
8608
8609 /* In theory, this call to XSync only needs to happen once, but in
8610 practice, it doesn't seem to work, hence the need for the surrounding
8611 loop. */
8612
8613 XSync (FRAME_X_DISPLAY (f), False);
8614 x_real_positions (f, &current_left, &current_top);
8615
8616 if (fuzzy)
8617 {
8618 /* The left fuzz-factor is 10 pixels. The top fuzz-factor is 40
8619 pixels. */
8620
8621 if (eabs (current_left - left) <= 10
8622 && eabs (current_top - top) <= 40)
8623 return;
8624 }
8625 else if (current_left == left && current_top == top)
8626 return;
8627 }
8628
8629 /* As a last resort, just wait 0.5 seconds and hope that XGetGeometry
8630 will then return up-to-date position info. */
8631
8632 wait_reading_process_output (0, 500000, 0, 0, Qnil, NULL, 0);
8633 }
8634
8635
8636 /* Wait for an event on frame F matching EVENTTYPE. */
8637 void
8638 x_wait_for_event (struct frame *f, int eventtype)
8639 {
8640 int level = interrupt_input_blocked;
8641
8642 fd_set fds;
8643 struct timespec tmo, tmo_at, time_now;
8644 int fd = ConnectionNumber (FRAME_X_DISPLAY (f));
8645
8646 f->wait_event_type = eventtype;
8647
8648 /* Set timeout to 0.1 second. Hopefully not noticeable.
8649 Maybe it should be configurable. */
8650 tmo = make_timespec (0, 100 * 1000 * 1000);
8651 tmo_at = timespec_add (current_timespec (), tmo);
8652
8653 while (f->wait_event_type)
8654 {
8655 pending_signals = 1;
8656 totally_unblock_input ();
8657 /* XTread_socket is called after unblock. */
8658 block_input ();
8659 interrupt_input_blocked = level;
8660
8661 FD_ZERO (&fds);
8662 FD_SET (fd, &fds);
8663
8664 time_now = current_timespec ();
8665 if (timespec_cmp (tmo_at, time_now) < 0)
8666 break;
8667
8668 tmo = timespec_sub (tmo_at, time_now);
8669 if (pselect (fd + 1, &fds, NULL, NULL, &tmo, NULL) == 0)
8670 break; /* Timeout */
8671 }
8672
8673 f->wait_event_type = 0;
8674 }
8675
8676
8677 /* Change the size of frame F's X window to COLS/ROWS in the case F
8678 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
8679 top-left-corner window gravity for this size change and subsequent
8680 size changes. Otherwise we leave the window gravity unchanged. */
8681
8682 static void
8683 x_set_window_size_1 (struct frame *f, int change_gravity, int cols, int rows)
8684 {
8685 int pixelwidth, pixelheight;
8686
8687 check_frame_size (f, &rows, &cols);
8688 f->scroll_bar_actual_width
8689 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
8690 ? 0
8691 : FRAME_CONFIG_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f));
8692
8693 compute_fringe_widths (f, 0);
8694
8695 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols)
8696 + FRAME_TOOLBAR_WIDTH (f);
8697 pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows)
8698 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f);
8699
8700 if (change_gravity) f->win_gravity = NorthWestGravity;
8701 x_wm_set_size_hint (f, (long) 0, 0);
8702 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8703 pixelwidth, pixelheight);
8704
8705
8706 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
8707 receive in the ConfigureNotify event; if we get what we asked
8708 for, then the event won't cause the screen to become garbaged, so
8709 we have to make sure to do it here. */
8710 SET_FRAME_GARBAGED (f);
8711
8712 /* Now, strictly speaking, we can't be sure that this is accurate,
8713 but the window manager will get around to dealing with the size
8714 change request eventually, and we'll hear how it went when the
8715 ConfigureNotify event gets here.
8716
8717 We could just not bother storing any of this information here,
8718 and let the ConfigureNotify event set everything up, but that
8719 might be kind of confusing to the Lisp code, since size changes
8720 wouldn't be reported in the frame parameters until some random
8721 point in the future when the ConfigureNotify event arrives.
8722
8723 We pass 1 for DELAY since we can't run Lisp code inside of
8724 a BLOCK_INPUT. */
8725
8726 /* But the ConfigureNotify may in fact never arrive, and then this is
8727 not right if the frame is visible. Instead wait (with timeout)
8728 for the ConfigureNotify. */
8729 if (FRAME_VISIBLE_P (f))
8730 x_wait_for_event (f, ConfigureNotify);
8731 else
8732 {
8733 change_frame_size (f, rows, cols, 0, 1, 0);
8734 FRAME_PIXEL_WIDTH (f) = pixelwidth;
8735 FRAME_PIXEL_HEIGHT (f) = pixelheight;
8736 x_sync (f);
8737 }
8738 }
8739
8740
8741 /* Call this to change the size of frame F's x-window.
8742 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
8743 for this size change and subsequent size changes.
8744 Otherwise we leave the window gravity unchanged. */
8745
8746 void
8747 x_set_window_size (struct frame *f, int change_gravity, int cols, int rows)
8748 {
8749 block_input ();
8750
8751 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
8752 {
8753 int r, c;
8754
8755 /* When the frame is maximized/fullscreen or running under for
8756 example Xmonad, x_set_window_size_1 will be a no-op.
8757 In that case, the right thing to do is extend rows/cols to
8758 the current frame size. We do that first if x_set_window_size_1
8759 turns out to not be a no-op (there is no way to know).
8760 The size will be adjusted again if the frame gets a
8761 ConfigureNotify event as a result of x_set_window_size. */
8762 int pixelh = FRAME_PIXEL_HEIGHT (f);
8763 #ifdef USE_X_TOOLKIT
8764 /* The menu bar is not part of text lines. The tool bar
8765 is however. */
8766 pixelh -= FRAME_MENUBAR_HEIGHT (f);
8767 #endif
8768 r = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, pixelh);
8769 /* Update f->scroll_bar_actual_width because it is used in
8770 FRAME_PIXEL_WIDTH_TO_TEXT_COLS. */
8771 f->scroll_bar_actual_width
8772 = FRAME_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f);
8773 c = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, FRAME_PIXEL_WIDTH (f));
8774 change_frame_size (f, r, c, 0, 1, 0);
8775 }
8776
8777 #ifdef USE_GTK
8778 if (FRAME_GTK_WIDGET (f))
8779 xg_frame_set_char_size (f, cols, rows);
8780 else
8781 x_set_window_size_1 (f, change_gravity, cols, rows);
8782 #else /* not USE_GTK */
8783
8784 x_set_window_size_1 (f, change_gravity, cols, rows);
8785
8786 #endif /* not USE_GTK */
8787
8788 /* If cursor was outside the new size, mark it as off. */
8789 mark_window_cursors_off (XWINDOW (f->root_window));
8790
8791 /* Clear out any recollection of where the mouse highlighting was,
8792 since it might be in a place that's outside the new frame size.
8793 Actually checking whether it is outside is a pain in the neck,
8794 so don't try--just let the highlighting be done afresh with new size. */
8795 cancel_mouse_face (f);
8796
8797 unblock_input ();
8798 }
8799 \f
8800 /* Mouse warping. */
8801
8802 void
8803 x_set_mouse_position (struct frame *f, int x, int y)
8804 {
8805 int pix_x, pix_y;
8806
8807 pix_x = FRAME_COL_TO_PIXEL_X (f, x) + FRAME_COLUMN_WIDTH (f) / 2;
8808 pix_y = FRAME_LINE_TO_PIXEL_Y (f, y) + FRAME_LINE_HEIGHT (f) / 2;
8809
8810 if (pix_x < 0) pix_x = 0;
8811 if (pix_x > FRAME_PIXEL_WIDTH (f)) pix_x = FRAME_PIXEL_WIDTH (f);
8812
8813 if (pix_y < 0) pix_y = 0;
8814 if (pix_y > FRAME_PIXEL_HEIGHT (f)) pix_y = FRAME_PIXEL_HEIGHT (f);
8815
8816 block_input ();
8817
8818 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
8819 0, 0, 0, 0, pix_x, pix_y);
8820 unblock_input ();
8821 }
8822
8823 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
8824
8825 void
8826 x_set_mouse_pixel_position (struct frame *f, int pix_x, int pix_y)
8827 {
8828 block_input ();
8829
8830 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
8831 0, 0, 0, 0, pix_x, pix_y);
8832 unblock_input ();
8833 }
8834 \f
8835 /* Raise frame F. */
8836
8837 void
8838 x_raise_frame (struct frame *f)
8839 {
8840 block_input ();
8841 if (FRAME_VISIBLE_P (f))
8842 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
8843 XFlush (FRAME_X_DISPLAY (f));
8844 unblock_input ();
8845 }
8846
8847 /* Lower frame F. */
8848
8849 static void
8850 x_lower_frame (struct frame *f)
8851 {
8852 if (FRAME_VISIBLE_P (f))
8853 {
8854 block_input ();
8855 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
8856 XFlush (FRAME_X_DISPLAY (f));
8857 unblock_input ();
8858 }
8859 }
8860
8861 /* Request focus with XEmbed */
8862
8863 void
8864 xembed_request_focus (struct frame *f)
8865 {
8866 /* See XEmbed Protocol Specification at
8867 http://freedesktop.org/wiki/Specifications/xembed-spec */
8868 if (FRAME_VISIBLE_P (f))
8869 xembed_send_message (f, CurrentTime,
8870 XEMBED_REQUEST_FOCUS, 0, 0, 0);
8871 }
8872
8873 /* Activate frame with Extended Window Manager Hints */
8874
8875 void
8876 x_ewmh_activate_frame (struct frame *f)
8877 {
8878 /* See Window Manager Specification/Extended Window Manager Hints at
8879 http://freedesktop.org/wiki/Specifications/wm-spec */
8880
8881 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
8882
8883 if (FRAME_VISIBLE_P (f) && wm_supports (f, dpyinfo->Xatom_net_active_window))
8884 {
8885 Lisp_Object frame;
8886 XSETFRAME (frame, f);
8887 x_send_client_event (frame, make_number (0), frame,
8888 dpyinfo->Xatom_net_active_window,
8889 make_number (32),
8890 list2i (1, dpyinfo->last_user_time));
8891 }
8892 }
8893
8894 static void
8895 XTframe_raise_lower (struct frame *f, int raise_flag)
8896 {
8897 if (raise_flag)
8898 x_raise_frame (f);
8899 else
8900 x_lower_frame (f);
8901 }
8902 \f
8903 /* XEmbed implementation. */
8904
8905 #if defined USE_X_TOOLKIT || ! defined USE_GTK
8906
8907 /* XEmbed implementation. */
8908
8909 #define XEMBED_VERSION 0
8910
8911 static void
8912 xembed_set_info (struct frame *f, enum xembed_info flags)
8913 {
8914 unsigned long data[2];
8915 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
8916
8917 data[0] = XEMBED_VERSION;
8918 data[1] = flags;
8919
8920 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8921 dpyinfo->Xatom_XEMBED_INFO, dpyinfo->Xatom_XEMBED_INFO,
8922 32, PropModeReplace, (unsigned char *) data, 2);
8923 }
8924 #endif /* defined USE_X_TOOLKIT || ! defined USE_GTK */
8925
8926 static void
8927 xembed_send_message (struct frame *f, Time t, enum xembed_message msg,
8928 long int detail, long int data1, long int data2)
8929 {
8930 XEvent event;
8931
8932 event.xclient.type = ClientMessage;
8933 event.xclient.window = FRAME_X_OUTPUT (f)->parent_desc;
8934 event.xclient.message_type = FRAME_DISPLAY_INFO (f)->Xatom_XEMBED;
8935 event.xclient.format = 32;
8936 event.xclient.data.l[0] = t;
8937 event.xclient.data.l[1] = msg;
8938 event.xclient.data.l[2] = detail;
8939 event.xclient.data.l[3] = data1;
8940 event.xclient.data.l[4] = data2;
8941
8942 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_OUTPUT (f)->parent_desc,
8943 False, NoEventMask, &event);
8944 XSync (FRAME_X_DISPLAY (f), False);
8945 }
8946 \f
8947 /* Change of visibility. */
8948
8949 /* This tries to wait until the frame is really visible.
8950 However, if the window manager asks the user where to position
8951 the frame, this will return before the user finishes doing that.
8952 The frame will not actually be visible at that time,
8953 but it will become visible later when the window manager
8954 finishes with it. */
8955
8956 void
8957 x_make_frame_visible (struct frame *f)
8958 {
8959 int original_top, original_left;
8960 int retry_count = 2;
8961
8962 retry:
8963
8964 block_input ();
8965
8966 x_set_bitmap_icon (f);
8967
8968 if (! FRAME_VISIBLE_P (f))
8969 {
8970 /* We test FRAME_GARBAGED_P here to make sure we don't
8971 call x_set_offset a second time
8972 if we get to x_make_frame_visible a second time
8973 before the window gets really visible. */
8974 if (! FRAME_ICONIFIED_P (f)
8975 && ! FRAME_X_EMBEDDED_P (f)
8976 && ! f->output_data.x->asked_for_visible)
8977 x_set_offset (f, f->left_pos, f->top_pos, 0);
8978
8979 f->output_data.x->asked_for_visible = 1;
8980
8981 if (! EQ (Vx_no_window_manager, Qt))
8982 x_wm_set_window_state (f, NormalState);
8983 #ifdef USE_X_TOOLKIT
8984 if (FRAME_X_EMBEDDED_P (f))
8985 xembed_set_info (f, XEMBED_MAPPED);
8986 else
8987 {
8988 /* This was XtPopup, but that did nothing for an iconified frame. */
8989 XtMapWidget (f->output_data.x->widget);
8990 }
8991 #else /* not USE_X_TOOLKIT */
8992 #ifdef USE_GTK
8993 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
8994 gtk_window_deiconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
8995 #else
8996 if (FRAME_X_EMBEDDED_P (f))
8997 xembed_set_info (f, XEMBED_MAPPED);
8998 else
8999 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9000 #endif /* not USE_GTK */
9001 #endif /* not USE_X_TOOLKIT */
9002 }
9003
9004 XFlush (FRAME_X_DISPLAY (f));
9005
9006 /* Synchronize to ensure Emacs knows the frame is visible
9007 before we do anything else. We do this loop with input not blocked
9008 so that incoming events are handled. */
9009 {
9010 Lisp_Object frame;
9011 int count;
9012 /* This must be before UNBLOCK_INPUT
9013 since events that arrive in response to the actions above
9014 will set it when they are handled. */
9015 int previously_visible = f->output_data.x->has_been_visible;
9016
9017 original_left = f->left_pos;
9018 original_top = f->top_pos;
9019
9020 /* This must come after we set COUNT. */
9021 unblock_input ();
9022
9023 /* We unblock here so that arriving X events are processed. */
9024
9025 /* Now move the window back to where it was "supposed to be".
9026 But don't do it if the gravity is negative.
9027 When the gravity is negative, this uses a position
9028 that is 3 pixels too low. Perhaps that's really the border width.
9029
9030 Don't do this if the window has never been visible before,
9031 because the window manager may choose the position
9032 and we don't want to override it. */
9033
9034 if (! FRAME_VISIBLE_P (f)
9035 && ! FRAME_ICONIFIED_P (f)
9036 && ! FRAME_X_EMBEDDED_P (f)
9037 && f->win_gravity == NorthWestGravity
9038 && previously_visible)
9039 {
9040 Drawable rootw;
9041 int x, y;
9042 unsigned int width, height, border, depth;
9043
9044 block_input ();
9045
9046 /* On some window managers (such as FVWM) moving an existing
9047 window, even to the same place, causes the window manager
9048 to introduce an offset. This can cause the window to move
9049 to an unexpected location. Check the geometry (a little
9050 slow here) and then verify that the window is in the right
9051 place. If the window is not in the right place, move it
9052 there, and take the potential window manager hit. */
9053 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9054 &rootw, &x, &y, &width, &height, &border, &depth);
9055
9056 if (original_left != x || original_top != y)
9057 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9058 original_left, original_top);
9059
9060 unblock_input ();
9061 }
9062
9063 XSETFRAME (frame, f);
9064
9065 /* Wait until the frame is visible. Process X events until a
9066 MapNotify event has been seen, or until we think we won't get a
9067 MapNotify at all.. */
9068 for (count = input_signal_count + 10;
9069 input_signal_count < count && !FRAME_VISIBLE_P (f);)
9070 {
9071 /* Force processing of queued events. */
9072 x_sync (f);
9073
9074 /* Machines that do polling rather than SIGIO have been
9075 observed to go into a busy-wait here. So we'll fake an
9076 alarm signal to let the handler know that there's something
9077 to be read. We used to raise a real alarm, but it seems
9078 that the handler isn't always enabled here. This is
9079 probably a bug. */
9080 if (input_polling_used ())
9081 {
9082 /* It could be confusing if a real alarm arrives while
9083 processing the fake one. Turn it off and let the
9084 handler reset it. */
9085 int old_poll_suppress_count = poll_suppress_count;
9086 poll_suppress_count = 1;
9087 poll_for_input_1 ();
9088 poll_suppress_count = old_poll_suppress_count;
9089 }
9090 }
9091
9092 /* 2000-09-28: In
9093
9094 (let ((f (selected-frame)))
9095 (iconify-frame f)
9096 (raise-frame f))
9097
9098 the frame is not raised with various window managers on
9099 FreeBSD, GNU/Linux and Solaris. It turns out that, for some
9100 unknown reason, the call to XtMapWidget is completely ignored.
9101 Mapping the widget a second time works. */
9102
9103 if (!FRAME_VISIBLE_P (f) && --retry_count != 0)
9104 goto retry;
9105 }
9106 }
9107
9108 /* Change from mapped state to withdrawn state. */
9109
9110 /* Make the frame visible (mapped and not iconified). */
9111
9112 void
9113 x_make_frame_invisible (struct frame *f)
9114 {
9115 Window window;
9116
9117 /* Use the frame's outermost window, not the one we normally draw on. */
9118 window = FRAME_OUTER_WINDOW (f);
9119
9120 /* Don't keep the highlight on an invisible frame. */
9121 if (FRAME_DISPLAY_INFO (f)->x_highlight_frame == f)
9122 FRAME_DISPLAY_INFO (f)->x_highlight_frame = 0;
9123
9124 block_input ();
9125
9126 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
9127 that the current position of the window is user-specified, rather than
9128 program-specified, so that when the window is mapped again, it will be
9129 placed at the same location, without forcing the user to position it
9130 by hand again (they have already done that once for this window.) */
9131 x_wm_set_size_hint (f, (long) 0, 1);
9132
9133 #ifdef USE_GTK
9134 if (FRAME_GTK_OUTER_WIDGET (f))
9135 gtk_widget_hide (FRAME_GTK_OUTER_WIDGET (f));
9136 else
9137 #else
9138 if (FRAME_X_EMBEDDED_P (f))
9139 xembed_set_info (f, 0);
9140 else
9141 #endif
9142 {
9143
9144 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
9145 DefaultScreen (FRAME_X_DISPLAY (f))))
9146 {
9147 unblock_input ();
9148 error ("Can't notify window manager of window withdrawal");
9149 }
9150 }
9151
9152 /* We can't distinguish this from iconification
9153 just by the event that we get from the server.
9154 So we can't win using the usual strategy of letting
9155 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
9156 and synchronize with the server to make sure we agree. */
9157 SET_FRAME_VISIBLE (f, 0);
9158 SET_FRAME_ICONIFIED (f, 0);
9159
9160 x_sync (f);
9161
9162 unblock_input ();
9163 }
9164
9165 /* Change window state from mapped to iconified. */
9166
9167 void
9168 x_iconify_frame (struct frame *f)
9169 {
9170 #ifdef USE_X_TOOLKIT
9171 int result;
9172 #endif
9173
9174 /* Don't keep the highlight on an invisible frame. */
9175 if (FRAME_DISPLAY_INFO (f)->x_highlight_frame == f)
9176 FRAME_DISPLAY_INFO (f)->x_highlight_frame = 0;
9177
9178 if (FRAME_ICONIFIED_P (f))
9179 return;
9180
9181 block_input ();
9182
9183 x_set_bitmap_icon (f);
9184
9185 #if defined (USE_GTK)
9186 if (FRAME_GTK_OUTER_WIDGET (f))
9187 {
9188 if (! FRAME_VISIBLE_P (f))
9189 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
9190
9191 gtk_window_iconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
9192 SET_FRAME_VISIBLE (f, 0);
9193 SET_FRAME_ICONIFIED (f, 1);
9194 unblock_input ();
9195 return;
9196 }
9197 #endif
9198
9199 #ifdef USE_X_TOOLKIT
9200
9201 if (! FRAME_VISIBLE_P (f))
9202 {
9203 if (! EQ (Vx_no_window_manager, Qt))
9204 x_wm_set_window_state (f, IconicState);
9205 /* This was XtPopup, but that did nothing for an iconified frame. */
9206 XtMapWidget (f->output_data.x->widget);
9207 /* The server won't give us any event to indicate
9208 that an invisible frame was changed to an icon,
9209 so we have to record it here. */
9210 SET_FRAME_VISIBLE (f, 0);
9211 SET_FRAME_ICONIFIED (f, 1);
9212 unblock_input ();
9213 return;
9214 }
9215
9216 result = XIconifyWindow (FRAME_X_DISPLAY (f),
9217 XtWindow (f->output_data.x->widget),
9218 DefaultScreen (FRAME_X_DISPLAY (f)));
9219 unblock_input ();
9220
9221 if (!result)
9222 error ("Can't notify window manager of iconification");
9223
9224 SET_FRAME_ICONIFIED (f, 1);
9225 SET_FRAME_VISIBLE (f, 0);
9226
9227 block_input ();
9228 XFlush (FRAME_X_DISPLAY (f));
9229 unblock_input ();
9230 #else /* not USE_X_TOOLKIT */
9231
9232 /* Make sure the X server knows where the window should be positioned,
9233 in case the user deiconifies with the window manager. */
9234 if (! FRAME_VISIBLE_P (f)
9235 && ! FRAME_ICONIFIED_P (f)
9236 && ! FRAME_X_EMBEDDED_P (f))
9237 x_set_offset (f, f->left_pos, f->top_pos, 0);
9238
9239 /* Since we don't know which revision of X we're running, we'll use both
9240 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
9241
9242 /* X11R4: send a ClientMessage to the window manager using the
9243 WM_CHANGE_STATE type. */
9244 {
9245 XEvent msg;
9246
9247 msg.xclient.window = FRAME_X_WINDOW (f);
9248 msg.xclient.type = ClientMessage;
9249 msg.xclient.message_type = FRAME_DISPLAY_INFO (f)->Xatom_wm_change_state;
9250 msg.xclient.format = 32;
9251 msg.xclient.data.l[0] = IconicState;
9252
9253 if (! XSendEvent (FRAME_X_DISPLAY (f),
9254 DefaultRootWindow (FRAME_X_DISPLAY (f)),
9255 False,
9256 SubstructureRedirectMask | SubstructureNotifyMask,
9257 &msg))
9258 {
9259 unblock_input ();
9260 error ("Can't notify window manager of iconification");
9261 }
9262 }
9263
9264 /* X11R3: set the initial_state field of the window manager hints to
9265 IconicState. */
9266 x_wm_set_window_state (f, IconicState);
9267
9268 if (!FRAME_VISIBLE_P (f))
9269 {
9270 /* If the frame was withdrawn, before, we must map it. */
9271 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9272 }
9273
9274 SET_FRAME_ICONIFIED (f, 1);
9275 SET_FRAME_VISIBLE (f, 0);
9276
9277 XFlush (FRAME_X_DISPLAY (f));
9278 unblock_input ();
9279 #endif /* not USE_X_TOOLKIT */
9280 }
9281
9282 \f
9283 /* Free X resources of frame F. */
9284
9285 void
9286 x_free_frame_resources (struct frame *f)
9287 {
9288 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
9289 Mouse_HLInfo *hlinfo = &dpyinfo->mouse_highlight;
9290 #ifdef USE_X_TOOLKIT
9291 Lisp_Object bar;
9292 struct scroll_bar *b;
9293 #endif
9294
9295 block_input ();
9296
9297 /* If a display connection is dead, don't try sending more
9298 commands to the X server. */
9299 if (dpyinfo->display)
9300 {
9301 /* We must free faces before destroying windows because some
9302 font-driver (e.g. xft) access a window while finishing a
9303 face. */
9304 if (FRAME_FACE_CACHE (f))
9305 free_frame_faces (f);
9306
9307 if (f->output_data.x->icon_desc)
9308 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
9309
9310 #ifdef USE_X_TOOLKIT
9311 /* Explicitly destroy the scroll bars of the frame. Without
9312 this, we get "BadDrawable" errors from the toolkit later on,
9313 presumably from expose events generated for the disappearing
9314 toolkit scroll bars. */
9315 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
9316 {
9317 b = XSCROLL_BAR (bar);
9318 x_scroll_bar_remove (b);
9319 }
9320 #endif
9321
9322 #ifdef HAVE_X_I18N
9323 if (FRAME_XIC (f))
9324 free_frame_xic (f);
9325 #endif
9326
9327 #ifdef USE_X_TOOLKIT
9328 if (f->output_data.x->widget)
9329 {
9330 XtDestroyWidget (f->output_data.x->widget);
9331 f->output_data.x->widget = NULL;
9332 }
9333 /* Tooltips don't have widgets, only a simple X window, even if
9334 we are using a toolkit. */
9335 else if (FRAME_X_WINDOW (f))
9336 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9337
9338 free_frame_menubar (f);
9339 #else /* !USE_X_TOOLKIT */
9340
9341 #ifdef USE_GTK
9342 xg_free_frame_widgets (f);
9343 #endif /* USE_GTK */
9344
9345 if (FRAME_X_WINDOW (f))
9346 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9347 #endif /* !USE_X_TOOLKIT */
9348
9349 unload_color (f, FRAME_FOREGROUND_PIXEL (f));
9350 unload_color (f, FRAME_BACKGROUND_PIXEL (f));
9351 unload_color (f, f->output_data.x->cursor_pixel);
9352 unload_color (f, f->output_data.x->cursor_foreground_pixel);
9353 unload_color (f, f->output_data.x->border_pixel);
9354 unload_color (f, f->output_data.x->mouse_pixel);
9355
9356 if (f->output_data.x->scroll_bar_background_pixel != -1)
9357 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
9358 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9359 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
9360 #ifdef USE_TOOLKIT_SCROLL_BARS
9361 /* Scrollbar shadow colors. */
9362 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
9363 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
9364 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
9365 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
9366 #endif /* USE_TOOLKIT_SCROLL_BARS */
9367 if (f->output_data.x->white_relief.allocated_p)
9368 unload_color (f, f->output_data.x->white_relief.pixel);
9369 if (f->output_data.x->black_relief.allocated_p)
9370 unload_color (f, f->output_data.x->black_relief.pixel);
9371
9372 x_free_gcs (f);
9373
9374 /* Free extra GCs allocated by x_setup_relief_colors. */
9375 if (f->output_data.x->white_relief.gc)
9376 {
9377 XFreeGC (dpyinfo->display, f->output_data.x->white_relief.gc);
9378 f->output_data.x->white_relief.gc = 0;
9379 }
9380 if (f->output_data.x->black_relief.gc)
9381 {
9382 XFreeGC (dpyinfo->display, f->output_data.x->black_relief.gc);
9383 f->output_data.x->black_relief.gc = 0;
9384 }
9385
9386 XFlush (FRAME_X_DISPLAY (f));
9387 }
9388
9389 xfree (f->output_data.x->saved_menu_event);
9390 xfree (f->output_data.x);
9391 f->output_data.x = NULL;
9392
9393 if (f == dpyinfo->x_focus_frame)
9394 dpyinfo->x_focus_frame = 0;
9395 if (f == dpyinfo->x_focus_event_frame)
9396 dpyinfo->x_focus_event_frame = 0;
9397 if (f == dpyinfo->x_highlight_frame)
9398 dpyinfo->x_highlight_frame = 0;
9399 if (f == hlinfo->mouse_face_mouse_frame)
9400 reset_mouse_highlight (hlinfo);
9401
9402 unblock_input ();
9403 }
9404
9405
9406 /* Destroy the X window of frame F. */
9407
9408 static void
9409 x_destroy_window (struct frame *f)
9410 {
9411 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
9412
9413 /* If a display connection is dead, don't try sending more
9414 commands to the X server. */
9415 if (dpyinfo->display != 0)
9416 x_free_frame_resources (f);
9417
9418 dpyinfo->reference_count--;
9419 }
9420
9421 \f
9422 /* Setting window manager hints. */
9423
9424 /* Set the normal size hints for the window manager, for frame F.
9425 FLAGS is the flags word to use--or 0 meaning preserve the flags
9426 that the window now has.
9427 If USER_POSITION, set the USPosition
9428 flag (this is useful when FLAGS is 0).
9429 The GTK version is in gtkutils.c. */
9430
9431 #ifndef USE_GTK
9432 void
9433 x_wm_set_size_hint (struct frame *f, long flags, bool user_position)
9434 {
9435 XSizeHints size_hints;
9436 Window window = FRAME_OUTER_WINDOW (f);
9437
9438 #ifdef USE_X_TOOLKIT
9439 if (f->output_data.x->widget)
9440 {
9441 widget_update_wm_size_hints (f->output_data.x->widget);
9442 return;
9443 }
9444 #endif
9445
9446 /* Setting PMaxSize caused various problems. */
9447 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
9448
9449 size_hints.x = f->left_pos;
9450 size_hints.y = f->top_pos;
9451
9452 size_hints.height = FRAME_PIXEL_HEIGHT (f);
9453 size_hints.width = FRAME_PIXEL_WIDTH (f);
9454
9455 size_hints.width_inc = FRAME_COLUMN_WIDTH (f);
9456 size_hints.height_inc = FRAME_LINE_HEIGHT (f);
9457 size_hints.max_width = x_display_pixel_width (FRAME_DISPLAY_INFO (f))
9458 - FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9459 size_hints.max_height = x_display_pixel_height (FRAME_DISPLAY_INFO (f))
9460 - FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
9461
9462 /* Calculate the base and minimum sizes. */
9463 {
9464 int base_width, base_height;
9465 int min_rows = 0, min_cols = 0;
9466
9467 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9468 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
9469
9470 check_frame_size (f, &min_rows, &min_cols);
9471
9472 /* The window manager uses the base width hints to calculate the
9473 current number of rows and columns in the frame while
9474 resizing; min_width and min_height aren't useful for this
9475 purpose, since they might not give the dimensions for a
9476 zero-row, zero-column frame.
9477
9478 We use the base_width and base_height members if we have
9479 them; otherwise, we set the min_width and min_height members
9480 to the size for a zero x zero frame. */
9481
9482 size_hints.flags |= PBaseSize;
9483 size_hints.base_width = base_width;
9484 size_hints.base_height = base_height + FRAME_MENUBAR_HEIGHT (f);
9485 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
9486 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
9487 }
9488
9489 /* If we don't need the old flags, we don't need the old hint at all. */
9490 if (flags)
9491 {
9492 size_hints.flags |= flags;
9493 goto no_read;
9494 }
9495
9496 {
9497 XSizeHints hints; /* Sometimes I hate X Windows... */
9498 long supplied_return;
9499 int value;
9500
9501 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
9502 &supplied_return);
9503
9504 if (flags)
9505 size_hints.flags |= flags;
9506 else
9507 {
9508 if (value == 0)
9509 hints.flags = 0;
9510 if (hints.flags & PSize)
9511 size_hints.flags |= PSize;
9512 if (hints.flags & PPosition)
9513 size_hints.flags |= PPosition;
9514 if (hints.flags & USPosition)
9515 size_hints.flags |= USPosition;
9516 if (hints.flags & USSize)
9517 size_hints.flags |= USSize;
9518 }
9519 }
9520
9521 no_read:
9522
9523 #ifdef PWinGravity
9524 size_hints.win_gravity = f->win_gravity;
9525 size_hints.flags |= PWinGravity;
9526
9527 if (user_position)
9528 {
9529 size_hints.flags &= ~ PPosition;
9530 size_hints.flags |= USPosition;
9531 }
9532 #endif /* PWinGravity */
9533
9534 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
9535 }
9536 #endif /* not USE_GTK */
9537
9538 /* Used for IconicState or NormalState */
9539
9540 static void
9541 x_wm_set_window_state (struct frame *f, int state)
9542 {
9543 #ifdef USE_X_TOOLKIT
9544 Arg al[1];
9545
9546 XtSetArg (al[0], XtNinitialState, state);
9547 XtSetValues (f->output_data.x->widget, al, 1);
9548 #else /* not USE_X_TOOLKIT */
9549 Window window = FRAME_X_WINDOW (f);
9550
9551 f->output_data.x->wm_hints.flags |= StateHint;
9552 f->output_data.x->wm_hints.initial_state = state;
9553
9554 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9555 #endif /* not USE_X_TOOLKIT */
9556 }
9557
9558 static void
9559 x_wm_set_icon_pixmap (struct frame *f, ptrdiff_t pixmap_id)
9560 {
9561 Pixmap icon_pixmap, icon_mask;
9562
9563 #if !defined USE_X_TOOLKIT && !defined USE_GTK
9564 Window window = FRAME_OUTER_WINDOW (f);
9565 #endif
9566
9567 if (pixmap_id > 0)
9568 {
9569 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
9570 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
9571 icon_mask = x_bitmap_mask (f, pixmap_id);
9572 f->output_data.x->wm_hints.icon_mask = icon_mask;
9573 }
9574 else
9575 {
9576 /* It seems there is no way to turn off use of an icon
9577 pixmap. */
9578 return;
9579 }
9580
9581
9582 #ifdef USE_GTK
9583 {
9584 xg_set_frame_icon (f, icon_pixmap, icon_mask);
9585 return;
9586 }
9587
9588 #elif defined (USE_X_TOOLKIT) /* same as in x_wm_set_window_state. */
9589
9590 {
9591 Arg al[1];
9592 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
9593 XtSetValues (f->output_data.x->widget, al, 1);
9594 XtSetArg (al[0], XtNiconMask, icon_mask);
9595 XtSetValues (f->output_data.x->widget, al, 1);
9596 }
9597
9598 #else /* not USE_X_TOOLKIT && not USE_GTK */
9599
9600 f->output_data.x->wm_hints.flags |= (IconPixmapHint | IconMaskHint);
9601 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9602
9603 #endif /* not USE_X_TOOLKIT && not USE_GTK */
9604 }
9605
9606 void
9607 x_wm_set_icon_position (struct frame *f, int icon_x, int icon_y)
9608 {
9609 Window window = FRAME_OUTER_WINDOW (f);
9610
9611 f->output_data.x->wm_hints.flags |= IconPositionHint;
9612 f->output_data.x->wm_hints.icon_x = icon_x;
9613 f->output_data.x->wm_hints.icon_y = icon_y;
9614
9615 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9616 }
9617
9618 \f
9619 /***********************************************************************
9620 Fonts
9621 ***********************************************************************/
9622
9623 #ifdef GLYPH_DEBUG
9624
9625 /* Check that FONT is valid on frame F. It is if it can be found in F's
9626 font table. */
9627
9628 static void
9629 x_check_font (struct frame *f, struct font *font)
9630 {
9631 eassert (font != NULL && ! NILP (font->props[FONT_TYPE_INDEX]));
9632 if (font->driver->check)
9633 eassert (font->driver->check (f, font) == 0);
9634 }
9635
9636 #endif /* GLYPH_DEBUG */
9637
9638 \f
9639 /***********************************************************************
9640 Initialization
9641 ***********************************************************************/
9642
9643 #ifdef USE_X_TOOLKIT
9644 static XrmOptionDescRec emacs_options[] = {
9645 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
9646 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
9647
9648 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
9649 XrmoptionSepArg, NULL},
9650 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
9651
9652 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
9653 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
9654 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
9655 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
9656 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
9657 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
9658 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
9659 };
9660
9661 /* Whether atimer for Xt timeouts is activated or not. */
9662
9663 static int x_timeout_atimer_activated_flag;
9664
9665 #endif /* USE_X_TOOLKIT */
9666
9667 static int x_initialized;
9668
9669 /* Test whether two display-name strings agree up to the dot that separates
9670 the screen number from the server number. */
9671 static int
9672 same_x_server (const char *name1, const char *name2)
9673 {
9674 int seen_colon = 0;
9675 const char *system_name = SSDATA (Vsystem_name);
9676 ptrdiff_t system_name_length = SBYTES (Vsystem_name);
9677 ptrdiff_t length_until_period = 0;
9678
9679 while (system_name[length_until_period] != 0
9680 && system_name[length_until_period] != '.')
9681 length_until_period++;
9682
9683 /* Treat `unix' like an empty host name. */
9684 if (! strncmp (name1, "unix:", 5))
9685 name1 += 4;
9686 if (! strncmp (name2, "unix:", 5))
9687 name2 += 4;
9688 /* Treat this host's name like an empty host name. */
9689 if (! strncmp (name1, system_name, system_name_length)
9690 && name1[system_name_length] == ':')
9691 name1 += system_name_length;
9692 if (! strncmp (name2, system_name, system_name_length)
9693 && name2[system_name_length] == ':')
9694 name2 += system_name_length;
9695 /* Treat this host's domainless name like an empty host name. */
9696 if (! strncmp (name1, system_name, length_until_period)
9697 && name1[length_until_period] == ':')
9698 name1 += length_until_period;
9699 if (! strncmp (name2, system_name, length_until_period)
9700 && name2[length_until_period] == ':')
9701 name2 += length_until_period;
9702
9703 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
9704 {
9705 if (*name1 == ':')
9706 seen_colon = 1;
9707 if (seen_colon && *name1 == '.')
9708 return 1;
9709 }
9710 return (seen_colon
9711 && (*name1 == '.' || *name1 == '\0')
9712 && (*name2 == '.' || *name2 == '\0'));
9713 }
9714
9715 /* Count number of set bits in mask and number of bits to shift to
9716 get to the first bit. With MASK 0x7e0, *BITS is set to 6, and *OFFSET
9717 to 5. */
9718 static void
9719 get_bits_and_offset (long unsigned int mask, int *bits, int *offset)
9720 {
9721 int nr = 0;
9722 int off = 0;
9723
9724 while (!(mask & 1))
9725 {
9726 off++;
9727 mask >>= 1;
9728 }
9729
9730 while (mask & 1)
9731 {
9732 nr++;
9733 mask >>= 1;
9734 }
9735
9736 *offset = off;
9737 *bits = nr;
9738 }
9739
9740 /* Return 1 if display DISPLAY is available for use, 0 otherwise.
9741 But don't permanently open it, just test its availability. */
9742
9743 bool
9744 x_display_ok (const char *display)
9745 {
9746 Display *dpy = XOpenDisplay (display);
9747 return dpy ? (XCloseDisplay (dpy), 1) : 0;
9748 }
9749
9750 #ifdef USE_GTK
9751 static void
9752 my_log_handler (const gchar *log_domain, GLogLevelFlags log_level,
9753 const gchar *msg, gpointer user_data)
9754 {
9755 if (!strstr (msg, "g_set_prgname"))
9756 fprintf (stderr, "%s-WARNING **: %s\n", log_domain, msg);
9757 }
9758 #endif
9759
9760 /* Open a connection to X display DISPLAY_NAME, and return
9761 the structure that describes the open display.
9762 If we cannot contact the display, return null. */
9763
9764 struct x_display_info *
9765 x_term_init (Lisp_Object display_name, char *xrm_option, char *resource_name)
9766 {
9767 int connection;
9768 Display *dpy;
9769 struct terminal *terminal;
9770 struct x_display_info *dpyinfo;
9771 XrmDatabase xrdb;
9772 ptrdiff_t lim;
9773
9774 block_input ();
9775
9776 if (!x_initialized)
9777 {
9778 x_initialize ();
9779 ++x_initialized;
9780 }
9781
9782 if (! x_display_ok (SSDATA (display_name)))
9783 error ("Display %s can't be opened", SSDATA (display_name));
9784
9785 #ifdef USE_GTK
9786 {
9787 #define NUM_ARGV 10
9788 int argc;
9789 char *argv[NUM_ARGV];
9790 char **argv2 = argv;
9791 guint id;
9792
9793 if (x_initialized++ > 1)
9794 {
9795 xg_display_open (SSDATA (display_name), &dpy);
9796 }
9797 else
9798 {
9799 static char display_opt[] = "--display";
9800 static char name_opt[] = "--name";
9801
9802 for (argc = 0; argc < NUM_ARGV; ++argc)
9803 argv[argc] = 0;
9804
9805 argc = 0;
9806 argv[argc++] = initial_argv[0];
9807
9808 if (! NILP (display_name))
9809 {
9810 argv[argc++] = display_opt;
9811 argv[argc++] = SSDATA (display_name);
9812 }
9813
9814 argv[argc++] = name_opt;
9815 argv[argc++] = resource_name;
9816
9817 XSetLocaleModifiers ("");
9818
9819 /* Emacs can only handle core input events, so make sure
9820 Gtk doesn't use Xinput or Xinput2 extensions. */
9821 xputenv ("GDK_CORE_DEVICE_EVENTS=1");
9822
9823 /* Work around GLib bug that outputs a faulty warning. See
9824 https://bugzilla.gnome.org/show_bug.cgi?id=563627. */
9825 id = g_log_set_handler ("GLib", G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL
9826 | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
9827
9828 /* NULL window -> events for all windows go to our function.
9829 Call before gtk_init so Gtk+ event filters comes after our. */
9830 gdk_window_add_filter (NULL, event_handler_gdk, NULL);
9831
9832 /* gtk_init does set_locale. Fix locale before and after. */
9833 fixup_locale ();
9834 gtk_init (&argc, &argv2);
9835 fixup_locale ();
9836
9837 g_log_remove_handler ("GLib", id);
9838
9839 xg_initialize ();
9840
9841 dpy = DEFAULT_GDK_DISPLAY ();
9842
9843 #if ! GTK_CHECK_VERSION (2, 90, 0)
9844 /* Load our own gtkrc if it exists. */
9845 {
9846 const char *file = "~/.emacs.d/gtkrc";
9847 Lisp_Object s, abs_file;
9848
9849 s = build_string (file);
9850 abs_file = Fexpand_file_name (s, Qnil);
9851
9852 if (! NILP (abs_file) && !NILP (Ffile_readable_p (abs_file)))
9853 gtk_rc_parse (SSDATA (abs_file));
9854 }
9855 #endif
9856
9857 XSetErrorHandler (x_error_handler);
9858 XSetIOErrorHandler (x_io_error_quitter);
9859 }
9860 }
9861 #else /* not USE_GTK */
9862 #ifdef USE_X_TOOLKIT
9863 /* weiner@footloose.sps.mot.com reports that this causes
9864 errors with X11R5:
9865 X protocol error: BadAtom (invalid Atom parameter)
9866 on protocol request 18skiloaf.
9867 So let's not use it until R6. */
9868 #ifdef HAVE_X11XTR6
9869 XtSetLanguageProc (NULL, NULL, NULL);
9870 #endif
9871
9872 {
9873 int argc = 0;
9874 char *argv[3];
9875
9876 argv[0] = "";
9877 argc = 1;
9878 if (xrm_option)
9879 {
9880 argv[argc++] = "-xrm";
9881 argv[argc++] = xrm_option;
9882 }
9883 turn_on_atimers (0);
9884 dpy = XtOpenDisplay (Xt_app_con, SSDATA (display_name),
9885 resource_name, EMACS_CLASS,
9886 emacs_options, XtNumber (emacs_options),
9887 &argc, argv);
9888 turn_on_atimers (1);
9889
9890 #ifdef HAVE_X11XTR6
9891 /* I think this is to compensate for XtSetLanguageProc. */
9892 fixup_locale ();
9893 #endif
9894 }
9895
9896 #else /* not USE_X_TOOLKIT */
9897 XSetLocaleModifiers ("");
9898 dpy = XOpenDisplay (SSDATA (display_name));
9899 #endif /* not USE_X_TOOLKIT */
9900 #endif /* not USE_GTK*/
9901
9902 /* Detect failure. */
9903 if (dpy == 0)
9904 {
9905 unblock_input ();
9906 return 0;
9907 }
9908
9909 /* We have definitely succeeded. Record the new connection. */
9910
9911 dpyinfo = xzalloc (sizeof *dpyinfo);
9912 terminal = x_create_terminal (dpyinfo);
9913
9914 {
9915 struct x_display_info *share;
9916 Lisp_Object tail;
9917
9918 for (share = x_display_list, tail = x_display_name_list; share;
9919 share = share->next, tail = XCDR (tail))
9920 if (same_x_server (SSDATA (XCAR (XCAR (tail))),
9921 SSDATA (display_name)))
9922 break;
9923 if (share)
9924 terminal->kboard = share->terminal->kboard;
9925 else
9926 {
9927 terminal->kboard = xmalloc (sizeof *terminal->kboard);
9928 init_kboard (terminal->kboard);
9929 kset_window_system (terminal->kboard, Qx);
9930
9931 /* Add the keyboard to the list before running Lisp code (via
9932 Qvendor_specific_keysyms below), since these are not traced
9933 via terminals but only through all_kboards. */
9934 terminal->kboard->next_kboard = all_kboards;
9935 all_kboards = terminal->kboard;
9936
9937 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
9938 {
9939 char *vendor = ServerVendor (dpy);
9940
9941 /* Protect terminal from GC before removing it from the
9942 list of terminals. */
9943 struct gcpro gcpro1;
9944 Lisp_Object gcpro_term;
9945 XSETTERMINAL (gcpro_term, terminal);
9946 GCPRO1 (gcpro_term);
9947
9948 /* Temporarily hide the partially initialized terminal. */
9949 terminal_list = terminal->next_terminal;
9950 unblock_input ();
9951 kset_system_key_alist
9952 (terminal->kboard,
9953 call1 (Qvendor_specific_keysyms,
9954 vendor ? build_string (vendor) : empty_unibyte_string));
9955 block_input ();
9956 terminal->next_terminal = terminal_list;
9957 terminal_list = terminal;
9958 UNGCPRO;
9959 }
9960
9961 /* Don't let the initial kboard remain current longer than necessary.
9962 That would cause problems if a file loaded on startup tries to
9963 prompt in the mini-buffer. */
9964 if (current_kboard == initial_kboard)
9965 current_kboard = terminal->kboard;
9966 }
9967 terminal->kboard->reference_count++;
9968 }
9969
9970 /* Put this display on the chain. */
9971 dpyinfo->next = x_display_list;
9972 x_display_list = dpyinfo;
9973
9974 /* Put it on x_display_name_list as well, to keep them parallel. */
9975 x_display_name_list = Fcons (Fcons (display_name, Qnil),
9976 x_display_name_list);
9977 dpyinfo->name_list_element = XCAR (x_display_name_list);
9978
9979 dpyinfo->display = dpy;
9980
9981 /* Set the name of the terminal. */
9982 terminal->name = xlispstrdup (display_name);
9983
9984 #if 0
9985 XSetAfterFunction (x_current_display, x_trace_wire);
9986 #endif /* ! 0 */
9987
9988 lim = min (PTRDIFF_MAX, SIZE_MAX) - sizeof "@";
9989 if (lim - SBYTES (Vinvocation_name) < SBYTES (Vsystem_name))
9990 memory_full (SIZE_MAX);
9991 dpyinfo->x_id_name = xmalloc (SBYTES (Vinvocation_name)
9992 + SBYTES (Vsystem_name) + 2);
9993 strcat (strcat (strcpy (dpyinfo->x_id_name, SSDATA (Vinvocation_name)), "@"),
9994 SSDATA (Vsystem_name));
9995
9996 /* Figure out which modifier bits mean what. */
9997 x_find_modifier_meanings (dpyinfo);
9998
9999 /* Get the scroll bar cursor. */
10000 #ifdef USE_GTK
10001 /* We must create a GTK cursor, it is required for GTK widgets. */
10002 dpyinfo->xg_cursor = xg_create_default_cursor (dpyinfo->display);
10003 #endif /* USE_GTK */
10004
10005 dpyinfo->vertical_scroll_bar_cursor
10006 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
10007
10008 xrdb = x_load_resources (dpyinfo->display, xrm_option,
10009 resource_name, EMACS_CLASS);
10010 #ifdef HAVE_XRMSETDATABASE
10011 XrmSetDatabase (dpyinfo->display, xrdb);
10012 #else
10013 dpyinfo->display->db = xrdb;
10014 #endif
10015 /* Put the rdb where we can find it in a way that works on
10016 all versions. */
10017 dpyinfo->xrdb = xrdb;
10018
10019 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
10020 DefaultScreen (dpyinfo->display));
10021 select_visual (dpyinfo);
10022 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
10023 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
10024 dpyinfo->icon_bitmap_id = -1;
10025 dpyinfo->wm_type = X_WMTYPE_UNKNOWN;
10026
10027 reset_mouse_highlight (&dpyinfo->mouse_highlight);
10028
10029 /* See if we can construct pixel values from RGB values. */
10030 if (dpyinfo->visual->class == TrueColor)
10031 {
10032 get_bits_and_offset (dpyinfo->visual->red_mask,
10033 &dpyinfo->red_bits, &dpyinfo->red_offset);
10034 get_bits_and_offset (dpyinfo->visual->blue_mask,
10035 &dpyinfo->blue_bits, &dpyinfo->blue_offset);
10036 get_bits_and_offset (dpyinfo->visual->green_mask,
10037 &dpyinfo->green_bits, &dpyinfo->green_offset);
10038 }
10039
10040 /* See if a private colormap is requested. */
10041 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
10042 {
10043 if (dpyinfo->visual->class == PseudoColor)
10044 {
10045 Lisp_Object value;
10046 value = display_x_get_resource (dpyinfo,
10047 build_string ("privateColormap"),
10048 build_string ("PrivateColormap"),
10049 Qnil, Qnil);
10050 if (STRINGP (value)
10051 && (!strcmp (SSDATA (value), "true")
10052 || !strcmp (SSDATA (value), "on")))
10053 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
10054 }
10055 }
10056 else
10057 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
10058 dpyinfo->visual, AllocNone);
10059
10060 #ifdef HAVE_XFT
10061 {
10062 /* If we are using Xft, check dpi value in X resources.
10063 It is better we use it as well, since Xft will use it, as will all
10064 Gnome applications. If our real DPI is smaller or larger than the
10065 one Xft uses, our font will look smaller or larger than other
10066 for other applications, even if it is the same font name (monospace-10
10067 for example). */
10068 char *v = XGetDefault (dpyinfo->display, "Xft", "dpi");
10069 double d;
10070 if (v != NULL && sscanf (v, "%lf", &d) == 1)
10071 dpyinfo->resy = dpyinfo->resx = d;
10072 }
10073 #endif
10074
10075 if (dpyinfo->resy < 1)
10076 {
10077 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
10078 double pixels = DisplayHeight (dpyinfo->display, screen_number);
10079 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
10080 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
10081 dpyinfo->resy = (mm < 1) ? 100 : pixels * 25.4 / mm;
10082 pixels = DisplayWidth (dpyinfo->display, screen_number);
10083 mm = DisplayWidthMM (dpyinfo->display, screen_number);
10084 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
10085 dpyinfo->resx = (mm < 1) ? 100 : pixels * 25.4 / mm;
10086 }
10087
10088 {
10089 static const struct
10090 {
10091 const char *name;
10092 int offset;
10093 } atom_refs[] = {
10094 #define ATOM_REFS_INIT(string, member) \
10095 { string, offsetof (struct x_display_info, member) },
10096 ATOM_REFS_INIT ("WM_PROTOCOLS", Xatom_wm_protocols)
10097 ATOM_REFS_INIT ("WM_TAKE_FOCUS", Xatom_wm_take_focus)
10098 ATOM_REFS_INIT ("WM_SAVE_YOURSELF", Xatom_wm_save_yourself)
10099 ATOM_REFS_INIT ("WM_DELETE_WINDOW", Xatom_wm_delete_window)
10100 ATOM_REFS_INIT ("WM_CHANGE_STATE", Xatom_wm_change_state)
10101 ATOM_REFS_INIT ("WM_CONFIGURE_DENIED", Xatom_wm_configure_denied)
10102 ATOM_REFS_INIT ("WM_MOVED", Xatom_wm_window_moved)
10103 ATOM_REFS_INIT ("WM_CLIENT_LEADER", Xatom_wm_client_leader)
10104 ATOM_REFS_INIT ("Editres", Xatom_editres)
10105 ATOM_REFS_INIT ("CLIPBOARD", Xatom_CLIPBOARD)
10106 ATOM_REFS_INIT ("TIMESTAMP", Xatom_TIMESTAMP)
10107 ATOM_REFS_INIT ("TEXT", Xatom_TEXT)
10108 ATOM_REFS_INIT ("COMPOUND_TEXT", Xatom_COMPOUND_TEXT)
10109 ATOM_REFS_INIT ("UTF8_STRING", Xatom_UTF8_STRING)
10110 ATOM_REFS_INIT ("DELETE", Xatom_DELETE)
10111 ATOM_REFS_INIT ("MULTIPLE", Xatom_MULTIPLE)
10112 ATOM_REFS_INIT ("INCR", Xatom_INCR)
10113 ATOM_REFS_INIT ("_EMACS_TMP_", Xatom_EMACS_TMP)
10114 ATOM_REFS_INIT ("TARGETS", Xatom_TARGETS)
10115 ATOM_REFS_INIT ("NULL", Xatom_NULL)
10116 ATOM_REFS_INIT ("ATOM", Xatom_ATOM)
10117 ATOM_REFS_INIT ("ATOM_PAIR", Xatom_ATOM_PAIR)
10118 ATOM_REFS_INIT ("CLIPBOARD_MANAGER", Xatom_CLIPBOARD_MANAGER)
10119 ATOM_REFS_INIT ("_XEMBED_INFO", Xatom_XEMBED_INFO)
10120 /* For properties of font. */
10121 ATOM_REFS_INIT ("PIXEL_SIZE", Xatom_PIXEL_SIZE)
10122 ATOM_REFS_INIT ("AVERAGE_WIDTH", Xatom_AVERAGE_WIDTH)
10123 ATOM_REFS_INIT ("_MULE_BASELINE_OFFSET", Xatom_MULE_BASELINE_OFFSET)
10124 ATOM_REFS_INIT ("_MULE_RELATIVE_COMPOSE", Xatom_MULE_RELATIVE_COMPOSE)
10125 ATOM_REFS_INIT ("_MULE_DEFAULT_ASCENT", Xatom_MULE_DEFAULT_ASCENT)
10126 /* Ghostscript support. */
10127 ATOM_REFS_INIT ("DONE", Xatom_DONE)
10128 ATOM_REFS_INIT ("PAGE", Xatom_PAGE)
10129 ATOM_REFS_INIT ("SCROLLBAR", Xatom_Scrollbar)
10130 ATOM_REFS_INIT ("_XEMBED", Xatom_XEMBED)
10131 /* EWMH */
10132 ATOM_REFS_INIT ("_NET_WM_STATE", Xatom_net_wm_state)
10133 ATOM_REFS_INIT ("_NET_WM_STATE_FULLSCREEN", Xatom_net_wm_state_fullscreen)
10134 ATOM_REFS_INIT ("_NET_WM_STATE_MAXIMIZED_HORZ",
10135 Xatom_net_wm_state_maximized_horz)
10136 ATOM_REFS_INIT ("_NET_WM_STATE_MAXIMIZED_VERT",
10137 Xatom_net_wm_state_maximized_vert)
10138 ATOM_REFS_INIT ("_NET_WM_STATE_STICKY", Xatom_net_wm_state_sticky)
10139 ATOM_REFS_INIT ("_NET_WM_STATE_HIDDEN", Xatom_net_wm_state_hidden)
10140 ATOM_REFS_INIT ("_NET_WM_WINDOW_TYPE", Xatom_net_window_type)
10141 ATOM_REFS_INIT ("_NET_WM_WINDOW_TYPE_TOOLTIP",
10142 Xatom_net_window_type_tooltip)
10143 ATOM_REFS_INIT ("_NET_WM_ICON_NAME", Xatom_net_wm_icon_name)
10144 ATOM_REFS_INIT ("_NET_WM_NAME", Xatom_net_wm_name)
10145 ATOM_REFS_INIT ("_NET_SUPPORTED", Xatom_net_supported)
10146 ATOM_REFS_INIT ("_NET_SUPPORTING_WM_CHECK", Xatom_net_supporting_wm_check)
10147 ATOM_REFS_INIT ("_NET_WM_WINDOW_OPACITY", Xatom_net_wm_window_opacity)
10148 ATOM_REFS_INIT ("_NET_ACTIVE_WINDOW", Xatom_net_active_window)
10149 ATOM_REFS_INIT ("_NET_FRAME_EXTENTS", Xatom_net_frame_extents)
10150 ATOM_REFS_INIT ("_NET_CURRENT_DESKTOP", Xatom_net_current_desktop)
10151 ATOM_REFS_INIT ("_NET_WORKAREA", Xatom_net_workarea)
10152 /* Session management */
10153 ATOM_REFS_INIT ("SM_CLIENT_ID", Xatom_SM_CLIENT_ID)
10154 ATOM_REFS_INIT ("_XSETTINGS_SETTINGS", Xatom_xsettings_prop)
10155 ATOM_REFS_INIT ("MANAGER", Xatom_xsettings_mgr)
10156 };
10157
10158 int i;
10159 const int atom_count = sizeof (atom_refs) / sizeof (atom_refs[0]);
10160 /* 1 for _XSETTINGS_SN */
10161 const int total_atom_count = 1 + atom_count;
10162 Atom *atoms_return = xmalloc (total_atom_count * sizeof *atoms_return);
10163 char **atom_names = xmalloc (total_atom_count * sizeof *atom_names);
10164 static char const xsettings_fmt[] = "_XSETTINGS_S%d";
10165 char xsettings_atom_name[sizeof xsettings_fmt - 2
10166 + INT_STRLEN_BOUND (int)];
10167
10168 for (i = 0; i < atom_count; i++)
10169 atom_names[i] = (char *) atom_refs[i].name;
10170
10171 /* Build _XSETTINGS_SN atom name */
10172 sprintf (xsettings_atom_name, xsettings_fmt,
10173 XScreenNumberOfScreen (dpyinfo->screen));
10174 atom_names[i] = xsettings_atom_name;
10175
10176 XInternAtoms (dpyinfo->display, atom_names, total_atom_count,
10177 False, atoms_return);
10178
10179 for (i = 0; i < atom_count; i++)
10180 *(Atom *) ((char *) dpyinfo + atom_refs[i].offset) = atoms_return[i];
10181
10182 /* Manual copy of last atom */
10183 dpyinfo->Xatom_xsettings_sel = atoms_return[i];
10184
10185 xfree (atom_names);
10186 xfree (atoms_return);
10187 }
10188
10189 dpyinfo->x_dnd_atoms_size = 8;
10190 dpyinfo->x_dnd_atoms = xmalloc (sizeof *dpyinfo->x_dnd_atoms
10191 * dpyinfo->x_dnd_atoms_size);
10192 dpyinfo->gray
10193 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
10194 gray_bits, gray_width, gray_height,
10195 1, 0, 1);
10196
10197 #ifdef HAVE_X_I18N
10198 xim_initialize (dpyinfo, resource_name);
10199 #endif
10200
10201 xsettings_initialize (dpyinfo);
10202
10203 connection = ConnectionNumber (dpyinfo->display);
10204
10205 /* This is only needed for distinguishing keyboard and process input. */
10206 if (connection != 0)
10207 add_keyboard_wait_descriptor (connection);
10208
10209 #ifdef F_SETOWN
10210 fcntl (connection, F_SETOWN, getpid ());
10211 #endif /* ! defined (F_SETOWN) */
10212
10213 if (interrupt_input)
10214 init_sigio (connection);
10215
10216 #ifdef USE_LUCID
10217 {
10218 XrmValue d, fr, to;
10219 Font font;
10220
10221 dpy = dpyinfo->display;
10222 d.addr = (XPointer)&dpy;
10223 d.size = sizeof (Display *);
10224 fr.addr = XtDefaultFont;
10225 fr.size = sizeof (XtDefaultFont);
10226 to.size = sizeof (Font *);
10227 to.addr = (XPointer)&font;
10228 x_catch_errors (dpy);
10229 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
10230 emacs_abort ();
10231 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
10232 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
10233 x_uncatch_errors ();
10234 }
10235 #endif
10236
10237 /* See if we should run in synchronous mode. This is useful
10238 for debugging X code. */
10239 {
10240 Lisp_Object value;
10241 value = display_x_get_resource (dpyinfo,
10242 build_string ("synchronous"),
10243 build_string ("Synchronous"),
10244 Qnil, Qnil);
10245 if (STRINGP (value)
10246 && (!strcmp (SSDATA (value), "true")
10247 || !strcmp (SSDATA (value), "on")))
10248 XSynchronize (dpyinfo->display, True);
10249 }
10250
10251 {
10252 Lisp_Object value;
10253 value = display_x_get_resource (dpyinfo,
10254 build_string ("useXIM"),
10255 build_string ("UseXIM"),
10256 Qnil, Qnil);
10257 #ifdef USE_XIM
10258 if (STRINGP (value)
10259 && (!strcmp (SSDATA (value), "false")
10260 || !strcmp (SSDATA (value), "off")))
10261 use_xim = 0;
10262 #else
10263 if (STRINGP (value)
10264 && (!strcmp (SSDATA (value), "true")
10265 || !strcmp (SSDATA (value), "on")))
10266 use_xim = 1;
10267 #endif
10268 }
10269
10270 #ifdef HAVE_X_SM
10271 /* Only do this for the very first display in the Emacs session.
10272 Ignore X session management when Emacs was first started on a
10273 tty. */
10274 if (terminal->id == 1)
10275 x_session_initialize (dpyinfo);
10276 #endif
10277
10278 unblock_input ();
10279
10280 return dpyinfo;
10281 }
10282 \f
10283 /* Get rid of display DPYINFO, deleting all frames on it,
10284 and without sending any more commands to the X server. */
10285
10286 static void
10287 x_delete_display (struct x_display_info *dpyinfo)
10288 {
10289 struct terminal *t;
10290
10291 /* Close all frames and delete the generic struct terminal for this
10292 X display. */
10293 for (t = terminal_list; t; t = t->next_terminal)
10294 if (t->type == output_x_window && t->display_info.x == dpyinfo)
10295 {
10296 #ifdef HAVE_X_SM
10297 /* Close X session management when we close its display. */
10298 if (t->id == 1 && x_session_have_connection ())
10299 x_session_close ();
10300 #endif
10301 delete_terminal (t);
10302 break;
10303 }
10304
10305 /* Discard this display from x_display_name_list and x_display_list.
10306 We can't use Fdelq because that can quit. */
10307 if (! NILP (x_display_name_list)
10308 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
10309 x_display_name_list = XCDR (x_display_name_list);
10310 else
10311 {
10312 Lisp_Object tail;
10313
10314 tail = x_display_name_list;
10315 while (CONSP (tail) && CONSP (XCDR (tail)))
10316 {
10317 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
10318 {
10319 XSETCDR (tail, XCDR (XCDR (tail)));
10320 break;
10321 }
10322 tail = XCDR (tail);
10323 }
10324 }
10325
10326 if (next_noop_dpyinfo == dpyinfo)
10327 next_noop_dpyinfo = dpyinfo->next;
10328
10329 if (x_display_list == dpyinfo)
10330 x_display_list = dpyinfo->next;
10331 else
10332 {
10333 struct x_display_info *tail;
10334
10335 for (tail = x_display_list; tail; tail = tail->next)
10336 if (tail->next == dpyinfo)
10337 tail->next = tail->next->next;
10338 }
10339
10340 xfree (dpyinfo->x_id_name);
10341 xfree (dpyinfo->x_dnd_atoms);
10342 xfree (dpyinfo->color_cells);
10343 xfree (dpyinfo);
10344 }
10345
10346 #ifdef USE_X_TOOLKIT
10347
10348 /* Atimer callback function for TIMER. Called every 0.1s to process
10349 Xt timeouts, if needed. We must avoid calling XtAppPending as
10350 much as possible because that function does an implicit XFlush
10351 that slows us down. */
10352
10353 static void
10354 x_process_timeouts (struct atimer *timer)
10355 {
10356 block_input ();
10357 x_timeout_atimer_activated_flag = 0;
10358 if (toolkit_scroll_bar_interaction || popup_activated ())
10359 {
10360 while (XtAppPending (Xt_app_con) & XtIMTimer)
10361 XtAppProcessEvent (Xt_app_con, XtIMTimer);
10362 /* Reactivate the atimer for next time. */
10363 x_activate_timeout_atimer ();
10364 }
10365 unblock_input ();
10366 }
10367
10368 /* Install an asynchronous timer that processes Xt timeout events
10369 every 0.1s as long as either `toolkit_scroll_bar_interaction' or
10370 `popup_activated_flag' (in xmenu.c) is set. Make sure to call this
10371 function whenever these variables are set. This is necessary
10372 because some widget sets use timeouts internally, for example the
10373 LessTif menu bar, or the Xaw3d scroll bar. When Xt timeouts aren't
10374 processed, these widgets don't behave normally. */
10375
10376 void
10377 x_activate_timeout_atimer (void)
10378 {
10379 block_input ();
10380 if (!x_timeout_atimer_activated_flag)
10381 {
10382 struct timespec interval = make_timespec (0, 100 * 1000 * 1000);
10383 start_atimer (ATIMER_RELATIVE, interval, x_process_timeouts, 0);
10384 x_timeout_atimer_activated_flag = 1;
10385 }
10386 unblock_input ();
10387 }
10388
10389 #endif /* USE_X_TOOLKIT */
10390
10391 \f
10392 /* Set up use of X before we make the first connection. */
10393
10394 static struct redisplay_interface x_redisplay_interface =
10395 {
10396 x_frame_parm_handlers,
10397 x_produce_glyphs,
10398 x_write_glyphs,
10399 x_insert_glyphs,
10400 x_clear_end_of_line,
10401 x_scroll_run,
10402 x_after_update_window_line,
10403 x_update_window_begin,
10404 x_update_window_end,
10405 x_flush,
10406 x_clear_window_mouse_face,
10407 x_get_glyph_overhangs,
10408 x_fix_overlapping_area,
10409 x_draw_fringe_bitmap,
10410 0, /* define_fringe_bitmap */
10411 0, /* destroy_fringe_bitmap */
10412 x_compute_glyph_string_overhangs,
10413 x_draw_glyph_string,
10414 x_define_frame_cursor,
10415 x_clear_frame_area,
10416 x_draw_window_cursor,
10417 x_draw_vertical_window_border,
10418 x_shift_glyphs_for_insert
10419 };
10420
10421
10422 /* This function is called when the last frame on a display is deleted. */
10423 void
10424 x_delete_terminal (struct terminal *terminal)
10425 {
10426 struct x_display_info *dpyinfo = terminal->display_info.x;
10427 int connection = -1;
10428
10429 /* Protect against recursive calls. delete_frame in
10430 delete_terminal calls us back when it deletes our last frame. */
10431 if (!terminal->name)
10432 return;
10433
10434 block_input ();
10435 #ifdef HAVE_X_I18N
10436 /* We must close our connection to the XIM server before closing the
10437 X display. */
10438 if (dpyinfo->xim)
10439 xim_close_dpy (dpyinfo);
10440 #endif
10441
10442 /* If called from x_connection_closed, the display may already be closed
10443 and dpyinfo->display was set to 0 to indicate that. */
10444 if (dpyinfo->display)
10445 {
10446 connection = ConnectionNumber (dpyinfo->display);
10447
10448 x_destroy_all_bitmaps (dpyinfo);
10449 XSetCloseDownMode (dpyinfo->display, DestroyAll);
10450
10451 /* Whether or not XCloseDisplay destroys the associated resource
10452 database depends on the version of libX11. To avoid both
10453 crash and memory leak, we dissociate the database from the
10454 display and then destroy dpyinfo->xrdb ourselves.
10455
10456 Unfortunately, the above strategy does not work in some
10457 situations due to a bug in newer versions of libX11: because
10458 XrmSetDatabase doesn't clear the flag XlibDisplayDfltRMDB if
10459 dpy->db is NULL, XCloseDisplay destroys the associated
10460 database whereas it has not been created by XGetDefault
10461 (Bug#21974 in freedesktop.org Bugzilla). As a workaround, we
10462 don't destroy the database here in order to avoid the crash
10463 in the above situations for now, though that may cause memory
10464 leaks in other situations. */
10465 #if 0
10466 #ifdef HAVE_XRMSETDATABASE
10467 XrmSetDatabase (dpyinfo->display, NULL);
10468 #else
10469 dpyinfo->display->db = NULL;
10470 #endif
10471 /* We used to call XrmDestroyDatabase from x_delete_display, but
10472 some older versions of libX11 crash if we call it after
10473 closing all the displays. */
10474 XrmDestroyDatabase (dpyinfo->xrdb);
10475 #endif
10476
10477 #ifdef USE_GTK
10478 xg_display_close (dpyinfo->display);
10479 #else
10480 #ifdef USE_X_TOOLKIT
10481 XtCloseDisplay (dpyinfo->display);
10482 #else
10483 XCloseDisplay (dpyinfo->display);
10484 #endif
10485 #endif /* ! USE_GTK */
10486 }
10487
10488 /* No more input on this descriptor. */
10489 if (connection != -1)
10490 delete_keyboard_wait_descriptor (connection);
10491
10492 /* Mark as dead. */
10493 dpyinfo->display = NULL;
10494 x_delete_display (dpyinfo);
10495 unblock_input ();
10496 }
10497
10498 /* Create a struct terminal, initialize it with the X11 specific
10499 functions and make DISPLAY->TERMINAL point to it. */
10500
10501 static struct terminal *
10502 x_create_terminal (struct x_display_info *dpyinfo)
10503 {
10504 struct terminal *terminal;
10505
10506 terminal = create_terminal ();
10507
10508 terminal->type = output_x_window;
10509 terminal->display_info.x = dpyinfo;
10510 dpyinfo->terminal = terminal;
10511
10512 /* kboard is initialized in x_term_init. */
10513
10514 terminal->clear_frame_hook = x_clear_frame;
10515 terminal->ins_del_lines_hook = x_ins_del_lines;
10516 terminal->delete_glyphs_hook = x_delete_glyphs;
10517 terminal->ring_bell_hook = XTring_bell;
10518 terminal->toggle_invisible_pointer_hook = XTtoggle_invisible_pointer;
10519 terminal->reset_terminal_modes_hook = NULL;
10520 terminal->set_terminal_modes_hook = NULL;
10521 terminal->update_begin_hook = x_update_begin;
10522 terminal->update_end_hook = x_update_end;
10523 terminal->set_terminal_window_hook = NULL;
10524 terminal->read_socket_hook = XTread_socket;
10525 terminal->frame_up_to_date_hook = XTframe_up_to_date;
10526 terminal->mouse_position_hook = XTmouse_position;
10527 terminal->frame_rehighlight_hook = XTframe_rehighlight;
10528 terminal->frame_raise_lower_hook = XTframe_raise_lower;
10529 terminal->fullscreen_hook = XTfullscreen_hook;
10530 terminal->set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
10531 terminal->condemn_scroll_bars_hook = XTcondemn_scroll_bars;
10532 terminal->redeem_scroll_bar_hook = XTredeem_scroll_bar;
10533 terminal->judge_scroll_bars_hook = XTjudge_scroll_bars;
10534
10535 terminal->delete_frame_hook = x_destroy_window;
10536 terminal->delete_terminal_hook = x_delete_terminal;
10537
10538 terminal->rif = &x_redisplay_interface;
10539 terminal->scroll_region_ok = 1; /* We'll scroll partial frames. */
10540 terminal->char_ins_del_ok = 1;
10541 terminal->line_ins_del_ok = 1; /* We'll just blt 'em. */
10542 terminal->fast_clear_end_of_line = 1; /* X does this well. */
10543 terminal->memory_below_frame = 0; /* We don't remember what scrolls
10544 off the bottom. */
10545
10546 return terminal;
10547 }
10548
10549 void
10550 x_initialize (void)
10551 {
10552 baud_rate = 19200;
10553
10554 x_noop_count = 0;
10555 last_tool_bar_item = -1;
10556 any_help_event_p = 0;
10557 ignore_next_mouse_click_timeout = 0;
10558
10559 #ifdef USE_GTK
10560 current_count = -1;
10561 #endif
10562
10563 /* Try to use interrupt input; if we can't, then start polling. */
10564 Fset_input_interrupt_mode (Qt);
10565
10566 #ifdef USE_X_TOOLKIT
10567 XtToolkitInitialize ();
10568
10569 Xt_app_con = XtCreateApplicationContext ();
10570
10571 /* Register a converter from strings to pixels, which uses
10572 Emacs' color allocation infrastructure. */
10573 XtAppSetTypeConverter (Xt_app_con,
10574 XtRString, XtRPixel, cvt_string_to_pixel,
10575 cvt_string_to_pixel_args,
10576 XtNumber (cvt_string_to_pixel_args),
10577 XtCacheByDisplay, cvt_pixel_dtor);
10578
10579 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
10580 #endif
10581
10582 #ifdef USE_TOOLKIT_SCROLL_BARS
10583 #ifndef USE_GTK
10584 xaw3d_arrow_scroll = False;
10585 xaw3d_pick_top = True;
10586 #endif
10587 #endif
10588
10589 /* Note that there is no real way portable across R3/R4 to get the
10590 original error handler. */
10591 XSetErrorHandler (x_error_handler);
10592 XSetIOErrorHandler (x_io_error_quitter);
10593 }
10594
10595
10596 void
10597 syms_of_xterm (void)
10598 {
10599 x_error_message = NULL;
10600
10601 staticpro (&x_display_name_list);
10602 x_display_name_list = Qnil;
10603
10604 DEFSYM (Qvendor_specific_keysyms, "vendor-specific-keysyms");
10605 DEFSYM (Qlatin_1, "latin-1");
10606
10607 #ifdef USE_GTK
10608 xg_default_icon_file = build_pure_c_string ("icons/hicolor/scalable/apps/emacs.svg");
10609 staticpro (&xg_default_icon_file);
10610
10611 DEFSYM (Qx_gtk_map_stock, "x-gtk-map-stock");
10612 #endif
10613
10614 DEFVAR_BOOL ("x-use-underline-position-properties",
10615 x_use_underline_position_properties,
10616 doc: /* Non-nil means make use of UNDERLINE_POSITION font properties.
10617 A value of nil means ignore them. If you encounter fonts with bogus
10618 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
10619 to 4.1, set this to nil. You can also use `underline-minimum-offset'
10620 to override the font's UNDERLINE_POSITION for small font display
10621 sizes. */);
10622 x_use_underline_position_properties = 1;
10623
10624 DEFVAR_BOOL ("x-underline-at-descent-line",
10625 x_underline_at_descent_line,
10626 doc: /* Non-nil means to draw the underline at the same place as the descent line.
10627 A value of nil means to draw the underline according to the value of the
10628 variable `x-use-underline-position-properties', which is usually at the
10629 baseline level. The default value is nil. */);
10630 x_underline_at_descent_line = 0;
10631
10632 DEFVAR_BOOL ("x-mouse-click-focus-ignore-position",
10633 x_mouse_click_focus_ignore_position,
10634 doc: /* Non-nil means that a mouse click to focus a frame does not move point.
10635 This variable is only used when the window manager requires that you
10636 click on a frame to select it (give it focus). In that case, a value
10637 of nil, means that the selected window and cursor position changes to
10638 reflect the mouse click position, while a non-nil value means that the
10639 selected window or cursor position is preserved. */);
10640 x_mouse_click_focus_ignore_position = 0;
10641
10642 DEFVAR_LISP ("x-toolkit-scroll-bars", Vx_toolkit_scroll_bars,
10643 doc: /* Which toolkit scroll bars Emacs uses, if any.
10644 A value of nil means Emacs doesn't use toolkit scroll bars.
10645 With the X Window system, the value is a symbol describing the
10646 X toolkit. Possible values are: gtk, motif, xaw, or xaw3d.
10647 With MS Windows or Nextstep, the value is t. */);
10648 #ifdef USE_TOOLKIT_SCROLL_BARS
10649 #ifdef USE_MOTIF
10650 Vx_toolkit_scroll_bars = intern_c_string ("motif");
10651 #elif defined HAVE_XAW3D
10652 Vx_toolkit_scroll_bars = intern_c_string ("xaw3d");
10653 #elif USE_GTK
10654 Vx_toolkit_scroll_bars = intern_c_string ("gtk");
10655 #else
10656 Vx_toolkit_scroll_bars = intern_c_string ("xaw");
10657 #endif
10658 #else
10659 Vx_toolkit_scroll_bars = Qnil;
10660 #endif
10661
10662 Qmodifier_value = intern_c_string ("modifier-value");
10663 Qalt = intern_c_string ("alt");
10664 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
10665 Qhyper = intern_c_string ("hyper");
10666 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
10667 Qmeta = intern_c_string ("meta");
10668 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
10669 Qsuper = intern_c_string ("super");
10670 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
10671
10672 DEFVAR_LISP ("x-alt-keysym", Vx_alt_keysym,
10673 doc: /* Which keys Emacs uses for the alt modifier.
10674 This should be one of the symbols `alt', `hyper', `meta', `super'.
10675 For example, `alt' means use the Alt_L and Alt_R keysyms. The default
10676 is nil, which is the same as `alt'. */);
10677 Vx_alt_keysym = Qnil;
10678
10679 DEFVAR_LISP ("x-hyper-keysym", Vx_hyper_keysym,
10680 doc: /* Which keys Emacs uses for the hyper modifier.
10681 This should be one of the symbols `alt', `hyper', `meta', `super'.
10682 For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
10683 default is nil, which is the same as `hyper'. */);
10684 Vx_hyper_keysym = Qnil;
10685
10686 DEFVAR_LISP ("x-meta-keysym", Vx_meta_keysym,
10687 doc: /* Which keys Emacs uses for the meta modifier.
10688 This should be one of the symbols `alt', `hyper', `meta', `super'.
10689 For example, `meta' means use the Meta_L and Meta_R keysyms. The
10690 default is nil, which is the same as `meta'. */);
10691 Vx_meta_keysym = Qnil;
10692
10693 DEFVAR_LISP ("x-super-keysym", Vx_super_keysym,
10694 doc: /* Which keys Emacs uses for the super modifier.
10695 This should be one of the symbols `alt', `hyper', `meta', `super'.
10696 For example, `super' means use the Super_L and Super_R keysyms. The
10697 default is nil, which is the same as `super'. */);
10698 Vx_super_keysym = Qnil;
10699
10700 DEFVAR_LISP ("x-keysym-table", Vx_keysym_table,
10701 doc: /* Hash table of character codes indexed by X keysym codes. */);
10702 Vx_keysym_table = make_hash_table (hashtest_eql, make_number (900),
10703 make_float (DEFAULT_REHASH_SIZE),
10704 make_float (DEFAULT_REHASH_THRESHOLD),
10705 Qnil);
10706 }