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