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