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