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