]> code.delx.au - gnu-emacs/blob - src/xterm.c
Put stdio.h after config.h.
[gnu-emacs] / src / xterm.c
1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1989, 1993, 1994 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 /* Xt features made by Fred Pierresteguy. */
21
22 /* On 4.3 these lose if they come after xterm.h. */
23 /* On HP-UX 8.0 signal.h loses if it comes after config.h. */
24 /* Putting these at the beginning seems to be standard for other .c files. */
25 #include <signal.h>
26
27 #include <config.h>
28
29 #include <stdio.h>
30
31 /* Need syssignal.h for various externs and definitions that may be required
32 by some configurations for calls to signal later in this source file. */
33 #include "syssignal.h"
34
35 #ifdef HAVE_X_WINDOWS
36
37 #include "lisp.h"
38 #include "blockinput.h"
39
40 /* This may include sys/types.h, and that somehow loses
41 if this is not done before the other system files. */
42 #include "xterm.h"
43 #include <X11/cursorfont.h>
44
45 #ifndef USG
46 /* Load sys/types.h if not already loaded.
47 In some systems loading it twice is suicidal. */
48 #ifndef makedev
49 #include <sys/types.h>
50 #endif /* makedev */
51 #endif /* USG */
52
53 #ifdef BSD
54 #include <sys/ioctl.h>
55 #include <strings.h>
56 #else /* ! defined (BSD) */
57 #ifndef VMS
58 #include <string.h>
59 #endif
60 #endif /* ! defined (BSD) */
61
62 #include "systty.h"
63 #include "systime.h"
64
65 #ifndef INCLUDED_FCNTL
66 #include <fcntl.h>
67 #endif
68 #include <ctype.h>
69 #include <errno.h>
70 #include <setjmp.h>
71 #include <sys/stat.h>
72 #include <sys/param.h>
73
74 #include "dispextern.h"
75 #include "termhooks.h"
76 #include "termopts.h"
77 #include "termchar.h"
78 #if 0
79 #include "sink.h"
80 #include "sinkmask.h"
81 #endif /* ! 0 */
82 #include "gnu.h"
83 #include "frame.h"
84 #include "disptab.h"
85 #include "buffer.h"
86 #include "window.h"
87
88 #ifdef USE_X_TOOLKIT
89 extern XtAppContext Xt_app_con;
90 extern Widget Xt_app_shell;
91 extern void free_frame_menubar ();
92 extern void _XEditResCheckMessages ();
93 #endif /* USE_X_TOOLKIT */
94
95 #ifndef USE_X_TOOLKIT
96 #define x_any_window_to_frame x_window_to_frame
97 #define x_top_window_to_frame x_window_to_frame
98 #endif
99
100 #ifdef USE_X_TOOLKIT
101 #ifndef XtNinitialState
102 #define XtNinitialState "initialState"
103 #endif
104 #endif
105
106 #ifdef HAVE_X11
107 #define XMapWindow XMapRaised /* Raise them when mapping. */
108 #else /* ! defined (HAVE_X11) */
109 #include <X/Xkeyboard.h>
110 /*#include <X/Xproto.h> */
111 #endif /* ! defined (HAVE_X11) */
112
113 #ifdef FD_SET
114 /* We could get this from param.h, but better not to depend on finding that.
115 And better not to risk that it might define other symbols used in this
116 file. */
117 #ifdef FD_SETSIZE
118 #define MAXDESC FD_SETSIZE
119 #else
120 #define MAXDESC 64
121 #endif
122 #define SELECT_TYPE fd_set
123 #else /* no FD_SET */
124 #define MAXDESC 32
125 #define SELECT_TYPE int
126
127 /* Define the macros to access a single-int bitmap of descriptors. */
128 #define FD_SET(n, p) (*(p) |= (1 << (n)))
129 #define FD_CLR(n, p) (*(p) &= ~(1 << (n)))
130 #define FD_ISSET(n, p) (*(p) & (1 << (n)))
131 #define FD_ZERO(p) (*(p) = 0)
132 #endif /* no FD_SET */
133
134 /* For sending Meta-characters. Do we need this? */
135 #define METABIT 0200
136
137 #define min(a,b) ((a)<(b) ? (a) : (b))
138 #define max(a,b) ((a)>(b) ? (a) : (b))
139 \f
140 /* Nonzero means we must reprint all windows
141 because 1) we received an ExposeWindow event
142 or 2) we received too many ExposeRegion events to record.
143
144 This is never needed under X11. */
145 static int expose_all_windows;
146
147 /* Nonzero means we must reprint all icon windows. */
148
149 static int expose_all_icons;
150
151 #ifndef HAVE_X11
152 /* ExposeRegion events, when received, are copied into this queue
153 for later processing. */
154
155 static struct event_queue x_expose_queue;
156
157 /* ButtonPress and ButtonReleased events, when received,
158 are copied into this queue for later processing. */
159
160 struct event_queue x_mouse_queue;
161 #endif /* HAVE_X11 */
162
163 #if defined (SIGIO) && defined (FIONREAD)
164 int BLOCK_INPUT_mask;
165 #endif /* ! defined (SIGIO) && defined (FIONREAD) */
166
167 /* The id of a bitmap used for icon windows.
168 One such map is shared by all Emacs icon windows.
169 This is zero if we have not yet had a need to create the bitmap. */
170
171 static Bitmap icon_bitmap;
172
173 /* Font used for text icons. */
174
175 static FONT_TYPE *icon_font_info;
176
177 /* Stuff for dealing with the main icon title. */
178
179 extern Lisp_Object Vcommand_line_args;
180 char *hostname, *x_id_name;
181
182 /* Initial values of argv and argc. */
183 extern char **initial_argv;
184 extern int initial_argc;
185
186 /* This is the X connection that we are using. */
187
188 Display *x_current_display;
189
190 /* The cursor to use for vertical scroll bars on x_current_display. */
191 static Cursor x_vertical_scroll_bar_cursor;
192
193 /* Frame being updated by update_frame. This is declared in term.c.
194 This is set by update_begin and looked at by all the
195 XT functions. It is zero while not inside an update.
196 In that case, the XT functions assume that `selected_frame'
197 is the frame to apply to. */
198 extern struct frame *updating_frame;
199
200 /* The frame (if any) which has the X window that has keyboard focus.
201 Zero if none. This is examined by Ffocus_frame in frame.c. Note
202 that a mere EnterNotify event can set this; if you need to know the
203 last frame specified in a FocusIn or FocusOut event, use
204 x_focus_event_frame. */
205 struct frame *x_focus_frame;
206
207 /* This is a frame waiting to be autoraised, within XTread_socket. */
208 struct frame *pending_autoraise_frame;
209
210 /* The last frame mentioned in a FocusIn or FocusOut event. This is
211 separate from x_focus_frame, because whether or not LeaveNotify
212 events cause us to lose focus depends on whether or not we have
213 received a FocusIn event for it. */
214 struct frame *x_focus_event_frame;
215
216 /* The frame which currently has the visual highlight, and should get
217 keyboard input (other sorts of input have the frame encoded in the
218 event). It points to the X focus frame's selected window's
219 frame. It differs from x_focus_frame when we're using a global
220 minibuffer. */
221 static struct frame *x_highlight_frame;
222
223 /* From .Xdefaults, the value of "emacs.WarpMouse". If non-zero,
224 mouse is moved to inside of frame when frame is de-iconified. */
225
226 static int warp_mouse_on_deiconify;
227
228 /* During an update, maximum vpos for ins/del line operations to affect. */
229
230 static int flexlines;
231
232 /* During an update, nonzero if chars output now should be highlighted. */
233
234 static int highlight;
235
236 /* Nominal cursor position -- where to draw output.
237 During an update, these are different from the cursor-box position. */
238
239 static int curs_x;
240 static int curs_y;
241
242 /* Mouse movement.
243
244 In order to avoid asking for motion events and then throwing most
245 of them away or busy-polling the server for mouse positions, we ask
246 the server for pointer motion hints. This means that we get only
247 one event per group of mouse movements. "Groups" are delimited by
248 other kinds of events (focus changes and button clicks, for
249 example), or by XQueryPointer calls; when one of these happens, we
250 get another MotionNotify event the next time the mouse moves. This
251 is at least as efficient as getting motion events when mouse
252 tracking is on, and I suspect only negligibly worse when tracking
253 is off.
254
255 The silly O'Reilly & Associates Nutshell guides barely document
256 pointer motion hints at all (I think you have to infer how they
257 work from an example), and the description of XQueryPointer doesn't
258 mention that calling it causes you to get another motion hint from
259 the server, which is very important. */
260
261 /* Where the mouse was last time we reported a mouse event. */
262 static FRAME_PTR last_mouse_frame;
263 static XRectangle last_mouse_glyph;
264
265 /* The scroll bar in which the last X motion event occurred.
266
267 If the last X motion event occurred in a scroll bar, we set this
268 so XTmouse_position can know whether to report a scroll bar motion or
269 an ordinary motion.
270
271 If the last X motion event didn't occur in a scroll bar, we set this
272 to Qnil, to tell XTmouse_position to return an ordinary motion event. */
273 static Lisp_Object last_mouse_scroll_bar;
274
275 /* Record which buttons are currently pressed. */
276 unsigned int x_mouse_grabbed;
277
278 /* This is a hack. We would really prefer that XTmouse_position would
279 return the time associated with the position it returns, but there
280 doesn't seem to be any way to wrest the timestamp from the server
281 along with the position query. So, we just keep track of the time
282 of the last movement we received, and return that in hopes that
283 it's somewhat accurate. */
284 static Time last_mouse_movement_time;
285
286 /* These variables describe the range of text currently shown
287 in its mouse-face, together with the window they apply to.
288 As long as the mouse stays within this range, we need not
289 redraw anything on its account. */
290 static int mouse_face_beg_row, mouse_face_beg_col;
291 static int mouse_face_end_row, mouse_face_end_col;
292 static int mouse_face_past_end;
293 static Lisp_Object mouse_face_window;
294 static int mouse_face_face_id;
295
296 /* 1 if a mouse motion event came and we didn't handle it right away because
297 gc was in progress. */
298 static int mouse_face_deferred_gc;
299
300 /* FRAME and X, Y position of mouse when last checked for
301 highlighting. X and Y can be negative or out of range for the frame. */
302 static FRAME_PTR mouse_face_mouse_frame;
303 static int mouse_face_mouse_x, mouse_face_mouse_y;
304
305 /* Nonzero means defer mouse-motion highlighting. */
306 static int mouse_face_defer;
307
308 #ifdef HAVE_X11
309 /* `t' if a mouse button is depressed. */
310
311 extern Lisp_Object Vmouse_depressed;
312
313 /* Tells if a window manager is present or not. */
314
315 extern Lisp_Object Vx_no_window_manager;
316
317 /* Timestamp that we requested selection data was made. */
318 extern Time requestor_time;
319
320 /* ID of the window requesting selection data. */
321 extern Window requestor_window;
322
323 /* Nonzero enables some debugging for the X interface code. */
324 extern int _Xdebug;
325
326 extern Lisp_Object Qface, Qmouse_face;
327
328 #else /* ! defined (HAVE_X11) */
329
330 /* Bit patterns for the mouse cursor. */
331
332 short MouseCursor[] = {
333 0x0000, 0x0008, 0x0018, 0x0038,
334 0x0078, 0x00f8, 0x01f8, 0x03f8,
335 0x07f8, 0x00f8, 0x00d8, 0x0188,
336 0x0180, 0x0300, 0x0300, 0x0000};
337
338 short MouseMask[] = {
339 0x000c, 0x001c, 0x003c, 0x007c,
340 0x00fc, 0x01fc, 0x03fc, 0x07fc,
341 0x0ffc, 0x0ffc, 0x01fc, 0x03dc,
342 0x03cc, 0x0780, 0x0780, 0x0300};
343
344 static short grey_bits[] = {
345 0x0005, 0x000a, 0x0005, 0x000a};
346
347 static Pixmap GreyPixmap = 0;
348 #endif /* ! defined (HAVE_X11) */
349
350 /* From time to time we get info on an Emacs window, here. */
351
352 static WINDOWINFO_TYPE windowinfo;
353
354 extern int errno;
355
356 /* A mask of extra modifier bits to put into every keyboard char. */
357 extern int extra_keyboard_modifiers;
358
359 extern Display *XOpenDisplay ();
360 extern Window XCreateWindow ();
361
362 extern Cursor XCreateCursor ();
363 extern FONT_TYPE *XOpenFont ();
364
365 static void flashback ();
366 static void redraw_previous_char ();
367 static void redraw_following_char ();
368 static unsigned int x_x_to_emacs_modifiers ();
369
370 static int fast_find_position ();
371 static void note_mouse_highlight ();
372 static void clear_mouse_face ();
373 static void show_mouse_face ();
374
375 #ifndef HAVE_X11
376 static void dumpqueue ();
377 #endif /* HAVE_X11 */
378
379 void dumpborder ();
380 static int XTcursor_to ();
381 static int XTclear_end_of_line ();
382
383 \f
384 /* Starting and ending updates.
385
386 These hooks are called by update_frame at the beginning and end
387 of a frame update. We record in `updating_frame' the identity
388 of the frame being updated, so that the XT... functions do not
389 need to take a frame as argument. Most of the XT... functions
390 should never be called except during an update, the only exceptions
391 being XTcursor_to, XTwrite_glyphs and XTreassert_line_highlight. */
392
393 extern int mouse_track_top, mouse_track_left, mouse_track_width;
394
395 static
396 XTupdate_begin (f)
397 struct frame *f;
398 {
399 int mask;
400
401 if (f == 0)
402 abort ();
403
404 flexlines = f->height;
405 highlight = 0;
406
407 BLOCK_INPUT;
408
409 if (f == mouse_face_mouse_frame)
410 {
411 /* Don't do highlighting for mouse motion during the update. */
412 mouse_face_defer = 1;
413 if (!NILP (mouse_face_window))
414 {
415 int firstline, lastline, i;
416 struct window *w = XWINDOW (mouse_face_window);
417
418 /* Find the first, and the last+1, lines affected by redisplay. */
419 for (firstline = 0; firstline < f->height; firstline++)
420 if (FRAME_DESIRED_GLYPHS (f)->enable[firstline])
421 break;
422
423 lastline = f->height;
424 for (i = f->height - 1; i >= 0; i--)
425 {
426 if (FRAME_DESIRED_GLYPHS (f)->enable[i])
427 break;
428 else
429 lastline = i;
430 }
431
432 /* Can we tell that this update does not affect the window
433 where the mouse highlight is? If so, no need to turn off. */
434 if (! (firstline > (XFASTINT (w->top) + window_internal_height (w))
435 || lastline < XFASTINT (w->top)))
436 /* Otherwise turn off the mouse highlight now. */
437 clear_mouse_face ();
438 }
439 }
440 #ifndef HAVE_X11
441 dumpqueue ();
442 #endif /* HAVE_X11 */
443 UNBLOCK_INPUT;
444 }
445
446 #ifndef HAVE_X11
447 static void x_do_pending_expose ();
448 #endif
449
450 static
451 XTupdate_end (f)
452 struct frame *f;
453 {
454 int mask;
455
456 BLOCK_INPUT;
457 #ifndef HAVE_X11
458 dumpqueue ();
459 x_do_pending_expose ();
460 #endif /* HAVE_X11 */
461
462 x_display_cursor (f, 1);
463
464 if (f == mouse_face_mouse_frame)
465 mouse_face_defer = 0;
466 #if 0
467 /* This fails in the case of having updated only the echo area
468 if we have switched buffers. In that case, FRAME_CURRENT_GLYPHS
469 has no relation to the current contents, and its charstarts
470 have no relation to the contents of the window-buffer.
471 I don't know a clean way to check
472 for that case. window_end_valid isn't set up yet. */
473 if (f == mouse_face_mouse_frame)
474 note_mouse_highlight (f, mouse_face_mouse_x, mouse_face_mouse_y);
475 #endif
476
477 XFlushQueue ();
478 UNBLOCK_INPUT;
479 }
480
481 /* This is called after a redisplay on frame F. */
482
483 static
484 XTframe_up_to_date (f)
485 FRAME_PTR f;
486 {
487 if (mouse_face_deferred_gc || f == mouse_face_mouse_frame)
488 {
489 note_mouse_highlight (mouse_face_mouse_frame,
490 mouse_face_mouse_x, mouse_face_mouse_y);
491 mouse_face_deferred_gc = 0;
492 }
493 }
494 \f
495 /* External interface to control of standout mode.
496 Call this when about to modify line at position VPOS
497 and not change whether it is highlighted. */
498
499 XTreassert_line_highlight (new, vpos)
500 int new, vpos;
501 {
502 highlight = new;
503 }
504
505 /* Call this when about to modify line at position VPOS
506 and change whether it is highlighted. */
507
508 static
509 XTchange_line_highlight (new_highlight, vpos, first_unused_hpos)
510 int new_highlight, vpos, first_unused_hpos;
511 {
512 highlight = new_highlight;
513 XTcursor_to (vpos, 0);
514 XTclear_end_of_line (updating_frame->width);
515 }
516
517 /* This is used when starting Emacs and when restarting after suspend.
518 When starting Emacs, no X window is mapped. And nothing must be done
519 to Emacs's own window if it is suspended (though that rarely happens). */
520
521 static
522 XTset_terminal_modes ()
523 {
524 }
525
526 /* This is called when exiting or suspending Emacs.
527 Exiting will make the X-windows go away, and suspending
528 requires no action. */
529
530 static
531 XTreset_terminal_modes ()
532 {
533 /* XTclear_frame (); */
534 }
535 \f
536 /* Set the nominal cursor position of the frame.
537 This is where display update commands will take effect.
538 This does not affect the place where the cursor-box is displayed. */
539
540 static int
541 XTcursor_to (row, col)
542 register int row, col;
543 {
544 int mask;
545 int orow = row;
546
547 curs_x = col;
548 curs_y = row;
549
550 if (updating_frame == 0)
551 {
552 BLOCK_INPUT;
553 x_display_cursor (selected_frame, 1);
554 XFlushQueue ();
555 UNBLOCK_INPUT;
556 }
557 }
558 \f
559 /* Display a sequence of N glyphs found at GP.
560 WINDOW is the x-window to output to. LEFT and TOP are starting coords.
561 HL is 1 if this text is highlighted, 2 if the cursor is on it,
562 3 if should appear in its mouse-face.
563 JUST_FOREGROUND if 1 means draw only the foreground;
564 don't alter the background.
565
566 FONT is the default font to use (for glyphs whose font-code is 0).
567
568 Since the display generation code is responsible for calling
569 compute_char_face and compute_glyph_face on everything it puts in
570 the display structure, we can assume that the face code on each
571 glyph is a valid index into FRAME_COMPUTED_FACES (f), and the one
572 to which we can actually apply intern_face.
573 Call this function with input blocked. */
574
575 #if 1
576 /* This is the multi-face code. */
577
578 static void
579 dumpglyphs (f, left, top, gp, n, hl, just_foreground)
580 struct frame *f;
581 int left, top;
582 register GLYPH *gp; /* Points to first GLYPH. */
583 register int n; /* Number of glyphs to display. */
584 int hl;
585 int just_foreground;
586 {
587 /* Holds characters to be displayed. */
588 char *buf = (char *) alloca (f->width * sizeof (*buf));
589 register char *cp; /* Steps through buf[]. */
590 register int tlen = GLYPH_TABLE_LENGTH;
591 register Lisp_Object *tbase = GLYPH_TABLE_BASE;
592 Window window = FRAME_X_WINDOW (f);
593 int orig_left = left;
594
595 while (n > 0)
596 {
597 /* Get the face-code of the next GLYPH. */
598 int cf, len;
599 int g = *gp;
600
601 GLYPH_FOLLOW_ALIASES (tbase, tlen, g);
602 cf = FAST_GLYPH_FACE (g);
603
604 /* Find the run of consecutive glyphs with the same face-code.
605 Extract their character codes into BUF. */
606 cp = buf;
607 while (n > 0)
608 {
609 g = *gp;
610 GLYPH_FOLLOW_ALIASES (tbase, tlen, g);
611 if (FAST_GLYPH_FACE (g) != cf)
612 break;
613
614 *cp++ = FAST_GLYPH_CHAR (g);
615 --n;
616 ++gp;
617 }
618
619 /* LEN gets the length of the run. */
620 len = cp - buf;
621
622 /* Now output this run of chars, with the font and pixel values
623 determined by the face code CF. */
624 {
625 struct face *face = FRAME_DEFAULT_FACE (f);
626 FONT_TYPE *font = FACE_FONT (face);
627 GC gc = FACE_GC (face);
628 int gc_temporary = 0;
629
630 /* HL = 3 means use a mouse face previously chosen. */
631 if (hl == 3)
632 cf = mouse_face_face_id;
633
634 /* First look at the face of the text itself. */
635 if (cf != 0)
636 {
637 /* It's possible for the display table to specify
638 a face code that is out of range. Use 0 in that case. */
639 if (cf < 0 || cf >= FRAME_N_COMPUTED_FACES (f)
640 || FRAME_COMPUTED_FACES (f) [cf] == 0)
641 cf = 0;
642
643 if (cf == 1)
644 face = FRAME_MODE_LINE_FACE (f);
645 else
646 face = intern_face (f, FRAME_COMPUTED_FACES (f) [cf]);
647 font = FACE_FONT (face);
648 gc = FACE_GC (face);
649 }
650
651 /* Then comes the distinction between modeline and normal text. */
652 else if (hl == 0)
653 ;
654 else if (hl == 1)
655 {
656 face = FRAME_MODE_LINE_FACE (f);
657 font = FACE_FONT (face);
658 gc = FACE_GC (face);
659 }
660
661 #define FACE_DEFAULT (~0)
662
663 /* Now override that if the cursor's on this character. */
664 if (hl == 2)
665 {
666 if (!face->font
667 || (int) face->font == FACE_DEFAULT)
668 {
669 gc = f->display.x->cursor_gc;
670 }
671 /* Cursor on non-default face: must merge. */
672 else
673 {
674 XGCValues xgcv;
675 unsigned long mask;
676
677 xgcv.background = f->display.x->cursor_pixel;
678 if (face == FRAME_DEFAULT_FACE (f))
679 xgcv.foreground = f->display.x->cursor_foreground_pixel;
680 else
681 xgcv.foreground = face->background;
682 /* If the glyph would be invisible,
683 try a different foreground. */
684 if (xgcv.foreground == xgcv.background)
685 xgcv.foreground = face->foreground;
686 if (xgcv.foreground == xgcv.background)
687 xgcv.foreground = f->display.x->cursor_foreground_pixel;
688 if (xgcv.foreground == xgcv.background)
689 xgcv.foreground = face->foreground;
690 /* Make sure the cursor is distinct from text in this face. */
691 if (xgcv.background == face->background
692 && xgcv.foreground == face->foreground)
693 {
694 xgcv.background = face->foreground;
695 xgcv.foreground = face->background;
696 }
697 xgcv.font = face->font->fid;
698 xgcv.graphics_exposures = 0;
699 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
700 gc = XCreateGC (x_current_display, FRAME_X_WINDOW (f),
701 mask, &xgcv);
702 #if 0
703 if (face->stipple && face->stipple != FACE_DEFAULT)
704 XSetStipple (x_current_display, gc, face->stipple);
705 #endif
706 gc_temporary = 1;
707 }
708 }
709
710 if ((int) font == FACE_DEFAULT)
711 font = f->display.x->font;
712
713 if (just_foreground)
714 XDrawString (x_current_display, window, gc,
715 left, top + FONT_BASE (font), buf, len);
716 else
717 {
718 XDrawImageString (x_current_display, window, gc,
719 left, top + FONT_BASE (font), buf, len);
720 /* Clear the rest of the line's height. */
721 if (f->display.x->line_height != FONT_HEIGHT (font))
722 XClearArea (x_current_display, window, left,
723 top + FONT_HEIGHT (font),
724 FONT_WIDTH (font) * len,
725 /* This is how many pixels of height
726 we have to clear. */
727 f->display.x->line_height - FONT_HEIGHT (font),
728 False);
729 }
730
731 #if 0 /* Doesn't work, because it uses FRAME_CURRENT_GLYPHS,
732 which often is not up to date yet. */
733 if (!just_foreground)
734 {
735 if (left == orig_left)
736 redraw_previous_char (f, PIXEL_TO_CHAR_COL (f, left),
737 PIXEL_TO_CHAR_ROW (f, top), hl == 1);
738 if (n == 0)
739 redraw_following_char (f, PIXEL_TO_CHAR_COL (f, left + len * FONT_WIDTH (font)),
740 PIXEL_TO_CHAR_ROW (f, top), hl == 1);
741 }
742 #endif
743
744 if (gc_temporary)
745 XFreeGC (x_current_display, gc);
746
747 /* We should probably check for XA_UNDERLINE_POSITION and
748 XA_UNDERLINE_THICKNESS properties on the font, but let's
749 just get the thing working, and come back to that. */
750 {
751 int underline_position = 1;
752
753 if (font->descent <= underline_position)
754 underline_position = font->descent - 1;
755
756 if (face->underline)
757 XFillRectangle (x_current_display, FRAME_X_WINDOW (f),
758 FACE_GC (face),
759 left, (top
760 + FONT_BASE (font)
761 + underline_position),
762 len * FONT_WIDTH (font), 1);
763 }
764
765 left += len * FONT_WIDTH (font);
766 }
767 }
768 }
769 #endif /* 1 */
770
771 #if 0
772 /* This is the old single-face code. */
773
774 static void
775 dumpglyphs (f, left, top, gp, n, hl, font)
776 struct frame *f;
777 int left, top;
778 register GLYPH *gp; /* Points to first GLYPH. */
779 register int n; /* Number of glyphs to display. */
780 int hl;
781 FONT_TYPE *font;
782 {
783 register int len;
784 Window window = FRAME_X_WINDOW (f);
785 GC drawing_gc = (hl == 2 ? f->display.x->cursor_gc
786 : (hl ? f->display.x->reverse_gc
787 : f->display.x->normal_gc));
788
789 if (sizeof (GLYPH) == sizeof (XChar2b))
790 XDrawImageString16 (x_current_display, window, drawing_gc,
791 left, top + FONT_BASE (font), (XChar2b *) gp, n);
792 else if (sizeof (GLYPH) == sizeof (unsigned char))
793 XDrawImageString (x_current_display, window, drawing_gc,
794 left, top + FONT_BASE (font), (char *) gp, n);
795 else
796 /* What size of glyph ARE you using? And does X have a function to
797 draw them? */
798 abort ();
799 }
800 #endif
801 \f
802 /* Output some text at the nominal frame cursor position.
803 Advance the cursor over the text.
804 Output LEN glyphs at START.
805
806 `highlight', set up by XTreassert_line_highlight or XTchange_line_highlight,
807 controls the pixel values used for foreground and background. */
808
809 static
810 XTwrite_glyphs (start, len)
811 register GLYPH *start;
812 int len;
813 {
814 register int temp_length;
815 int mask;
816 struct frame *f;
817
818 BLOCK_INPUT;
819
820 f = updating_frame;
821 if (f == 0)
822 {
823 f = selected_frame;
824 /* If not within an update,
825 output at the frame's visible cursor. */
826 curs_x = f->cursor_x;
827 curs_y = f->cursor_y;
828 }
829
830 dumpglyphs (f,
831 CHAR_TO_PIXEL_COL (f, curs_x),
832 CHAR_TO_PIXEL_ROW (f, curs_y),
833 start, len, highlight, 0);
834
835 /* If we drew on top of the cursor, note that it is turned off. */
836 if (curs_y == f->phys_cursor_y
837 && curs_x <= f->phys_cursor_x
838 && curs_x + len > f->phys_cursor_x)
839 f->phys_cursor_x = -1;
840
841 if (updating_frame == 0)
842 {
843 f->cursor_x += len;
844 x_display_cursor (f, 1);
845 f->cursor_x -= len;
846 }
847 else
848 curs_x += len;
849
850 UNBLOCK_INPUT;
851 }
852 \f
853 /* Clear to the end of the line.
854 Erase the current text line from the nominal cursor position (inclusive)
855 to column FIRST_UNUSED (exclusive). The idea is that everything
856 from FIRST_UNUSED onward is already erased. */
857
858 static int
859 XTclear_end_of_line (first_unused)
860 register int first_unused;
861 {
862 struct frame *f = updating_frame;
863 int mask;
864
865 if (f == 0)
866 abort ();
867
868 if (curs_y < 0 || curs_y >= f->height)
869 return;
870 if (first_unused <= 0)
871 return;
872
873 if (first_unused >= f->width)
874 first_unused = f->width;
875
876 BLOCK_INPUT;
877
878 /* Notice if the cursor will be cleared by this operation. */
879 if (curs_y == f->phys_cursor_y
880 && curs_x <= f->phys_cursor_x
881 && f->phys_cursor_x < first_unused)
882 f->phys_cursor_x = -1;
883
884 #ifdef HAVE_X11
885 XClearArea (x_current_display, FRAME_X_WINDOW (f),
886 CHAR_TO_PIXEL_COL (f, curs_x),
887 CHAR_TO_PIXEL_ROW (f, curs_y),
888 FONT_WIDTH (f->display.x->font) * (first_unused - curs_x),
889 f->display.x->line_height, False);
890 #if 0
891 redraw_previous_char (f, curs_x, curs_y, highlight);
892 #endif
893 #else /* ! defined (HAVE_X11) */
894 XPixSet (FRAME_X_WINDOW (f),
895 CHAR_TO_PIXEL_COL (f, curs_x),
896 CHAR_TO_PIXEL_ROW (f, curs_y),
897 FONT_WIDTH (f->display.x->font) * (first_unused - curs_x),
898 f->display.x->line_height,
899 f->display.x->background_pixel);
900 #endif /* ! defined (HAVE_X11) */
901
902 UNBLOCK_INPUT;
903 }
904
905 static
906 XTclear_frame ()
907 {
908 int mask;
909 struct frame *f = updating_frame;
910
911 if (f == 0)
912 f = selected_frame;
913
914 f->phys_cursor_x = -1; /* Cursor not visible. */
915 curs_x = 0; /* Nominal cursor position is top left. */
916 curs_y = 0;
917
918 BLOCK_INPUT;
919
920 XClear (FRAME_X_WINDOW (f));
921
922 /* We have to clear the scroll bars, too. If we have changed
923 colors or something like that, then they should be notified. */
924 x_scroll_bar_clear (f);
925
926 #ifndef HAVE_X11
927 dumpborder (f, 0);
928 #endif /* HAVE_X11 */
929
930 XFlushQueue ();
931 UNBLOCK_INPUT;
932 }
933 \f
934 #if 0
935 /* This currently does not work because FRAME_CURRENT_GLYPHS doesn't
936 always contain the right glyphs to use.
937
938 It also needs to be changed to look at the details of the font and
939 see whether there is really overlap, and do nothing when there is
940 not. This can use font_char_overlap_left and font_char_overlap_right,
941 but just how to use them is not clear. */
942
943 /* Erase the character (if any) at the position just before X, Y in frame F,
944 then redraw it and the character before it.
945 This is necessary when we erase starting at X,
946 in case the character after X overlaps into the one before X.
947 Call this function with input blocked. */
948
949 static void
950 redraw_previous_char (f, x, y, highlight_flag)
951 FRAME_PTR f;
952 int x, y;
953 int highlight_flag;
954 {
955 /* Erase the character before the new ones, in case
956 what was here before overlaps it.
957 Reoutput that character, and the previous character
958 (in case the previous character overlaps it). */
959 if (x > 0)
960 {
961 int start_x = x - 2;
962 if (start_x < 0)
963 start_x = 0;
964 XClearArea (x_current_display, FRAME_X_WINDOW (f),
965 CHAR_TO_PIXEL_COL (f, x - 1),
966 CHAR_TO_PIXEL_ROW (f, y),
967 FONT_WIDTH (f->display.x->font),
968 f->display.x->line_height, False);
969
970 dumpglyphs (f, CHAR_TO_PIXEL_COL (f, start_x),
971 CHAR_TO_PIXEL_ROW (f, y),
972 &FRAME_CURRENT_GLYPHS (f)->glyphs[y][start_x],
973 x - start_x, highlight_flag, 1);
974 }
975 }
976
977 /* Erase the character (if any) at the position X, Y in frame F,
978 then redraw it and the character after it.
979 This is necessary when we erase endng at X,
980 in case the character after X overlaps into the one before X.
981 Call this function with input blocked. */
982
983 static void
984 redraw_following_char (f, x, y, highlight_flag)
985 FRAME_PTR f;
986 int x, y;
987 int highlight_flag;
988 {
989 int limit = FRAME_CURRENT_GLYPHS (f)->used[y];
990 /* Erase the character after the new ones, in case
991 what was here before overlaps it.
992 Reoutput that character, and the following character
993 (in case the following character overlaps it). */
994 if (x < limit
995 && FRAME_CURRENT_GLYPHS (f)->glyphs[y][x] != SPACEGLYPH)
996 {
997 int end_x = x + 2;
998 if (end_x > limit)
999 end_x = limit;
1000 XClearArea (x_current_display, FRAME_X_WINDOW (f),
1001 CHAR_TO_PIXEL_COL (f, x),
1002 CHAR_TO_PIXEL_ROW (f, y),
1003 FONT_WIDTH (f->display.x->font),
1004 f->display.x->line_height, False);
1005
1006 dumpglyphs (f, CHAR_TO_PIXEL_COL (f, x),
1007 CHAR_TO_PIXEL_ROW (f, y),
1008 &FRAME_CURRENT_GLYPHS (f)->glyphs[y][x],
1009 end_x - x, highlight_flag, 1);
1010 }
1011 }
1012 #endif /* 0 */
1013 \f
1014 #if 0 /* Not in use yet */
1015
1016 /* Return 1 if character C in font F extends past its left edge. */
1017
1018 static int
1019 font_char_overlap_left (font, c)
1020 XFontStruct *font;
1021 int c;
1022 {
1023 XCharStruct *s;
1024
1025 /* Find the bounding-box info for C. */
1026 if (font->per_char == 0)
1027 s = &font->max_bounds;
1028 else
1029 {
1030 int rowlen = font->max_char_or_byte2 - font->min_char_or_byte2 + 1;
1031 int row, within;
1032
1033 /* Decode char into row number (byte 1) and code within row (byte 2). */
1034 row = c >> 8;
1035 within = c & 0177;
1036 if (!(within >= font->min_char_or_byte2
1037 && within <= font->max_char_or_byte2
1038 && row >= font->min_byte1
1039 && row <= font->max_byte1))
1040 {
1041 /* If char is out of range, try the font's default char instead. */
1042 c = font->default_char;
1043 row = c >> (INTBITS - 8);
1044 within = c & 0177;
1045 }
1046 if (!(within >= font->min_char_or_byte2
1047 && within <= font->max_char_or_byte2
1048 && row >= font->min_byte1
1049 && row <= font->max_byte1))
1050 /* Still out of range means this char does not overlap. */
1051 return 0;
1052 else
1053 /* We found the info for this char. */
1054 s = (font->per_char + (within - font->min_char_or_byte2)
1055 + row * rowlen);
1056 }
1057
1058 return (s && s->lbearing < 0);
1059 }
1060
1061 /* Return 1 if character C in font F extends past its right edge. */
1062
1063 static int
1064 font_char_overlap_right (font, c)
1065 XFontStruct *font;
1066 int c;
1067 {
1068 XCharStruct *s;
1069
1070 /* Find the bounding-box info for C. */
1071 if (font->per_char == 0)
1072 s = &font->max_bounds;
1073 else
1074 {
1075 int rowlen = font->max_char_or_byte2 - font->min_char_or_byte2 + 1;
1076 int row, within;
1077
1078 /* Decode char into row number (byte 1) and code within row (byte 2). */
1079 row = c >> 8;
1080 within = c & 0177;
1081 if (!(within >= font->min_char_or_byte2
1082 && within <= font->max_char_or_byte2
1083 && row >= font->min_byte1
1084 && row <= font->max_byte1))
1085 {
1086 /* If char is out of range, try the font's default char instead. */
1087 c = font->default_char;
1088 row = c >> (INTBITS - 8);
1089 within = c & 0177;
1090 }
1091 if (!(within >= font->min_char_or_byte2
1092 && within <= font->max_char_or_byte2
1093 && row >= font->min_byte1
1094 && row <= font->max_byte1))
1095 /* Still out of range means this char does not overlap. */
1096 return 0;
1097 else
1098 /* We found the info for this char. */
1099 s = (font->per_char + (within - font->min_char_or_byte2)
1100 + row * rowlen);
1101 }
1102
1103 return (s && s->rbearing >= s->width);
1104 }
1105 #endif /* 0 */
1106 \f
1107 /* Invert the middle quarter of the frame for .15 sec. */
1108
1109 /* We use the select system call to do the waiting, so we have to make sure
1110 it's available. If it isn't, we just won't do visual bells. */
1111 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
1112
1113 /* Subtract the `struct timeval' values X and Y,
1114 storing the result in RESULT.
1115 Return 1 if the difference is negative, otherwise 0. */
1116
1117 static int
1118 timeval_subtract (result, x, y)
1119 struct timeval *result, x, y;
1120 {
1121 /* Perform the carry for the later subtraction by updating y.
1122 This is safer because on some systems
1123 the tv_sec member is unsigned. */
1124 if (x.tv_usec < y.tv_usec)
1125 {
1126 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
1127 y.tv_usec -= 1000000 * nsec;
1128 y.tv_sec += nsec;
1129 }
1130 if (x.tv_usec - y.tv_usec > 1000000)
1131 {
1132 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
1133 y.tv_usec += 1000000 * nsec;
1134 y.tv_sec -= nsec;
1135 }
1136
1137 /* Compute the time remaining to wait. tv_usec is certainly positive. */
1138 result->tv_sec = x.tv_sec - y.tv_sec;
1139 result->tv_usec = x.tv_usec - y.tv_usec;
1140
1141 /* Return indication of whether the result should be considered negative. */
1142 return x.tv_sec < y.tv_sec;
1143 }
1144
1145 XTflash (f)
1146 struct frame *f;
1147 {
1148 BLOCK_INPUT;
1149
1150 {
1151 GC gc;
1152
1153 /* Create a GC that will use the GXxor function to flip foreground pixels
1154 into background pixels. */
1155 {
1156 XGCValues values;
1157
1158 values.function = GXxor;
1159 values.foreground = (f->display.x->foreground_pixel
1160 ^ f->display.x->background_pixel);
1161
1162 gc = XCreateGC (x_current_display, FRAME_X_WINDOW (f),
1163 GCFunction | GCForeground, &values);
1164 }
1165
1166 {
1167 int width = PIXEL_WIDTH (f);
1168 int height = PIXEL_HEIGHT (f);
1169
1170 XFillRectangle (x_current_display, FRAME_X_WINDOW (f), gc,
1171 width/4, height/4, width/2, height/2);
1172 XFlush (x_current_display);
1173
1174 {
1175 struct timeval wakeup, now;
1176
1177 EMACS_GET_TIME (wakeup);
1178
1179 /* Compute time to wait until, propagating carry from usecs. */
1180 wakeup.tv_usec += 150000;
1181 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
1182 wakeup.tv_usec %= 1000000;
1183
1184 /* Keep waiting until past the time wakeup. */
1185 while (1)
1186 {
1187 struct timeval timeout;
1188
1189 EMACS_GET_TIME (timeout);
1190
1191 /* In effect, timeout = wakeup - timeout.
1192 Break if result would be negative. */
1193 if (timeval_subtract (&timeout, wakeup, timeout))
1194 break;
1195
1196 /* Try to wait that long--but we might wake up sooner. */
1197 select (0, 0, 0, 0, &timeout);
1198 }
1199 }
1200
1201 XFillRectangle (x_current_display, FRAME_X_WINDOW (f), gc,
1202 width/4, height/4, width/2, height/2);
1203 XFreeGC (x_current_display, gc);
1204 XFlush (x_current_display);
1205 }
1206 }
1207
1208 UNBLOCK_INPUT;
1209 }
1210
1211 #endif
1212
1213
1214 /* Make audible bell. */
1215
1216 #ifdef HAVE_X11
1217 #define XRINGBELL XBell (x_current_display, 0)
1218 #else /* ! defined (HAVE_X11) */
1219 #define XRINGBELL XFeep (0);
1220 #endif /* ! defined (HAVE_X11) */
1221
1222 XTring_bell ()
1223 {
1224 if (x_current_display == 0)
1225 return;
1226
1227 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
1228 if (visible_bell)
1229 XTflash (selected_frame);
1230 else
1231 #endif
1232 {
1233 BLOCK_INPUT;
1234 XRINGBELL;
1235 XFlushQueue ();
1236 UNBLOCK_INPUT;
1237 }
1238 }
1239 \f
1240 /* Insert and delete character.
1241 These are not supposed to be used because we are supposed to turn
1242 off the feature of using them. */
1243
1244 static
1245 XTinsert_glyphs (start, len)
1246 register char *start;
1247 register int len;
1248 {
1249 abort ();
1250 }
1251
1252 static
1253 XTdelete_glyphs (n)
1254 register int n;
1255 {
1256 abort ();
1257 }
1258 \f
1259 /* Specify how many text lines, from the top of the window,
1260 should be affected by insert-lines and delete-lines operations.
1261 This, and those operations, are used only within an update
1262 that is bounded by calls to XTupdate_begin and XTupdate_end. */
1263
1264 static
1265 XTset_terminal_window (n)
1266 register int n;
1267 {
1268 if (updating_frame == 0)
1269 abort ();
1270
1271 if ((n <= 0) || (n > updating_frame->height))
1272 flexlines = updating_frame->height;
1273 else
1274 flexlines = n;
1275 }
1276 \f
1277 /* Perform an insert-lines operation.
1278 Insert N lines at a vertical position curs_y. */
1279
1280 static void
1281 stufflines (n)
1282 register int n;
1283 {
1284 register int topregion, bottomregion;
1285 register int length, newtop, mask;
1286 register struct frame *f = updating_frame;
1287 int intborder = f->display.x->internal_border_width;
1288
1289 if (curs_y >= flexlines)
1290 return;
1291
1292 topregion = curs_y;
1293 bottomregion = flexlines - (n + 1);
1294 newtop = topregion + n;
1295 length = (bottomregion - topregion) + 1;
1296
1297 #ifndef HAVE_X11
1298 dumpqueue ();
1299 #endif /* HAVE_X11 */
1300
1301 if ((length > 0) && (newtop <= flexlines))
1302 {
1303 #ifdef HAVE_X11
1304 XCopyArea (x_current_display, FRAME_X_WINDOW (f),
1305 FRAME_X_WINDOW (f), f->display.x->normal_gc,
1306 intborder, CHAR_TO_PIXEL_ROW (f, topregion),
1307 f->width * FONT_WIDTH (f->display.x->font),
1308 length * f->display.x->line_height, intborder,
1309 CHAR_TO_PIXEL_ROW (f, newtop));
1310 #else /* ! defined (HAVE_X11) */
1311 XMoveArea (FRAME_X_WINDOW (f),
1312 intborder, CHAR_TO_PIXEL_ROW (f, topregion),
1313 intborder, CHAR_TO_PIXEL_ROW (f, newtop),
1314 f->width * FONT_WIDTH (f->display.x->font),
1315 length * f->display.x->line_height);
1316 /* Now we must process any ExposeRegion events that occur
1317 if the area being copied from is obscured.
1318 We can't let it wait because further i/d operations
1319 may want to copy this area to another area. */
1320 x_read_exposes ();
1321 #endif /* ! defined (HAVE_X11) */
1322 }
1323
1324 newtop = min (newtop, (flexlines - 1));
1325 length = newtop - topregion;
1326 if (length > 0)
1327 {
1328 #ifdef HAVE_X11
1329 XClearArea (x_current_display, FRAME_X_WINDOW (f), intborder,
1330 CHAR_TO_PIXEL_ROW (f, topregion),
1331 f->width * FONT_WIDTH (f->display.x->font),
1332 n * f->display.x->line_height, False);
1333 #else /* ! defined (HAVE_X11) */
1334 XPixSet (FRAME_X_WINDOW (f),
1335 intborder,
1336 CHAR_TO_PIXEL_ROW (f, topregion),
1337 f->width * FONT_WIDTH (f->display.x->font),
1338 n * f->display.x->line_height,
1339 f->display.x->background_pixel);
1340 #endif /* ! defined (HAVE_X11) */
1341 }
1342 }
1343
1344 /* Perform a delete-lines operation, deleting N lines
1345 at a vertical position curs_y. */
1346
1347 static void
1348 scraplines (n)
1349 register int n;
1350 {
1351 int mask;
1352 register struct frame *f = updating_frame;
1353 int intborder = f->display.x->internal_border_width;
1354
1355 if (curs_y >= flexlines)
1356 return;
1357
1358 #ifndef HAVE_X11
1359 dumpqueue ();
1360 #endif /* HAVE_X11 */
1361
1362 if ((curs_y + n) >= flexlines)
1363 {
1364 if (flexlines >= (curs_y + 1))
1365 {
1366 #ifdef HAVE_X11
1367 XClearArea (x_current_display, FRAME_X_WINDOW (f), intborder,
1368 CHAR_TO_PIXEL_ROW (f, curs_y),
1369 f->width * FONT_WIDTH (f->display.x->font),
1370 (flexlines - curs_y) * f->display.x->line_height, False);
1371 #else /* ! defined (HAVE_X11) */
1372 XPixSet (FRAME_X_WINDOW (f),
1373 intborder, CHAR_TO_PIXEL_ROW (f, curs_y),
1374 f->width * FONT_WIDTH (f->display.x->font),
1375 (flexlines - curs_y) * f->display.x->line_height,
1376 f->display.x->background_pixel);
1377 #endif /* ! defined (HAVE_X11) */
1378 }
1379 }
1380 else
1381 {
1382 #ifdef HAVE_X11
1383 XCopyArea (x_current_display, FRAME_X_WINDOW (f),
1384 FRAME_X_WINDOW (f), f->display.x->normal_gc,
1385 intborder,
1386 CHAR_TO_PIXEL_ROW (f, curs_y + n),
1387 f->width * FONT_WIDTH (f->display.x->font),
1388 (flexlines - (curs_y + n)) * f->display.x->line_height,
1389 intborder, CHAR_TO_PIXEL_ROW (f, curs_y));
1390 XClearArea (x_current_display, FRAME_X_WINDOW (f),
1391 intborder,
1392 CHAR_TO_PIXEL_ROW (f, flexlines - n),
1393 f->width * FONT_WIDTH (f->display.x->font),
1394 n * f->display.x->line_height, False);
1395 #else /* ! defined (HAVE_X11) */
1396 XMoveArea (FRAME_X_WINDOW (f),
1397 intborder,
1398 CHAR_TO_PIXEL_ROW (f, curs_y + n),
1399 intborder, CHAR_TO_PIXEL_ROW (f, curs_y),
1400 f->width * FONT_WIDTH (f->display.x->font),
1401 (flexlines - (curs_y + n)) * f->display.x->line_height);
1402 /* Now we must process any ExposeRegion events that occur
1403 if the area being copied from is obscured.
1404 We can't let it wait because further i/d operations
1405 may want to copy this area to another area. */
1406 x_read_exposes ();
1407 XPixSet (FRAME_X_WINDOW (f), intborder,
1408 CHAR_TO_PIXEL_ROW (f, flexlines - n),
1409 f->width * FONT_WIDTH (f->display.x->font),
1410 n * f->display.x->line_height, f->display.x->background_pixel);
1411 #endif /* ! defined (HAVE_X11) */
1412 }
1413 }
1414
1415 /* Perform an insert-lines or delete-lines operation,
1416 inserting N lines or deleting -N lines at vertical position VPOS. */
1417
1418 XTins_del_lines (vpos, n)
1419 int vpos, n;
1420 {
1421 if (updating_frame == 0)
1422 abort ();
1423
1424 /* Hide the cursor. */
1425 x_display_cursor (updating_frame, 0);
1426
1427 XTcursor_to (vpos, 0);
1428
1429 BLOCK_INPUT;
1430 if (n >= 0)
1431 stufflines (n);
1432 else
1433 scraplines (-n);
1434 XFlushQueue ();
1435 UNBLOCK_INPUT;
1436 }
1437 \f
1438 /* Support routines for exposure events. */
1439 static void clear_cursor ();
1440
1441 /* Output into a rectangle of an X-window (for frame F)
1442 the characters in f->phys_lines that overlap that rectangle.
1443 TOP and LEFT are the position of the upper left corner of the rectangle.
1444 ROWS and COLS are the size of the rectangle.
1445 Call this function with input blocked. */
1446
1447 static void
1448 dumprectangle (f, left, top, cols, rows)
1449 struct frame *f;
1450 register int left, top, cols, rows;
1451 {
1452 register struct frame_glyphs *active_frame = FRAME_CURRENT_GLYPHS (f);
1453 int cursor_cleared = 0;
1454 int bottom, right;
1455 register int y;
1456
1457 if (FRAME_GARBAGED_P (f))
1458 return;
1459
1460 /* Express rectangle as four edges, instead of position-and-size. */
1461 bottom = top + rows;
1462 right = left + cols;
1463
1464 #ifndef HAVE_X11 /* Window manger does this for X11. */
1465 {
1466 int intborder = f->display.x->internal_border_width;
1467
1468 /* If the rectangle includes any of the internal border area,
1469 redisplay the border emphasis. */
1470 if (top < intborder || left < intborder
1471 || bottom > intborder + f->height * f->display.x->line_height
1472 || right > intborder + f->width * f->display.x->line_height)
1473 dumpborder (f, 0);
1474 }
1475 #endif /* not HAVE_X11 Window manger does this for X11. */
1476
1477 /* Convert rectangle edges in pixels to edges in chars.
1478 Round down for left and top, up for right and bottom. */
1479 top = PIXEL_TO_CHAR_ROW (f, top);
1480 left = PIXEL_TO_CHAR_COL (f, left);
1481 bottom += (f->display.x->line_height - 1);
1482 right += (FONT_WIDTH (f->display.x->font) - 1);
1483 bottom = PIXEL_TO_CHAR_ROW (f, bottom);
1484 right = PIXEL_TO_CHAR_COL (f, right);
1485
1486 /* Clip the rectangle to what can be visible. */
1487 if (left < 0)
1488 left = 0;
1489 if (top < 0)
1490 top = 0;
1491 if (right > f->width)
1492 right = f->width;
1493 if (bottom > f->height)
1494 bottom = f->height;
1495
1496 /* Get size in chars of the rectangle. */
1497 cols = right - left;
1498 rows = bottom - top;
1499
1500 /* If rectangle has zero area, return. */
1501 if (rows <= 0) return;
1502 if (cols <= 0) return;
1503
1504 /* Turn off the cursor if it is in the rectangle.
1505 We will turn it back on afterward. */
1506 if ((f->phys_cursor_x >= left) && (f->phys_cursor_x < right)
1507 && (f->phys_cursor_y >= top) && (f->phys_cursor_y < bottom))
1508 {
1509 clear_cursor (f);
1510 cursor_cleared = 1;
1511 }
1512
1513 /* Display the text in the rectangle, one text line at a time. */
1514
1515 for (y = top; y < bottom; y++)
1516 {
1517 GLYPH *line = &active_frame->glyphs[y][left];
1518
1519 if (! active_frame->enable[y] || left > active_frame->used[y])
1520 continue;
1521
1522 dumpglyphs (f,
1523 CHAR_TO_PIXEL_COL (f, left),
1524 CHAR_TO_PIXEL_ROW (f, y),
1525 line, min (cols, active_frame->used[y] - left),
1526 active_frame->highlight[y], 0);
1527 }
1528
1529 /* Turn the cursor on if we turned it off. */
1530
1531 if (cursor_cleared)
1532 x_display_cursor (f, 1);
1533 }
1534
1535 #ifndef HAVE_X11
1536 /* Process all queued ExposeRegion events. */
1537
1538 static void
1539 dumpqueue ()
1540 {
1541 register int i;
1542 XExposeRegionEvent r;
1543
1544 while (dequeue_event (&r, &x_expose_queue))
1545 {
1546 struct frame *f = x_window_to_frame (r.window);
1547 if (f->display.x->icon_desc == r.window)
1548 refreshicon (f);
1549 else
1550 dumprectangle (f, r.x, r.y, r.width, r.height);
1551 }
1552 XFlushQueue ();
1553 }
1554 #endif /* HAVE_X11 */
1555 \f
1556 /* Process all expose events that are pending, for X10.
1557 Redraws the cursor if necessary on any frame that
1558 is not in the process of being updated with update_frame. */
1559
1560 #ifndef HAVE_X11
1561 static void
1562 x_do_pending_expose ()
1563 {
1564 int mask;
1565 struct frame *f;
1566 Lisp_Object tail, frame;
1567
1568 if (expose_all_windows)
1569 {
1570 expose_all_windows = 0;
1571 for (tail = Vframe_list; CONSP (tail); tail = XCONS (tail)->cdr)
1572 {
1573 register int temp_width, temp_height;
1574 int intborder;
1575
1576 frame = XCONS (tail)->car;
1577 if (XGCTYPE (frame) != Lisp_Frame)
1578 continue;
1579 f = XFRAME (frame);
1580 if (! FRAME_X_P (f))
1581 continue;
1582 if (!f->async_visible)
1583 continue;
1584 if (!f->display.x->needs_exposure)
1585 continue;
1586
1587 intborder = f->display.x->internal_border_width;
1588
1589 clear_cursor (f);
1590 XGetWindowInfo (FRAME_X_WINDOW (f), &windowinfo);
1591 temp_width = ((windowinfo.width - 2 * intborder
1592 - f->display.x->v_scroll_bar_width)
1593 / FONT_WIDTH (f->display.x->font));
1594 temp_height = ((windowinfo.height- 2 * intborder
1595 - f->display.x->h_scroll_bar_height)
1596 / f->display.x->line_height);
1597 if (temp_width != f->width || temp_height != f->height)
1598 {
1599 change_frame_size (f, max (1, temp_height),
1600 max (1, temp_width), 0, 1);
1601 x_resize_scroll_bars (f);
1602 }
1603 f->display.x->left_pos = windowinfo.x;
1604 f->display.x->top_pos = windowinfo.y;
1605 dumprectangle (f, 0, 0, PIXEL_WIDTH (f), PIXEL_HEIGHT (f));
1606 #if 0
1607 dumpborder (f, 0);
1608 #endif /* ! 0 */
1609 f->display.x->needs_exposure = 0;
1610 if (updating_frame != f)
1611 x_display_cursor (f, 1);
1612 XFlushQueue ();
1613 }
1614 }
1615 else
1616 /* Handle any individual-rectangle expose events queued
1617 for various windows. */
1618 #ifdef HAVE_X11
1619 ;
1620 #else /* ! defined (HAVE_X11) */
1621 dumpqueue ();
1622 #endif /* ! defined (HAVE_X11) */
1623 }
1624 #endif
1625
1626 #ifdef HAVE_X11
1627 static void
1628 frame_highlight (frame)
1629 struct frame *frame;
1630 {
1631 /* We used to only do this if Vx_no_window_manager was non-nil, but
1632 the ICCCM (section 4.1.6) says that the window's border pixmap
1633 and border pixel are window attributes which are "private to the
1634 client", so we can always change it to whatever we want. */
1635 BLOCK_INPUT;
1636 XSetWindowBorder (x_current_display, FRAME_X_WINDOW (frame),
1637 frame->display.x->border_pixel);
1638 UNBLOCK_INPUT;
1639 x_display_cursor (frame, 1);
1640 }
1641
1642 static void
1643 frame_unhighlight (frame)
1644 struct frame *frame;
1645 {
1646 /* We used to only do this if Vx_no_window_manager was non-nil, but
1647 the ICCCM (section 4.1.6) says that the window's border pixmap
1648 and border pixel are window attributes which are "private to the
1649 client", so we can always change it to whatever we want. */
1650 BLOCK_INPUT;
1651 XSetWindowBorderPixmap (x_current_display, FRAME_X_WINDOW (frame),
1652 frame->display.x->border_tile);
1653 UNBLOCK_INPUT;
1654 x_display_cursor (frame, 1);
1655 }
1656 #else /* ! defined (HAVE_X11) */
1657 /* Dump the border-emphasis of frame F.
1658 If F is selected, this is a lining of the same color as the border,
1659 just within the border, occupying a portion of the internal border.
1660 If F is not selected, it is background in the same place.
1661 If ALWAYS is 0, don't bother explicitly drawing if it's background.
1662
1663 ALWAYS = 1 is used when a frame becomes selected or deselected.
1664 In that case, we also turn the cursor off and on again
1665 so it will appear in the proper shape (solid if selected; else hollow.) */
1666
1667 static void
1668 dumpborder (f, always)
1669 struct frame *f;
1670 int always;
1671 {
1672 int thickness = f->display.x->internal_border_width / 2;
1673 int width = PIXEL_WIDTH (f);
1674 int height = PIXEL_HEIGHT (f);
1675 int pixel;
1676
1677 if (f != selected_frame)
1678 {
1679 if (!always)
1680 return;
1681
1682 pixel = f->display.x->background_pixel;
1683 }
1684 else
1685 {
1686 pixel = f->display.x->border_pixel;
1687 }
1688
1689 XPixSet (FRAME_X_WINDOW (f), 0, 0, width, thickness, pixel);
1690 XPixSet (FRAME_X_WINDOW (f), 0, 0, thickness, height, pixel);
1691 XPixSet (FRAME_X_WINDOW (f), 0, height - thickness, width,
1692 thickness, pixel);
1693 XPixSet (FRAME_X_WINDOW (f), width - thickness, 0, thickness,
1694 height, pixel);
1695
1696 if (always)
1697 x_display_cursor (f, 1);
1698 }
1699 #endif /* ! defined (HAVE_X11) */
1700
1701 static void XTframe_rehighlight ();
1702
1703 /* The focus has changed. Update the frames as necessary to reflect
1704 the new situation. Note that we can't change the selected frame
1705 here, because the lisp code we are interrupting might become confused.
1706 Each event gets marked with the frame in which it occurred, so the
1707 lisp code can tell when the switch took place by examining the events. */
1708
1709 static void
1710 x_new_focus_frame (frame)
1711 struct frame *frame;
1712 {
1713 struct frame *old_focus = x_focus_frame;
1714 int events_enqueued = 0;
1715
1716 if (frame != x_focus_frame)
1717 {
1718 /* Set this before calling other routines, so that they see
1719 the correct value of x_focus_frame. */
1720 x_focus_frame = frame;
1721
1722 if (old_focus && old_focus->auto_lower)
1723 x_lower_frame (old_focus);
1724
1725 #if 0
1726 selected_frame = frame;
1727 XSET (XWINDOW (selected_frame->selected_window)->frame,
1728 Lisp_Frame, selected_frame);
1729 Fselect_window (selected_frame->selected_window);
1730 choose_minibuf_frame ();
1731 #endif /* ! 0 */
1732
1733 if (x_focus_frame && x_focus_frame->auto_raise)
1734 pending_autoraise_frame = x_focus_frame;
1735 else
1736 pending_autoraise_frame = 0;
1737 }
1738
1739 XTframe_rehighlight ();
1740 }
1741
1742
1743 /* The focus has changed, or we have redirected a frame's focus to
1744 another frame (this happens when a frame uses a surrogate
1745 minibuffer frame). Shift the highlight as appropriate. */
1746 static void
1747 XTframe_rehighlight ()
1748 {
1749 struct frame *old_highlight = x_highlight_frame;
1750
1751 if (x_focus_frame)
1752 {
1753 x_highlight_frame =
1754 ((XGCTYPE (FRAME_FOCUS_FRAME (x_focus_frame)) == Lisp_Frame)
1755 ? XFRAME (FRAME_FOCUS_FRAME (x_focus_frame))
1756 : x_focus_frame);
1757 if (! FRAME_LIVE_P (x_highlight_frame))
1758 {
1759 FRAME_FOCUS_FRAME (x_focus_frame) = Qnil;
1760 x_highlight_frame = x_focus_frame;
1761 }
1762 }
1763 else
1764 x_highlight_frame = 0;
1765
1766 if (x_highlight_frame != old_highlight)
1767 {
1768 if (old_highlight)
1769 frame_unhighlight (old_highlight);
1770 if (x_highlight_frame)
1771 frame_highlight (x_highlight_frame);
1772 }
1773 }
1774 \f
1775 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
1776
1777 /* Which modifier keys are on which modifier bits?
1778
1779 With each keystroke, X returns eight bits indicating which modifier
1780 keys were held down when the key was pressed. The interpretation
1781 of the top five modifier bits depends on what keys are attached
1782 to them. If the Meta_L and Meta_R keysyms are on mod5, then mod5
1783 is the meta bit.
1784
1785 x_meta_mod_mask is a mask containing the bits used for the meta key.
1786 It may have more than one bit set, if more than one modifier bit
1787 has meta keys on it. Basically, if EVENT is a KeyPress event,
1788 the meta key is pressed if (EVENT.state & x_meta_mod_mask) != 0.
1789
1790 x_shift_lock_mask is LockMask if the XK_Shift_Lock keysym is on the
1791 lock modifier bit, or zero otherwise. Non-alphabetic keys should
1792 only be affected by the lock modifier bit if XK_Shift_Lock is in
1793 use; XK_Caps_Lock should only affect alphabetic keys. With this
1794 arrangement, the lock modifier should shift the character if
1795 (EVENT.state & x_shift_lock_mask) != 0. */
1796 static int x_meta_mod_mask, x_shift_lock_mask;
1797
1798 /* These are like x_meta_mod_mask, but for different modifiers. */
1799 static int x_alt_mod_mask, x_super_mod_mask, x_hyper_mod_mask;
1800
1801 /* Initialize mode_switch_bit and modifier_meaning. */
1802 static void
1803 x_find_modifier_meanings ()
1804 {
1805 int min_code, max_code;
1806 KeySym *syms;
1807 int syms_per_code;
1808 XModifierKeymap *mods;
1809
1810 x_meta_mod_mask = 0;
1811 x_shift_lock_mask = 0;
1812 x_alt_mod_mask = 0;
1813 x_super_mod_mask = 0;
1814 x_hyper_mod_mask = 0;
1815
1816 #ifdef HAVE_X11R4
1817 XDisplayKeycodes (x_current_display, &min_code, &max_code);
1818 #else
1819 min_code = x_current_display->min_keycode;
1820 max_code = x_current_display->max_keycode;
1821 #endif
1822
1823 syms = XGetKeyboardMapping (x_current_display,
1824 min_code, max_code - min_code + 1,
1825 &syms_per_code);
1826 mods = XGetModifierMapping (x_current_display);
1827
1828 /* Scan the modifier table to see which modifier bits the Meta and
1829 Alt keysyms are on. */
1830 {
1831 int row, col; /* The row and column in the modifier table. */
1832
1833 for (row = 3; row < 8; row++)
1834 for (col = 0; col < mods->max_keypermod; col++)
1835 {
1836 KeyCode code =
1837 mods->modifiermap[(row * mods->max_keypermod) + col];
1838
1839 /* Zeroes are used for filler. Skip them. */
1840 if (code == 0)
1841 continue;
1842
1843 /* Are any of this keycode's keysyms a meta key? */
1844 {
1845 int code_col;
1846
1847 for (code_col = 0; code_col < syms_per_code; code_col++)
1848 {
1849 int sym = syms[((code - min_code) * syms_per_code) + code_col];
1850
1851 switch (sym)
1852 {
1853 case XK_Meta_L:
1854 case XK_Meta_R:
1855 x_meta_mod_mask |= (1 << row);
1856 break;
1857
1858 case XK_Alt_L:
1859 case XK_Alt_R:
1860 x_alt_mod_mask |= (1 << row);
1861 break;
1862
1863 case XK_Hyper_L:
1864 case XK_Hyper_R:
1865 x_hyper_mod_mask |= (1 << row);
1866 break;
1867
1868 case XK_Super_L:
1869 case XK_Super_R:
1870 x_super_mod_mask |= (1 << row);
1871 break;
1872
1873 case XK_Shift_Lock:
1874 /* Ignore this if it's not on the lock modifier. */
1875 if ((1 << row) == LockMask)
1876 x_shift_lock_mask = LockMask;
1877 break;
1878 }
1879 }
1880 }
1881 }
1882 }
1883
1884 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
1885 if (! x_meta_mod_mask)
1886 {
1887 x_meta_mod_mask = x_alt_mod_mask;
1888 x_alt_mod_mask = 0;
1889 }
1890
1891 /* If some keys are both alt and meta,
1892 make them just meta, not alt. */
1893 if (x_alt_mod_mask & x_meta_mod_mask)
1894 {
1895 x_alt_mod_mask &= ~x_meta_mod_mask;
1896 }
1897
1898 XFree ((char *) syms);
1899 XFreeModifiermap (mods);
1900 }
1901
1902 /* Convert between the modifier bits X uses and the modifier bits
1903 Emacs uses. */
1904 static unsigned int
1905 x_x_to_emacs_modifiers (state)
1906 unsigned int state;
1907 {
1908 return ( ((state & (ShiftMask | x_shift_lock_mask)) ? shift_modifier : 0)
1909 | ((state & ControlMask) ? ctrl_modifier : 0)
1910 | ((state & x_meta_mod_mask) ? meta_modifier : 0)
1911 | ((state & x_alt_mod_mask) ? alt_modifier : 0)
1912 | ((state & x_super_mod_mask) ? super_modifier : 0)
1913 | ((state & x_hyper_mod_mask) ? hyper_modifier : 0));
1914 }
1915
1916 static unsigned int
1917 x_emacs_to_x_modifiers (state)
1918 unsigned int state;
1919 {
1920 return ( ((state & alt_modifier) ? x_alt_mod_mask : 0)
1921 | ((state & super_modifier) ? x_super_mod_mask : 0)
1922 | ((state & hyper_modifier) ? x_hyper_mod_mask : 0)
1923 | ((state & shift_modifier) ? ShiftMask : 0)
1924 | ((state & ctrl_modifier) ? ControlMask : 0)
1925 | ((state & meta_modifier) ? x_meta_mod_mask : 0));
1926 }
1927 \f
1928 /* Mouse clicks and mouse movement. Rah. */
1929 #ifdef HAVE_X11
1930
1931 /* Given a pixel position (PIX_X, PIX_Y) on the frame F, return
1932 glyph co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle
1933 that the glyph at X, Y occupies, if BOUNDS != 0.
1934 If NOCLIP is nonzero, do not force the value into range. */
1935
1936 void
1937 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1938 FRAME_PTR f;
1939 register int pix_x, pix_y;
1940 register int *x, *y;
1941 XRectangle *bounds;
1942 int noclip;
1943 {
1944 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
1945 even for negative values. */
1946 if (pix_x < 0)
1947 pix_x -= FONT_WIDTH ((f)->display.x->font) - 1;
1948 if (pix_y < 0)
1949 pix_y -= (f)->display.x->line_height - 1;
1950
1951 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
1952 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
1953
1954 if (bounds)
1955 {
1956 bounds->width = FONT_WIDTH (f->display.x->font);
1957 bounds->height = f->display.x->line_height;
1958 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
1959 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
1960 }
1961
1962 if (!noclip)
1963 {
1964 if (pix_x < 0)
1965 pix_x = 0;
1966 else if (pix_x > f->width)
1967 pix_x = f->width;
1968
1969 if (pix_y < 0)
1970 pix_y = 0;
1971 else if (pix_y > f->height)
1972 pix_y = f->height;
1973 }
1974
1975 *x = pix_x;
1976 *y = pix_y;
1977 }
1978
1979 void
1980 glyph_to_pixel_coords (f, x, y, pix_x, pix_y)
1981 FRAME_PTR f;
1982 register int x, y;
1983 register int *pix_x, *pix_y;
1984 {
1985 *pix_x = CHAR_TO_PIXEL_COL (f, x);
1986 *pix_y = CHAR_TO_PIXEL_ROW (f, y);
1987 }
1988
1989 /* Prepare a mouse-event in *RESULT for placement in the input queue.
1990
1991 If the event is a button press, then note that we have grabbed
1992 the mouse. */
1993
1994 static Lisp_Object
1995 construct_mouse_click (result, event, f)
1996 struct input_event *result;
1997 XButtonEvent *event;
1998 struct frame *f;
1999 {
2000 /* Make the event type no_event; we'll change that when we decide
2001 otherwise. */
2002 result->kind = mouse_click;
2003 result->code = event->button - Button1;
2004 result->timestamp = event->time;
2005 result->modifiers = (x_x_to_emacs_modifiers (event->state)
2006 | (event->type == ButtonRelease
2007 ? up_modifier
2008 : down_modifier));
2009
2010 /* Notice if the mouse is still grabbed. */
2011 if (event->type == ButtonPress)
2012 {
2013 if (! x_mouse_grabbed)
2014 Vmouse_depressed = Qt;
2015 x_mouse_grabbed |= (1 << event->button);
2016 last_mouse_frame = f;
2017 }
2018 else if (event->type == ButtonRelease)
2019 {
2020 x_mouse_grabbed &= ~(1 << event->button);
2021 if (!x_mouse_grabbed)
2022 Vmouse_depressed = Qnil;
2023 }
2024
2025 {
2026 int row, column;
2027
2028 #if 0
2029 pixel_to_glyph_coords (f, event->x, event->y, &column, &row, NULL, 0);
2030 XFASTINT (result->x) = column;
2031 XFASTINT (result->y) = row;
2032 #endif
2033 XSET (result->x, Lisp_Int, event->x);
2034 XSET (result->y, Lisp_Int, event->y);
2035 XSET (result->frame_or_window, Lisp_Frame, f);
2036 }
2037 }
2038
2039 /* Prepare a menu-event in *RESULT for placement in the input queue. */
2040
2041 static Lisp_Object
2042 construct_menu_click (result, event, f)
2043 struct input_event *result;
2044 XButtonEvent *event;
2045 struct frame *f;
2046 {
2047 /* Make the event type no_event; we'll change that when we decide
2048 otherwise. */
2049 result->kind = mouse_click;
2050 XSET (result->code, Lisp_Int, event->button - Button1);
2051 result->timestamp = event->time;
2052 result->modifiers = (x_x_to_emacs_modifiers (event->state)
2053 | (event->type == ButtonRelease
2054 ? up_modifier
2055 : down_modifier));
2056
2057 XSET (result->x, Lisp_Int, event->x);
2058 XSET (result->y, Lisp_Int, -1);
2059 XSET (result->frame_or_window, Lisp_Frame, f);
2060 }
2061 \f
2062 /* Function to report a mouse movement to the mainstream Emacs code.
2063 The input handler calls this.
2064
2065 We have received a mouse movement event, which is given in *event.
2066 If the mouse is over a different glyph than it was last time, tell
2067 the mainstream emacs code by setting mouse_moved. If not, ask for
2068 another motion event, so we can check again the next time it moves. */
2069
2070 static void
2071 note_mouse_movement (frame, event)
2072 FRAME_PTR frame;
2073 XMotionEvent *event;
2074
2075 {
2076 last_mouse_movement_time = event->time;
2077
2078 if (event->window != FRAME_X_WINDOW (frame))
2079 {
2080 mouse_moved = 1;
2081 last_mouse_scroll_bar = Qnil;
2082
2083 note_mouse_highlight (frame, -1, -1);
2084
2085 /* Ask for another mouse motion event. */
2086 {
2087 int dummy;
2088
2089 XQueryPointer (event->display, FRAME_X_WINDOW (frame),
2090 (Window *) &dummy, (Window *) &dummy,
2091 &dummy, &dummy, &dummy, &dummy,
2092 (unsigned int *) &dummy);
2093 }
2094 }
2095
2096 /* Has the mouse moved off the glyph it was on at the last sighting? */
2097 else if (event->x < last_mouse_glyph.x
2098 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
2099 || event->y < last_mouse_glyph.y
2100 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
2101 {
2102 mouse_moved = 1;
2103 last_mouse_scroll_bar = Qnil;
2104
2105 note_mouse_highlight (frame, event->x, event->y);
2106
2107 /* Ask for another mouse motion event. */
2108 {
2109 int dummy;
2110
2111 XQueryPointer (event->display, FRAME_X_WINDOW (frame),
2112 (Window *) &dummy, (Window *) &dummy,
2113 &dummy, &dummy, &dummy, &dummy,
2114 (unsigned int *) &dummy);
2115 }
2116 }
2117 else
2118 {
2119 /* It's on the same glyph. Call XQueryPointer so we'll get an
2120 event the next time the mouse moves and we can see if it's
2121 *still* on the same glyph. */
2122 int dummy;
2123
2124 XQueryPointer (event->display, FRAME_X_WINDOW (frame),
2125 (Window *) &dummy, (Window *) &dummy,
2126 &dummy, &dummy, &dummy, &dummy,
2127 (unsigned int *) &dummy);
2128 }
2129 }
2130
2131 /* This is used for debugging, to turn off note_mouse_highlight. */
2132 static int disable_mouse_highlight;
2133
2134 /* Take proper action when the mouse has moved to position X, Y on frame F
2135 as regards highlighting characters that have mouse-face properties.
2136 Also dehighlighting chars where the mouse was before.
2137 X and Y can be negative or out of range. */
2138
2139 static void
2140 note_mouse_highlight (f, x, y)
2141 FRAME_PTR f;
2142 {
2143 int row, column, portion;
2144 XRectangle new_glyph;
2145 Lisp_Object window;
2146 struct window *w;
2147
2148 if (disable_mouse_highlight)
2149 return;
2150
2151 mouse_face_mouse_x = x;
2152 mouse_face_mouse_y = y;
2153 mouse_face_mouse_frame = f;
2154
2155 if (mouse_face_defer)
2156 return;
2157
2158 if (gc_in_progress)
2159 {
2160 mouse_face_deferred_gc = 1;
2161 return;
2162 }
2163
2164 /* Find out which glyph the mouse is on. */
2165 pixel_to_glyph_coords (f, x, y, &column, &row,
2166 &new_glyph, x_mouse_grabbed);
2167
2168 /* Which window is that in? */
2169 window = window_from_coordinates (f, column, row, &portion);
2170 w = XWINDOW (window);
2171
2172 /* If we were displaying active text in another window, clear that. */
2173 if (! EQ (window, mouse_face_window))
2174 clear_mouse_face ();
2175
2176 /* Are we in a window whose display is up to date?
2177 And verify the buffer's text has not changed. */
2178 if (WINDOWP (window) && portion == 0 && row >= 0 && column >= 0
2179 && row < FRAME_HEIGHT (f) && column < FRAME_WIDTH (f)
2180 && EQ (w->window_end_valid, w->buffer)
2181 && w->last_modified == BUF_MODIFF (XBUFFER (w->buffer)))
2182 {
2183 int *ptr = FRAME_CURRENT_GLYPHS (f)->charstarts[row];
2184 int i, pos;
2185
2186 /* Find which buffer position the mouse corresponds to. */
2187 for (i = column; i >= 0; i--)
2188 if (ptr[i] > 0)
2189 break;
2190 pos = ptr[i];
2191 /* Is it outside the displayed active region (if any)? */
2192 if (pos <= 0)
2193 clear_mouse_face ();
2194 else if (! (EQ (window, mouse_face_window)
2195 && row >= mouse_face_beg_row
2196 && row <= mouse_face_end_row
2197 && (row > mouse_face_beg_row || column >= mouse_face_beg_col)
2198 && (row < mouse_face_end_row || column < mouse_face_end_col
2199 || mouse_face_past_end)))
2200 {
2201 Lisp_Object mouse_face, overlay, position;
2202 Lisp_Object *overlay_vec;
2203 int len, noverlays, ignor1;
2204 struct buffer *obuf;
2205 int obegv, ozv;
2206
2207 /* If we get an out-of-range value, return now; avoid an error. */
2208 if (pos > BUF_Z (XBUFFER (w->buffer)))
2209 return;
2210
2211 /* Make the window's buffer temporarily current for
2212 overlays_at and compute_char_face. */
2213 obuf = current_buffer;
2214 current_buffer = XBUFFER (w->buffer);
2215 obegv = BEGV;
2216 ozv = ZV;
2217 BEGV = BEG;
2218 ZV = Z;
2219
2220 /* Yes. Clear the display of the old active region, if any. */
2221 clear_mouse_face ();
2222
2223 /* Is this char mouse-active? */
2224 XSET (position, Lisp_Int, pos);
2225
2226 len = 10;
2227 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
2228
2229 /* Put all the overlays we want in a vector in overlay_vec.
2230 Store the length in len. */
2231 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len, &ignor1);
2232 noverlays = sort_overlays (overlay_vec, noverlays, w);
2233
2234 /* Find the highest priority overlay that has a mouse-face prop. */
2235 overlay = Qnil;
2236 for (i = 0; i < noverlays; i++)
2237 {
2238 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
2239 if (!NILP (mouse_face))
2240 {
2241 overlay = overlay_vec[i];
2242 break;
2243 }
2244 }
2245 free (overlay_vec);
2246 /* If no overlay applies, get a text property. */
2247 if (NILP (overlay))
2248 mouse_face = Fget_text_property (position, Qmouse_face, w->buffer);
2249
2250 /* Handle the overlay case. */
2251 if (! NILP (overlay))
2252 {
2253 /* Find the range of text around this char that
2254 should be active. */
2255 Lisp_Object before, after;
2256 int ignore;
2257
2258 before = Foverlay_start (overlay);
2259 after = Foverlay_end (overlay);
2260 /* Record this as the current active region. */
2261 fast_find_position (window, before, &mouse_face_beg_col,
2262 &mouse_face_beg_row);
2263 mouse_face_past_end
2264 = !fast_find_position (window, after, &mouse_face_end_col,
2265 &mouse_face_end_row);
2266 mouse_face_window = window;
2267 mouse_face_face_id = compute_char_face (f, w, pos, 0, 0,
2268 &ignore, pos + 1, 1);
2269
2270 /* Display it as active. */
2271 show_mouse_face (1);
2272 }
2273 /* Handle the text property case. */
2274 else if (! NILP (mouse_face))
2275 {
2276 /* Find the range of text around this char that
2277 should be active. */
2278 Lisp_Object before, after, beginning, end;
2279 int ignore;
2280
2281 beginning = Fmarker_position (w->start);
2282 XSET (end, Lisp_Int,
2283 (BUF_Z (XBUFFER (w->buffer))
2284 - XFASTINT (w->window_end_pos)));
2285 before
2286 = Fprevious_single_property_change (make_number (pos + 1),
2287 Qmouse_face,
2288 w->buffer, beginning);
2289 after
2290 = Fnext_single_property_change (position, Qmouse_face,
2291 w->buffer, end);
2292 /* Record this as the current active region. */
2293 fast_find_position (window, before, &mouse_face_beg_col,
2294 &mouse_face_beg_row);
2295 mouse_face_past_end
2296 = !fast_find_position (window, after, &mouse_face_end_col,
2297 &mouse_face_end_row);
2298 mouse_face_window = window;
2299 mouse_face_face_id
2300 = compute_char_face (f, w, pos, 0, 0,
2301 &ignore, pos + 1, 1);
2302
2303 /* Display it as active. */
2304 show_mouse_face (1);
2305 }
2306 BEGV = obegv;
2307 ZV = ozv;
2308 current_buffer = obuf;
2309 }
2310 }
2311 }
2312 \f
2313 /* Find the row and column of position POS in window WINDOW.
2314 Store them in *COLUMNP and *ROWP.
2315 This assumes display in WINDOW is up to date.
2316 If POS is above start of WINDOW, return coords
2317 of start of first screen line.
2318 If POS is after end of WINDOW, return coords of end of last screen line.
2319
2320 Value is 1 if POS is in range, 0 if it was off screen. */
2321
2322 static int
2323 fast_find_position (window, pos, columnp, rowp)
2324 Lisp_Object window;
2325 int pos;
2326 int *columnp, *rowp;
2327 {
2328 struct window *w = XWINDOW (window);
2329 FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
2330 int i;
2331 int row = 0;
2332 int left = w->left;
2333 int top = w->top;
2334 int height = XFASTINT (w->height) - ! MINI_WINDOW_P (w);
2335 int width = window_internal_width (w);
2336 int *charstarts;
2337 int lastcol;
2338
2339 /* Find the right row. */
2340 for (i = 0;
2341 i < height;
2342 i++)
2343 {
2344 int linestart = FRAME_CURRENT_GLYPHS (f)->charstarts[top + i][left];
2345 if (linestart > pos)
2346 break;
2347 if (linestart > 0)
2348 row = i;
2349 }
2350
2351 /* Find the right column with in it. */
2352 charstarts = FRAME_CURRENT_GLYPHS (f)->charstarts[top + row];
2353 lastcol = left;
2354 for (i = 0; i < width; i++)
2355 {
2356 if (charstarts[left + i] == pos)
2357 {
2358 *rowp = row + top;
2359 *columnp = i + left;
2360 return 1;
2361 }
2362 else if (charstarts[left + i] > pos)
2363 break;
2364 else if (charstarts[left + i] > 0)
2365 lastcol = left + i;
2366 }
2367
2368 *rowp = row + top;
2369 *columnp = lastcol;
2370 return 0;
2371 }
2372
2373 /* Display the active region described by mouse_face_*
2374 in its mouse-face if HL > 0, in its normal face if HL = 0. */
2375
2376 static void
2377 show_mouse_face (hl)
2378 int hl;
2379 {
2380 struct window *w = XWINDOW (mouse_face_window);
2381 int width = window_internal_width (w);
2382 FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
2383 int i;
2384 int curs_x = f->phys_cursor_x;
2385 int curs_y = f->phys_cursor_y;
2386 int cursor_off = 0;
2387
2388 for (i = mouse_face_beg_row; i <= mouse_face_end_row; i++)
2389 {
2390 int column = (i == mouse_face_beg_row ? mouse_face_beg_col : w->left);
2391 int endcolumn = (i == mouse_face_end_row ? mouse_face_end_col : w->left + width);
2392 endcolumn = min (endcolumn, FRAME_CURRENT_GLYPHS (f)->used[i] - w->left);
2393
2394 /* If the cursor's in the text we are about to rewrite,
2395 turn the cursor off. */
2396 if (i == curs_y
2397 && curs_x >= mouse_face_beg_col - 1 && curs_x <= mouse_face_end_col)
2398 {
2399 x_display_cursor (f, 0);
2400 cursor_off = 1;
2401 }
2402
2403 dumpglyphs (f,
2404 CHAR_TO_PIXEL_COL (f, column),
2405 CHAR_TO_PIXEL_ROW (f, i),
2406 FRAME_CURRENT_GLYPHS (f)->glyphs[i] + column,
2407 endcolumn - column,
2408 /* Highlight with mouse face if hl > 0. */
2409 hl > 0 ? 3 : 0, 0);
2410 }
2411
2412 /* If we turned the cursor off, turn it back on. */
2413 if (cursor_off)
2414 x_display_cursor (f, 1);
2415
2416 /* Change the mouse cursor according to the value of HL. */
2417 if (hl > 0)
2418 XDefineCursor (XDISPLAY FRAME_X_WINDOW (f), f->display.x->cross_cursor);
2419 else
2420 XDefineCursor (XDISPLAY FRAME_X_WINDOW (f), f->display.x->text_cursor);
2421 }
2422
2423 /* Clear out the mouse-highlighted active region.
2424 Redraw it unhighlighted first. */
2425
2426 static void
2427 clear_mouse_face ()
2428 {
2429 if (! NILP (mouse_face_window))
2430 show_mouse_face (0);
2431
2432 mouse_face_beg_row = mouse_face_beg_col = -1;
2433 mouse_face_end_row = mouse_face_end_col = -1;
2434 mouse_face_window = Qnil;
2435 }
2436 \f
2437 static struct scroll_bar *x_window_to_scroll_bar ();
2438 static void x_scroll_bar_report_motion ();
2439
2440 /* Return the current position of the mouse.
2441
2442 If the mouse movement started in a scroll bar, set *f, *bar_window,
2443 and *part to the frame, window, and scroll bar part that the mouse
2444 is over. Set *x and *y to the portion and whole of the mouse's
2445 position on the scroll bar.
2446
2447 If the mouse movement started elsewhere, set *f to the frame the
2448 mouse is on, *bar_window to nil, and *x and *y to the character cell
2449 the mouse is over.
2450
2451 Set *time to the server timestamp for the time at which the mouse
2452 was at this position.
2453
2454 Don't store anything if we don't have a valid set of values to report.
2455
2456 This clears the mouse_moved flag, so we can wait for the next mouse
2457 movement. This also calls XQueryPointer, which will cause the
2458 server to give us another MotionNotify when the mouse moves
2459 again. */
2460
2461 static void
2462 XTmouse_position (f, bar_window, part, x, y, time)
2463 FRAME_PTR *f;
2464 Lisp_Object *bar_window;
2465 enum scroll_bar_part *part;
2466 Lisp_Object *x, *y;
2467 unsigned long *time;
2468 {
2469 FRAME_PTR f1;
2470
2471 BLOCK_INPUT;
2472
2473 if (! NILP (last_mouse_scroll_bar))
2474 x_scroll_bar_report_motion (f, bar_window, part, x, y, time);
2475 else
2476 {
2477 Window root;
2478 int root_x, root_y;
2479
2480 Window dummy_window;
2481 int dummy;
2482
2483 mouse_moved = 0;
2484 last_mouse_scroll_bar = Qnil;
2485
2486 /* Figure out which root window we're on. */
2487 XQueryPointer (x_current_display,
2488 DefaultRootWindow (x_current_display),
2489
2490 /* The root window which contains the pointer. */
2491 &root,
2492
2493 /* Trash which we can't trust if the pointer is on
2494 a different screen. */
2495 &dummy_window,
2496
2497 /* The position on that root window. */
2498 &root_x, &root_y,
2499
2500 /* More trash we can't trust. */
2501 &dummy, &dummy,
2502
2503 /* Modifier keys and pointer buttons, about which
2504 we don't care. */
2505 (unsigned int *) &dummy);
2506
2507 /* Now we have a position on the root; find the innermost window
2508 containing the pointer. */
2509 {
2510 Window win, child;
2511 int win_x, win_y;
2512 int parent_x, parent_y;
2513
2514 win = root;
2515
2516 if (x_mouse_grabbed && FRAME_LIVE_P (last_mouse_frame))
2517 {
2518 /* If mouse was grabbed on a frame, give coords for that frame
2519 even if the mouse is now outside it. */
2520 XTranslateCoordinates (x_current_display,
2521
2522 /* From-window, to-window. */
2523 root, FRAME_X_WINDOW (last_mouse_frame),
2524
2525 /* From-position, to-position. */
2526 root_x, root_y, &win_x, &win_y,
2527
2528 /* Child of win. */
2529 &child);
2530 f1 = last_mouse_frame;
2531 }
2532 else
2533 {
2534 while (1)
2535 {
2536 XTranslateCoordinates (x_current_display,
2537
2538 /* From-window, to-window. */
2539 root, win,
2540
2541 /* From-position, to-position. */
2542 root_x, root_y, &win_x, &win_y,
2543
2544 /* Child of win. */
2545 &child);
2546
2547 if (child == None)
2548 break;
2549
2550 win = child;
2551 parent_x = win_x;
2552 parent_y = win_y;
2553 }
2554
2555 /* Now we know that:
2556 win is the innermost window containing the pointer
2557 (XTC says it has no child containing the pointer),
2558 win_x and win_y are the pointer's position in it
2559 (XTC did this the last time through), and
2560 parent_x and parent_y are the pointer's position in win's parent.
2561 (They are what win_x and win_y were when win was child.
2562 If win is the root window, it has no parent, and
2563 parent_{x,y} are invalid, but that's okay, because we'll
2564 never use them in that case.) */
2565
2566 /* Is win one of our frames? */
2567 f1 = x_any_window_to_frame (win);
2568 }
2569
2570 /* If not, is it one of our scroll bars? */
2571 if (! f1)
2572 {
2573 struct scroll_bar *bar = x_window_to_scroll_bar (win);
2574
2575 if (bar)
2576 {
2577 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
2578 win_x = parent_x;
2579 win_y = parent_y;
2580 }
2581 }
2582
2583 if (f1)
2584 {
2585 int ignore1, ignore2;
2586
2587 /* Ok, we found a frame. Store all the values. */
2588
2589 pixel_to_glyph_coords (f1, win_x, win_y, &ignore1, &ignore2,
2590 &last_mouse_glyph, x_mouse_grabbed);
2591
2592 *bar_window = Qnil;
2593 *part = 0;
2594 *f = f1;
2595 XSET (*x, Lisp_Int, win_x);
2596 XSET (*y, Lisp_Int, win_y);
2597 *time = last_mouse_movement_time;
2598 }
2599 }
2600 }
2601
2602 UNBLOCK_INPUT;
2603 }
2604
2605 #else /* ! defined (HAVE_X11) */
2606 #define XEvent XKeyPressedEvent
2607 #endif /* ! defined (HAVE_X11) */
2608 \f
2609 /* Scroll bar support. */
2610
2611 /* Given an X window ID, find the struct scroll_bar which manages it.
2612 This can be called in GC, so we have to make sure to strip off mark
2613 bits. */
2614 static struct scroll_bar *
2615 x_window_to_scroll_bar (window_id)
2616 Window window_id;
2617 {
2618 Lisp_Object tail, frame;
2619
2620 for (tail = Vframe_list;
2621 XGCTYPE (tail) == Lisp_Cons;
2622 tail = XCONS (tail)->cdr)
2623 {
2624 Lisp_Object frame, bar, condemned;
2625
2626 frame = XCONS (tail)->car;
2627 /* All elements of Vframe_list should be frames. */
2628 if (XGCTYPE (frame) != Lisp_Frame)
2629 abort ();
2630
2631 /* Scan this frame's scroll bar list for a scroll bar with the
2632 right window ID. */
2633 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
2634 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
2635 /* This trick allows us to search both the ordinary and
2636 condemned scroll bar lists with one loop. */
2637 ! GC_NILP (bar) || (bar = condemned,
2638 condemned = Qnil,
2639 ! GC_NILP (bar));
2640 bar = XSCROLL_BAR (bar)->next)
2641 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
2642 return XSCROLL_BAR (bar);
2643 }
2644
2645 return 0;
2646 }
2647
2648 /* Open a new X window to serve as a scroll bar, and return the
2649 scroll bar vector for it. */
2650 static struct scroll_bar *
2651 x_scroll_bar_create (window, top, left, width, height)
2652 struct window *window;
2653 int top, left, width, height;
2654 {
2655 FRAME_PTR frame = XFRAME (WINDOW_FRAME (window));
2656 struct scroll_bar *bar =
2657 XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
2658
2659 BLOCK_INPUT;
2660
2661 {
2662 XSetWindowAttributes a;
2663 unsigned long mask;
2664 a.background_pixel = frame->display.x->background_pixel;
2665 a.event_mask = (ButtonPressMask | ButtonReleaseMask
2666 | ButtonMotionMask | PointerMotionHintMask
2667 | ExposureMask);
2668 a.cursor = x_vertical_scroll_bar_cursor;
2669
2670 mask = (CWBackPixel | CWEventMask | CWCursor);
2671
2672 #if 0
2673
2674 ac = 0;
2675 XtSetArg (al[ac], XtNx, left); ac++;
2676 XtSetArg (al[ac], XtNy, top); ac++;
2677 XtSetArg (al[ac], XtNwidth, width); ac++;
2678 XtSetArg (al[ac], XtNheight, height); ac++;
2679 XtSetArg (al[ac], XtNborderWidth, 0); ac++;
2680 sb_widget = XtCreateManagedWidget ("box",
2681 boxWidgetClass,
2682 frame->display.x->edit_widget, al, ac);
2683 SET_SCROLL_BAR_X_WINDOW
2684 (bar, sb_widget->core.window);
2685 #endif
2686 SET_SCROLL_BAR_X_WINDOW
2687 (bar,
2688 XCreateWindow (x_current_display, FRAME_X_WINDOW (frame),
2689
2690 /* Position and size of scroll bar. */
2691 left, top, width, height,
2692
2693 /* Border width, depth, class, and visual. */
2694 0, CopyFromParent, CopyFromParent, CopyFromParent,
2695
2696 /* Attributes. */
2697 mask, &a));
2698 }
2699
2700 XSET (bar->window, Lisp_Window, window);
2701 XSET (bar->top, Lisp_Int, top);
2702 XSET (bar->left, Lisp_Int, left);
2703 XSET (bar->width, Lisp_Int, width);
2704 XSET (bar->height, Lisp_Int, height);
2705 XSET (bar->start, Lisp_Int, 0);
2706 XSET (bar->end, Lisp_Int, 0);
2707 bar->dragging = Qnil;
2708
2709 /* Add bar to its frame's list of scroll bars. */
2710 bar->next = FRAME_SCROLL_BARS (frame);
2711 bar->prev = Qnil;
2712 XSET (FRAME_SCROLL_BARS (frame), Lisp_Vector, bar);
2713 if (! NILP (bar->next))
2714 XSET (XSCROLL_BAR (bar->next)->prev, Lisp_Vector, bar);
2715
2716 XMapWindow (x_current_display, SCROLL_BAR_X_WINDOW (bar));
2717
2718 UNBLOCK_INPUT;
2719
2720 return bar;
2721 }
2722
2723 /* Draw BAR's handle in the proper position.
2724 If the handle is already drawn from START to END, don't bother
2725 redrawing it, unless REBUILD is non-zero; in that case, always
2726 redraw it. (REBUILD is handy for drawing the handle after expose
2727 events.)
2728
2729 Normally, we want to constrain the start and end of the handle to
2730 fit inside its rectangle, but if the user is dragging the scroll bar
2731 handle, we want to let them drag it down all the way, so that the
2732 bar's top is as far down as it goes; otherwise, there's no way to
2733 move to the very end of the buffer. */
2734 static void
2735 x_scroll_bar_set_handle (bar, start, end, rebuild)
2736 struct scroll_bar *bar;
2737 int start, end;
2738 int rebuild;
2739 {
2740 int dragging = ! NILP (bar->dragging);
2741 Window w = SCROLL_BAR_X_WINDOW (bar);
2742 GC gc = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))->display.x->normal_gc;
2743
2744 /* If the display is already accurate, do nothing. */
2745 if (! rebuild
2746 && start == XINT (bar->start)
2747 && end == XINT (bar->end))
2748 return;
2749
2750 BLOCK_INPUT;
2751
2752 {
2753 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (XINT (bar->width));
2754 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (XINT (bar->height));
2755 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (XINT (bar->height));
2756
2757 /* Make sure the values are reasonable, and try to preserve
2758 the distance between start and end. */
2759 {
2760 int length = end - start;
2761
2762 if (start < 0)
2763 start = 0;
2764 else if (start > top_range)
2765 start = top_range;
2766 end = start + length;
2767
2768 if (end < start)
2769 end = start;
2770 else if (end > top_range && ! dragging)
2771 end = top_range;
2772 }
2773
2774 /* Store the adjusted setting in the scroll bar. */
2775 XSET (bar->start, Lisp_Int, start);
2776 XSET (bar->end, Lisp_Int, end);
2777
2778 /* Clip the end position, just for display. */
2779 if (end > top_range)
2780 end = top_range;
2781
2782 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
2783 below top positions, to make sure the handle is always at least
2784 that many pixels tall. */
2785 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
2786
2787 /* Draw the empty space above the handle. Note that we can't clear
2788 zero-height areas; that means "clear to end of window." */
2789 if (0 < start)
2790 XClearArea (x_current_display, w,
2791
2792 /* x, y, width, height, and exposures. */
2793 VERTICAL_SCROLL_BAR_LEFT_BORDER,
2794 VERTICAL_SCROLL_BAR_TOP_BORDER,
2795 inside_width, start,
2796 False);
2797
2798 /* Draw the handle itself. */
2799 XFillRectangle (x_current_display, w, gc,
2800
2801 /* x, y, width, height */
2802 VERTICAL_SCROLL_BAR_LEFT_BORDER,
2803 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
2804 inside_width, end - start);
2805
2806
2807 /* Draw the empty space below the handle. Note that we can't
2808 clear zero-height areas; that means "clear to end of window." */
2809 if (end < inside_height)
2810 XClearArea (x_current_display, w,
2811
2812 /* x, y, width, height, and exposures. */
2813 VERTICAL_SCROLL_BAR_LEFT_BORDER,
2814 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
2815 inside_width, inside_height - end,
2816 False);
2817
2818 }
2819
2820 UNBLOCK_INPUT;
2821 }
2822
2823 /* Move a scroll bar around on the screen, to accommodate changing
2824 window configurations. */
2825 static void
2826 x_scroll_bar_move (bar, top, left, width, height)
2827 struct scroll_bar *bar;
2828 int top, left, width, height;
2829 {
2830 BLOCK_INPUT;
2831
2832 {
2833 XWindowChanges wc;
2834 unsigned int mask = 0;
2835
2836 wc.x = left;
2837 wc.y = top;
2838 wc.width = width;
2839 wc.height = height;
2840
2841 if (left != XINT (bar->left)) mask |= CWX;
2842 if (top != XINT (bar->top)) mask |= CWY;
2843 if (width != XINT (bar->width)) mask |= CWWidth;
2844 if (height != XINT (bar->height)) mask |= CWHeight;
2845
2846 if (mask)
2847 XConfigureWindow (x_current_display, SCROLL_BAR_X_WINDOW (bar),
2848 mask, &wc);
2849 }
2850
2851 XSET (bar->left, Lisp_Int, left);
2852 XSET (bar->top, Lisp_Int, top);
2853 XSET (bar->width, Lisp_Int, width);
2854 XSET (bar->height, Lisp_Int, height);
2855
2856 UNBLOCK_INPUT;
2857 }
2858
2859 /* Destroy the X window for BAR, and set its Emacs window's scroll bar
2860 to nil. */
2861 static void
2862 x_scroll_bar_remove (bar)
2863 struct scroll_bar *bar;
2864 {
2865 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
2866
2867 BLOCK_INPUT;
2868
2869 /* Destroy the window. */
2870 XDestroyWindow (x_current_display, SCROLL_BAR_X_WINDOW (bar));
2871
2872 /* Disassociate this scroll bar from its window. */
2873 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
2874
2875 UNBLOCK_INPUT;
2876 }
2877
2878 /* Set the handle of the vertical scroll bar for WINDOW to indicate
2879 that we are displaying PORTION characters out of a total of WHOLE
2880 characters, starting at POSITION. If WINDOW has no scroll bar,
2881 create one. */
2882 static void
2883 XTset_vertical_scroll_bar (window, portion, whole, position)
2884 struct window *window;
2885 int portion, whole, position;
2886 {
2887 FRAME_PTR f = XFRAME (WINDOW_FRAME (window));
2888 int top = XINT (window->top);
2889 int left = WINDOW_VERTICAL_SCROLL_BAR_COLUMN (window);
2890 int height = WINDOW_VERTICAL_SCROLL_BAR_HEIGHT (window);
2891
2892 /* Where should this scroll bar be, pixelwise? */
2893 int pixel_top = CHAR_TO_PIXEL_ROW (f, top);
2894 int pixel_left = CHAR_TO_PIXEL_COL (f, left);
2895 int pixel_width = VERTICAL_SCROLL_BAR_PIXEL_WIDTH (f);
2896 int pixel_height = VERTICAL_SCROLL_BAR_PIXEL_HEIGHT (f, height);
2897
2898 struct scroll_bar *bar;
2899
2900 /* Does the scroll bar exist yet? */
2901 if (NILP (window->vertical_scroll_bar))
2902 bar = x_scroll_bar_create (window,
2903 pixel_top, pixel_left,
2904 pixel_width, pixel_height);
2905 else
2906 {
2907 /* It may just need to be moved and resized. */
2908 bar = XSCROLL_BAR (window->vertical_scroll_bar);
2909 x_scroll_bar_move (bar, pixel_top, pixel_left, pixel_width, pixel_height);
2910 }
2911
2912 /* Set the scroll bar's current state, unless we're currently being
2913 dragged. */
2914 if (NILP (bar->dragging))
2915 {
2916 int top_range =
2917 VERTICAL_SCROLL_BAR_TOP_RANGE (pixel_height);
2918
2919 if (whole == 0)
2920 x_scroll_bar_set_handle (bar, 0, top_range, 0);
2921 else
2922 {
2923 int start = ((double) position * top_range) / whole;
2924 int end = ((double) (position + portion) * top_range) / whole;
2925
2926 x_scroll_bar_set_handle (bar, start, end, 0);
2927 }
2928 }
2929
2930 XSET (window->vertical_scroll_bar, Lisp_Vector, bar);
2931 }
2932
2933
2934 /* The following three hooks are used when we're doing a thorough
2935 redisplay of the frame. We don't explicitly know which scroll bars
2936 are going to be deleted, because keeping track of when windows go
2937 away is a real pain - "Can you say set-window-configuration, boys
2938 and girls?" Instead, we just assert at the beginning of redisplay
2939 that *all* scroll bars are to be removed, and then save a scroll bar
2940 from the fiery pit when we actually redisplay its window. */
2941
2942 /* Arrange for all scroll bars on FRAME to be removed at the next call
2943 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
2944 `*redeem_scroll_bar_hook' is applied to its window before the judgement. */
2945 static void
2946 XTcondemn_scroll_bars (frame)
2947 FRAME_PTR frame;
2948 {
2949 /* The condemned list should be empty at this point; if it's not,
2950 then the rest of Emacs isn't using the condemn/redeem/judge
2951 protocol correctly. */
2952 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
2953 abort ();
2954
2955 /* Move them all to the "condemned" list. */
2956 FRAME_CONDEMNED_SCROLL_BARS (frame) = FRAME_SCROLL_BARS (frame);
2957 FRAME_SCROLL_BARS (frame) = Qnil;
2958 }
2959
2960 /* Unmark WINDOW's scroll bar for deletion in this judgement cycle.
2961 Note that WINDOW isn't necessarily condemned at all. */
2962 static void
2963 XTredeem_scroll_bar (window)
2964 struct window *window;
2965 {
2966 struct scroll_bar *bar;
2967
2968 /* We can't redeem this window's scroll bar if it doesn't have one. */
2969 if (NILP (window->vertical_scroll_bar))
2970 abort ();
2971
2972 bar = XSCROLL_BAR (window->vertical_scroll_bar);
2973
2974 /* Unlink it from the condemned list. */
2975 {
2976 FRAME_PTR f = XFRAME (WINDOW_FRAME (window));
2977
2978 if (NILP (bar->prev))
2979 {
2980 /* If the prev pointer is nil, it must be the first in one of
2981 the lists. */
2982 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
2983 /* It's not condemned. Everything's fine. */
2984 return;
2985 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
2986 window->vertical_scroll_bar))
2987 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
2988 else
2989 /* If its prev pointer is nil, it must be at the front of
2990 one or the other! */
2991 abort ();
2992 }
2993 else
2994 XSCROLL_BAR (bar->prev)->next = bar->next;
2995
2996 if (! NILP (bar->next))
2997 XSCROLL_BAR (bar->next)->prev = bar->prev;
2998
2999 bar->next = FRAME_SCROLL_BARS (f);
3000 bar->prev = Qnil;
3001 XSET (FRAME_SCROLL_BARS (f), Lisp_Vector, bar);
3002 if (! NILP (bar->next))
3003 XSET (XSCROLL_BAR (bar->next)->prev, Lisp_Vector, bar);
3004 }
3005 }
3006
3007 /* Remove all scroll bars on FRAME that haven't been saved since the
3008 last call to `*condemn_scroll_bars_hook'. */
3009 static void
3010 XTjudge_scroll_bars (f)
3011 FRAME_PTR f;
3012 {
3013 Lisp_Object bar, next;
3014
3015 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
3016
3017 /* Clear out the condemned list now so we won't try to process any
3018 more events on the hapless scroll bars. */
3019 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
3020
3021 for (; ! NILP (bar); bar = next)
3022 {
3023 struct scroll_bar *b = XSCROLL_BAR (bar);
3024
3025 x_scroll_bar_remove (b);
3026
3027 next = b->next;
3028 b->next = b->prev = Qnil;
3029 }
3030
3031 /* Now there should be no references to the condemned scroll bars,
3032 and they should get garbage-collected. */
3033 }
3034
3035
3036 /* Handle an Expose or GraphicsExpose event on a scroll bar.
3037
3038 This may be called from a signal handler, so we have to ignore GC
3039 mark bits. */
3040 static void
3041 x_scroll_bar_expose (bar, event)
3042 struct scroll_bar *bar;
3043 XEvent *event;
3044 {
3045 Window w = SCROLL_BAR_X_WINDOW (bar);
3046 GC gc = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))->display.x->normal_gc;
3047
3048 BLOCK_INPUT;
3049
3050 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
3051
3052 /* Draw a one-pixel border just inside the edges of the scroll bar. */
3053 XDrawRectangle (x_current_display, w, gc,
3054
3055 /* x, y, width, height */
3056 0, 0, XINT (bar->width) - 1, XINT (bar->height) - 1);
3057
3058 UNBLOCK_INPUT;
3059 }
3060
3061 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
3062 is set to something other than no_event, it is enqueued.
3063
3064 This may be called from a signal handler, so we have to ignore GC
3065 mark bits. */
3066 static void
3067 x_scroll_bar_handle_click (bar, event, emacs_event)
3068 struct scroll_bar *bar;
3069 XEvent *event;
3070 struct input_event *emacs_event;
3071 {
3072 if (XGCTYPE (bar->window) != Lisp_Window)
3073 abort ();
3074
3075 emacs_event->kind = scroll_bar_click;
3076 emacs_event->code = event->xbutton.button - Button1;
3077 emacs_event->modifiers =
3078 (x_x_to_emacs_modifiers (event->xbutton.state)
3079 | (event->type == ButtonRelease
3080 ? up_modifier
3081 : down_modifier));
3082 emacs_event->frame_or_window = bar->window;
3083 emacs_event->timestamp = event->xbutton.time;
3084 {
3085 int internal_height =
3086 VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (XINT (bar->height));
3087 int top_range =
3088 VERTICAL_SCROLL_BAR_TOP_RANGE (XINT (bar->height));
3089 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
3090
3091 if (y < 0) y = 0;
3092 if (y > top_range) y = top_range;
3093
3094 if (y < XINT (bar->start))
3095 emacs_event->part = scroll_bar_above_handle;
3096 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
3097 emacs_event->part = scroll_bar_handle;
3098 else
3099 emacs_event->part = scroll_bar_below_handle;
3100
3101 /* Just because the user has clicked on the handle doesn't mean
3102 they want to drag it. Lisp code needs to be able to decide
3103 whether or not we're dragging. */
3104 #if 0
3105 /* If the user has just clicked on the handle, record where they're
3106 holding it. */
3107 if (event->type == ButtonPress
3108 && emacs_event->part == scroll_bar_handle)
3109 XSET (bar->dragging, Lisp_Int, y - XINT (bar->start));
3110 #endif
3111
3112 /* If the user has released the handle, set it to its final position. */
3113 if (event->type == ButtonRelease
3114 && ! NILP (bar->dragging))
3115 {
3116 int new_start = y - XINT (bar->dragging);
3117 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
3118
3119 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
3120 bar->dragging = Qnil;
3121 }
3122
3123 /* Same deal here as the other #if 0. */
3124 #if 0
3125 /* Clicks on the handle are always reported as occurring at the top of
3126 the handle. */
3127 if (emacs_event->part == scroll_bar_handle)
3128 emacs_event->x = bar->start;
3129 else
3130 XSET (emacs_event->x, Lisp_Int, y);
3131 #else
3132 XSET (emacs_event->x, Lisp_Int, y);
3133 #endif
3134
3135 XSET (emacs_event->y, Lisp_Int, top_range);
3136 }
3137 }
3138
3139 /* Handle some mouse motion while someone is dragging the scroll bar.
3140
3141 This may be called from a signal handler, so we have to ignore GC
3142 mark bits. */
3143 static void
3144 x_scroll_bar_note_movement (bar, event)
3145 struct scroll_bar *bar;
3146 XEvent *event;
3147 {
3148 last_mouse_movement_time = event->xmotion.time;
3149
3150 mouse_moved = 1;
3151 XSET (last_mouse_scroll_bar, Lisp_Vector, bar);
3152
3153 /* If we're dragging the bar, display it. */
3154 if (! GC_NILP (bar->dragging))
3155 {
3156 /* Where should the handle be now? */
3157 int new_start = event->xmotion.y - XINT (bar->dragging);
3158
3159 if (new_start != XINT (bar->start))
3160 {
3161 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
3162
3163 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
3164 }
3165 }
3166
3167 /* Call XQueryPointer so we'll get an event the next time the mouse
3168 moves and we can see *still* on the same position. */
3169 {
3170 int dummy;
3171
3172 XQueryPointer (event->xmotion.display, event->xmotion.window,
3173 (Window *) &dummy, (Window *) &dummy,
3174 &dummy, &dummy, &dummy, &dummy,
3175 (unsigned int *) &dummy);
3176 }
3177 }
3178
3179 /* Return information to the user about the current position of the mouse
3180 on the scroll bar. */
3181 static void
3182 x_scroll_bar_report_motion (f, bar_window, part, x, y, time)
3183 FRAME_PTR *f;
3184 Lisp_Object *bar_window;
3185 enum scroll_bar_part *part;
3186 Lisp_Object *x, *y;
3187 unsigned long *time;
3188 {
3189 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
3190 int win_x, win_y;
3191 Window dummy_window;
3192 int dummy_coord;
3193 unsigned int dummy_mask;
3194
3195 BLOCK_INPUT;
3196
3197 /* Get the mouse's position relative to the scroll bar window, and
3198 report that. */
3199 if (! XQueryPointer (x_current_display,
3200 SCROLL_BAR_X_WINDOW (bar),
3201
3202 /* Root, child, root x and root y. */
3203 &dummy_window, &dummy_window,
3204 &dummy_coord, &dummy_coord,
3205
3206 /* Position relative to scroll bar. */
3207 &win_x, &win_y,
3208
3209 /* Mouse buttons and modifier keys. */
3210 &dummy_mask))
3211 *f = 0;
3212 else
3213 {
3214 int inside_height
3215 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (XINT (bar->height));
3216 int top_range
3217 = VERTICAL_SCROLL_BAR_TOP_RANGE (XINT (bar->height));
3218
3219 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
3220
3221 if (! NILP (bar->dragging))
3222 win_y -= XINT (bar->dragging);
3223
3224 if (win_y < 0)
3225 win_y = 0;
3226 if (win_y > top_range)
3227 win_y = top_range;
3228
3229 *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3230 *bar_window = bar->window;
3231
3232 if (! NILP (bar->dragging))
3233 *part = scroll_bar_handle;
3234 else if (win_y < XINT (bar->start))
3235 *part = scroll_bar_above_handle;
3236 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
3237 *part = scroll_bar_handle;
3238 else
3239 *part = scroll_bar_below_handle;
3240
3241 XSET (*x, Lisp_Int, win_y);
3242 XSET (*y, Lisp_Int, top_range);
3243
3244 mouse_moved = 0;
3245 last_mouse_scroll_bar = Qnil;
3246 }
3247
3248 *time = last_mouse_movement_time;
3249
3250 UNBLOCK_INPUT;
3251 }
3252
3253
3254 /* The screen has been cleared so we may have changed foreground or
3255 background colors, and the scroll bars may need to be redrawn.
3256 Clear out the scroll bars, and ask for expose events, so we can
3257 redraw them. */
3258
3259 x_scroll_bar_clear (f)
3260 FRAME_PTR f;
3261 {
3262 Lisp_Object bar;
3263
3264 for (bar = FRAME_SCROLL_BARS (f);
3265 XTYPE (bar) == Lisp_Vector;
3266 bar = XSCROLL_BAR (bar)->next)
3267 XClearArea (x_current_display, SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
3268 0, 0, 0, 0, True);
3269 }
3270
3271 /* This processes Expose events from the menubar specific X event
3272 loop in menubar.c. This allows to redisplay the frame if necessary
3273 when handling menubar or popup items. */
3274
3275 void
3276 process_expose_from_menu (event)
3277 XEvent event;
3278 {
3279 FRAME_PTR f;
3280
3281 BLOCK_INPUT;
3282
3283 f = x_window_to_frame (event.xexpose.window);
3284 if (f)
3285 {
3286 if (f->async_visible == 0)
3287 {
3288 f->async_visible = 1;
3289 f->async_iconified = 0;
3290 SET_FRAME_GARBAGED (f);
3291 }
3292 else
3293 {
3294 dumprectangle (x_window_to_frame (event.xexpose.window),
3295 event.xexpose.x, event.xexpose.y,
3296 event.xexpose.width, event.xexpose.height);
3297 }
3298 }
3299 else
3300 {
3301 struct scroll_bar *bar
3302 = x_window_to_scroll_bar (event.xexpose.window);
3303
3304 if (bar)
3305 x_scroll_bar_expose (bar, &event);
3306 }
3307
3308 UNBLOCK_INPUT;
3309 }
3310
3311 \f
3312 /* The main X event-reading loop - XTread_socket. */
3313
3314 /* Timestamp of enter window event. This is only used by XTread_socket,
3315 but we have to put it out here, since static variables within functions
3316 sometimes don't work. */
3317 static Time enter_timestamp;
3318
3319 /* This holds the state XLookupString needs to implement dead keys
3320 and other tricks known as "compose processing". _X Window System_
3321 says that a portable program can't use this, but Stephen Gildea assures
3322 me that letting the compiler initialize it to zeros will work okay.
3323
3324 This must be defined outside of XTread_socket, for the same reasons
3325 given for enter_timestamp, above. */
3326 static XComposeStatus compose_status;
3327
3328 /* Communication with window managers. */
3329 Atom Xatom_wm_protocols;
3330
3331 /* Kinds of protocol things we may receive. */
3332 Atom Xatom_wm_take_focus;
3333 Atom Xatom_wm_save_yourself;
3334 Atom Xatom_wm_delete_window;
3335
3336 /* Other WM communication */
3337 Atom Xatom_wm_configure_denied; /* When our config request is denied */
3338 Atom Xatom_wm_window_moved; /* When the WM moves us. */
3339
3340 /* Window manager communication. */
3341 Atom Xatom_wm_change_state;
3342
3343 /* EditRes protocol */
3344 Atom Xatom_editres_name;
3345
3346 /* Record the last 100 characters stored
3347 to help debug the loss-of-chars-during-GC problem. */
3348 int temp_index;
3349 short temp_buffer[100];
3350
3351 /* Read events coming from the X server.
3352 This routine is called by the SIGIO handler.
3353 We return as soon as there are no more events to be read.
3354
3355 Events representing keys are stored in buffer BUFP,
3356 which can hold up to NUMCHARS characters.
3357 We return the number of characters stored into the buffer,
3358 thus pretending to be `read'.
3359
3360 WAITP is nonzero if we should block until input arrives.
3361 EXPECTED is nonzero if the caller knows input is available. */
3362
3363 int
3364 XTread_socket (sd, bufp, numchars, waitp, expected)
3365 register int sd;
3366 register struct input_event *bufp;
3367 register int numchars;
3368 int waitp;
3369 int expected;
3370 {
3371 int count = 0;
3372 int nbytes = 0;
3373 int mask;
3374 int items_pending; /* How many items are in the X queue. */
3375 XEvent event;
3376 struct frame *f;
3377 int event_found = 0;
3378 int prefix;
3379 Lisp_Object part;
3380
3381 if (interrupt_input_blocked)
3382 {
3383 interrupt_input_pending = 1;
3384 return -1;
3385 }
3386
3387 interrupt_input_pending = 0;
3388 BLOCK_INPUT;
3389
3390 if (numchars <= 0)
3391 abort (); /* Don't think this happens. */
3392
3393 #ifdef FIOSNBIO
3394 /* If available, Xlib uses FIOSNBIO to make the socket
3395 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
3396 FIOSNBIO is ignored, and instead of signalling EWOULDBLOCK,
3397 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
3398 fcntl (fileno (stdin), F_SETFL, 0);
3399 #endif /* ! defined (FIOSNBIO) */
3400
3401 #ifndef SIGIO
3402 #ifndef HAVE_SELECT
3403 if (! (fcntl (fileno (stdin), F_GETFL, 0) & O_NDELAY))
3404 {
3405 extern int read_alarm_should_throw;
3406 read_alarm_should_throw = 1;
3407 XPeekEvent (XDISPLAY &event);
3408 read_alarm_should_throw = 0;
3409 }
3410 #endif /* HAVE_SELECT */
3411 #endif /* SIGIO */
3412
3413 while (XStuffPending () != 0)
3414 {
3415 XNextEvent (XDISPLAY &event);
3416 event_found = 1;
3417
3418 switch (event.type)
3419 {
3420 #ifdef HAVE_X11
3421 case ClientMessage:
3422 {
3423 if (event.xclient.message_type == Xatom_wm_protocols
3424 && event.xclient.format == 32)
3425 {
3426 if (event.xclient.data.l[0] == Xatom_wm_take_focus)
3427 {
3428 f = x_window_to_frame (event.xclient.window);
3429 /* Since we set WM_TAKE_FOCUS, we must call
3430 XSetInputFocus explicitly. But not if f is null,
3431 since that might be an event for a deleted frame. */
3432 if (f)
3433 XSetInputFocus (event.xclient.display,
3434 event.xclient.window,
3435 RevertToPointerRoot,
3436 event.xclient.data.l[1]);
3437 /* Not certain about handling scroll bars here */
3438 }
3439 else if (event.xclient.data.l[0] == Xatom_wm_save_yourself)
3440 {
3441 /* Save state modify the WM_COMMAND property to
3442 something which can reinstate us. This notifies
3443 the session manager, who's looking for such a
3444 PropertyNotify. Can restart processing when
3445 a keyboard or mouse event arrives. */
3446 if (numchars > 0)
3447 {
3448 /* This is just so we only give real data once
3449 for a single Emacs process. */
3450 if (x_top_window_to_frame (event.xclient.window)
3451 == selected_frame)
3452 XSetCommand (x_current_display,
3453 event.xclient.window,
3454 initial_argv, initial_argc);
3455 else
3456 XSetCommand (x_current_display,
3457 event.xclient.window,
3458 0, 0);
3459 }
3460 }
3461 else if (event.xclient.data.l[0] == Xatom_wm_delete_window)
3462 {
3463 struct frame *f = x_any_window_to_frame (event.xclient.window);
3464
3465 if (f)
3466 {
3467 if (numchars == 0)
3468 abort ();
3469
3470 bufp->kind = delete_window_event;
3471 XSET (bufp->frame_or_window, Lisp_Frame, f);
3472 bufp++;
3473
3474 count += 1;
3475 numchars -= 1;
3476 }
3477 }
3478 }
3479 else if (event.xclient.message_type == Xatom_wm_configure_denied)
3480 {
3481 }
3482 else if (event.xclient.message_type == Xatom_wm_window_moved)
3483 {
3484 int new_x, new_y;
3485 struct frame *f = x_window_to_frame (event.xclient.window);
3486
3487 new_x = event.xclient.data.s[0];
3488 new_y = event.xclient.data.s[1];
3489
3490 if (f)
3491 {
3492 f->display.x->left_pos = new_x;
3493 f->display.x->top_pos = new_y;
3494 }
3495 }
3496 #if defined (USE_X_TOOLKIT) && defined (HAVE_X11R5)
3497 else if (event.xclient.message_type == Xatom_editres_name)
3498 {
3499 struct frame *f = x_any_window_to_frame (event.xclient.window);
3500 _XEditResCheckMessages (f->display.x->widget, NULL, &event, NULL);
3501 }
3502 #endif /* USE_X_TOOLKIT and HAVE_X11R5 */
3503 }
3504 break;
3505
3506 case SelectionNotify:
3507 #ifdef USE_X_TOOLKIT
3508 if (! x_window_to_frame (event.xselection.requestor))
3509 goto OTHER;
3510 #endif /* not USE_X_TOOLKIT */
3511 x_handle_selection_notify (&event);
3512 break;
3513
3514 case SelectionClear: /* Someone has grabbed ownership. */
3515 #ifdef USE_X_TOOLKIT
3516 if (! x_window_to_frame (event.xselectionclear.window))
3517 goto OTHER;
3518 #endif /* USE_X_TOOLKIT */
3519 {
3520 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
3521
3522 if (numchars == 0)
3523 abort ();
3524
3525 bufp->kind = selection_clear_event;
3526 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
3527 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
3528 SELECTION_EVENT_TIME (bufp) = eventp->time;
3529 bufp++;
3530
3531 count += 1;
3532 numchars -= 1;
3533 }
3534 break;
3535
3536 case SelectionRequest: /* Someone wants our selection. */
3537 #ifdef USE_X_TOOLKIT
3538 if (!x_window_to_frame (event.xselectionrequest.owner))
3539 goto OTHER;
3540 #endif /* USE_X_TOOLKIT */
3541 {
3542 XSelectionRequestEvent *eventp = (XSelectionRequestEvent *) &event;
3543
3544 if (numchars == 0)
3545 abort ();
3546
3547 bufp->kind = selection_request_event;
3548 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
3549 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
3550 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
3551 SELECTION_EVENT_TARGET (bufp) = eventp->target;
3552 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
3553 SELECTION_EVENT_TIME (bufp) = eventp->time;
3554 bufp++;
3555
3556 count += 1;
3557 numchars -= 1;
3558 }
3559 break;
3560
3561 case PropertyNotify:
3562 #ifdef USE_X_TOOLKIT
3563 if (!x_any_window_to_frame (event.xproperty.window))
3564 goto OTHER;
3565 #endif /* not USE_X_TOOLKIT */
3566 x_handle_property_notify (&event);
3567 break;
3568
3569 case ReparentNotify:
3570 f = x_top_window_to_frame (event.xreparent.window);
3571 if (f)
3572 {
3573 int x, y;
3574 f->display.x->parent_desc = event.xreparent.parent;
3575 x_real_positions (f, &x, &y);
3576 f->display.x->left_pos = x;
3577 f->display.x->top_pos = y;
3578 }
3579 break;
3580
3581 case Expose:
3582 f = x_window_to_frame (event.xexpose.window);
3583 if (f)
3584 {
3585 if (f->async_visible == 0)
3586 {
3587 f->async_visible = 1;
3588 f->async_iconified = 0;
3589 SET_FRAME_GARBAGED (f);
3590 }
3591 else
3592 dumprectangle (x_window_to_frame (event.xexpose.window),
3593 event.xexpose.x, event.xexpose.y,
3594 event.xexpose.width, event.xexpose.height);
3595 }
3596 else
3597 {
3598 struct scroll_bar *bar
3599 = x_window_to_scroll_bar (event.xexpose.window);
3600
3601 if (bar)
3602 x_scroll_bar_expose (bar, &event);
3603 #ifdef USE_X_TOOLKIT
3604 else
3605 goto OTHER;
3606 #endif /* USE_X_TOOLKIT */
3607 }
3608 break;
3609
3610 case GraphicsExpose: /* This occurs when an XCopyArea's
3611 source area was obscured or not
3612 available.*/
3613 f = x_window_to_frame (event.xgraphicsexpose.drawable);
3614 if (f)
3615 {
3616 dumprectangle (f,
3617 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
3618 event.xgraphicsexpose.width,
3619 event.xgraphicsexpose.height);
3620 }
3621 #ifdef USE_X_TOOLKIT
3622 else
3623 goto OTHER;
3624 #endif /* USE_X_TOOLKIT */
3625 break;
3626
3627 case NoExpose: /* This occurs when an XCopyArea's
3628 source area was completely
3629 available */
3630 break;
3631 #else /* ! defined (HAVE_X11) */
3632 case ExposeWindow:
3633 if (event.subwindow != 0)
3634 break; /* duplicate event */
3635 f = x_window_to_frame (event.window);
3636 if (event.window == f->display.x->icon_desc)
3637 {
3638 refreshicon (f);
3639 f->async_iconified = 1;
3640 }
3641 if (event.window == FRAME_X_WINDOW (f))
3642 {
3643 /* Say must check all windows' needs_exposure flags. */
3644 expose_all_windows = 1;
3645 f->display.x->needs_exposure = 1;
3646 f->async_visible = 1;
3647 }
3648 break;
3649
3650 case ExposeRegion:
3651 if (event.subwindow != 0)
3652 break; /* duplicate event */
3653 f = x_window_to_frame (event.window);
3654 if (event.window == f->display.x->icon_desc)
3655 {
3656 refreshicon (f);
3657 break;
3658 }
3659 /* If window already needs full redraw, ignore this rectangle. */
3660 if (expose_all_windows && f->display.x->needs_exposure)
3661 break;
3662 /* Put the event on the queue of rectangles to redraw. */
3663 if (enqueue_event (&event, &x_expose_queue))
3664 /* If it is full, we can't record the rectangle,
3665 so redraw this entire window. */
3666 {
3667 /* Say must check all windows' needs_exposure flags. */
3668 expose_all_windows = 1;
3669 f->display.x->needs_exposure = 1;
3670 }
3671 break;
3672
3673 case ExposeCopy:
3674 /* This should happen only when we are expecting it,
3675 in x_read_exposes. */
3676 abort ();
3677 #endif /* ! defined (HAVE_X11) */
3678
3679 #ifdef HAVE_X11
3680 case UnmapNotify:
3681 f = x_any_window_to_frame (event.xunmap.window);
3682 if (f) /* F may no longer exist if
3683 the frame was deleted. */
3684 {
3685 /* While a frame is unmapped, display generation is
3686 disabled; you don't want to spend time updating a
3687 display that won't ever be seen. */
3688 f->async_visible = 0;
3689 /* We can't distinguish, from the event, whether the window
3690 has become iconified or invisible. So assume, if it
3691 was previously visible, than now it is iconified.
3692 We depend on x_make_frame_invisible to mark it iconified. */
3693 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
3694 f->async_iconified = 1;
3695 }
3696 #ifdef USE_X_TOOLKIT
3697 goto OTHER;
3698 #endif /* USE_X_TOOLKIT */
3699 break;
3700
3701 case MapNotify:
3702 /* We use x_top_window_to_frame because map events can come
3703 for subwindows and they don't mean that the frame is visible. */
3704 f = x_top_window_to_frame (event.xmap.window);
3705 if (f)
3706 {
3707 f->async_visible = 1;
3708 f->async_iconified = 0;
3709
3710 /* wait_reading_process_input will notice this and update
3711 the frame's display structures. */
3712 SET_FRAME_GARBAGED (f);
3713 }
3714 #ifdef USE_X_TOOLKIT
3715 goto OTHER;
3716 #endif /* USE_X_TOOLKIT */
3717 break;
3718
3719 /* Turn off processing if we become fully obscured. */
3720 case VisibilityNotify:
3721 break;
3722
3723 #else /* ! defined (HAVE_X11) */
3724 case UnmapWindow:
3725 f = x_window_to_frame (event.window);
3726 if (event.window == f->display.x->icon_desc)
3727 f->async_iconified = 0;
3728 if (event.window == FRAME_X_WINDOW (f))
3729 f->async_visible = 0;
3730 break;
3731 #endif /* ! defined (HAVE_X11) */
3732
3733 #ifdef HAVE_X11
3734 case KeyPress:
3735 f = x_any_window_to_frame (event.xkey.window);
3736
3737 if (f != 0)
3738 {
3739 KeySym keysym, orig_keysym;
3740 /* al%imercury@uunet.uu.net says that making this 81 instead of
3741 80 fixed a bug whereby meta chars made his Emacs hang. */
3742 unsigned char copy_buffer[81];
3743 int modifiers;
3744
3745 event.xkey.state
3746 |= x_emacs_to_x_modifiers (extra_keyboard_modifiers);
3747 modifiers = event.xkey.state;
3748
3749 /* This will have to go some day... */
3750
3751 /* make_lispy_event turns chars into control chars.
3752 Don't do it here because XLookupString is too eager. */
3753 event.xkey.state &= ~ControlMask;
3754 nbytes =
3755 XLookupString (&event.xkey, copy_buffer, 80, &keysym,
3756 &compose_status);
3757
3758 orig_keysym = keysym;
3759
3760 if (numchars > 1)
3761 {
3762 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
3763 || keysym == XK_Delete
3764 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
3765 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
3766 #ifdef HPUX
3767 /* This recognizes the "extended function keys".
3768 It seems there's no cleaner way.
3769 Test IsModifierKey to avoid handling mode_switch
3770 incorrectly. */
3771 || ((unsigned) (keysym) >= XK_Select
3772 && (unsigned)(keysym) < XK_KP_Space)
3773 #endif
3774 #ifdef XK_dead_circumflex
3775 || orig_keysym == XK_dead_circumflex
3776 #endif
3777 #ifdef XK_dead_grave
3778 || orig_keysym == XK_dead_grave
3779 #endif
3780 #ifdef XK_dead_tilde
3781 || orig_keysym == XK_dead_tilde
3782 #endif
3783 #ifdef XK_dead_diaeresis
3784 || orig_keysym == XK_dead_diaeresis
3785 #endif
3786 #ifdef XK_dead_macron
3787 || orig_keysym == XK_dead_macron
3788 #endif
3789 #ifdef XK_dead_degree
3790 || orig_keysym == XK_dead_degree
3791 #endif
3792 #ifdef XK_dead_acute
3793 || orig_keysym == XK_dead_acute
3794 #endif
3795 #ifdef XK_dead_cedilla
3796 || orig_keysym == XK_dead_cedilla
3797 #endif
3798 #ifdef XK_dead_breve
3799 || orig_keysym == XK_dead_breve
3800 #endif
3801 #ifdef XK_dead_ogonek
3802 || orig_keysym == XK_dead_ogonek
3803 #endif
3804 #ifdef XK_dead_caron
3805 || orig_keysym == XK_dead_caron
3806 #endif
3807 #ifdef XK_dead_doubleacute
3808 || orig_keysym == XK_dead_doubleacute
3809 #endif
3810 #ifdef XK_dead_abovedot
3811 || orig_keysym == XK_dead_abovedot
3812 #endif
3813 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
3814 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
3815 /* Any "vendor-specific" key is ok. */
3816 || (orig_keysym & (1 << 28)))
3817 && ! (IsModifierKey (orig_keysym)
3818 #ifndef HAVE_X11R5
3819 #ifdef XK_Mode_switch
3820 || ((unsigned)(orig_keysym) == XK_Mode_switch)
3821 #endif
3822 #ifdef XK_Num_Lock
3823 || ((unsigned)(orig_keysym) == XK_Num_Lock)
3824 #endif
3825 #endif /* not HAVE_X11R5 */
3826 ))
3827 {
3828 if (temp_index == sizeof temp_buffer / sizeof (short))
3829 temp_index = 0;
3830 temp_buffer[temp_index++] = keysym;
3831 bufp->kind = non_ascii_keystroke;
3832 bufp->code = keysym;
3833 XSET (bufp->frame_or_window, Lisp_Frame, f);
3834 bufp->modifiers = x_x_to_emacs_modifiers (modifiers);
3835 bufp->timestamp = event.xkey.time;
3836 bufp++;
3837 count++;
3838 numchars--;
3839 }
3840 else if (numchars > nbytes)
3841 {
3842 register int i;
3843
3844 for (i = 0; i < nbytes; i++)
3845 {
3846 if (temp_index == sizeof temp_buffer / sizeof (short))
3847 temp_index = 0;
3848 temp_buffer[temp_index++] = copy_buffer[i];
3849 bufp->kind = ascii_keystroke;
3850 bufp->code = copy_buffer[i];
3851 XSET (bufp->frame_or_window, Lisp_Frame, f);
3852 bufp->modifiers = x_x_to_emacs_modifiers (modifiers);
3853 bufp->timestamp = event.xkey.time;
3854 bufp++;
3855 }
3856
3857 count += nbytes;
3858 numchars -= nbytes;
3859 }
3860 else
3861 abort ();
3862 }
3863 else
3864 abort ();
3865 }
3866 break;
3867 #else /* ! defined (HAVE_X11) */
3868 case KeyPressed:
3869 {
3870 register char *where_mapping;
3871
3872 f = x_window_to_frame (event.window);
3873 /* Ignore keys typed on icon windows. */
3874 if (f != 0 && event.window == f->display.x->icon_desc)
3875 break;
3876 where_mapping = XLookupMapping (&event, &nbytes);
3877 /* Nasty fix for arrow keys */
3878 if (!nbytes && IsCursorKey (event.detail & 0xff))
3879 {
3880 switch (event.detail & 0xff)
3881 {
3882 case KC_CURSOR_LEFT:
3883 where_mapping = "\002";
3884 break;
3885 case KC_CURSOR_RIGHT:
3886 where_mapping = "\006";
3887 break;
3888 case KC_CURSOR_UP:
3889 where_mapping = "\020";
3890 break;
3891 case KC_CURSOR_DOWN:
3892 where_mapping = "\016";
3893 break;
3894 }
3895 nbytes = 1;
3896 }
3897 if (numchars - nbytes > 0)
3898 {
3899 register int i;
3900
3901 for (i = 0; i < nbytes; i++)
3902 {
3903 bufp->kind = ascii_keystroke;
3904 bufp->code = where_mapping[i];
3905 XSET (bufp->time, Lisp_Int, event.xkey.time);
3906 XSET (bufp->frame_or_window, Lisp_Frame, f);
3907 bufp++;
3908 }
3909 count += nbytes;
3910 numchars -= nbytes;
3911 }
3912 }
3913 break;
3914 #endif /* ! defined (HAVE_X11) */
3915
3916 #ifdef HAVE_X11
3917
3918 /* Here's a possible interpretation of the whole
3919 FocusIn-EnterNotify FocusOut-LeaveNotify mess. If you get a
3920 FocusIn event, you have to get a FocusOut event before you
3921 relinquish the focus. If you haven't received a FocusIn event,
3922 then a mere LeaveNotify is enough to free you. */
3923
3924 case EnterNotify:
3925 f = x_any_window_to_frame (event.xcrossing.window);
3926
3927 if (event.xcrossing.focus) /* Entered Window */
3928 {
3929 /* Avoid nasty pop/raise loops. */
3930 if (f && (!(f->auto_raise)
3931 || !(f->auto_lower)
3932 || (event.xcrossing.time - enter_timestamp) > 500))
3933 {
3934 x_new_focus_frame (f);
3935 enter_timestamp = event.xcrossing.time;
3936 }
3937 }
3938 else if (f == x_focus_frame)
3939 x_new_focus_frame (0);
3940 /* EnterNotify counts as mouse movement,
3941 so update things that depend on mouse position. */
3942 if (f)
3943 note_mouse_movement (f, &event.xmotion);
3944 #ifdef USE_X_TOOLKIT
3945 goto OTHER;
3946 #endif /* USE_X_TOOLKIT */
3947 break;
3948
3949 case FocusIn:
3950 f = x_any_window_to_frame (event.xfocus.window);
3951 if (event.xfocus.detail != NotifyPointer)
3952 x_focus_event_frame = f;
3953 if (f)
3954 x_new_focus_frame (f);
3955 #ifdef USE_X_TOOLKIT
3956 goto OTHER;
3957 #endif /* USE_X_TOOLKIT */
3958 break;
3959
3960
3961 case LeaveNotify:
3962 f = x_top_window_to_frame (event.xcrossing.window);
3963 if (f)
3964 {
3965 if (f == mouse_face_mouse_frame)
3966 /* If we move outside the frame,
3967 then we're certainly no longer on any text in the frame. */
3968 clear_mouse_face ();
3969
3970 if (event.xcrossing.focus)
3971 {
3972 if (! x_focus_event_frame)
3973 x_new_focus_frame (0);
3974 else
3975 x_new_focus_frame (f);
3976 }
3977 else
3978 {
3979 if (f == x_focus_event_frame)
3980 x_focus_event_frame = 0;
3981 if (f == x_focus_frame)
3982 x_new_focus_frame (0);
3983 }
3984 }
3985 #ifdef USE_X_TOOLKIT
3986 goto OTHER;
3987 #endif /* USE_X_TOOLKIT */
3988 break;
3989
3990 case FocusOut:
3991 f = x_any_window_to_frame (event.xfocus.window);
3992 if (event.xfocus.detail != NotifyPointer
3993 && f == x_focus_event_frame)
3994 x_focus_event_frame = 0;
3995 if (f && f == x_focus_frame)
3996 x_new_focus_frame (0);
3997 #ifdef USE_X_TOOLKIT
3998 goto OTHER;
3999 #endif /* USE_X_TOOLKIT */
4000 break;
4001
4002 #else /* ! defined (HAVE_X11) */
4003
4004 case EnterWindow:
4005 if ((event.detail & 0xFF) == 1)
4006 break; /* Coming from our own subwindow */
4007 if (event.subwindow != 0)
4008 break; /* Entering our own subwindow. */
4009
4010 {
4011 f = x_window_to_frame (event.window);
4012 x_mouse_frame = f;
4013
4014 x_new_focus_frame (f);
4015 }
4016 break;
4017
4018 case LeaveWindow:
4019 if ((event.detail & 0xFF) == 1)
4020 break; /* Entering our own subwindow */
4021 if (event.subwindow != 0)
4022 break; /* Leaving our own subwindow. */
4023
4024 x_mouse_frame = 0;
4025 if (x_focus_frame == 0
4026 && x_input_frame != 0
4027 && x_input_frame == x_window_to_frame (event.window)
4028 && event.window == FRAME_X_WINDOW (x_input_frame))
4029 {
4030 f = x_input_frame;
4031 x_input_frame = 0;
4032 if (f)
4033 frame_unhighlight (f);
4034 }
4035 break;
4036 #endif /* ! defined (HAVE_X11) */
4037
4038 #ifdef HAVE_X11
4039 case MotionNotify:
4040 {
4041 if (x_mouse_grabbed && FRAME_LIVE_P (last_mouse_frame))
4042 f = last_mouse_frame;
4043 else
4044 f = x_window_to_frame (event.xmotion.window);
4045 if (f)
4046 note_mouse_movement (f, &event.xmotion);
4047 else
4048 {
4049 struct scroll_bar *bar
4050 = x_window_to_scroll_bar (event.xmotion.window);
4051
4052 if (bar)
4053 x_scroll_bar_note_movement (bar, &event);
4054
4055 /* If we move outside the frame,
4056 then we're certainly no longer on any text in the frame. */
4057 clear_mouse_face ();
4058 }
4059 }
4060 #if 0 /* This should be unnecessary, since the toolkit has no use
4061 for motion events that happen outside of the menu event loop,
4062 and it seems to cause the bug that mouse events stop coming
4063 after a while. */
4064 #ifdef USE_X_TOOLKIT
4065 goto OTHER;
4066 #endif /* USE_X_TOOLKIT */
4067 #endif
4068 break;
4069
4070 case ConfigureNotify:
4071 f = x_any_window_to_frame (event.xconfigure.window);
4072 #ifdef USE_X_TOOLKIT
4073 if (f
4074 #if 0
4075 && ! event.xconfigure.send_event
4076 #endif
4077 && (event.xconfigure.window == XtWindow (f->display.x->widget)))
4078 {
4079 Window win, child;
4080 int win_x, win_y;
4081
4082 /* Find the position of the outside upper-left corner of
4083 the window, in the root coordinate system. Don't
4084 refer to the parent window here; we may be processing
4085 this event after the window manager has changed our
4086 parent, but before we have reached the ReparentNotify. */
4087 XTranslateCoordinates (x_current_display,
4088
4089 /* From-window, to-window. */
4090 XtWindow (f->display.x->widget),
4091 ROOT_WINDOW,
4092
4093 /* From-position, to-position. */
4094 -event.xconfigure.border_width,
4095 -event.xconfigure.border_width,
4096 &win_x, &win_y,
4097
4098 /* Child of win. */
4099 &child);
4100 event.xconfigure.x = win_x;
4101 event.xconfigure.y = win_y;
4102
4103 f->display.x->pixel_width = event.xconfigure.width;
4104 f->display.x->pixel_height = event.xconfigure.height;
4105 f->display.x->left_pos = event.xconfigure.x;
4106 f->display.x->top_pos = event.xconfigure.y;
4107
4108 /* What we have now is the position of Emacs's own window.
4109 Convert that to the position of the window manager window. */
4110 {
4111 int x, y;
4112 x_real_positions (f, &x, &y);
4113 f->display.x->left_pos = x;
4114 f->display.x->top_pos = y;
4115 }
4116 }
4117 goto OTHER;
4118 #else /* not USE_X_TOOLKIT */
4119 if (f)
4120 {
4121 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
4122 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
4123
4124 /* Even if the number of character rows and columns has
4125 not changed, the font size may have changed, so we need
4126 to check the pixel dimensions as well. */
4127 if (columns != f->width
4128 || rows != f->height
4129 || event.xconfigure.width != f->display.x->pixel_width
4130 || event.xconfigure.height != f->display.x->pixel_height)
4131 {
4132 change_frame_size (f, rows, columns, 0, 1);
4133 SET_FRAME_GARBAGED (f);
4134 }
4135
4136 if (! event.xconfigure.send_event)
4137 {
4138 Window win, child;
4139 int win_x, win_y;
4140
4141 /* Find the position of the outside upper-left corner of
4142 the window, in the root coordinate system. Don't
4143 refer to the parent window here; we may be processing
4144 this event after the window manager has changed our
4145 parent, but before we have reached the ReparentNotify. */
4146 XTranslateCoordinates (x_current_display,
4147
4148 /* From-window, to-window. */
4149 f->display.x->window_desc,
4150 ROOT_WINDOW,
4151
4152 /* From-position, to-position. */
4153 -event.xconfigure.border_width,
4154 -event.xconfigure.border_width,
4155 &win_x, &win_y,
4156
4157 /* Child of win. */
4158 &child);
4159 event.xconfigure.x = win_x;
4160 event.xconfigure.y = win_y;
4161 }
4162
4163 f->display.x->pixel_width = event.xconfigure.width;
4164 f->display.x->pixel_height = event.xconfigure.height;
4165 f->display.x->left_pos = event.xconfigure.x;
4166 f->display.x->top_pos = event.xconfigure.y;
4167
4168 /* What we have now is the position of Emacs's own window.
4169 Convert that to the position of the window manager window. */
4170 {
4171 int x, y;
4172 x_real_positions (f, &x, &y);
4173 f->display.x->left_pos = x;
4174 f->display.x->top_pos = y;
4175 if (y != event.xconfigure.y)
4176 {
4177 /* Since the WM decorations come below top_pos now,
4178 we must put them below top_pos in the future. */
4179 f->display.x->win_gravity = NorthWestGravity;
4180 x_wm_set_size_hint (f, 0, 0);
4181 }
4182 }
4183 }
4184 #endif /* not USE_X_TOOLKIT */
4185 break;
4186
4187 case ButtonPress:
4188 case ButtonRelease:
4189 {
4190 /* If we decide we want to generate an event to be seen
4191 by the rest of Emacs, we put it here. */
4192 struct input_event emacs_event;
4193 emacs_event.kind = no_event;
4194
4195 bzero (&compose_status, sizeof (compose_status));
4196
4197 f = x_window_to_frame (event.xbutton.window);
4198 if (f)
4199 {
4200 if (!x_focus_frame || (f == x_focus_frame))
4201 construct_mouse_click (&emacs_event, &event, f);
4202 }
4203 else
4204 {
4205 struct scroll_bar *bar =
4206 x_window_to_scroll_bar (event.xbutton.window);
4207
4208 if (bar)
4209 x_scroll_bar_handle_click (bar, &event, &emacs_event);
4210 #ifdef USE_X_TOOLKIT
4211 else
4212 {
4213 f = x_any_window_to_frame (event.xbutton.window);
4214 if (f && event.type == ButtonPress)
4215 construct_menu_click (&emacs_event,
4216 &event, f);
4217 }
4218 #endif /* USE_X_TOOLKIT */
4219 }
4220
4221 if (numchars >= 1 && emacs_event.kind != no_event)
4222 {
4223 bcopy (&emacs_event, bufp, sizeof (struct input_event));
4224 bufp++;
4225 count++;
4226 numchars--;
4227 }
4228
4229 #ifdef USE_X_TOOLKIT
4230 goto OTHER;
4231 #endif /* USE_X_TOOLKIT */
4232 }
4233 break;
4234
4235 #else /* ! defined (HAVE_X11) */
4236 case ButtonPressed:
4237 case ButtonReleased:
4238 f = x_window_to_frame (event.window);
4239 if (f)
4240 {
4241 if (event.window == f->display.x->icon_desc)
4242 {
4243 x_make_frame_visible (f);
4244
4245 if (warp_mouse_on_deiconify)
4246 XWarpMouse (FRAME_X_WINDOW (f), 10, 10);
4247 break;
4248 }
4249 if (event.window == FRAME_X_WINDOW (f))
4250 {
4251 if (f->auto_raise)
4252 x_raise_frame (f);
4253 }
4254 }
4255 enqueue_event (&event, &x_mouse_queue);
4256 if (numchars >= 2)
4257 {
4258 bufp->kind = ascii_keystroke;
4259 bufp->code = 'X' & 037; /* C-x */
4260 XSET (bufp->frame_or_window, Lisp_Frame, f);
4261 XSET (bufp->time, Lisp_Int, event.xkey.time);
4262 bufp++;
4263
4264 bufp->kind = ascii_keystroke;
4265 bufp->code = 0; /* C-@ */
4266 XSET (bufp->frame_or_window, Lisp_Frame, f);
4267 XSET (bufp->time, Lisp_Int, event.xkey.time);
4268 bufp++;
4269
4270 count += 2;
4271 numchars -= 2;
4272 }
4273 break;
4274 #endif /* ! defined (HAVE_X11) */
4275
4276 #ifdef HAVE_X11
4277
4278 case CirculateNotify:
4279 break;
4280 case CirculateRequest:
4281 break;
4282
4283 #endif /* ! defined (HAVE_X11) */
4284
4285 case MappingNotify:
4286 /* Someone has changed the keyboard mapping - update the
4287 local cache. */
4288 switch (event.xmapping.request)
4289 {
4290 case MappingModifier:
4291 x_find_modifier_meanings ();
4292 /* This is meant to fall through. */
4293 case MappingKeyboard:
4294 XRefreshKeyboardMapping (&event.xmapping);
4295 }
4296 #ifdef USE_X_TOOLKIT
4297 goto OTHER;
4298 #endif /* USE_X_TOOLKIT */
4299 break;
4300
4301 default:
4302 #ifdef USE_X_TOOLKIT
4303 OTHER:
4304 BLOCK_INPUT;
4305 XtDispatchEvent (&event);
4306 UNBLOCK_INPUT;
4307 #endif /* USE_X_TOOLKIT */
4308 break;
4309 }
4310 }
4311
4312 #ifdef X_IO_BUG
4313 if (! event_found)
4314 /* On some systems, an X bug causes Emacs to get no more events
4315 when the window is destroyed. Detect that. (1994.) */
4316 XNoOp (x_current_display);
4317 #endif /* X_IO_BUG */
4318
4319 #if 0 /* This fails for serial-line connections to the X server,
4320 because the characters arrive one by one, and a partial
4321 command makes select return but gives nothing to read.
4322 We'll have to hope that the bug that this tried to fix
4323 in 1988 has been fixed in Xlib or the X server. */
4324 #ifdef HAVE_SELECT
4325 if (expected && ! event_found)
4326 {
4327 /* AOJ 880406: if select returns true but XPending doesn't, it means that
4328 there is an EOF condition; in other words, that X has died.
4329 Act as if there had been a hangup. */
4330 int fd = ConnectionNumber (x_current_display);
4331 SELECT_TYPE mask, junk1, junk2;
4332 EMACS_TIME timeout;
4333
4334 FD_ZERO (&mask);
4335 FD_SET (fd, &mask);
4336 EMACS_SET_SECS_USECS (timeout, 0, 0);
4337 FD_ZERO (&junk1);
4338 FD_ZERO (&junk2);
4339 if (0 != select (fd + 1, &mask, &junk1, &junk2, &timeout)
4340 && !XStuffPending ())
4341 kill (getpid (), SIGHUP);
4342 }
4343 #endif /* HAVE_SELECT */
4344 #endif /* 0 */
4345
4346 #ifndef HAVE_X11
4347 if (updating_frame == 0)
4348 x_do_pending_expose ();
4349 #endif
4350
4351 /* If the focus was just given to an autoraising frame,
4352 raise it now. */
4353 #ifdef HAVE_X11
4354 if (pending_autoraise_frame)
4355 {
4356 x_raise_frame (pending_autoraise_frame);
4357 pending_autoraise_frame = 0;
4358 }
4359 #endif
4360
4361 UNBLOCK_INPUT;
4362 return count;
4363 }
4364
4365 #ifndef HAVE_X11
4366 /* Read and process only Expose events
4367 until we get an ExposeCopy event; then return.
4368 This is used in insert/delete line.
4369 We assume input is already blocked. */
4370
4371 static void
4372 x_read_exposes ()
4373 {
4374 struct frame *f;
4375 XKeyPressedEvent event;
4376
4377 while (1)
4378 {
4379 /* while there are more events*/
4380 XMaskEvent (ExposeWindow | ExposeRegion | ExposeCopy, &event);
4381 switch (event.type)
4382 {
4383 case ExposeWindow:
4384 if (event.subwindow != 0)
4385 break; /* duplicate event */
4386 f = x_window_to_frame (event.window);
4387 if (event.window == f->display.x->icon_desc)
4388 {
4389 refreshicon (f);
4390 break;
4391 }
4392 if (event.window == FRAME_X_WINDOW (f))
4393 {
4394 expose_all_windows = 1;
4395 f->display.x->needs_exposure = 1;
4396 break;
4397 }
4398 break;
4399
4400 case ExposeRegion:
4401 if (event.subwindow != 0)
4402 break; /* duplicate event */
4403 f = x_window_to_frame (event.window);
4404 if (event.window == f->display.x->icon_desc)
4405 {
4406 refreshicon (f);
4407 break;
4408 }
4409 /* If window already needs full redraw, ignore this rectangle. */
4410 if (expose_all_windows && f->display.x->needs_exposure)
4411 break;
4412 /* Put the event on the queue of rectangles to redraw. */
4413 if (enqueue_event (&event, &x_expose_queue))
4414 /* If it is full, we can't record the rectangle,
4415 so redraw this entire window. */
4416 {
4417 /* Say must check all windows' needs_exposure flags. */
4418 expose_all_windows = 1;
4419 f->display.x->needs_exposure = 1;
4420 }
4421 break;
4422
4423 case ExposeCopy:
4424 return;
4425 }
4426 }
4427 }
4428 #endif /* HAVE_X11 */
4429
4430 \f
4431 /* Drawing the cursor. */
4432
4433
4434 /* Draw a hollow box cursor. Don't change the inside of the box. */
4435
4436 static void
4437 x_draw_box (f)
4438 struct frame *f;
4439 {
4440 int left = CHAR_TO_PIXEL_COL (f, f->cursor_x);
4441 int top = CHAR_TO_PIXEL_ROW (f, f->cursor_y);
4442 int width = FONT_WIDTH (f->display.x->font);
4443 int height = f->display.x->line_height;
4444
4445 #ifdef HAVE_X11
4446 XDrawRectangle (x_current_display, FRAME_X_WINDOW (f),
4447 f->display.x->cursor_gc,
4448 left, top, width - 1, height - 1);
4449 #else /* ! defined (HAVE_X11) */
4450 XPixSet (FRAME_X_WINDOW (f),
4451 left, top, width, 1,
4452 f->display.x->cursor_pixel);
4453
4454 XPixSet (FRAME_X_WINDOW (f),
4455 left, top, 1, height,
4456 f->display.x->cursor_pixel);
4457
4458 XPixSet (FRAME_X_WINDOW (f),
4459 left+width-1, top, 1, height,
4460 f->display.x->cursor_pixel);
4461
4462 XPixSet (FRAME_X_WINDOW (f),
4463 left, top+height-1, width, 1,
4464 f->display.x->cursor_pixel);
4465 #endif /* ! defined (HAVE_X11) */
4466 }
4467
4468 /* Clear the cursor of frame F to background color,
4469 and mark the cursor as not shown.
4470 This is used when the text where the cursor is
4471 is about to be rewritten. */
4472
4473 static void
4474 clear_cursor (f)
4475 struct frame *f;
4476 {
4477 int mask;
4478
4479 if (! FRAME_VISIBLE_P (f)
4480 || f->phys_cursor_x < 0)
4481 return;
4482
4483 #ifdef HAVE_X11
4484 x_display_cursor (f, 0);
4485 #else /* ! defined (HAVE_X11) */
4486 XPixSet (FRAME_X_WINDOW (f),
4487 CHAR_TO_PIXEL_COL (f, f->phys_cursor_x),
4488 CHAR_TO_PIXEL_ROW (f, f->phys_cursor_y),
4489 FONT_WIDTH (f->display.x->font), f->display.x->line_height,
4490 f->display.x->background_pixel);
4491 #endif /* ! defined (HAVE_X11) */
4492 f->phys_cursor_x = -1;
4493 }
4494
4495 /* Redraw the glyph at ROW, COLUMN on frame F, in the style
4496 HIGHLIGHT. HIGHLIGHT is as defined for dumpglyphs. Return the
4497 glyph drawn. */
4498
4499 static void
4500 x_draw_single_glyph (f, row, column, glyph, highlight)
4501 struct frame *f;
4502 int row, column;
4503 GLYPH glyph;
4504 int highlight;
4505 {
4506 dumpglyphs (f,
4507 CHAR_TO_PIXEL_COL (f, column),
4508 CHAR_TO_PIXEL_ROW (f, row),
4509 &glyph, 1, highlight, 0);
4510 }
4511
4512 static void
4513 x_display_bar_cursor (f, on)
4514 struct frame *f;
4515 int on;
4516 {
4517 struct frame_glyphs *current_glyphs = FRAME_CURRENT_GLYPHS (f);
4518
4519 /* This is pointless on invisible frames, and dangerous on garbaged
4520 frames; in the latter case, the frame may be in the midst of
4521 changing its size, and curs_x and curs_y may be off the frame. */
4522 if (! FRAME_VISIBLE_P (f) || FRAME_GARBAGED_P (f))
4523 return;
4524
4525 if (! on && f->phys_cursor_x < 0)
4526 return;
4527
4528 /* If we're not updating, then we want to use the current frame's
4529 cursor position, not our local idea of where the cursor ought to be. */
4530 if (f != updating_frame)
4531 {
4532 curs_x = FRAME_CURSOR_X (f);
4533 curs_y = FRAME_CURSOR_Y (f);
4534 }
4535
4536 /* If there is anything wrong with the current cursor state, remove it. */
4537 if (f->phys_cursor_x >= 0
4538 && (!on
4539 || f->phys_cursor_x != curs_x
4540 || f->phys_cursor_y != curs_y
4541 || f->display.x->current_cursor != bar_cursor))
4542 {
4543 /* Erase the cursor by redrawing the character underneath it. */
4544 x_draw_single_glyph (f, f->phys_cursor_y, f->phys_cursor_x,
4545 f->phys_cursor_glyph,
4546 current_glyphs->highlight[f->phys_cursor_y]);
4547 f->phys_cursor_x = -1;
4548 }
4549
4550 /* If we now need a cursor in the new place or in the new form, do it so. */
4551 if (on
4552 && (f->phys_cursor_x < 0
4553 || (f->display.x->current_cursor != bar_cursor)))
4554 {
4555 f->phys_cursor_glyph
4556 = ((current_glyphs->enable[curs_y]
4557 && curs_x < current_glyphs->used[curs_y])
4558 ? current_glyphs->glyphs[curs_y][curs_x]
4559 : SPACEGLYPH);
4560 XFillRectangle (x_current_display, FRAME_X_WINDOW (f),
4561 f->display.x->cursor_gc,
4562 CHAR_TO_PIXEL_COL (f, curs_x),
4563 CHAR_TO_PIXEL_ROW (f, curs_y),
4564 1, f->display.x->line_height);
4565
4566 f->phys_cursor_x = curs_x;
4567 f->phys_cursor_y = curs_y;
4568
4569 f->display.x->current_cursor = bar_cursor;
4570 }
4571
4572 if (updating_frame != f)
4573 XFlushQueue ();
4574 }
4575
4576
4577 /* Turn the displayed cursor of frame F on or off according to ON.
4578 If ON is nonzero, where to put the cursor is specified
4579 by F->cursor_x and F->cursor_y. */
4580
4581 static void
4582 x_display_box_cursor (f, on)
4583 struct frame *f;
4584 int on;
4585 {
4586 struct frame_glyphs *current_glyphs = FRAME_CURRENT_GLYPHS (f);
4587
4588 /* This is pointless on invisible frames, and dangerous on garbaged
4589 frames; in the latter case, the frame may be in the midst of
4590 changing its size, and curs_x and curs_y may be off the frame. */
4591 if (! FRAME_VISIBLE_P (f) || FRAME_GARBAGED_P (f))
4592 return;
4593
4594 /* If cursor is off and we want it off, return quickly. */
4595 if (!on && f->phys_cursor_x < 0)
4596 return;
4597
4598 /* If we're not updating, then we want to use the current frame's
4599 cursor position, not our local idea of where the cursor ought to be. */
4600 if (f != updating_frame)
4601 {
4602 curs_x = FRAME_CURSOR_X (f);
4603 curs_y = FRAME_CURSOR_Y (f);
4604 }
4605
4606 /* If cursor is currently being shown and we don't want it to be
4607 or it is in the wrong place,
4608 or we want a hollow box and it's not so, (pout!)
4609 erase it. */
4610 if (f->phys_cursor_x >= 0
4611 && (!on
4612 || f->phys_cursor_x != curs_x
4613 || f->phys_cursor_y != curs_y
4614 || (f->display.x->current_cursor != hollow_box_cursor
4615 && (f != x_highlight_frame))))
4616 {
4617 int mouse_face_here = 0;
4618
4619 /* If the cursor is in the mouse face area, redisplay that when
4620 we clear the cursor. */
4621 if (f == mouse_face_mouse_frame
4622 &&
4623 (f->phys_cursor_y > mouse_face_beg_row
4624 || (f->phys_cursor_y == mouse_face_beg_row
4625 && f->phys_cursor_x >= mouse_face_beg_col))
4626 &&
4627 (f->phys_cursor_y < mouse_face_end_row
4628 || (f->phys_cursor_y == mouse_face_end_row
4629 && f->phys_cursor_x < mouse_face_end_col)))
4630 mouse_face_here = 1;
4631
4632 /* If the font is not as tall as a whole line,
4633 we must explicitly clear the line's whole height. */
4634 if (FONT_HEIGHT (f->display.x->font) != f->display.x->line_height)
4635 XClearArea (x_current_display, FRAME_X_WINDOW (f),
4636 CHAR_TO_PIXEL_COL (f, f->phys_cursor_x),
4637 CHAR_TO_PIXEL_ROW (f, f->phys_cursor_y),
4638 FONT_WIDTH (f->display.x->font),
4639 f->display.x->line_height, False);
4640 /* Erase the cursor by redrawing the character underneath it. */
4641 x_draw_single_glyph (f, f->phys_cursor_y, f->phys_cursor_x,
4642 f->phys_cursor_glyph,
4643 (mouse_face_here
4644 ? 3
4645 : current_glyphs->highlight[f->phys_cursor_y]));
4646 f->phys_cursor_x = -1;
4647 }
4648
4649 /* If we want to show a cursor,
4650 or we want a box cursor and it's not so,
4651 write it in the right place. */
4652 if (on
4653 && (f->phys_cursor_x < 0
4654 || (f->display.x->current_cursor != filled_box_cursor
4655 && f == x_highlight_frame)))
4656 {
4657 f->phys_cursor_glyph
4658 = ((current_glyphs->enable[curs_y]
4659 && curs_x < current_glyphs->used[curs_y])
4660 ? current_glyphs->glyphs[curs_y][curs_x]
4661 : SPACEGLYPH);
4662 if (f != x_highlight_frame)
4663 {
4664 x_draw_box (f);
4665 f->display.x->current_cursor = hollow_box_cursor;
4666 }
4667 else
4668 {
4669 x_draw_single_glyph (f, curs_y, curs_x,
4670 f->phys_cursor_glyph, 2);
4671 f->display.x->current_cursor = filled_box_cursor;
4672 }
4673
4674 f->phys_cursor_x = curs_x;
4675 f->phys_cursor_y = curs_y;
4676 }
4677
4678 if (updating_frame != f)
4679 XFlushQueue ();
4680 }
4681
4682 x_display_cursor (f, on)
4683 struct frame *f;
4684 int on;
4685 {
4686 BLOCK_INPUT;
4687
4688 if (FRAME_DESIRED_CURSOR (f) == filled_box_cursor)
4689 x_display_box_cursor (f, on);
4690 else if (FRAME_DESIRED_CURSOR (f) == bar_cursor)
4691 x_display_bar_cursor (f, on);
4692 else
4693 /* Those are the only two we have implemented! */
4694 abort ();
4695
4696 UNBLOCK_INPUT;
4697 }
4698 \f
4699 /* Icons. */
4700
4701 /* Refresh bitmap kitchen sink icon for frame F
4702 when we get an expose event for it. */
4703
4704 refreshicon (f)
4705 struct frame *f;
4706 {
4707 #ifdef HAVE_X11
4708 /* Normally, the window manager handles this function. */
4709 #else /* ! defined (HAVE_X11) */
4710 int mask;
4711
4712 if (f->display.x->icon_bitmap_flag)
4713 XBitmapBitsPut (f->display.x->icon_desc, 0, 0, sink_width, sink_height,
4714 sink_bits, BlackPixel, WHITE_PIX_DEFAULT,
4715 icon_bitmap, GXcopy, AllPlanes);
4716 else
4717 {
4718 extern struct frame *selected_frame;
4719 struct Lisp_String *str;
4720 unsigned char *string;
4721
4722 string
4723 = XSTRING (XBUFFER (XWINDOW (f->selected_window)->buffer)->name)->data;
4724
4725 if (f->display.x->icon_label != string)
4726 {
4727 f->display.x->icon_label = string;
4728 XChangeWindow (f->display.x->icon_desc,
4729 XQueryWidth (string, icon_font_info->id) + 10,
4730 icon_font_info->height + 10);
4731 }
4732
4733 XText (f->display.x->icon_desc, 5, 5, string,
4734 str->size, icon_font_info->id,
4735 BLACK_PIX_DEFAULT, WHITE_PIX_DEFAULT);
4736 }
4737 XFlushQueue ();
4738 #endif /* ! defined (HAVE_X11) */
4739 }
4740
4741 /* Make the x-window of frame F use the gnu icon bitmap. */
4742
4743 int
4744 x_bitmap_icon (f)
4745 struct frame *f;
4746 {
4747 int mask;
4748 Window icon_window;
4749
4750 if (FRAME_X_WINDOW (f) == 0)
4751 return 1;
4752
4753 #ifdef HAVE_X11
4754 if (! icon_bitmap)
4755 icon_bitmap =
4756 XCreateBitmapFromData (x_current_display, FRAME_X_WINDOW (f),
4757 gnu_bits, gnu_width, gnu_height);
4758 x_wm_set_icon_pixmap (f, icon_bitmap);
4759 f->display.x->icon_bitmap_flag = 1;
4760 #else /* ! defined (HAVE_X11) */
4761 if (f->display.x->icon_desc)
4762 {
4763 XClearIconWindow (FRAME_X_WINDOW (f));
4764 XDestroyWindow (f->display.x->icon_desc);
4765 }
4766
4767 icon_window = XCreateWindow (f->display.x->parent_desc,
4768 0, 0, sink_width, sink_height,
4769 2, WhitePixmap, (Pixmap) NULL);
4770
4771 if (icon_window == 0)
4772 return 1;
4773
4774 XSetIconWindow (FRAME_X_WINDOW (f), icon_window);
4775 XSelectInput (icon_window, ExposeWindow | UnmapWindow);
4776
4777 f->display.x->icon_desc = icon_window;
4778 f->display.x->icon_bitmap_flag = 1;
4779
4780 if (icon_bitmap == 0)
4781 icon_bitmap
4782 = XStoreBitmap (sink_mask_width, sink_mask_height, sink_mask_bits);
4783 #endif /* ! defined (HAVE_X11) */
4784
4785 return 0;
4786 }
4787
4788
4789 /* Make the x-window of frame F use a rectangle with text. */
4790
4791 int
4792 x_text_icon (f, icon_name)
4793 struct frame *f;
4794 char *icon_name;
4795 {
4796 #ifndef HAVE_X11
4797 int mask;
4798 int width;
4799 Window icon_window;
4800 char *X_DefaultValue;
4801 Bitmap b1;
4802
4803 #ifndef WhitePixel
4804 #define WhitePixel 1
4805 #endif /* WhitePixel */
4806
4807 #ifndef BlackPixel
4808 #define BlackPixel 0
4809 #endif /* BlackPixel */
4810 #endif /* HAVE_X11 */
4811
4812 if (FRAME_X_WINDOW (f) == 0)
4813 return 1;
4814
4815 #ifdef HAVE_X11
4816 if (icon_name)
4817 f->display.x->icon_label = icon_name;
4818 else
4819 if (! f->display.x->icon_label)
4820 f->display.x->icon_label = " *emacs* ";
4821
4822 #if 0
4823 XSetIconName (x_current_display, FRAME_X_WINDOW (f),
4824 (char *) f->display.x->icon_label);
4825 #endif
4826
4827 f->display.x->icon_bitmap_flag = 0;
4828 x_wm_set_icon_pixmap (f, 0);
4829 #else /* ! defined (HAVE_X11) */
4830 if (icon_font_info == 0)
4831 icon_font_info
4832 = XGetFont (XGetDefault (XDISPLAY
4833 (char *) XSTRING (Vinvocation_name)->data,
4834 "BodyFont"));
4835
4836 if (f->display.x->icon_desc)
4837 {
4838 XClearIconWindow (XDISPLAY FRAME_X_WINDOW (f));
4839 XDestroyWindow (XDISPLAY f->display.x->icon_desc);
4840 }
4841
4842 if (icon_name)
4843 f->display.x->icon_label = (unsigned char *) icon_name;
4844 else
4845 if (! f->display.x->icon_label)
4846 f->display.x->icon_label = XSTRING (f->name)->data;
4847
4848 width = XStringWidth (f->display.x->icon_label, icon_font_info, 0, 0);
4849 icon_window = XCreateWindow (f->display.x->parent_desc,
4850 f->display.x->left_pos,
4851 f->display.x->top_pos,
4852 width + 10, icon_font_info->height + 10,
4853 2, BlackPixmap, WhitePixmap);
4854
4855 if (icon_window == 0)
4856 return 1;
4857
4858 XSetIconWindow (FRAME_X_WINDOW (f), icon_window);
4859 XSelectInput (icon_window, ExposeWindow | ExposeRegion | UnmapWindow | ButtonPressed);
4860
4861 f->display.x->icon_desc = icon_window;
4862 f->display.x->icon_bitmap_flag = 0;
4863 f->display.x->icon_label = 0;
4864 #endif /* ! defined (HAVE_X11) */
4865
4866 return 0;
4867 }
4868 \f
4869 /* Handling X errors. */
4870
4871 /* Shut down Emacs in an orderly fashion, because of a SIGPIPE on the
4872 X server's connection, or an error reported via the X protocol. */
4873
4874 static SIGTYPE
4875 x_connection_closed ()
4876 {
4877 if (_Xdebug)
4878 abort ();
4879
4880 shut_down_emacs (0, 1, Qnil);
4881
4882 exit (70);
4883 }
4884
4885 /* An X error handler which prints an error message and then kills
4886 Emacs. This is what's normally installed as Xlib's handler for
4887 protocol errors. */
4888 static int
4889 x_error_quitter (display, error)
4890 Display *display;
4891 XErrorEvent *error;
4892 {
4893 char buf[256];
4894
4895 /* Note that there is no real way portable across R3/R4 to get the
4896 original error handler. */
4897
4898 XGetErrorText (display, error->error_code, buf, sizeof (buf));
4899 fprintf (stderr, "X protocol error: %s on protocol request %d\n",
4900 buf, error->request_code);
4901
4902 #if 0
4903 /* While we're testing Emacs 19, we'll just dump core whenever we
4904 get an X error, so we can figure out why it happened. */
4905 abort ();
4906 #endif
4907
4908 x_connection_closed ();
4909 }
4910
4911 /* A handler for X IO errors which prints an error message and then
4912 kills Emacs. This is what is always installed as Xlib's handler
4913 for I/O errors. */
4914 static int
4915 x_io_error_quitter (display)
4916 Display *display;
4917 {
4918 fprintf (stderr, "Connection to X server %s lost.\n",
4919 XDisplayName (DisplayString (display)));
4920
4921 #if 0
4922 /* While we're testing Emacs 19, we'll just dump core whenever we
4923 get an X error, so we can figure out why it happened. */
4924 abort ();
4925 #endif
4926
4927 x_connection_closed ();
4928 }
4929
4930 /* A buffer for storing X error messages. */
4931 static char *x_caught_error_message;
4932 #define X_CAUGHT_ERROR_MESSAGE_SIZE 200
4933
4934 /* An X error handler which stores the error message in
4935 x_caught_error_message. This is what's installed when
4936 x_catch_errors is in effect. */
4937 static int
4938 x_error_catcher (display, error)
4939 Display *display;
4940 XErrorEvent *error;
4941 {
4942 XGetErrorText (display, error->error_code,
4943 x_caught_error_message, X_CAUGHT_ERROR_MESSAGE_SIZE);
4944 }
4945
4946
4947 /* Begin trapping X errors.
4948
4949 After calling this function, X protocol errors no longer cause
4950 Emacs to exit; instead, they are recorded in x_cfc_error_message.
4951
4952 Calling x_check_errors signals an Emacs error if an X error has
4953 occurred since the last call to x_catch_errors or x_check_errors.
4954
4955 Calling x_uncatch_errors resumes the normal error handling. */
4956
4957 void x_catch_errors (), x_check_errors (), x_uncatch_errors ();
4958
4959 void
4960 x_catch_errors ()
4961 {
4962 /* Make sure any errors from previous requests have been dealt with. */
4963 XSync (x_current_display, False);
4964
4965 /* Set up the error buffer. */
4966 x_caught_error_message
4967 = (char*) xmalloc (X_CAUGHT_ERROR_MESSAGE_SIZE);
4968 x_caught_error_message[0] = '\0';
4969
4970 /* Install our little error handler. */
4971 XHandleError (x_error_catcher);
4972 }
4973
4974 /* If any X protocol errors have arrived since the last call to
4975 x_catch_errors or x_check_errors, signal an Emacs error using
4976 sprintf (a buffer, FORMAT, the x error message text) as the text. */
4977
4978 void
4979 x_check_errors (format)
4980 char *format;
4981 {
4982 /* Make sure to catch any errors incurred so far. */
4983 XSync (x_current_display, False);
4984
4985 if (x_caught_error_message[0])
4986 {
4987 char buf[X_CAUGHT_ERROR_MESSAGE_SIZE + 56];
4988
4989 sprintf (buf, format, x_caught_error_message);
4990 x_uncatch_errors ();
4991 error (buf);
4992 }
4993 }
4994
4995 /* Nonzero if we had any X protocol errors since we did x_catch_errors. */
4996
4997 int
4998 x_had_errors_p ()
4999 {
5000 /* Make sure to catch any errors incurred so far. */
5001 XSync (x_current_display, False);
5002
5003 return x_caught_error_message[0] != 0;
5004 }
5005
5006 /* Stop catching X protocol errors and let them make Emacs die. */
5007
5008 void
5009 x_uncatch_errors ()
5010 {
5011 xfree (x_caught_error_message);
5012 x_caught_error_message = 0;
5013 XHandleError (x_error_quitter);
5014 }
5015
5016 #if 0
5017 static unsigned int x_wire_count;
5018 x_trace_wire ()
5019 {
5020 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
5021 }
5022 #endif /* ! 0 */
5023
5024 \f
5025 /* Changing the font of the frame. */
5026
5027 /* Set the font of the x-window specified by frame F
5028 to the font named NEWNAME. This is safe to use
5029 even before F has an actual x-window. */
5030
5031 #ifdef HAVE_X11
5032
5033 struct font_info
5034 {
5035 XFontStruct *font;
5036 char *name;
5037 };
5038
5039 /* A table of all the fonts we have already loaded. */
5040 static struct font_info *x_font_table;
5041
5042 /* The current capacity of x_font_table. */
5043 static int x_font_table_size;
5044
5045 /* The number of fonts actually stored in x_font_table.
5046 x_font_table[n] is used and valid iff 0 <= n < n_fonts.
5047 0 <= n_fonts <= x_font_table_size. */
5048 static int n_fonts;
5049
5050 Lisp_Object
5051 x_new_font (f, fontname)
5052 struct frame *f;
5053 register char *fontname;
5054 {
5055 int already_loaded;
5056 int n_matching_fonts;
5057 XFontStruct *font_info;
5058 char **font_names;
5059
5060 /* Get a list of all the fonts that match this name. Once we
5061 have a list of matching fonts, we compare them against the fonts
5062 we already have by comparing font ids. */
5063 font_names = (char **) XListFonts (x_current_display, fontname,
5064 1024, &n_matching_fonts);
5065 /* Apparently it doesn't set n_matching_fonts to zero when it can't
5066 find any matches; font_names == 0 is the only clue. */
5067 if (! font_names)
5068 n_matching_fonts = 0;
5069
5070 /* Don't just give up if n_matching_fonts is 0.
5071 Apparently there's a bug on Suns: XListFontsWithInfo can
5072 fail to find a font, but XLoadQueryFont may still find it. */
5073
5074 /* See if we've already loaded a matching font. */
5075 already_loaded = -1;
5076 if (n_matching_fonts != 0)
5077 {
5078 int i, j;
5079
5080 for (i = 0; i < n_fonts; i++)
5081 for (j = 0; j < n_matching_fonts; j++)
5082 if (!strcmp (x_font_table[i].name, font_names[j]))
5083 {
5084 already_loaded = i;
5085 fontname = font_names[j];
5086 goto found_font;
5087 }
5088 }
5089 found_font:
5090
5091 /* If we have, just return it from the table. */
5092 if (already_loaded >= 0)
5093 f->display.x->font = x_font_table[already_loaded].font;
5094
5095 /* Otherwise, load the font and add it to the table. */
5096 else
5097 {
5098 int i;
5099 XFontStruct *font;
5100
5101 /* Try to find a character-cell font in the list. */
5102 #if 0
5103 /* A laudable goal, but this isn't how to do it. */
5104 for (i = 0; i < n_matching_fonts; i++)
5105 if (! font_info[i].per_char)
5106 break;
5107 #else
5108 i = 0;
5109 #endif
5110
5111 /* See comment above. */
5112 if (n_matching_fonts != 0)
5113 fontname = font_names[i];
5114
5115 font = (XFontStruct *) XLoadQueryFont (x_current_display, fontname);
5116 if (! font)
5117 {
5118 /* Free the information from XListFonts. */
5119 if (n_matching_fonts)
5120 XFreeFontNames (font_names);
5121 return Qnil;
5122 }
5123
5124 /* Do we need to create the table? */
5125 if (x_font_table_size == 0)
5126 {
5127 x_font_table_size = 16;
5128 x_font_table
5129 = (struct font_info *) xmalloc (x_font_table_size
5130 * sizeof (x_font_table[0]));
5131 }
5132 /* Do we need to grow the table? */
5133 else if (n_fonts >= x_font_table_size)
5134 {
5135 x_font_table_size *= 2;
5136 x_font_table
5137 = (struct font_info *) xrealloc (x_font_table,
5138 (x_font_table_size
5139 * sizeof (x_font_table[0])));
5140 }
5141
5142 x_font_table[n_fonts].name = (char *) xmalloc (strlen (fontname) + 1);
5143 bcopy (fontname, x_font_table[n_fonts].name, strlen (fontname) + 1);
5144 f->display.x->font = x_font_table[n_fonts++].font = font;
5145 }
5146
5147 /* Now make the frame display the given font. */
5148 if (FRAME_X_WINDOW (f) != 0)
5149 {
5150 XSetFont (x_current_display, f->display.x->normal_gc,
5151 f->display.x->font->fid);
5152 XSetFont (x_current_display, f->display.x->reverse_gc,
5153 f->display.x->font->fid);
5154 XSetFont (x_current_display, f->display.x->cursor_gc,
5155 f->display.x->font->fid);
5156
5157 frame_update_line_height (f);
5158 x_set_window_size (f, 0, f->width, f->height);
5159 }
5160 else
5161 /* If we are setting a new frame's font for the first time,
5162 there are no faces yet, so this font's height is the line height. */
5163 f->display.x->line_height = FONT_HEIGHT (f->display.x->font);
5164
5165 {
5166 Lisp_Object lispy_name;
5167
5168 lispy_name = build_string (fontname);
5169
5170 /* Free the information from XListFonts. The data
5171 we actually retain comes from XLoadQueryFont. */
5172 XFreeFontNames (font_names);
5173
5174 return lispy_name;
5175 }
5176 }
5177 #else /* ! defined (HAVE_X11) */
5178 x_new_font (f, newname)
5179 struct frame *f;
5180 register char *newname;
5181 {
5182 FONT_TYPE *temp;
5183 int mask;
5184
5185 temp = XGetFont (newname);
5186 if (temp == (FONT_TYPE *) 0)
5187 return 1;
5188
5189 if (f->display.x->font)
5190 XLoseFont (f->display.x->font);
5191
5192 f->display.x->font = temp;
5193
5194 if (FRAME_X_WINDOW (f) != 0)
5195 x_set_window_size (f, 0, f->width, f->height);
5196
5197 return 0;
5198 }
5199 #endif /* ! defined (HAVE_X11) */
5200 \f
5201 x_calc_absolute_position (f)
5202 struct frame *f;
5203 {
5204 #ifdef HAVE_X11
5205 Window win, child;
5206 int win_x = 0, win_y = 0;
5207 int flags = f->display.x->size_hint_flags;
5208
5209 /* Find the position of the outside upper-left corner of
5210 the inner window, with respect to the outer window. */
5211 if (f->display.x->parent_desc != ROOT_WINDOW)
5212 {
5213 BLOCK_INPUT;
5214 XTranslateCoordinates (x_current_display,
5215
5216 /* From-window, to-window. */
5217 f->display.x->window_desc,
5218 f->display.x->parent_desc,
5219
5220 /* From-position, to-position. */
5221 0, 0, &win_x, &win_y,
5222
5223 /* Child of win. */
5224 &child);
5225 UNBLOCK_INPUT;
5226 }
5227
5228 /* Treat negative positions as relative to the leftmost bottommost
5229 position that fits on the screen. */
5230 if (flags & XNegative)
5231 f->display.x->left_pos = (x_screen_width
5232 - 2 * f->display.x->border_width - win_x
5233 - PIXEL_WIDTH (f)
5234 + f->display.x->left_pos);
5235
5236 if (flags & YNegative)
5237 f->display.x->top_pos = (x_screen_height
5238 - 2 * f->display.x->border_width - win_y
5239 - PIXEL_HEIGHT (f)
5240 + f->display.x->top_pos);
5241 /* The left_pos and top_pos
5242 are now relative to the top and left screen edges,
5243 so the flags should correspond. */
5244 f->display.x->size_hint_flags &= ~ (XNegative | YNegative);
5245 #else /* ! defined (HAVE_X11) */
5246 WINDOWINFO_TYPE parentinfo;
5247
5248 XGetWindowInfo (FRAME_X_WINDOW (f), &parentinfo);
5249
5250 if (f->display.x->left_pos < 0)
5251 f->display.x->left_pos = parentinfo.width + (f->display.x->left_pos + 1)
5252 - PIXEL_WIDTH (f) - 2 * f->display.x->internal_border_width;
5253
5254 if (f->display.x->top_pos < 0)
5255 f->display.x->top_pos = parentinfo.height + (f->display.x->top_pos + 1)
5256 - PIXEL_HEIGHT (f) - 2 * f->display.x->internal_border_width;
5257 #endif /* ! defined (HAVE_X11) */
5258 }
5259
5260 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
5261 to really change the position, and 0 when calling from
5262 x_make_frame_visible (in that case, XOFF and YOFF are the current
5263 position values). */
5264
5265 x_set_offset (f, xoff, yoff, change_gravity)
5266 struct frame *f;
5267 register int xoff, yoff;
5268 int change_gravity;
5269 {
5270 if (change_gravity)
5271 {
5272 f->display.x->top_pos = yoff;
5273 f->display.x->left_pos = xoff;
5274 f->display.x->size_hint_flags &= ~ (XNegative | YNegative);
5275 if (xoff < 0)
5276 f->display.x->size_hint_flags |= XNegative;
5277 if (yoff < 0)
5278 f->display.x->size_hint_flags |= YNegative;
5279 f->display.x->win_gravity = NorthWestGravity;
5280 }
5281 x_calc_absolute_position (f);
5282
5283 BLOCK_INPUT;
5284 x_wm_set_size_hint (f, 0, 0);
5285
5286 #ifdef USE_X_TOOLKIT
5287 XMoveWindow (XDISPLAY XtWindow (f->display.x->widget),
5288 f->display.x->left_pos, f->display.x->top_pos);
5289 #else /* not USE_X_TOOLKIT */
5290 XMoveWindow (XDISPLAY FRAME_X_WINDOW (f),
5291 f->display.x->left_pos, f->display.x->top_pos);
5292 #endif /* not USE_X_TOOLKIT */
5293 UNBLOCK_INPUT;
5294 }
5295
5296 /* Call this to change the size of frame F's x-window.
5297 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
5298 for this size change and subsequent size changes.
5299 Otherwise we leave the window gravity unchanged. */
5300
5301 x_set_window_size (f, change_gravity, cols, rows)
5302 struct frame *f;
5303 int change_gravity;
5304 int cols, rows;
5305 {
5306 int pixelwidth, pixelheight;
5307 int mask;
5308
5309 #ifdef USE_X_TOOLKIT
5310 BLOCK_INPUT;
5311 {
5312 /* The x and y position of the widget is clobbered by the
5313 call to XtSetValues within EmacsFrameSetCharSize.
5314 This is a real kludge, but I don't understand Xt so I can't
5315 figure out a correct fix. Can anyone else tell me? -- rms. */
5316 int xpos = f->display.x->widget->core.x;
5317 int ypos = f->display.x->widget->core.y;
5318 EmacsFrameSetCharSize (f->display.x->edit_widget, cols, rows);
5319 f->display.x->widget->core.x = xpos;
5320 f->display.x->widget->core.y = ypos;
5321 }
5322 UNBLOCK_INPUT;
5323
5324 #else /* not USE_X_TOOLKIT */
5325
5326 BLOCK_INPUT;
5327
5328 check_frame_size (f, &rows, &cols);
5329 f->display.x->vertical_scroll_bar_extra
5330 = (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
5331 ? VERTICAL_SCROLL_BAR_PIXEL_WIDTH (f)
5332 : 0);
5333 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
5334 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
5335
5336 #ifdef HAVE_X11
5337 f->display.x->win_gravity = NorthWestGravity;
5338 x_wm_set_size_hint (f, 0, 0);
5339 #endif /* ! defined (HAVE_X11) */
5340 XSync (x_current_display, False);
5341 XChangeWindowSize (FRAME_X_WINDOW (f), pixelwidth, pixelheight);
5342
5343 /* Now, strictly speaking, we can't be sure that this is accurate,
5344 but the window manager will get around to dealing with the size
5345 change request eventually, and we'll hear how it went when the
5346 ConfigureNotify event gets here.
5347
5348 We could just not bother storing any of this information here,
5349 and let the ConfigureNotify event set everything up, but that
5350 might be kind of confusing to the lisp code, since size changes
5351 wouldn't be reported in the frame parameters until some random
5352 point in the future when the ConfigureNotify event arrives. */
5353 change_frame_size (f, rows, cols, 0, 0);
5354 PIXEL_WIDTH (f) = pixelwidth;
5355 PIXEL_HEIGHT (f) = pixelheight;
5356
5357 /* If cursor was outside the new size, mark it as off. */
5358 if (f->phys_cursor_y >= rows
5359 || f->phys_cursor_x >= cols)
5360 {
5361 f->phys_cursor_x = -1;
5362 f->phys_cursor_y = -1;
5363 }
5364
5365 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
5366 receive in the ConfigureNotify event; if we get what we asked
5367 for, then the event won't cause the screen to become garbaged, so
5368 we have to make sure to do it here. */
5369 SET_FRAME_GARBAGED (f);
5370
5371 XFlushQueue ();
5372 UNBLOCK_INPUT;
5373 #endif /* not USE_X_TOOLKIT */
5374 }
5375
5376 #ifndef HAVE_X11
5377 x_set_resize_hint (f)
5378 struct frame *f;
5379 {
5380 XSetResizeHint (FRAME_X_WINDOW (f),
5381 2 * f->display.x->internal_border_width,
5382 2 * f->display.x->internal_border_width,
5383 FONT_WIDTH (f->display.x->font),
5384 f->display.x->line_height);
5385 }
5386 #endif /* HAVE_X11 */
5387 \f
5388 /* Mouse warping, focus shifting, raising and lowering. */
5389
5390 void
5391 x_set_mouse_position (f, x, y)
5392 struct frame *f;
5393 int x, y;
5394 {
5395 int pix_x, pix_y;
5396
5397 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->display.x->font) / 2;
5398 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->display.x->line_height / 2;
5399
5400 if (pix_x < 0) pix_x = 0;
5401 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
5402
5403 if (pix_y < 0) pix_y = 0;
5404 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
5405
5406 BLOCK_INPUT;
5407
5408 XWarpMousePointer (FRAME_X_WINDOW (f), pix_x, pix_y);
5409 UNBLOCK_INPUT;
5410 }
5411
5412 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
5413
5414 void
5415 x_set_mouse_pixel_position (f, pix_x, pix_y)
5416 struct frame *f;
5417 int pix_x, pix_y;
5418 {
5419 BLOCK_INPUT;
5420
5421 XWarpMousePointer (FRAME_X_WINDOW (f), pix_x, pix_y);
5422 UNBLOCK_INPUT;
5423 }
5424
5425 #ifdef HAVE_X11
5426 x_focus_on_frame (f)
5427 struct frame *f;
5428 {
5429 #if 0 /* This proves to be unpleasant. */
5430 x_raise_frame (f);
5431 #endif
5432 #if 0
5433 /* I don't think that the ICCCM allows programs to do things like this
5434 without the interaction of the window manager. Whatever you end up
5435 doing with this code, do it to x_unfocus_frame too. */
5436 XSetInputFocus (x_current_display, FRAME_X_WINDOW (f),
5437 RevertToPointerRoot, CurrentTime);
5438 #endif /* ! 0 */
5439 }
5440
5441 x_unfocus_frame (f)
5442 struct frame *f;
5443 {
5444 #if 0
5445 /* Look at the remarks in x_focus_on_frame. */
5446 if (x_focus_frame == f)
5447 XSetInputFocus (x_current_display, PointerRoot,
5448 RevertToPointerRoot, CurrentTime);
5449 #endif /* ! 0 */
5450 }
5451
5452 #endif /* ! defined (HAVE_X11) */
5453
5454 /* Raise frame F. */
5455
5456 x_raise_frame (f)
5457 struct frame *f;
5458 {
5459 if (f->async_visible)
5460 {
5461 BLOCK_INPUT;
5462 #ifdef USE_X_TOOLKIT
5463 XRaiseWindow (XDISPLAY XtWindow (f->display.x->widget));
5464 #else /* not USE_X_TOOLKIT */
5465 XRaiseWindow (XDISPLAY FRAME_X_WINDOW (f));
5466 #endif /* not USE_X_TOOLKIT */
5467 XFlushQueue ();
5468 UNBLOCK_INPUT;
5469 }
5470 }
5471
5472 /* Lower frame F. */
5473
5474 x_lower_frame (f)
5475 struct frame *f;
5476 {
5477 if (f->async_visible)
5478 {
5479 BLOCK_INPUT;
5480 #ifdef USE_X_TOOLKIT
5481 XLowerWindow (XDISPLAY XtWindow (f->display.x->widget));
5482 #else /* not USE_X_TOOLKIT */
5483 XLowerWindow (XDISPLAY FRAME_X_WINDOW (f));
5484 #endif /* not USE_X_TOOLKIT */
5485 XFlushQueue ();
5486 UNBLOCK_INPUT;
5487 }
5488 }
5489
5490 static void
5491 XTframe_raise_lower (f, raise)
5492 FRAME_PTR f;
5493 int raise;
5494 {
5495 if (raise)
5496 x_raise_frame (f);
5497 else
5498 x_lower_frame (f);
5499 }
5500
5501
5502 /* Change from withdrawn state to mapped state,
5503 or deiconify. */
5504
5505 x_make_frame_visible (f)
5506 struct frame *f;
5507 {
5508 int mask;
5509
5510 BLOCK_INPUT;
5511
5512 if (! FRAME_VISIBLE_P (f))
5513 {
5514 #ifdef HAVE_X11
5515 #ifndef USE_X_TOOLKIT
5516 if (! FRAME_ICONIFIED_P (f))
5517 x_set_offset (f, f->display.x->left_pos, f->display.x->top_pos, 0);
5518 #endif
5519
5520 if (! EQ (Vx_no_window_manager, Qt))
5521 x_wm_set_window_state (f, NormalState);
5522 #ifdef USE_X_TOOLKIT
5523 /* This was XtPopup, but that did nothing for an iconified frame. */
5524 XtMapWidget (f->display.x->widget);
5525 #else /* not USE_X_TOOLKIT */
5526 XMapWindow (XDISPLAY FRAME_X_WINDOW (f));
5527 #endif /* not USE_X_TOOLKIT */
5528 #if 0 /* This seems to bring back scroll bars in the wrong places
5529 if the window configuration has changed. They seem
5530 to come back ok without this. */
5531 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5532 XMapSubwindows (x_current_display, FRAME_X_WINDOW (f));
5533 #endif
5534 #else /* ! defined (HAVE_X11) */
5535 XMapWindow (XDISPLAY FRAME_X_WINDOW (f));
5536 if (f->display.x->icon_desc != 0)
5537 XUnmapWindow (f->display.x->icon_desc);
5538
5539 /* Handled by the MapNotify event for X11 */
5540 f->async_visible = 1;
5541 f->async_iconified = 0;
5542
5543 /* NOTE: this may cause problems for the first frame. */
5544 XTcursor_to (0, 0);
5545 #endif /* ! defined (HAVE_X11) */
5546 }
5547
5548 XFlushQueue ();
5549
5550 UNBLOCK_INPUT;
5551 }
5552
5553 /* Change from mapped state to withdrawn state. */
5554
5555 x_make_frame_invisible (f)
5556 struct frame *f;
5557 {
5558 int mask;
5559 Window window;
5560
5561 #ifdef USE_X_TOOLKIT
5562 /* Use the frame's outermost window, not the one we normally draw on. */
5563 window = XtWindow (f->display.x->widget);
5564 #else /* not USE_X_TOOLKIT */
5565 window = FRAME_X_WINDOW (f);
5566 #endif /* not USE_X_TOOLKIT */
5567
5568 /* Don't keep the highlight on an invisible frame. */
5569 if (x_highlight_frame == f)
5570 x_highlight_frame = 0;
5571
5572 #if 0/* This might add unreliability; I don't trust it -- rms. */
5573 if (! f->async_visible && ! f->async_iconified)
5574 return;
5575 #endif
5576
5577 BLOCK_INPUT;
5578
5579 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
5580 that the current position of the window is user-specified, rather than
5581 program-specified, so that when the window is mapped again, it will be
5582 placed at the same location, without forcing the user to position it
5583 by hand again (they have already done that once for this window.) */
5584 x_wm_set_size_hint (f, 0, 1);
5585
5586 #ifdef HAVE_X11R4
5587
5588 if (! XWithdrawWindow (x_current_display, window,
5589 DefaultScreen (x_current_display)))
5590 {
5591 UNBLOCK_INPUT_RESIGNAL;
5592 error ("Can't notify window manager of window withdrawal");
5593 }
5594 #else /* ! defined (HAVE_X11R4) */
5595 #ifdef HAVE_X11
5596
5597 /* Tell the window manager what we're going to do. */
5598 if (! EQ (Vx_no_window_manager, Qt))
5599 {
5600 XEvent unmap;
5601
5602 unmap.xunmap.type = UnmapNotify;
5603 unmap.xunmap.window = window;
5604 unmap.xunmap.event = DefaultRootWindow (x_current_display);
5605 unmap.xunmap.from_configure = False;
5606 if (! XSendEvent (x_current_display,
5607 DefaultRootWindow (x_current_display),
5608 False,
5609 SubstructureRedirectMask|SubstructureNotifyMask,
5610 &unmap))
5611 {
5612 UNBLOCK_INPUT_RESIGNAL;
5613 error ("Can't notify window manager of withdrawal");
5614 }
5615 }
5616
5617 /* Unmap the window ourselves. Cheeky! */
5618 XUnmapWindow (x_current_display, window);
5619 #else /* ! defined (HAVE_X11) */
5620
5621 XUnmapWindow (FRAME_X_WINDOW (f));
5622 if (f->display.x->icon_desc != 0)
5623 XUnmapWindow (f->display.x->icon_desc);
5624
5625 #endif /* ! defined (HAVE_X11) */
5626 #endif /* ! defined (HAVE_X11R4) */
5627
5628 /* We can't distinguish this from iconification
5629 just by the event that we get from the server.
5630 So we can't win using the usual strategy of letting
5631 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
5632 and synchronize with the server to make sure we agree. */
5633 f->visible = 0;
5634 FRAME_ICONIFIED_P (f) = 0;
5635 f->async_visible = 0;
5636 f->async_iconified = 0;
5637
5638 x_sync ();
5639
5640 UNBLOCK_INPUT;
5641 }
5642
5643 /* Change window state from mapped to iconified. */
5644
5645 x_iconify_frame (f)
5646 struct frame *f;
5647 {
5648 int mask;
5649 int result;
5650
5651 /* Don't keep the highlight on an invisible frame. */
5652 if (x_highlight_frame == f)
5653 x_highlight_frame = 0;
5654
5655 if (f->async_iconified)
5656 return;
5657
5658 #ifdef USE_X_TOOLKIT
5659 BLOCK_INPUT;
5660
5661 if (! FRAME_VISIBLE_P (f))
5662 {
5663 if (! EQ (Vx_no_window_manager, Qt))
5664 x_wm_set_window_state (f, IconicState);
5665 /* This was XtPopup, but that did nothing for an iconified frame. */
5666 XtMapWidget (f->display.x->widget);
5667 UNBLOCK_INPUT;
5668 return;
5669 }
5670
5671 result = XIconifyWindow (x_current_display,
5672 XtWindow (f->display.x->widget),
5673 DefaultScreen (x_current_display));
5674 UNBLOCK_INPUT;
5675
5676 if (!result)
5677 error ("Can't notify window manager of iconification");
5678
5679 f->async_iconified = 1;
5680
5681 BLOCK_INPUT;
5682 XFlushQueue ();
5683 UNBLOCK_INPUT;
5684 #else /* not USE_X_TOOLKIT */
5685
5686 BLOCK_INPUT;
5687
5688 #ifdef HAVE_X11
5689 /* Make sure the X server knows where the window should be positioned,
5690 in case the user deiconifies with the window manager. */
5691 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
5692 x_set_offset (f, f->display.x->left_pos, f->display.x->top_pos, 0);
5693
5694 /* Since we don't know which revision of X we're running, we'll use both
5695 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
5696
5697 /* X11R4: send a ClientMessage to the window manager using the
5698 WM_CHANGE_STATE type. */
5699 {
5700 XEvent message;
5701
5702 message.xclient.window = FRAME_X_WINDOW (f);
5703 message.xclient.type = ClientMessage;
5704 message.xclient.message_type = Xatom_wm_change_state;
5705 message.xclient.format = 32;
5706 message.xclient.data.l[0] = IconicState;
5707
5708 if (! XSendEvent (x_current_display,
5709 DefaultRootWindow (x_current_display),
5710 False,
5711 SubstructureRedirectMask | SubstructureNotifyMask,
5712 &message))
5713 {
5714 UNBLOCK_INPUT_RESIGNAL;
5715 error ("Can't notify window manager of iconification");
5716 }
5717 }
5718
5719 /* X11R3: set the initial_state field of the window manager hints to
5720 IconicState. */
5721 x_wm_set_window_state (f, IconicState);
5722
5723 if (!FRAME_VISIBLE_P (f))
5724 {
5725 /* If the frame was withdrawn, before, we must map it. */
5726 XMapWindow (XDISPLAY FRAME_X_WINDOW (f));
5727 #if 0 /* We don't have subwindows in the icon. */
5728 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5729 XMapSubwindows (x_current_display, FRAME_X_WINDOW (f));
5730 #endif
5731 }
5732
5733 f->async_iconified = 1;
5734 #else /* ! defined (HAVE_X11) */
5735 XUnmapWindow (XDISPLAY FRAME_X_WINDOW (f));
5736
5737 f->async_visible = 0; /* Handled in the UnMap event for X11. */
5738 if (f->display.x->icon_desc != 0)
5739 {
5740 XMapWindow (XDISPLAY f->display.x->icon_desc);
5741 refreshicon (f);
5742 }
5743 #endif /* ! defined (HAVE_X11) */
5744
5745 XFlushQueue ();
5746 UNBLOCK_INPUT;
5747 #endif /* not USE_X_TOOLKIT */
5748 }
5749
5750 /* Destroy the X window of frame F. */
5751
5752 x_destroy_window (f)
5753 struct frame *f;
5754 {
5755 BLOCK_INPUT;
5756
5757 if (f->display.x->icon_desc != 0)
5758 XDestroyWindow (XDISPLAY f->display.x->icon_desc);
5759 XDestroyWindow (XDISPLAY f->display.x->window_desc);
5760 #ifdef USE_X_TOOLKIT
5761 XtDestroyWidget (f->display.x->widget);
5762 free_frame_menubar (f);
5763 #endif /* USE_X_TOOLKIT */
5764
5765 free_frame_faces (f);
5766 XFlushQueue ();
5767
5768 xfree (f->display.x);
5769 f->display.x = 0;
5770 if (f == x_focus_frame)
5771 x_focus_frame = 0;
5772 if (f == x_highlight_frame)
5773 x_highlight_frame = 0;
5774
5775 if (f == mouse_face_mouse_frame)
5776 {
5777 mouse_face_beg_row = mouse_face_beg_col = -1;
5778 mouse_face_end_row = mouse_face_end_col = -1;
5779 mouse_face_window = Qnil;
5780 }
5781
5782 UNBLOCK_INPUT;
5783 }
5784 \f
5785 /* Manage event queues for X10. */
5786
5787 #ifndef HAVE_X11
5788
5789 /* Manage event queues.
5790
5791 This code is only used by the X10 support.
5792
5793 We cannot leave events in the X queue and get them when we are ready
5794 because X does not provide a subroutine to get only a certain kind
5795 of event but not block if there are no queued events of that kind.
5796
5797 Therefore, we must examine events as they come in and copy events
5798 of certain kinds into our private queues.
5799
5800 All ExposeRegion events are put in x_expose_queue.
5801 All ButtonPress and ButtonRelease events are put in x_mouse_queue. */
5802
5803
5804 /* Write the event *P_XREP into the event queue *QUEUE.
5805 If the queue is full, do nothing, but return nonzero. */
5806
5807 int
5808 enqueue_event (p_xrep, queue)
5809 register XEvent *p_xrep;
5810 register struct event_queue *queue;
5811 {
5812 int newindex = queue->windex + 1;
5813 if (newindex == EVENT_BUFFER_SIZE)
5814 newindex = 0;
5815 if (newindex == queue->rindex)
5816 return -1;
5817 queue->xrep[queue->windex] = *p_xrep;
5818 queue->windex = newindex;
5819 return 0;
5820 }
5821
5822 /* Fetch the next event from queue *QUEUE and store it in *P_XREP.
5823 If *QUEUE is empty, do nothing and return 0. */
5824
5825 int
5826 dequeue_event (p_xrep, queue)
5827 register XEvent *p_xrep;
5828 register struct event_queue *queue;
5829 {
5830 if (queue->windex == queue->rindex)
5831 return 0;
5832 *p_xrep = queue->xrep[queue->rindex++];
5833 if (queue->rindex == EVENT_BUFFER_SIZE)
5834 queue->rindex = 0;
5835 return 1;
5836 }
5837
5838 /* Return the number of events buffered in *QUEUE. */
5839
5840 int
5841 queue_event_count (queue)
5842 register struct event_queue *queue;
5843 {
5844 int tem = queue->windex - queue->rindex;
5845 if (tem >= 0)
5846 return tem;
5847 return EVENT_BUFFER_SIZE + tem;
5848 }
5849
5850 /* Return nonzero if mouse input is pending. */
5851
5852 int
5853 mouse_event_pending_p ()
5854 {
5855 return queue_event_count (&x_mouse_queue);
5856 }
5857 #endif /* HAVE_X11 */
5858 \f
5859 /* Setting window manager hints. */
5860
5861 #ifdef HAVE_X11
5862
5863 /* Set the normal size hints for the window manager, for frame F.
5864 FLAGS is the flags word to use--or 0 meaning preserve the flags
5865 that the window now has.
5866 If USER_POSITION is nonzero, we set the USPosition
5867 flag (this is useful when FLAGS is 0). */
5868
5869 x_wm_set_size_hint (f, flags, user_position)
5870 struct frame *f;
5871 long flags;
5872 int user_position;
5873 {
5874 XSizeHints size_hints;
5875
5876 #ifdef USE_X_TOOLKIT
5877 Arg al[2];
5878 int ac = 0;
5879 Dimension widget_width, widget_height;
5880 Window window = XtWindow (f->display.x->widget);
5881 #else /* not USE_X_TOOLKIT */
5882 Window window = FRAME_X_WINDOW (f);
5883 #endif /* not USE_X_TOOLKIT */
5884
5885 /* Setting PMaxSize caused various problems. */
5886 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
5887
5888 flexlines = f->height;
5889
5890 size_hints.x = f->display.x->left_pos;
5891 size_hints.y = f->display.x->top_pos;
5892
5893 #ifdef USE_X_TOOLKIT
5894 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
5895 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
5896 XtGetValues (f->display.x->column_widget, al, ac);
5897 size_hints.height = widget_height;
5898 size_hints.width = widget_width;
5899 #else /* not USE_X_TOOLKIT */
5900 size_hints.height = PIXEL_HEIGHT (f);
5901 size_hints.width = PIXEL_WIDTH (f);
5902 #endif /* not USE_X_TOOLKIT */
5903
5904 size_hints.width_inc = FONT_WIDTH (f->display.x->font);
5905 size_hints.height_inc = f->display.x->line_height;
5906 size_hints.max_width = x_screen_width - CHAR_TO_PIXEL_WIDTH (f, 0);
5907 size_hints.max_height = x_screen_height - CHAR_TO_PIXEL_HEIGHT (f, 0);
5908
5909 {
5910 int base_width, base_height;
5911 int min_rows = 0, min_cols = 0;
5912
5913 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
5914 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
5915
5916 check_frame_size (f, &min_rows, &min_cols);
5917
5918 /* The window manager uses the base width hints to calculate the
5919 current number of rows and columns in the frame while
5920 resizing; min_width and min_height aren't useful for this
5921 purpose, since they might not give the dimensions for a
5922 zero-row, zero-column frame.
5923
5924 We use the base_width and base_height members if we have
5925 them; otherwise, we set the min_width and min_height members
5926 to the size for a zero x zero frame. */
5927
5928 #ifdef HAVE_X11R4
5929 size_hints.flags |= PBaseSize;
5930 size_hints.base_width = base_width;
5931 size_hints.base_height = base_height;
5932 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
5933 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
5934 #else
5935 size_hints.min_width = base_width;
5936 size_hints.min_height = base_height;
5937 #endif
5938 }
5939
5940 if (flags)
5941 size_hints.flags |= flags;
5942 else
5943 {
5944 XSizeHints hints; /* Sometimes I hate X Windows... */
5945 long supplied_return;
5946 int value;
5947
5948 #ifdef HAVE_X11R4
5949 value = XGetWMNormalHints (x_current_display, window, &hints,
5950 &supplied_return);
5951 #else
5952 value = XGetNormalHints (x_current_display, window, &hints);
5953 #endif
5954
5955 if (value == 0)
5956 hints.flags = 0;
5957 if (hints.flags & PSize)
5958 size_hints.flags |= PSize;
5959 if (hints.flags & PPosition)
5960 size_hints.flags |= PPosition;
5961 if (hints.flags & USPosition)
5962 size_hints.flags |= USPosition;
5963 if (hints.flags & USSize)
5964 size_hints.flags |= USSize;
5965 }
5966
5967 #ifdef PWinGravity
5968 size_hints.win_gravity = f->display.x->win_gravity;
5969 size_hints.flags |= PWinGravity;
5970
5971 if (user_position)
5972 {
5973 size_hints.flags &= ~ PPosition;
5974 size_hints.flags |= USPosition;
5975 }
5976 #endif /* PWinGravity */
5977
5978 #ifdef HAVE_X11R4
5979 XSetWMNormalHints (x_current_display, window, &size_hints);
5980 #else
5981 XSetNormalHints (x_current_display, window, &size_hints);
5982 #endif
5983 }
5984
5985 /* Used for IconicState or NormalState */
5986 x_wm_set_window_state (f, state)
5987 struct frame *f;
5988 int state;
5989 {
5990 #ifdef USE_X_TOOLKIT
5991 Arg al[1];
5992
5993 XtSetArg (al[0], XtNinitialState, state);
5994 XtSetValues (f->display.x->widget, al, 1);
5995 #else /* not USE_X_TOOLKIT */
5996 Window window = FRAME_X_WINDOW (f);
5997
5998 f->display.x->wm_hints.flags |= StateHint;
5999 f->display.x->wm_hints.initial_state = state;
6000
6001 XSetWMHints (x_current_display, window, &f->display.x->wm_hints);
6002 #endif /* not USE_X_TOOLKIT */
6003 }
6004
6005 x_wm_set_icon_pixmap (f, icon_pixmap)
6006 struct frame *f;
6007 Pixmap icon_pixmap;
6008 {
6009 #ifdef USE_X_TOOLKIT
6010 Window window = XtWindow (f->display.x->widget);
6011 #else
6012 Window window = FRAME_X_WINDOW (f);
6013 #endif
6014
6015 if (icon_pixmap)
6016 {
6017 f->display.x->wm_hints.icon_pixmap = icon_pixmap;
6018 f->display.x->wm_hints.flags |= IconPixmapHint;
6019 }
6020 else
6021 f->display.x->wm_hints.flags &= ~IconPixmapHint;
6022
6023 XSetWMHints (x_current_display, window, &f->display.x->wm_hints);
6024 }
6025
6026 x_wm_set_icon_position (f, icon_x, icon_y)
6027 struct frame *f;
6028 int icon_x, icon_y;
6029 {
6030 #ifdef USE_X_TOOLKIT
6031 Window window = XtWindow (f->display.x->widget);
6032 #else
6033 Window window = FRAME_X_WINDOW (f);
6034 #endif
6035
6036 f->display.x->wm_hints.flags |= IconPositionHint;
6037 f->display.x->wm_hints.icon_x = icon_x;
6038 f->display.x->wm_hints.icon_y = icon_y;
6039
6040 XSetWMHints (x_current_display, window, &f->display.x->wm_hints);
6041 }
6042
6043 \f
6044 /* Initialization. */
6045
6046 #ifdef USE_X_TOOLKIT
6047 static XrmOptionDescRec emacs_options[] = {
6048 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
6049 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
6050
6051 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
6052 XrmoptionSepArg, NULL},
6053 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
6054
6055 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
6056 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
6057 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
6058 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
6059 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
6060 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
6061 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
6062 };
6063 #endif /* USE_X_TOOLKIT */
6064
6065 void
6066 x_term_init (display_name, xrm_option, resource_name)
6067 char *display_name;
6068 char *xrm_option;
6069 char *resource_name;
6070 {
6071 Lisp_Object frame;
6072 char *defaultvalue;
6073 int argc = 0;
6074 char** argv = 0;
6075 #ifndef F_SETOWN_BUG
6076 #ifdef F_SETOWN
6077 extern int old_fcntl_owner;
6078 #endif /* ! defined (F_SETOWN) */
6079 #endif /* F_SETOWN_BUG */
6080
6081 x_focus_frame = x_highlight_frame = 0;
6082
6083 #ifdef USE_X_TOOLKIT
6084 argv = (char **) XtMalloc (7 * sizeof (char *));
6085 argv[0] = "";
6086 argv[1] = "-display";
6087 argv[2] = display_name;
6088 argv[3] = "-name";
6089 /* Usually `emacs', but not always. */
6090 argv[4] = resource_name;
6091 argc = 5;
6092 if (xrm_option)
6093 {
6094 argv[argc++] = "-xrm";
6095 argv[argc++] = xrm_option;
6096 }
6097 Xt_app_shell = XtAppInitialize (&Xt_app_con, "Emacs",
6098 emacs_options, XtNumber (emacs_options),
6099 &argc, argv,
6100 NULL, NULL, 0);
6101 XtFree ((char *)argv);
6102 x_current_display = XtDisplay (Xt_app_shell);
6103
6104 #else /* not USE_X_TOOLKIT */
6105 x_current_display = XOpenDisplay (display_name);
6106 #endif /* not USE_X_TOOLKIT */
6107 if (x_current_display == 0)
6108 fatal ("X server %s not responding.\n\
6109 Check the DISPLAY environment variable or use \"-d\"\n",
6110 display_name);
6111
6112 #ifdef HAVE_X11
6113 {
6114 #if 0
6115 XSetAfterFunction (x_current_display, x_trace_wire);
6116 #endif /* ! 0 */
6117 hostname = get_system_name ();
6118 x_id_name = (char *) xmalloc (XSTRING (Vinvocation_name)->size
6119 + strlen (hostname)
6120 + 2);
6121 sprintf (x_id_name, "%s@%s", XSTRING (Vinvocation_name)->data, hostname);
6122 }
6123
6124 /* Figure out which modifier bits mean what. */
6125 x_find_modifier_meanings ();
6126
6127 /* Get the scroll bar cursor. */
6128 x_vertical_scroll_bar_cursor
6129 = XCreateFontCursor (x_current_display, XC_sb_v_double_arrow);
6130
6131 #if 0
6132 /* Watch for PropertyNotify events on the root window; we use them
6133 to figure out when to invalidate our cache of the cut buffers. */
6134 x_watch_cut_buffer_cache ();
6135 #endif
6136
6137 if (ConnectionNumber (x_current_display) != 0)
6138 change_keyboard_wait_descriptor (ConnectionNumber (x_current_display));
6139 change_input_fd (ConnectionNumber (x_current_display));
6140
6141 #endif /* ! defined (HAVE_X11) */
6142
6143 #ifndef F_SETOWN_BUG
6144 #ifdef F_SETOWN
6145 old_fcntl_owner = fcntl (ConnectionNumber (x_current_display), F_GETOWN, 0);
6146 #ifdef F_SETOWN_SOCK_NEG
6147 /* stdin is a socket here */
6148 fcntl (ConnectionNumber (x_current_display), F_SETOWN, -getpid ());
6149 #else /* ! defined (F_SETOWN_SOCK_NEG) */
6150 fcntl (ConnectionNumber (x_current_display), F_SETOWN, getpid ());
6151 #endif /* ! defined (F_SETOWN_SOCK_NEG) */
6152 #endif /* ! defined (F_SETOWN) */
6153 #endif /* F_SETOWN_BUG */
6154
6155 #ifdef SIGIO
6156 init_sigio ();
6157 #endif /* ! defined (SIGIO) */
6158
6159 expose_all_windows = 0;
6160
6161 clear_frame_hook = XTclear_frame;
6162 clear_end_of_line_hook = XTclear_end_of_line;
6163 ins_del_lines_hook = XTins_del_lines;
6164 change_line_highlight_hook = XTchange_line_highlight;
6165 insert_glyphs_hook = XTinsert_glyphs;
6166 write_glyphs_hook = XTwrite_glyphs;
6167 delete_glyphs_hook = XTdelete_glyphs;
6168 ring_bell_hook = XTring_bell;
6169 reset_terminal_modes_hook = XTreset_terminal_modes;
6170 set_terminal_modes_hook = XTset_terminal_modes;
6171 update_begin_hook = XTupdate_begin;
6172 update_end_hook = XTupdate_end;
6173 set_terminal_window_hook = XTset_terminal_window;
6174 read_socket_hook = XTread_socket;
6175 frame_up_to_date_hook = XTframe_up_to_date;
6176 cursor_to_hook = XTcursor_to;
6177 reassert_line_highlight_hook = XTreassert_line_highlight;
6178 mouse_position_hook = XTmouse_position;
6179 frame_rehighlight_hook = XTframe_rehighlight;
6180 frame_raise_lower_hook = XTframe_raise_lower;
6181 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
6182 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
6183 redeem_scroll_bar_hook = XTredeem_scroll_bar;
6184 judge_scroll_bars_hook = XTjudge_scroll_bars;
6185
6186 scroll_region_ok = 1; /* we'll scroll partial frames */
6187 char_ins_del_ok = 0; /* just as fast to write the line */
6188 line_ins_del_ok = 1; /* we'll just blt 'em */
6189 fast_clear_end_of_line = 1; /* X does this well */
6190 memory_below_frame = 0; /* we don't remember what scrolls
6191 off the bottom */
6192 baud_rate = 19200;
6193
6194 /* Try to use interrupt input; if we can't, then start polling. */
6195 Fset_input_mode (Qt, Qnil, Qt, Qnil);
6196
6197 /* Note that there is no real way portable across R3/R4 to get the
6198 original error handler. */
6199 XHandleError (x_error_quitter);
6200 XHandleIOError (x_io_error_quitter);
6201
6202 /* Disable Window Change signals; they are handled by X events. */
6203 #ifdef SIGWINCH
6204 signal (SIGWINCH, SIG_DFL);
6205 #endif /* ! defined (SIGWINCH) */
6206
6207 signal (SIGPIPE, x_connection_closed);
6208 }
6209
6210 void
6211 syms_of_xterm ()
6212 {
6213 staticpro (&last_mouse_scroll_bar);
6214 last_mouse_scroll_bar = Qnil;
6215 staticpro (&mouse_face_window);
6216 mouse_face_window = Qnil;
6217 }
6218 #endif /* ! defined (HAVE_X11) */
6219 #endif /* ! defined (HAVE_X_WINDOWS) */