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