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