]> code.delx.au - gnu-emacs/blob - src/xterm.c
Don't access display after i/o error (Bug#19147).
[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 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
2900 f->output_data.x->normal_gc,
2901 x, y, width, height,
2902 x + shift_by, y);
2903 }
2904
2905 /* Delete N glyphs at the nominal cursor position. Not implemented
2906 for X frames. */
2907
2908 static void
2909 x_delete_glyphs (struct frame *f, register int n)
2910 {
2911 emacs_abort ();
2912 }
2913
2914
2915 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
2916 If they are <= 0, this is probably an error. */
2917
2918 void
2919 x_clear_area (Display *dpy, Window window, int x, int y, int width, int height)
2920 {
2921 eassert (width > 0 && height > 0);
2922 XClearArea (dpy, window, x, y, width, height, False);
2923 }
2924
2925
2926 /* Clear an entire frame. */
2927
2928 static void
2929 x_clear_frame (struct frame *f)
2930 {
2931 /* Clearing the frame will erase any cursor, so mark them all as no
2932 longer visible. */
2933 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
2934
2935 block_input ();
2936
2937 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
2938
2939 /* We have to clear the scroll bars. If we have changed colors or
2940 something like that, then they should be notified. */
2941 x_scroll_bar_clear (f);
2942
2943 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
2944 /* Make sure scroll bars are redrawn. As they aren't redrawn by
2945 redisplay, do it here. */
2946 if (FRAME_GTK_WIDGET (f))
2947 gtk_widget_queue_draw (FRAME_GTK_WIDGET (f));
2948 #endif
2949
2950 XFlush (FRAME_X_DISPLAY (f));
2951
2952 unblock_input ();
2953 }
2954
2955 /* RIF: Show hourglass cursor on frame F. */
2956
2957 static void
2958 x_show_hourglass (struct frame *f)
2959 {
2960 Display *dpy = FRAME_X_DISPLAY (f);
2961
2962 if (dpy)
2963 {
2964 struct x_output *x = FRAME_X_OUTPUT (f);
2965 #ifdef USE_X_TOOLKIT
2966 if (x->widget)
2967 #else
2968 if (FRAME_OUTER_WINDOW (f))
2969 #endif
2970 {
2971 x->hourglass_p = true;
2972
2973 if (!x->hourglass_window)
2974 {
2975 unsigned long mask = CWCursor;
2976 XSetWindowAttributes attrs;
2977 #ifdef USE_GTK
2978 Window parent = FRAME_X_WINDOW (f);
2979 #else
2980 Window parent = FRAME_OUTER_WINDOW (f);
2981 #endif
2982 attrs.cursor = x->hourglass_cursor;
2983
2984 x->hourglass_window = XCreateWindow
2985 (dpy, parent, 0, 0, 32000, 32000, 0, 0,
2986 InputOnly, CopyFromParent, mask, &attrs);
2987 }
2988
2989 XMapRaised (dpy, x->hourglass_window);
2990 XFlush (dpy);
2991 }
2992 }
2993 }
2994
2995 /* RIF: Cancel hourglass cursor on frame F. */
2996
2997 static void
2998 x_hide_hourglass (struct frame *f)
2999 {
3000 struct x_output *x = FRAME_X_OUTPUT (f);
3001
3002 /* Watch out for newly created frames. */
3003 if (x->hourglass_window)
3004 {
3005 XUnmapWindow (FRAME_X_DISPLAY (f), x->hourglass_window);
3006 /* Sync here because XTread_socket looks at the
3007 hourglass_p flag that is reset to zero below. */
3008 XSync (FRAME_X_DISPLAY (f), False);
3009 x->hourglass_p = false;
3010 }
3011 }
3012
3013 /* Invert the middle quarter of the frame for .15 sec. */
3014
3015 static void
3016 XTflash (struct frame *f)
3017 {
3018 block_input ();
3019
3020 {
3021 #ifdef USE_GTK
3022 /* Use Gdk routines to draw. This way, we won't draw over scroll bars
3023 when the scroll bars and the edit widget share the same X window. */
3024 GdkWindow *window = gtk_widget_get_window (FRAME_GTK_WIDGET (f));
3025 #ifdef HAVE_GTK3
3026 cairo_t *cr = gdk_cairo_create (window);
3027 cairo_set_source_rgb (cr, 1, 1, 1);
3028 cairo_set_operator (cr, CAIRO_OPERATOR_DIFFERENCE);
3029 #define XFillRectangle(d, win, gc, x, y, w, h) \
3030 do { \
3031 cairo_rectangle (cr, x, y, w, h); \
3032 cairo_fill (cr); \
3033 } \
3034 while (false)
3035 #else /* ! HAVE_GTK3 */
3036 GdkGCValues vals;
3037 GdkGC *gc;
3038 vals.foreground.pixel = (FRAME_FOREGROUND_PIXEL (f)
3039 ^ FRAME_BACKGROUND_PIXEL (f));
3040 vals.function = GDK_XOR;
3041 gc = gdk_gc_new_with_values (window,
3042 &vals, GDK_GC_FUNCTION | GDK_GC_FOREGROUND);
3043 #define XFillRectangle(d, win, gc, x, y, w, h) \
3044 gdk_draw_rectangle (window, gc, true, x, y, w, h)
3045 #endif /* ! HAVE_GTK3 */
3046 #else /* ! USE_GTK */
3047 GC gc;
3048
3049 /* Create a GC that will use the GXxor function to flip foreground
3050 pixels into background pixels. */
3051 {
3052 XGCValues values;
3053
3054 values.function = GXxor;
3055 values.foreground = (FRAME_FOREGROUND_PIXEL (f)
3056 ^ FRAME_BACKGROUND_PIXEL (f));
3057
3058 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3059 GCFunction | GCForeground, &values);
3060 }
3061 #endif
3062 {
3063 /* Get the height not including a menu bar widget. */
3064 int height = FRAME_PIXEL_HEIGHT (f);
3065 /* Height of each line to flash. */
3066 int flash_height = FRAME_LINE_HEIGHT (f);
3067 /* These will be the left and right margins of the rectangles. */
3068 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
3069 int flash_right = FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
3070 int width = flash_right - flash_left;
3071
3072 /* If window is tall, flash top and bottom line. */
3073 if (height > 3 * FRAME_LINE_HEIGHT (f))
3074 {
3075 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3076 flash_left,
3077 (FRAME_INTERNAL_BORDER_WIDTH (f)
3078 + FRAME_TOP_MARGIN_HEIGHT (f)),
3079 width, flash_height);
3080 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3081 flash_left,
3082 (height - flash_height
3083 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3084 width, flash_height);
3085
3086 }
3087 else
3088 /* If it is short, flash it all. */
3089 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3090 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3091 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3092
3093 x_flush (f);
3094
3095 {
3096 struct timespec delay = make_timespec (0, 150 * 1000 * 1000);
3097 struct timespec wakeup = timespec_add (current_timespec (), delay);
3098
3099 /* Keep waiting until past the time wakeup or any input gets
3100 available. */
3101 while (! detect_input_pending ())
3102 {
3103 struct timespec current = current_timespec ();
3104 struct timespec timeout;
3105
3106 /* Break if result would not be positive. */
3107 if (timespec_cmp (wakeup, current) <= 0)
3108 break;
3109
3110 /* How long `select' should wait. */
3111 timeout = make_timespec (0, 10 * 1000 * 1000);
3112
3113 /* Try to wait that long--but we might wake up sooner. */
3114 pselect (0, NULL, NULL, NULL, &timeout, NULL);
3115 }
3116 }
3117
3118 /* If window is tall, flash top and bottom line. */
3119 if (height > 3 * FRAME_LINE_HEIGHT (f))
3120 {
3121 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3122 flash_left,
3123 (FRAME_INTERNAL_BORDER_WIDTH (f)
3124 + FRAME_TOP_MARGIN_HEIGHT (f)),
3125 width, flash_height);
3126 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3127 flash_left,
3128 (height - flash_height
3129 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3130 width, flash_height);
3131 }
3132 else
3133 /* If it is short, flash it all. */
3134 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3135 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3136 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3137
3138 #ifdef USE_GTK
3139 #ifdef HAVE_GTK3
3140 cairo_destroy (cr);
3141 #else
3142 g_object_unref (G_OBJECT (gc));
3143 #endif
3144 #undef XFillRectangle
3145 #else
3146 XFreeGC (FRAME_X_DISPLAY (f), gc);
3147 #endif
3148 x_flush (f);
3149 }
3150 }
3151
3152 unblock_input ();
3153 }
3154
3155
3156 static void
3157 XTtoggle_invisible_pointer (struct frame *f, bool invisible)
3158 {
3159 block_input ();
3160 FRAME_DISPLAY_INFO (f)->toggle_visible_pointer (f, invisible);
3161 unblock_input ();
3162 }
3163
3164
3165 /* Make audible bell. */
3166
3167 static void
3168 XTring_bell (struct frame *f)
3169 {
3170 if (FRAME_X_DISPLAY (f))
3171 {
3172 if (visible_bell)
3173 XTflash (f);
3174 else
3175 {
3176 block_input ();
3177 #ifdef HAVE_XKB
3178 XkbBell (FRAME_X_DISPLAY (f), None, 0, None);
3179 #else
3180 XBell (FRAME_X_DISPLAY (f), 0);
3181 #endif
3182 XFlush (FRAME_X_DISPLAY (f));
3183 unblock_input ();
3184 }
3185 }
3186 }
3187
3188 /***********************************************************************
3189 Line Dance
3190 ***********************************************************************/
3191
3192 /* Perform an insert-lines or delete-lines operation, inserting N
3193 lines or deleting -N lines at vertical position VPOS. */
3194
3195 static void
3196 x_ins_del_lines (struct frame *f, int vpos, int n)
3197 {
3198 emacs_abort ();
3199 }
3200
3201
3202 /* Scroll part of the display as described by RUN. */
3203
3204 static void
3205 x_scroll_run (struct window *w, struct run *run)
3206 {
3207 struct frame *f = XFRAME (w->frame);
3208 int x, y, width, height, from_y, to_y, bottom_y;
3209
3210 /* Get frame-relative bounding box of the text display area of W,
3211 without mode lines. Include in this box the left and right
3212 fringe of W. */
3213 window_box (w, ANY_AREA, &x, &y, &width, &height);
3214
3215 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
3216 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
3217 bottom_y = y + height;
3218
3219 if (to_y < from_y)
3220 {
3221 /* Scrolling up. Make sure we don't copy part of the mode
3222 line at the bottom. */
3223 if (from_y + run->height > bottom_y)
3224 height = bottom_y - from_y;
3225 else
3226 height = run->height;
3227 }
3228 else
3229 {
3230 /* Scrolling down. Make sure we don't copy over the mode line.
3231 at the bottom. */
3232 if (to_y + run->height > bottom_y)
3233 height = bottom_y - to_y;
3234 else
3235 height = run->height;
3236 }
3237
3238 block_input ();
3239
3240 /* Cursor off. Will be switched on again in x_update_window_end. */
3241 x_clear_cursor (w);
3242
3243 XCopyArea (FRAME_X_DISPLAY (f),
3244 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
3245 f->output_data.x->normal_gc,
3246 x, from_y,
3247 width, height,
3248 x, to_y);
3249
3250 unblock_input ();
3251 }
3252
3253
3254 \f
3255 /***********************************************************************
3256 Exposure Events
3257 ***********************************************************************/
3258
3259 \f
3260 static void
3261 frame_highlight (struct frame *f)
3262 {
3263 /* We used to only do this if Vx_no_window_manager was non-nil, but
3264 the ICCCM (section 4.1.6) says that the window's border pixmap
3265 and border pixel are window attributes which are "private to the
3266 client", so we can always change it to whatever we want. */
3267 block_input ();
3268 /* I recently started to get errors in this XSetWindowBorder, depending on
3269 the window-manager in use, tho something more is at play since I've been
3270 using that same window-manager binary for ever. Let's not crash just
3271 because of this (bug#9310). */
3272 x_catch_errors (FRAME_X_DISPLAY (f));
3273 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3274 f->output_data.x->border_pixel);
3275 x_uncatch_errors ();
3276 unblock_input ();
3277 x_update_cursor (f, true);
3278 x_set_frame_alpha (f);
3279 }
3280
3281 static void
3282 frame_unhighlight (struct frame *f)
3283 {
3284 /* We used to only do this if Vx_no_window_manager was non-nil, but
3285 the ICCCM (section 4.1.6) says that the window's border pixmap
3286 and border pixel are window attributes which are "private to the
3287 client", so we can always change it to whatever we want. */
3288 block_input ();
3289 /* Same as above for XSetWindowBorder (bug#9310). */
3290 x_catch_errors (FRAME_X_DISPLAY (f));
3291 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3292 f->output_data.x->border_tile);
3293 x_uncatch_errors ();
3294 unblock_input ();
3295 x_update_cursor (f, true);
3296 x_set_frame_alpha (f);
3297 }
3298
3299 /* The focus has changed. Update the frames as necessary to reflect
3300 the new situation. Note that we can't change the selected frame
3301 here, because the Lisp code we are interrupting might become confused.
3302 Each event gets marked with the frame in which it occurred, so the
3303 Lisp code can tell when the switch took place by examining the events. */
3304
3305 static void
3306 x_new_focus_frame (struct x_display_info *dpyinfo, struct frame *frame)
3307 {
3308 struct frame *old_focus = dpyinfo->x_focus_frame;
3309
3310 if (frame != dpyinfo->x_focus_frame)
3311 {
3312 /* Set this before calling other routines, so that they see
3313 the correct value of x_focus_frame. */
3314 dpyinfo->x_focus_frame = frame;
3315
3316 if (old_focus && old_focus->auto_lower)
3317 x_lower_frame (old_focus);
3318
3319 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
3320 dpyinfo->x_pending_autoraise_frame = dpyinfo->x_focus_frame;
3321 else
3322 dpyinfo->x_pending_autoraise_frame = NULL;
3323 }
3324
3325 x_frame_rehighlight (dpyinfo);
3326 }
3327
3328 /* Handle FocusIn and FocusOut state changes for FRAME.
3329 If FRAME has focus and there exists more than one frame, puts
3330 a FOCUS_IN_EVENT into *BUFP. */
3331
3332 static void
3333 x_focus_changed (int type, int state, struct x_display_info *dpyinfo, struct frame *frame, struct input_event *bufp)
3334 {
3335 if (type == FocusIn)
3336 {
3337 if (dpyinfo->x_focus_event_frame != frame)
3338 {
3339 x_new_focus_frame (dpyinfo, frame);
3340 dpyinfo->x_focus_event_frame = frame;
3341
3342 /* Don't stop displaying the initial startup message
3343 for a switch-frame event we don't need. */
3344 /* When run as a daemon, Vterminal_frame is always NIL. */
3345 bufp->arg = (((NILP (Vterminal_frame)
3346 || ! FRAME_X_P (XFRAME (Vterminal_frame))
3347 || EQ (Fdaemonp (), Qt))
3348 && CONSP (Vframe_list)
3349 && !NILP (XCDR (Vframe_list)))
3350 ? Qt : Qnil);
3351 bufp->kind = FOCUS_IN_EVENT;
3352 XSETFRAME (bufp->frame_or_window, frame);
3353 }
3354
3355 frame->output_data.x->focus_state |= state;
3356
3357 #ifdef HAVE_X_I18N
3358 if (FRAME_XIC (frame))
3359 XSetICFocus (FRAME_XIC (frame));
3360 #endif
3361 }
3362 else if (type == FocusOut)
3363 {
3364 frame->output_data.x->focus_state &= ~state;
3365
3366 if (dpyinfo->x_focus_event_frame == frame)
3367 {
3368 dpyinfo->x_focus_event_frame = 0;
3369 x_new_focus_frame (dpyinfo, 0);
3370
3371 bufp->kind = FOCUS_OUT_EVENT;
3372 XSETFRAME (bufp->frame_or_window, frame);
3373 }
3374
3375 #ifdef HAVE_X_I18N
3376 if (FRAME_XIC (frame))
3377 XUnsetICFocus (FRAME_XIC (frame));
3378 #endif
3379 if (frame->pointer_invisible)
3380 XTtoggle_invisible_pointer (frame, false);
3381 }
3382 }
3383
3384 /* Return the Emacs frame-object corresponding to an X window.
3385 It could be the frame's main window or an icon window. */
3386
3387 static struct frame *
3388 x_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
3389 {
3390 Lisp_Object tail, frame;
3391 struct frame *f;
3392
3393 if (wdesc == None)
3394 return NULL;
3395
3396 FOR_EACH_FRAME (tail, frame)
3397 {
3398 f = XFRAME (frame);
3399 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
3400 continue;
3401 if (f->output_data.x->hourglass_window == wdesc)
3402 return f;
3403 #ifdef USE_X_TOOLKIT
3404 if ((f->output_data.x->edit_widget
3405 && XtWindow (f->output_data.x->edit_widget) == wdesc)
3406 /* A tooltip frame? */
3407 || (!f->output_data.x->edit_widget
3408 && FRAME_X_WINDOW (f) == wdesc)
3409 || f->output_data.x->icon_desc == wdesc)
3410 return f;
3411 #else /* not USE_X_TOOLKIT */
3412 #ifdef USE_GTK
3413 if (f->output_data.x->edit_widget)
3414 {
3415 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
3416 struct x_output *x = f->output_data.x;
3417 if (gwdesc != 0 && gwdesc == x->edit_widget)
3418 return f;
3419 }
3420 #endif /* USE_GTK */
3421 if (FRAME_X_WINDOW (f) == wdesc
3422 || f->output_data.x->icon_desc == wdesc)
3423 return f;
3424 #endif /* not USE_X_TOOLKIT */
3425 }
3426 return 0;
3427 }
3428
3429 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
3430
3431 /* Like x_window_to_frame but also compares the window with the widget's
3432 windows. */
3433
3434 static struct frame *
3435 x_any_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
3436 {
3437 Lisp_Object tail, frame;
3438 struct frame *f, *found = NULL;
3439 struct x_output *x;
3440
3441 if (wdesc == None)
3442 return NULL;
3443
3444 FOR_EACH_FRAME (tail, frame)
3445 {
3446 if (found)
3447 break;
3448 f = XFRAME (frame);
3449 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo)
3450 {
3451 /* This frame matches if the window is any of its widgets. */
3452 x = f->output_data.x;
3453 if (x->hourglass_window == wdesc)
3454 found = f;
3455 else if (x->widget)
3456 {
3457 #ifdef USE_GTK
3458 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
3459 if (gwdesc != 0
3460 && gtk_widget_get_toplevel (gwdesc) == x->widget)
3461 found = f;
3462 #else
3463 if (wdesc == XtWindow (x->widget)
3464 || wdesc == XtWindow (x->column_widget)
3465 || wdesc == XtWindow (x->edit_widget))
3466 found = f;
3467 /* Match if the window is this frame's menubar. */
3468 else if (lw_window_is_in_menubar (wdesc, x->menubar_widget))
3469 found = f;
3470 #endif
3471 }
3472 else if (FRAME_X_WINDOW (f) == wdesc)
3473 /* A tooltip frame. */
3474 found = f;
3475 }
3476 }
3477
3478 return found;
3479 }
3480
3481 /* Likewise, but consider only the menu bar widget. */
3482
3483 static struct frame *
3484 x_menubar_window_to_frame (struct x_display_info *dpyinfo,
3485 const XEvent *event)
3486 {
3487 Window wdesc = event->xany.window;
3488 Lisp_Object tail, frame;
3489 struct frame *f;
3490 struct x_output *x;
3491
3492 if (wdesc == None)
3493 return NULL;
3494
3495 FOR_EACH_FRAME (tail, frame)
3496 {
3497 f = XFRAME (frame);
3498 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
3499 continue;
3500 x = f->output_data.x;
3501 #ifdef USE_GTK
3502 if (x->menubar_widget && xg_event_is_for_menubar (f, event))
3503 return f;
3504 #else
3505 /* Match if the window is this frame's menubar. */
3506 if (x->menubar_widget
3507 && lw_window_is_in_menubar (wdesc, x->menubar_widget))
3508 return f;
3509 #endif
3510 }
3511 return 0;
3512 }
3513
3514 /* Return the frame whose principal (outermost) window is WDESC.
3515 If WDESC is some other (smaller) window, we return 0. */
3516
3517 struct frame *
3518 x_top_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
3519 {
3520 Lisp_Object tail, frame;
3521 struct frame *f;
3522 struct x_output *x;
3523
3524 if (wdesc == None)
3525 return NULL;
3526
3527 FOR_EACH_FRAME (tail, frame)
3528 {
3529 f = XFRAME (frame);
3530 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
3531 continue;
3532 x = f->output_data.x;
3533
3534 if (x->widget)
3535 {
3536 /* This frame matches if the window is its topmost widget. */
3537 #ifdef USE_GTK
3538 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
3539 if (gwdesc == x->widget)
3540 return f;
3541 #else
3542 if (wdesc == XtWindow (x->widget))
3543 return f;
3544 #endif
3545 }
3546 else if (FRAME_X_WINDOW (f) == wdesc)
3547 /* Tooltip frame. */
3548 return f;
3549 }
3550 return 0;
3551 }
3552
3553 #else /* !USE_X_TOOLKIT && !USE_GTK */
3554
3555 #define x_any_window_to_frame(d, i) x_window_to_frame (d, i)
3556 #define x_top_window_to_frame(d, i) x_window_to_frame (d, i)
3557
3558 #endif /* USE_X_TOOLKIT || USE_GTK */
3559
3560 /* The focus may have changed. Figure out if it is a real focus change,
3561 by checking both FocusIn/Out and Enter/LeaveNotify events.
3562
3563 Returns FOCUS_IN_EVENT event in *BUFP. */
3564
3565 static void
3566 x_detect_focus_change (struct x_display_info *dpyinfo, struct frame *frame,
3567 const XEvent *event, struct input_event *bufp)
3568 {
3569 if (!frame)
3570 return;
3571
3572 switch (event->type)
3573 {
3574 case EnterNotify:
3575 case LeaveNotify:
3576 {
3577 struct frame *focus_frame = dpyinfo->x_focus_event_frame;
3578 int focus_state
3579 = focus_frame ? focus_frame->output_data.x->focus_state : 0;
3580
3581 if (event->xcrossing.detail != NotifyInferior
3582 && event->xcrossing.focus
3583 && ! (focus_state & FOCUS_EXPLICIT))
3584 x_focus_changed ((event->type == EnterNotify ? FocusIn : FocusOut),
3585 FOCUS_IMPLICIT,
3586 dpyinfo, frame, bufp);
3587 }
3588 break;
3589
3590 case FocusIn:
3591 case FocusOut:
3592 x_focus_changed (event->type,
3593 (event->xfocus.detail == NotifyPointer ?
3594 FOCUS_IMPLICIT : FOCUS_EXPLICIT),
3595 dpyinfo, frame, bufp);
3596 break;
3597
3598 case ClientMessage:
3599 if (event->xclient.message_type == dpyinfo->Xatom_XEMBED)
3600 {
3601 enum xembed_message msg = event->xclient.data.l[1];
3602 x_focus_changed ((msg == XEMBED_FOCUS_IN ? FocusIn : FocusOut),
3603 FOCUS_EXPLICIT, dpyinfo, frame, bufp);
3604 }
3605 break;
3606 }
3607 }
3608
3609
3610 #if !defined USE_X_TOOLKIT && !defined USE_GTK
3611 /* Handle an event saying the mouse has moved out of an Emacs frame. */
3612
3613 void
3614 x_mouse_leave (struct x_display_info *dpyinfo)
3615 {
3616 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
3617 }
3618 #endif
3619
3620 /* The focus has changed, or we have redirected a frame's focus to
3621 another frame (this happens when a frame uses a surrogate
3622 mini-buffer frame). Shift the highlight as appropriate.
3623
3624 The FRAME argument doesn't necessarily have anything to do with which
3625 frame is being highlighted or un-highlighted; we only use it to find
3626 the appropriate X display info. */
3627
3628 static void
3629 XTframe_rehighlight (struct frame *frame)
3630 {
3631 x_frame_rehighlight (FRAME_DISPLAY_INFO (frame));
3632 }
3633
3634 static void
3635 x_frame_rehighlight (struct x_display_info *dpyinfo)
3636 {
3637 struct frame *old_highlight = dpyinfo->x_highlight_frame;
3638
3639 if (dpyinfo->x_focus_frame)
3640 {
3641 dpyinfo->x_highlight_frame
3642 = ((FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
3643 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
3644 : dpyinfo->x_focus_frame);
3645 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
3646 {
3647 fset_focus_frame (dpyinfo->x_focus_frame, Qnil);
3648 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
3649 }
3650 }
3651 else
3652 dpyinfo->x_highlight_frame = 0;
3653
3654 if (dpyinfo->x_highlight_frame != old_highlight)
3655 {
3656 if (old_highlight)
3657 frame_unhighlight (old_highlight);
3658 if (dpyinfo->x_highlight_frame)
3659 frame_highlight (dpyinfo->x_highlight_frame);
3660 }
3661 }
3662
3663
3664 \f
3665 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
3666
3667 /* Initialize mode_switch_bit and modifier_meaning. */
3668 static void
3669 x_find_modifier_meanings (struct x_display_info *dpyinfo)
3670 {
3671 int min_code, max_code;
3672 KeySym *syms;
3673 int syms_per_code;
3674 XModifierKeymap *mods;
3675
3676 dpyinfo->meta_mod_mask = 0;
3677 dpyinfo->shift_lock_mask = 0;
3678 dpyinfo->alt_mod_mask = 0;
3679 dpyinfo->super_mod_mask = 0;
3680 dpyinfo->hyper_mod_mask = 0;
3681
3682 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
3683
3684 syms = XGetKeyboardMapping (dpyinfo->display,
3685 min_code, max_code - min_code + 1,
3686 &syms_per_code);
3687 mods = XGetModifierMapping (dpyinfo->display);
3688
3689 /* Scan the modifier table to see which modifier bits the Meta and
3690 Alt keysyms are on. */
3691 {
3692 int row, col; /* The row and column in the modifier table. */
3693 bool found_alt_or_meta;
3694
3695 for (row = 3; row < 8; row++)
3696 {
3697 found_alt_or_meta = false;
3698 for (col = 0; col < mods->max_keypermod; col++)
3699 {
3700 KeyCode code = mods->modifiermap[(row * mods->max_keypermod) + col];
3701
3702 /* Zeroes are used for filler. Skip them. */
3703 if (code == 0)
3704 continue;
3705
3706 /* Are any of this keycode's keysyms a meta key? */
3707 {
3708 int code_col;
3709
3710 for (code_col = 0; code_col < syms_per_code; code_col++)
3711 {
3712 int sym = syms[((code - min_code) * syms_per_code) + code_col];
3713
3714 switch (sym)
3715 {
3716 case XK_Meta_L:
3717 case XK_Meta_R:
3718 found_alt_or_meta = true;
3719 dpyinfo->meta_mod_mask |= (1 << row);
3720 break;
3721
3722 case XK_Alt_L:
3723 case XK_Alt_R:
3724 found_alt_or_meta = true;
3725 dpyinfo->alt_mod_mask |= (1 << row);
3726 break;
3727
3728 case XK_Hyper_L:
3729 case XK_Hyper_R:
3730 if (!found_alt_or_meta)
3731 dpyinfo->hyper_mod_mask |= (1 << row);
3732 code_col = syms_per_code;
3733 col = mods->max_keypermod;
3734 break;
3735
3736 case XK_Super_L:
3737 case XK_Super_R:
3738 if (!found_alt_or_meta)
3739 dpyinfo->super_mod_mask |= (1 << row);
3740 code_col = syms_per_code;
3741 col = mods->max_keypermod;
3742 break;
3743
3744 case XK_Shift_Lock:
3745 /* Ignore this if it's not on the lock modifier. */
3746 if (!found_alt_or_meta && ((1 << row) == LockMask))
3747 dpyinfo->shift_lock_mask = LockMask;
3748 code_col = syms_per_code;
3749 col = mods->max_keypermod;
3750 break;
3751 }
3752 }
3753 }
3754 }
3755 }
3756 }
3757
3758 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
3759 if (! dpyinfo->meta_mod_mask)
3760 {
3761 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
3762 dpyinfo->alt_mod_mask = 0;
3763 }
3764
3765 /* If some keys are both alt and meta,
3766 make them just meta, not alt. */
3767 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
3768 {
3769 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
3770 }
3771
3772 XFree (syms);
3773 XFreeModifiermap (mods);
3774 }
3775
3776 /* Convert between the modifier bits X uses and the modifier bits
3777 Emacs uses. */
3778
3779 int
3780 x_x_to_emacs_modifiers (struct x_display_info *dpyinfo, int state)
3781 {
3782 int mod_meta = meta_modifier;
3783 int mod_alt = alt_modifier;
3784 int mod_hyper = hyper_modifier;
3785 int mod_super = super_modifier;
3786 Lisp_Object tem;
3787
3788 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3789 if (INTEGERP (tem)) mod_alt = XINT (tem) & INT_MAX;
3790 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3791 if (INTEGERP (tem)) mod_meta = XINT (tem) & INT_MAX;
3792 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3793 if (INTEGERP (tem)) mod_hyper = XINT (tem) & INT_MAX;
3794 tem = Fget (Vx_super_keysym, Qmodifier_value);
3795 if (INTEGERP (tem)) mod_super = XINT (tem) & INT_MAX;
3796
3797 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
3798 | ((state & ControlMask) ? ctrl_modifier : 0)
3799 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
3800 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
3801 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
3802 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
3803 }
3804
3805 static int
3806 x_emacs_to_x_modifiers (struct x_display_info *dpyinfo, EMACS_INT state)
3807 {
3808 EMACS_INT mod_meta = meta_modifier;
3809 EMACS_INT mod_alt = alt_modifier;
3810 EMACS_INT mod_hyper = hyper_modifier;
3811 EMACS_INT mod_super = super_modifier;
3812
3813 Lisp_Object tem;
3814
3815 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3816 if (INTEGERP (tem)) mod_alt = XINT (tem);
3817 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3818 if (INTEGERP (tem)) mod_meta = XINT (tem);
3819 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3820 if (INTEGERP (tem)) mod_hyper = XINT (tem);
3821 tem = Fget (Vx_super_keysym, Qmodifier_value);
3822 if (INTEGERP (tem)) mod_super = XINT (tem);
3823
3824
3825 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
3826 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
3827 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
3828 | ((state & shift_modifier) ? ShiftMask : 0)
3829 | ((state & ctrl_modifier) ? ControlMask : 0)
3830 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
3831 }
3832
3833 /* Convert a keysym to its name. */
3834
3835 char *
3836 x_get_keysym_name (int keysym)
3837 {
3838 char *value;
3839
3840 block_input ();
3841 value = XKeysymToString (keysym);
3842 unblock_input ();
3843
3844 return value;
3845 }
3846
3847 /* Mouse clicks and mouse movement. Rah.
3848
3849 Formerly, we used PointerMotionHintMask (in standard_event_mask)
3850 so that we would have to call XQueryPointer after each MotionNotify
3851 event to ask for another such event. However, this made mouse tracking
3852 slow, and there was a bug that made it eventually stop.
3853
3854 Simply asking for MotionNotify all the time seems to work better.
3855
3856 In order to avoid asking for motion events and then throwing most
3857 of them away or busy-polling the server for mouse positions, we ask
3858 the server for pointer motion hints. This means that we get only
3859 one event per group of mouse movements. "Groups" are delimited by
3860 other kinds of events (focus changes and button clicks, for
3861 example), or by XQueryPointer calls; when one of these happens, we
3862 get another MotionNotify event the next time the mouse moves. This
3863 is at least as efficient as getting motion events when mouse
3864 tracking is on, and I suspect only negligibly worse when tracking
3865 is off. */
3866
3867 /* Prepare a mouse-event in *RESULT for placement in the input queue.
3868
3869 If the event is a button press, then note that we have grabbed
3870 the mouse. */
3871
3872 static Lisp_Object
3873 construct_mouse_click (struct input_event *result,
3874 const XButtonEvent *event,
3875 struct frame *f)
3876 {
3877 /* Make the event type NO_EVENT; we'll change that when we decide
3878 otherwise. */
3879 result->kind = MOUSE_CLICK_EVENT;
3880 result->code = event->button - Button1;
3881 result->timestamp = event->time;
3882 result->modifiers = (x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO (f),
3883 event->state)
3884 | (event->type == ButtonRelease
3885 ? up_modifier
3886 : down_modifier));
3887
3888 XSETINT (result->x, event->x);
3889 XSETINT (result->y, event->y);
3890 XSETFRAME (result->frame_or_window, f);
3891 result->arg = Qnil;
3892 return Qnil;
3893 }
3894
3895 /* Function to report a mouse movement to the mainstream Emacs code.
3896 The input handler calls this.
3897
3898 We have received a mouse movement event, which is given in *event.
3899 If the mouse is over a different glyph than it was last time, tell
3900 the mainstream emacs code by setting mouse_moved. If not, ask for
3901 another motion event, so we can check again the next time it moves. */
3902
3903 static bool
3904 note_mouse_movement (struct frame *frame, const XMotionEvent *event)
3905 {
3906 XRectangle *r;
3907 struct x_display_info *dpyinfo;
3908
3909 if (!FRAME_X_OUTPUT (frame))
3910 return false;
3911
3912 dpyinfo = FRAME_DISPLAY_INFO (frame);
3913 dpyinfo->last_mouse_movement_time = event->time;
3914 dpyinfo->last_mouse_motion_frame = frame;
3915 dpyinfo->last_mouse_motion_x = event->x;
3916 dpyinfo->last_mouse_motion_y = event->y;
3917
3918 if (event->window != FRAME_X_WINDOW (frame))
3919 {
3920 frame->mouse_moved = true;
3921 dpyinfo->last_mouse_scroll_bar = NULL;
3922 note_mouse_highlight (frame, -1, -1);
3923 dpyinfo->last_mouse_glyph_frame = NULL;
3924 return true;
3925 }
3926
3927
3928 /* Has the mouse moved off the glyph it was on at the last sighting? */
3929 r = &dpyinfo->last_mouse_glyph;
3930 if (frame != dpyinfo->last_mouse_glyph_frame
3931 || event->x < r->x || event->x >= r->x + r->width
3932 || event->y < r->y || event->y >= r->y + r->height)
3933 {
3934 frame->mouse_moved = true;
3935 dpyinfo->last_mouse_scroll_bar = NULL;
3936 note_mouse_highlight (frame, event->x, event->y);
3937 /* Remember which glyph we're now on. */
3938 remember_mouse_glyph (frame, event->x, event->y, r);
3939 dpyinfo->last_mouse_glyph_frame = frame;
3940 return true;
3941 }
3942
3943 return false;
3944 }
3945
3946 /* Return the current position of the mouse.
3947 *FP should be a frame which indicates which display to ask about.
3948
3949 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
3950 and *PART to the frame, window, and scroll bar part that the mouse
3951 is over. Set *X and *Y to the portion and whole of the mouse's
3952 position on the scroll bar.
3953
3954 If the mouse movement started elsewhere, set *FP to the frame the
3955 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
3956 the mouse is over.
3957
3958 Set *TIMESTAMP to the server time-stamp for the time at which the mouse
3959 was at this position.
3960
3961 Don't store anything if we don't have a valid set of values to report.
3962
3963 This clears the mouse_moved flag, so we can wait for the next mouse
3964 movement. */
3965
3966 static void
3967 XTmouse_position (struct frame **fp, int insist, Lisp_Object *bar_window,
3968 enum scroll_bar_part *part, Lisp_Object *x, Lisp_Object *y,
3969 Time *timestamp)
3970 {
3971 struct frame *f1;
3972 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
3973
3974 block_input ();
3975
3976 if (dpyinfo->last_mouse_scroll_bar && insist == 0)
3977 {
3978 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
3979
3980 if (bar->horizontal)
3981 x_horizontal_scroll_bar_report_motion (fp, bar_window, part, x, y, timestamp);
3982 else
3983 x_scroll_bar_report_motion (fp, bar_window, part, x, y, timestamp);
3984 }
3985 else
3986 {
3987 Window root;
3988 int root_x, root_y;
3989
3990 Window dummy_window;
3991 int dummy;
3992
3993 Lisp_Object frame, tail;
3994
3995 /* Clear the mouse-moved flag for every frame on this display. */
3996 FOR_EACH_FRAME (tail, frame)
3997 if (FRAME_X_P (XFRAME (frame))
3998 && FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
3999 XFRAME (frame)->mouse_moved = false;
4000
4001 dpyinfo->last_mouse_scroll_bar = NULL;
4002
4003 /* Figure out which root window we're on. */
4004 XQueryPointer (FRAME_X_DISPLAY (*fp),
4005 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
4006
4007 /* The root window which contains the pointer. */
4008 &root,
4009
4010 /* Trash which we can't trust if the pointer is on
4011 a different screen. */
4012 &dummy_window,
4013
4014 /* The position on that root window. */
4015 &root_x, &root_y,
4016
4017 /* More trash we can't trust. */
4018 &dummy, &dummy,
4019
4020 /* Modifier keys and pointer buttons, about which
4021 we don't care. */
4022 (unsigned int *) &dummy);
4023
4024 /* Now we have a position on the root; find the innermost window
4025 containing the pointer. */
4026 {
4027 Window win, child;
4028 int win_x, win_y;
4029 int parent_x = 0, parent_y = 0;
4030
4031 win = root;
4032
4033 /* XTranslateCoordinates can get errors if the window
4034 structure is changing at the same time this function
4035 is running. So at least we must not crash from them. */
4036
4037 x_catch_errors (FRAME_X_DISPLAY (*fp));
4038
4039 if (x_mouse_grabbed (dpyinfo))
4040 {
4041 /* If mouse was grabbed on a frame, give coords for that frame
4042 even if the mouse is now outside it. */
4043 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
4044
4045 /* From-window. */
4046 root,
4047
4048 /* To-window. */
4049 FRAME_X_WINDOW (dpyinfo->last_mouse_frame),
4050
4051 /* From-position, to-position. */
4052 root_x, root_y, &win_x, &win_y,
4053
4054 /* Child of win. */
4055 &child);
4056 f1 = dpyinfo->last_mouse_frame;
4057 }
4058 else
4059 {
4060 while (true)
4061 {
4062 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
4063
4064 /* From-window, to-window. */
4065 root, win,
4066
4067 /* From-position, to-position. */
4068 root_x, root_y, &win_x, &win_y,
4069
4070 /* Child of win. */
4071 &child);
4072
4073 if (child == None || child == win)
4074 break;
4075 #ifdef USE_GTK
4076 /* We don't wan't to know the innermost window. We
4077 want the edit window. For non-Gtk+ the innermost
4078 window is the edit window. For Gtk+ it might not
4079 be. It might be the tool bar for example. */
4080 if (x_window_to_frame (dpyinfo, win))
4081 break;
4082 #endif
4083 win = child;
4084 parent_x = win_x;
4085 parent_y = win_y;
4086 }
4087
4088 /* Now we know that:
4089 win is the innermost window containing the pointer
4090 (XTC says it has no child containing the pointer),
4091 win_x and win_y are the pointer's position in it
4092 (XTC did this the last time through), and
4093 parent_x and parent_y are the pointer's position in win's parent.
4094 (They are what win_x and win_y were when win was child.
4095 If win is the root window, it has no parent, and
4096 parent_{x,y} are invalid, but that's okay, because we'll
4097 never use them in that case.) */
4098
4099 #ifdef USE_GTK
4100 /* We don't wan't to know the innermost window. We
4101 want the edit window. */
4102 f1 = x_window_to_frame (dpyinfo, win);
4103 #else
4104 /* Is win one of our frames? */
4105 f1 = x_any_window_to_frame (dpyinfo, win);
4106 #endif
4107
4108 #ifdef USE_X_TOOLKIT
4109 /* If we end up with the menu bar window, say it's not
4110 on the frame. */
4111 if (f1 != NULL
4112 && f1->output_data.x->menubar_widget
4113 && win == XtWindow (f1->output_data.x->menubar_widget))
4114 f1 = NULL;
4115 #endif /* USE_X_TOOLKIT */
4116 }
4117
4118 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
4119 f1 = 0;
4120
4121 x_uncatch_errors ();
4122
4123 /* If not, is it one of our scroll bars? */
4124 if (! f1)
4125 {
4126 struct scroll_bar *bar;
4127
4128 bar = x_window_to_scroll_bar (FRAME_X_DISPLAY (*fp), win, 2);
4129
4130 if (bar)
4131 {
4132 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4133 win_x = parent_x;
4134 win_y = parent_y;
4135 }
4136 }
4137
4138 if (f1 == 0 && insist > 0)
4139 f1 = SELECTED_FRAME ();
4140
4141 if (f1)
4142 {
4143 /* Ok, we found a frame. Store all the values.
4144 last_mouse_glyph is a rectangle used to reduce the
4145 generation of mouse events. To not miss any motion
4146 events, we must divide the frame into rectangles of the
4147 size of the smallest character that could be displayed
4148 on it, i.e. into the same rectangles that matrices on
4149 the frame are divided into. */
4150
4151 /* FIXME: what if F1 is not an X frame? */
4152 dpyinfo = FRAME_DISPLAY_INFO (f1);
4153 remember_mouse_glyph (f1, win_x, win_y, &dpyinfo->last_mouse_glyph);
4154 dpyinfo->last_mouse_glyph_frame = f1;
4155
4156 *bar_window = Qnil;
4157 *part = scroll_bar_above_handle;
4158 *fp = f1;
4159 XSETINT (*x, win_x);
4160 XSETINT (*y, win_y);
4161 *timestamp = dpyinfo->last_mouse_movement_time;
4162 }
4163 }
4164 }
4165
4166 unblock_input ();
4167 }
4168
4169
4170 \f
4171 /***********************************************************************
4172 Scroll bars
4173 ***********************************************************************/
4174
4175 /* Scroll bar support. */
4176
4177 /* Given an X window ID and a DISPLAY, find the struct scroll_bar which
4178 manages it.
4179 This can be called in GC, so we have to make sure to strip off mark
4180 bits. */
4181
4182 static struct scroll_bar *
4183 x_window_to_scroll_bar (Display *display, Window window_id, int type)
4184 {
4185 Lisp_Object tail, frame;
4186
4187 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
4188 window_id = (Window) xg_get_scroll_id_for_window (display, window_id);
4189 #endif /* USE_GTK && USE_TOOLKIT_SCROLL_BARS */
4190
4191 FOR_EACH_FRAME (tail, frame)
4192 {
4193 Lisp_Object bar, condemned;
4194
4195 if (! FRAME_X_P (XFRAME (frame)))
4196 continue;
4197
4198 /* Scan this frame's scroll bar list for a scroll bar with the
4199 right window ID. */
4200 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
4201 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
4202 /* This trick allows us to search both the ordinary and
4203 condemned scroll bar lists with one loop. */
4204 ! NILP (bar) || (bar = condemned,
4205 condemned = Qnil,
4206 ! NILP (bar));
4207 bar = XSCROLL_BAR (bar)->next)
4208 if (XSCROLL_BAR (bar)->x_window == window_id
4209 && FRAME_X_DISPLAY (XFRAME (frame)) == display
4210 && (type = 2
4211 || (type == 1 && XSCROLL_BAR (bar)->horizontal)
4212 || (type == 0 && !XSCROLL_BAR (bar)->horizontal)))
4213 return XSCROLL_BAR (bar);
4214 }
4215
4216 return NULL;
4217 }
4218
4219
4220 #if defined USE_LUCID
4221
4222 /* Return the Lucid menu bar WINDOW is part of. Return null
4223 if WINDOW is not part of a menu bar. */
4224
4225 static Widget
4226 x_window_to_menu_bar (Window window)
4227 {
4228 Lisp_Object tail, frame;
4229
4230 FOR_EACH_FRAME (tail, frame)
4231 if (FRAME_X_P (XFRAME (frame)))
4232 {
4233 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
4234
4235 if (menu_bar && xlwmenu_window_p (menu_bar, window))
4236 return menu_bar;
4237 }
4238 return NULL;
4239 }
4240
4241 #endif /* USE_LUCID */
4242
4243 \f
4244 /************************************************************************
4245 Toolkit scroll bars
4246 ************************************************************************/
4247
4248 #ifdef USE_TOOLKIT_SCROLL_BARS
4249
4250 static void x_send_scroll_bar_event (Lisp_Object, enum scroll_bar_part,
4251 int, int, bool);
4252
4253 /* Lisp window being scrolled. Set when starting to interact with
4254 a toolkit scroll bar, reset to nil when ending the interaction. */
4255
4256 static Lisp_Object window_being_scrolled;
4257
4258 /* Whether this is an Xaw with arrow-scrollbars. This should imply
4259 that movements of 1/20 of the screen size are mapped to up/down. */
4260
4261 #ifndef USE_GTK
4262 /* Id of action hook installed for scroll bars. */
4263
4264 static XtActionHookId action_hook_id;
4265 static XtActionHookId horizontal_action_hook_id;
4266
4267 static Boolean xaw3d_arrow_scroll;
4268
4269 /* Whether the drag scrolling maintains the mouse at the top of the
4270 thumb. If not, resizing the thumb needs to be done more carefully
4271 to avoid jerkiness. */
4272
4273 static Boolean xaw3d_pick_top;
4274
4275 /* Action hook installed via XtAppAddActionHook when toolkit scroll
4276 bars are used.. The hook is responsible for detecting when
4277 the user ends an interaction with the scroll bar, and generates
4278 a `end-scroll' SCROLL_BAR_CLICK_EVENT' event if so. */
4279
4280 static void
4281 xt_action_hook (Widget widget, XtPointer client_data, String action_name,
4282 XEvent *event, String *params, Cardinal *num_params)
4283 {
4284 bool scroll_bar_p;
4285 const char *end_action;
4286
4287 #ifdef USE_MOTIF
4288 scroll_bar_p = XmIsScrollBar (widget);
4289 end_action = "Release";
4290 #else /* !USE_MOTIF i.e. use Xaw */
4291 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
4292 end_action = "EndScroll";
4293 #endif /* USE_MOTIF */
4294
4295 if (scroll_bar_p
4296 && strcmp (action_name, end_action) == 0
4297 && WINDOWP (window_being_scrolled))
4298 {
4299 struct window *w;
4300 struct scroll_bar *bar;
4301
4302 x_send_scroll_bar_event (window_being_scrolled,
4303 scroll_bar_end_scroll, 0, 0, false);
4304 w = XWINDOW (window_being_scrolled);
4305 bar = XSCROLL_BAR (w->vertical_scroll_bar);
4306
4307 if (bar->dragging != -1)
4308 {
4309 bar->dragging = -1;
4310 /* The thumb size is incorrect while dragging: fix it. */
4311 set_vertical_scroll_bar (w);
4312 }
4313 window_being_scrolled = Qnil;
4314 #if defined (USE_LUCID)
4315 bar->last_seen_part = scroll_bar_nowhere;
4316 #endif
4317 /* Xt timeouts no longer needed. */
4318 toolkit_scroll_bar_interaction = false;
4319 }
4320 }
4321
4322
4323 static void
4324 xt_horizontal_action_hook (Widget widget, XtPointer client_data, String action_name,
4325 XEvent *event, String *params, Cardinal *num_params)
4326 {
4327 bool scroll_bar_p;
4328 const char *end_action;
4329
4330 #ifdef USE_MOTIF
4331 scroll_bar_p = XmIsScrollBar (widget);
4332 end_action = "Release";
4333 #else /* !USE_MOTIF i.e. use Xaw */
4334 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
4335 end_action = "EndScroll";
4336 #endif /* USE_MOTIF */
4337
4338 if (scroll_bar_p
4339 && strcmp (action_name, end_action) == 0
4340 && WINDOWP (window_being_scrolled))
4341 {
4342 struct window *w;
4343 struct scroll_bar *bar;
4344
4345 x_send_scroll_bar_event (window_being_scrolled,
4346 scroll_bar_end_scroll, 0, 0, true);
4347 w = XWINDOW (window_being_scrolled);
4348 bar = XSCROLL_BAR (w->horizontal_scroll_bar);
4349
4350 if (bar->dragging != -1)
4351 {
4352 bar->dragging = -1;
4353 /* The thumb size is incorrect while dragging: fix it. */
4354 set_horizontal_scroll_bar (w);
4355 }
4356 window_being_scrolled = Qnil;
4357 #if defined (USE_LUCID)
4358 bar->last_seen_part = scroll_bar_nowhere;
4359 #endif
4360 /* Xt timeouts no longer needed. */
4361 toolkit_scroll_bar_interaction = false;
4362 }
4363 }
4364 #endif /* not USE_GTK */
4365
4366 /* Send a client message with message type Xatom_Scrollbar for a
4367 scroll action to the frame of WINDOW. PART is a value identifying
4368 the part of the scroll bar that was clicked on. PORTION is the
4369 amount to scroll of a whole of WHOLE. */
4370
4371 static void
4372 x_send_scroll_bar_event (Lisp_Object window, enum scroll_bar_part part,
4373 int portion, int whole, bool horizontal)
4374 {
4375 XEvent event;
4376 XClientMessageEvent *ev = &event.xclient;
4377 struct window *w = XWINDOW (window);
4378 struct frame *f = XFRAME (w->frame);
4379 intptr_t iw = (intptr_t) w;
4380 enum { BITS_PER_INTPTR = CHAR_BIT * sizeof iw };
4381 verify (BITS_PER_INTPTR <= 64);
4382 int sign_shift = BITS_PER_INTPTR - 32;
4383
4384 block_input ();
4385
4386 /* Construct a ClientMessage event to send to the frame. */
4387 ev->type = ClientMessage;
4388 ev->message_type = (horizontal
4389 ? FRAME_DISPLAY_INFO (f)->Xatom_Horizontal_Scrollbar
4390 : FRAME_DISPLAY_INFO (f)->Xatom_Scrollbar);
4391 ev->display = FRAME_X_DISPLAY (f);
4392 ev->window = FRAME_X_WINDOW (f);
4393 ev->format = 32;
4394
4395 /* A 32-bit X client on a 64-bit X server can pass a window pointer
4396 as-is. A 64-bit client on a 32-bit X server is in trouble
4397 because a pointer does not fit and would be truncated while
4398 passing through the server. So use two slots and hope that X12
4399 will resolve such issues someday. */
4400 ev->data.l[0] = iw >> 31 >> 1;
4401 ev->data.l[1] = sign_shift <= 0 ? iw : iw << sign_shift >> sign_shift;
4402 ev->data.l[2] = part;
4403 ev->data.l[3] = portion;
4404 ev->data.l[4] = whole;
4405
4406 /* Make Xt timeouts work while the scroll bar is active. */
4407 #ifdef USE_X_TOOLKIT
4408 toolkit_scroll_bar_interaction = true;
4409 x_activate_timeout_atimer ();
4410 #endif
4411
4412 /* Setting the event mask to zero means that the message will
4413 be sent to the client that created the window, and if that
4414 window no longer exists, no event will be sent. */
4415 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
4416 unblock_input ();
4417 }
4418
4419
4420 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
4421 in *IEVENT. */
4422
4423 static void
4424 x_scroll_bar_to_input_event (const XEvent *event,
4425 struct input_event *ievent)
4426 {
4427 const XClientMessageEvent *ev = &event->xclient;
4428 Lisp_Object window;
4429 struct window *w;
4430
4431 /* See the comment in the function above. */
4432 intptr_t iw0 = ev->data.l[0];
4433 intptr_t iw1 = ev->data.l[1];
4434 intptr_t iw = (iw0 << 31 << 1) + (iw1 & 0xffffffffu);
4435 w = (struct window *) iw;
4436
4437 XSETWINDOW (window, w);
4438
4439 ievent->kind = SCROLL_BAR_CLICK_EVENT;
4440 ievent->frame_or_window = window;
4441 ievent->arg = Qnil;
4442 #ifdef USE_GTK
4443 ievent->timestamp = CurrentTime;
4444 #else
4445 ievent->timestamp =
4446 XtLastTimestampProcessed (FRAME_X_DISPLAY (XFRAME (w->frame)));
4447 #endif
4448 ievent->code = 0;
4449 ievent->part = ev->data.l[2];
4450 ievent->x = make_number (ev->data.l[3]);
4451 ievent->y = make_number (ev->data.l[4]);
4452 ievent->modifiers = 0;
4453 }
4454
4455 /* Transform a horizontal scroll bar ClientMessage EVENT to an Emacs
4456 input event in *IEVENT. */
4457
4458 static void
4459 x_horizontal_scroll_bar_to_input_event (const XEvent *event,
4460 struct input_event *ievent)
4461 {
4462 const XClientMessageEvent *ev = &event->xclient;
4463 Lisp_Object window;
4464 struct window *w;
4465
4466 /* See the comment in the function above. */
4467 intptr_t iw0 = ev->data.l[0];
4468 intptr_t iw1 = ev->data.l[1];
4469 intptr_t iw = (iw0 << 31 << 1) + (iw1 & 0xffffffffu);
4470 w = (struct window *) iw;
4471
4472 XSETWINDOW (window, w);
4473
4474 ievent->kind = HORIZONTAL_SCROLL_BAR_CLICK_EVENT;
4475 ievent->frame_or_window = window;
4476 ievent->arg = Qnil;
4477 #ifdef USE_GTK
4478 ievent->timestamp = CurrentTime;
4479 #else
4480 ievent->timestamp =
4481 XtLastTimestampProcessed (FRAME_X_DISPLAY (XFRAME (w->frame)));
4482 #endif
4483 ievent->code = 0;
4484 ievent->part = ev->data.l[2];
4485 ievent->x = make_number (ev->data.l[3]);
4486 ievent->y = make_number (ev->data.l[4]);
4487 ievent->modifiers = 0;
4488 }
4489
4490
4491 #ifdef USE_MOTIF
4492
4493 /* Minimum and maximum values used for Motif scroll bars. */
4494
4495 #define XM_SB_MAX 10000000
4496
4497 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
4498 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
4499 CALL_DATA is a pointer to a XmScrollBarCallbackStruct. */
4500
4501 static void
4502 xm_scroll_callback (Widget widget, XtPointer client_data, XtPointer call_data)
4503 {
4504 struct scroll_bar *bar = client_data;
4505 XmScrollBarCallbackStruct *cs = call_data;
4506 enum scroll_bar_part part = scroll_bar_nowhere;
4507 bool horizontal = bar->horizontal;
4508 int whole = 0, portion = 0;
4509
4510 switch (cs->reason)
4511 {
4512 case XmCR_DECREMENT:
4513 bar->dragging = -1;
4514 part = horizontal ? scroll_bar_left_arrow : scroll_bar_up_arrow;
4515 break;
4516
4517 case XmCR_INCREMENT:
4518 bar->dragging = -1;
4519 part = horizontal ? scroll_bar_right_arrow : scroll_bar_down_arrow;
4520 break;
4521
4522 case XmCR_PAGE_DECREMENT:
4523 bar->dragging = -1;
4524 part = horizontal ? scroll_bar_before_handle : scroll_bar_above_handle;
4525 break;
4526
4527 case XmCR_PAGE_INCREMENT:
4528 bar->dragging = -1;
4529 part = horizontal ? scroll_bar_after_handle : scroll_bar_below_handle;
4530 break;
4531
4532 case XmCR_TO_TOP:
4533 bar->dragging = -1;
4534 part = horizontal ? scroll_bar_to_leftmost : scroll_bar_to_top;
4535 break;
4536
4537 case XmCR_TO_BOTTOM:
4538 bar->dragging = -1;
4539 part = horizontal ? scroll_bar_to_rightmost : scroll_bar_to_bottom;
4540 break;
4541
4542 case XmCR_DRAG:
4543 {
4544 int slider_size;
4545
4546 block_input ();
4547 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
4548 unblock_input ();
4549
4550 if (horizontal)
4551 {
4552 portion = bar->whole * ((float)cs->value / XM_SB_MAX);
4553 whole = bar->whole * ((float)(XM_SB_MAX - slider_size) / XM_SB_MAX);
4554 portion = min (portion, whole);
4555 part = scroll_bar_horizontal_handle;
4556 }
4557 else
4558 {
4559 whole = XM_SB_MAX - slider_size;
4560 portion = min (cs->value, whole);
4561 part = scroll_bar_handle;
4562 }
4563
4564 bar->dragging = cs->value;
4565 }
4566 break;
4567
4568 case XmCR_VALUE_CHANGED:
4569 break;
4570 };
4571
4572 if (part != scroll_bar_nowhere)
4573 {
4574 window_being_scrolled = bar->window;
4575 x_send_scroll_bar_event (bar->window, part, portion, whole,
4576 bar->horizontal);
4577 }
4578 }
4579
4580 #elif defined USE_GTK
4581
4582 /* Scroll bar callback for GTK scroll bars. WIDGET is the scroll
4583 bar widget. DATA is a pointer to the scroll_bar structure. */
4584
4585 static gboolean
4586 xg_scroll_callback (GtkRange *range,
4587 GtkScrollType scroll,
4588 gdouble value,
4589 gpointer user_data)
4590 {
4591 int whole = 0, portion = 0;
4592 struct scroll_bar *bar = user_data;
4593 enum scroll_bar_part part = scroll_bar_nowhere;
4594 GtkAdjustment *adj = GTK_ADJUSTMENT (gtk_range_get_adjustment (range));
4595 struct frame *f = g_object_get_data (G_OBJECT (range), XG_FRAME_DATA);
4596
4597 if (xg_ignore_gtk_scrollbar) return false;
4598
4599 switch (scroll)
4600 {
4601 case GTK_SCROLL_JUMP:
4602 /* Buttons 1 2 or 3 must be grabbed. */
4603 if (FRAME_DISPLAY_INFO (f)->grabbed != 0
4604 && FRAME_DISPLAY_INFO (f)->grabbed < (1 << 4))
4605 {
4606 if (bar->horizontal)
4607 {
4608 part = scroll_bar_horizontal_handle;
4609 whole = (int)(gtk_adjustment_get_upper (adj) -
4610 gtk_adjustment_get_page_size (adj));
4611 portion = min ((int)value, whole);
4612 bar->dragging = portion;
4613 }
4614 else
4615 {
4616 part = scroll_bar_handle;
4617 whole = gtk_adjustment_get_upper (adj) -
4618 gtk_adjustment_get_page_size (adj);
4619 portion = min ((int)value, whole);
4620 bar->dragging = portion;
4621 }
4622 }
4623 break;
4624 case GTK_SCROLL_STEP_BACKWARD:
4625 part = (bar->horizontal
4626 ? scroll_bar_left_arrow : scroll_bar_up_arrow);
4627 bar->dragging = -1;
4628 break;
4629 case GTK_SCROLL_STEP_FORWARD:
4630 part = (bar->horizontal
4631 ? scroll_bar_right_arrow : scroll_bar_down_arrow);
4632 bar->dragging = -1;
4633 break;
4634 case GTK_SCROLL_PAGE_BACKWARD:
4635 part = (bar->horizontal
4636 ? scroll_bar_before_handle : scroll_bar_above_handle);
4637 bar->dragging = -1;
4638 break;
4639 case GTK_SCROLL_PAGE_FORWARD:
4640 part = (bar->horizontal
4641 ? scroll_bar_after_handle : scroll_bar_below_handle);
4642 bar->dragging = -1;
4643 break;
4644 }
4645
4646 if (part != scroll_bar_nowhere)
4647 {
4648 window_being_scrolled = bar->window;
4649 x_send_scroll_bar_event (bar->window, part, portion, whole,
4650 bar->horizontal);
4651 }
4652
4653 return false;
4654 }
4655
4656 /* Callback for button release. Sets dragging to -1 when dragging is done. */
4657
4658 static gboolean
4659 xg_end_scroll_callback (GtkWidget *widget,
4660 GdkEventButton *event,
4661 gpointer user_data)
4662 {
4663 struct scroll_bar *bar = user_data;
4664 bar->dragging = -1;
4665 if (WINDOWP (window_being_scrolled))
4666 {
4667 x_send_scroll_bar_event (window_being_scrolled,
4668 scroll_bar_end_scroll, 0, 0, bar->horizontal);
4669 window_being_scrolled = Qnil;
4670 }
4671
4672 return false;
4673 }
4674
4675
4676 #else /* not USE_GTK and not USE_MOTIF */
4677
4678 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
4679 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
4680 scroll bar struct. CALL_DATA is a pointer to a float saying where
4681 the thumb is. */
4682
4683 static void
4684 xaw_jump_callback (Widget widget, XtPointer client_data, XtPointer call_data)
4685 {
4686 struct scroll_bar *bar = client_data;
4687 float *top_addr = call_data;
4688 float top = *top_addr;
4689 float shown;
4690 int whole, portion, height, width;
4691 enum scroll_bar_part part;
4692 bool horizontal = bar->horizontal;
4693
4694
4695 if (horizontal)
4696 {
4697 /* Get the size of the thumb, a value between 0 and 1. */
4698 block_input ();
4699 XtVaGetValues (widget, XtNshown, &shown, XtNwidth, &width, NULL);
4700 unblock_input ();
4701
4702 if (shown < 1)
4703 {
4704 whole = bar->whole - (shown * bar->whole);
4705 portion = min (top * bar->whole, whole);
4706 }
4707 else
4708 {
4709 whole = bar->whole;
4710 portion = 0;
4711 }
4712
4713 part = scroll_bar_horizontal_handle;
4714 }
4715 else
4716 {
4717 /* Get the size of the thumb, a value between 0 and 1. */
4718 block_input ();
4719 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
4720 unblock_input ();
4721
4722 whole = 10000000;
4723 portion = shown < 1 ? top * whole : 0;
4724
4725 if (shown < 1 && (eabs (top + shown - 1) < 1.0f / height))
4726 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
4727 the bottom, so we force the scrolling whenever we see that we're
4728 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
4729 we try to ensure that we always stay two pixels away from the
4730 bottom). */
4731 part = scroll_bar_down_arrow;
4732 else
4733 part = scroll_bar_handle;
4734 }
4735
4736 window_being_scrolled = bar->window;
4737 bar->dragging = portion;
4738 bar->last_seen_part = part;
4739 x_send_scroll_bar_event (bar->window, part, portion, whole, bar->horizontal);
4740 }
4741
4742
4743 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
4744 i.e. line or page up or down. WIDGET is the Xaw scroll bar
4745 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
4746 the scroll bar. CALL_DATA is an integer specifying the action that
4747 has taken place. Its magnitude is in the range 0..height of the
4748 scroll bar. Negative values mean scroll towards buffer start.
4749 Values < height of scroll bar mean line-wise movement. */
4750
4751 static void
4752 xaw_scroll_callback (Widget widget, XtPointer client_data, XtPointer call_data)
4753 {
4754 struct scroll_bar *bar = client_data;
4755 /* The position really is stored cast to a pointer. */
4756 int position = (intptr_t) call_data;
4757 Dimension height, width;
4758 enum scroll_bar_part part;
4759
4760 if (bar->horizontal)
4761 {
4762 /* Get the width of the scroll bar. */
4763 block_input ();
4764 XtVaGetValues (widget, XtNwidth, &width, NULL);
4765 unblock_input ();
4766
4767 if (eabs (position) >= width)
4768 part = (position < 0) ? scroll_bar_before_handle : scroll_bar_after_handle;
4769
4770 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
4771 it maps line-movement to call_data = max(5, height/20). */
4772 else if (xaw3d_arrow_scroll && eabs (position) <= max (5, width / 20))
4773 part = (position < 0) ? scroll_bar_left_arrow : scroll_bar_right_arrow;
4774 else
4775 part = scroll_bar_move_ratio;
4776
4777 window_being_scrolled = bar->window;
4778 bar->dragging = -1;
4779 bar->last_seen_part = part;
4780 x_send_scroll_bar_event (bar->window, part, position, width,
4781 bar->horizontal);
4782 }
4783 else
4784 {
4785
4786 /* Get the height of the scroll bar. */
4787 block_input ();
4788 XtVaGetValues (widget, XtNheight, &height, NULL);
4789 unblock_input ();
4790
4791 if (eabs (position) >= height)
4792 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
4793
4794 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
4795 it maps line-movement to call_data = max(5, height/20). */
4796 else if (xaw3d_arrow_scroll && eabs (position) <= max (5, height / 20))
4797 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
4798 else
4799 part = scroll_bar_move_ratio;
4800
4801 window_being_scrolled = bar->window;
4802 bar->dragging = -1;
4803 bar->last_seen_part = part;
4804 x_send_scroll_bar_event (bar->window, part, position, height,
4805 bar->horizontal);
4806 }
4807 }
4808
4809 #endif /* not USE_GTK and not USE_MOTIF */
4810
4811 #define SCROLL_BAR_NAME "verticalScrollBar"
4812 #define SCROLL_BAR_HORIZONTAL_NAME "horizontalScrollBar"
4813
4814 /* Create the widget for scroll bar BAR on frame F. Record the widget
4815 and X window of the scroll bar in BAR. */
4816
4817 #ifdef USE_GTK
4818 static void
4819 x_create_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
4820 {
4821 const char *scroll_bar_name = SCROLL_BAR_NAME;
4822
4823 block_input ();
4824 xg_create_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
4825 G_CALLBACK (xg_end_scroll_callback),
4826 scroll_bar_name);
4827 unblock_input ();
4828 }
4829
4830 static void
4831 x_create_horizontal_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
4832 {
4833 const char *scroll_bar_name = SCROLL_BAR_HORIZONTAL_NAME;
4834
4835 block_input ();
4836 xg_create_horizontal_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
4837 G_CALLBACK (xg_end_scroll_callback),
4838 scroll_bar_name);
4839 unblock_input ();
4840 }
4841
4842 #else /* not USE_GTK */
4843
4844 static void
4845 x_create_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
4846 {
4847 Window xwindow;
4848 Widget widget;
4849 Arg av[20];
4850 int ac = 0;
4851 const char *scroll_bar_name = SCROLL_BAR_NAME;
4852 unsigned long pixel;
4853
4854 block_input ();
4855
4856 #ifdef USE_MOTIF
4857 /* Set resources. Create the widget. */
4858 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4859 XtSetArg (av[ac], XmNminimum, 0); ++ac;
4860 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
4861 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
4862 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
4863 XtSetArg (av[ac], XmNincrement, 1); ++ac;
4864 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
4865
4866 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4867 if (pixel != -1)
4868 {
4869 XtSetArg (av[ac], XmNforeground, pixel);
4870 ++ac;
4871 }
4872
4873 pixel = f->output_data.x->scroll_bar_background_pixel;
4874 if (pixel != -1)
4875 {
4876 XtSetArg (av[ac], XmNbackground, pixel);
4877 ++ac;
4878 }
4879
4880 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
4881 (char *) scroll_bar_name, av, ac);
4882
4883 /* Add one callback for everything that can happen. */
4884 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
4885 (XtPointer) bar);
4886 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
4887 (XtPointer) bar);
4888 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
4889 (XtPointer) bar);
4890 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
4891 (XtPointer) bar);
4892 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
4893 (XtPointer) bar);
4894 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
4895 (XtPointer) bar);
4896 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
4897 (XtPointer) bar);
4898
4899 /* Realize the widget. Only after that is the X window created. */
4900 XtRealizeWidget (widget);
4901
4902 /* Set the cursor to an arrow. I didn't find a resource to do that.
4903 And I'm wondering why it hasn't an arrow cursor by default. */
4904 XDefineCursor (XtDisplay (widget), XtWindow (widget),
4905 f->output_data.x->nontext_cursor);
4906
4907 #else /* !USE_MOTIF i.e. use Xaw */
4908
4909 /* Set resources. Create the widget. The background of the
4910 Xaw3d scroll bar widget is a little bit light for my taste.
4911 We don't alter it here to let users change it according
4912 to their taste with `emacs*verticalScrollBar.background: xxx'. */
4913 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4914 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
4915 /* For smoother scrolling with Xaw3d -sm */
4916 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
4917
4918 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4919 if (pixel != -1)
4920 {
4921 XtSetArg (av[ac], XtNforeground, pixel);
4922 ++ac;
4923 }
4924
4925 pixel = f->output_data.x->scroll_bar_background_pixel;
4926 if (pixel != -1)
4927 {
4928 XtSetArg (av[ac], XtNbackground, pixel);
4929 ++ac;
4930 }
4931
4932 /* Top/bottom shadow colors. */
4933
4934 /* Allocate them, if necessary. */
4935 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
4936 {
4937 pixel = f->output_data.x->scroll_bar_background_pixel;
4938 if (pixel != -1)
4939 {
4940 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
4941 FRAME_X_COLORMAP (f),
4942 &pixel, 1.2, 0x8000))
4943 pixel = -1;
4944 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
4945 }
4946 }
4947 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4948 {
4949 pixel = f->output_data.x->scroll_bar_background_pixel;
4950 if (pixel != -1)
4951 {
4952 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
4953 FRAME_X_COLORMAP (f),
4954 &pixel, 0.6, 0x4000))
4955 pixel = -1;
4956 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
4957 }
4958 }
4959
4960 #ifdef XtNbeNiceToColormap
4961 /* Tell the toolkit about them. */
4962 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
4963 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4964 /* We tried to allocate a color for the top/bottom shadow, and
4965 failed, so tell Xaw3d to use dithering instead. */
4966 /* But only if we have a small colormap. Xaw3d can allocate nice
4967 colors itself. */
4968 {
4969 XtSetArg (av[ac], XtNbeNiceToColormap,
4970 DefaultDepthOfScreen (FRAME_X_SCREEN (f)) < 16);
4971 ++ac;
4972 }
4973 else
4974 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
4975 be more consistent with other emacs 3d colors, and since Xaw3d is
4976 not good at dealing with allocation failure. */
4977 {
4978 /* This tells Xaw3d to use real colors instead of dithering for
4979 the shadows. */
4980 XtSetArg (av[ac], XtNbeNiceToColormap, False);
4981 ++ac;
4982
4983 /* Specify the colors. */
4984 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
4985 if (pixel != -1)
4986 {
4987 XtSetArg (av[ac], XtNtopShadowPixel, pixel);
4988 ++ac;
4989 }
4990 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
4991 if (pixel != -1)
4992 {
4993 XtSetArg (av[ac], XtNbottomShadowPixel, pixel);
4994 ++ac;
4995 }
4996 }
4997 #endif
4998
4999 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
5000 f->output_data.x->edit_widget, av, ac);
5001
5002 {
5003 char const *initial = "";
5004 char const *val = initial;
5005 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
5006 #ifdef XtNarrowScrollbars
5007 XtNarrowScrollbars, (XtPointer) &xaw3d_arrow_scroll,
5008 #endif
5009 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
5010 if (xaw3d_arrow_scroll || val == initial)
5011 { /* ARROW_SCROLL */
5012 xaw3d_arrow_scroll = True;
5013 /* Isn't that just a personal preference ? --Stef */
5014 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
5015 }
5016 }
5017
5018 /* Define callbacks. */
5019 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
5020 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
5021 (XtPointer) bar);
5022
5023 /* Realize the widget. Only after that is the X window created. */
5024 XtRealizeWidget (widget);
5025
5026 #endif /* !USE_MOTIF */
5027
5028 /* Install an action hook that lets us detect when the user
5029 finishes interacting with a scroll bar. */
5030 if (action_hook_id == 0)
5031 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
5032
5033 /* Remember X window and widget in the scroll bar vector. */
5034 SET_SCROLL_BAR_X_WIDGET (bar, widget);
5035 xwindow = XtWindow (widget);
5036 bar->x_window = xwindow;
5037 bar->whole = 1;
5038 bar->horizontal = false;
5039
5040 unblock_input ();
5041 }
5042
5043 static void
5044 x_create_horizontal_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
5045 {
5046 Window xwindow;
5047 Widget widget;
5048 Arg av[20];
5049 int ac = 0;
5050 const char *scroll_bar_name = SCROLL_BAR_HORIZONTAL_NAME;
5051 unsigned long pixel;
5052
5053 block_input ();
5054
5055 #ifdef USE_MOTIF
5056 /* Set resources. Create the widget. */
5057 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
5058 XtSetArg (av[ac], XmNminimum, 0); ++ac;
5059 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
5060 XtSetArg (av[ac], XmNorientation, XmHORIZONTAL); ++ac;
5061 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_RIGHT), ++ac;
5062 XtSetArg (av[ac], XmNincrement, 1); ++ac;
5063 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
5064
5065 pixel = f->output_data.x->scroll_bar_foreground_pixel;
5066 if (pixel != -1)
5067 {
5068 XtSetArg (av[ac], XmNforeground, pixel);
5069 ++ac;
5070 }
5071
5072 pixel = f->output_data.x->scroll_bar_background_pixel;
5073 if (pixel != -1)
5074 {
5075 XtSetArg (av[ac], XmNbackground, pixel);
5076 ++ac;
5077 }
5078
5079 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
5080 (char *) scroll_bar_name, av, ac);
5081
5082 /* Add one callback for everything that can happen. */
5083 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
5084 (XtPointer) bar);
5085 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
5086 (XtPointer) bar);
5087 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
5088 (XtPointer) bar);
5089 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
5090 (XtPointer) bar);
5091 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
5092 (XtPointer) bar);
5093 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
5094 (XtPointer) bar);
5095 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
5096 (XtPointer) bar);
5097
5098 /* Realize the widget. Only after that is the X window created. */
5099 XtRealizeWidget (widget);
5100
5101 /* Set the cursor to an arrow. I didn't find a resource to do that.
5102 And I'm wondering why it hasn't an arrow cursor by default. */
5103 XDefineCursor (XtDisplay (widget), XtWindow (widget),
5104 f->output_data.x->nontext_cursor);
5105
5106 #else /* !USE_MOTIF i.e. use Xaw */
5107
5108 /* Set resources. Create the widget. The background of the
5109 Xaw3d scroll bar widget is a little bit light for my taste.
5110 We don't alter it here to let users change it according
5111 to their taste with `emacs*verticalScrollBar.background: xxx'. */
5112 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
5113 XtSetArg (av[ac], XtNorientation, XtorientHorizontal); ++ac;
5114 /* For smoother scrolling with Xaw3d -sm */
5115 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
5116
5117 pixel = f->output_data.x->scroll_bar_foreground_pixel;
5118 if (pixel != -1)
5119 {
5120 XtSetArg (av[ac], XtNforeground, pixel);
5121 ++ac;
5122 }
5123
5124 pixel = f->output_data.x->scroll_bar_background_pixel;
5125 if (pixel != -1)
5126 {
5127 XtSetArg (av[ac], XtNbackground, pixel);
5128 ++ac;
5129 }
5130
5131 /* Top/bottom shadow colors. */
5132
5133 /* Allocate them, if necessary. */
5134 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
5135 {
5136 pixel = f->output_data.x->scroll_bar_background_pixel;
5137 if (pixel != -1)
5138 {
5139 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
5140 FRAME_X_COLORMAP (f),
5141 &pixel, 1.2, 0x8000))
5142 pixel = -1;
5143 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
5144 }
5145 }
5146 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
5147 {
5148 pixel = f->output_data.x->scroll_bar_background_pixel;
5149 if (pixel != -1)
5150 {
5151 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
5152 FRAME_X_COLORMAP (f),
5153 &pixel, 0.6, 0x4000))
5154 pixel = -1;
5155 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
5156 }
5157 }
5158
5159 #ifdef XtNbeNiceToColormap
5160 /* Tell the toolkit about them. */
5161 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
5162 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
5163 /* We tried to allocate a color for the top/bottom shadow, and
5164 failed, so tell Xaw3d to use dithering instead. */
5165 /* But only if we have a small colormap. Xaw3d can allocate nice
5166 colors itself. */
5167 {
5168 XtSetArg (av[ac], XtNbeNiceToColormap,
5169 DefaultDepthOfScreen (FRAME_X_SCREEN (f)) < 16);
5170 ++ac;
5171 }
5172 else
5173 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
5174 be more consistent with other emacs 3d colors, and since Xaw3d is
5175 not good at dealing with allocation failure. */
5176 {
5177 /* This tells Xaw3d to use real colors instead of dithering for
5178 the shadows. */
5179 XtSetArg (av[ac], XtNbeNiceToColormap, False);
5180 ++ac;
5181
5182 /* Specify the colors. */
5183 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
5184 if (pixel != -1)
5185 {
5186 XtSetArg (av[ac], XtNtopShadowPixel, pixel);
5187 ++ac;
5188 }
5189 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
5190 if (pixel != -1)
5191 {
5192 XtSetArg (av[ac], XtNbottomShadowPixel, pixel);
5193 ++ac;
5194 }
5195 }
5196 #endif
5197
5198 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
5199 f->output_data.x->edit_widget, av, ac);
5200
5201 {
5202 char const *initial = "";
5203 char const *val = initial;
5204 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
5205 #ifdef XtNarrowScrollbars
5206 XtNarrowScrollbars, (XtPointer) &xaw3d_arrow_scroll,
5207 #endif
5208 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
5209 if (xaw3d_arrow_scroll || val == initial)
5210 { /* ARROW_SCROLL */
5211 xaw3d_arrow_scroll = True;
5212 /* Isn't that just a personal preference ? --Stef */
5213 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
5214 }
5215 }
5216
5217 /* Define callbacks. */
5218 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
5219 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
5220 (XtPointer) bar);
5221
5222 /* Realize the widget. Only after that is the X window created. */
5223 XtRealizeWidget (widget);
5224
5225 #endif /* !USE_MOTIF */
5226
5227 /* Install an action hook that lets us detect when the user
5228 finishes interacting with a scroll bar. */
5229 if (horizontal_action_hook_id == 0)
5230 horizontal_action_hook_id
5231 = XtAppAddActionHook (Xt_app_con, xt_horizontal_action_hook, 0);
5232
5233 /* Remember X window and widget in the scroll bar vector. */
5234 SET_SCROLL_BAR_X_WIDGET (bar, widget);
5235 xwindow = XtWindow (widget);
5236 bar->x_window = xwindow;
5237 bar->whole = 1;
5238 bar->horizontal = true;
5239
5240 unblock_input ();
5241 }
5242 #endif /* not USE_GTK */
5243
5244
5245 /* Set the thumb size and position of scroll bar BAR. We are currently
5246 displaying PORTION out of a whole WHOLE, and our position POSITION. */
5247
5248 #ifdef USE_GTK
5249 static void
5250 x_set_toolkit_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position, int whole)
5251 {
5252 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
5253 }
5254
5255 static void
5256 x_set_toolkit_horizontal_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position, int whole)
5257 {
5258 xg_set_toolkit_horizontal_scroll_bar_thumb (bar, portion, position, whole);
5259 }
5260
5261 #else /* not USE_GTK */
5262 static void
5263 x_set_toolkit_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position,
5264 int whole)
5265 {
5266 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5267 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
5268 float top, shown;
5269
5270 block_input ();
5271
5272 #ifdef USE_MOTIF
5273
5274 if (scroll_bar_adjust_thumb_portion_p)
5275 {
5276 /* We use an estimate of 30 chars per line rather than the real
5277 `portion' value. This has the disadvantage that the thumb size
5278 is not very representative, but it makes our life a lot easier.
5279 Otherwise, we have to constantly adjust the thumb size, which
5280 we can't always do quickly enough: while dragging, the size of
5281 the thumb might prevent the user from dragging the thumb all the
5282 way to the end. but Motif and some versions of Xaw3d don't allow
5283 updating the thumb size while dragging. Also, even if we can update
5284 its size, the update will often happen too late.
5285 If you don't believe it, check out revision 1.650 of xterm.c to see
5286 what hoops we were going through and the still poor behavior we got. */
5287 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
5288 /* When the thumb is at the bottom, position == whole.
5289 So we need to increase `whole' to make space for the thumb. */
5290 whole += portion;
5291 }
5292
5293 if (whole <= 0)
5294 top = 0, shown = 1;
5295 else
5296 {
5297 top = (float) position / whole;
5298 shown = (float) portion / whole;
5299 }
5300
5301 if (bar->dragging == -1)
5302 {
5303 int size, value;
5304
5305 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
5306 is the scroll bar's maximum and MIN is the scroll bar's minimum
5307 value. */
5308 size = clip_to_bounds (1, shown * XM_SB_MAX, XM_SB_MAX);
5309
5310 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
5311 value = top * XM_SB_MAX;
5312 value = min (value, XM_SB_MAX - size);
5313
5314 XmScrollBarSetValues (widget, value, size, 0, 0, False);
5315 }
5316 #else /* !USE_MOTIF i.e. use Xaw */
5317
5318 if (whole == 0)
5319 top = 0, shown = 1;
5320 else
5321 {
5322 top = (float) position / whole;
5323 shown = (float) portion / whole;
5324 }
5325
5326 {
5327 float old_top, old_shown;
5328 Dimension height;
5329 XtVaGetValues (widget,
5330 XtNtopOfThumb, &old_top,
5331 XtNshown, &old_shown,
5332 XtNheight, &height,
5333 NULL);
5334
5335 /* Massage the top+shown values. */
5336 if (bar->dragging == -1 || bar->last_seen_part == scroll_bar_down_arrow)
5337 top = max (0, min (1, top));
5338 else
5339 top = old_top;
5340 #if ! defined (HAVE_XAW3D)
5341 /* With Xaw, 'top' values too closer to 1.0 may
5342 cause the thumb to disappear. Fix that. */
5343 top = min (top, 0.99f);
5344 #endif
5345 /* Keep two pixels available for moving the thumb down. */
5346 shown = max (0, min (1 - top - (2.0f / height), shown));
5347 #if ! defined (HAVE_XAW3D)
5348 /* Likewise with too small 'shown'. */
5349 shown = max (shown, 0.01f);
5350 #endif
5351
5352 /* If the call to XawScrollbarSetThumb below doesn't seem to
5353 work, check that 'NARROWPROTO' is defined in src/config.h.
5354 If this is not so, most likely you need to fix configure. */
5355 if (top != old_top || shown != old_shown)
5356 {
5357 if (bar->dragging == -1)
5358 XawScrollbarSetThumb (widget, top, shown);
5359 else
5360 {
5361 /* Try to make the scrolling a tad smoother. */
5362 if (!xaw3d_pick_top)
5363 shown = min (shown, old_shown);
5364
5365 XawScrollbarSetThumb (widget, top, shown);
5366 }
5367 }
5368 }
5369 #endif /* !USE_MOTIF */
5370
5371 unblock_input ();
5372 }
5373
5374 static void
5375 x_set_toolkit_horizontal_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position,
5376 int whole)
5377 {
5378 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5379 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
5380 float top, shown;
5381
5382 block_input ();
5383
5384 #ifdef USE_MOTIF
5385 bar->whole = whole;
5386 shown = (float) portion / whole;
5387 top = (float) position / (whole - portion);
5388 {
5389 int size = clip_to_bounds (1, shown * XM_SB_MAX, XM_SB_MAX);
5390 int value = clip_to_bounds (0, top * (XM_SB_MAX - size), XM_SB_MAX - size);
5391
5392 XmScrollBarSetValues (widget, value, size, 0, 0, False);
5393 }
5394 #else /* !USE_MOTIF i.e. use Xaw */
5395 bar->whole = whole;
5396 if (whole == 0)
5397 top = 0, shown = 1;
5398 else
5399 {
5400 top = (float) position / whole;
5401 shown = (float) portion / whole;
5402 }
5403
5404 {
5405 float old_top, old_shown;
5406 Dimension height;
5407 XtVaGetValues (widget,
5408 XtNtopOfThumb, &old_top,
5409 XtNshown, &old_shown,
5410 XtNheight, &height,
5411 NULL);
5412
5413 #if false
5414 /* Massage the top+shown values. */
5415 if (bar->dragging == -1 || bar->last_seen_part == scroll_bar_down_arrow)
5416 top = max (0, min (1, top));
5417 else
5418 top = old_top;
5419 #if ! defined (HAVE_XAW3D)
5420 /* With Xaw, 'top' values too closer to 1.0 may
5421 cause the thumb to disappear. Fix that. */
5422 top = min (top, 0.99f);
5423 #endif
5424 /* Keep two pixels available for moving the thumb down. */
5425 shown = max (0, min (1 - top - (2.0f / height), shown));
5426 #if ! defined (HAVE_XAW3D)
5427 /* Likewise with too small 'shown'. */
5428 shown = max (shown, 0.01f);
5429 #endif
5430 #endif
5431
5432 /* If the call to XawScrollbarSetThumb below doesn't seem to
5433 work, check that 'NARROWPROTO' is defined in src/config.h.
5434 If this is not so, most likely you need to fix configure. */
5435 XawScrollbarSetThumb (widget, top, shown);
5436 #if false
5437 if (top != old_top || shown != old_shown)
5438 {
5439 if (bar->dragging == -1)
5440 XawScrollbarSetThumb (widget, top, shown);
5441 else
5442 {
5443 /* Try to make the scrolling a tad smoother. */
5444 if (!xaw3d_pick_top)
5445 shown = min (shown, old_shown);
5446
5447 XawScrollbarSetThumb (widget, top, shown);
5448 }
5449 }
5450 #endif
5451 }
5452 #endif /* !USE_MOTIF */
5453
5454 unblock_input ();
5455 }
5456 #endif /* not USE_GTK */
5457
5458 #endif /* USE_TOOLKIT_SCROLL_BARS */
5459
5460
5461 \f
5462 /************************************************************************
5463 Scroll bars, general
5464 ************************************************************************/
5465
5466 /* Create a scroll bar and return the scroll bar vector for it. W is
5467 the Emacs window on which to create the scroll bar. TOP, LEFT,
5468 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
5469 scroll bar. */
5470
5471 static struct scroll_bar *
5472 x_scroll_bar_create (struct window *w, int top, int left,
5473 int width, int height, bool horizontal)
5474 {
5475 struct frame *f = XFRAME (w->frame);
5476 struct scroll_bar *bar
5477 = ALLOCATE_PSEUDOVECTOR (struct scroll_bar, x_window, PVEC_OTHER);
5478 Lisp_Object barobj;
5479
5480 block_input ();
5481
5482 #ifdef USE_TOOLKIT_SCROLL_BARS
5483 if (horizontal)
5484 x_create_horizontal_toolkit_scroll_bar (f, bar);
5485 else
5486 x_create_toolkit_scroll_bar (f, bar);
5487 #else /* not USE_TOOLKIT_SCROLL_BARS */
5488 {
5489 XSetWindowAttributes a;
5490 unsigned long mask;
5491 Window window;
5492
5493 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
5494 if (a.background_pixel == -1)
5495 a.background_pixel = FRAME_BACKGROUND_PIXEL (f);
5496
5497 a.event_mask = (ButtonPressMask | ButtonReleaseMask
5498 | ButtonMotionMask | PointerMotionHintMask
5499 | ExposureMask);
5500 a.cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
5501
5502 mask = (CWBackPixel | CWEventMask | CWCursor);
5503
5504 /* Clear the area of W that will serve as a scroll bar. This is
5505 for the case that a window has been split horizontally. In
5506 this case, no clear_frame is generated to reduce flickering. */
5507 if (width > 0 && window_box_height (w) > 0)
5508 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5509 left, top, width, window_box_height (w));
5510
5511 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5512 /* Position and size of scroll bar. */
5513 left, top, width, height,
5514 /* Border width, depth, class, and visual. */
5515 0,
5516 CopyFromParent,
5517 CopyFromParent,
5518 CopyFromParent,
5519 /* Attributes. */
5520 mask, &a);
5521 bar->x_window = window;
5522 }
5523 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5524
5525 XSETWINDOW (bar->window, w);
5526 bar->top = top;
5527 bar->left = left;
5528 bar->width = width;
5529 bar->height = height;
5530 bar->start = 0;
5531 bar->end = 0;
5532 bar->dragging = -1;
5533 bar->horizontal = horizontal;
5534 #if defined (USE_TOOLKIT_SCROLL_BARS) && defined (USE_LUCID)
5535 bar->last_seen_part = scroll_bar_nowhere;
5536 #endif
5537
5538 /* Add bar to its frame's list of scroll bars. */
5539 bar->next = FRAME_SCROLL_BARS (f);
5540 bar->prev = Qnil;
5541 XSETVECTOR (barobj, bar);
5542 fset_scroll_bars (f, barobj);
5543 if (!NILP (bar->next))
5544 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
5545
5546 /* Map the window/widget. */
5547 #ifdef USE_TOOLKIT_SCROLL_BARS
5548 {
5549 #ifdef USE_GTK
5550 if (horizontal)
5551 xg_update_horizontal_scrollbar_pos (f, bar->x_window, top,
5552 left, width, max (height, 1));
5553 else
5554 xg_update_scrollbar_pos (f, bar->x_window, top,
5555 left, width, max (height, 1));
5556 #else /* not USE_GTK */
5557 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
5558 XtConfigureWidget (scroll_bar, left, top, width, max (height, 1), 0);
5559 XtMapWidget (scroll_bar);
5560 #endif /* not USE_GTK */
5561 }
5562 #else /* not USE_TOOLKIT_SCROLL_BARS */
5563 XMapRaised (FRAME_X_DISPLAY (f), bar->x_window);
5564 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5565
5566 unblock_input ();
5567 return bar;
5568 }
5569
5570
5571 #ifndef USE_TOOLKIT_SCROLL_BARS
5572
5573 /* Draw BAR's handle in the proper position.
5574
5575 If the handle is already drawn from START to END, don't bother
5576 redrawing it, unless REBUILD; in that case, always
5577 redraw it. (REBUILD is handy for drawing the handle after expose
5578 events.)
5579
5580 Normally, we want to constrain the start and end of the handle to
5581 fit inside its rectangle, but if the user is dragging the scroll
5582 bar handle, we want to let them drag it down all the way, so that
5583 the bar's top is as far down as it goes; otherwise, there's no way
5584 to move to the very end of the buffer. */
5585
5586 static void
5587 x_scroll_bar_set_handle (struct scroll_bar *bar, int start, int end,
5588 bool rebuild)
5589 {
5590 bool dragging = bar->dragging != -1;
5591 Window w = bar->x_window;
5592 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5593 GC gc = f->output_data.x->normal_gc;
5594
5595 /* If the display is already accurate, do nothing. */
5596 if (! rebuild
5597 && start == bar->start
5598 && end == bar->end)
5599 return;
5600
5601 block_input ();
5602
5603 {
5604 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, bar->width);
5605 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, bar->height);
5606 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
5607
5608 /* Make sure the values are reasonable, and try to preserve
5609 the distance between start and end. */
5610 {
5611 int length = end - start;
5612
5613 if (start < 0)
5614 start = 0;
5615 else if (start > top_range)
5616 start = top_range;
5617 end = start + length;
5618
5619 if (end < start)
5620 end = start;
5621 else if (end > top_range && ! dragging)
5622 end = top_range;
5623 }
5624
5625 /* Store the adjusted setting in the scroll bar. */
5626 bar->start = start;
5627 bar->end = end;
5628
5629 /* Clip the end position, just for display. */
5630 if (end > top_range)
5631 end = top_range;
5632
5633 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
5634 below top positions, to make sure the handle is always at least
5635 that many pixels tall. */
5636 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
5637
5638 /* Draw the empty space above the handle. Note that we can't clear
5639 zero-height areas; that means "clear to end of window." */
5640 if ((inside_width > 0) && (start > 0))
5641 x_clear_area (FRAME_X_DISPLAY (f), w,
5642 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5643 VERTICAL_SCROLL_BAR_TOP_BORDER,
5644 inside_width, start);
5645
5646 /* Change to proper foreground color if one is specified. */
5647 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5648 XSetForeground (FRAME_X_DISPLAY (f), gc,
5649 f->output_data.x->scroll_bar_foreground_pixel);
5650
5651 /* Draw the handle itself. */
5652 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
5653 /* x, y, width, height */
5654 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5655 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
5656 inside_width, end - start);
5657
5658 /* Restore the foreground color of the GC if we changed it above. */
5659 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5660 XSetForeground (FRAME_X_DISPLAY (f), gc,
5661 FRAME_FOREGROUND_PIXEL (f));
5662
5663 /* Draw the empty space below the handle. Note that we can't
5664 clear zero-height areas; that means "clear to end of window." */
5665 if ((inside_width > 0) && (end < inside_height))
5666 x_clear_area (FRAME_X_DISPLAY (f), w,
5667 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5668 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
5669 inside_width, inside_height - end);
5670 }
5671
5672 unblock_input ();
5673 }
5674
5675 #endif /* !USE_TOOLKIT_SCROLL_BARS */
5676
5677 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
5678 nil. */
5679
5680 static void
5681 x_scroll_bar_remove (struct scroll_bar *bar)
5682 {
5683 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5684 block_input ();
5685
5686 #ifdef USE_TOOLKIT_SCROLL_BARS
5687 #ifdef USE_GTK
5688 xg_remove_scroll_bar (f, bar->x_window);
5689 #else /* not USE_GTK */
5690 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
5691 #endif /* not USE_GTK */
5692 #else
5693 XDestroyWindow (FRAME_X_DISPLAY (f), bar->x_window);
5694 #endif
5695
5696 /* Dissociate this scroll bar from its window. */
5697 if (bar->horizontal)
5698 wset_horizontal_scroll_bar (XWINDOW (bar->window), Qnil);
5699 else
5700 wset_vertical_scroll_bar (XWINDOW (bar->window), Qnil);
5701
5702 unblock_input ();
5703 }
5704
5705
5706 /* Set the handle of the vertical scroll bar for WINDOW to indicate
5707 that we are displaying PORTION characters out of a total of WHOLE
5708 characters, starting at POSITION. If WINDOW has no scroll bar,
5709 create one. */
5710
5711 static void
5712 XTset_vertical_scroll_bar (struct window *w, int portion, int whole, int position)
5713 {
5714 struct frame *f = XFRAME (w->frame);
5715 Lisp_Object barobj;
5716 struct scroll_bar *bar;
5717 int top, height, left, width;
5718 int window_y, window_height;
5719
5720 /* Get window dimensions. */
5721 window_box (w, ANY_AREA, 0, &window_y, 0, &window_height);
5722 top = window_y;
5723 height = window_height;
5724 left = WINDOW_SCROLL_BAR_AREA_X (w);
5725 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
5726
5727 /* Does the scroll bar exist yet? */
5728 if (NILP (w->vertical_scroll_bar))
5729 {
5730 if (width > 0 && height > 0)
5731 {
5732 block_input ();
5733 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5734 left, top, width, height);
5735 unblock_input ();
5736 }
5737
5738 bar = x_scroll_bar_create (w, top, left, width, max (height, 1), false);
5739 }
5740 else
5741 {
5742 /* It may just need to be moved and resized. */
5743 unsigned int mask = 0;
5744
5745 bar = XSCROLL_BAR (w->vertical_scroll_bar);
5746
5747 block_input ();
5748
5749 if (left != bar->left)
5750 mask |= CWX;
5751 if (top != bar->top)
5752 mask |= CWY;
5753 if (width != bar->width)
5754 mask |= CWWidth;
5755 if (height != bar->height)
5756 mask |= CWHeight;
5757
5758 #ifdef USE_TOOLKIT_SCROLL_BARS
5759
5760 /* Move/size the scroll bar widget. */
5761 if (mask)
5762 {
5763 /* Since toolkit scroll bars are smaller than the space reserved
5764 for them on the frame, we have to clear "under" them. */
5765 if (width > 0 && height > 0)
5766 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5767 left, top, width, height);
5768 #ifdef USE_GTK
5769 xg_update_scrollbar_pos (f, bar->x_window, top,
5770 left, width, max (height, 1));
5771 #else /* not USE_GTK */
5772 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
5773 left, top, width, max (height, 1), 0);
5774 #endif /* not USE_GTK */
5775 }
5776 #else /* not USE_TOOLKIT_SCROLL_BARS */
5777
5778 /* Move/size the scroll bar window. */
5779 if (mask)
5780 {
5781 XWindowChanges wc;
5782
5783 wc.x = left;
5784 wc.y = top;
5785 wc.width = width;
5786 wc.height = height;
5787 XConfigureWindow (FRAME_X_DISPLAY (f), bar->x_window,
5788 mask, &wc);
5789 }
5790
5791 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5792
5793 /* Remember new settings. */
5794 bar->left = left;
5795 bar->top = top;
5796 bar->width = width;
5797 bar->height = height;
5798
5799 unblock_input ();
5800 }
5801
5802 #ifdef USE_TOOLKIT_SCROLL_BARS
5803 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
5804 #else /* not USE_TOOLKIT_SCROLL_BARS */
5805 /* Set the scroll bar's current state, unless we're currently being
5806 dragged. */
5807 if (bar->dragging == -1)
5808 {
5809 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
5810
5811 if (whole == 0)
5812 x_scroll_bar_set_handle (bar, 0, top_range, false);
5813 else
5814 {
5815 int start = ((double) position * top_range) / whole;
5816 int end = ((double) (position + portion) * top_range) / whole;
5817 x_scroll_bar_set_handle (bar, start, end, false);
5818 }
5819 }
5820 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5821
5822 XSETVECTOR (barobj, bar);
5823 wset_vertical_scroll_bar (w, barobj);
5824 }
5825
5826
5827 static void
5828 XTset_horizontal_scroll_bar (struct window *w, int portion, int whole, int position)
5829 {
5830 struct frame *f = XFRAME (w->frame);
5831 Lisp_Object barobj;
5832 struct scroll_bar *bar;
5833 int top, height, left, width;
5834 int window_x, window_width;
5835 int pixel_width = WINDOW_PIXEL_WIDTH (w);
5836
5837 /* Get window dimensions. */
5838 window_box (w, ANY_AREA, &window_x, 0, &window_width, 0);
5839 left = window_x;
5840 width = window_width;
5841 top = WINDOW_SCROLL_BAR_AREA_Y (w);
5842 height = WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
5843
5844 /* Does the scroll bar exist yet? */
5845 if (NILP (w->horizontal_scroll_bar))
5846 {
5847 if (width > 0 && height > 0)
5848 {
5849 block_input ();
5850
5851 /* Clear also part between window_width and
5852 WINDOW_PIXEL_WIDTH. */
5853 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5854 left, top, pixel_width, height);
5855 unblock_input ();
5856 }
5857
5858 bar = x_scroll_bar_create (w, top, left, width, height, true);
5859 }
5860 else
5861 {
5862 /* It may just need to be moved and resized. */
5863 unsigned int mask = 0;
5864
5865 bar = XSCROLL_BAR (w->horizontal_scroll_bar);
5866
5867 block_input ();
5868
5869 if (left != bar->left)
5870 mask |= CWX;
5871 if (top != bar->top)
5872 mask |= CWY;
5873 if (width != bar->width)
5874 mask |= CWWidth;
5875 if (height != bar->height)
5876 mask |= CWHeight;
5877
5878 #ifdef USE_TOOLKIT_SCROLL_BARS
5879 /* Move/size the scroll bar widget. */
5880 if (mask)
5881 {
5882 /* Since toolkit scroll bars are smaller than the space reserved
5883 for them on the frame, we have to clear "under" them. */
5884 if (width > 0 && height > 0)
5885 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5886 WINDOW_LEFT_EDGE_X (w), top,
5887 pixel_width - WINDOW_RIGHT_DIVIDER_WIDTH (w), height);
5888 #ifdef USE_GTK
5889 xg_update_horizontal_scrollbar_pos (f, bar->x_window, top, left,
5890 width, height);
5891 #else /* not USE_GTK */
5892 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
5893 left, top, width, height, 0);
5894 #endif /* not USE_GTK */
5895 }
5896 #else /* not USE_TOOLKIT_SCROLL_BARS */
5897
5898 /* Clear areas not covered by the scroll bar because it's not as
5899 wide as the area reserved for it. This makes sure a
5900 previous mode line display is cleared after C-x 2 C-x 1, for
5901 example. */
5902 {
5903 int area_height = WINDOW_CONFIG_SCROLL_BAR_HEIGHT (w);
5904 int rest = area_height - height;
5905 if (rest > 0 && width > 0)
5906 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5907 left, top, width, rest);
5908 }
5909
5910 /* Move/size the scroll bar window. */
5911 if (mask)
5912 {
5913 XWindowChanges wc;
5914
5915 wc.x = left;
5916 wc.y = top;
5917 wc.width = width;
5918 wc.height = height;
5919 XConfigureWindow (FRAME_X_DISPLAY (f), bar->x_window,
5920 mask, &wc);
5921 }
5922
5923 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5924
5925 /* Remember new settings. */
5926 bar->left = left;
5927 bar->top = top;
5928 bar->width = width;
5929 bar->height = height;
5930
5931 unblock_input ();
5932 }
5933
5934 #ifdef USE_TOOLKIT_SCROLL_BARS
5935 x_set_toolkit_horizontal_scroll_bar_thumb (bar, portion, position, whole);
5936 #else /* not USE_TOOLKIT_SCROLL_BARS */
5937 /* Set the scroll bar's current state, unless we're currently being
5938 dragged. */
5939 if (bar->dragging == -1)
5940 {
5941 int left_range = HORIZONTAL_SCROLL_BAR_LEFT_RANGE (f, width);
5942
5943 if (whole == 0)
5944 x_scroll_bar_set_handle (bar, 0, left_range, false);
5945 else
5946 {
5947 int start = ((double) position * left_range) / whole;
5948 int end = ((double) (position + portion) * left_range) / whole;
5949 x_scroll_bar_set_handle (bar, start, end, false);
5950 }
5951 }
5952 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5953
5954 XSETVECTOR (barobj, bar);
5955 wset_horizontal_scroll_bar (w, barobj);
5956 }
5957
5958
5959 /* The following three hooks are used when we're doing a thorough
5960 redisplay of the frame. We don't explicitly know which scroll bars
5961 are going to be deleted, because keeping track of when windows go
5962 away is a real pain - "Can you say set-window-configuration, boys
5963 and girls?" Instead, we just assert at the beginning of redisplay
5964 that *all* scroll bars are to be removed, and then save a scroll bar
5965 from the fiery pit when we actually redisplay its window. */
5966
5967 /* Arrange for all scroll bars on FRAME to be removed at the next call
5968 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
5969 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
5970
5971 static void
5972 XTcondemn_scroll_bars (struct frame *frame)
5973 {
5974 if (!NILP (FRAME_SCROLL_BARS (frame)))
5975 {
5976 if (!NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
5977 {
5978 /* Prepend scrollbars to already condemned ones. */
5979 Lisp_Object last = FRAME_SCROLL_BARS (frame);
5980
5981 while (!NILP (XSCROLL_BAR (last)->next))
5982 last = XSCROLL_BAR (last)->next;
5983
5984 XSCROLL_BAR (last)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
5985 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = last;
5986 }
5987
5988 fset_condemned_scroll_bars (frame, FRAME_SCROLL_BARS (frame));
5989 fset_scroll_bars (frame, Qnil);
5990 }
5991 }
5992
5993
5994 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
5995 Note that WINDOW isn't necessarily condemned at all. */
5996
5997 static void
5998 XTredeem_scroll_bar (struct window *w)
5999 {
6000 struct scroll_bar *bar;
6001 Lisp_Object barobj;
6002 struct frame *f;
6003
6004 /* We can't redeem this window's scroll bar if it doesn't have one. */
6005 if (NILP (w->vertical_scroll_bar) && NILP (w->horizontal_scroll_bar))
6006 emacs_abort ();
6007
6008 if (!NILP (w->vertical_scroll_bar) && WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
6009 {
6010 bar = XSCROLL_BAR (w->vertical_scroll_bar);
6011 /* Unlink it from the condemned list. */
6012 f = XFRAME (WINDOW_FRAME (w));
6013 if (NILP (bar->prev))
6014 {
6015 /* If the prev pointer is nil, it must be the first in one of
6016 the lists. */
6017 if (EQ (FRAME_SCROLL_BARS (f), w->vertical_scroll_bar))
6018 /* It's not condemned. Everything's fine. */
6019 goto horizontal;
6020 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
6021 w->vertical_scroll_bar))
6022 fset_condemned_scroll_bars (f, bar->next);
6023 else
6024 /* If its prev pointer is nil, it must be at the front of
6025 one or the other! */
6026 emacs_abort ();
6027 }
6028 else
6029 XSCROLL_BAR (bar->prev)->next = bar->next;
6030
6031 if (! NILP (bar->next))
6032 XSCROLL_BAR (bar->next)->prev = bar->prev;
6033
6034 bar->next = FRAME_SCROLL_BARS (f);
6035 bar->prev = Qnil;
6036 XSETVECTOR (barobj, bar);
6037 fset_scroll_bars (f, barobj);
6038 if (! NILP (bar->next))
6039 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
6040 }
6041
6042 horizontal:
6043 if (!NILP (w->horizontal_scroll_bar) && WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
6044 {
6045 bar = XSCROLL_BAR (w->horizontal_scroll_bar);
6046 /* Unlink it from the condemned list. */
6047 f = XFRAME (WINDOW_FRAME (w));
6048 if (NILP (bar->prev))
6049 {
6050 /* If the prev pointer is nil, it must be the first in one of
6051 the lists. */
6052 if (EQ (FRAME_SCROLL_BARS (f), w->horizontal_scroll_bar))
6053 /* It's not condemned. Everything's fine. */
6054 return;
6055 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
6056 w->horizontal_scroll_bar))
6057 fset_condemned_scroll_bars (f, bar->next);
6058 else
6059 /* If its prev pointer is nil, it must be at the front of
6060 one or the other! */
6061 emacs_abort ();
6062 }
6063 else
6064 XSCROLL_BAR (bar->prev)->next = bar->next;
6065
6066 if (! NILP (bar->next))
6067 XSCROLL_BAR (bar->next)->prev = bar->prev;
6068
6069 bar->next = FRAME_SCROLL_BARS (f);
6070 bar->prev = Qnil;
6071 XSETVECTOR (barobj, bar);
6072 fset_scroll_bars (f, barobj);
6073 if (! NILP (bar->next))
6074 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
6075 }
6076 }
6077
6078 /* Remove all scroll bars on FRAME that haven't been saved since the
6079 last call to `*condemn_scroll_bars_hook'. */
6080
6081 static void
6082 XTjudge_scroll_bars (struct frame *f)
6083 {
6084 Lisp_Object bar, next;
6085
6086 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
6087
6088 /* Clear out the condemned list now so we won't try to process any
6089 more events on the hapless scroll bars. */
6090 fset_condemned_scroll_bars (f, Qnil);
6091
6092 for (; ! NILP (bar); bar = next)
6093 {
6094 struct scroll_bar *b = XSCROLL_BAR (bar);
6095
6096 x_scroll_bar_remove (b);
6097
6098 next = b->next;
6099 b->next = b->prev = Qnil;
6100 }
6101
6102 /* Now there should be no references to the condemned scroll bars,
6103 and they should get garbage-collected. */
6104 }
6105
6106
6107 #ifndef USE_TOOLKIT_SCROLL_BARS
6108 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
6109 is a no-op when using toolkit scroll bars.
6110
6111 This may be called from a signal handler, so we have to ignore GC
6112 mark bits. */
6113
6114 static void
6115 x_scroll_bar_expose (struct scroll_bar *bar, const XEvent *event)
6116 {
6117 Window w = bar->x_window;
6118 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
6119 GC gc = f->output_data.x->normal_gc;
6120
6121 block_input ();
6122
6123 x_scroll_bar_set_handle (bar, bar->start, bar->end, true);
6124
6125 /* Switch to scroll bar foreground color. */
6126 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
6127 XSetForeground (FRAME_X_DISPLAY (f), gc,
6128 f->output_data.x->scroll_bar_foreground_pixel);
6129
6130 /* Draw a one-pixel border just inside the edges of the scroll bar. */
6131 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
6132 /* x, y, width, height */
6133 0, 0, bar->width - 1, bar->height - 1);
6134
6135 /* Restore the foreground color of the GC if we changed it above. */
6136 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
6137 XSetForeground (FRAME_X_DISPLAY (f), gc,
6138 FRAME_FOREGROUND_PIXEL (f));
6139
6140 unblock_input ();
6141
6142 }
6143 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6144
6145 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
6146 is set to something other than NO_EVENT, it is enqueued.
6147
6148 This may be called from a signal handler, so we have to ignore GC
6149 mark bits. */
6150
6151
6152 static void
6153 x_scroll_bar_handle_click (struct scroll_bar *bar,
6154 const XEvent *event,
6155 struct input_event *emacs_event)
6156 {
6157 if (! WINDOWP (bar->window))
6158 emacs_abort ();
6159
6160 emacs_event->kind = (bar->horizontal
6161 ? HORIZONTAL_SCROLL_BAR_CLICK_EVENT
6162 : SCROLL_BAR_CLICK_EVENT);
6163 emacs_event->code = event->xbutton.button - Button1;
6164 emacs_event->modifiers
6165 = (x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO
6166 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
6167 event->xbutton.state)
6168 | (event->type == ButtonRelease
6169 ? up_modifier
6170 : down_modifier));
6171 emacs_event->frame_or_window = bar->window;
6172 emacs_event->arg = Qnil;
6173 emacs_event->timestamp = event->xbutton.time;
6174 if (bar->horizontal)
6175 {
6176 int left_range
6177 = HORIZONTAL_SCROLL_BAR_LEFT_RANGE (f, bar->width);
6178 int x = event->xbutton.x - HORIZONTAL_SCROLL_BAR_LEFT_BORDER;
6179
6180 if (x < 0) x = 0;
6181 if (x > left_range) x = left_range;
6182
6183 if (x < bar->start)
6184 emacs_event->part = scroll_bar_before_handle;
6185 else if (x < bar->end + HORIZONTAL_SCROLL_BAR_MIN_HANDLE)
6186 emacs_event->part = scroll_bar_horizontal_handle;
6187 else
6188 emacs_event->part = scroll_bar_after_handle;
6189
6190 #ifndef USE_TOOLKIT_SCROLL_BARS
6191 /* If the user has released the handle, set it to its final position. */
6192 if (event->type == ButtonRelease && bar->dragging != -1)
6193 {
6194 int new_start = - bar->dragging;
6195 int new_end = new_start + bar->end - bar->start;
6196
6197 x_scroll_bar_set_handle (bar, new_start, new_end, false);
6198 bar->dragging = -1;
6199 }
6200 #endif
6201
6202 XSETINT (emacs_event->x, left_range);
6203 XSETINT (emacs_event->y, x);
6204 }
6205 else
6206 {
6207 int top_range
6208 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
6209 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
6210
6211 if (y < 0) y = 0;
6212 if (y > top_range) y = top_range;
6213
6214 if (y < bar->start)
6215 emacs_event->part = scroll_bar_above_handle;
6216 else if (y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
6217 emacs_event->part = scroll_bar_handle;
6218 else
6219 emacs_event->part = scroll_bar_below_handle;
6220
6221 #ifndef USE_TOOLKIT_SCROLL_BARS
6222 /* If the user has released the handle, set it to its final position. */
6223 if (event->type == ButtonRelease && bar->dragging != -1)
6224 {
6225 int new_start = y - bar->dragging;
6226 int new_end = new_start + bar->end - bar->start;
6227
6228 x_scroll_bar_set_handle (bar, new_start, new_end, false);
6229 bar->dragging = -1;
6230 }
6231 #endif
6232
6233 XSETINT (emacs_event->x, y);
6234 XSETINT (emacs_event->y, top_range);
6235 }
6236 }
6237
6238 #ifndef USE_TOOLKIT_SCROLL_BARS
6239
6240 /* Handle some mouse motion while someone is dragging the scroll bar.
6241
6242 This may be called from a signal handler, so we have to ignore GC
6243 mark bits. */
6244
6245 static void
6246 x_scroll_bar_note_movement (struct scroll_bar *bar,
6247 const XMotionEvent *event)
6248 {
6249 struct frame *f = XFRAME (XWINDOW (bar->window)->frame);
6250 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
6251
6252 dpyinfo->last_mouse_movement_time = event->time;
6253 dpyinfo->last_mouse_scroll_bar = bar;
6254 f->mouse_moved = true;
6255
6256 /* If we're dragging the bar, display it. */
6257 if (bar->dragging != -1)
6258 {
6259 /* Where should the handle be now? */
6260 int new_start = event->y - bar->dragging;
6261
6262 if (new_start != bar->start)
6263 {
6264 int new_end = new_start + bar->end - bar->start;
6265
6266 x_scroll_bar_set_handle (bar, new_start, new_end, false);
6267 }
6268 }
6269 }
6270
6271 #endif /* !USE_TOOLKIT_SCROLL_BARS */
6272
6273 /* Return information to the user about the current position of the mouse
6274 on the scroll bar. */
6275
6276 static void
6277 x_scroll_bar_report_motion (struct frame **fp, Lisp_Object *bar_window,
6278 enum scroll_bar_part *part, Lisp_Object *x,
6279 Lisp_Object *y, Time *timestamp)
6280 {
6281 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
6282 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
6283 Window w = bar->x_window;
6284 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
6285 int win_x, win_y;
6286 Window dummy_window;
6287 int dummy_coord;
6288 unsigned int dummy_mask;
6289
6290 block_input ();
6291
6292 /* Get the mouse's position relative to the scroll bar window, and
6293 report that. */
6294 if (XQueryPointer (FRAME_X_DISPLAY (f), w,
6295
6296 /* Root, child, root x and root y. */
6297 &dummy_window, &dummy_window,
6298 &dummy_coord, &dummy_coord,
6299
6300 /* Position relative to scroll bar. */
6301 &win_x, &win_y,
6302
6303 /* Mouse buttons and modifier keys. */
6304 &dummy_mask))
6305 {
6306 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
6307
6308 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
6309
6310 if (bar->dragging != -1)
6311 win_y -= bar->dragging;
6312
6313 if (win_y < 0)
6314 win_y = 0;
6315 if (win_y > top_range)
6316 win_y = top_range;
6317
6318 *fp = f;
6319 *bar_window = bar->window;
6320
6321 if (bar->dragging != -1)
6322 *part = scroll_bar_handle;
6323 else if (win_y < bar->start)
6324 *part = scroll_bar_above_handle;
6325 else if (win_y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
6326 *part = scroll_bar_handle;
6327 else
6328 *part = scroll_bar_below_handle;
6329
6330 XSETINT (*x, win_y);
6331 XSETINT (*y, top_range);
6332
6333 f->mouse_moved = false;
6334 dpyinfo->last_mouse_scroll_bar = NULL;
6335 *timestamp = dpyinfo->last_mouse_movement_time;
6336 }
6337
6338 unblock_input ();
6339 }
6340
6341
6342 /* Return information to the user about the current position of the mouse
6343 on the scroll bar. */
6344
6345 static void
6346 x_horizontal_scroll_bar_report_motion (struct frame **fp, Lisp_Object *bar_window,
6347 enum scroll_bar_part *part, Lisp_Object *x,
6348 Lisp_Object *y, Time *timestamp)
6349 {
6350 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
6351 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
6352 Window w = bar->x_window;
6353 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
6354 int win_x, win_y;
6355 Window dummy_window;
6356 int dummy_coord;
6357 unsigned int dummy_mask;
6358
6359 block_input ();
6360
6361 /* Get the mouse's position relative to the scroll bar window, and
6362 report that. */
6363 if (XQueryPointer (FRAME_X_DISPLAY (f), w,
6364
6365 /* Root, child, root x and root y. */
6366 &dummy_window, &dummy_window,
6367 &dummy_coord, &dummy_coord,
6368
6369 /* Position relative to scroll bar. */
6370 &win_x, &win_y,
6371
6372 /* Mouse buttons and modifier keys. */
6373 &dummy_mask))
6374 {
6375 int left_range = HORIZONTAL_SCROLL_BAR_LEFT_RANGE (f, bar->width);
6376
6377 win_x -= HORIZONTAL_SCROLL_BAR_LEFT_BORDER;
6378
6379 if (bar->dragging != -1)
6380 win_x -= bar->dragging;
6381
6382 if (win_x < 0)
6383 win_x = 0;
6384 if (win_x > left_range)
6385 win_x = left_range;
6386
6387 *fp = f;
6388 *bar_window = bar->window;
6389
6390 if (bar->dragging != -1)
6391 *part = scroll_bar_horizontal_handle;
6392 else if (win_x < bar->start)
6393 *part = scroll_bar_before_handle;
6394 else if (win_x < bar->end + HORIZONTAL_SCROLL_BAR_MIN_HANDLE)
6395 *part = scroll_bar_handle;
6396 else
6397 *part = scroll_bar_after_handle;
6398
6399 XSETINT (*y, win_x);
6400 XSETINT (*x, left_range);
6401
6402 f->mouse_moved = false;
6403 dpyinfo->last_mouse_scroll_bar = NULL;
6404 *timestamp = dpyinfo->last_mouse_movement_time;
6405 }
6406
6407 unblock_input ();
6408 }
6409
6410
6411 /* The screen has been cleared so we may have changed foreground or
6412 background colors, and the scroll bars may need to be redrawn.
6413 Clear out the scroll bars, and ask for expose events, so we can
6414 redraw them. */
6415
6416 static void
6417 x_scroll_bar_clear (struct frame *f)
6418 {
6419 #ifndef USE_TOOLKIT_SCROLL_BARS
6420 Lisp_Object bar;
6421
6422 /* We can have scroll bars even if this is 0,
6423 if we just turned off scroll bar mode.
6424 But in that case we should not clear them. */
6425 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
6426 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
6427 bar = XSCROLL_BAR (bar)->next)
6428 XClearArea (FRAME_X_DISPLAY (f),
6429 XSCROLL_BAR (bar)->x_window,
6430 0, 0, 0, 0, True);
6431 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6432 }
6433
6434 #ifdef ENABLE_CHECKING
6435
6436 /* Record the last 100 characters stored
6437 to help debug the loss-of-chars-during-GC problem. */
6438
6439 static int temp_index;
6440 static short temp_buffer[100];
6441
6442 #define STORE_KEYSYM_FOR_DEBUG(keysym) \
6443 if (temp_index == ARRAYELTS (temp_buffer)) \
6444 temp_index = 0; \
6445 temp_buffer[temp_index++] = (keysym)
6446
6447 #else /* not ENABLE_CHECKING */
6448
6449 #define STORE_KEYSYM_FOR_DEBUG(keysym) ((void)0)
6450
6451 #endif /* ENABLE_CHECKING */
6452
6453 /* Set this to nonzero to fake an "X I/O error"
6454 on a particular display. */
6455
6456 static struct x_display_info *XTread_socket_fake_io_error;
6457
6458 /* When we find no input here, we occasionally do a no-op command
6459 to verify that the X server is still running and we can still talk with it.
6460 We try all the open displays, one by one.
6461 This variable is used for cycling thru the displays. */
6462
6463 static struct x_display_info *next_noop_dpyinfo;
6464
6465 enum
6466 {
6467 X_EVENT_NORMAL,
6468 X_EVENT_GOTO_OUT,
6469 X_EVENT_DROP
6470 };
6471
6472 /* Filter events for the current X input method.
6473 DPYINFO is the display this event is for.
6474 EVENT is the X event to filter.
6475
6476 Returns non-zero if the event was filtered, caller shall not process
6477 this event further.
6478 Returns zero if event is wasn't filtered. */
6479
6480 #ifdef HAVE_X_I18N
6481 static int
6482 x_filter_event (struct x_display_info *dpyinfo, XEvent *event)
6483 {
6484 /* XFilterEvent returns non-zero if the input method has
6485 consumed the event. We pass the frame's X window to
6486 XFilterEvent because that's the one for which the IC
6487 was created. */
6488
6489 struct frame *f1 = x_any_window_to_frame (dpyinfo,
6490 event->xclient.window);
6491
6492 return XFilterEvent (event, f1 ? FRAME_X_WINDOW (f1) : None);
6493 }
6494 #endif
6495
6496 #ifdef USE_GTK
6497 static int current_count;
6498 static int current_finish;
6499 static struct input_event *current_hold_quit;
6500
6501 /* This is the filter function invoked by the GTK event loop.
6502 It is invoked before the XEvent is translated to a GdkEvent,
6503 so we have a chance to act on the event before GTK. */
6504 static GdkFilterReturn
6505 event_handler_gdk (GdkXEvent *gxev, GdkEvent *ev, gpointer data)
6506 {
6507 XEvent *xev = (XEvent *) gxev;
6508
6509 block_input ();
6510 if (current_count >= 0)
6511 {
6512 struct x_display_info *dpyinfo;
6513
6514 dpyinfo = x_display_info_for_display (xev->xany.display);
6515
6516 #ifdef HAVE_X_I18N
6517 /* Filter events for the current X input method.
6518 GTK calls XFilterEvent but not for key press and release,
6519 so we do it here. */
6520 if ((xev->type == KeyPress || xev->type == KeyRelease)
6521 && dpyinfo
6522 && x_filter_event (dpyinfo, xev))
6523 {
6524 unblock_input ();
6525 return GDK_FILTER_REMOVE;
6526 }
6527 #endif
6528
6529 if (! dpyinfo)
6530 current_finish = X_EVENT_NORMAL;
6531 else
6532 current_count
6533 += handle_one_xevent (dpyinfo, xev, &current_finish,
6534 current_hold_quit);
6535 }
6536 else
6537 current_finish = x_dispatch_event (xev, xev->xany.display);
6538
6539 unblock_input ();
6540
6541 if (current_finish == X_EVENT_GOTO_OUT || current_finish == X_EVENT_DROP)
6542 return GDK_FILTER_REMOVE;
6543
6544 return GDK_FILTER_CONTINUE;
6545 }
6546 #endif /* USE_GTK */
6547
6548
6549 static void xembed_send_message (struct frame *f, Time,
6550 enum xembed_message,
6551 long detail, long data1, long data2);
6552
6553 static void
6554 x_net_wm_state (struct frame *f, Window window)
6555 {
6556 int value = FULLSCREEN_NONE;
6557 Lisp_Object lval = Qnil;
6558 bool sticky = false;
6559
6560 get_current_wm_state (f, window, &value, &sticky);
6561
6562 switch (value)
6563 {
6564 case FULLSCREEN_WIDTH:
6565 lval = Qfullwidth;
6566 break;
6567 case FULLSCREEN_HEIGHT:
6568 lval = Qfullheight;
6569 break;
6570 case FULLSCREEN_BOTH:
6571 lval = Qfullboth;
6572 break;
6573 case FULLSCREEN_MAXIMIZED:
6574 lval = Qmaximized;
6575 break;
6576 }
6577
6578 frame_size_history_add
6579 (f, Qx_net_wm_state, 0, 0,
6580 list2 (get_frame_param (f, Qfullscreen), lval));
6581
6582 store_frame_param (f, Qfullscreen, lval);
6583 /** store_frame_param (f, Qsticky, sticky ? Qt : Qnil); **/
6584 }
6585
6586 /* Handles the XEvent EVENT on display DPYINFO.
6587
6588 *FINISH is X_EVENT_GOTO_OUT if caller should stop reading events.
6589 *FINISH is zero if caller should continue reading events.
6590 *FINISH is X_EVENT_DROP if event should not be passed to the toolkit.
6591 *EVENT is unchanged unless we're processing KeyPress event.
6592
6593 We return the number of characters stored into the buffer. */
6594
6595 static int
6596 handle_one_xevent (struct x_display_info *dpyinfo,
6597 const XEvent *event,
6598 int *finish, struct input_event *hold_quit)
6599 {
6600 union {
6601 struct input_event ie;
6602 struct selection_input_event sie;
6603 } inev;
6604 int count = 0;
6605 int do_help = 0;
6606 ptrdiff_t nbytes = 0;
6607 struct frame *any, *f = NULL;
6608 struct coding_system coding;
6609 Mouse_HLInfo *hlinfo = &dpyinfo->mouse_highlight;
6610 /* This holds the state XLookupString needs to implement dead keys
6611 and other tricks known as "compose processing". _X Window System_
6612 says that a portable program can't use this, but Stephen Gildea assures
6613 me that letting the compiler initialize it to zeros will work okay. */
6614 static XComposeStatus compose_status;
6615
6616 USE_SAFE_ALLOCA;
6617
6618 *finish = X_EVENT_NORMAL;
6619
6620 EVENT_INIT (inev.ie);
6621 inev.ie.kind = NO_EVENT;
6622 inev.ie.arg = Qnil;
6623
6624 any = x_any_window_to_frame (dpyinfo, event->xany.window);
6625
6626 if (any && any->wait_event_type == event->type)
6627 any->wait_event_type = 0; /* Indicates we got it. */
6628
6629 switch (event->type)
6630 {
6631 case ClientMessage:
6632 {
6633 if (event->xclient.message_type == dpyinfo->Xatom_wm_protocols
6634 && event->xclient.format == 32)
6635 {
6636 if (event->xclient.data.l[0] == dpyinfo->Xatom_wm_take_focus)
6637 {
6638 /* Use the value returned by x_any_window_to_frame
6639 because this could be the shell widget window
6640 if the frame has no title bar. */
6641 f = any;
6642 #ifdef HAVE_X_I18N
6643 /* Not quite sure this is needed -pd */
6644 if (f && FRAME_XIC (f))
6645 XSetICFocus (FRAME_XIC (f));
6646 #endif
6647 #if false
6648 /* Emacs sets WM hints whose `input' field is `true'. This
6649 instructs the WM to set the input focus automatically for
6650 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
6651 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
6652 it has set the focus. So, XSetInputFocus below is not
6653 needed.
6654
6655 The call to XSetInputFocus below has also caused trouble. In
6656 cases where the XSetInputFocus done by the WM and the one
6657 below are temporally close (on a fast machine), the call
6658 below can generate additional FocusIn events which confuse
6659 Emacs. */
6660
6661 /* Since we set WM_TAKE_FOCUS, we must call
6662 XSetInputFocus explicitly. But not if f is null,
6663 since that might be an event for a deleted frame. */
6664 if (f)
6665 {
6666 Display *d = event->xclient.display;
6667 /* Catch and ignore errors, in case window has been
6668 iconified by a window manager such as GWM. */
6669 x_catch_errors (d);
6670 XSetInputFocus (d, event->xclient.window,
6671 /* The ICCCM says this is
6672 the only valid choice. */
6673 RevertToParent,
6674 event->xclient.data.l[1]);
6675 /* This is needed to detect the error
6676 if there is an error. */
6677 XSync (d, False);
6678 x_uncatch_errors ();
6679 }
6680 /* Not certain about handling scroll bars here */
6681 #endif
6682 goto done;
6683 }
6684
6685 if (event->xclient.data.l[0] == dpyinfo->Xatom_wm_save_yourself)
6686 {
6687 /* Save state modify the WM_COMMAND property to
6688 something which can reinstate us. This notifies
6689 the session manager, who's looking for such a
6690 PropertyNotify. Can restart processing when
6691 a keyboard or mouse event arrives. */
6692 /* If we have a session manager, don't set this.
6693 KDE will then start two Emacsen, one for the
6694 session manager and one for this. */
6695 #ifdef HAVE_X_SM
6696 if (! x_session_have_connection ())
6697 #endif
6698 {
6699 f = x_top_window_to_frame (dpyinfo,
6700 event->xclient.window);
6701 /* This is just so we only give real data once
6702 for a single Emacs process. */
6703 if (f == SELECTED_FRAME ())
6704 XSetCommand (FRAME_X_DISPLAY (f),
6705 event->xclient.window,
6706 initial_argv, initial_argc);
6707 else if (f)
6708 XSetCommand (FRAME_X_DISPLAY (f),
6709 event->xclient.window,
6710 0, 0);
6711 }
6712 goto done;
6713 }
6714
6715 if (event->xclient.data.l[0] == dpyinfo->Xatom_wm_delete_window)
6716 {
6717 f = any;
6718 if (!f)
6719 goto OTHER; /* May be a dialog that is to be removed */
6720
6721 inev.ie.kind = DELETE_WINDOW_EVENT;
6722 XSETFRAME (inev.ie.frame_or_window, f);
6723 goto done;
6724 }
6725
6726 goto done;
6727 }
6728
6729 if (event->xclient.message_type == dpyinfo->Xatom_wm_configure_denied)
6730 goto done;
6731
6732 if (event->xclient.message_type == dpyinfo->Xatom_wm_window_moved)
6733 {
6734 int new_x, new_y;
6735 f = x_window_to_frame (dpyinfo, event->xclient.window);
6736
6737 new_x = event->xclient.data.s[0];
6738 new_y = event->xclient.data.s[1];
6739
6740 if (f)
6741 {
6742 f->left_pos = new_x;
6743 f->top_pos = new_y;
6744 }
6745 goto done;
6746 }
6747
6748 #ifdef HACK_EDITRES
6749 if (event->xclient.message_type == dpyinfo->Xatom_editres)
6750 {
6751 f = any;
6752 if (f)
6753 _XEditResCheckMessages (f->output_data.x->widget,
6754 NULL, (XEvent *) event, NULL);
6755 goto done;
6756 }
6757 #endif /* HACK_EDITRES */
6758
6759 if (event->xclient.message_type == dpyinfo->Xatom_DONE
6760 || event->xclient.message_type == dpyinfo->Xatom_PAGE)
6761 {
6762 /* Ghostview job completed. Kill it. We could
6763 reply with "Next" if we received "Page", but we
6764 currently never do because we are interested in
6765 images, only, which should have 1 page. */
6766 Pixmap pixmap = (Pixmap) event->xclient.data.l[1];
6767 f = x_window_to_frame (dpyinfo, event->xclient.window);
6768 if (!f)
6769 goto OTHER;
6770 x_kill_gs_process (pixmap, f);
6771 expose_frame (f, 0, 0, 0, 0);
6772 goto done;
6773 }
6774
6775 #ifdef USE_TOOLKIT_SCROLL_BARS
6776 /* Scroll bar callbacks send a ClientMessage from which
6777 we construct an input_event. */
6778 if (event->xclient.message_type == dpyinfo->Xatom_Scrollbar)
6779 {
6780 x_scroll_bar_to_input_event (event, &inev.ie);
6781 *finish = X_EVENT_GOTO_OUT;
6782 goto done;
6783 }
6784 else if (event->xclient.message_type == dpyinfo->Xatom_Horizontal_Scrollbar)
6785 {
6786 x_horizontal_scroll_bar_to_input_event (event, &inev.ie);
6787 *finish = X_EVENT_GOTO_OUT;
6788 goto done;
6789 }
6790 #endif /* USE_TOOLKIT_SCROLL_BARS */
6791
6792 /* XEmbed messages from the embedder (if any). */
6793 if (event->xclient.message_type == dpyinfo->Xatom_XEMBED)
6794 {
6795 enum xembed_message msg = event->xclient.data.l[1];
6796 if (msg == XEMBED_FOCUS_IN || msg == XEMBED_FOCUS_OUT)
6797 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
6798
6799 *finish = X_EVENT_GOTO_OUT;
6800 goto done;
6801 }
6802
6803 xft_settings_event (dpyinfo, event);
6804
6805 f = any;
6806 if (!f)
6807 goto OTHER;
6808 if (x_handle_dnd_message (f, &event->xclient, dpyinfo, &inev.ie))
6809 *finish = X_EVENT_DROP;
6810 }
6811 break;
6812
6813 case SelectionNotify:
6814 x_display_set_last_user_time (dpyinfo, event->xselection.time);
6815 #ifdef USE_X_TOOLKIT
6816 if (! x_window_to_frame (dpyinfo, event->xselection.requestor))
6817 goto OTHER;
6818 #endif /* not USE_X_TOOLKIT */
6819 x_handle_selection_notify (&event->xselection);
6820 break;
6821
6822 case SelectionClear: /* Someone has grabbed ownership. */
6823 x_display_set_last_user_time (dpyinfo, event->xselectionclear.time);
6824 #ifdef USE_X_TOOLKIT
6825 if (! x_window_to_frame (dpyinfo, event->xselectionclear.window))
6826 goto OTHER;
6827 #endif /* USE_X_TOOLKIT */
6828 {
6829 const XSelectionClearEvent *eventp = &event->xselectionclear;
6830
6831 inev.ie.kind = SELECTION_CLEAR_EVENT;
6832 SELECTION_EVENT_DPYINFO (&inev.sie) = dpyinfo;
6833 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
6834 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
6835 }
6836 break;
6837
6838 case SelectionRequest: /* Someone wants our selection. */
6839 x_display_set_last_user_time (dpyinfo, event->xselectionrequest.time);
6840 #ifdef USE_X_TOOLKIT
6841 if (!x_window_to_frame (dpyinfo, event->xselectionrequest.owner))
6842 goto OTHER;
6843 #endif /* USE_X_TOOLKIT */
6844 {
6845 const XSelectionRequestEvent *eventp = &event->xselectionrequest;
6846
6847 inev.ie.kind = SELECTION_REQUEST_EVENT;
6848 SELECTION_EVENT_DPYINFO (&inev.sie) = dpyinfo;
6849 SELECTION_EVENT_REQUESTOR (&inev.sie) = eventp->requestor;
6850 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
6851 SELECTION_EVENT_TARGET (&inev.sie) = eventp->target;
6852 SELECTION_EVENT_PROPERTY (&inev.sie) = eventp->property;
6853 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
6854 }
6855 break;
6856
6857 case PropertyNotify:
6858 x_display_set_last_user_time (dpyinfo, event->xproperty.time);
6859 f = x_top_window_to_frame (dpyinfo, event->xproperty.window);
6860 if (f && event->xproperty.atom == dpyinfo->Xatom_net_wm_state)
6861 {
6862 bool not_hidden = x_handle_net_wm_state (f, &event->xproperty);
6863 if (not_hidden && FRAME_ICONIFIED_P (f))
6864 {
6865 /* Gnome shell does not iconify us when C-z is pressed.
6866 It hides the frame. So if our state says we aren't
6867 hidden anymore, treat it as deiconified. */
6868 SET_FRAME_VISIBLE (f, 1);
6869 SET_FRAME_ICONIFIED (f, false);
6870 f->output_data.x->has_been_visible = true;
6871 inev.ie.kind = DEICONIFY_EVENT;
6872 XSETFRAME (inev.ie.frame_or_window, f);
6873 }
6874 else if (! not_hidden && ! FRAME_ICONIFIED_P (f))
6875 {
6876 SET_FRAME_VISIBLE (f, 0);
6877 SET_FRAME_ICONIFIED (f, true);
6878 inev.ie.kind = ICONIFY_EVENT;
6879 XSETFRAME (inev.ie.frame_or_window, f);
6880 }
6881 }
6882
6883 x_handle_property_notify (&event->xproperty);
6884 xft_settings_event (dpyinfo, event);
6885 goto OTHER;
6886
6887 case ReparentNotify:
6888 f = x_top_window_to_frame (dpyinfo, event->xreparent.window);
6889 if (f)
6890 {
6891 f->output_data.x->parent_desc = event->xreparent.parent;
6892 x_real_positions (f, &f->left_pos, &f->top_pos);
6893
6894 /* Perhaps reparented due to a WM restart. Reset this. */
6895 FRAME_DISPLAY_INFO (f)->wm_type = X_WMTYPE_UNKNOWN;
6896 FRAME_DISPLAY_INFO (f)->net_supported_window = 0;
6897
6898 x_set_frame_alpha (f);
6899 }
6900 goto OTHER;
6901
6902 case Expose:
6903 f = x_window_to_frame (dpyinfo, event->xexpose.window);
6904 if (f)
6905 {
6906 if (!FRAME_VISIBLE_P (f))
6907 {
6908 SET_FRAME_VISIBLE (f, 1);
6909 SET_FRAME_ICONIFIED (f, false);
6910 f->output_data.x->has_been_visible = true;
6911 SET_FRAME_GARBAGED (f);
6912 }
6913 else
6914 {
6915 #ifdef USE_GTK
6916 /* This seems to be needed for GTK 2.6 and later, see
6917 http://debbugs.gnu.org/cgi/bugreport.cgi?bug=15398. */
6918 x_clear_area (event->xexpose.display,
6919 event->xexpose.window,
6920 event->xexpose.x, event->xexpose.y,
6921 event->xexpose.width, event->xexpose.height);
6922 #endif
6923 expose_frame (f, event->xexpose.x, event->xexpose.y,
6924 event->xexpose.width, event->xexpose.height);
6925 }
6926 }
6927 else
6928 {
6929 #ifndef USE_TOOLKIT_SCROLL_BARS
6930 struct scroll_bar *bar;
6931 #endif
6932 #if defined USE_LUCID
6933 /* Submenus of the Lucid menu bar aren't widgets
6934 themselves, so there's no way to dispatch events
6935 to them. Recognize this case separately. */
6936 {
6937 Widget widget = x_window_to_menu_bar (event->xexpose.window);
6938 if (widget)
6939 xlwmenu_redisplay (widget);
6940 }
6941 #endif /* USE_LUCID */
6942
6943 #ifdef USE_TOOLKIT_SCROLL_BARS
6944 /* Dispatch event to the widget. */
6945 goto OTHER;
6946 #else /* not USE_TOOLKIT_SCROLL_BARS */
6947 bar = x_window_to_scroll_bar (event->xexpose.display,
6948 event->xexpose.window, 2);
6949
6950 if (bar)
6951 x_scroll_bar_expose (bar, event);
6952 #ifdef USE_X_TOOLKIT
6953 else
6954 goto OTHER;
6955 #endif /* USE_X_TOOLKIT */
6956 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6957 }
6958 break;
6959
6960 case GraphicsExpose: /* This occurs when an XCopyArea's
6961 source area was obscured or not
6962 available. */
6963 f = x_window_to_frame (dpyinfo, event->xgraphicsexpose.drawable);
6964 if (f)
6965 expose_frame (f, event->xgraphicsexpose.x,
6966 event->xgraphicsexpose.y,
6967 event->xgraphicsexpose.width,
6968 event->xgraphicsexpose.height);
6969 #ifdef USE_X_TOOLKIT
6970 else
6971 goto OTHER;
6972 #endif /* USE_X_TOOLKIT */
6973 break;
6974
6975 case NoExpose: /* This occurs when an XCopyArea's
6976 source area was completely
6977 available. */
6978 break;
6979
6980 case UnmapNotify:
6981 /* Redo the mouse-highlight after the tooltip has gone. */
6982 if (event->xunmap.window == tip_window)
6983 {
6984 tip_window = 0;
6985 x_redo_mouse_highlight (dpyinfo);
6986 }
6987
6988 f = x_top_window_to_frame (dpyinfo, event->xunmap.window);
6989 if (f) /* F may no longer exist if
6990 the frame was deleted. */
6991 {
6992 bool visible = FRAME_VISIBLE_P (f);
6993 /* While a frame is unmapped, display generation is
6994 disabled; you don't want to spend time updating a
6995 display that won't ever be seen. */
6996 SET_FRAME_VISIBLE (f, 0);
6997 /* We can't distinguish, from the event, whether the window
6998 has become iconified or invisible. So assume, if it
6999 was previously visible, than now it is iconified.
7000 But x_make_frame_invisible clears both
7001 the visible flag and the iconified flag;
7002 and that way, we know the window is not iconified now. */
7003 if (visible || FRAME_ICONIFIED_P (f))
7004 {
7005 SET_FRAME_ICONIFIED (f, true);
7006 inev.ie.kind = ICONIFY_EVENT;
7007 XSETFRAME (inev.ie.frame_or_window, f);
7008 }
7009 }
7010 goto OTHER;
7011
7012 case MapNotify:
7013 /* We use x_top_window_to_frame because map events can
7014 come for sub-windows and they don't mean that the
7015 frame is visible. */
7016 f = x_top_window_to_frame (dpyinfo, event->xmap.window);
7017 if (f)
7018 {
7019 bool iconified = FRAME_ICONIFIED_P (f);
7020
7021 /* Check if fullscreen was specified before we where mapped the
7022 first time, i.e. from the command line. */
7023 if (!f->output_data.x->has_been_visible)
7024 x_check_fullscreen (f);
7025
7026 SET_FRAME_VISIBLE (f, 1);
7027 SET_FRAME_ICONIFIED (f, false);
7028 f->output_data.x->has_been_visible = true;
7029
7030 if (iconified)
7031 {
7032 inev.ie.kind = DEICONIFY_EVENT;
7033 XSETFRAME (inev.ie.frame_or_window, f);
7034 }
7035 else if (! NILP (Vframe_list) && ! NILP (XCDR (Vframe_list)))
7036 /* Force a redisplay sooner or later to update the
7037 frame titles in case this is the second frame. */
7038 record_asynch_buffer_change ();
7039
7040 #ifdef USE_GTK
7041 xg_frame_resized (f, -1, -1);
7042 #endif
7043 }
7044 goto OTHER;
7045
7046 case KeyPress:
7047
7048 x_display_set_last_user_time (dpyinfo, event->xkey.time);
7049 ignore_next_mouse_click_timeout = 0;
7050
7051 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
7052 /* Dispatch KeyPress events when in menu. */
7053 if (popup_activated ())
7054 goto OTHER;
7055 #endif
7056
7057 f = any;
7058
7059 #if ! defined (USE_GTK)
7060 /* If mouse-highlight is an integer, input clears out
7061 mouse highlighting. */
7062 if (!hlinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
7063 && (f == 0
7064 || !EQ (f->tool_bar_window, hlinfo->mouse_face_window)))
7065 {
7066 clear_mouse_face (hlinfo);
7067 hlinfo->mouse_face_hidden = true;
7068 }
7069 #endif
7070
7071 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
7072 if (f == 0)
7073 {
7074 /* Scroll bars consume key events, but we want
7075 the keys to go to the scroll bar's frame. */
7076 Widget widget = XtWindowToWidget (dpyinfo->display,
7077 event->xkey.window);
7078 if (widget && XmIsScrollBar (widget))
7079 {
7080 widget = XtParent (widget);
7081 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
7082 }
7083 }
7084 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
7085
7086 if (f != 0)
7087 {
7088 KeySym keysym, orig_keysym;
7089 /* al%imercury@uunet.uu.net says that making this 81
7090 instead of 80 fixed a bug whereby meta chars made
7091 his Emacs hang.
7092
7093 It seems that some version of XmbLookupString has
7094 a bug of not returning XBufferOverflow in
7095 status_return even if the input is too long to
7096 fit in 81 bytes. So, we must prepare sufficient
7097 bytes for copy_buffer. 513 bytes (256 chars for
7098 two-byte character set) seems to be a fairly good
7099 approximation. -- 2000.8.10 handa@etl.go.jp */
7100 unsigned char copy_buffer[513];
7101 unsigned char *copy_bufptr = copy_buffer;
7102 int copy_bufsiz = sizeof (copy_buffer);
7103 int modifiers;
7104 Lisp_Object coding_system = Qlatin_1;
7105 Lisp_Object c;
7106 /* Event will be modified. */
7107 XKeyEvent xkey = event->xkey;
7108
7109 #ifdef USE_GTK
7110 /* Don't pass keys to GTK. A Tab will shift focus to the
7111 tool bar in GTK 2.4. Keys will still go to menus and
7112 dialogs because in that case popup_activated is nonzero
7113 (see above). */
7114 *finish = X_EVENT_DROP;
7115 #endif
7116
7117 xkey.state |= x_emacs_to_x_modifiers (FRAME_DISPLAY_INFO (f),
7118 extra_keyboard_modifiers);
7119 modifiers = xkey.state;
7120
7121 /* This will have to go some day... */
7122
7123 /* make_lispy_event turns chars into control chars.
7124 Don't do it here because XLookupString is too eager. */
7125 xkey.state &= ~ControlMask;
7126 xkey.state &= ~(dpyinfo->meta_mod_mask
7127 | dpyinfo->super_mod_mask
7128 | dpyinfo->hyper_mod_mask
7129 | dpyinfo->alt_mod_mask);
7130
7131 /* In case Meta is ComposeCharacter,
7132 clear its status. According to Markus Ehrnsperger
7133 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
7134 this enables ComposeCharacter to work whether or
7135 not it is combined with Meta. */
7136 if (modifiers & dpyinfo->meta_mod_mask)
7137 memset (&compose_status, 0, sizeof (compose_status));
7138
7139 #ifdef HAVE_X_I18N
7140 if (FRAME_XIC (f))
7141 {
7142 Status status_return;
7143
7144 coding_system = Vlocale_coding_system;
7145 nbytes = XmbLookupString (FRAME_XIC (f),
7146 &xkey, (char *) copy_bufptr,
7147 copy_bufsiz, &keysym,
7148 &status_return);
7149 if (status_return == XBufferOverflow)
7150 {
7151 copy_bufsiz = nbytes + 1;
7152 copy_bufptr = alloca (copy_bufsiz);
7153 nbytes = XmbLookupString (FRAME_XIC (f),
7154 &xkey, (char *) copy_bufptr,
7155 copy_bufsiz, &keysym,
7156 &status_return);
7157 }
7158 /* Xutf8LookupString is a new but already deprecated interface. -stef */
7159 if (status_return == XLookupNone)
7160 break;
7161 else if (status_return == XLookupChars)
7162 {
7163 keysym = NoSymbol;
7164 modifiers = 0;
7165 }
7166 else if (status_return != XLookupKeySym
7167 && status_return != XLookupBoth)
7168 emacs_abort ();
7169 }
7170 else
7171 nbytes = XLookupString (&xkey, (char *) copy_bufptr,
7172 copy_bufsiz, &keysym,
7173 &compose_status);
7174 #else
7175 nbytes = XLookupString (&xkey, (char *) copy_bufptr,
7176 copy_bufsiz, &keysym,
7177 &compose_status);
7178 #endif
7179
7180 /* If not using XIM/XIC, and a compose sequence is in progress,
7181 we break here. Otherwise, chars_matched is always 0. */
7182 if (compose_status.chars_matched > 0 && nbytes == 0)
7183 break;
7184
7185 memset (&compose_status, 0, sizeof (compose_status));
7186 orig_keysym = keysym;
7187
7188 /* Common for all keysym input events. */
7189 XSETFRAME (inev.ie.frame_or_window, f);
7190 inev.ie.modifiers
7191 = x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO (f), modifiers);
7192 inev.ie.timestamp = xkey.time;
7193
7194 /* First deal with keysyms which have defined
7195 translations to characters. */
7196 if (keysym >= 32 && keysym < 128)
7197 /* Avoid explicitly decoding each ASCII character. */
7198 {
7199 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
7200 inev.ie.code = keysym;
7201 goto done_keysym;
7202 }
7203
7204 /* Keysyms directly mapped to Unicode characters. */
7205 if (keysym >= 0x01000000 && keysym <= 0x0110FFFF)
7206 {
7207 if (keysym < 0x01000080)
7208 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
7209 else
7210 inev.ie.kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
7211 inev.ie.code = keysym & 0xFFFFFF;
7212 goto done_keysym;
7213 }
7214
7215 /* Now non-ASCII. */
7216 if (HASH_TABLE_P (Vx_keysym_table)
7217 && (c = Fgethash (make_number (keysym),
7218 Vx_keysym_table,
7219 Qnil),
7220 NATNUMP (c)))
7221 {
7222 inev.ie.kind = (SINGLE_BYTE_CHAR_P (XFASTINT (c))
7223 ? ASCII_KEYSTROKE_EVENT
7224 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
7225 inev.ie.code = XFASTINT (c);
7226 goto done_keysym;
7227 }
7228
7229 /* Random non-modifier sorts of keysyms. */
7230 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
7231 || keysym == XK_Delete
7232 #ifdef XK_ISO_Left_Tab
7233 || (keysym >= XK_ISO_Left_Tab
7234 && keysym <= XK_ISO_Enter)
7235 #endif
7236 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
7237 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
7238 #ifdef HPUX
7239 /* This recognizes the "extended function
7240 keys". It seems there's no cleaner way.
7241 Test IsModifierKey to avoid handling
7242 mode_switch incorrectly. */
7243 || (XK_Select <= keysym && keysym < XK_KP_Space)
7244 #endif
7245 #ifdef XK_dead_circumflex
7246 || orig_keysym == XK_dead_circumflex
7247 #endif
7248 #ifdef XK_dead_grave
7249 || orig_keysym == XK_dead_grave
7250 #endif
7251 #ifdef XK_dead_tilde
7252 || orig_keysym == XK_dead_tilde
7253 #endif
7254 #ifdef XK_dead_diaeresis
7255 || orig_keysym == XK_dead_diaeresis
7256 #endif
7257 #ifdef XK_dead_macron
7258 || orig_keysym == XK_dead_macron
7259 #endif
7260 #ifdef XK_dead_degree
7261 || orig_keysym == XK_dead_degree
7262 #endif
7263 #ifdef XK_dead_acute
7264 || orig_keysym == XK_dead_acute
7265 #endif
7266 #ifdef XK_dead_cedilla
7267 || orig_keysym == XK_dead_cedilla
7268 #endif
7269 #ifdef XK_dead_breve
7270 || orig_keysym == XK_dead_breve
7271 #endif
7272 #ifdef XK_dead_ogonek
7273 || orig_keysym == XK_dead_ogonek
7274 #endif
7275 #ifdef XK_dead_caron
7276 || orig_keysym == XK_dead_caron
7277 #endif
7278 #ifdef XK_dead_doubleacute
7279 || orig_keysym == XK_dead_doubleacute
7280 #endif
7281 #ifdef XK_dead_abovedot
7282 || orig_keysym == XK_dead_abovedot
7283 #endif
7284 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
7285 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
7286 /* Any "vendor-specific" key is ok. */
7287 || (orig_keysym & (1 << 28))
7288 || (keysym != NoSymbol && nbytes == 0))
7289 && ! (IsModifierKey (orig_keysym)
7290 /* The symbols from XK_ISO_Lock
7291 to XK_ISO_Last_Group_Lock
7292 don't have real modifiers but
7293 should be treated similarly to
7294 Mode_switch by Emacs. */
7295 #if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
7296 || (XK_ISO_Lock <= orig_keysym
7297 && orig_keysym <= XK_ISO_Last_Group_Lock)
7298 #endif
7299 ))
7300 {
7301 STORE_KEYSYM_FOR_DEBUG (keysym);
7302 /* make_lispy_event will convert this to a symbolic
7303 key. */
7304 inev.ie.kind = NON_ASCII_KEYSTROKE_EVENT;
7305 inev.ie.code = keysym;
7306 goto done_keysym;
7307 }
7308
7309 { /* Raw bytes, not keysym. */
7310 ptrdiff_t i;
7311 int nchars, len;
7312
7313 for (i = 0, nchars = 0; i < nbytes; i++)
7314 {
7315 if (ASCII_CHAR_P (copy_bufptr[i]))
7316 nchars++;
7317 STORE_KEYSYM_FOR_DEBUG (copy_bufptr[i]);
7318 }
7319
7320 if (nchars < nbytes)
7321 {
7322 /* Decode the input data. */
7323
7324 /* The input should be decoded with `coding_system'
7325 which depends on which X*LookupString function
7326 we used just above and the locale. */
7327 setup_coding_system (coding_system, &coding);
7328 coding.src_multibyte = false;
7329 coding.dst_multibyte = true;
7330 /* The input is converted to events, thus we can't
7331 handle composition. Anyway, there's no XIM that
7332 gives us composition information. */
7333 coding.common_flags &= ~CODING_ANNOTATION_MASK;
7334
7335 SAFE_NALLOCA (coding.destination, MAX_MULTIBYTE_LENGTH,
7336 nbytes);
7337 coding.dst_bytes = MAX_MULTIBYTE_LENGTH * nbytes;
7338 coding.mode |= CODING_MODE_LAST_BLOCK;
7339 decode_coding_c_string (&coding, copy_bufptr, nbytes, Qnil);
7340 nbytes = coding.produced;
7341 nchars = coding.produced_char;
7342 copy_bufptr = coding.destination;
7343 }
7344
7345 /* Convert the input data to a sequence of
7346 character events. */
7347 for (i = 0; i < nbytes; i += len)
7348 {
7349 int ch;
7350 if (nchars == nbytes)
7351 ch = copy_bufptr[i], len = 1;
7352 else
7353 ch = STRING_CHAR_AND_LENGTH (copy_bufptr + i, len);
7354 inev.ie.kind = (SINGLE_BYTE_CHAR_P (ch)
7355 ? ASCII_KEYSTROKE_EVENT
7356 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
7357 inev.ie.code = ch;
7358 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
7359 }
7360
7361 count += nchars;
7362
7363 inev.ie.kind = NO_EVENT; /* Already stored above. */
7364
7365 if (keysym == NoSymbol)
7366 break;
7367 }
7368 /* FIXME: check side effects and remove this. */
7369 ((XEvent *) event)->xkey = xkey;
7370 }
7371 done_keysym:
7372 #ifdef HAVE_X_I18N
7373 /* Don't dispatch this event since XtDispatchEvent calls
7374 XFilterEvent, and two calls in a row may freeze the
7375 client. */
7376 break;
7377 #else
7378 goto OTHER;
7379 #endif
7380
7381 case KeyRelease:
7382 x_display_set_last_user_time (dpyinfo, event->xkey.time);
7383 #ifdef HAVE_X_I18N
7384 /* Don't dispatch this event since XtDispatchEvent calls
7385 XFilterEvent, and two calls in a row may freeze the
7386 client. */
7387 break;
7388 #else
7389 goto OTHER;
7390 #endif
7391
7392 case EnterNotify:
7393 x_display_set_last_user_time (dpyinfo, event->xcrossing.time);
7394 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
7395
7396 f = any;
7397
7398 if (f && x_mouse_click_focus_ignore_position)
7399 ignore_next_mouse_click_timeout = event->xmotion.time + 200;
7400
7401 /* EnterNotify counts as mouse movement,
7402 so update things that depend on mouse position. */
7403 if (f && !f->output_data.x->hourglass_p)
7404 note_mouse_movement (f, &event->xmotion);
7405 #ifdef USE_GTK
7406 /* We may get an EnterNotify on the buttons in the toolbar. In that
7407 case we moved out of any highlighted area and need to note this. */
7408 if (!f && dpyinfo->last_mouse_glyph_frame)
7409 note_mouse_movement (dpyinfo->last_mouse_glyph_frame, &event->xmotion);
7410 #endif
7411 goto OTHER;
7412
7413 case FocusIn:
7414 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
7415 goto OTHER;
7416
7417 case LeaveNotify:
7418 x_display_set_last_user_time (dpyinfo, event->xcrossing.time);
7419 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
7420
7421 f = x_top_window_to_frame (dpyinfo, event->xcrossing.window);
7422 if (f)
7423 {
7424 if (f == hlinfo->mouse_face_mouse_frame)
7425 {
7426 /* If we move outside the frame, then we're
7427 certainly no longer on any text in the frame. */
7428 clear_mouse_face (hlinfo);
7429 hlinfo->mouse_face_mouse_frame = 0;
7430 }
7431
7432 /* Generate a nil HELP_EVENT to cancel a help-echo.
7433 Do it only if there's something to cancel.
7434 Otherwise, the startup message is cleared when
7435 the mouse leaves the frame. */
7436 if (any_help_event_p)
7437 do_help = -1;
7438 }
7439 #ifdef USE_GTK
7440 /* See comment in EnterNotify above */
7441 else if (dpyinfo->last_mouse_glyph_frame)
7442 note_mouse_movement (dpyinfo->last_mouse_glyph_frame, &event->xmotion);
7443 #endif
7444 goto OTHER;
7445
7446 case FocusOut:
7447 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
7448 goto OTHER;
7449
7450 case MotionNotify:
7451 {
7452 x_display_set_last_user_time (dpyinfo, event->xmotion.time);
7453 previous_help_echo_string = help_echo_string;
7454 help_echo_string = Qnil;
7455
7456 f = (x_mouse_grabbed (dpyinfo) ? dpyinfo->last_mouse_frame
7457 : x_window_to_frame (dpyinfo, event->xmotion.window));
7458
7459 if (hlinfo->mouse_face_hidden)
7460 {
7461 hlinfo->mouse_face_hidden = false;
7462 clear_mouse_face (hlinfo);
7463 }
7464
7465 #ifdef USE_GTK
7466 if (f && xg_event_is_for_scrollbar (f, event))
7467 f = 0;
7468 #endif
7469 if (f)
7470 {
7471
7472 /* Generate SELECT_WINDOW_EVENTs when needed.
7473 Don't let popup menus influence things (bug#1261). */
7474 if (!NILP (Vmouse_autoselect_window) && !popup_activated ())
7475 {
7476 static Lisp_Object last_mouse_window;
7477 Lisp_Object window = window_from_coordinates
7478 (f, event->xmotion.x, event->xmotion.y, 0, false);
7479
7480 /* Window will be selected only when it is not selected now and
7481 last mouse movement event was not in it. Minibuffer window
7482 will be selected only when it is active. */
7483 if (WINDOWP (window)
7484 && !EQ (window, last_mouse_window)
7485 && !EQ (window, selected_window)
7486 /* For click-to-focus window managers
7487 create event iff we don't leave the
7488 selected frame. */
7489 && (focus_follows_mouse
7490 || (EQ (XWINDOW (window)->frame,
7491 XWINDOW (selected_window)->frame))))
7492 {
7493 inev.ie.kind = SELECT_WINDOW_EVENT;
7494 inev.ie.frame_or_window = window;
7495 }
7496 /* Remember the last window where we saw the mouse. */
7497 last_mouse_window = window;
7498 }
7499 if (!note_mouse_movement (f, &event->xmotion))
7500 help_echo_string = previous_help_echo_string;
7501 }
7502 else
7503 {
7504 #ifndef USE_TOOLKIT_SCROLL_BARS
7505 struct scroll_bar *bar
7506 = x_window_to_scroll_bar (event->xmotion.display,
7507 event->xmotion.window, 2);
7508
7509 if (bar)
7510 x_scroll_bar_note_movement (bar, &event->xmotion);
7511 #endif /* USE_TOOLKIT_SCROLL_BARS */
7512
7513 /* If we move outside the frame, then we're
7514 certainly no longer on any text in the frame. */
7515 clear_mouse_face (hlinfo);
7516 }
7517
7518 /* If the contents of the global variable help_echo_string
7519 has changed, generate a HELP_EVENT. */
7520 if (!NILP (help_echo_string)
7521 || !NILP (previous_help_echo_string))
7522 do_help = 1;
7523 goto OTHER;
7524 }
7525
7526 case ConfigureNotify:
7527 f = x_top_window_to_frame (dpyinfo, event->xconfigure.window);
7528 #ifdef USE_GTK
7529 if (!f
7530 && (f = any)
7531 && event->xconfigure.window == FRAME_X_WINDOW (f))
7532 {
7533 xg_frame_resized (f, event->xconfigure.width,
7534 event->xconfigure.height);
7535 f = 0;
7536 }
7537 #endif
7538 if (f)
7539 {
7540 x_net_wm_state (f, event->xconfigure.window);
7541
7542 #ifdef USE_X_TOOLKIT
7543 /* Tip frames are pure X window, set size for them. */
7544 if (! NILP (tip_frame) && XFRAME (tip_frame) == f)
7545 {
7546 if (FRAME_PIXEL_HEIGHT (f) != event->xconfigure.height
7547 || FRAME_PIXEL_WIDTH (f) != event->xconfigure.width)
7548 SET_FRAME_GARBAGED (f);
7549 FRAME_PIXEL_HEIGHT (f) = event->xconfigure.height;
7550 FRAME_PIXEL_WIDTH (f) = event->xconfigure.width;
7551 }
7552 #endif
7553
7554 #ifndef USE_X_TOOLKIT
7555 #ifndef USE_GTK
7556 int width = FRAME_PIXEL_TO_TEXT_WIDTH (f, event->xconfigure.width);
7557 int height = FRAME_PIXEL_TO_TEXT_HEIGHT (f, event->xconfigure.height);
7558
7559 /* In the toolkit version, change_frame_size
7560 is called by the code that handles resizing
7561 of the EmacsFrame widget. */
7562
7563 /* Even if the number of character rows and columns has
7564 not changed, the font size may have changed, so we need
7565 to check the pixel dimensions as well. */
7566 if (width != FRAME_TEXT_WIDTH (f)
7567 || height != FRAME_TEXT_HEIGHT (f)
7568 || event->xconfigure.width != FRAME_PIXEL_WIDTH (f)
7569 || event->xconfigure.height != FRAME_PIXEL_HEIGHT (f))
7570 {
7571 change_frame_size (f, width, height, false, true, false, true);
7572 x_clear_under_internal_border (f);
7573 SET_FRAME_GARBAGED (f);
7574 cancel_mouse_face (f);
7575 }
7576 #endif /* not USE_GTK */
7577 #endif
7578
7579 #ifdef USE_GTK
7580 /* GTK creates windows but doesn't map them.
7581 Only get real positions when mapped. */
7582 if (FRAME_GTK_OUTER_WIDGET (f)
7583 && gtk_widget_get_mapped (FRAME_GTK_OUTER_WIDGET (f)))
7584 #endif
7585 x_real_positions (f, &f->left_pos, &f->top_pos);
7586
7587 #ifdef HAVE_X_I18N
7588 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
7589 xic_set_statusarea (f);
7590 #endif
7591
7592 }
7593 goto OTHER;
7594
7595 case ButtonRelease:
7596 case ButtonPress:
7597 {
7598 /* If we decide we want to generate an event to be seen
7599 by the rest of Emacs, we put it here. */
7600 bool tool_bar_p = false;
7601
7602 memset (&compose_status, 0, sizeof (compose_status));
7603 dpyinfo->last_mouse_glyph_frame = NULL;
7604 x_display_set_last_user_time (dpyinfo, event->xbutton.time);
7605
7606 f = (x_mouse_grabbed (dpyinfo) ? dpyinfo->last_mouse_frame
7607 : x_window_to_frame (dpyinfo, event->xbutton.window));
7608
7609 #ifdef USE_GTK
7610 if (f && xg_event_is_for_scrollbar (f, event))
7611 f = 0;
7612 #endif
7613 if (f)
7614 {
7615 #if ! defined (USE_GTK)
7616 /* Is this in the tool-bar? */
7617 if (WINDOWP (f->tool_bar_window)
7618 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
7619 {
7620 Lisp_Object window;
7621 int x = event->xbutton.x;
7622 int y = event->xbutton.y;
7623
7624 window = window_from_coordinates (f, x, y, 0, true);
7625 tool_bar_p = EQ (window, f->tool_bar_window);
7626
7627 if (tool_bar_p && event->xbutton.button < 4)
7628 handle_tool_bar_click
7629 (f, x, y, event->xbutton.type == ButtonPress,
7630 x_x_to_emacs_modifiers (dpyinfo, event->xbutton.state));
7631 }
7632 #endif /* !USE_GTK */
7633
7634 if (!tool_bar_p)
7635 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
7636 if (! popup_activated ())
7637 #endif
7638 {
7639 if (ignore_next_mouse_click_timeout)
7640 {
7641 if (event->type == ButtonPress
7642 && event->xbutton.time > ignore_next_mouse_click_timeout)
7643 {
7644 ignore_next_mouse_click_timeout = 0;
7645 construct_mouse_click (&inev.ie, &event->xbutton, f);
7646 }
7647 if (event->type == ButtonRelease)
7648 ignore_next_mouse_click_timeout = 0;
7649 }
7650 else
7651 construct_mouse_click (&inev.ie, &event->xbutton, f);
7652 }
7653 if (FRAME_X_EMBEDDED_P (f))
7654 xembed_send_message (f, event->xbutton.time,
7655 XEMBED_REQUEST_FOCUS, 0, 0, 0);
7656 }
7657 else
7658 {
7659 struct scroll_bar *bar
7660 = x_window_to_scroll_bar (event->xbutton.display,
7661 event->xbutton.window, 2);
7662
7663 #ifdef USE_TOOLKIT_SCROLL_BARS
7664 /* Make the "Ctrl-Mouse-2 splits window" work for toolkit
7665 scroll bars. */
7666 if (bar && event->xbutton.state & ControlMask)
7667 {
7668 x_scroll_bar_handle_click (bar, event, &inev.ie);
7669 *finish = X_EVENT_DROP;
7670 }
7671 #else /* not USE_TOOLKIT_SCROLL_BARS */
7672 if (bar)
7673 x_scroll_bar_handle_click (bar, event, &inev.ie);
7674 #endif /* not USE_TOOLKIT_SCROLL_BARS */
7675 }
7676
7677 if (event->type == ButtonPress)
7678 {
7679 dpyinfo->grabbed |= (1 << event->xbutton.button);
7680 dpyinfo->last_mouse_frame = f;
7681 #if ! defined (USE_GTK)
7682 if (f && !tool_bar_p)
7683 f->last_tool_bar_item = -1;
7684 #endif /* not USE_GTK */
7685 }
7686 else
7687 dpyinfo->grabbed &= ~(1 << event->xbutton.button);
7688
7689 /* Ignore any mouse motion that happened before this event;
7690 any subsequent mouse-movement Emacs events should reflect
7691 only motion after the ButtonPress/Release. */
7692 if (f != 0)
7693 f->mouse_moved = false;
7694
7695 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
7696 f = x_menubar_window_to_frame (dpyinfo, event);
7697 /* For a down-event in the menu bar,
7698 don't pass it to Xt right now.
7699 Instead, save it away
7700 and we will pass it to Xt from kbd_buffer_get_event.
7701 That way, we can run some Lisp code first. */
7702 if (! popup_activated ()
7703 #ifdef USE_GTK
7704 /* Gtk+ menus only react to the first three buttons. */
7705 && event->xbutton.button < 3
7706 #endif
7707 && f && event->type == ButtonPress
7708 /* Verify the event is really within the menu bar
7709 and not just sent to it due to grabbing. */
7710 && event->xbutton.x >= 0
7711 && event->xbutton.x < FRAME_PIXEL_WIDTH (f)
7712 && event->xbutton.y >= 0
7713 && event->xbutton.y < FRAME_MENUBAR_HEIGHT (f)
7714 && event->xbutton.same_screen)
7715 {
7716 if (!f->output_data.x->saved_menu_event)
7717 f->output_data.x->saved_menu_event = xmalloc (sizeof *event);
7718 *f->output_data.x->saved_menu_event = *event;
7719 inev.ie.kind = MENU_BAR_ACTIVATE_EVENT;
7720 XSETFRAME (inev.ie.frame_or_window, f);
7721 *finish = X_EVENT_DROP;
7722 }
7723 else
7724 goto OTHER;
7725 #endif /* USE_X_TOOLKIT || USE_GTK */
7726 }
7727 break;
7728
7729 case CirculateNotify:
7730 goto OTHER;
7731
7732 case CirculateRequest:
7733 goto OTHER;
7734
7735 case VisibilityNotify:
7736 goto OTHER;
7737
7738 case MappingNotify:
7739 /* Someone has changed the keyboard mapping - update the
7740 local cache. */
7741 switch (event->xmapping.request)
7742 {
7743 case MappingModifier:
7744 x_find_modifier_meanings (dpyinfo);
7745 /* This is meant to fall through. */
7746 case MappingKeyboard:
7747 XRefreshKeyboardMapping ((XMappingEvent *) &event->xmapping);
7748 }
7749 goto OTHER;
7750
7751 case DestroyNotify:
7752 xft_settings_event (dpyinfo, event);
7753 break;
7754
7755 default:
7756 OTHER:
7757 #ifdef USE_X_TOOLKIT
7758 block_input ();
7759 if (*finish != X_EVENT_DROP)
7760 XtDispatchEvent ((XEvent *) event);
7761 unblock_input ();
7762 #endif /* USE_X_TOOLKIT */
7763 break;
7764 }
7765
7766 done:
7767 if (inev.ie.kind != NO_EVENT)
7768 {
7769 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
7770 count++;
7771 }
7772
7773 if (do_help
7774 && !(hold_quit && hold_quit->kind != NO_EVENT))
7775 {
7776 Lisp_Object frame;
7777
7778 if (f)
7779 XSETFRAME (frame, f);
7780 else
7781 frame = Qnil;
7782
7783 if (do_help > 0)
7784 {
7785 any_help_event_p = true;
7786 gen_help_event (help_echo_string, frame, help_echo_window,
7787 help_echo_object, help_echo_pos);
7788 }
7789 else
7790 {
7791 help_echo_string = Qnil;
7792 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
7793 }
7794 count++;
7795 }
7796
7797 SAFE_FREE ();
7798 return count;
7799 }
7800
7801 /* Handles the XEvent EVENT on display DISPLAY.
7802 This is used for event loops outside the normal event handling,
7803 i.e. looping while a popup menu or a dialog is posted.
7804
7805 Returns the value handle_one_xevent sets in the finish argument. */
7806 int
7807 x_dispatch_event (XEvent *event, Display *display)
7808 {
7809 struct x_display_info *dpyinfo;
7810 int finish = X_EVENT_NORMAL;
7811
7812 dpyinfo = x_display_info_for_display (display);
7813
7814 if (dpyinfo)
7815 handle_one_xevent (dpyinfo, event, &finish, 0);
7816
7817 return finish;
7818 }
7819
7820 /* Read events coming from the X server.
7821 Return as soon as there are no more events to be read.
7822
7823 Return the number of characters stored into the buffer,
7824 thus pretending to be `read' (except the characters we store
7825 in the keyboard buffer can be multibyte, so are not necessarily
7826 C chars). */
7827
7828 static int
7829 XTread_socket (struct terminal *terminal, struct input_event *hold_quit)
7830 {
7831 int count = 0;
7832 bool event_found = false;
7833 struct x_display_info *dpyinfo = terminal->display_info.x;
7834
7835 block_input ();
7836
7837 /* For debugging, this gives a way to fake an I/O error. */
7838 if (dpyinfo == XTread_socket_fake_io_error)
7839 {
7840 XTread_socket_fake_io_error = 0;
7841 x_io_error_quitter (dpyinfo->display);
7842 }
7843
7844 #ifndef USE_GTK
7845 while (XPending (dpyinfo->display))
7846 {
7847 int finish;
7848 XEvent event;
7849
7850 XNextEvent (dpyinfo->display, &event);
7851
7852 #ifdef HAVE_X_I18N
7853 /* Filter events for the current X input method. */
7854 if (x_filter_event (dpyinfo, &event))
7855 continue;
7856 #endif
7857 event_found = true;
7858
7859 count += handle_one_xevent (dpyinfo, &event, &finish, hold_quit);
7860
7861 if (finish == X_EVENT_GOTO_OUT)
7862 break;
7863 }
7864
7865 #else /* USE_GTK */
7866
7867 /* For GTK we must use the GTK event loop. But XEvents gets passed
7868 to our filter function above, and then to the big event switch.
7869 We use a bunch of globals to communicate with our filter function,
7870 that is kind of ugly, but it works.
7871
7872 There is no way to do one display at the time, GTK just does events
7873 from all displays. */
7874
7875 while (gtk_events_pending ())
7876 {
7877 current_count = count;
7878 current_hold_quit = hold_quit;
7879
7880 gtk_main_iteration ();
7881
7882 count = current_count;
7883 current_count = -1;
7884 current_hold_quit = 0;
7885
7886 if (current_finish == X_EVENT_GOTO_OUT)
7887 break;
7888 }
7889 #endif /* USE_GTK */
7890
7891 /* On some systems, an X bug causes Emacs to get no more events
7892 when the window is destroyed. Detect that. (1994.) */
7893 if (! event_found)
7894 {
7895 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
7896 One XNOOP in 100 loops will make Emacs terminate.
7897 B. Bretthauer, 1994 */
7898 x_noop_count++;
7899 if (x_noop_count >= 100)
7900 {
7901 x_noop_count=0;
7902
7903 if (next_noop_dpyinfo == 0)
7904 next_noop_dpyinfo = x_display_list;
7905
7906 XNoOp (next_noop_dpyinfo->display);
7907
7908 /* Each time we get here, cycle through the displays now open. */
7909 next_noop_dpyinfo = next_noop_dpyinfo->next;
7910 }
7911 }
7912
7913 /* If the focus was just given to an auto-raising frame,
7914 raise it now. FIXME: handle more than one such frame. */
7915 if (dpyinfo->x_pending_autoraise_frame)
7916 {
7917 x_raise_frame (dpyinfo->x_pending_autoraise_frame);
7918 dpyinfo->x_pending_autoraise_frame = NULL;
7919 }
7920
7921 unblock_input ();
7922
7923 return count;
7924 }
7925
7926
7927
7928 \f
7929 /***********************************************************************
7930 Text Cursor
7931 ***********************************************************************/
7932
7933 /* Set clipping for output in glyph row ROW. W is the window in which
7934 we operate. GC is the graphics context to set clipping in.
7935
7936 ROW may be a text row or, e.g., a mode line. Text rows must be
7937 clipped to the interior of the window dedicated to text display,
7938 mode lines must be clipped to the whole window. */
7939
7940 static void
7941 x_clip_to_row (struct window *w, struct glyph_row *row,
7942 enum glyph_row_area area, GC gc)
7943 {
7944 struct frame *f = XFRAME (WINDOW_FRAME (w));
7945 XRectangle clip_rect;
7946 int window_x, window_y, window_width;
7947
7948 window_box (w, area, &window_x, &window_y, &window_width, 0);
7949
7950 clip_rect.x = window_x;
7951 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, row->y));
7952 clip_rect.y = max (clip_rect.y, window_y);
7953 clip_rect.width = window_width;
7954 clip_rect.height = row->visible_height;
7955
7956 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
7957 }
7958
7959
7960 /* Draw a hollow box cursor on window W in glyph row ROW. */
7961
7962 static void
7963 x_draw_hollow_cursor (struct window *w, struct glyph_row *row)
7964 {
7965 struct frame *f = XFRAME (WINDOW_FRAME (w));
7966 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
7967 Display *dpy = FRAME_X_DISPLAY (f);
7968 int x, y, wd, h;
7969 XGCValues xgcv;
7970 struct glyph *cursor_glyph;
7971 GC gc;
7972
7973 /* Get the glyph the cursor is on. If we can't tell because
7974 the current matrix is invalid or such, give up. */
7975 cursor_glyph = get_phys_cursor_glyph (w);
7976 if (cursor_glyph == NULL)
7977 return;
7978
7979 /* Compute frame-relative coordinates for phys cursor. */
7980 get_phys_cursor_geometry (w, row, cursor_glyph, &x, &y, &h);
7981 wd = w->phys_cursor_width - 1;
7982
7983 /* The foreground of cursor_gc is typically the same as the normal
7984 background color, which can cause the cursor box to be invisible. */
7985 xgcv.foreground = f->output_data.x->cursor_pixel;
7986 if (dpyinfo->scratch_cursor_gc)
7987 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
7988 else
7989 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
7990 GCForeground, &xgcv);
7991 gc = dpyinfo->scratch_cursor_gc;
7992
7993 /* When on R2L character, show cursor at the right edge of the
7994 glyph, unless the cursor box is as wide as the glyph or wider
7995 (the latter happens when x-stretch-cursor is non-nil). */
7996 if ((cursor_glyph->resolved_level & 1) != 0
7997 && cursor_glyph->pixel_width > wd)
7998 {
7999 x += cursor_glyph->pixel_width - wd;
8000 if (wd > 0)
8001 wd -= 1;
8002 }
8003 /* Set clipping, draw the rectangle, and reset clipping again. */
8004 x_clip_to_row (w, row, TEXT_AREA, gc);
8005 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h - 1);
8006 XSetClipMask (dpy, gc, None);
8007 }
8008
8009
8010 /* Draw a bar cursor on window W in glyph row ROW.
8011
8012 Implementation note: One would like to draw a bar cursor with an
8013 angle equal to the one given by the font property XA_ITALIC_ANGLE.
8014 Unfortunately, I didn't find a font yet that has this property set.
8015 --gerd. */
8016
8017 static void
8018 x_draw_bar_cursor (struct window *w, struct glyph_row *row, int width, enum text_cursor_kinds kind)
8019 {
8020 struct frame *f = XFRAME (w->frame);
8021 struct glyph *cursor_glyph;
8022
8023 /* If cursor is out of bounds, don't draw garbage. This can happen
8024 in mini-buffer windows when switching between echo area glyphs
8025 and mini-buffer. */
8026 cursor_glyph = get_phys_cursor_glyph (w);
8027 if (cursor_glyph == NULL)
8028 return;
8029
8030 /* If on an image, draw like a normal cursor. That's usually better
8031 visible than drawing a bar, esp. if the image is large so that
8032 the bar might not be in the window. */
8033 if (cursor_glyph->type == IMAGE_GLYPH)
8034 {
8035 struct glyph_row *r;
8036 r = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
8037 draw_phys_cursor_glyph (w, r, DRAW_CURSOR);
8038 }
8039 else
8040 {
8041 Display *dpy = FRAME_X_DISPLAY (f);
8042 Window window = FRAME_X_WINDOW (f);
8043 GC gc = FRAME_DISPLAY_INFO (f)->scratch_cursor_gc;
8044 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
8045 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
8046 XGCValues xgcv;
8047
8048 /* If the glyph's background equals the color we normally draw
8049 the bars cursor in, the bar cursor in its normal color is
8050 invisible. Use the glyph's foreground color instead in this
8051 case, on the assumption that the glyph's colors are chosen so
8052 that the glyph is legible. */
8053 if (face->background == f->output_data.x->cursor_pixel)
8054 xgcv.background = xgcv.foreground = face->foreground;
8055 else
8056 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
8057 xgcv.graphics_exposures = False;
8058
8059 if (gc)
8060 XChangeGC (dpy, gc, mask, &xgcv);
8061 else
8062 {
8063 gc = XCreateGC (dpy, window, mask, &xgcv);
8064 FRAME_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
8065 }
8066
8067 x_clip_to_row (w, row, TEXT_AREA, gc);
8068
8069 if (kind == BAR_CURSOR)
8070 {
8071 int x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
8072
8073 if (width < 0)
8074 width = FRAME_CURSOR_WIDTH (f);
8075 width = min (cursor_glyph->pixel_width, width);
8076
8077 w->phys_cursor_width = width;
8078
8079 /* If the character under cursor is R2L, draw the bar cursor
8080 on the right of its glyph, rather than on the left. */
8081 if ((cursor_glyph->resolved_level & 1) != 0)
8082 x += cursor_glyph->pixel_width - width;
8083
8084 XFillRectangle (dpy, window, gc, x,
8085 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
8086 width, row->height);
8087 }
8088 else /* HBAR_CURSOR */
8089 {
8090 int dummy_x, dummy_y, dummy_h;
8091 int x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
8092
8093 if (width < 0)
8094 width = row->height;
8095
8096 width = min (row->height, width);
8097
8098 get_phys_cursor_geometry (w, row, cursor_glyph, &dummy_x,
8099 &dummy_y, &dummy_h);
8100
8101 if ((cursor_glyph->resolved_level & 1) != 0
8102 && cursor_glyph->pixel_width > w->phys_cursor_width - 1)
8103 x += cursor_glyph->pixel_width - w->phys_cursor_width + 1;
8104 XFillRectangle (dpy, window, gc, x,
8105 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
8106 row->height - width),
8107 w->phys_cursor_width - 1, width);
8108 }
8109
8110 XSetClipMask (dpy, gc, None);
8111 }
8112 }
8113
8114
8115 /* RIF: Define cursor CURSOR on frame F. */
8116
8117 static void
8118 x_define_frame_cursor (struct frame *f, Cursor cursor)
8119 {
8120 if (!f->pointer_invisible
8121 && f->output_data.x->current_cursor != cursor)
8122 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
8123 f->output_data.x->current_cursor = cursor;
8124 }
8125
8126
8127 /* RIF: Clear area on frame F. */
8128
8129 static void
8130 x_clear_frame_area (struct frame *f, int x, int y, int width, int height)
8131 {
8132 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), x, y, width, height);
8133 #ifdef USE_GTK
8134 /* Must queue a redraw, because scroll bars might have been cleared. */
8135 if (FRAME_GTK_WIDGET (f))
8136 gtk_widget_queue_draw (FRAME_GTK_WIDGET (f));
8137 #endif
8138 }
8139
8140
8141 /* RIF: Draw cursor on window W. */
8142
8143 static void
8144 x_draw_window_cursor (struct window *w, struct glyph_row *glyph_row, int x,
8145 int y, enum text_cursor_kinds cursor_type,
8146 int cursor_width, bool on_p, bool active_p)
8147 {
8148 struct frame *f = XFRAME (WINDOW_FRAME (w));
8149
8150 if (on_p)
8151 {
8152 w->phys_cursor_type = cursor_type;
8153 w->phys_cursor_on_p = true;
8154
8155 if (glyph_row->exact_window_width_line_p
8156 && (glyph_row->reversed_p
8157 ? (w->phys_cursor.hpos < 0)
8158 : (w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])))
8159 {
8160 glyph_row->cursor_in_fringe_p = true;
8161 draw_fringe_bitmap (w, glyph_row, glyph_row->reversed_p);
8162 }
8163 else
8164 {
8165 switch (cursor_type)
8166 {
8167 case HOLLOW_BOX_CURSOR:
8168 x_draw_hollow_cursor (w, glyph_row);
8169 break;
8170
8171 case FILLED_BOX_CURSOR:
8172 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
8173 break;
8174
8175 case BAR_CURSOR:
8176 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
8177 break;
8178
8179 case HBAR_CURSOR:
8180 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
8181 break;
8182
8183 case NO_CURSOR:
8184 w->phys_cursor_width = 0;
8185 break;
8186
8187 default:
8188 emacs_abort ();
8189 }
8190 }
8191
8192 #ifdef HAVE_X_I18N
8193 if (w == XWINDOW (f->selected_window))
8194 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
8195 xic_set_preeditarea (w, x, y);
8196 #endif
8197 }
8198
8199 XFlush (FRAME_X_DISPLAY (f));
8200 }
8201
8202 \f
8203 /* Icons. */
8204
8205 /* Make the x-window of frame F use the gnu icon bitmap. */
8206
8207 bool
8208 x_bitmap_icon (struct frame *f, Lisp_Object file)
8209 {
8210 ptrdiff_t bitmap_id;
8211
8212 if (FRAME_X_WINDOW (f) == 0)
8213 return true;
8214
8215 /* Free up our existing icon bitmap and mask if any. */
8216 if (f->output_data.x->icon_bitmap > 0)
8217 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
8218 f->output_data.x->icon_bitmap = 0;
8219
8220 if (STRINGP (file))
8221 {
8222 #ifdef USE_GTK
8223 /* Use gtk_window_set_icon_from_file () if available,
8224 It's not restricted to bitmaps */
8225 if (xg_set_icon (f, file))
8226 return false;
8227 #endif /* USE_GTK */
8228 bitmap_id = x_create_bitmap_from_file (f, file);
8229 x_create_bitmap_mask (f, bitmap_id);
8230 }
8231 else
8232 {
8233 /* Create the GNU bitmap and mask if necessary. */
8234 if (FRAME_DISPLAY_INFO (f)->icon_bitmap_id < 0)
8235 {
8236 ptrdiff_t rc = -1;
8237
8238 #ifdef USE_GTK
8239
8240 if (xg_set_icon (f, xg_default_icon_file)
8241 || xg_set_icon_from_xpm_data (f, gnu_xpm_bits))
8242 {
8243 FRAME_DISPLAY_INFO (f)->icon_bitmap_id = -2;
8244 return false;
8245 }
8246
8247 #elif defined (HAVE_XPM) && defined (HAVE_X_WINDOWS)
8248
8249 rc = x_create_bitmap_from_xpm_data (f, gnu_xpm_bits);
8250 if (rc != -1)
8251 FRAME_DISPLAY_INFO (f)->icon_bitmap_id = rc;
8252
8253 #endif
8254
8255 /* If all else fails, use the (black and white) xbm image. */
8256 if (rc == -1)
8257 {
8258 rc = x_create_bitmap_from_data (f, (char *) gnu_xbm_bits,
8259 gnu_xbm_width, gnu_xbm_height);
8260 if (rc == -1)
8261 return true;
8262
8263 FRAME_DISPLAY_INFO (f)->icon_bitmap_id = rc;
8264 x_create_bitmap_mask (f, FRAME_DISPLAY_INFO (f)->icon_bitmap_id);
8265 }
8266 }
8267
8268 /* The first time we create the GNU bitmap and mask,
8269 this increments the ref-count one extra time.
8270 As a result, the GNU bitmap and mask are never freed.
8271 That way, we don't have to worry about allocating it again. */
8272 x_reference_bitmap (f, FRAME_DISPLAY_INFO (f)->icon_bitmap_id);
8273
8274 bitmap_id = FRAME_DISPLAY_INFO (f)->icon_bitmap_id;
8275 }
8276
8277 x_wm_set_icon_pixmap (f, bitmap_id);
8278 f->output_data.x->icon_bitmap = bitmap_id;
8279
8280 return false;
8281 }
8282
8283
8284 /* Make the x-window of frame F use a rectangle with text.
8285 Use ICON_NAME as the text. */
8286
8287 bool
8288 x_text_icon (struct frame *f, const char *icon_name)
8289 {
8290 if (FRAME_X_WINDOW (f) == 0)
8291 return true;
8292
8293 {
8294 XTextProperty text;
8295 text.value = (unsigned char *) icon_name;
8296 text.encoding = XA_STRING;
8297 text.format = 8;
8298 text.nitems = strlen (icon_name);
8299 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
8300 }
8301
8302 if (f->output_data.x->icon_bitmap > 0)
8303 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
8304 f->output_data.x->icon_bitmap = 0;
8305 x_wm_set_icon_pixmap (f, 0);
8306
8307 return false;
8308 }
8309 \f
8310 #define X_ERROR_MESSAGE_SIZE 200
8311
8312 /* If non-nil, this should be a string.
8313 It means catch X errors and store the error message in this string.
8314
8315 The reason we use a stack is that x_catch_error/x_uncatch_error can
8316 be called from a signal handler.
8317 */
8318
8319 struct x_error_message_stack {
8320 char string[X_ERROR_MESSAGE_SIZE];
8321 Display *dpy;
8322 struct x_error_message_stack *prev;
8323 };
8324 static struct x_error_message_stack *x_error_message;
8325
8326 /* An X error handler which stores the error message in
8327 *x_error_message. This is called from x_error_handler if
8328 x_catch_errors is in effect. */
8329
8330 static void
8331 x_error_catcher (Display *display, XErrorEvent *event)
8332 {
8333 XGetErrorText (display, event->error_code,
8334 x_error_message->string,
8335 X_ERROR_MESSAGE_SIZE);
8336 }
8337
8338 /* Begin trapping X errors for display DPY. Actually we trap X errors
8339 for all displays, but DPY should be the display you are actually
8340 operating on.
8341
8342 After calling this function, X protocol errors no longer cause
8343 Emacs to exit; instead, they are recorded in the string
8344 stored in *x_error_message.
8345
8346 Calling x_check_errors signals an Emacs error if an X error has
8347 occurred since the last call to x_catch_errors or x_check_errors.
8348
8349 Calling x_uncatch_errors resumes the normal error handling. */
8350
8351 void
8352 x_catch_errors (Display *dpy)
8353 {
8354 struct x_error_message_stack *data = xmalloc (sizeof *data);
8355
8356 /* Make sure any errors from previous requests have been dealt with. */
8357 XSync (dpy, False);
8358
8359 data->dpy = dpy;
8360 data->string[0] = 0;
8361 data->prev = x_error_message;
8362 x_error_message = data;
8363 }
8364
8365 /* Undo the last x_catch_errors call.
8366 DPY should be the display that was passed to x_catch_errors. */
8367
8368 void
8369 x_uncatch_errors (void)
8370 {
8371 struct x_error_message_stack *tmp;
8372
8373 block_input ();
8374
8375 /* The display may have been closed before this function is called.
8376 Check if it is still open before calling XSync. */
8377 if (x_display_info_for_display (x_error_message->dpy) != 0)
8378 XSync (x_error_message->dpy, False);
8379
8380 tmp = x_error_message;
8381 x_error_message = x_error_message->prev;
8382 xfree (tmp);
8383 unblock_input ();
8384 }
8385
8386 /* If any X protocol errors have arrived since the last call to
8387 x_catch_errors or x_check_errors, signal an Emacs error using
8388 sprintf (a buffer, FORMAT, the x error message text) as the text. */
8389
8390 void
8391 x_check_errors (Display *dpy, const char *format)
8392 {
8393 /* Make sure to catch any errors incurred so far. */
8394 XSync (dpy, False);
8395
8396 if (x_error_message->string[0])
8397 {
8398 char string[X_ERROR_MESSAGE_SIZE];
8399 memcpy (string, x_error_message->string, X_ERROR_MESSAGE_SIZE);
8400 x_uncatch_errors ();
8401 error (format, string);
8402 }
8403 }
8404
8405 /* Nonzero if we had any X protocol errors
8406 since we did x_catch_errors on DPY. */
8407
8408 bool
8409 x_had_errors_p (Display *dpy)
8410 {
8411 /* Make sure to catch any errors incurred so far. */
8412 XSync (dpy, False);
8413
8414 return x_error_message->string[0] != 0;
8415 }
8416
8417 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
8418
8419 void
8420 x_clear_errors (Display *dpy)
8421 {
8422 x_error_message->string[0] = 0;
8423 }
8424
8425 #if false
8426 /* See comment in unwind_to_catch why calling this is a bad
8427 * idea. --lorentey */
8428 /* Close off all unclosed x_catch_errors calls. */
8429
8430 void
8431 x_fully_uncatch_errors (void)
8432 {
8433 while (x_error_message)
8434 x_uncatch_errors ();
8435 }
8436 #endif
8437
8438 #if false
8439 static unsigned int x_wire_count;
8440 x_trace_wire (void)
8441 {
8442 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
8443 }
8444 #endif
8445
8446 \f
8447 /************************************************************************
8448 Handling X errors
8449 ************************************************************************/
8450
8451 /* Error message passed to x_connection_closed. */
8452
8453 static char *error_msg;
8454
8455 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
8456 the text of an error message that lead to the connection loss. */
8457
8458 static void
8459 x_connection_closed (Display *dpy, const char *error_message, bool ioerror)
8460 {
8461 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
8462 Lisp_Object frame, tail;
8463 ptrdiff_t idx = SPECPDL_INDEX ();
8464
8465 error_msg = alloca (strlen (error_message) + 1);
8466 strcpy (error_msg, error_message);
8467
8468 /* Inhibit redisplay while frames are being deleted. */
8469 specbind (Qinhibit_redisplay, Qt);
8470
8471 if (dpyinfo)
8472 {
8473 /* Protect display from being closed when we delete the last
8474 frame on it. */
8475 dpyinfo->reference_count++;
8476 dpyinfo->terminal->reference_count++;
8477 }
8478 if (ioerror) dpyinfo->display = 0;
8479
8480 /* First delete frames whose mini-buffers are on frames
8481 that are on the dead display. */
8482 FOR_EACH_FRAME (tail, frame)
8483 {
8484 Lisp_Object minibuf_frame;
8485 minibuf_frame
8486 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
8487 if (FRAME_X_P (XFRAME (frame))
8488 && FRAME_X_P (XFRAME (minibuf_frame))
8489 && ! EQ (frame, minibuf_frame)
8490 && FRAME_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
8491 delete_frame (frame, Qnoelisp);
8492 }
8493
8494 /* Now delete all remaining frames on the dead display.
8495 We are now sure none of these is used as the mini-buffer
8496 for another frame that we need to delete. */
8497 FOR_EACH_FRAME (tail, frame)
8498 if (FRAME_X_P (XFRAME (frame))
8499 && FRAME_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
8500 {
8501 /* Set this to t so that delete_frame won't get confused
8502 trying to find a replacement. */
8503 kset_default_minibuffer_frame (FRAME_KBOARD (XFRAME (frame)), Qt);
8504 delete_frame (frame, Qnoelisp);
8505 }
8506
8507 /* If DPYINFO is null, this means we didn't open the display in the
8508 first place, so don't try to close it. */
8509 if (dpyinfo)
8510 {
8511 /* We can not call XtCloseDisplay here because it calls XSync.
8512 XSync inside the error handler apparently hangs Emacs. On
8513 current Xt versions, this isn't needed either. */
8514 #ifdef USE_GTK
8515 /* A long-standing GTK bug prevents proper disconnect handling
8516 (https://bugzilla.gnome.org/show_bug.cgi?id=85715). Once,
8517 the resulting Glib error message loop filled a user's disk.
8518 To avoid this, kill Emacs unconditionally on disconnect. */
8519 shut_down_emacs (0, Qnil);
8520 fprintf (stderr, "%s\n\
8521 When compiled with GTK, Emacs cannot recover from X disconnects.\n\
8522 This is a GTK bug: https://bugzilla.gnome.org/show_bug.cgi?id=85715\n\
8523 For details, see etc/PROBLEMS.\n",
8524 error_msg);
8525 emacs_abort ();
8526 #endif /* USE_GTK */
8527
8528 /* Indicate that this display is dead. */
8529 dpyinfo->display = 0;
8530
8531 dpyinfo->reference_count--;
8532 dpyinfo->terminal->reference_count--;
8533 if (dpyinfo->reference_count != 0)
8534 /* We have just closed all frames on this display. */
8535 emacs_abort ();
8536
8537 {
8538 Lisp_Object tmp;
8539 XSETTERMINAL (tmp, dpyinfo->terminal);
8540 Fdelete_terminal (tmp, Qnoelisp);
8541 }
8542 }
8543
8544 if (terminal_list == 0)
8545 {
8546 fprintf (stderr, "%s\n", error_msg);
8547 Fkill_emacs (make_number (70));
8548 /* NOTREACHED */
8549 }
8550
8551 totally_unblock_input ();
8552
8553 unbind_to (idx, Qnil);
8554 clear_waiting_for_input ();
8555
8556 /* Tell GCC not to suggest attribute 'noreturn' for this function. */
8557 IF_LINT (if (! terminal_list) return; )
8558
8559 /* Here, we absolutely have to use a non-local exit (e.g. signal, throw,
8560 longjmp), because returning from this function would get us back into
8561 Xlib's code which will directly call `exit'. */
8562 error ("%s", error_msg);
8563 }
8564
8565 /* We specifically use it before defining it, so that gcc doesn't inline it,
8566 otherwise gdb doesn't know how to properly put a breakpoint on it. */
8567 static void x_error_quitter (Display *, XErrorEvent *);
8568
8569 /* This is the first-level handler for X protocol errors.
8570 It calls x_error_quitter or x_error_catcher. */
8571
8572 static int
8573 x_error_handler (Display *display, XErrorEvent *event)
8574 {
8575 #if defined USE_GTK && defined HAVE_GTK3
8576 if ((event->error_code == BadMatch || event->error_code == BadWindow)
8577 && event->request_code == X_SetInputFocus)
8578 {
8579 return 0;
8580 }
8581 #endif
8582
8583 if (x_error_message)
8584 x_error_catcher (display, event);
8585 else
8586 x_error_quitter (display, event);
8587 return 0;
8588 }
8589
8590 /* This is the usual handler for X protocol errors.
8591 It kills all frames on the display that we got the error for.
8592 If that was the only one, it prints an error message and kills Emacs. */
8593
8594 /* .gdbinit puts a breakpoint here, so make sure it is not inlined. */
8595
8596 /* On older GCC versions, just putting x_error_quitter
8597 after x_error_handler prevents inlining into the former. */
8598
8599 static void NO_INLINE
8600 x_error_quitter (Display *display, XErrorEvent *event)
8601 {
8602 char buf[256], buf1[356];
8603
8604 /* Ignore BadName errors. They can happen because of fonts
8605 or colors that are not defined. */
8606
8607 if (event->error_code == BadName)
8608 return;
8609
8610 /* Note that there is no real way portable across R3/R4 to get the
8611 original error handler. */
8612
8613 XGetErrorText (display, event->error_code, buf, sizeof (buf));
8614 sprintf (buf1, "X protocol error: %s on protocol request %d",
8615 buf, event->request_code);
8616 x_connection_closed (display, buf1, false);
8617 }
8618
8619
8620 /* This is the handler for X IO errors, always.
8621 It kills all frames on the display that we lost touch with.
8622 If that was the only one, it prints an error message and kills Emacs. */
8623
8624 static int
8625 x_io_error_quitter (Display *display)
8626 {
8627 char buf[256];
8628
8629 snprintf (buf, sizeof buf, "Connection lost to X server `%s'",
8630 DisplayString (display));
8631 x_connection_closed (display, buf, true);
8632 return 0;
8633 }
8634 \f
8635 /* Changing the font of the frame. */
8636
8637 /* Give frame F the font FONT-OBJECT as its default font. The return
8638 value is FONT-OBJECT. FONTSET is an ID of the fontset for the
8639 frame. If it is negative, generate a new fontset from
8640 FONT-OBJECT. */
8641
8642 Lisp_Object
8643 x_new_font (struct frame *f, Lisp_Object font_object, int fontset)
8644 {
8645 struct font *font = XFONT_OBJECT (font_object);
8646 int unit;
8647
8648 if (fontset < 0)
8649 fontset = fontset_from_font (font_object);
8650 FRAME_FONTSET (f) = fontset;
8651 if (FRAME_FONT (f) == font)
8652 /* This font is already set in frame F. There's nothing more to
8653 do. */
8654 return font_object;
8655
8656 FRAME_FONT (f) = font;
8657 FRAME_BASELINE_OFFSET (f) = font->baseline_offset;
8658 FRAME_COLUMN_WIDTH (f) = font->average_width;
8659 FRAME_LINE_HEIGHT (f) = FONT_HEIGHT (font);
8660
8661 #ifndef USE_X_TOOLKIT
8662 FRAME_MENU_BAR_HEIGHT (f) = FRAME_MENU_BAR_LINES (f) * FRAME_LINE_HEIGHT (f);
8663 #endif
8664
8665 /* Compute character columns occupied by scrollbar.
8666
8667 Don't do things differently for non-toolkit scrollbars
8668 (Bug#17163). */
8669 unit = FRAME_COLUMN_WIDTH (f);
8670 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
8671 FRAME_CONFIG_SCROLL_BAR_COLS (f)
8672 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + unit - 1) / unit;
8673 else
8674 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + unit - 1) / unit;
8675
8676 if (FRAME_X_WINDOW (f) != 0)
8677 {
8678 /* Don't change the size of a tip frame; there's no point in
8679 doing it because it's done in Fx_show_tip, and it leads to
8680 problems because the tip frame has no widget. */
8681 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
8682 adjust_frame_size (f, FRAME_COLS (f) * FRAME_COLUMN_WIDTH (f),
8683 FRAME_LINES (f) * FRAME_LINE_HEIGHT (f), 3,
8684 false, Qfont);
8685 }
8686
8687 #ifdef HAVE_X_I18N
8688 if (FRAME_XIC (f)
8689 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
8690 {
8691 block_input ();
8692 xic_set_xfontset (f, SSDATA (fontset_ascii (fontset)));
8693 unblock_input ();
8694 }
8695 #endif
8696
8697 return font_object;
8698 }
8699
8700 \f
8701 /***********************************************************************
8702 X Input Methods
8703 ***********************************************************************/
8704
8705 #ifdef HAVE_X_I18N
8706
8707 #ifdef HAVE_X11R6
8708
8709 /* XIM destroy callback function, which is called whenever the
8710 connection to input method XIM dies. CLIENT_DATA contains a
8711 pointer to the x_display_info structure corresponding to XIM. */
8712
8713 static void
8714 xim_destroy_callback (XIM xim, XPointer client_data, XPointer call_data)
8715 {
8716 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
8717 Lisp_Object frame, tail;
8718
8719 block_input ();
8720
8721 /* No need to call XDestroyIC.. */
8722 FOR_EACH_FRAME (tail, frame)
8723 {
8724 struct frame *f = XFRAME (frame);
8725 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo)
8726 {
8727 FRAME_XIC (f) = NULL;
8728 xic_free_xfontset (f);
8729 }
8730 }
8731
8732 /* No need to call XCloseIM. */
8733 dpyinfo->xim = NULL;
8734 XFree (dpyinfo->xim_styles);
8735 unblock_input ();
8736 }
8737
8738 #endif /* HAVE_X11R6 */
8739
8740 /* Open the connection to the XIM server on display DPYINFO.
8741 RESOURCE_NAME is the resource name Emacs uses. */
8742
8743 static void
8744 xim_open_dpy (struct x_display_info *dpyinfo, char *resource_name)
8745 {
8746 XIM xim;
8747
8748 #ifdef HAVE_XIM
8749 if (use_xim)
8750 {
8751 if (dpyinfo->xim)
8752 XCloseIM (dpyinfo->xim);
8753 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name,
8754 emacs_class);
8755 dpyinfo->xim = xim;
8756
8757 if (xim)
8758 {
8759 #ifdef HAVE_X11R6
8760 XIMCallback destroy;
8761 #endif
8762
8763 /* Get supported styles and XIM values. */
8764 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
8765
8766 #ifdef HAVE_X11R6
8767 destroy.callback = xim_destroy_callback;
8768 destroy.client_data = (XPointer)dpyinfo;
8769 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
8770 #endif
8771 }
8772 }
8773
8774 else
8775 #endif /* HAVE_XIM */
8776 dpyinfo->xim = NULL;
8777 }
8778
8779
8780 #ifdef HAVE_X11R6_XIM
8781
8782 /* XIM instantiate callback function, which is called whenever an XIM
8783 server is available. DISPLAY is the display of the XIM.
8784 CLIENT_DATA contains a pointer to an xim_inst_t structure created
8785 when the callback was registered. */
8786
8787 static void
8788 xim_instantiate_callback (Display *display, XPointer client_data, XPointer call_data)
8789 {
8790 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
8791 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
8792
8793 /* We don't support multiple XIM connections. */
8794 if (dpyinfo->xim)
8795 return;
8796
8797 xim_open_dpy (dpyinfo, xim_inst->resource_name);
8798
8799 /* Create XIC for the existing frames on the same display, as long
8800 as they have no XIC. */
8801 if (dpyinfo->xim && dpyinfo->reference_count > 0)
8802 {
8803 Lisp_Object tail, frame;
8804
8805 block_input ();
8806 FOR_EACH_FRAME (tail, frame)
8807 {
8808 struct frame *f = XFRAME (frame);
8809
8810 if (FRAME_X_P (f)
8811 && FRAME_DISPLAY_INFO (f) == xim_inst->dpyinfo)
8812 if (FRAME_XIC (f) == NULL)
8813 {
8814 create_frame_xic (f);
8815 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
8816 xic_set_statusarea (f);
8817 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
8818 {
8819 struct window *w = XWINDOW (f->selected_window);
8820 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
8821 }
8822 }
8823 }
8824
8825 unblock_input ();
8826 }
8827 }
8828
8829 #endif /* HAVE_X11R6_XIM */
8830
8831
8832 /* Open a connection to the XIM server on display DPYINFO.
8833 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
8834 connection only at the first time. On X11R6, open the connection
8835 in the XIM instantiate callback function. */
8836
8837 static void
8838 xim_initialize (struct x_display_info *dpyinfo, char *resource_name)
8839 {
8840 dpyinfo->xim = NULL;
8841 #ifdef HAVE_XIM
8842 if (use_xim)
8843 {
8844 #ifdef HAVE_X11R6_XIM
8845 struct xim_inst_t *xim_inst = xmalloc (sizeof *xim_inst);
8846 Bool ret;
8847
8848 dpyinfo->xim_callback_data = xim_inst;
8849 xim_inst->dpyinfo = dpyinfo;
8850 xim_inst->resource_name = xstrdup (resource_name);
8851 ret = XRegisterIMInstantiateCallback
8852 (dpyinfo->display, dpyinfo->xrdb, xim_inst->resource_name,
8853 emacs_class, xim_instantiate_callback,
8854 /* This is XPointer in XFree86 but (XPointer *)
8855 on Tru64, at least, hence the configure test. */
8856 (XRegisterIMInstantiateCallback_arg6) xim_inst);
8857 eassert (ret == True);
8858 #else /* not HAVE_X11R6_XIM */
8859 xim_open_dpy (dpyinfo, resource_name);
8860 #endif /* not HAVE_X11R6_XIM */
8861 }
8862 #endif /* HAVE_XIM */
8863 }
8864
8865
8866 /* Close the connection to the XIM server on display DPYINFO. */
8867
8868 static void
8869 xim_close_dpy (struct x_display_info *dpyinfo)
8870 {
8871 #ifdef HAVE_XIM
8872 if (use_xim)
8873 {
8874 #ifdef HAVE_X11R6_XIM
8875 struct xim_inst_t *xim_inst = dpyinfo->xim_callback_data;
8876
8877 if (dpyinfo->display)
8878 {
8879 Bool ret = XUnregisterIMInstantiateCallback
8880 (dpyinfo->display, dpyinfo->xrdb, xim_inst->resource_name,
8881 emacs_class, xim_instantiate_callback,
8882 (XRegisterIMInstantiateCallback_arg6) xim_inst);
8883 eassert (ret == True);
8884 }
8885 xfree (xim_inst->resource_name);
8886 xfree (xim_inst);
8887 #endif /* HAVE_X11R6_XIM */
8888 if (dpyinfo->display)
8889 XCloseIM (dpyinfo->xim);
8890 dpyinfo->xim = NULL;
8891 XFree (dpyinfo->xim_styles);
8892 }
8893 #endif /* HAVE_XIM */
8894 }
8895
8896 #endif /* not HAVE_X11R6_XIM */
8897
8898
8899 \f
8900 /* Calculate the absolute position in frame F
8901 from its current recorded position values and gravity. */
8902
8903 static void
8904 x_calc_absolute_position (struct frame *f)
8905 {
8906 int flags = f->size_hint_flags;
8907
8908 /* We have nothing to do if the current position
8909 is already for the top-left corner. */
8910 if (! ((flags & XNegative) || (flags & YNegative)))
8911 return;
8912
8913 /* Treat negative positions as relative to the leftmost bottommost
8914 position that fits on the screen. */
8915 if (flags & XNegative)
8916 f->left_pos = x_display_pixel_width (FRAME_DISPLAY_INFO (f))
8917 - FRAME_PIXEL_WIDTH (f) + f->left_pos;
8918
8919 {
8920 int height = FRAME_PIXEL_HEIGHT (f);
8921
8922 #if defined USE_X_TOOLKIT && defined USE_MOTIF
8923 /* Something is fishy here. When using Motif, starting Emacs with
8924 `-g -0-0', the frame appears too low by a few pixels.
8925
8926 This seems to be so because initially, while Emacs is starting,
8927 the column widget's height and the frame's pixel height are
8928 different. The column widget's height is the right one. In
8929 later invocations, when Emacs is up, the frame's pixel height
8930 is right, though.
8931
8932 It's not obvious where the initial small difference comes from.
8933 2000-12-01, gerd. */
8934
8935 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
8936 #endif
8937
8938 if (flags & YNegative)
8939 f->top_pos = x_display_pixel_height (FRAME_DISPLAY_INFO (f))
8940 - height + f->top_pos;
8941 }
8942
8943 /* The left_pos and top_pos
8944 are now relative to the top and left screen edges,
8945 so the flags should correspond. */
8946 f->size_hint_flags &= ~ (XNegative | YNegative);
8947 }
8948
8949 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
8950 to really change the position, and 0 when calling from
8951 x_make_frame_visible (in that case, XOFF and YOFF are the current
8952 position values). It is -1 when calling from x_set_frame_parameters,
8953 which means, do adjust for borders but don't change the gravity. */
8954
8955 void
8956 x_set_offset (struct frame *f, register int xoff, register int yoff, int change_gravity)
8957 {
8958 int modified_top, modified_left;
8959
8960 if (change_gravity > 0)
8961 {
8962 f->top_pos = yoff;
8963 f->left_pos = xoff;
8964 f->size_hint_flags &= ~ (XNegative | YNegative);
8965 if (xoff < 0)
8966 f->size_hint_flags |= XNegative;
8967 if (yoff < 0)
8968 f->size_hint_flags |= YNegative;
8969 f->win_gravity = NorthWestGravity;
8970 }
8971 x_calc_absolute_position (f);
8972
8973 block_input ();
8974 x_wm_set_size_hint (f, 0, false);
8975
8976 modified_left = f->left_pos;
8977 modified_top = f->top_pos;
8978
8979 if (change_gravity != 0 && FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A)
8980 {
8981 /* Some WMs (twm, wmaker at least) has an offset that is smaller
8982 than the WM decorations. So we use the calculated offset instead
8983 of the WM decoration sizes here (x/y_pixels_outer_diff). */
8984 modified_left += FRAME_X_OUTPUT (f)->move_offset_left;
8985 modified_top += FRAME_X_OUTPUT (f)->move_offset_top;
8986 }
8987
8988 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8989 modified_left, modified_top);
8990
8991 x_sync_with_move (f, f->left_pos, f->top_pos,
8992 FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN);
8993
8994 /* change_gravity is non-zero when this function is called from Lisp to
8995 programmatically move a frame. In that case, we call
8996 x_check_expected_move to discover if we have a "Type A" or "Type B"
8997 window manager, and, for a "Type A" window manager, adjust the position
8998 of the frame.
8999
9000 We call x_check_expected_move if a programmatic move occurred, and
9001 either the window manager type (A/B) is unknown or it is Type A but we
9002 need to compute the top/left offset adjustment for this frame. */
9003
9004 if (change_gravity != 0
9005 && (FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
9006 || (FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A
9007 && (FRAME_X_OUTPUT (f)->move_offset_left == 0
9008 && FRAME_X_OUTPUT (f)->move_offset_top == 0))))
9009 x_check_expected_move (f, modified_left, modified_top);
9010
9011 unblock_input ();
9012 }
9013
9014 /* Return true if _NET_SUPPORTING_WM_CHECK window exists and _NET_SUPPORTED
9015 on the root window for frame F contains ATOMNAME.
9016 This is how a WM check shall be done according to the Window Manager
9017 Specification/Extended Window Manager Hints at
9018 http://freedesktop.org/wiki/Specifications/wm-spec. */
9019
9020 static bool
9021 wm_supports (struct frame *f, Atom want_atom)
9022 {
9023 Atom actual_type;
9024 unsigned long actual_size, bytes_remaining;
9025 int i, rc, actual_format;
9026 bool ret;
9027 Window wmcheck_window;
9028 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
9029 Window target_window = dpyinfo->root_window;
9030 int max_len = 65536;
9031 Display *dpy = FRAME_X_DISPLAY (f);
9032 unsigned char *tmp_data = NULL;
9033 Atom target_type = XA_WINDOW;
9034
9035 block_input ();
9036
9037 x_catch_errors (dpy);
9038 rc = XGetWindowProperty (dpy, target_window,
9039 dpyinfo->Xatom_net_supporting_wm_check,
9040 0, max_len, False, target_type,
9041 &actual_type, &actual_format, &actual_size,
9042 &bytes_remaining, &tmp_data);
9043
9044 if (rc != Success || actual_type != XA_WINDOW || x_had_errors_p (dpy))
9045 {
9046 if (tmp_data) XFree (tmp_data);
9047 x_uncatch_errors ();
9048 unblock_input ();
9049 return false;
9050 }
9051
9052 wmcheck_window = *(Window *) tmp_data;
9053 XFree (tmp_data);
9054
9055 /* Check if window exists. */
9056 XSelectInput (dpy, wmcheck_window, StructureNotifyMask);
9057 x_sync (f);
9058 if (x_had_errors_p (dpy))
9059 {
9060 x_uncatch_errors ();
9061 unblock_input ();
9062 return false;
9063 }
9064
9065 if (dpyinfo->net_supported_window != wmcheck_window)
9066 {
9067 /* Window changed, reload atoms */
9068 if (dpyinfo->net_supported_atoms != NULL)
9069 XFree (dpyinfo->net_supported_atoms);
9070 dpyinfo->net_supported_atoms = NULL;
9071 dpyinfo->nr_net_supported_atoms = 0;
9072 dpyinfo->net_supported_window = 0;
9073
9074 target_type = XA_ATOM;
9075 tmp_data = NULL;
9076 rc = XGetWindowProperty (dpy, target_window,
9077 dpyinfo->Xatom_net_supported,
9078 0, max_len, False, target_type,
9079 &actual_type, &actual_format, &actual_size,
9080 &bytes_remaining, &tmp_data);
9081
9082 if (rc != Success || actual_type != XA_ATOM || x_had_errors_p (dpy))
9083 {
9084 if (tmp_data) XFree (tmp_data);
9085 x_uncatch_errors ();
9086 unblock_input ();
9087 return false;
9088 }
9089
9090 dpyinfo->net_supported_atoms = (Atom *)tmp_data;
9091 dpyinfo->nr_net_supported_atoms = actual_size;
9092 dpyinfo->net_supported_window = wmcheck_window;
9093 }
9094
9095 ret = false;
9096
9097 for (i = 0; !ret && i < dpyinfo->nr_net_supported_atoms; ++i)
9098 ret = dpyinfo->net_supported_atoms[i] == want_atom;
9099
9100 x_uncatch_errors ();
9101 unblock_input ();
9102
9103 return ret;
9104 }
9105
9106 static void
9107 set_wm_state (Lisp_Object frame, bool add, Atom atom, Atom value)
9108 {
9109 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (XFRAME (frame));
9110
9111 x_send_client_event (frame, make_number (0), frame,
9112 dpyinfo->Xatom_net_wm_state,
9113 make_number (32),
9114 /* 1 = add, 0 = remove */
9115 Fcons
9116 (make_number (add),
9117 Fcons
9118 (make_fixnum_or_float (atom),
9119 (value != 0
9120 ? list1 (make_fixnum_or_float (value))
9121 : Qnil))));
9122 }
9123
9124 void
9125 x_set_sticky (struct frame *f, Lisp_Object new_value, Lisp_Object old_value)
9126 {
9127 Lisp_Object frame;
9128 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
9129
9130 XSETFRAME (frame, f);
9131
9132 set_wm_state (frame, !NILP (new_value),
9133 dpyinfo->Xatom_net_wm_state_sticky, None);
9134 }
9135
9136 /* Return the current _NET_WM_STATE.
9137 SIZE_STATE is set to one of the FULLSCREEN_* values.
9138 Set *STICKY to the sticky state.
9139
9140 Return true iff we are not hidden. */
9141
9142 static bool
9143 get_current_wm_state (struct frame *f,
9144 Window window,
9145 int *size_state,
9146 bool *sticky)
9147 {
9148 Atom actual_type;
9149 unsigned long actual_size, bytes_remaining;
9150 int i, rc, actual_format;
9151 bool is_hidden = false;
9152 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
9153 long max_len = 65536;
9154 Display *dpy = FRAME_X_DISPLAY (f);
9155 unsigned char *tmp_data = NULL;
9156 Atom target_type = XA_ATOM;
9157
9158 *sticky = false;
9159 *size_state = FULLSCREEN_NONE;
9160
9161 block_input ();
9162 x_catch_errors (dpy);
9163 rc = XGetWindowProperty (dpy, window, dpyinfo->Xatom_net_wm_state,
9164 0, max_len, False, target_type,
9165 &actual_type, &actual_format, &actual_size,
9166 &bytes_remaining, &tmp_data);
9167
9168 if (rc != Success || actual_type != target_type || x_had_errors_p (dpy))
9169 {
9170 if (tmp_data) XFree (tmp_data);
9171 x_uncatch_errors ();
9172 unblock_input ();
9173 return !FRAME_ICONIFIED_P (f);
9174 }
9175
9176 x_uncatch_errors ();
9177
9178 for (i = 0; i < actual_size; ++i)
9179 {
9180 Atom a = ((Atom*)tmp_data)[i];
9181 if (a == dpyinfo->Xatom_net_wm_state_hidden)
9182 is_hidden = true;
9183 else if (a == dpyinfo->Xatom_net_wm_state_maximized_horz)
9184 {
9185 if (*size_state == FULLSCREEN_HEIGHT)
9186 *size_state = FULLSCREEN_MAXIMIZED;
9187 else
9188 *size_state = FULLSCREEN_WIDTH;
9189 }
9190 else if (a == dpyinfo->Xatom_net_wm_state_maximized_vert)
9191 {
9192 if (*size_state == FULLSCREEN_WIDTH)
9193 *size_state = FULLSCREEN_MAXIMIZED;
9194 else
9195 *size_state = FULLSCREEN_HEIGHT;
9196 }
9197 else if (a == dpyinfo->Xatom_net_wm_state_fullscreen)
9198 *size_state = FULLSCREEN_BOTH;
9199 else if (a == dpyinfo->Xatom_net_wm_state_sticky)
9200 *sticky = true;
9201 }
9202
9203 if (tmp_data) XFree (tmp_data);
9204 unblock_input ();
9205 return ! is_hidden;
9206 }
9207
9208 /* Do fullscreen as specified in extended window manager hints */
9209
9210 static bool
9211 do_ewmh_fullscreen (struct frame *f)
9212 {
9213 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
9214 bool have_net_atom = wm_supports (f, dpyinfo->Xatom_net_wm_state);
9215 int cur;
9216 bool dummy;
9217
9218 get_current_wm_state (f, FRAME_OUTER_WINDOW (f), &cur, &dummy);
9219
9220 /* Some window managers don't say they support _NET_WM_STATE, but they do say
9221 they support _NET_WM_STATE_FULLSCREEN. Try that also. */
9222 if (!have_net_atom)
9223 have_net_atom = wm_supports (f, dpyinfo->Xatom_net_wm_state_fullscreen);
9224
9225 if (have_net_atom && cur != f->want_fullscreen)
9226 {
9227 Lisp_Object frame;
9228
9229 XSETFRAME (frame, f);
9230
9231 /* Keep number of calls to set_wm_state as low as possible.
9232 Some window managers, or possible Gtk+, hangs when too many
9233 are sent at once. */
9234 switch (f->want_fullscreen)
9235 {
9236 case FULLSCREEN_BOTH:
9237 if (cur != FULLSCREEN_BOTH)
9238 set_wm_state (frame, true, dpyinfo->Xatom_net_wm_state_fullscreen,
9239 None);
9240 break;
9241 case FULLSCREEN_WIDTH:
9242 if (x_frame_normalize_before_maximize && cur == FULLSCREEN_MAXIMIZED)
9243 {
9244 set_wm_state (frame, false,
9245 dpyinfo->Xatom_net_wm_state_maximized_horz,
9246 dpyinfo->Xatom_net_wm_state_maximized_vert);
9247 set_wm_state (frame, true,
9248 dpyinfo->Xatom_net_wm_state_maximized_horz, None);
9249 }
9250 else
9251 {
9252 if (cur == FULLSCREEN_BOTH || cur == FULLSCREEN_HEIGHT
9253 || cur == FULLSCREEN_MAXIMIZED)
9254 set_wm_state (frame, false, dpyinfo->Xatom_net_wm_state_fullscreen,
9255 dpyinfo->Xatom_net_wm_state_maximized_vert);
9256 if (cur != FULLSCREEN_MAXIMIZED || x_frame_normalize_before_maximize)
9257 set_wm_state (frame, true,
9258 dpyinfo->Xatom_net_wm_state_maximized_horz, None);
9259 }
9260 break;
9261 case FULLSCREEN_HEIGHT:
9262 if (x_frame_normalize_before_maximize && cur == FULLSCREEN_MAXIMIZED)
9263 {
9264 set_wm_state (frame, false,
9265 dpyinfo->Xatom_net_wm_state_maximized_horz,
9266 dpyinfo->Xatom_net_wm_state_maximized_vert);
9267 set_wm_state (frame, true,
9268 dpyinfo->Xatom_net_wm_state_maximized_vert, None);
9269 }
9270 else
9271 {
9272 if (cur == FULLSCREEN_BOTH || cur == FULLSCREEN_WIDTH
9273 || cur == FULLSCREEN_MAXIMIZED)
9274 set_wm_state (frame, false, dpyinfo->Xatom_net_wm_state_fullscreen,
9275 dpyinfo->Xatom_net_wm_state_maximized_horz);
9276 if (cur != FULLSCREEN_MAXIMIZED || x_frame_normalize_before_maximize)
9277 set_wm_state (frame, true,
9278 dpyinfo->Xatom_net_wm_state_maximized_vert, None);
9279 }
9280 break;
9281 case FULLSCREEN_MAXIMIZED:
9282 if (x_frame_normalize_before_maximize && cur == FULLSCREEN_BOTH)
9283 {
9284 set_wm_state (frame, false,
9285 dpyinfo->Xatom_net_wm_state_fullscreen, None);
9286 set_wm_state (frame, true,
9287 dpyinfo->Xatom_net_wm_state_maximized_horz,
9288 dpyinfo->Xatom_net_wm_state_maximized_vert);
9289 }
9290 else if (x_frame_normalize_before_maximize && cur == FULLSCREEN_WIDTH)
9291 {
9292 set_wm_state (frame, false,
9293 dpyinfo->Xatom_net_wm_state_maximized_horz, None);
9294 set_wm_state (frame, true,
9295 dpyinfo->Xatom_net_wm_state_maximized_horz,
9296 dpyinfo->Xatom_net_wm_state_maximized_vert);
9297 }
9298 else if (x_frame_normalize_before_maximize && cur == FULLSCREEN_HEIGHT)
9299 {
9300 set_wm_state (frame, false,
9301 dpyinfo->Xatom_net_wm_state_maximized_vert, None);
9302 set_wm_state (frame, true,
9303 dpyinfo->Xatom_net_wm_state_maximized_horz,
9304 dpyinfo->Xatom_net_wm_state_maximized_vert);
9305 }
9306 else
9307 {
9308 if (cur == FULLSCREEN_BOTH)
9309 set_wm_state (frame, false, dpyinfo->Xatom_net_wm_state_fullscreen,
9310 None);
9311 else if (cur == FULLSCREEN_HEIGHT)
9312 set_wm_state (frame, true,
9313 dpyinfo->Xatom_net_wm_state_maximized_horz, None);
9314 else if (cur == FULLSCREEN_WIDTH)
9315 set_wm_state (frame, true, None,
9316 dpyinfo->Xatom_net_wm_state_maximized_vert);
9317 else
9318 set_wm_state (frame, true,
9319 dpyinfo->Xatom_net_wm_state_maximized_horz,
9320 dpyinfo->Xatom_net_wm_state_maximized_vert);
9321 }
9322 break;
9323 case FULLSCREEN_NONE:
9324 if (cur == FULLSCREEN_BOTH)
9325 set_wm_state (frame, false, dpyinfo->Xatom_net_wm_state_fullscreen,
9326 None);
9327 else
9328 set_wm_state (frame, false,
9329 dpyinfo->Xatom_net_wm_state_maximized_horz,
9330 dpyinfo->Xatom_net_wm_state_maximized_vert);
9331 }
9332
9333 f->want_fullscreen = FULLSCREEN_NONE;
9334
9335 }
9336
9337 return have_net_atom;
9338 }
9339
9340 static void
9341 XTfullscreen_hook (struct frame *f)
9342 {
9343 if (FRAME_VISIBLE_P (f))
9344 {
9345 block_input ();
9346 x_check_fullscreen (f);
9347 x_sync (f);
9348 unblock_input ();
9349 }
9350 }
9351
9352
9353 static bool
9354 x_handle_net_wm_state (struct frame *f, const XPropertyEvent *event)
9355 {
9356 int value = FULLSCREEN_NONE;
9357 Lisp_Object lval;
9358 bool sticky = false;
9359 bool not_hidden = get_current_wm_state (f, event->window, &value, &sticky);
9360
9361 lval = Qnil;
9362 switch (value)
9363 {
9364 case FULLSCREEN_WIDTH:
9365 lval = Qfullwidth;
9366 break;
9367 case FULLSCREEN_HEIGHT:
9368 lval = Qfullheight;
9369 break;
9370 case FULLSCREEN_BOTH:
9371 lval = Qfullboth;
9372 break;
9373 case FULLSCREEN_MAXIMIZED:
9374 lval = Qmaximized;
9375 break;
9376 }
9377
9378 frame_size_history_add
9379 (f, Qx_handle_net_wm_state, 0, 0,
9380 list2 (get_frame_param (f, Qfullscreen), lval));
9381
9382 store_frame_param (f, Qfullscreen, lval);
9383 store_frame_param (f, Qsticky, sticky ? Qt : Qnil);
9384
9385 return not_hidden;
9386 }
9387
9388 /* Check if we need to resize the frame due to a fullscreen request.
9389 If so needed, resize the frame. */
9390 static void
9391 x_check_fullscreen (struct frame *f)
9392 {
9393 if (do_ewmh_fullscreen (f))
9394 return;
9395
9396 if (f->output_data.x->parent_desc != FRAME_DISPLAY_INFO (f)->root_window)
9397 return; /* Only fullscreen without WM or with EWM hints (above). */
9398
9399 /* Setting fullscreen to nil doesn't do anything. We could save the
9400 last non-fullscreen size and restore it, but it seems like a
9401 lot of work for this unusual case (no window manager running). */
9402
9403 if (f->want_fullscreen != FULLSCREEN_NONE)
9404 {
9405 int width = FRAME_PIXEL_WIDTH (f), height = FRAME_PIXEL_HEIGHT (f);
9406 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
9407
9408 switch (f->want_fullscreen)
9409 {
9410 /* No difference between these two when there is no WM */
9411 case FULLSCREEN_BOTH:
9412 case FULLSCREEN_MAXIMIZED:
9413 width = x_display_pixel_width (dpyinfo);
9414 height = x_display_pixel_height (dpyinfo);
9415 break;
9416 case FULLSCREEN_WIDTH:
9417 width = x_display_pixel_width (dpyinfo);
9418 height = height + FRAME_MENUBAR_HEIGHT (f);
9419 break;
9420 case FULLSCREEN_HEIGHT:
9421 height = x_display_pixel_height (dpyinfo);
9422 }
9423
9424 frame_size_history_add
9425 (f, Qx_check_fullscreen, width, height, Qnil);
9426
9427 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9428 width, height);
9429
9430 if (FRAME_VISIBLE_P (f))
9431 x_wait_for_event (f, ConfigureNotify);
9432 else
9433 {
9434 change_frame_size (f, width, height - FRAME_MENUBAR_HEIGHT (f),
9435 false, true, false, true);
9436 x_sync (f);
9437 }
9438 }
9439 }
9440
9441 /* This function is called by x_set_offset to determine whether the window
9442 manager interfered with the positioning of the frame. Type A window
9443 managers position the surrounding window manager decorations a small
9444 amount above and left of the user-supplied position. Type B window
9445 managers position the surrounding window manager decorations at the
9446 user-specified position. If we detect a Type A window manager, we
9447 compensate by moving the window right and down by the proper amount. */
9448
9449 static void
9450 x_check_expected_move (struct frame *f, int expected_left, int expected_top)
9451 {
9452 int current_left = 0, current_top = 0;
9453
9454 /* x_real_positions returns the left and top offsets of the outermost
9455 window manager window around the frame. */
9456
9457 x_real_positions (f, &current_left, &current_top);
9458
9459 if (current_left != expected_left || current_top != expected_top)
9460 {
9461 /* It's a "Type A" window manager. */
9462
9463 int adjusted_left;
9464 int adjusted_top;
9465
9466 FRAME_DISPLAY_INFO (f)->wm_type = X_WMTYPE_A;
9467 FRAME_X_OUTPUT (f)->move_offset_left = expected_left - current_left;
9468 FRAME_X_OUTPUT (f)->move_offset_top = expected_top - current_top;
9469
9470 /* Now fix the mispositioned frame's location. */
9471
9472 adjusted_left = expected_left + FRAME_X_OUTPUT (f)->move_offset_left;
9473 adjusted_top = expected_top + FRAME_X_OUTPUT (f)->move_offset_top;
9474
9475 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9476 adjusted_left, adjusted_top);
9477
9478 x_sync_with_move (f, expected_left, expected_top, false);
9479 }
9480 else
9481 /* It's a "Type B" window manager. We don't have to adjust the
9482 frame's position. */
9483
9484 FRAME_DISPLAY_INFO (f)->wm_type = X_WMTYPE_B;
9485 }
9486
9487
9488 /* Wait for XGetGeometry to return up-to-date position information for a
9489 recently-moved frame. Call this immediately after calling XMoveWindow.
9490 If FUZZY is non-zero, then LEFT and TOP are just estimates of where the
9491 frame has been moved to, so we use a fuzzy position comparison instead
9492 of an exact comparison. */
9493
9494 static void
9495 x_sync_with_move (struct frame *f, int left, int top, bool fuzzy)
9496 {
9497 int count = 0;
9498
9499 while (count++ < 50)
9500 {
9501 int current_left = 0, current_top = 0;
9502
9503 /* In theory, this call to XSync only needs to happen once, but in
9504 practice, it doesn't seem to work, hence the need for the surrounding
9505 loop. */
9506
9507 XSync (FRAME_X_DISPLAY (f), False);
9508 x_real_positions (f, &current_left, &current_top);
9509
9510 if (fuzzy)
9511 {
9512 /* The left fuzz-factor is 10 pixels. The top fuzz-factor is 40
9513 pixels. */
9514
9515 if (eabs (current_left - left) <= 10
9516 && eabs (current_top - top) <= 40)
9517 return;
9518 }
9519 else if (current_left == left && current_top == top)
9520 return;
9521 }
9522
9523 /* As a last resort, just wait 0.5 seconds and hope that XGetGeometry
9524 will then return up-to-date position info. */
9525
9526 wait_reading_process_output (0, 500000000, 0, false, Qnil, NULL, 0);
9527 }
9528
9529
9530 /* Wait for an event on frame F matching EVENTTYPE. */
9531 void
9532 x_wait_for_event (struct frame *f, int eventtype)
9533 {
9534 int level = interrupt_input_blocked;
9535
9536 fd_set fds;
9537 struct timespec tmo, tmo_at, time_now;
9538 int fd = ConnectionNumber (FRAME_X_DISPLAY (f));
9539
9540 f->wait_event_type = eventtype;
9541
9542 /* Set timeout to 0.1 second. Hopefully not noticeable.
9543 Maybe it should be configurable. */
9544 tmo = make_timespec (0, 100 * 1000 * 1000);
9545 tmo_at = timespec_add (current_timespec (), tmo);
9546
9547 while (f->wait_event_type)
9548 {
9549 pending_signals = true;
9550 totally_unblock_input ();
9551 /* XTread_socket is called after unblock. */
9552 block_input ();
9553 interrupt_input_blocked = level;
9554
9555 FD_ZERO (&fds);
9556 FD_SET (fd, &fds);
9557
9558 time_now = current_timespec ();
9559 if (timespec_cmp (tmo_at, time_now) < 0)
9560 break;
9561
9562 tmo = timespec_sub (tmo_at, time_now);
9563 if (pselect (fd + 1, &fds, NULL, NULL, &tmo, NULL) == 0)
9564 break; /* Timeout */
9565 }
9566
9567 f->wait_event_type = 0;
9568 }
9569
9570
9571 /* Change the size of frame F's X window to WIDTH/HEIGHT in the case F
9572 doesn't have a widget. If CHANGE_GRAVITY, change to
9573 top-left-corner window gravity for this size change and subsequent
9574 size changes. Otherwise leave the window gravity unchanged. */
9575
9576 static void
9577 x_set_window_size_1 (struct frame *f, bool change_gravity,
9578 int width, int height)
9579 {
9580 int pixelwidth = FRAME_TEXT_TO_PIXEL_WIDTH (f, width);
9581 int pixelheight = FRAME_TEXT_TO_PIXEL_HEIGHT (f, height);
9582 int old_width = FRAME_PIXEL_WIDTH (f);
9583 int old_height = FRAME_PIXEL_HEIGHT (f);
9584 Lisp_Object fullscreen = get_frame_param (f, Qfullscreen);
9585
9586 if (change_gravity) f->win_gravity = NorthWestGravity;
9587 x_wm_set_size_hint (f, 0, false);
9588
9589 /* When the frame is fullheight and we only want to change the width
9590 or it is fullwidth and we only want to change the height we should
9591 be able to preserve the fullscreen property. However, due to the
9592 fact that we have to send a resize request anyway, the window
9593 manager will abolish it. At least the respective size should
9594 remain unchanged but giving the frame back its normal size will
9595 be broken ... */
9596 if (EQ (fullscreen, Qfullwidth) && width == FRAME_TEXT_WIDTH (f))
9597 {
9598 frame_size_history_add
9599 (f, Qxg_frame_set_char_size_1, width, height,
9600 list2 (make_number (old_height),
9601 make_number (pixelheight + FRAME_MENUBAR_HEIGHT (f))));
9602
9603 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9604 old_width, pixelheight + FRAME_MENUBAR_HEIGHT (f));
9605 }
9606 else if (EQ (fullscreen, Qfullheight) && height == FRAME_TEXT_HEIGHT (f))
9607 {
9608 frame_size_history_add
9609 (f, Qxg_frame_set_char_size_2, width, height,
9610 list2 (make_number (old_width), make_number (pixelwidth)));
9611
9612 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9613 pixelwidth, old_height);
9614 }
9615
9616 else
9617 {
9618 frame_size_history_add
9619 (f, Qxg_frame_set_char_size_3, width, height,
9620 list2 (make_number (pixelwidth + FRAME_TOOLBAR_WIDTH (f)),
9621 make_number (pixelheight + FRAME_TOOLBAR_HEIGHT (f)
9622 + FRAME_MENUBAR_HEIGHT (f))));
9623
9624 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9625 pixelwidth, pixelheight + FRAME_MENUBAR_HEIGHT (f));
9626 fullscreen = Qnil;
9627 }
9628
9629
9630
9631 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
9632 receive in the ConfigureNotify event; if we get what we asked
9633 for, then the event won't cause the screen to become garbaged, so
9634 we have to make sure to do it here. */
9635 SET_FRAME_GARBAGED (f);
9636
9637 /* Now, strictly speaking, we can't be sure that this is accurate,
9638 but the window manager will get around to dealing with the size
9639 change request eventually, and we'll hear how it went when the
9640 ConfigureNotify event gets here.
9641
9642 We could just not bother storing any of this information here,
9643 and let the ConfigureNotify event set everything up, but that
9644 might be kind of confusing to the Lisp code, since size changes
9645 wouldn't be reported in the frame parameters until some random
9646 point in the future when the ConfigureNotify event arrives.
9647
9648 Pass true for DELAY since we can't run Lisp code inside of
9649 a BLOCK_INPUT. */
9650
9651 /* But the ConfigureNotify may in fact never arrive, and then this is
9652 not right if the frame is visible. Instead wait (with timeout)
9653 for the ConfigureNotify. */
9654 if (FRAME_VISIBLE_P (f))
9655 {
9656 x_wait_for_event (f, ConfigureNotify);
9657
9658 if (!NILP (fullscreen))
9659 /* Try to restore fullscreen state. */
9660 {
9661 store_frame_param (f, Qfullscreen, fullscreen);
9662 x_set_fullscreen (f, fullscreen, fullscreen);
9663 }
9664 }
9665 else
9666 {
9667 change_frame_size (f, width, height, false, true, false, true);
9668 x_sync (f);
9669 }
9670 }
9671
9672
9673 /* Call this to change the size of frame F's x-window.
9674 If CHANGE_GRAVITY, change to top-left-corner window gravity
9675 for this size change and subsequent size changes.
9676 Otherwise we leave the window gravity unchanged. */
9677
9678 void
9679 x_set_window_size (struct frame *f, bool change_gravity,
9680 int width, int height, bool pixelwise)
9681 {
9682 block_input ();
9683
9684 /* The following breaks our calculations. If it's really needed,
9685 think of something else. */
9686 #if false
9687 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
9688 {
9689 int text_width, text_height;
9690
9691 /* When the frame is maximized/fullscreen or running under for
9692 example Xmonad, x_set_window_size_1 will be a no-op.
9693 In that case, the right thing to do is extend rows/width to
9694 the current frame size. We do that first if x_set_window_size_1
9695 turns out to not be a no-op (there is no way to know).
9696 The size will be adjusted again if the frame gets a
9697 ConfigureNotify event as a result of x_set_window_size. */
9698 int pixelh = FRAME_PIXEL_HEIGHT (f);
9699 #ifdef USE_X_TOOLKIT
9700 /* The menu bar is not part of text lines. The tool bar
9701 is however. */
9702 pixelh -= FRAME_MENUBAR_HEIGHT (f);
9703 #endif
9704 text_width = FRAME_PIXEL_TO_TEXT_WIDTH (f, FRAME_PIXEL_WIDTH (f));
9705 text_height = FRAME_PIXEL_TO_TEXT_HEIGHT (f, pixelh);
9706
9707 change_frame_size (f, text_width, text_height, false, true, false, true);
9708 }
9709 #endif
9710
9711 /* Pixelize width and height, if necessary. */
9712 if (! pixelwise)
9713 {
9714 width = width * FRAME_COLUMN_WIDTH (f);
9715 height = height * FRAME_LINE_HEIGHT (f);
9716 }
9717
9718 #ifdef USE_GTK
9719 if (FRAME_GTK_WIDGET (f))
9720 xg_frame_set_char_size (f, width, height);
9721 else
9722 x_set_window_size_1 (f, change_gravity, width, height);
9723 #else /* not USE_GTK */
9724 x_set_window_size_1 (f, change_gravity, width, height);
9725 x_clear_under_internal_border (f);
9726 #endif /* not USE_GTK */
9727
9728 /* If cursor was outside the new size, mark it as off. */
9729 mark_window_cursors_off (XWINDOW (f->root_window));
9730
9731 /* Clear out any recollection of where the mouse highlighting was,
9732 since it might be in a place that's outside the new frame size.
9733 Actually checking whether it is outside is a pain in the neck,
9734 so don't try--just let the highlighting be done afresh with new size. */
9735 cancel_mouse_face (f);
9736
9737 unblock_input ();
9738
9739 do_pending_window_change (false);
9740 }
9741
9742 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
9743
9744 void
9745 frame_set_mouse_pixel_position (struct frame *f, int pix_x, int pix_y)
9746 {
9747 block_input ();
9748
9749 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
9750 0, 0, 0, 0, pix_x, pix_y);
9751 unblock_input ();
9752 }
9753 \f
9754 /* Raise frame F. */
9755
9756 void
9757 x_raise_frame (struct frame *f)
9758 {
9759 block_input ();
9760 if (FRAME_VISIBLE_P (f))
9761 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
9762 XFlush (FRAME_X_DISPLAY (f));
9763 unblock_input ();
9764 }
9765
9766 /* Lower frame F. */
9767
9768 static void
9769 x_lower_frame (struct frame *f)
9770 {
9771 if (FRAME_VISIBLE_P (f))
9772 {
9773 block_input ();
9774 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
9775 XFlush (FRAME_X_DISPLAY (f));
9776 unblock_input ();
9777 }
9778 }
9779
9780 /* Request focus with XEmbed */
9781
9782 void
9783 xembed_request_focus (struct frame *f)
9784 {
9785 /* See XEmbed Protocol Specification at
9786 http://freedesktop.org/wiki/Specifications/xembed-spec */
9787 if (FRAME_VISIBLE_P (f))
9788 xembed_send_message (f, CurrentTime,
9789 XEMBED_REQUEST_FOCUS, 0, 0, 0);
9790 }
9791
9792 /* Activate frame with Extended Window Manager Hints */
9793
9794 void
9795 x_ewmh_activate_frame (struct frame *f)
9796 {
9797 /* See Window Manager Specification/Extended Window Manager Hints at
9798 http://freedesktop.org/wiki/Specifications/wm-spec */
9799
9800 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
9801
9802 if (FRAME_VISIBLE_P (f) && wm_supports (f, dpyinfo->Xatom_net_active_window))
9803 {
9804 Lisp_Object frame;
9805 XSETFRAME (frame, f);
9806 x_send_client_event (frame, make_number (0), frame,
9807 dpyinfo->Xatom_net_active_window,
9808 make_number (32),
9809 list2i (1, dpyinfo->last_user_time));
9810 }
9811 }
9812
9813 static void
9814 XTframe_raise_lower (struct frame *f, bool raise_flag)
9815 {
9816 if (raise_flag)
9817 x_raise_frame (f);
9818 else
9819 x_lower_frame (f);
9820 }
9821 \f
9822 /* XEmbed implementation. */
9823
9824 #if defined USE_X_TOOLKIT || ! defined USE_GTK
9825
9826 /* XEmbed implementation. */
9827
9828 #define XEMBED_VERSION 0
9829
9830 static void
9831 xembed_set_info (struct frame *f, enum xembed_info flags)
9832 {
9833 unsigned long data[2];
9834 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
9835
9836 data[0] = XEMBED_VERSION;
9837 data[1] = flags;
9838
9839 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9840 dpyinfo->Xatom_XEMBED_INFO, dpyinfo->Xatom_XEMBED_INFO,
9841 32, PropModeReplace, (unsigned char *) data, 2);
9842 }
9843 #endif /* defined USE_X_TOOLKIT || ! defined USE_GTK */
9844
9845 static void
9846 xembed_send_message (struct frame *f, Time t, enum xembed_message msg,
9847 long int detail, long int data1, long int data2)
9848 {
9849 XEvent event;
9850
9851 event.xclient.type = ClientMessage;
9852 event.xclient.window = FRAME_X_OUTPUT (f)->parent_desc;
9853 event.xclient.message_type = FRAME_DISPLAY_INFO (f)->Xatom_XEMBED;
9854 event.xclient.format = 32;
9855 event.xclient.data.l[0] = t;
9856 event.xclient.data.l[1] = msg;
9857 event.xclient.data.l[2] = detail;
9858 event.xclient.data.l[3] = data1;
9859 event.xclient.data.l[4] = data2;
9860
9861 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_OUTPUT (f)->parent_desc,
9862 False, NoEventMask, &event);
9863 XSync (FRAME_X_DISPLAY (f), False);
9864 }
9865 \f
9866 /* Change of visibility. */
9867
9868 /* This tries to wait until the frame is really visible.
9869 However, if the window manager asks the user where to position
9870 the frame, this will return before the user finishes doing that.
9871 The frame will not actually be visible at that time,
9872 but it will become visible later when the window manager
9873 finishes with it. */
9874
9875 void
9876 x_make_frame_visible (struct frame *f)
9877 {
9878 int original_top, original_left;
9879 int tries = 0;
9880
9881 block_input ();
9882
9883 x_set_bitmap_icon (f);
9884
9885 if (! FRAME_VISIBLE_P (f))
9886 {
9887 /* We test FRAME_GARBAGED_P here to make sure we don't
9888 call x_set_offset a second time
9889 if we get to x_make_frame_visible a second time
9890 before the window gets really visible. */
9891 if (! FRAME_ICONIFIED_P (f)
9892 && ! FRAME_X_EMBEDDED_P (f)
9893 && ! f->output_data.x->asked_for_visible)
9894 x_set_offset (f, f->left_pos, f->top_pos, 0);
9895
9896 f->output_data.x->asked_for_visible = true;
9897
9898 if (! EQ (Vx_no_window_manager, Qt))
9899 x_wm_set_window_state (f, NormalState);
9900 #ifdef USE_X_TOOLKIT
9901 if (FRAME_X_EMBEDDED_P (f))
9902 xembed_set_info (f, XEMBED_MAPPED);
9903 else
9904 {
9905 /* This was XtPopup, but that did nothing for an iconified frame. */
9906 XtMapWidget (f->output_data.x->widget);
9907 }
9908 #else /* not USE_X_TOOLKIT */
9909 #ifdef USE_GTK
9910 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
9911 gtk_window_deiconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
9912 #else
9913 if (FRAME_X_EMBEDDED_P (f))
9914 xembed_set_info (f, XEMBED_MAPPED);
9915 else
9916 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9917 #endif /* not USE_GTK */
9918 #endif /* not USE_X_TOOLKIT */
9919 }
9920
9921 XFlush (FRAME_X_DISPLAY (f));
9922
9923 /* Synchronize to ensure Emacs knows the frame is visible
9924 before we do anything else. We do this loop with input not blocked
9925 so that incoming events are handled. */
9926 {
9927 Lisp_Object frame;
9928 /* This must be before UNBLOCK_INPUT
9929 since events that arrive in response to the actions above
9930 will set it when they are handled. */
9931 bool previously_visible = f->output_data.x->has_been_visible;
9932
9933 original_left = f->left_pos;
9934 original_top = f->top_pos;
9935
9936 /* This must come after we set COUNT. */
9937 unblock_input ();
9938
9939 /* We unblock here so that arriving X events are processed. */
9940
9941 /* Now move the window back to where it was "supposed to be".
9942 But don't do it if the gravity is negative.
9943 When the gravity is negative, this uses a position
9944 that is 3 pixels too low. Perhaps that's really the border width.
9945
9946 Don't do this if the window has never been visible before,
9947 because the window manager may choose the position
9948 and we don't want to override it. */
9949
9950 if (! FRAME_VISIBLE_P (f)
9951 && ! FRAME_ICONIFIED_P (f)
9952 && ! FRAME_X_EMBEDDED_P (f)
9953 && f->win_gravity == NorthWestGravity
9954 && previously_visible)
9955 {
9956 Drawable rootw;
9957 int x, y;
9958 unsigned int width, height, border, depth;
9959
9960 block_input ();
9961
9962 /* On some window managers (such as FVWM) moving an existing
9963 window, even to the same place, causes the window manager
9964 to introduce an offset. This can cause the window to move
9965 to an unexpected location. Check the geometry (a little
9966 slow here) and then verify that the window is in the right
9967 place. If the window is not in the right place, move it
9968 there, and take the potential window manager hit. */
9969 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9970 &rootw, &x, &y, &width, &height, &border, &depth);
9971
9972 if (original_left != x || original_top != y)
9973 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9974 original_left, original_top);
9975
9976 unblock_input ();
9977 }
9978
9979 XSETFRAME (frame, f);
9980
9981 /* Process X events until a MapNotify event has been seen. */
9982 while (!FRAME_VISIBLE_P (f))
9983 {
9984 /* Force processing of queued events. */
9985 x_sync (f);
9986
9987 /* If on another desktop, the deiconify/map may be ignored and the
9988 frame never becomes visible. XMonad does this.
9989 Prevent an endless loop. */
9990 if (FRAME_ICONIFIED_P (f) && ++tries > 100)
9991 break;
9992
9993 /* This hack is still in use at least for Cygwin. See
9994 http://lists.gnu.org/archive/html/emacs-devel/2013-12/msg00351.html.
9995
9996 Machines that do polling rather than SIGIO have been
9997 observed to go into a busy-wait here. So we'll fake an
9998 alarm signal to let the handler know that there's something
9999 to be read. We used to raise a real alarm, but it seems
10000 that the handler isn't always enabled here. This is
10001 probably a bug. */
10002 if (input_polling_used ())
10003 {
10004 /* It could be confusing if a real alarm arrives while
10005 processing the fake one. Turn it off and let the
10006 handler reset it. */
10007 int old_poll_suppress_count = poll_suppress_count;
10008 poll_suppress_count = 1;
10009 poll_for_input_1 ();
10010 poll_suppress_count = old_poll_suppress_count;
10011 }
10012
10013 if (XPending (FRAME_X_DISPLAY (f)))
10014 {
10015 XEvent xev;
10016 XNextEvent (FRAME_X_DISPLAY (f), &xev);
10017 x_dispatch_event (&xev, FRAME_X_DISPLAY (f));
10018 }
10019 }
10020 }
10021 }
10022
10023 /* Change from mapped state to withdrawn state. */
10024
10025 /* Make the frame visible (mapped and not iconified). */
10026
10027 void
10028 x_make_frame_invisible (struct frame *f)
10029 {
10030 Window window;
10031
10032 /* Use the frame's outermost window, not the one we normally draw on. */
10033 window = FRAME_OUTER_WINDOW (f);
10034
10035 /* Don't keep the highlight on an invisible frame. */
10036 if (FRAME_DISPLAY_INFO (f)->x_highlight_frame == f)
10037 FRAME_DISPLAY_INFO (f)->x_highlight_frame = 0;
10038
10039 block_input ();
10040
10041 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
10042 that the current position of the window is user-specified, rather than
10043 program-specified, so that when the window is mapped again, it will be
10044 placed at the same location, without forcing the user to position it
10045 by hand again (they have already done that once for this window.) */
10046 x_wm_set_size_hint (f, 0, true);
10047
10048 #ifdef USE_GTK
10049 if (FRAME_GTK_OUTER_WIDGET (f))
10050 gtk_widget_hide (FRAME_GTK_OUTER_WIDGET (f));
10051 else
10052 #else
10053 if (FRAME_X_EMBEDDED_P (f))
10054 xembed_set_info (f, 0);
10055 else
10056 #endif
10057 {
10058
10059 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
10060 DefaultScreen (FRAME_X_DISPLAY (f))))
10061 {
10062 unblock_input ();
10063 error ("Can't notify window manager of window withdrawal");
10064 }
10065 }
10066
10067 /* We can't distinguish this from iconification
10068 just by the event that we get from the server.
10069 So we can't win using the usual strategy of letting
10070 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
10071 and synchronize with the server to make sure we agree. */
10072 SET_FRAME_VISIBLE (f, 0);
10073 SET_FRAME_ICONIFIED (f, false);
10074
10075 x_sync (f);
10076
10077 unblock_input ();
10078 }
10079
10080 /* Change window state from mapped to iconified. */
10081
10082 void
10083 x_iconify_frame (struct frame *f)
10084 {
10085 #ifdef USE_X_TOOLKIT
10086 int result;
10087 #endif
10088
10089 /* Don't keep the highlight on an invisible frame. */
10090 if (FRAME_DISPLAY_INFO (f)->x_highlight_frame == f)
10091 FRAME_DISPLAY_INFO (f)->x_highlight_frame = 0;
10092
10093 if (FRAME_ICONIFIED_P (f))
10094 return;
10095
10096 block_input ();
10097
10098 x_set_bitmap_icon (f);
10099
10100 #if defined (USE_GTK)
10101 if (FRAME_GTK_OUTER_WIDGET (f))
10102 {
10103 if (! FRAME_VISIBLE_P (f))
10104 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
10105
10106 gtk_window_iconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
10107 SET_FRAME_VISIBLE (f, 0);
10108 SET_FRAME_ICONIFIED (f, true);
10109 unblock_input ();
10110 return;
10111 }
10112 #endif
10113
10114 #ifdef USE_X_TOOLKIT
10115
10116 if (! FRAME_VISIBLE_P (f))
10117 {
10118 if (! EQ (Vx_no_window_manager, Qt))
10119 x_wm_set_window_state (f, IconicState);
10120 /* This was XtPopup, but that did nothing for an iconified frame. */
10121 XtMapWidget (f->output_data.x->widget);
10122 /* The server won't give us any event to indicate
10123 that an invisible frame was changed to an icon,
10124 so we have to record it here. */
10125 SET_FRAME_VISIBLE (f, 0);
10126 SET_FRAME_ICONIFIED (f, true);
10127 unblock_input ();
10128 return;
10129 }
10130
10131 result = XIconifyWindow (FRAME_X_DISPLAY (f),
10132 XtWindow (f->output_data.x->widget),
10133 DefaultScreen (FRAME_X_DISPLAY (f)));
10134 unblock_input ();
10135
10136 if (!result)
10137 error ("Can't notify window manager of iconification");
10138
10139 SET_FRAME_ICONIFIED (f, true);
10140 SET_FRAME_VISIBLE (f, 0);
10141
10142 block_input ();
10143 XFlush (FRAME_X_DISPLAY (f));
10144 unblock_input ();
10145 #else /* not USE_X_TOOLKIT */
10146
10147 /* Make sure the X server knows where the window should be positioned,
10148 in case the user deiconifies with the window manager. */
10149 if (! FRAME_VISIBLE_P (f)
10150 && ! FRAME_ICONIFIED_P (f)
10151 && ! FRAME_X_EMBEDDED_P (f))
10152 x_set_offset (f, f->left_pos, f->top_pos, 0);
10153
10154 /* Since we don't know which revision of X we're running, we'll use both
10155 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
10156
10157 /* X11R4: send a ClientMessage to the window manager using the
10158 WM_CHANGE_STATE type. */
10159 {
10160 XEvent msg;
10161
10162 msg.xclient.window = FRAME_X_WINDOW (f);
10163 msg.xclient.type = ClientMessage;
10164 msg.xclient.message_type = FRAME_DISPLAY_INFO (f)->Xatom_wm_change_state;
10165 msg.xclient.format = 32;
10166 msg.xclient.data.l[0] = IconicState;
10167
10168 if (! XSendEvent (FRAME_X_DISPLAY (f),
10169 DefaultRootWindow (FRAME_X_DISPLAY (f)),
10170 False,
10171 SubstructureRedirectMask | SubstructureNotifyMask,
10172 &msg))
10173 {
10174 unblock_input ();
10175 error ("Can't notify window manager of iconification");
10176 }
10177 }
10178
10179 /* X11R3: set the initial_state field of the window manager hints to
10180 IconicState. */
10181 x_wm_set_window_state (f, IconicState);
10182
10183 if (!FRAME_VISIBLE_P (f))
10184 {
10185 /* If the frame was withdrawn, before, we must map it. */
10186 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
10187 }
10188
10189 SET_FRAME_ICONIFIED (f, true);
10190 SET_FRAME_VISIBLE (f, 0);
10191
10192 XFlush (FRAME_X_DISPLAY (f));
10193 unblock_input ();
10194 #endif /* not USE_X_TOOLKIT */
10195 }
10196
10197 \f
10198 /* Free X resources of frame F. */
10199
10200 void
10201 x_free_frame_resources (struct frame *f)
10202 {
10203 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10204 Mouse_HLInfo *hlinfo = &dpyinfo->mouse_highlight;
10205 #ifdef USE_X_TOOLKIT
10206 Lisp_Object bar;
10207 struct scroll_bar *b;
10208 #endif
10209
10210 block_input ();
10211
10212 /* If a display connection is dead, don't try sending more
10213 commands to the X server. */
10214 if (dpyinfo->display)
10215 {
10216 /* Always exit with visible pointer to avoid weird issue
10217 with Xfixes (Bug#17609). */
10218 if (f->pointer_invisible)
10219 FRAME_DISPLAY_INFO (f)->toggle_visible_pointer (f, 0);
10220
10221 /* We must free faces before destroying windows because some
10222 font-driver (e.g. xft) access a window while finishing a
10223 face. */
10224 free_frame_faces (f);
10225
10226 if (f->output_data.x->icon_desc)
10227 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
10228
10229 #ifdef USE_X_TOOLKIT
10230 /* Explicitly destroy the scroll bars of the frame. Without
10231 this, we get "BadDrawable" errors from the toolkit later on,
10232 presumably from expose events generated for the disappearing
10233 toolkit scroll bars. */
10234 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
10235 {
10236 b = XSCROLL_BAR (bar);
10237 x_scroll_bar_remove (b);
10238 }
10239 #endif
10240
10241 #ifdef HAVE_X_I18N
10242 if (FRAME_XIC (f))
10243 free_frame_xic (f);
10244 #endif
10245
10246 #ifdef USE_X_TOOLKIT
10247 if (f->output_data.x->widget)
10248 {
10249 XtDestroyWidget (f->output_data.x->widget);
10250 f->output_data.x->widget = NULL;
10251 }
10252 /* Tooltips don't have widgets, only a simple X window, even if
10253 we are using a toolkit. */
10254 else if (FRAME_X_WINDOW (f))
10255 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
10256
10257 free_frame_menubar (f);
10258
10259 if (f->shell_position)
10260 xfree (f->shell_position);
10261 #else /* !USE_X_TOOLKIT */
10262
10263 #ifdef USE_GTK
10264 xg_free_frame_widgets (f);
10265 #endif /* USE_GTK */
10266
10267 if (FRAME_X_WINDOW (f))
10268 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
10269 #endif /* !USE_X_TOOLKIT */
10270
10271 unload_color (f, FRAME_FOREGROUND_PIXEL (f));
10272 unload_color (f, FRAME_BACKGROUND_PIXEL (f));
10273 unload_color (f, f->output_data.x->cursor_pixel);
10274 unload_color (f, f->output_data.x->cursor_foreground_pixel);
10275 unload_color (f, f->output_data.x->border_pixel);
10276 unload_color (f, f->output_data.x->mouse_pixel);
10277
10278 if (f->output_data.x->scroll_bar_background_pixel != -1)
10279 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
10280 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
10281 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
10282 #if defined (USE_LUCID) && defined (USE_TOOLKIT_SCROLL_BARS)
10283 /* Scrollbar shadow colors. */
10284 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
10285 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
10286 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
10287 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
10288 #endif /* USE_LUCID && USE_TOOLKIT_SCROLL_BARS */
10289 if (f->output_data.x->white_relief.pixel != -1)
10290 unload_color (f, f->output_data.x->white_relief.pixel);
10291 if (f->output_data.x->black_relief.pixel != -1)
10292 unload_color (f, f->output_data.x->black_relief.pixel);
10293
10294 x_free_gcs (f);
10295
10296 /* Free extra GCs allocated by x_setup_relief_colors. */
10297 if (f->output_data.x->white_relief.gc)
10298 {
10299 XFreeGC (dpyinfo->display, f->output_data.x->white_relief.gc);
10300 f->output_data.x->white_relief.gc = 0;
10301 }
10302 if (f->output_data.x->black_relief.gc)
10303 {
10304 XFreeGC (dpyinfo->display, f->output_data.x->black_relief.gc);
10305 f->output_data.x->black_relief.gc = 0;
10306 }
10307
10308 /* Free cursors. */
10309 if (f->output_data.x->text_cursor != 0)
10310 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->text_cursor);
10311 if (f->output_data.x->nontext_cursor != 0)
10312 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->nontext_cursor);
10313 if (f->output_data.x->modeline_cursor != 0)
10314 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->modeline_cursor);
10315 if (f->output_data.x->hand_cursor != 0)
10316 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->hand_cursor);
10317 if (f->output_data.x->hourglass_cursor != 0)
10318 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->hourglass_cursor);
10319 if (f->output_data.x->horizontal_drag_cursor != 0)
10320 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->horizontal_drag_cursor);
10321 if (f->output_data.x->vertical_drag_cursor != 0)
10322 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->vertical_drag_cursor);
10323
10324 XFlush (FRAME_X_DISPLAY (f));
10325 }
10326
10327 xfree (f->output_data.x->saved_menu_event);
10328 xfree (f->output_data.x);
10329 f->output_data.x = NULL;
10330
10331 if (f == dpyinfo->x_focus_frame)
10332 dpyinfo->x_focus_frame = 0;
10333 if (f == dpyinfo->x_focus_event_frame)
10334 dpyinfo->x_focus_event_frame = 0;
10335 if (f == dpyinfo->x_highlight_frame)
10336 dpyinfo->x_highlight_frame = 0;
10337 if (f == hlinfo->mouse_face_mouse_frame)
10338 reset_mouse_highlight (hlinfo);
10339
10340 unblock_input ();
10341 }
10342
10343
10344 /* Destroy the X window of frame F. */
10345
10346 static void
10347 x_destroy_window (struct frame *f)
10348 {
10349 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10350
10351 /* If a display connection is dead, don't try sending more
10352 commands to the X server. */
10353 if (dpyinfo->display != 0)
10354 x_free_frame_resources (f);
10355
10356 dpyinfo->reference_count--;
10357 }
10358
10359 \f
10360 /* Setting window manager hints. */
10361
10362 /* Set the normal size hints for the window manager, for frame F.
10363 FLAGS is the flags word to use--or 0 meaning preserve the flags
10364 that the window now has.
10365 If USER_POSITION, set the USPosition
10366 flag (this is useful when FLAGS is 0).
10367 The GTK version is in gtkutils.c. */
10368
10369 #ifndef USE_GTK
10370 void
10371 x_wm_set_size_hint (struct frame *f, long flags, bool user_position)
10372 {
10373 XSizeHints size_hints;
10374 Window window = FRAME_OUTER_WINDOW (f);
10375
10376 if (!window)
10377 return;
10378
10379 #ifdef USE_X_TOOLKIT
10380 if (f->output_data.x->widget)
10381 {
10382 widget_update_wm_size_hints (f->output_data.x->widget);
10383 return;
10384 }
10385 #endif
10386
10387 /* Setting PMaxSize caused various problems. */
10388 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
10389
10390 size_hints.x = f->left_pos;
10391 size_hints.y = f->top_pos;
10392
10393 size_hints.height = FRAME_PIXEL_HEIGHT (f);
10394 size_hints.width = FRAME_PIXEL_WIDTH (f);
10395
10396 size_hints.width_inc = frame_resize_pixelwise ? 1 : FRAME_COLUMN_WIDTH (f);
10397 size_hints.height_inc = frame_resize_pixelwise ? 1 : FRAME_LINE_HEIGHT (f);
10398
10399 size_hints.max_width = x_display_pixel_width (FRAME_DISPLAY_INFO (f))
10400 - FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
10401 size_hints.max_height = x_display_pixel_height (FRAME_DISPLAY_INFO (f))
10402 - FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
10403
10404 /* Calculate the base and minimum sizes. */
10405 {
10406 int base_width, base_height;
10407 int min_rows = 0, min_cols = 0;
10408
10409 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
10410 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
10411
10412 if (frame_resize_pixelwise)
10413 /* Needed to prevent a bad protocol error crash when making the
10414 frame size very small. */
10415 {
10416 min_cols = 2 * min_cols;
10417 min_rows = 2 * min_rows;
10418 }
10419
10420 /* The window manager uses the base width hints to calculate the
10421 current number of rows and columns in the frame while
10422 resizing; min_width and min_height aren't useful for this
10423 purpose, since they might not give the dimensions for a
10424 zero-row, zero-column frame.
10425
10426 We use the base_width and base_height members if we have
10427 them; otherwise, we set the min_width and min_height members
10428 to the size for a zero x zero frame. */
10429
10430 size_hints.flags |= PBaseSize;
10431 size_hints.base_width = base_width;
10432 size_hints.base_height = base_height + FRAME_MENUBAR_HEIGHT (f);
10433 size_hints.min_width = base_width + min_cols * FRAME_COLUMN_WIDTH (f);
10434 size_hints.min_height = base_height + min_rows * FRAME_LINE_HEIGHT (f);
10435 }
10436
10437 /* If we don't need the old flags, we don't need the old hint at all. */
10438 if (flags)
10439 {
10440 size_hints.flags |= flags;
10441 goto no_read;
10442 }
10443
10444 {
10445 XSizeHints hints; /* Sometimes I hate X Windows... */
10446 long supplied_return;
10447 int value;
10448
10449 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
10450 &supplied_return);
10451
10452 if (flags)
10453 size_hints.flags |= flags;
10454 else
10455 {
10456 if (value == 0)
10457 hints.flags = 0;
10458 if (hints.flags & PSize)
10459 size_hints.flags |= PSize;
10460 if (hints.flags & PPosition)
10461 size_hints.flags |= PPosition;
10462 if (hints.flags & USPosition)
10463 size_hints.flags |= USPosition;
10464 if (hints.flags & USSize)
10465 size_hints.flags |= USSize;
10466 }
10467 }
10468
10469 no_read:
10470
10471 #ifdef PWinGravity
10472 size_hints.win_gravity = f->win_gravity;
10473 size_hints.flags |= PWinGravity;
10474
10475 if (user_position)
10476 {
10477 size_hints.flags &= ~ PPosition;
10478 size_hints.flags |= USPosition;
10479 }
10480 #endif /* PWinGravity */
10481
10482 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
10483 }
10484 #endif /* not USE_GTK */
10485
10486 /* Used for IconicState or NormalState */
10487
10488 static void
10489 x_wm_set_window_state (struct frame *f, int state)
10490 {
10491 #ifdef USE_X_TOOLKIT
10492 Arg al[1];
10493
10494 XtSetArg (al[0], XtNinitialState, state);
10495 XtSetValues (f->output_data.x->widget, al, 1);
10496 #else /* not USE_X_TOOLKIT */
10497 Window window = FRAME_X_WINDOW (f);
10498
10499 f->output_data.x->wm_hints.flags |= StateHint;
10500 f->output_data.x->wm_hints.initial_state = state;
10501
10502 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
10503 #endif /* not USE_X_TOOLKIT */
10504 }
10505
10506 static void
10507 x_wm_set_icon_pixmap (struct frame *f, ptrdiff_t pixmap_id)
10508 {
10509 Pixmap icon_pixmap, icon_mask;
10510
10511 #if !defined USE_X_TOOLKIT && !defined USE_GTK
10512 Window window = FRAME_OUTER_WINDOW (f);
10513 #endif
10514
10515 if (pixmap_id > 0)
10516 {
10517 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
10518 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
10519 icon_mask = x_bitmap_mask (f, pixmap_id);
10520 f->output_data.x->wm_hints.icon_mask = icon_mask;
10521 }
10522 else
10523 {
10524 /* It seems there is no way to turn off use of an icon
10525 pixmap. */
10526 return;
10527 }
10528
10529
10530 #ifdef USE_GTK
10531 {
10532 xg_set_frame_icon (f, icon_pixmap, icon_mask);
10533 return;
10534 }
10535
10536 #elif defined (USE_X_TOOLKIT) /* same as in x_wm_set_window_state. */
10537
10538 {
10539 Arg al[1];
10540 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
10541 XtSetValues (f->output_data.x->widget, al, 1);
10542 XtSetArg (al[0], XtNiconMask, icon_mask);
10543 XtSetValues (f->output_data.x->widget, al, 1);
10544 }
10545
10546 #else /* not USE_X_TOOLKIT && not USE_GTK */
10547
10548 f->output_data.x->wm_hints.flags |= (IconPixmapHint | IconMaskHint);
10549 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
10550
10551 #endif /* not USE_X_TOOLKIT && not USE_GTK */
10552 }
10553
10554 void
10555 x_wm_set_icon_position (struct frame *f, int icon_x, int icon_y)
10556 {
10557 Window window = FRAME_OUTER_WINDOW (f);
10558
10559 f->output_data.x->wm_hints.flags |= IconPositionHint;
10560 f->output_data.x->wm_hints.icon_x = icon_x;
10561 f->output_data.x->wm_hints.icon_y = icon_y;
10562
10563 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
10564 }
10565
10566 \f
10567 /***********************************************************************
10568 Fonts
10569 ***********************************************************************/
10570
10571 #ifdef GLYPH_DEBUG
10572
10573 /* Check that FONT is valid on frame F. It is if it can be found in F's
10574 font table. */
10575
10576 static void
10577 x_check_font (struct frame *f, struct font *font)
10578 {
10579 eassert (font != NULL && ! NILP (font->props[FONT_TYPE_INDEX]));
10580 if (font->driver->check)
10581 eassert (font->driver->check (f, font) == 0);
10582 }
10583
10584 #endif /* GLYPH_DEBUG */
10585
10586 \f
10587 /***********************************************************************
10588 Initialization
10589 ***********************************************************************/
10590
10591 #ifdef USE_X_TOOLKIT
10592 static XrmOptionDescRec emacs_options[] = {
10593 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
10594 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
10595
10596 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
10597 XrmoptionSepArg, NULL},
10598 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
10599
10600 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10601 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10602 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10603 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
10604 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
10605 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
10606 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
10607 };
10608
10609 /* Whether atimer for Xt timeouts is activated or not. */
10610
10611 static bool x_timeout_atimer_activated_flag;
10612
10613 #endif /* USE_X_TOOLKIT */
10614
10615 static int x_initialized;
10616
10617 /* Test whether two display-name strings agree up to the dot that separates
10618 the screen number from the server number. */
10619 static bool
10620 same_x_server (const char *name1, const char *name2)
10621 {
10622 bool seen_colon = false;
10623 Lisp_Object sysname = Fsystem_name ();
10624 const char *system_name = SSDATA (sysname);
10625 ptrdiff_t system_name_length = SBYTES (sysname);
10626 ptrdiff_t length_until_period = 0;
10627
10628 while (system_name[length_until_period] != 0
10629 && system_name[length_until_period] != '.')
10630 length_until_period++;
10631
10632 /* Treat `unix' like an empty host name. */
10633 if (! strncmp (name1, "unix:", 5))
10634 name1 += 4;
10635 if (! strncmp (name2, "unix:", 5))
10636 name2 += 4;
10637 /* Treat this host's name like an empty host name. */
10638 if (! strncmp (name1, system_name, system_name_length)
10639 && name1[system_name_length] == ':')
10640 name1 += system_name_length;
10641 if (! strncmp (name2, system_name, system_name_length)
10642 && name2[system_name_length] == ':')
10643 name2 += system_name_length;
10644 /* Treat this host's domainless name like an empty host name. */
10645 if (! strncmp (name1, system_name, length_until_period)
10646 && name1[length_until_period] == ':')
10647 name1 += length_until_period;
10648 if (! strncmp (name2, system_name, length_until_period)
10649 && name2[length_until_period] == ':')
10650 name2 += length_until_period;
10651
10652 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
10653 {
10654 if (*name1 == ':')
10655 seen_colon = true;
10656 if (seen_colon && *name1 == '.')
10657 return true;
10658 }
10659 return (seen_colon
10660 && (*name1 == '.' || *name1 == '\0')
10661 && (*name2 == '.' || *name2 == '\0'));
10662 }
10663
10664 /* Count number of set bits in mask and number of bits to shift to
10665 get to the first bit. With MASK 0x7e0, *BITS is set to 6, and *OFFSET
10666 to 5. */
10667 static void
10668 get_bits_and_offset (unsigned long mask, int *bits, int *offset)
10669 {
10670 int nr = 0;
10671 int off = 0;
10672
10673 while (!(mask & 1))
10674 {
10675 off++;
10676 mask >>= 1;
10677 }
10678
10679 while (mask & 1)
10680 {
10681 nr++;
10682 mask >>= 1;
10683 }
10684
10685 *offset = off;
10686 *bits = nr;
10687 }
10688
10689 /* Return true iff display DISPLAY is available for use.
10690 But don't permanently open it, just test its availability. */
10691
10692 bool
10693 x_display_ok (const char *display)
10694 {
10695 /* XOpenDisplay fails if it gets a signal. Block SIGIO which may arrive. */
10696 unrequest_sigio ();
10697 Display *dpy = XOpenDisplay (display);
10698 request_sigio ();
10699 if (!dpy)
10700 return false;
10701 XCloseDisplay (dpy);
10702 return true;
10703 }
10704
10705 #ifdef USE_GTK
10706 static void
10707 my_log_handler (const gchar *log_domain, GLogLevelFlags log_level,
10708 const gchar *msg, gpointer user_data)
10709 {
10710 if (!strstr (msg, "g_set_prgname"))
10711 fprintf (stderr, "%s-WARNING **: %s\n", log_domain, msg);
10712 }
10713 #endif
10714
10715 /* Create invisible cursor on X display referred by DPYINFO. */
10716
10717 static Cursor
10718 make_invisible_cursor (struct x_display_info *dpyinfo)
10719 {
10720 Display *dpy = dpyinfo->display;
10721 static char const no_data[] = { 0 };
10722 Pixmap pix;
10723 XColor col;
10724 Cursor c = 0;
10725
10726 x_catch_errors (dpy);
10727 pix = XCreateBitmapFromData (dpy, dpyinfo->root_window, no_data, 1, 1);
10728 if (! x_had_errors_p (dpy) && pix != None)
10729 {
10730 Cursor pixc;
10731 col.pixel = 0;
10732 col.red = col.green = col.blue = 0;
10733 col.flags = DoRed | DoGreen | DoBlue;
10734 pixc = XCreatePixmapCursor (dpy, pix, pix, &col, &col, 0, 0);
10735 if (! x_had_errors_p (dpy) && pixc != None)
10736 c = pixc;
10737 XFreePixmap (dpy, pix);
10738 }
10739
10740 x_uncatch_errors ();
10741
10742 return c;
10743 }
10744
10745 /* True if DPY supports Xfixes extension >= 4. */
10746
10747 static bool
10748 x_probe_xfixes_extension (Display *dpy)
10749 {
10750 #ifdef HAVE_XFIXES
10751 int major, minor;
10752 return XFixesQueryVersion (dpy, &major, &minor) && major >= 4;
10753 #else
10754 return false;
10755 #endif /* HAVE_XFIXES */
10756 }
10757
10758 /* Toggle mouse pointer visibility on frame F by using Xfixes functions. */
10759
10760 static void
10761 xfixes_toggle_visible_pointer (struct frame *f, bool invisible)
10762 {
10763 #ifdef HAVE_XFIXES
10764 if (invisible)
10765 XFixesHideCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
10766 else
10767 XFixesShowCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
10768 f->pointer_invisible = invisible;
10769 #else
10770 emacs_abort ();
10771 #endif /* HAVE_XFIXES */
10772 }
10773
10774 /* Toggle mouse pointer visibility on frame F by using invisible cursor. */
10775
10776 static void
10777 x_toggle_visible_pointer (struct frame *f, bool invisible)
10778 {
10779 eassert (FRAME_DISPLAY_INFO (f)->invisible_cursor != 0);
10780 if (invisible)
10781 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10782 FRAME_DISPLAY_INFO (f)->invisible_cursor);
10783 else
10784 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10785 f->output_data.x->current_cursor);
10786 f->pointer_invisible = invisible;
10787 }
10788
10789 /* Setup pointer blanking, prefer Xfixes if available. */
10790
10791 static void
10792 x_setup_pointer_blanking (struct x_display_info *dpyinfo)
10793 {
10794 /* FIXME: the brave tester should set EMACS_XFIXES because we're suspecting
10795 X server bug, see http://debbugs.gnu.org/cgi/bugreport.cgi?bug=17609. */
10796 if (egetenv ("EMACS_XFIXES") && x_probe_xfixes_extension (dpyinfo->display))
10797 dpyinfo->toggle_visible_pointer = xfixes_toggle_visible_pointer;
10798 else
10799 {
10800 dpyinfo->toggle_visible_pointer = x_toggle_visible_pointer;
10801 dpyinfo->invisible_cursor = make_invisible_cursor (dpyinfo);
10802 }
10803 }
10804
10805 /* Current X display connection identifier. Incremented for each next
10806 connection established. */
10807 static unsigned x_display_id;
10808
10809 /* Open a connection to X display DISPLAY_NAME, and return
10810 the structure that describes the open display.
10811 If we cannot contact the display, return null. */
10812
10813 struct x_display_info *
10814 x_term_init (Lisp_Object display_name, char *xrm_option, char *resource_name)
10815 {
10816 Display *dpy;
10817 struct terminal *terminal;
10818 struct x_display_info *dpyinfo;
10819 XrmDatabase xrdb;
10820 ptrdiff_t lim;
10821
10822 block_input ();
10823
10824 if (!x_initialized)
10825 {
10826 x_initialize ();
10827 ++x_initialized;
10828 }
10829
10830 if (! x_display_ok (SSDATA (display_name)))
10831 error ("Display %s can't be opened", SSDATA (display_name));
10832
10833 #ifdef USE_GTK
10834 {
10835 #define NUM_ARGV 10
10836 int argc;
10837 char *argv[NUM_ARGV];
10838 char **argv2 = argv;
10839 guint id;
10840
10841 if (x_initialized++ > 1)
10842 {
10843 xg_display_open (SSDATA (display_name), &dpy);
10844 }
10845 else
10846 {
10847 static char display_opt[] = "--display";
10848 static char name_opt[] = "--name";
10849
10850 for (argc = 0; argc < NUM_ARGV; ++argc)
10851 argv[argc] = 0;
10852
10853 argc = 0;
10854 argv[argc++] = initial_argv[0];
10855
10856 if (! NILP (display_name))
10857 {
10858 argv[argc++] = display_opt;
10859 argv[argc++] = SSDATA (display_name);
10860 }
10861
10862 argv[argc++] = name_opt;
10863 argv[argc++] = resource_name;
10864
10865 XSetLocaleModifiers ("");
10866
10867 /* Work around GLib bug that outputs a faulty warning. See
10868 https://bugzilla.gnome.org/show_bug.cgi?id=563627. */
10869 id = g_log_set_handler ("GLib", G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL
10870 | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
10871
10872 /* NULL window -> events for all windows go to our function.
10873 Call before gtk_init so Gtk+ event filters comes after our. */
10874 gdk_window_add_filter (NULL, event_handler_gdk, NULL);
10875
10876 /* gtk_init does set_locale. Fix locale before and after. */
10877 fixup_locale ();
10878 unrequest_sigio (); /* See comment in x_display_ok. */
10879 gtk_init (&argc, &argv2);
10880 request_sigio ();
10881 fixup_locale ();
10882
10883 g_log_remove_handler ("GLib", id);
10884
10885 xg_initialize ();
10886
10887 dpy = DEFAULT_GDK_DISPLAY ();
10888
10889 #if ! GTK_CHECK_VERSION (2, 90, 0)
10890 /* Load our own gtkrc if it exists. */
10891 {
10892 const char *file = "~/.emacs.d/gtkrc";
10893 Lisp_Object s, abs_file;
10894
10895 s = build_string (file);
10896 abs_file = Fexpand_file_name (s, Qnil);
10897
10898 if (! NILP (abs_file) && !NILP (Ffile_readable_p (abs_file)))
10899 gtk_rc_parse (SSDATA (abs_file));
10900 }
10901 #endif
10902
10903 XSetErrorHandler (x_error_handler);
10904 XSetIOErrorHandler (x_io_error_quitter);
10905 }
10906 }
10907 #else /* not USE_GTK */
10908 #ifdef USE_X_TOOLKIT
10909 /* weiner@footloose.sps.mot.com reports that this causes
10910 errors with X11R5:
10911 X protocol error: BadAtom (invalid Atom parameter)
10912 on protocol request 18skiloaf.
10913 So let's not use it until R6. */
10914 #ifdef HAVE_X11XTR6
10915 XtSetLanguageProc (NULL, NULL, NULL);
10916 #endif
10917
10918 {
10919 int argc = 0;
10920 char *argv[3];
10921
10922 argv[0] = "";
10923 argc = 1;
10924 if (xrm_option)
10925 {
10926 argv[argc++] = "-xrm";
10927 argv[argc++] = xrm_option;
10928 }
10929 turn_on_atimers (false);
10930 unrequest_sigio (); /* See comment in x_display_ok. */
10931 dpy = XtOpenDisplay (Xt_app_con, SSDATA (display_name),
10932 resource_name, EMACS_CLASS,
10933 emacs_options, XtNumber (emacs_options),
10934 &argc, argv);
10935 request_sigio ();
10936 turn_on_atimers (true);
10937
10938 #ifdef HAVE_X11XTR6
10939 /* I think this is to compensate for XtSetLanguageProc. */
10940 fixup_locale ();
10941 #endif
10942 }
10943
10944 #else /* not USE_X_TOOLKIT */
10945 XSetLocaleModifiers ("");
10946 unrequest_sigio (); // See comment in x_display_ok.
10947 dpy = XOpenDisplay (SSDATA (display_name));
10948 request_sigio ();
10949 #endif /* not USE_X_TOOLKIT */
10950 #endif /* not USE_GTK*/
10951
10952 /* Detect failure. */
10953 if (dpy == 0)
10954 {
10955 unblock_input ();
10956 return 0;
10957 }
10958
10959 /* We have definitely succeeded. Record the new connection. */
10960
10961 dpyinfo = xzalloc (sizeof *dpyinfo);
10962 terminal = x_create_terminal (dpyinfo);
10963
10964 {
10965 struct x_display_info *share;
10966
10967 for (share = x_display_list; share; share = share->next)
10968 if (same_x_server (SSDATA (XCAR (share->name_list_element)),
10969 SSDATA (display_name)))
10970 break;
10971 if (share)
10972 terminal->kboard = share->terminal->kboard;
10973 else
10974 {
10975 terminal->kboard = allocate_kboard (Qx);
10976
10977 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
10978 {
10979 char *vendor = ServerVendor (dpy);
10980
10981 /* Protect terminal from GC before removing it from the
10982 list of terminals. */
10983 struct gcpro gcpro1;
10984 Lisp_Object gcpro_term;
10985 XSETTERMINAL (gcpro_term, terminal);
10986 GCPRO1 (gcpro_term);
10987
10988 /* Temporarily hide the partially initialized terminal. */
10989 terminal_list = terminal->next_terminal;
10990 unblock_input ();
10991 kset_system_key_alist
10992 (terminal->kboard,
10993 call1 (Qvendor_specific_keysyms,
10994 vendor ? build_string (vendor) : empty_unibyte_string));
10995 block_input ();
10996 terminal->next_terminal = terminal_list;
10997 terminal_list = terminal;
10998 UNGCPRO;
10999 }
11000
11001 /* Don't let the initial kboard remain current longer than necessary.
11002 That would cause problems if a file loaded on startup tries to
11003 prompt in the mini-buffer. */
11004 if (current_kboard == initial_kboard)
11005 current_kboard = terminal->kboard;
11006 }
11007 terminal->kboard->reference_count++;
11008 }
11009
11010 /* Put this display on the chain. */
11011 dpyinfo->next = x_display_list;
11012 x_display_list = dpyinfo;
11013
11014 dpyinfo->name_list_element = Fcons (display_name, Qnil);
11015 dpyinfo->display = dpy;
11016 dpyinfo->connection = ConnectionNumber (dpyinfo->display);
11017
11018 /* Set the name of the terminal. */
11019 terminal->name = xlispstrdup (display_name);
11020
11021 #if false
11022 XSetAfterFunction (x_current_display, x_trace_wire);
11023 #endif
11024
11025 lim = min (PTRDIFF_MAX, SIZE_MAX) - sizeof "@";
11026 Lisp_Object system_name = Fsystem_name ();
11027 if (lim - SBYTES (Vinvocation_name) < SBYTES (system_name))
11028 memory_full (SIZE_MAX);
11029 dpyinfo->x_id = ++x_display_id;
11030 dpyinfo->x_id_name = xmalloc (SBYTES (Vinvocation_name)
11031 + SBYTES (system_name) + 2);
11032 char *nametail = lispstpcpy (dpyinfo->x_id_name, Vinvocation_name);
11033 *nametail++ = '@';
11034 lispstpcpy (nametail, system_name);
11035
11036 /* Figure out which modifier bits mean what. */
11037 x_find_modifier_meanings (dpyinfo);
11038
11039 /* Get the scroll bar cursor. */
11040 #ifdef USE_GTK
11041 /* We must create a GTK cursor, it is required for GTK widgets. */
11042 dpyinfo->xg_cursor = xg_create_default_cursor (dpyinfo->display);
11043 #endif /* USE_GTK */
11044
11045 dpyinfo->vertical_scroll_bar_cursor
11046 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
11047
11048 dpyinfo->horizontal_scroll_bar_cursor
11049 = XCreateFontCursor (dpyinfo->display, XC_sb_h_double_arrow);
11050
11051 xrdb = x_load_resources (dpyinfo->display, xrm_option,
11052 resource_name, EMACS_CLASS);
11053 #ifdef HAVE_XRMSETDATABASE
11054 XrmSetDatabase (dpyinfo->display, xrdb);
11055 #else
11056 dpyinfo->display->db = xrdb;
11057 #endif
11058 /* Put the rdb where we can find it in a way that works on
11059 all versions. */
11060 dpyinfo->xrdb = xrdb;
11061
11062 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
11063 DefaultScreen (dpyinfo->display));
11064 select_visual (dpyinfo);
11065 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
11066 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
11067 dpyinfo->icon_bitmap_id = -1;
11068 dpyinfo->wm_type = X_WMTYPE_UNKNOWN;
11069
11070 reset_mouse_highlight (&dpyinfo->mouse_highlight);
11071
11072 /* See if we can construct pixel values from RGB values. */
11073 if (dpyinfo->visual->class == TrueColor)
11074 {
11075 get_bits_and_offset (dpyinfo->visual->red_mask,
11076 &dpyinfo->red_bits, &dpyinfo->red_offset);
11077 get_bits_and_offset (dpyinfo->visual->blue_mask,
11078 &dpyinfo->blue_bits, &dpyinfo->blue_offset);
11079 get_bits_and_offset (dpyinfo->visual->green_mask,
11080 &dpyinfo->green_bits, &dpyinfo->green_offset);
11081 }
11082
11083 /* See if a private colormap is requested. */
11084 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
11085 {
11086 if (dpyinfo->visual->class == PseudoColor)
11087 {
11088 AUTO_STRING (privateColormap, "privateColormap");
11089 AUTO_STRING (PrivateColormap, "PrivateColormap");
11090 Lisp_Object value
11091 = display_x_get_resource (dpyinfo, privateColormap,
11092 PrivateColormap, Qnil, Qnil);
11093 if (STRINGP (value)
11094 && (!strcmp (SSDATA (value), "true")
11095 || !strcmp (SSDATA (value), "on")))
11096 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
11097 }
11098 }
11099 else
11100 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
11101 dpyinfo->visual, AllocNone);
11102
11103 #ifdef HAVE_XFT
11104 {
11105 /* If we are using Xft, the following precautions should be made:
11106
11107 1. Make sure that the Xrender extension is added before the Xft one.
11108 Otherwise, the close-display hook set by Xft is called after the one
11109 for Xrender, and the former tries to re-add the latter. This results
11110 in inconsistency of internal states and leads to X protocol error when
11111 one reconnects to the same X server (Bug#1696).
11112
11113 2. Check dpi value in X resources. It is better we use it as well,
11114 since Xft will use it, as will all Gnome applications. If our real DPI
11115 is smaller or larger than the one Xft uses, our font will look smaller
11116 or larger than other for other applications, even if it is the same
11117 font name (monospace-10 for example). */
11118
11119 int event_base, error_base;
11120 char *v;
11121 double d;
11122
11123 XRenderQueryExtension (dpyinfo->display, &event_base, &error_base);
11124
11125 v = XGetDefault (dpyinfo->display, "Xft", "dpi");
11126 if (v != NULL && sscanf (v, "%lf", &d) == 1)
11127 dpyinfo->resy = dpyinfo->resx = d;
11128 }
11129 #endif
11130
11131 if (dpyinfo->resy < 1)
11132 {
11133 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
11134 double pixels = DisplayHeight (dpyinfo->display, screen_number);
11135 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
11136 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
11137 dpyinfo->resy = (mm < 1) ? 100 : pixels * 25.4 / mm;
11138 pixels = DisplayWidth (dpyinfo->display, screen_number);
11139 mm = DisplayWidthMM (dpyinfo->display, screen_number);
11140 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
11141 dpyinfo->resx = (mm < 1) ? 100 : pixels * 25.4 / mm;
11142 }
11143
11144 {
11145 static const struct
11146 {
11147 const char *name;
11148 int offset;
11149 } atom_refs[] = {
11150 #define ATOM_REFS_INIT(string, member) \
11151 { string, offsetof (struct x_display_info, member) },
11152 ATOM_REFS_INIT ("WM_PROTOCOLS", Xatom_wm_protocols)
11153 ATOM_REFS_INIT ("WM_TAKE_FOCUS", Xatom_wm_take_focus)
11154 ATOM_REFS_INIT ("WM_SAVE_YOURSELF", Xatom_wm_save_yourself)
11155 ATOM_REFS_INIT ("WM_DELETE_WINDOW", Xatom_wm_delete_window)
11156 ATOM_REFS_INIT ("WM_CHANGE_STATE", Xatom_wm_change_state)
11157 ATOM_REFS_INIT ("WM_CONFIGURE_DENIED", Xatom_wm_configure_denied)
11158 ATOM_REFS_INIT ("WM_MOVED", Xatom_wm_window_moved)
11159 ATOM_REFS_INIT ("WM_CLIENT_LEADER", Xatom_wm_client_leader)
11160 ATOM_REFS_INIT ("Editres", Xatom_editres)
11161 ATOM_REFS_INIT ("CLIPBOARD", Xatom_CLIPBOARD)
11162 ATOM_REFS_INIT ("TIMESTAMP", Xatom_TIMESTAMP)
11163 ATOM_REFS_INIT ("TEXT", Xatom_TEXT)
11164 ATOM_REFS_INIT ("COMPOUND_TEXT", Xatom_COMPOUND_TEXT)
11165 ATOM_REFS_INIT ("UTF8_STRING", Xatom_UTF8_STRING)
11166 ATOM_REFS_INIT ("DELETE", Xatom_DELETE)
11167 ATOM_REFS_INIT ("MULTIPLE", Xatom_MULTIPLE)
11168 ATOM_REFS_INIT ("INCR", Xatom_INCR)
11169 ATOM_REFS_INIT ("_EMACS_TMP_", Xatom_EMACS_TMP)
11170 ATOM_REFS_INIT ("TARGETS", Xatom_TARGETS)
11171 ATOM_REFS_INIT ("NULL", Xatom_NULL)
11172 ATOM_REFS_INIT ("ATOM", Xatom_ATOM)
11173 ATOM_REFS_INIT ("ATOM_PAIR", Xatom_ATOM_PAIR)
11174 ATOM_REFS_INIT ("CLIPBOARD_MANAGER", Xatom_CLIPBOARD_MANAGER)
11175 ATOM_REFS_INIT ("_XEMBED_INFO", Xatom_XEMBED_INFO)
11176 /* For properties of font. */
11177 ATOM_REFS_INIT ("PIXEL_SIZE", Xatom_PIXEL_SIZE)
11178 ATOM_REFS_INIT ("AVERAGE_WIDTH", Xatom_AVERAGE_WIDTH)
11179 ATOM_REFS_INIT ("_MULE_BASELINE_OFFSET", Xatom_MULE_BASELINE_OFFSET)
11180 ATOM_REFS_INIT ("_MULE_RELATIVE_COMPOSE", Xatom_MULE_RELATIVE_COMPOSE)
11181 ATOM_REFS_INIT ("_MULE_DEFAULT_ASCENT", Xatom_MULE_DEFAULT_ASCENT)
11182 /* Ghostscript support. */
11183 ATOM_REFS_INIT ("DONE", Xatom_DONE)
11184 ATOM_REFS_INIT ("PAGE", Xatom_PAGE)
11185 ATOM_REFS_INIT ("SCROLLBAR", Xatom_Scrollbar)
11186 ATOM_REFS_INIT ("HORIZONTAL_SCROLLBAR", Xatom_Horizontal_Scrollbar)
11187 ATOM_REFS_INIT ("_XEMBED", Xatom_XEMBED)
11188 /* EWMH */
11189 ATOM_REFS_INIT ("_NET_WM_STATE", Xatom_net_wm_state)
11190 ATOM_REFS_INIT ("_NET_WM_STATE_FULLSCREEN", Xatom_net_wm_state_fullscreen)
11191 ATOM_REFS_INIT ("_NET_WM_STATE_MAXIMIZED_HORZ",
11192 Xatom_net_wm_state_maximized_horz)
11193 ATOM_REFS_INIT ("_NET_WM_STATE_MAXIMIZED_VERT",
11194 Xatom_net_wm_state_maximized_vert)
11195 ATOM_REFS_INIT ("_NET_WM_STATE_STICKY", Xatom_net_wm_state_sticky)
11196 ATOM_REFS_INIT ("_NET_WM_STATE_HIDDEN", Xatom_net_wm_state_hidden)
11197 ATOM_REFS_INIT ("_NET_WM_WINDOW_TYPE", Xatom_net_window_type)
11198 ATOM_REFS_INIT ("_NET_WM_WINDOW_TYPE_TOOLTIP",
11199 Xatom_net_window_type_tooltip)
11200 ATOM_REFS_INIT ("_NET_WM_ICON_NAME", Xatom_net_wm_icon_name)
11201 ATOM_REFS_INIT ("_NET_WM_NAME", Xatom_net_wm_name)
11202 ATOM_REFS_INIT ("_NET_SUPPORTED", Xatom_net_supported)
11203 ATOM_REFS_INIT ("_NET_SUPPORTING_WM_CHECK", Xatom_net_supporting_wm_check)
11204 ATOM_REFS_INIT ("_NET_WM_WINDOW_OPACITY", Xatom_net_wm_window_opacity)
11205 ATOM_REFS_INIT ("_NET_ACTIVE_WINDOW", Xatom_net_active_window)
11206 ATOM_REFS_INIT ("_NET_FRAME_EXTENTS", Xatom_net_frame_extents)
11207 ATOM_REFS_INIT ("_NET_CURRENT_DESKTOP", Xatom_net_current_desktop)
11208 ATOM_REFS_INIT ("_NET_WORKAREA", Xatom_net_workarea)
11209 /* Session management */
11210 ATOM_REFS_INIT ("SM_CLIENT_ID", Xatom_SM_CLIENT_ID)
11211 ATOM_REFS_INIT ("_XSETTINGS_SETTINGS", Xatom_xsettings_prop)
11212 ATOM_REFS_INIT ("MANAGER", Xatom_xsettings_mgr)
11213 };
11214
11215 int i;
11216 enum { atom_count = ARRAYELTS (atom_refs) };
11217 /* 1 for _XSETTINGS_SN. */
11218 enum { total_atom_count = 1 + atom_count };
11219 Atom atoms_return[total_atom_count];
11220 char *atom_names[total_atom_count];
11221 static char const xsettings_fmt[] = "_XSETTINGS_S%d";
11222 char xsettings_atom_name[sizeof xsettings_fmt - 2
11223 + INT_STRLEN_BOUND (int)];
11224
11225 for (i = 0; i < atom_count; i++)
11226 atom_names[i] = (char *) atom_refs[i].name;
11227
11228 /* Build _XSETTINGS_SN atom name. */
11229 sprintf (xsettings_atom_name, xsettings_fmt,
11230 XScreenNumberOfScreen (dpyinfo->screen));
11231 atom_names[i] = xsettings_atom_name;
11232
11233 XInternAtoms (dpyinfo->display, atom_names, total_atom_count,
11234 False, atoms_return);
11235
11236 for (i = 0; i < atom_count; i++)
11237 *(Atom *) ((char *) dpyinfo + atom_refs[i].offset) = atoms_return[i];
11238
11239 /* Manually copy last atom. */
11240 dpyinfo->Xatom_xsettings_sel = atoms_return[i];
11241 }
11242
11243 dpyinfo->x_dnd_atoms_size = 8;
11244 dpyinfo->x_dnd_atoms = xmalloc (sizeof *dpyinfo->x_dnd_atoms
11245 * dpyinfo->x_dnd_atoms_size);
11246 dpyinfo->gray
11247 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
11248 gray_bits, gray_width, gray_height,
11249 1, 0, 1);
11250
11251 x_setup_pointer_blanking (dpyinfo);
11252
11253 #ifdef HAVE_X_I18N
11254 xim_initialize (dpyinfo, resource_name);
11255 #endif
11256
11257 xsettings_initialize (dpyinfo);
11258
11259 /* This is only needed for distinguishing keyboard and process input. */
11260 if (dpyinfo->connection != 0)
11261 add_keyboard_wait_descriptor (dpyinfo->connection);
11262
11263 #ifdef F_SETOWN
11264 fcntl (dpyinfo->connection, F_SETOWN, getpid ());
11265 #endif /* ! defined (F_SETOWN) */
11266
11267 if (interrupt_input)
11268 init_sigio (dpyinfo->connection);
11269
11270 #ifdef USE_LUCID
11271 {
11272 XrmValue d, fr, to;
11273 Font font;
11274
11275 dpy = dpyinfo->display;
11276 d.addr = (XPointer)&dpy;
11277 d.size = sizeof (Display *);
11278 fr.addr = XtDefaultFont;
11279 fr.size = sizeof (XtDefaultFont);
11280 to.size = sizeof (Font *);
11281 to.addr = (XPointer)&font;
11282 x_catch_errors (dpy);
11283 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
11284 emacs_abort ();
11285 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
11286 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
11287 /* Do not free XFontStruct returned by the above call to XQueryFont.
11288 This leads to X protocol errors at XtCloseDisplay (Bug#18403). */
11289 x_uncatch_errors ();
11290 }
11291 #endif
11292
11293 /* See if we should run in synchronous mode. This is useful
11294 for debugging X code. */
11295 {
11296 AUTO_STRING (synchronous, "synchronous");
11297 AUTO_STRING (Synchronous, "Synchronous");
11298 Lisp_Object value = display_x_get_resource (dpyinfo, synchronous,
11299 Synchronous, Qnil, Qnil);
11300 if (STRINGP (value)
11301 && (!strcmp (SSDATA (value), "true")
11302 || !strcmp (SSDATA (value), "on")))
11303 XSynchronize (dpyinfo->display, True);
11304 }
11305
11306 {
11307 AUTO_STRING (useXIM, "useXIM");
11308 AUTO_STRING (UseXIM, "UseXIM");
11309 Lisp_Object value = display_x_get_resource (dpyinfo, useXIM, UseXIM,
11310 Qnil, Qnil);
11311 #ifdef USE_XIM
11312 if (STRINGP (value)
11313 && (!strcmp (SSDATA (value), "false")
11314 || !strcmp (SSDATA (value), "off")))
11315 use_xim = false;
11316 #else
11317 if (STRINGP (value)
11318 && (!strcmp (SSDATA (value), "true")
11319 || !strcmp (SSDATA (value), "on")))
11320 use_xim = true;
11321 #endif
11322 }
11323
11324 #ifdef HAVE_X_SM
11325 /* Only do this for the very first display in the Emacs session.
11326 Ignore X session management when Emacs was first started on a
11327 tty or started as a daemon. */
11328 if (terminal->id == 1 && ! IS_DAEMON)
11329 x_session_initialize (dpyinfo);
11330 #endif
11331
11332 unblock_input ();
11333
11334 return dpyinfo;
11335 }
11336 \f
11337 /* Get rid of display DPYINFO, deleting all frames on it,
11338 and without sending any more commands to the X server. */
11339
11340 static void
11341 x_delete_display (struct x_display_info *dpyinfo)
11342 {
11343 struct terminal *t;
11344
11345 /* Close all frames and delete the generic struct terminal for this
11346 X display. */
11347 for (t = terminal_list; t; t = t->next_terminal)
11348 if (t->type == output_x_window && t->display_info.x == dpyinfo)
11349 {
11350 #ifdef HAVE_X_SM
11351 /* Close X session management when we close its display. */
11352 if (t->id == 1 && x_session_have_connection ())
11353 x_session_close ();
11354 #endif
11355 delete_terminal (t);
11356 break;
11357 }
11358
11359 if (next_noop_dpyinfo == dpyinfo)
11360 next_noop_dpyinfo = dpyinfo->next;
11361
11362 if (x_display_list == dpyinfo)
11363 x_display_list = dpyinfo->next;
11364 else
11365 {
11366 struct x_display_info *tail;
11367
11368 for (tail = x_display_list; tail; tail = tail->next)
11369 if (tail->next == dpyinfo)
11370 tail->next = tail->next->next;
11371 }
11372
11373 xfree (dpyinfo->x_id_name);
11374 xfree (dpyinfo->x_dnd_atoms);
11375 xfree (dpyinfo->color_cells);
11376 xfree (dpyinfo);
11377 }
11378
11379 #ifdef USE_X_TOOLKIT
11380
11381 /* Atimer callback function for TIMER. Called every 0.1s to process
11382 Xt timeouts, if needed. We must avoid calling XtAppPending as
11383 much as possible because that function does an implicit XFlush
11384 that slows us down. */
11385
11386 static void
11387 x_process_timeouts (struct atimer *timer)
11388 {
11389 block_input ();
11390 x_timeout_atimer_activated_flag = false;
11391 if (toolkit_scroll_bar_interaction || popup_activated ())
11392 {
11393 while (XtAppPending (Xt_app_con) & XtIMTimer)
11394 XtAppProcessEvent (Xt_app_con, XtIMTimer);
11395 /* Reactivate the atimer for next time. */
11396 x_activate_timeout_atimer ();
11397 }
11398 unblock_input ();
11399 }
11400
11401 /* Install an asynchronous timer that processes Xt timeout events
11402 every 0.1s as long as either `toolkit_scroll_bar_interaction' or
11403 `popup_activated_flag' (in xmenu.c) is set. Make sure to call this
11404 function whenever these variables are set. This is necessary
11405 because some widget sets use timeouts internally, for example the
11406 LessTif menu bar, or the Xaw3d scroll bar. When Xt timeouts aren't
11407 processed, these widgets don't behave normally. */
11408
11409 void
11410 x_activate_timeout_atimer (void)
11411 {
11412 block_input ();
11413 if (!x_timeout_atimer_activated_flag)
11414 {
11415 struct timespec interval = make_timespec (0, 100 * 1000 * 1000);
11416 start_atimer (ATIMER_RELATIVE, interval, x_process_timeouts, 0);
11417 x_timeout_atimer_activated_flag = true;
11418 }
11419 unblock_input ();
11420 }
11421
11422 #endif /* USE_X_TOOLKIT */
11423
11424 \f
11425 /* Set up use of X before we make the first connection. */
11426
11427 static struct redisplay_interface x_redisplay_interface =
11428 {
11429 x_frame_parm_handlers,
11430 x_produce_glyphs,
11431 x_write_glyphs,
11432 x_insert_glyphs,
11433 x_clear_end_of_line,
11434 x_scroll_run,
11435 x_after_update_window_line,
11436 x_update_window_begin,
11437 x_update_window_end,
11438 x_flush,
11439 x_clear_window_mouse_face,
11440 x_get_glyph_overhangs,
11441 x_fix_overlapping_area,
11442 x_draw_fringe_bitmap,
11443 0, /* define_fringe_bitmap */
11444 0, /* destroy_fringe_bitmap */
11445 x_compute_glyph_string_overhangs,
11446 x_draw_glyph_string,
11447 x_define_frame_cursor,
11448 x_clear_frame_area,
11449 x_draw_window_cursor,
11450 x_draw_vertical_window_border,
11451 x_draw_window_divider,
11452 x_shift_glyphs_for_insert,
11453 x_show_hourglass,
11454 x_hide_hourglass
11455 };
11456
11457
11458 /* This function is called when the last frame on a display is deleted. */
11459 void
11460 x_delete_terminal (struct terminal *terminal)
11461 {
11462 struct x_display_info *dpyinfo = terminal->display_info.x;
11463
11464 /* Protect against recursive calls. delete_frame in
11465 delete_terminal calls us back when it deletes our last frame. */
11466 if (!terminal->name)
11467 return;
11468
11469 block_input ();
11470 #ifdef HAVE_X_I18N
11471 /* We must close our connection to the XIM server before closing the
11472 X display. */
11473 if (dpyinfo->xim)
11474 xim_close_dpy (dpyinfo);
11475 #endif
11476
11477 /* Normally, the display is available... */
11478 if (dpyinfo->display)
11479 {
11480 x_destroy_all_bitmaps (dpyinfo);
11481 XSetCloseDownMode (dpyinfo->display, DestroyAll);
11482
11483 /* Whether or not XCloseDisplay destroys the associated resource
11484 database depends on the version of libX11. To avoid both
11485 crash and memory leak, we dissociate the database from the
11486 display and then destroy dpyinfo->xrdb ourselves.
11487
11488 Unfortunately, the above strategy does not work in some
11489 situations due to a bug in newer versions of libX11: because
11490 XrmSetDatabase doesn't clear the flag XlibDisplayDfltRMDB if
11491 dpy->db is NULL, XCloseDisplay destroys the associated
11492 database whereas it has not been created by XGetDefault
11493 (Bug#21974 in freedesktop.org Bugzilla). As a workaround, we
11494 don't destroy the database here in order to avoid the crash
11495 in the above situations for now, though that may cause memory
11496 leaks in other situations. */
11497 #if false
11498 #ifdef HAVE_XRMSETDATABASE
11499 XrmSetDatabase (dpyinfo->display, NULL);
11500 #else
11501 dpyinfo->display->db = NULL;
11502 #endif
11503 /* We used to call XrmDestroyDatabase from x_delete_display, but
11504 some older versions of libX11 crash if we call it after
11505 closing all the displays. */
11506 XrmDestroyDatabase (dpyinfo->xrdb);
11507 #endif
11508
11509 #ifdef USE_GTK
11510 xg_display_close (dpyinfo->display);
11511 #else
11512 #ifdef USE_X_TOOLKIT
11513 XtCloseDisplay (dpyinfo->display);
11514 #else
11515 XCloseDisplay (dpyinfo->display);
11516 #endif
11517 #endif /* ! USE_GTK */
11518 /* Do not close the connection here because it's already closed
11519 by X(t)CloseDisplay (Bug#18403). */
11520 dpyinfo->display = NULL;
11521 }
11522
11523 /* ...but if called from x_connection_closed, the display may already
11524 be closed and dpyinfo->display was set to 0 to indicate that. Since
11525 X server is most likely gone, explicit close is the only reliable
11526 way to continue and avoid Bug#19147. */
11527 else if (dpyinfo->connection >= 0)
11528 emacs_close (dpyinfo->connection);
11529
11530 /* No more input on this descriptor. */
11531 delete_keyboard_wait_descriptor (dpyinfo->connection);
11532 /* Mark as dead. */
11533 dpyinfo->connection = -1;
11534
11535 x_delete_display (dpyinfo);
11536 unblock_input ();
11537 }
11538
11539 /* Create a struct terminal, initialize it with the X11 specific
11540 functions and make DISPLAY->TERMINAL point to it. */
11541
11542 static struct terminal *
11543 x_create_terminal (struct x_display_info *dpyinfo)
11544 {
11545 struct terminal *terminal;
11546
11547 terminal = create_terminal (output_x_window, &x_redisplay_interface);
11548
11549 terminal->display_info.x = dpyinfo;
11550 dpyinfo->terminal = terminal;
11551
11552 /* kboard is initialized in x_term_init. */
11553
11554 terminal->clear_frame_hook = x_clear_frame;
11555 terminal->ins_del_lines_hook = x_ins_del_lines;
11556 terminal->delete_glyphs_hook = x_delete_glyphs;
11557 terminal->ring_bell_hook = XTring_bell;
11558 terminal->toggle_invisible_pointer_hook = XTtoggle_invisible_pointer;
11559 terminal->update_begin_hook = x_update_begin;
11560 terminal->update_end_hook = x_update_end;
11561 terminal->read_socket_hook = XTread_socket;
11562 terminal->frame_up_to_date_hook = XTframe_up_to_date;
11563 terminal->mouse_position_hook = XTmouse_position;
11564 terminal->frame_rehighlight_hook = XTframe_rehighlight;
11565 terminal->frame_raise_lower_hook = XTframe_raise_lower;
11566 terminal->fullscreen_hook = XTfullscreen_hook;
11567 terminal->menu_show_hook = x_menu_show;
11568 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
11569 terminal->popup_dialog_hook = xw_popup_dialog;
11570 #endif
11571 terminal->set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
11572 terminal->set_horizontal_scroll_bar_hook = XTset_horizontal_scroll_bar;
11573 terminal->condemn_scroll_bars_hook = XTcondemn_scroll_bars;
11574 terminal->redeem_scroll_bar_hook = XTredeem_scroll_bar;
11575 terminal->judge_scroll_bars_hook = XTjudge_scroll_bars;
11576 terminal->delete_frame_hook = x_destroy_window;
11577 terminal->delete_terminal_hook = x_delete_terminal;
11578 /* Other hooks are NULL by default. */
11579
11580 return terminal;
11581 }
11582
11583 static void
11584 x_initialize (void)
11585 {
11586 baud_rate = 19200;
11587
11588 x_noop_count = 0;
11589 any_help_event_p = false;
11590 ignore_next_mouse_click_timeout = 0;
11591
11592 #ifdef USE_GTK
11593 current_count = -1;
11594 #endif
11595
11596 /* Try to use interrupt input; if we can't, then start polling. */
11597 Fset_input_interrupt_mode (Qt);
11598
11599 #ifdef USE_X_TOOLKIT
11600 XtToolkitInitialize ();
11601
11602 Xt_app_con = XtCreateApplicationContext ();
11603
11604 /* Register a converter from strings to pixels, which uses
11605 Emacs' color allocation infrastructure. */
11606 XtAppSetTypeConverter (Xt_app_con,
11607 XtRString, XtRPixel, cvt_string_to_pixel,
11608 cvt_string_to_pixel_args,
11609 XtNumber (cvt_string_to_pixel_args),
11610 XtCacheByDisplay, cvt_pixel_dtor);
11611
11612 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
11613 #endif
11614
11615 #ifdef USE_TOOLKIT_SCROLL_BARS
11616 #ifndef USE_GTK
11617 xaw3d_arrow_scroll = False;
11618 xaw3d_pick_top = True;
11619 #endif
11620 #endif
11621
11622 /* Note that there is no real way portable across R3/R4 to get the
11623 original error handler. */
11624 XSetErrorHandler (x_error_handler);
11625 XSetIOErrorHandler (x_io_error_quitter);
11626 }
11627
11628 #ifdef USE_GTK
11629 void
11630 init_xterm (void)
11631 {
11632 /* Emacs can handle only core input events, so make sure
11633 Gtk doesn't use Xinput or Xinput2 extensions. */
11634 xputenv ("GDK_CORE_DEVICE_EVENTS=1");
11635 }
11636 #endif
11637
11638 void
11639 syms_of_xterm (void)
11640 {
11641 x_error_message = NULL;
11642
11643 DEFSYM (Qvendor_specific_keysyms, "vendor-specific-keysyms");
11644 DEFSYM (Qlatin_1, "latin-1");
11645
11646 #ifdef USE_GTK
11647 xg_default_icon_file = build_pure_c_string ("icons/hicolor/scalable/apps/emacs.svg");
11648 staticpro (&xg_default_icon_file);
11649
11650 DEFSYM (Qx_gtk_map_stock, "x-gtk-map-stock");
11651 #endif
11652
11653 DEFVAR_BOOL ("x-use-underline-position-properties",
11654 x_use_underline_position_properties,
11655 doc: /* Non-nil means make use of UNDERLINE_POSITION font properties.
11656 A value of nil means ignore them. If you encounter fonts with bogus
11657 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
11658 to 4.1, set this to nil. You can also use `underline-minimum-offset'
11659 to override the font's UNDERLINE_POSITION for small font display
11660 sizes. */);
11661 x_use_underline_position_properties = true;
11662
11663 DEFVAR_BOOL ("x-underline-at-descent-line",
11664 x_underline_at_descent_line,
11665 doc: /* Non-nil means to draw the underline at the same place as the descent line.
11666 A value of nil means to draw the underline according to the value of the
11667 variable `x-use-underline-position-properties', which is usually at the
11668 baseline level. The default value is nil. */);
11669 x_underline_at_descent_line = false;
11670
11671 DEFVAR_BOOL ("x-mouse-click-focus-ignore-position",
11672 x_mouse_click_focus_ignore_position,
11673 doc: /* Non-nil means that a mouse click to focus a frame does not move point.
11674 This variable is only used when the window manager requires that you
11675 click on a frame to select it (give it focus). In that case, a value
11676 of nil, means that the selected window and cursor position changes to
11677 reflect the mouse click position, while a non-nil value means that the
11678 selected window or cursor position is preserved. */);
11679 x_mouse_click_focus_ignore_position = false;
11680
11681 DEFVAR_LISP ("x-toolkit-scroll-bars", Vx_toolkit_scroll_bars,
11682 doc: /* Which toolkit scroll bars Emacs uses, if any.
11683 A value of nil means Emacs doesn't use toolkit scroll bars.
11684 With the X Window system, the value is a symbol describing the
11685 X toolkit. Possible values are: gtk, motif, xaw, or xaw3d.
11686 With MS Windows or Nextstep, the value is t. */);
11687 #ifdef USE_TOOLKIT_SCROLL_BARS
11688 #ifdef USE_MOTIF
11689 Vx_toolkit_scroll_bars = intern_c_string ("motif");
11690 #elif defined HAVE_XAW3D
11691 Vx_toolkit_scroll_bars = intern_c_string ("xaw3d");
11692 #elif USE_GTK
11693 Vx_toolkit_scroll_bars = intern_c_string ("gtk");
11694 #else
11695 Vx_toolkit_scroll_bars = intern_c_string ("xaw");
11696 #endif
11697 #else
11698 Vx_toolkit_scroll_bars = Qnil;
11699 #endif
11700
11701 DEFSYM (Qmodifier_value, "modifier-value");
11702 DEFSYM (Qalt, "alt");
11703 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
11704 DEFSYM (Qhyper, "hyper");
11705 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
11706 DEFSYM (Qmeta, "meta");
11707 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
11708 DEFSYM (Qsuper, "super");
11709 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
11710
11711 DEFVAR_LISP ("x-alt-keysym", Vx_alt_keysym,
11712 doc: /* Which keys Emacs uses for the alt modifier.
11713 This should be one of the symbols `alt', `hyper', `meta', `super'.
11714 For example, `alt' means use the Alt_L and Alt_R keysyms. The default
11715 is nil, which is the same as `alt'. */);
11716 Vx_alt_keysym = Qnil;
11717
11718 DEFVAR_LISP ("x-hyper-keysym", Vx_hyper_keysym,
11719 doc: /* Which keys Emacs uses for the hyper modifier.
11720 This should be one of the symbols `alt', `hyper', `meta', `super'.
11721 For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
11722 default is nil, which is the same as `hyper'. */);
11723 Vx_hyper_keysym = Qnil;
11724
11725 DEFVAR_LISP ("x-meta-keysym", Vx_meta_keysym,
11726 doc: /* Which keys Emacs uses for the meta modifier.
11727 This should be one of the symbols `alt', `hyper', `meta', `super'.
11728 For example, `meta' means use the Meta_L and Meta_R keysyms. The
11729 default is nil, which is the same as `meta'. */);
11730 Vx_meta_keysym = Qnil;
11731
11732 DEFVAR_LISP ("x-super-keysym", Vx_super_keysym,
11733 doc: /* Which keys Emacs uses for the super modifier.
11734 This should be one of the symbols `alt', `hyper', `meta', `super'.
11735 For example, `super' means use the Super_L and Super_R keysyms. The
11736 default is nil, which is the same as `super'. */);
11737 Vx_super_keysym = Qnil;
11738
11739 DEFVAR_LISP ("x-keysym-table", Vx_keysym_table,
11740 doc: /* Hash table of character codes indexed by X keysym codes. */);
11741 Vx_keysym_table = make_hash_table (hashtest_eql, make_number (900),
11742 make_float (DEFAULT_REHASH_SIZE),
11743 make_float (DEFAULT_REHASH_THRESHOLD),
11744 Qnil);
11745
11746 DEFVAR_BOOL ("x-frame-normalize-before-maximize",
11747 x_frame_normalize_before_maximize,
11748 doc: /* Non-nil means normalize frame before maximizing.
11749 If this variable is t, Emacs first asks the window manager to give the
11750 frame its normal size, and only then the final state, whenever changing
11751 from a full-height, full-width or full-both state to the maximized one
11752 or when changing from the maximized to the full-height or full-width
11753 state.
11754
11755 Set this variable only if your window manager cannot handle the
11756 transition between the various maximization states. */);
11757 x_frame_normalize_before_maximize = false;
11758 }