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