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