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