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