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