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