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