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