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