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