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