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