(Fget_buffer_create): Disallow empty string.
[emacs.git] / src / xterm.c
blob8d77ea4108dd5679736545c9dc2012a0b1380948
1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1989, 1993, 1994 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
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.
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.
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. */
20 /* Xt features made by Fred Pierresteguy. */
22 #define NEW_SELECTIONS
24 /* On 4.3 these lose if they come after xterm.h. */
25 /* On HP-UX 8.0 signal.h loses if it comes after config.h. */
26 /* Putting these at the beginning seems to be standard for other .c files. */
27 #include <stdio.h>
28 #include <signal.h>
30 #include <config.h>
32 /* Need syssignal.h for various externs and definitions that may be required
33 by some configurations for calls to signal later in this source file. */
34 #include "syssignal.h"
36 #ifdef HAVE_X_WINDOWS
38 #include "lisp.h"
39 #include "blockinput.h"
41 /* This may include sys/types.h, and that somehow loses
42 if this is not done before the other system files. */
43 #include "xterm.h"
44 #include <X11/cursorfont.h>
46 #ifndef USG
47 /* Load sys/types.h if not already loaded.
48 In some systems loading it twice is suicidal. */
49 #ifndef makedev
50 #include <sys/types.h>
51 #endif /* makedev */
52 #endif /* USG */
54 #ifdef BSD
55 #include <sys/ioctl.h>
56 #include <strings.h>
57 #else /* ! defined (BSD) */
58 #ifndef VMS
59 #include <string.h>
60 #endif
61 #endif /* ! defined (BSD) */
63 #include "systty.h"
64 #include "systime.h"
66 #ifndef INCLUDED_FCNTL
67 #include <fcntl.h>
68 #endif
69 #include <ctype.h>
70 #include <errno.h>
71 #include <setjmp.h>
72 #include <sys/stat.h>
73 #include <sys/param.h>
75 #include "dispextern.h"
76 #include "termhooks.h"
77 #include "termopts.h"
78 #include "termchar.h"
79 #if 0
80 #include "sink.h"
81 #include "sinkmask.h"
82 #endif /* ! 0 */
83 #include "gnu.h"
84 #include "frame.h"
85 #include "disptab.h"
86 #include "buffer.h"
87 #include "window.h"
89 #ifdef USE_X_TOOLKIT
90 extern XtAppContext Xt_app_con;
91 extern Widget Xt_app_shell;
92 extern void free_frame_menubar ();
93 #endif /* USE_X_TOOLKIT */
95 #ifndef USE_X_TOOLKIT
96 #define x_any_window_to_frame x_window_to_frame
97 #endif
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) */
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
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 */
127 /* For sending Meta-characters. Do we need this? */
128 #define METABIT 0200
130 #define min(a,b) ((a)<(b) ? (a) : (b))
131 #define max(a,b) ((a)>(b) ? (a) : (b))
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.
137 This is never needed under X11. */
138 static int expose_all_windows;
140 /* Nonzero means we must reprint all icon windows. */
142 static int expose_all_icons;
144 #ifndef HAVE_X11
145 /* ExposeRegion events, when received, are copied into this queue
146 for later processing. */
148 static struct event_queue x_expose_queue;
150 /* ButtonPress and ButtonReleased events, when received,
151 are copied into this queue for later processing. */
153 struct event_queue x_mouse_queue;
154 #endif /* HAVE_X11 */
156 #if defined (SIGIO) && defined (FIONREAD)
157 int BLOCK_INPUT_mask;
158 #endif /* ! defined (SIGIO) && defined (FIONREAD) */
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. */
164 static Bitmap icon_bitmap;
166 /* Font used for text icons. */
168 static FONT_TYPE *icon_font_info;
170 /* Stuff for dealing with the main icon title. */
172 extern Lisp_Object Vcommand_line_args;
173 char *hostname, *x_id_name;
175 /* This is the X connection that we are using. */
177 Display *x_current_display;
179 /* The cursor to use for vertical scroll bars on x_current_display. */
180 static Cursor x_vertical_scroll_bar_cursor;
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;
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;
196 /* The last frame mentioned in a FocusIn or FocusOut event. This is
197 separate from x_focus_frame, because whether or not LeaveNotify
198 events cause us to lose focus depends on whether or not we have
199 received a FocusIn event for it. */
200 struct frame *x_focus_event_frame;
202 /* The frame which currently has the visual highlight, and should get
203 keyboard input (other sorts of input have the frame encoded in the
204 event). It points to the X focus frame's selected window's
205 frame. It differs from x_focus_frame when we're using a global
206 minibuffer. */
207 static struct frame *x_highlight_frame;
209 /* From .Xdefaults, the value of "emacs.WarpMouse". If non-zero,
210 mouse is moved to inside of frame when frame is de-iconified. */
212 static int warp_mouse_on_deiconify;
214 /* During an update, maximum vpos for ins/del line operations to affect. */
216 static int flexlines;
218 /* During an update, nonzero if chars output now should be highlighted. */
220 static int highlight;
222 /* Nominal cursor position -- where to draw output.
223 During an update, these are different from the cursor-box position. */
225 static int curs_x;
226 static int curs_y;
228 /* Mouse movement.
230 In order to avoid asking for motion events and then throwing most
231 of them away or busy-polling the server for mouse positions, we ask
232 the server for pointer motion hints. This means that we get only
233 one event per group of mouse movements. "Groups" are delimited by
234 other kinds of events (focus changes and button clicks, for
235 example), or by XQueryPointer calls; when one of these happens, we
236 get another MotionNotify event the next time the mouse moves. This
237 is at least as efficient as getting motion events when mouse
238 tracking is on, and I suspect only negligibly worse when tracking
239 is off.
241 The silly O'Reilly & Associates Nutshell guides barely document
242 pointer motion hints at all (I think you have to infer how they
243 work from an example), and the description of XQueryPointer doesn't
244 mention that calling it causes you to get another motion hint from
245 the server, which is very important. */
247 /* Where the mouse was last time we reported a mouse event. */
248 static FRAME_PTR last_mouse_frame;
249 static XRectangle last_mouse_glyph;
251 /* The scroll bar in which the last X motion event occurred.
253 If the last X motion event occurred in a scroll bar, we set this
254 so XTmouse_position can know whether to report a scroll bar motion or
255 an ordinary motion.
257 If the last X motion event didn't occur in a scroll bar, we set this
258 to Qnil, to tell XTmouse_position to return an ordinary motion event. */
259 static Lisp_Object last_mouse_scroll_bar;
261 /* Record which buttons are currently pressed. */
262 unsigned int x_mouse_grabbed;
264 /* This is a hack. We would really prefer that XTmouse_position would
265 return the time associated with the position it returns, but there
266 doesn't seem to be any way to wrest the timestamp from the server
267 along with the position query. So, we just keep track of the time
268 of the last movement we received, and return that in hopes that
269 it's somewhat accurate. */
270 static Time last_mouse_movement_time;
272 /* These variables describe the range of text currently shown
273 in its mouse-face, together with the window they apply to.
274 As long as the mouse stays within this range, we need not
275 redraw anything on its account. */
276 static int mouse_face_beg, mouse_face_end;
277 static Lisp_Object mouse_face_window;
278 static int mouse_face_face_id;
280 /* 1 if a mouse motion event came and we didn't handle it right away because
281 gc was in progress. */
282 static int mouse_face_deferred_gc;
284 /* FRAME and X, Y position of mouse when last checked for highlighting. */
285 static FRAME_PTR mouse_face_mouse_frame;
286 static int mouse_face_mouse_x, mouse_face_mouse_y;
288 /* Nonzero means defer mouse-motion highlighting. */
289 static int mouse_face_defer;
291 #ifdef HAVE_X11
292 /* `t' if a mouse button is depressed. */
294 extern Lisp_Object Vmouse_depressed;
296 /* Tells if a window manager is present or not. */
298 extern Lisp_Object Vx_no_window_manager;
300 /* Timestamp that we requested selection data was made. */
301 extern Time requestor_time;
303 /* ID of the window requesting selection data. */
304 extern Window requestor_window;
306 /* Nonzero enables some debugging for the X interface code. */
307 extern int _Xdebug;
309 extern Qface, Qmouse_face;
311 #else /* ! defined (HAVE_X11) */
313 /* Bit patterns for the mouse cursor. */
315 short MouseCursor[] = {
316 0x0000, 0x0008, 0x0018, 0x0038,
317 0x0078, 0x00f8, 0x01f8, 0x03f8,
318 0x07f8, 0x00f8, 0x00d8, 0x0188,
319 0x0180, 0x0300, 0x0300, 0x0000};
321 short MouseMask[] = {
322 0x000c, 0x001c, 0x003c, 0x007c,
323 0x00fc, 0x01fc, 0x03fc, 0x07fc,
324 0x0ffc, 0x0ffc, 0x01fc, 0x03dc,
325 0x03cc, 0x0780, 0x0780, 0x0300};
327 static short grey_bits[] = {
328 0x0005, 0x000a, 0x0005, 0x000a};
330 static Pixmap GreyPixmap = 0;
331 #endif /* ! defined (HAVE_X11) */
333 /* From time to time we get info on an Emacs window, here. */
335 static WINDOWINFO_TYPE windowinfo;
337 extern int errno;
339 /* A mask of extra modifier bits to put into every keyboard char. */
340 extern int extra_keyboard_modifiers;
342 extern Display *XOpenDisplay ();
343 extern Window XCreateWindow ();
345 extern Cursor XCreateCursor ();
346 extern FONT_TYPE *XOpenFont ();
348 static void flashback ();
349 static void redraw_previous_char ();
350 static unsigned int x_x_to_emacs_modifiers ();
352 static void note_mouse_highlight ();
353 static void clear_mouse_face ();
354 static void show_mouse_face ();
356 #ifndef HAVE_X11
357 static void dumpqueue ();
358 #endif /* HAVE_X11 */
360 void dumpborder ();
361 static int XTcursor_to ();
362 static int XTclear_end_of_line ();
365 /* Starting and ending updates.
367 These hooks are called by update_frame at the beginning and end
368 of a frame update. We record in `updating_frame' the identity
369 of the frame being updated, so that the XT... functions do not
370 need to take a frame as argument. Most of the XT... functions
371 should never be called except during an update, the only exceptions
372 being XTcursor_to, XTwrite_glyphs and XTreassert_line_highlight. */
374 extern int mouse_track_top, mouse_track_left, mouse_track_width;
376 static
377 XTupdate_begin (f)
378 struct frame *f;
380 int mask;
382 if (f == 0)
383 abort ();
385 flexlines = f->height;
386 highlight = 0;
388 BLOCK_INPUT;
390 if (f == mouse_face_mouse_frame)
392 /* Don't do highlighting for mouse motion during the update. */
393 mouse_face_defer = 1;
394 if (!NILP (mouse_face_window))
396 int firstline, lastline, i;
397 struct window *w = XWINDOW (mouse_face_window);
399 /* Find the first, and the last+1, lines affected by redisplay. */
400 for (firstline = 0; firstline < f->height; firstline++)
401 if (FRAME_DESIRED_GLYPHS (f)->enable[firstline])
402 break;
404 lastline = f->height;
405 for (i = f->height - 1; i >= 0; i--)
407 if (FRAME_DESIRED_GLYPHS (f)->enable[i])
408 break;
409 else
410 lastline = i;
413 /* Can we tell that this update does not affect the window
414 where the mouse highlight is? If so, no need to turn off. */
415 if (! (firstline > (XFASTINT (w->top) + window_internal_height (w))
416 || lastline < XFASTINT (w->top)))
417 /* Otherwise turn off the mouse highlight now. */
418 clear_mouse_face ();
421 #ifndef HAVE_X11
422 dumpqueue ();
423 #endif /* HAVE_X11 */
424 UNBLOCK_INPUT;
427 #ifndef HAVE_X11
428 static void x_do_pending_expose ();
429 #endif
431 static
432 XTupdate_end (f)
433 struct frame *f;
435 int mask;
437 BLOCK_INPUT;
438 #ifndef HAVE_X11
439 dumpqueue ();
440 x_do_pending_expose ();
441 #endif /* HAVE_X11 */
443 x_display_cursor (f, 1);
445 if (f == mouse_face_mouse_frame)
446 mouse_face_defer = 0;
447 #if 0
448 /* This fails in the case of having updated only the echo area
449 if we have switched buffers. In that case, FRAME_CURRENT_GLYPHS
450 has no relation to the current contents, and its charstarts
451 have no relation to the contents of the window-buffer.
452 I don't know a clean way to check
453 for that case. window_end_valid isn't set up yet. */
454 if (f == mouse_face_mouse_frame)
455 note_mouse_highlight (f, mouse_face_mouse_x, mouse_face_mouse_y);
456 #endif
458 XFlushQueue ();
459 UNBLOCK_INPUT;
462 /* This is called after a redisplay on frame F. */
464 static
465 XTframe_up_to_date (f)
466 FRAME_PTR f;
468 if (mouse_face_deferred_gc || f == mouse_face_mouse_frame)
470 note_mouse_highlight (mouse_face_mouse_frame,
471 mouse_face_mouse_x, mouse_face_mouse_y);
472 mouse_face_deferred_gc = 0;
476 /* External interface to control of standout mode.
477 Call this when about to modify line at position VPOS
478 and not change whether it is highlighted. */
480 XTreassert_line_highlight (new, vpos)
481 int new, vpos;
483 highlight = new;
486 /* Call this when about to modify line at position VPOS
487 and change whether it is highlighted. */
489 static
490 XTchange_line_highlight (new_highlight, vpos, first_unused_hpos)
491 int new_highlight, vpos, first_unused_hpos;
493 highlight = new_highlight;
494 XTcursor_to (vpos, 0);
495 XTclear_end_of_line (updating_frame->width);
498 /* This is used when starting Emacs and when restarting after suspend.
499 When starting Emacs, no X window is mapped. And nothing must be done
500 to Emacs's own window if it is suspended (though that rarely happens). */
502 static
503 XTset_terminal_modes ()
507 /* This is called when exiting or suspending Emacs.
508 Exiting will make the X-windows go away, and suspending
509 requires no action. */
511 static
512 XTreset_terminal_modes ()
514 /* XTclear_frame (); */
517 /* Set the nominal cursor position of the frame.
518 This is where display update commands will take effect.
519 This does not affect the place where the cursor-box is displayed. */
521 static int
522 XTcursor_to (row, col)
523 register int row, col;
525 int mask;
526 int orow = row;
528 curs_x = col;
529 curs_y = row;
531 if (updating_frame == 0)
533 BLOCK_INPUT;
534 x_display_cursor (selected_frame, 1);
535 XFlushQueue ();
536 UNBLOCK_INPUT;
540 /* Display a sequence of N glyphs found at GP.
541 WINDOW is the x-window to output to. LEFT and TOP are starting coords.
542 HL is 1 if this text is highlighted, 2 if the cursor is on it,
543 3 if should appear in its mouse-face.
545 FONT is the default font to use (for glyphs whose font-code is 0).
547 Since the display generation code is responsible for calling
548 compute_char_face and compute_glyph_face on everything it puts in
549 the display structure, we can assume that the face code on each
550 glyph is a valid index into FRAME_COMPUTED_FACES (f), and the one
551 to which we can actually apply intern_face.
552 Call this function with input blocked. */
554 #if 1
555 /* This is the multi-face code. */
557 static void
558 dumpglyphs (f, left, top, gp, n, hl)
559 struct frame *f;
560 int left, top;
561 register GLYPH *gp; /* Points to first GLYPH. */
562 register int n; /* Number of glyphs to display. */
563 int hl;
565 /* Holds characters to be displayed. */
566 char *buf = (char *) alloca (f->width * sizeof (*buf));
567 register char *cp; /* Steps through buf[]. */
568 register int tlen = GLYPH_TABLE_LENGTH;
569 register Lisp_Object *tbase = GLYPH_TABLE_BASE;
570 Window window = FRAME_X_WINDOW (f);
572 while (n > 0)
574 /* Get the face-code of the next GLYPH. */
575 int cf, len;
576 int g = *gp;
578 GLYPH_FOLLOW_ALIASES (tbase, tlen, g);
579 cf = FAST_GLYPH_FACE (g);
581 /* Find the run of consecutive glyphs with the same face-code.
582 Extract their character codes into BUF. */
583 cp = buf;
584 while (n > 0)
586 g = *gp;
587 GLYPH_FOLLOW_ALIASES (tbase, tlen, g);
588 if (FAST_GLYPH_FACE (g) != cf)
589 break;
591 *cp++ = FAST_GLYPH_CHAR (g);
592 --n;
593 ++gp;
596 /* LEN gets the length of the run. */
597 len = cp - buf;
599 /* Now output this run of chars, with the font and pixel values
600 determined by the face code CF. */
602 struct face *face = FRAME_DEFAULT_FACE (f);
603 FONT_TYPE *font = FACE_FONT (face);
604 GC gc = FACE_GC (face);
605 int defaulted = 1;
606 int gc_temporary = 0;
608 /* HL = 3 means use a mouse face previously chosen. */
609 if (hl == 3)
610 cf = mouse_face_face_id;
612 /* First look at the face of the text itself. */
613 if (cf != 0)
615 /* It's possible for the display table to specify
616 a face code that is out of range. Use 0 in that case. */
617 if (cf < 0 || cf >= FRAME_N_COMPUTED_FACES (f)
618 || FRAME_COMPUTED_FACES (f) [cf] == 0)
619 cf = 0;
621 if (cf == 1)
622 face = FRAME_MODE_LINE_FACE (f);
623 else
624 face = intern_face (f, FRAME_COMPUTED_FACES (f) [cf]);
625 font = FACE_FONT (face);
626 gc = FACE_GC (face);
627 defaulted = 0;
630 /* Then comes the distinction between modeline and normal text. */
631 else if (hl == 0)
633 else if (hl == 1)
635 face = FRAME_MODE_LINE_FACE (f);
636 font = FACE_FONT (face);
637 gc = FACE_GC (face);
638 defaulted = 0;
641 #define FACE_DEFAULT (~0)
643 /* Now override that if the cursor's on this character. */
644 if (hl == 2)
646 if (defaulted
647 || !face->font
648 || (int) face->font == FACE_DEFAULT)
650 gc = f->display.x->cursor_gc;
652 /* Cursor on non-default face: must merge. */
653 else
655 XGCValues xgcv;
656 unsigned long mask;
658 xgcv.background = f->display.x->cursor_pixel;
659 if (face == FRAME_DEFAULT_FACE (f))
660 xgcv.foreground = f->display.x->cursor_foreground_pixel;
661 else
662 xgcv.foreground = face->foreground;
663 /* If the glyph would be invisible,
664 try a different foreground. */
665 if (xgcv.foreground == xgcv.background)
666 xgcv.foreground = face->background;
667 if (xgcv.foreground == xgcv.background)
668 xgcv.foreground = f->display.x->cursor_foreground_pixel;
669 if (xgcv.foreground == xgcv.background)
670 xgcv.foreground = face->foreground;
671 /* Make sure the cursor is distinct from text in this face. */
672 if (xgcv.background == face->background
673 && xgcv.foreground == face->foreground)
675 xgcv.background = face->foreground;
676 xgcv.foreground = face->background;
678 xgcv.font = face->font->fid;
679 xgcv.graphics_exposures = 0;
680 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
681 gc = XCreateGC (x_current_display, FRAME_X_WINDOW (f),
682 mask, &xgcv);
683 #if 0
684 if (face->stipple && face->stipple != FACE_DEFAULT)
685 XSetStipple (x_current_display, gc, face->stipple);
686 #endif
687 gc_temporary = 1;
691 if ((int) font == FACE_DEFAULT)
692 font = f->display.x->font;
694 XDrawImageString (x_current_display, window, gc,
695 left, top + FONT_BASE (font), buf, len);
697 if (gc_temporary)
698 XFreeGC (x_current_display, gc);
700 /* We should probably check for XA_UNDERLINE_POSITION and
701 XA_UNDERLINE_THICKNESS properties on the font, but let's
702 just get the thing working, and come back to that. */
704 int underline_position = 1;
706 if (font->descent <= underline_position)
707 underline_position = font->descent - 1;
709 if (face->underline)
710 XFillRectangle (x_current_display, FRAME_X_WINDOW (f),
711 FACE_GC (face),
712 left, (top
713 + FONT_BASE (font)
714 + underline_position),
715 len * FONT_WIDTH (font), 1);
718 left += len * FONT_WIDTH (font);
722 #endif /* 1 */
724 #if 0
725 /* This is the old single-face code. */
727 static void
728 dumpglyphs (f, left, top, gp, n, hl, font)
729 struct frame *f;
730 int left, top;
731 register GLYPH *gp; /* Points to first GLYPH. */
732 register int n; /* Number of glyphs to display. */
733 int hl;
734 FONT_TYPE *font;
736 register int len;
737 Window window = FRAME_X_WINDOW (f);
738 GC drawing_gc = (hl == 2 ? f->display.x->cursor_gc
739 : (hl ? f->display.x->reverse_gc
740 : f->display.x->normal_gc));
742 if (sizeof (GLYPH) == sizeof (XChar2b))
743 XDrawImageString16 (x_current_display, window, drawing_gc,
744 left, top + FONT_BASE (font), (XChar2b *) gp, n);
745 else if (sizeof (GLYPH) == sizeof (unsigned char))
746 XDrawImageString (x_current_display, window, drawing_gc,
747 left, top + FONT_BASE (font), (char *) gp, n);
748 else
749 /* What size of glyph ARE you using? And does X have a function to
750 draw them? */
751 abort ();
753 #endif
755 /* Output some text at the nominal frame cursor position.
756 Advance the cursor over the text.
757 Output LEN glyphs at START.
759 `highlight', set up by XTreassert_line_highlight or XTchange_line_highlight,
760 controls the pixel values used for foreground and background. */
762 static
763 XTwrite_glyphs (start, len)
764 register GLYPH *start;
765 int len;
767 register int temp_length;
768 int mask;
769 struct frame *f;
771 BLOCK_INPUT;
773 f = updating_frame;
774 if (f == 0)
776 f = selected_frame;
777 /* If not within an update,
778 output at the frame's visible cursor. */
779 curs_x = f->cursor_x;
780 curs_y = f->cursor_y;
783 dumpglyphs (f,
784 CHAR_TO_PIXEL_COL (f, curs_x),
785 CHAR_TO_PIXEL_ROW (f, curs_y),
786 start, len, highlight);
788 /* If we drew on top of the cursor, note that it is turned off. */
789 if (curs_y == f->phys_cursor_y
790 && curs_x <= f->phys_cursor_x
791 && curs_x + len > f->phys_cursor_x)
792 f->phys_cursor_x = -1;
794 if (updating_frame == 0)
796 f->cursor_x += len;
797 x_display_cursor (f, 1);
798 f->cursor_x -= len;
800 else
801 curs_x += len;
803 UNBLOCK_INPUT;
806 /* Clear to the end of the line.
807 Erase the current text line from the nominal cursor position (inclusive)
808 to column FIRST_UNUSED (exclusive). The idea is that everything
809 from FIRST_UNUSED onward is already erased. */
811 static int
812 XTclear_end_of_line (first_unused)
813 register int first_unused;
815 struct frame *f = updating_frame;
816 int mask;
818 if (f == 0)
819 abort ();
821 if (curs_y < 0 || curs_y >= f->height)
822 return;
823 if (first_unused <= 0)
824 return;
826 if (first_unused >= f->width)
827 first_unused = f->width;
829 BLOCK_INPUT;
831 /* Notice if the cursor will be cleared by this operation. */
832 if (curs_y == f->phys_cursor_y
833 && curs_x <= f->phys_cursor_x
834 && f->phys_cursor_x < first_unused)
835 f->phys_cursor_x = -1;
837 #ifdef HAVE_X11
838 XClearArea (x_current_display, FRAME_X_WINDOW (f),
839 CHAR_TO_PIXEL_COL (f, curs_x),
840 CHAR_TO_PIXEL_ROW (f, curs_y),
841 FONT_WIDTH (f->display.x->font) * (first_unused - curs_x),
842 FONT_HEIGHT (f->display.x->font), False);
843 #if 0
844 redraw_previous_char (f, curs_x, curs_y);
845 #endif
846 #else /* ! defined (HAVE_X11) */
847 XPixSet (FRAME_X_WINDOW (f),
848 CHAR_TO_PIXEL_COL (f, curs_x),
849 CHAR_TO_PIXEL_ROW (f, curs_y),
850 FONT_WIDTH (f->display.x->font) * (first_unused - curs_x),
851 FONT_HEIGHT (f->display.x->font),
852 f->display.x->background_pixel);
853 #endif /* ! defined (HAVE_X11) */
855 UNBLOCK_INPUT;
858 /* Erase the character (if any) at the position just before X, Y in frame F,
859 then redraw it and the character before it.
860 This is necessary when we erase starting at X,
861 in case the character after X overlaps into the one before X.
862 Call this function with input blocked. */
864 static void
865 redraw_previous_char (f, x, y)
866 FRAME_PTR f;
867 int x, y;
869 /* Erase the character before the new ones, in case
870 what was here before overlaps it.
871 Reoutput that character, and the previous character
872 (in case the previous character overlaps it). */
873 if (x > 0)
875 int start_x = x - 2;
876 if (start_x < 0)
877 start_x = 0;
878 XClearArea (x_current_display, FRAME_X_WINDOW (f),
879 CHAR_TO_PIXEL_COL (f, x - 1),
880 CHAR_TO_PIXEL_ROW (f, y),
881 FONT_WIDTH (f->display.x->font),
882 FONT_HEIGHT (f->display.x->font), False);
884 dumpglyphs (f, CHAR_TO_PIXEL_COL (f, start_x),
885 CHAR_TO_PIXEL_ROW (f, y),
886 &FRAME_CURRENT_GLYPHS (f)->glyphs[y][start_x],
887 x - start_x, highlight);
891 static
892 XTclear_frame ()
894 int mask;
895 struct frame *f = updating_frame;
897 if (f == 0)
898 f = selected_frame;
900 f->phys_cursor_x = -1; /* Cursor not visible. */
901 curs_x = 0; /* Nominal cursor position is top left. */
902 curs_y = 0;
904 BLOCK_INPUT;
906 XClear (FRAME_X_WINDOW (f));
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);
912 #ifndef HAVE_X11
913 dumpborder (f, 0);
914 #endif /* HAVE_X11 */
916 XFlushQueue ();
917 UNBLOCK_INPUT;
920 /* Invert the middle quarter of the frame for .15 sec. */
922 /* We use the select system call to do the waiting, so we have to make sure
923 it's available. If it isn't, we just won't do visual bells. */
924 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
926 /* Subtract the `struct timeval' values X and Y,
927 storing the result in RESULT.
928 Return 1 if the difference is negative, otherwise 0. */
930 static int
931 timeval_subtract (result, x, y)
932 struct timeval *result, x, y;
934 /* Perform the carry for the later subtraction by updating y.
935 This is safer because on some systems
936 the tv_sec member is unsigned. */
937 if (x.tv_usec < y.tv_usec)
939 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
940 y.tv_usec -= 1000000 * nsec;
941 y.tv_sec += nsec;
943 if (x.tv_usec - y.tv_usec > 1000000)
945 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
946 y.tv_usec += 1000000 * nsec;
947 y.tv_sec -= nsec;
950 /* Compute the time remaining to wait. tv_usec is certainly positive. */
951 result->tv_sec = x.tv_sec - y.tv_sec;
952 result->tv_usec = x.tv_usec - y.tv_usec;
954 /* Return indication of whether the result should be considered negative. */
955 return x.tv_sec < y.tv_sec;
958 XTflash (f)
959 struct frame *f;
961 BLOCK_INPUT;
964 GC gc;
966 /* Create a GC that will use the GXxor function to flip foreground pixels
967 into background pixels. */
969 XGCValues values;
971 values.function = GXxor;
972 values.foreground = (f->display.x->foreground_pixel
973 ^ f->display.x->background_pixel);
975 gc = XCreateGC (x_current_display, FRAME_X_WINDOW (f),
976 GCFunction | GCForeground, &values);
980 int width = PIXEL_WIDTH (f);
981 int height = PIXEL_HEIGHT (f);
983 XFillRectangle (x_current_display, FRAME_X_WINDOW (f), gc,
984 width/4, height/4, width/2, height/2);
985 XFlush (x_current_display);
988 struct timeval wakeup, now;
990 EMACS_GET_TIME (wakeup);
992 /* Compute time to wait until, propagating carry from usecs. */
993 wakeup.tv_usec += 150000;
994 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
995 wakeup.tv_usec %= 1000000;
997 /* Keep waiting until past the time wakeup. */
998 while (1)
1000 struct timeval timeout;
1002 EMACS_GET_TIME (timeout);
1004 /* In effect, timeout = wakeup - timeout.
1005 Break if result would be negative. */
1006 if (timeval_subtract (&timeout, wakeup, timeout))
1007 break;
1009 /* Try to wait that long--but we might wake up sooner. */
1010 select (0, 0, 0, 0, &timeout);
1014 XFillRectangle (x_current_display, FRAME_X_WINDOW (f), gc,
1015 width/4, height/4, width/2, height/2);
1016 XFreeGC (x_current_display, gc);
1017 XFlush (x_current_display);
1021 UNBLOCK_INPUT;
1024 #endif
1027 /* Make audible bell. */
1029 #ifdef HAVE_X11
1030 #define XRINGBELL XBell (x_current_display, 0)
1031 #else /* ! defined (HAVE_X11) */
1032 #define XRINGBELL XFeep (0);
1033 #endif /* ! defined (HAVE_X11) */
1035 XTring_bell ()
1037 if (x_current_display == 0)
1038 return;
1040 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
1041 if (visible_bell)
1042 XTflash (selected_frame);
1043 else
1044 #endif
1046 BLOCK_INPUT;
1047 XRINGBELL;
1048 XFlushQueue ();
1049 UNBLOCK_INPUT;
1053 /* Insert and delete character.
1054 These are not supposed to be used because we are supposed to turn
1055 off the feature of using them. */
1057 static
1058 XTinsert_glyphs (start, len)
1059 register char *start;
1060 register int len;
1062 abort ();
1065 static
1066 XTdelete_glyphs (n)
1067 register int n;
1069 abort ();
1072 /* Specify how many text lines, from the top of the window,
1073 should be affected by insert-lines and delete-lines operations.
1074 This, and those operations, are used only within an update
1075 that is bounded by calls to XTupdate_begin and XTupdate_end. */
1077 static
1078 XTset_terminal_window (n)
1079 register int n;
1081 if (updating_frame == 0)
1082 abort ();
1084 if ((n <= 0) || (n > updating_frame->height))
1085 flexlines = updating_frame->height;
1086 else
1087 flexlines = n;
1090 /* Perform an insert-lines operation.
1091 Insert N lines at a vertical position curs_y. */
1093 static void
1094 stufflines (n)
1095 register int n;
1097 register int topregion, bottomregion;
1098 register int length, newtop, mask;
1099 register struct frame *f = updating_frame;
1100 int intborder = f->display.x->internal_border_width;
1102 if (curs_y >= flexlines)
1103 return;
1105 topregion = curs_y;
1106 bottomregion = flexlines - (n + 1);
1107 newtop = topregion + n;
1108 length = (bottomregion - topregion) + 1;
1110 #ifndef HAVE_X11
1111 dumpqueue ();
1112 #endif /* HAVE_X11 */
1114 if ((length > 0) && (newtop <= flexlines))
1116 #ifdef HAVE_X11
1117 XCopyArea (x_current_display, FRAME_X_WINDOW (f),
1118 FRAME_X_WINDOW (f), f->display.x->normal_gc,
1119 intborder, CHAR_TO_PIXEL_ROW (f, topregion),
1120 f->width * FONT_WIDTH (f->display.x->font),
1121 length * FONT_HEIGHT (f->display.x->font), intborder,
1122 CHAR_TO_PIXEL_ROW (f, newtop));
1123 #else /* ! defined (HAVE_X11) */
1124 XMoveArea (FRAME_X_WINDOW (f),
1125 intborder, CHAR_TO_PIXEL_ROW (f, topregion),
1126 intborder, CHAR_TO_PIXEL_ROW (f, newtop),
1127 f->width * FONT_WIDTH (f->display.x->font),
1128 length * FONT_HEIGHT (f->display.x->font));
1129 /* Now we must process any ExposeRegion events that occur
1130 if the area being copied from is obscured.
1131 We can't let it wait because further i/d operations
1132 may want to copy this area to another area. */
1133 x_read_exposes ();
1134 #endif /* ! defined (HAVE_X11) */
1137 newtop = min (newtop, (flexlines - 1));
1138 length = newtop - topregion;
1139 if (length > 0)
1141 #ifdef HAVE_X11
1142 XClearArea (x_current_display, FRAME_X_WINDOW (f), intborder,
1143 CHAR_TO_PIXEL_ROW (f, topregion),
1144 f->width * FONT_WIDTH (f->display.x->font),
1145 n * FONT_HEIGHT (f->display.x->font), False);
1146 #else /* ! defined (HAVE_X11) */
1147 XPixSet (FRAME_X_WINDOW (f),
1148 intborder,
1149 CHAR_TO_PIXEL_ROW (f, topregion),
1150 f->width * FONT_WIDTH (f->display.x->font),
1151 n * FONT_HEIGHT (f->display.x->font),
1152 f->display.x->background_pixel);
1153 #endif /* ! defined (HAVE_X11) */
1157 /* Perform a delete-lines operation, deleting N lines
1158 at a vertical position curs_y. */
1160 static void
1161 scraplines (n)
1162 register int n;
1164 int mask;
1165 register struct frame *f = updating_frame;
1166 int intborder = f->display.x->internal_border_width;
1168 if (curs_y >= flexlines)
1169 return;
1171 #ifndef HAVE_X11
1172 dumpqueue ();
1173 #endif /* HAVE_X11 */
1175 if ((curs_y + n) >= flexlines)
1177 if (flexlines >= (curs_y + 1))
1179 #ifdef HAVE_X11
1180 XClearArea (x_current_display, FRAME_X_WINDOW (f), intborder,
1181 CHAR_TO_PIXEL_ROW (f, curs_y),
1182 f->width * FONT_WIDTH (f->display.x->font),
1183 (flexlines - curs_y) * FONT_HEIGHT (f->display.x->font), False);
1184 #else /* ! defined (HAVE_X11) */
1185 XPixSet (FRAME_X_WINDOW (f),
1186 intborder, CHAR_TO_PIXEL_ROW (f, curs_y),
1187 f->width * FONT_WIDTH (f->display.x->font),
1188 (flexlines - curs_y) * FONT_HEIGHT (f->display.x->font),
1189 f->display.x->background_pixel);
1190 #endif /* ! defined (HAVE_X11) */
1193 else
1195 #ifdef HAVE_X11
1196 XCopyArea (x_current_display, FRAME_X_WINDOW (f),
1197 FRAME_X_WINDOW (f), f->display.x->normal_gc,
1198 intborder,
1199 CHAR_TO_PIXEL_ROW (f, curs_y + n),
1200 f->width * FONT_WIDTH (f->display.x->font),
1201 (flexlines - (curs_y + n)) * FONT_HEIGHT (f->display.x->font),
1202 intborder, CHAR_TO_PIXEL_ROW (f, curs_y));
1203 XClearArea (x_current_display, FRAME_X_WINDOW (f),
1204 intborder,
1205 CHAR_TO_PIXEL_ROW (f, flexlines - n),
1206 f->width * FONT_WIDTH (f->display.x->font),
1207 n * FONT_HEIGHT (f->display.x->font), False);
1208 #else /* ! defined (HAVE_X11) */
1209 XMoveArea (FRAME_X_WINDOW (f),
1210 intborder,
1211 CHAR_TO_PIXEL_ROW (f, curs_y + n),
1212 intborder, CHAR_TO_PIXEL_ROW (f, curs_y),
1213 f->width * FONT_WIDTH (f->display.x->font),
1214 (flexlines - (curs_y + n)) * FONT_HEIGHT (f->display.x->font));
1215 /* Now we must process any ExposeRegion events that occur
1216 if the area being copied from is obscured.
1217 We can't let it wait because further i/d operations
1218 may want to copy this area to another area. */
1219 x_read_exposes ();
1220 XPixSet (FRAME_X_WINDOW (f), intborder,
1221 CHAR_TO_PIXEL_ROW (f, flexlines - n),
1222 f->width * FONT_WIDTH (f->display.x->font),
1223 n * FONT_HEIGHT (f->display.x->font), f->display.x->background_pixel);
1224 #endif /* ! defined (HAVE_X11) */
1228 /* Perform an insert-lines or delete-lines operation,
1229 inserting N lines or deleting -N lines at vertical position VPOS. */
1231 XTins_del_lines (vpos, n)
1232 int vpos, n;
1234 if (updating_frame == 0)
1235 abort ();
1237 /* Hide the cursor. */
1238 x_display_cursor (updating_frame, 0);
1240 XTcursor_to (vpos, 0);
1242 BLOCK_INPUT;
1243 if (n >= 0)
1244 stufflines (n);
1245 else
1246 scraplines (-n);
1247 XFlushQueue ();
1248 UNBLOCK_INPUT;
1251 /* Support routines for exposure events. */
1252 static void clear_cursor ();
1254 /* Output into a rectangle of an X-window (for frame F)
1255 the characters in f->phys_lines that overlap that rectangle.
1256 TOP and LEFT are the position of the upper left corner of the rectangle.
1257 ROWS and COLS are the size of the rectangle.
1258 Call this function with input blocked. */
1260 static void
1261 dumprectangle (f, left, top, cols, rows)
1262 struct frame *f;
1263 register int left, top, cols, rows;
1265 register struct frame_glyphs *active_frame = FRAME_CURRENT_GLYPHS (f);
1266 int cursor_cleared = 0;
1267 int bottom, right;
1268 register int y;
1270 if (FRAME_GARBAGED_P (f))
1271 return;
1273 /* Express rectangle as four edges, instead of position-and-size. */
1274 bottom = top + rows;
1275 right = left + cols;
1277 #ifndef HAVE_X11 /* Window manger does this for X11. */
1279 int intborder = f->display.x->internal_border_width;
1281 /* If the rectangle includes any of the internal border area,
1282 redisplay the border emphasis. */
1283 if (top < intborder || left < intborder
1284 || bottom > intborder + f->height * FONT_HEIGHT (f->display.x->font)
1285 || right > intborder + f->width * FONT_WIDTH (f->display.x->font))
1286 dumpborder (f, 0);
1288 #endif /* not HAVE_X11 Window manger does this for X11. */
1290 /* Convert rectangle edges in pixels to edges in chars.
1291 Round down for left and top, up for right and bottom. */
1292 top = PIXEL_TO_CHAR_ROW (f, top);
1293 left = PIXEL_TO_CHAR_COL (f, left);
1294 bottom += (FONT_HEIGHT (f->display.x->font) - 1);
1295 right += (FONT_WIDTH (f->display.x->font) - 1);
1296 bottom = PIXEL_TO_CHAR_ROW (f, bottom);
1297 right = PIXEL_TO_CHAR_COL (f, right);
1299 /* Clip the rectangle to what can be visible. */
1300 if (left < 0)
1301 left = 0;
1302 if (top < 0)
1303 top = 0;
1304 if (right > f->width)
1305 right = f->width;
1306 if (bottom > f->height)
1307 bottom = f->height;
1309 /* Get size in chars of the rectangle. */
1310 cols = right - left;
1311 rows = bottom - top;
1313 /* If rectangle has zero area, return. */
1314 if (rows <= 0) return;
1315 if (cols <= 0) return;
1317 /* Turn off the cursor if it is in the rectangle.
1318 We will turn it back on afterward. */
1319 if ((f->phys_cursor_x >= left) && (f->phys_cursor_x < right)
1320 && (f->phys_cursor_y >= top) && (f->phys_cursor_y < bottom))
1322 clear_cursor (f);
1323 cursor_cleared = 1;
1326 /* Display the text in the rectangle, one text line at a time. */
1328 for (y = top; y < bottom; y++)
1330 GLYPH *line = &active_frame->glyphs[y][left];
1332 if (! active_frame->enable[y] || left > active_frame->used[y])
1333 continue;
1335 dumpglyphs (f,
1336 CHAR_TO_PIXEL_COL (f, left),
1337 CHAR_TO_PIXEL_ROW (f, y),
1338 line, min (cols, active_frame->used[y] - left),
1339 active_frame->highlight[y]);
1342 /* Turn the cursor on if we turned it off. */
1344 if (cursor_cleared)
1345 x_display_cursor (f, 1);
1348 #ifndef HAVE_X11
1349 /* Process all queued ExposeRegion events. */
1351 static void
1352 dumpqueue ()
1354 register int i;
1355 XExposeRegionEvent r;
1357 while (dequeue_event (&r, &x_expose_queue))
1359 struct frame *f = x_window_to_frame (r.window);
1360 if (f->display.x->icon_desc == r.window)
1361 refreshicon (f);
1362 else
1363 dumprectangle (f, r.x, r.y, r.width, r.height);
1365 XFlushQueue ();
1367 #endif /* HAVE_X11 */
1369 /* Process all expose events that are pending, for X10.
1370 Redraws the cursor if necessary on any frame that
1371 is not in the process of being updated with update_frame. */
1373 #ifndef HAVE_X11
1374 static void
1375 x_do_pending_expose ()
1377 int mask;
1378 struct frame *f;
1379 Lisp_Object tail, frame;
1381 if (expose_all_windows)
1383 expose_all_windows = 0;
1384 for (tail = Vframe_list; CONSP (tail); tail = XCONS (tail)->cdr)
1386 register int temp_width, temp_height;
1387 int intborder;
1389 frame = XCONS (tail)->car;
1390 if (XGCTYPE (frame) != Lisp_Frame)
1391 continue;
1392 f = XFRAME (frame);
1393 if (! FRAME_X_P (f))
1394 continue;
1395 if (!f->async_visible)
1396 continue;
1397 if (!f->display.x->needs_exposure)
1398 continue;
1400 intborder = f->display.x->internal_border_width;
1402 clear_cursor (f);
1403 XGetWindowInfo (FRAME_X_WINDOW (f), &windowinfo);
1404 temp_width = ((windowinfo.width - 2 * intborder
1405 - f->display.x->v_scroll_bar_width)
1406 / FONT_WIDTH (f->display.x->font));
1407 temp_height = ((windowinfo.height- 2 * intborder
1408 - f->display.x->h_scroll_bar_height)
1409 / FONT_HEIGHT (f->display.x->font));
1410 if (temp_width != f->width || temp_height != f->height)
1412 change_frame_size (f, max (1, temp_height),
1413 max (1, temp_width), 0, 1);
1414 x_resize_scroll_bars (f);
1416 f->display.x->left_pos = windowinfo.x;
1417 f->display.x->top_pos = windowinfo.y;
1418 dumprectangle (f, 0, 0, PIXEL_WIDTH (f), PIXEL_HEIGHT (f));
1419 #if 0
1420 dumpborder (f, 0);
1421 #endif /* ! 0 */
1422 f->display.x->needs_exposure = 0;
1423 if (updating_frame != f)
1424 x_display_cursor (f, 1);
1425 XFlushQueue ();
1428 else
1429 /* Handle any individual-rectangle expose events queued
1430 for various windows. */
1431 #ifdef HAVE_X11
1433 #else /* ! defined (HAVE_X11) */
1434 dumpqueue ();
1435 #endif /* ! defined (HAVE_X11) */
1437 #endif
1439 #ifdef HAVE_X11
1440 static void
1441 frame_highlight (frame)
1442 struct frame *frame;
1444 /* We used to only do this if Vx_no_window_manager was non-nil, but
1445 the ICCCM (section 4.1.6) says that the window's border pixmap
1446 and border pixel are window attributes which are "private to the
1447 client", so we can always change it to whatever we want. */
1448 BLOCK_INPUT;
1449 XSetWindowBorder (x_current_display, FRAME_X_WINDOW (frame),
1450 frame->display.x->border_pixel);
1451 UNBLOCK_INPUT;
1452 x_display_cursor (frame, 1);
1455 static void
1456 frame_unhighlight (frame)
1457 struct frame *frame;
1459 /* We used to only do this if Vx_no_window_manager was non-nil, but
1460 the ICCCM (section 4.1.6) says that the window's border pixmap
1461 and border pixel are window attributes which are "private to the
1462 client", so we can always change it to whatever we want. */
1463 BLOCK_INPUT;
1464 XSetWindowBorderPixmap (x_current_display, FRAME_X_WINDOW (frame),
1465 frame->display.x->border_tile);
1466 UNBLOCK_INPUT;
1467 x_display_cursor (frame, 1);
1469 #else /* ! defined (HAVE_X11) */
1470 /* Dump the border-emphasis of frame F.
1471 If F is selected, this is a lining of the same color as the border,
1472 just within the border, occupying a portion of the internal border.
1473 If F is not selected, it is background in the same place.
1474 If ALWAYS is 0, don't bother explicitly drawing if it's background.
1476 ALWAYS = 1 is used when a frame becomes selected or deselected.
1477 In that case, we also turn the cursor off and on again
1478 so it will appear in the proper shape (solid if selected; else hollow.) */
1480 static void
1481 dumpborder (f, always)
1482 struct frame *f;
1483 int always;
1485 int thickness = f->display.x->internal_border_width / 2;
1486 int width = PIXEL_WIDTH (f);
1487 int height = PIXEL_HEIGHT (f);
1488 int pixel;
1490 if (f != selected_frame)
1492 if (!always)
1493 return;
1495 pixel = f->display.x->background_pixel;
1497 else
1499 pixel = f->display.x->border_pixel;
1502 XPixSet (FRAME_X_WINDOW (f), 0, 0, width, thickness, pixel);
1503 XPixSet (FRAME_X_WINDOW (f), 0, 0, thickness, height, pixel);
1504 XPixSet (FRAME_X_WINDOW (f), 0, height - thickness, width,
1505 thickness, pixel);
1506 XPixSet (FRAME_X_WINDOW (f), width - thickness, 0, thickness,
1507 height, pixel);
1509 if (always)
1510 x_display_cursor (f, 1);
1512 #endif /* ! defined (HAVE_X11) */
1514 static void XTframe_rehighlight ();
1516 /* The focus has changed. Update the frames as necessary to reflect
1517 the new situation. Note that we can't change the selected frame
1518 here, because the lisp code we are interrupting might become confused.
1519 Each event gets marked with the frame in which it occurred, so the
1520 lisp code can tell when the switch took place by examining the events. */
1522 static void
1523 x_new_focus_frame (frame)
1524 struct frame *frame;
1526 struct frame *old_focus = x_focus_frame;
1527 int events_enqueued = 0;
1529 if (frame != x_focus_frame)
1531 /* Set this before calling other routines, so that they see
1532 the correct value of x_focus_frame. */
1533 x_focus_frame = frame;
1535 if (old_focus && old_focus->auto_lower)
1536 x_lower_frame (old_focus);
1538 #if 0
1539 selected_frame = frame;
1540 XSET (XWINDOW (selected_frame->selected_window)->frame,
1541 Lisp_Frame, selected_frame);
1542 Fselect_window (selected_frame->selected_window);
1543 choose_minibuf_frame ();
1544 #endif /* ! 0 */
1546 if (x_focus_frame && x_focus_frame->auto_raise)
1547 x_raise_frame (x_focus_frame);
1550 XTframe_rehighlight ();
1554 /* The focus has changed, or we have redirected a frame's focus to
1555 another frame (this happens when a frame uses a surrogate
1556 minibuffer frame). Shift the highlight as appropriate. */
1557 static void
1558 XTframe_rehighlight ()
1560 struct frame *old_highlight = x_highlight_frame;
1562 if (x_focus_frame)
1564 x_highlight_frame =
1565 ((XGCTYPE (FRAME_FOCUS_FRAME (x_focus_frame)) == Lisp_Frame)
1566 ? XFRAME (FRAME_FOCUS_FRAME (x_focus_frame))
1567 : x_focus_frame);
1568 if (! FRAME_LIVE_P (x_highlight_frame))
1570 FRAME_FOCUS_FRAME (x_focus_frame) = Qnil;
1571 x_highlight_frame = x_focus_frame;
1574 else
1575 x_highlight_frame = 0;
1577 if (x_highlight_frame != old_highlight)
1579 if (old_highlight)
1580 frame_unhighlight (old_highlight);
1581 if (x_highlight_frame)
1582 frame_highlight (x_highlight_frame);
1586 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
1588 /* Which modifier keys are on which modifier bits?
1590 With each keystroke, X returns eight bits indicating which modifier
1591 keys were held down when the key was pressed. The interpretation
1592 of the top five modifier bits depends on what keys are attached
1593 to them. If the Meta_L and Meta_R keysyms are on mod5, then mod5
1594 is the meta bit.
1596 x_meta_mod_mask is a mask containing the bits used for the meta key.
1597 It may have more than one bit set, if more than one modifier bit
1598 has meta keys on it. Basically, if EVENT is a KeyPress event,
1599 the meta key is pressed if (EVENT.state & x_meta_mod_mask) != 0.
1601 x_shift_lock_mask is LockMask if the XK_Shift_Lock keysym is on the
1602 lock modifier bit, or zero otherwise. Non-alphabetic keys should
1603 only be affected by the lock modifier bit if XK_Shift_Lock is in
1604 use; XK_Caps_Lock should only affect alphabetic keys. With this
1605 arrangement, the lock modifier should shift the character if
1606 (EVENT.state & x_shift_lock_mask) != 0. */
1607 static int x_meta_mod_mask, x_shift_lock_mask;
1609 /* These are like x_meta_mod_mask, but for different modifiers. */
1610 static int x_alt_mod_mask, x_super_mod_mask, x_hyper_mod_mask;
1612 /* Initialize mode_switch_bit and modifier_meaning. */
1613 static void
1614 x_find_modifier_meanings ()
1616 int min_code, max_code;
1617 KeySym *syms;
1618 int syms_per_code;
1619 XModifierKeymap *mods;
1621 x_meta_mod_mask = 0;
1622 x_shift_lock_mask = 0;
1623 x_alt_mod_mask = 0;
1624 x_super_mod_mask = 0;
1625 x_hyper_mod_mask = 0;
1627 #ifdef HAVE_X11R4
1628 XDisplayKeycodes (x_current_display, &min_code, &max_code);
1629 #else
1630 min_code = x_current_display->min_keycode;
1631 max_code = x_current_display->max_keycode;
1632 #endif
1634 syms = XGetKeyboardMapping (x_current_display,
1635 min_code, max_code - min_code + 1,
1636 &syms_per_code);
1637 mods = XGetModifierMapping (x_current_display);
1639 /* Scan the modifier table to see which modifier bits the Meta and
1640 Alt keysyms are on. */
1642 int row, col; /* The row and column in the modifier table. */
1644 for (row = 3; row < 8; row++)
1645 for (col = 0; col < mods->max_keypermod; col++)
1647 KeyCode code =
1648 mods->modifiermap[(row * mods->max_keypermod) + col];
1650 /* Zeroes are used for filler. Skip them. */
1651 if (code == 0)
1652 continue;
1654 /* Are any of this keycode's keysyms a meta key? */
1656 int code_col;
1658 for (code_col = 0; code_col < syms_per_code; code_col++)
1660 int sym = syms[((code - min_code) * syms_per_code) + code_col];
1662 switch (sym)
1664 case XK_Meta_L:
1665 case XK_Meta_R:
1666 x_meta_mod_mask |= (1 << row);
1667 break;
1669 case XK_Alt_L:
1670 case XK_Alt_R:
1671 x_alt_mod_mask |= (1 << row);
1672 break;
1674 case XK_Hyper_L:
1675 case XK_Hyper_R:
1676 x_hyper_mod_mask |= (1 << row);
1677 break;
1679 case XK_Super_L:
1680 case XK_Super_R:
1681 x_super_mod_mask |= (1 << row);
1682 break;
1684 case XK_Shift_Lock:
1685 /* Ignore this if it's not on the lock modifier. */
1686 if ((1 << row) == LockMask)
1687 x_shift_lock_mask = LockMask;
1688 break;
1695 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
1696 if (! x_meta_mod_mask)
1698 x_meta_mod_mask = x_alt_mod_mask;
1699 x_alt_mod_mask = 0;
1702 /* If some keys are both alt and meta,
1703 make them just meta, not alt. */
1704 if (x_alt_mod_mask & x_meta_mod_mask)
1706 x_alt_mod_mask &= ~x_meta_mod_mask;
1709 XFree ((char *) syms);
1710 XFreeModifiermap (mods);
1713 /* Convert between the modifier bits X uses and the modifier bits
1714 Emacs uses. */
1715 static unsigned int
1716 x_x_to_emacs_modifiers (state)
1717 unsigned int state;
1719 return ( ((state & (ShiftMask | x_shift_lock_mask)) ? shift_modifier : 0)
1720 | ((state & ControlMask) ? ctrl_modifier : 0)
1721 | ((state & x_meta_mod_mask) ? meta_modifier : 0)
1722 | ((state & x_alt_mod_mask) ? alt_modifier : 0)
1723 | ((state & x_super_mod_mask) ? super_modifier : 0)
1724 | ((state & x_hyper_mod_mask) ? hyper_modifier : 0));
1727 static unsigned int
1728 x_emacs_to_x_modifiers (state)
1729 unsigned int state;
1731 return ( ((state & alt_modifier) ? x_alt_mod_mask : 0)
1732 | ((state & super_modifier) ? x_super_mod_mask : 0)
1733 | ((state & hyper_modifier) ? x_hyper_mod_mask : 0)
1734 | ((state & shift_modifier) ? ShiftMask : 0)
1735 | ((state & ctrl_modifier) ? ControlMask : 0)
1736 | ((state & meta_modifier) ? x_meta_mod_mask : 0));
1739 /* Return true iff KEYSYM is a vendor-specific keysym that we should
1740 return as a function key. If you add a keysym to this, you should
1741 make sure that the tables make_lispy_event uses contain a suitable
1742 name for it. */
1743 static int
1744 x_is_vendor_fkey (sym)
1745 KeySym sym;
1747 return 0
1748 #ifdef DXK_Remove
1749 || (sym == DXK_Remove)
1750 #endif
1755 /* Mouse clicks and mouse movement. Rah. */
1756 #ifdef HAVE_X11
1758 /* Given a pixel position (PIX_X, PIX_Y) on the frame F, return
1759 glyph co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle
1760 that the glyph at X, Y occupies, if BOUNDS != 0.
1761 If NOCLIP is nonzero, do not force the value into range. */
1763 void
1764 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1765 FRAME_PTR f;
1766 register int pix_x, pix_y;
1767 register int *x, *y;
1768 XRectangle *bounds;
1769 int noclip;
1771 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
1772 even for negative values. */
1773 if (pix_x < 0)
1774 pix_x -= FONT_WIDTH ((f)->display.x->font) - 1;
1775 if (pix_y < 0)
1776 pix_y -= FONT_HEIGHT ((f)->display.x->font) - 1;
1778 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
1779 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
1781 if (bounds)
1783 bounds->width = FONT_WIDTH (f->display.x->font);
1784 bounds->height = FONT_HEIGHT (f->display.x->font);
1785 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
1786 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
1789 if (!noclip)
1791 if (pix_x < 0)
1792 pix_x = 0;
1793 else if (pix_x > f->width)
1794 pix_x = f->width;
1796 if (pix_y < 0)
1797 pix_y = 0;
1798 else if (pix_y > f->height)
1799 pix_y = f->height;
1802 *x = pix_x;
1803 *y = pix_y;
1806 void
1807 glyph_to_pixel_coords (f, x, y, pix_x, pix_y)
1808 FRAME_PTR f;
1809 register int x, y;
1810 register int *pix_x, *pix_y;
1812 *pix_x = CHAR_TO_PIXEL_COL (f, x);
1813 *pix_y = CHAR_TO_PIXEL_ROW (f, y);
1816 /* Prepare a mouse-event in *RESULT for placement in the input queue.
1818 If the event is a button press, then note that we have grabbed
1819 the mouse. */
1821 static Lisp_Object
1822 construct_mouse_click (result, event, f)
1823 struct input_event *result;
1824 XButtonEvent *event;
1825 struct frame *f;
1827 /* Make the event type no_event; we'll change that when we decide
1828 otherwise. */
1829 result->kind = mouse_click;
1830 result->code = event->button - Button1;
1831 result->timestamp = event->time;
1832 result->modifiers = (x_x_to_emacs_modifiers (event->state)
1833 | (event->type == ButtonRelease
1834 ? up_modifier
1835 : down_modifier));
1837 /* Notice if the mouse is still grabbed. */
1838 if (event->type == ButtonPress)
1840 if (! x_mouse_grabbed)
1841 Vmouse_depressed = Qt;
1842 x_mouse_grabbed |= (1 << event->button);
1843 last_mouse_frame = f;
1845 else if (event->type == ButtonRelease)
1847 x_mouse_grabbed &= ~(1 << event->button);
1848 if (!x_mouse_grabbed)
1849 Vmouse_depressed = Qnil;
1853 int row, column;
1855 #if 0
1856 pixel_to_glyph_coords (f, event->x, event->y, &column, &row, NULL, 0);
1857 XFASTINT (result->x) = column;
1858 XFASTINT (result->y) = row;
1859 #endif
1860 XSET (result->x, Lisp_Int, event->x);
1861 XSET (result->y, Lisp_Int, event->y);
1862 XSET (result->frame_or_window, Lisp_Frame, f);
1866 /* Prepare a menu-event in *RESULT for placement in the input queue. */
1868 static Lisp_Object
1869 construct_menu_click (result, event, f)
1870 struct input_event *result;
1871 XButtonEvent *event;
1872 struct frame *f;
1874 /* Make the event type no_event; we'll change that when we decide
1875 otherwise. */
1876 result->kind = mouse_click;
1877 XSET (result->code, Lisp_Int, event->button - Button1);
1878 result->timestamp = event->time;
1879 result->modifiers = (x_x_to_emacs_modifiers (event->state)
1880 | (event->type == ButtonRelease
1881 ? up_modifier
1882 : down_modifier));
1884 XSET (result->x, Lisp_Int, event->x);
1885 XSET (result->y, Lisp_Int, -1);
1886 XSET (result->frame_or_window, Lisp_Frame, f);
1889 /* Function to report a mouse movement to the mainstream Emacs code.
1890 The input handler calls this.
1892 We have received a mouse movement event, which is given in *event.
1893 If the mouse is over a different glyph than it was last time, tell
1894 the mainstream emacs code by setting mouse_moved. If not, ask for
1895 another motion event, so we can check again the next time it moves. */
1897 static void
1898 note_mouse_movement (frame, event)
1899 FRAME_PTR frame;
1900 XMotionEvent *event;
1903 last_mouse_movement_time = event->time;
1905 /* Has the mouse moved off the glyph it was on at the last sighting? */
1906 if (event->x < last_mouse_glyph.x
1907 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
1908 || event->y < last_mouse_glyph.y
1909 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
1911 mouse_moved = 1;
1912 last_mouse_scroll_bar = Qnil;
1914 note_mouse_highlight (frame, event->x, event->y);
1916 /* Ask for another mouse motion event. */
1918 int dummy;
1920 XQueryPointer (event->display, event->window,
1921 (Window *) &dummy, (Window *) &dummy,
1922 &dummy, &dummy, &dummy, &dummy,
1923 (unsigned int *) &dummy);
1926 else
1928 /* It's on the same glyph. Call XQueryPointer so we'll get an
1929 event the next time the mouse moves and we can see if it's
1930 *still* on the same glyph. */
1931 int dummy;
1933 XQueryPointer (event->display, event->window,
1934 (Window *) &dummy, (Window *) &dummy,
1935 &dummy, &dummy, &dummy, &dummy,
1936 (unsigned int *) &dummy);
1940 /* This is used for debugging, to turn off note_mouse_highlight. */
1941 static int disable_mouse_highlight;
1943 /* Take proper action when the mouse has moved to position X, Y on frame F
1944 as regards highlighting characters that have mouse-face properties.
1945 Also dehighlighting chars where the mouse was before. */
1947 static void
1948 note_mouse_highlight (f, x, y)
1949 FRAME_PTR f;
1951 int row, column, portion;
1952 XRectangle new_glyph;
1953 Lisp_Object window;
1954 struct window *w;
1956 if (disable_mouse_highlight)
1957 return;
1959 mouse_face_mouse_x = x;
1960 mouse_face_mouse_y = y;
1961 mouse_face_mouse_frame = f;
1963 if (mouse_face_defer)
1964 return;
1966 if (gc_in_progress)
1968 mouse_face_deferred_gc = 1;
1969 return;
1972 /* Find out which glyph the mouse is on. */
1973 pixel_to_glyph_coords (f, x, y, &column, &row,
1974 &new_glyph, x_mouse_grabbed);
1976 /* Which window is that in? */
1977 window = window_from_coordinates (f, column, row, &portion);
1978 w = XWINDOW (window);
1980 /* If we were displaying active text in another window, clear that. */
1981 if (! EQ (window, mouse_face_window))
1982 clear_mouse_face ();
1984 /* Are we in a window whose display is up to date? */
1985 if (WINDOWP (window) && portion == 0
1986 && EQ (w->window_end_valid, w->buffer))
1988 int *ptr = FRAME_CURRENT_GLYPHS (f)->charstarts[row];
1989 int i, pos;
1991 /* Find which buffer position the mouse corresponds to. */
1992 for (i = column; i >= 0; i--)
1993 if (ptr[i] > 0)
1994 break;
1995 pos = ptr[i];
1996 /* Is it outside the displayed active region (if any)? */
1997 if (pos > 0
1998 && ! (EQ (window, mouse_face_window)
1999 && pos >= mouse_face_beg && pos < mouse_face_end))
2001 Lisp_Object mouse_face, overlay, position;
2002 Lisp_Object *overlay_vec;
2003 int len, noverlays, ignor1;
2004 struct buffer *obuf;
2005 int obegv, ozv;
2007 /* If we get an out-of-range value, return now; avoid an error. */
2008 if (pos > BUF_Z (XBUFFER (w->buffer)))
2009 return;
2011 /* Make the window's buffer temporarily current for
2012 overlays_at and compute_char_face. */
2013 obuf = current_buffer;
2014 current_buffer = XBUFFER (w->buffer);
2015 obegv = BEGV;
2016 ozv = ZV;
2017 BEGV = BEG;
2018 ZV = Z;
2020 /* Yes. Clear the display of the old active region, if any. */
2021 clear_mouse_face ();
2023 /* Is this char mouse-active? */
2024 XSET (position, Lisp_Int, pos);
2026 len = 10;
2027 overlay_vec = (Lisp_Object *) xmalloc (len * sizeof (Lisp_Object));
2029 /* Put all the overlays we want in a vector in overlay_vec.
2030 Store the length in len. */
2031 noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len, &ignor1);
2032 noverlays = sort_overlays (overlay_vec, noverlays, w);
2034 /* Find the highest priority overlay that has a mouse-face prop. */
2035 overlay = Qnil;
2036 for (i = 0; i < noverlays; i++)
2038 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
2039 if (!NILP (mouse_face))
2041 overlay = overlay_vec[i];
2042 break;
2045 free (overlay_vec);
2046 /* If no overlay applies, get a text property. */
2047 if (NILP (overlay))
2048 mouse_face = Fget_text_property (position, Qmouse_face, w->buffer);
2050 /* Handle the overlay case. */
2051 if (! NILP (overlay))
2053 /* Find the range of text around this char that
2054 should be active. */
2055 Lisp_Object before, after;
2056 int ignore;
2058 before = Foverlay_start (overlay);
2059 after = Foverlay_end (overlay);
2060 /* Record this as the current active region. */
2061 mouse_face_beg = XFASTINT (before);
2062 mouse_face_end = XFASTINT (after);
2063 mouse_face_window = window;
2064 mouse_face_face_id = compute_char_face (f, w, pos, 0, 0,
2065 &ignore, pos + 1, 1);
2067 /* Display it as active. */
2068 show_mouse_face (1);
2070 /* Handle the text property case. */
2071 else if (! NILP (mouse_face))
2073 /* Find the range of text around this char that
2074 should be active. */
2075 Lisp_Object before, after, beginning, end;
2076 int ignore;
2078 beginning = Fmarker_position (w->start);
2079 XSET (end, Lisp_Int,
2080 (BUF_Z (XBUFFER (w->buffer))
2081 - XFASTINT (w->window_end_pos)));
2082 before
2083 = Fprevious_single_property_change (make_number (pos + 1),
2084 Qmouse_face,
2085 w->buffer, beginning);
2086 after
2087 = Fnext_single_property_change (position, Qmouse_face,
2088 w->buffer, end);
2089 /* Record this as the current active region. */
2090 mouse_face_beg = XFASTINT (before);
2091 mouse_face_end = XFASTINT (after);
2092 mouse_face_window = window;
2093 mouse_face_face_id
2094 = compute_char_face (f, w, pos, 0, 0,
2095 &ignore, pos + 1, 1);
2097 /* Display it as active. */
2098 show_mouse_face (1);
2100 BEGV = obegv;
2101 ZV = ozv;
2102 current_buffer = obuf;
2104 else if (pos <= 0)
2105 clear_mouse_face ();
2109 /* Find the row and column of position POS in window WINDOW.
2110 Store them in *COLUMNP and *ROWP.
2111 This assumes display in WINDOW is up to date.
2112 If POS is above start of WINDOW, return coords
2113 of start of first screen line.
2114 If POS is after end of WINDOW, return coords of end of last screen line. */
2116 static int
2117 fast_find_position (window, pos, columnp, rowp)
2118 Lisp_Object window;
2119 int pos;
2120 int *columnp, *rowp;
2122 struct window *w = XWINDOW (window);
2123 FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
2124 int i;
2125 int row;
2126 int left = w->left;
2127 int top = w->top;
2128 int height = XFASTINT (w->height) - ! MINI_WINDOW_P (w);
2129 int width = window_internal_width (w);
2130 int *charstarts;
2131 int lastcol;
2133 for (i = 0;
2134 i < height;
2135 i++)
2137 int linestart = FRAME_CURRENT_GLYPHS (f)->charstarts[top + i][left];
2138 if (linestart > pos)
2139 break;
2140 if (linestart > 0)
2141 row = i;
2144 charstarts = FRAME_CURRENT_GLYPHS (f)->charstarts[top + row];
2145 lastcol = left;
2146 for (i = 0; i < width; i++)
2148 if (charstarts[left + i] == pos)
2150 *rowp = row + top;
2151 *columnp = i + left;
2152 return 1;
2154 else if (charstarts[left + i] > pos)
2155 lastcol = left + i;
2158 *rowp = row + top;
2159 *columnp = lastcol;
2160 return 0;
2163 /* Display the active region described by mouse_face_*
2164 in its mouse-face if HL > 0, in its normal face if HL = 0. */
2166 static void
2167 show_mouse_face (hl)
2168 int hl;
2170 int begcol, begrow, endcol, endrow;
2171 struct window *w = XWINDOW (mouse_face_window);
2172 int width = window_internal_width (w);
2173 FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
2174 int i;
2175 int curs_x = f->phys_cursor_x;
2176 int curs_y = f->phys_cursor_y;
2177 int cursor_off = 0;
2179 fast_find_position (mouse_face_window, mouse_face_beg,
2180 &begcol, &begrow);
2181 fast_find_position (mouse_face_window, mouse_face_end,
2182 &endcol, &endrow);
2184 for (i = begrow; i <= endrow; i++)
2186 int column = (i == begrow ? begcol : w->left);
2187 int endcolumn = (i == endrow ? endcol : w->left + width);
2188 endcolumn = min (endcolumn, FRAME_CURRENT_GLYPHS (f)->used[i] - w->left);
2190 /* If the cursor's in the text we are about to rewrite,
2191 turn the cursor off. */
2192 if (i == curs_y
2193 && (curs_x >= begrow - 1 || curs_x <= endrow))
2195 x_display_cursor (f, 0);
2196 cursor_off = 1;
2199 dumpglyphs (f,
2200 CHAR_TO_PIXEL_COL (f, column),
2201 CHAR_TO_PIXEL_ROW (f, i),
2202 FRAME_CURRENT_GLYPHS (f)->glyphs[i] + column,
2203 endcolumn - column,
2204 /* Highlight with mouse face if hl > 0. */
2205 hl > 0 ? 3 : 0);
2208 /* If we turned the cursor off, turn it back on. */
2209 if (cursor_off)
2210 x_display_cursor (f, 1);
2213 /* Clear out the mouse-highlighted active region.
2214 Redraw it unhighlighted first. */
2216 static void
2217 clear_mouse_face ()
2219 if (! NILP (mouse_face_window))
2220 show_mouse_face (0);
2222 mouse_face_beg = -1;
2223 mouse_face_end = -1;
2224 mouse_face_window = Qnil;
2227 static struct scroll_bar *x_window_to_scroll_bar ();
2228 static void x_scroll_bar_report_motion ();
2230 /* Return the current position of the mouse.
2232 If the mouse movement started in a scroll bar, set *f, *bar_window,
2233 and *part to the frame, window, and scroll bar part that the mouse
2234 is over. Set *x and *y to the portion and whole of the mouse's
2235 position on the scroll bar.
2237 If the mouse movement started elsewhere, set *f to the frame the
2238 mouse is on, *bar_window to nil, and *x and *y to the character cell
2239 the mouse is over.
2241 Set *time to the server timestamp for the time at which the mouse
2242 was at this position.
2244 Don't store anything if we don't have a valid set of values to report.
2246 This clears the mouse_moved flag, so we can wait for the next mouse
2247 movement. This also calls XQueryPointer, which will cause the
2248 server to give us another MotionNotify when the mouse moves
2249 again. */
2251 static void
2252 XTmouse_position (f, bar_window, part, x, y, time)
2253 FRAME_PTR *f;
2254 Lisp_Object *bar_window;
2255 enum scroll_bar_part *part;
2256 Lisp_Object *x, *y;
2257 unsigned long *time;
2259 FRAME_PTR f1;
2261 BLOCK_INPUT;
2263 if (! NILP (last_mouse_scroll_bar))
2264 x_scroll_bar_report_motion (f, bar_window, part, x, y, time);
2265 else
2267 Window root;
2268 int root_x, root_y;
2270 Window dummy_window;
2271 int dummy;
2273 mouse_moved = 0;
2274 last_mouse_scroll_bar = Qnil;
2276 /* Figure out which root window we're on. */
2277 XQueryPointer (x_current_display,
2278 DefaultRootWindow (x_current_display),
2280 /* The root window which contains the pointer. */
2281 &root,
2283 /* Trash which we can't trust if the pointer is on
2284 a different screen. */
2285 &dummy_window,
2287 /* The position on that root window. */
2288 &root_x, &root_y,
2290 /* More trash we can't trust. */
2291 &dummy, &dummy,
2293 /* Modifier keys and pointer buttons, about which
2294 we don't care. */
2295 (unsigned int *) &dummy);
2297 /* Now we have a position on the root; find the innermost window
2298 containing the pointer. */
2300 Window win, child;
2301 int win_x, win_y;
2302 int parent_x, parent_y;
2304 win = root;
2306 if (x_mouse_grabbed)
2308 /* If mouse was grabbed on a frame, give coords for that frame
2309 even if the mouse is now outside it. */
2310 XTranslateCoordinates (x_current_display,
2312 /* From-window, to-window. */
2313 root, FRAME_X_WINDOW (last_mouse_frame),
2315 /* From-position, to-position. */
2316 root_x, root_y, &win_x, &win_y,
2318 /* Child of win. */
2319 &child);
2320 f1 = last_mouse_frame;
2322 else
2324 while (1)
2326 XTranslateCoordinates (x_current_display,
2328 /* From-window, to-window. */
2329 root, win,
2331 /* From-position, to-position. */
2332 root_x, root_y, &win_x, &win_y,
2334 /* Child of win. */
2335 &child);
2337 if (child == None)
2338 break;
2340 win = child;
2341 parent_x = win_x;
2342 parent_y = win_y;
2345 /* Now we know that:
2346 win is the innermost window containing the pointer
2347 (XTC says it has no child containing the pointer),
2348 win_x and win_y are the pointer's position in it
2349 (XTC did this the last time through), and
2350 parent_x and parent_y are the pointer's position in win's parent.
2351 (They are what win_x and win_y were when win was child.
2352 If win is the root window, it has no parent, and
2353 parent_{x,y} are invalid, but that's okay, because we'll
2354 never use them in that case.) */
2356 /* Is win one of our frames? */
2357 f1 = x_any_window_to_frame (win);
2360 /* If not, is it one of our scroll bars? */
2361 if (! f1)
2363 struct scroll_bar *bar = x_window_to_scroll_bar (win);
2365 if (bar)
2367 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
2368 win_x = parent_x;
2369 win_y = parent_y;
2373 if (f1)
2375 int ignore1, ignore2;
2377 /* Ok, we found a frame. Store all the values. */
2379 pixel_to_glyph_coords (f1, win_x, win_y, &ignore1, &ignore2,
2380 &last_mouse_glyph, x_mouse_grabbed);
2382 *bar_window = Qnil;
2383 *part = 0;
2384 *f = f1;
2385 XSET (*x, Lisp_Int, win_x);
2386 XSET (*y, Lisp_Int, win_y);
2387 *time = last_mouse_movement_time;
2392 UNBLOCK_INPUT;
2395 #else /* ! defined (HAVE_X11) */
2396 #define XEvent XKeyPressedEvent
2397 #endif /* ! defined (HAVE_X11) */
2399 /* Scroll bar support. */
2401 /* Given an X window ID, find the struct scroll_bar which manages it.
2402 This can be called in GC, so we have to make sure to strip off mark
2403 bits. */
2404 static struct scroll_bar *
2405 x_window_to_scroll_bar (window_id)
2406 Window window_id;
2408 Lisp_Object tail, frame;
2410 for (tail = Vframe_list;
2411 XGCTYPE (tail) == Lisp_Cons;
2412 tail = XCONS (tail)->cdr)
2414 Lisp_Object frame, bar, condemned;
2416 frame = XCONS (tail)->car;
2417 /* All elements of Vframe_list should be frames. */
2418 if (XGCTYPE (frame) != Lisp_Frame)
2419 abort ();
2421 /* Scan this frame's scroll bar list for a scroll bar with the
2422 right window ID. */
2423 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
2424 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
2425 /* This trick allows us to search both the ordinary and
2426 condemned scroll bar lists with one loop. */
2427 ! GC_NILP (bar) || (bar = condemned,
2428 condemned = Qnil,
2429 ! GC_NILP (bar));
2430 bar = XSCROLL_BAR (bar)->next)
2431 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
2432 return XSCROLL_BAR (bar);
2435 return 0;
2438 /* Open a new X window to serve as a scroll bar, and return the
2439 scroll bar vector for it. */
2440 static struct scroll_bar *
2441 x_scroll_bar_create (window, top, left, width, height)
2442 struct window *window;
2443 int top, left, width, height;
2445 FRAME_PTR frame = XFRAME (WINDOW_FRAME (window));
2446 struct scroll_bar *bar =
2447 XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
2449 BLOCK_INPUT;
2452 XSetWindowAttributes a;
2453 unsigned long mask;
2454 a.background_pixel = frame->display.x->background_pixel;
2455 a.event_mask = (ButtonPressMask | ButtonReleaseMask
2456 | ButtonMotionMask | PointerMotionHintMask
2457 | ExposureMask);
2458 a.cursor = x_vertical_scroll_bar_cursor;
2460 mask = (CWBackPixel | CWEventMask | CWCursor);
2462 #if 0
2464 ac = 0;
2465 XtSetArg (al[ac], XtNx, left); ac++;
2466 XtSetArg (al[ac], XtNy, top); ac++;
2467 XtSetArg (al[ac], XtNwidth, width); ac++;
2468 XtSetArg (al[ac], XtNheight, height); ac++;
2469 XtSetArg (al[ac], XtNborderWidth, 0); ac++;
2470 sb_widget = XtCreateManagedWidget ("box",
2471 boxWidgetClass,
2472 frame->display.x->edit_widget, al, ac);
2473 SET_SCROLL_BAR_X_WINDOW
2474 (bar, sb_widget->core.window);
2475 #endif
2476 SET_SCROLL_BAR_X_WINDOW
2477 (bar,
2478 XCreateWindow (x_current_display, FRAME_X_WINDOW (frame),
2480 /* Position and size of scroll bar. */
2481 left, top, width, height,
2483 /* Border width, depth, class, and visual. */
2484 0, CopyFromParent, CopyFromParent, CopyFromParent,
2486 /* Attributes. */
2487 mask, &a));
2490 XSET (bar->window, Lisp_Window, window);
2491 XSET (bar->top, Lisp_Int, top);
2492 XSET (bar->left, Lisp_Int, left);
2493 XSET (bar->width, Lisp_Int, width);
2494 XSET (bar->height, Lisp_Int, height);
2495 XSET (bar->start, Lisp_Int, 0);
2496 XSET (bar->end, Lisp_Int, 0);
2497 bar->dragging = Qnil;
2499 /* Add bar to its frame's list of scroll bars. */
2500 bar->next = FRAME_SCROLL_BARS (frame);
2501 bar->prev = Qnil;
2502 XSET (FRAME_SCROLL_BARS (frame), Lisp_Vector, bar);
2503 if (! NILP (bar->next))
2504 XSET (XSCROLL_BAR (bar->next)->prev, Lisp_Vector, bar);
2506 XMapWindow (x_current_display, SCROLL_BAR_X_WINDOW (bar));
2508 UNBLOCK_INPUT;
2510 return bar;
2513 /* Draw BAR's handle in the proper position.
2514 If the handle is already drawn from START to END, don't bother
2515 redrawing it, unless REBUILD is non-zero; in that case, always
2516 redraw it. (REBUILD is handy for drawing the handle after expose
2517 events.)
2519 Normally, we want to constrain the start and end of the handle to
2520 fit inside its rectangle, but if the user is dragging the scroll bar
2521 handle, we want to let them drag it down all the way, so that the
2522 bar's top is as far down as it goes; otherwise, there's no way to
2523 move to the very end of the buffer. */
2524 static void
2525 x_scroll_bar_set_handle (bar, start, end, rebuild)
2526 struct scroll_bar *bar;
2527 int start, end;
2528 int rebuild;
2530 int dragging = ! NILP (bar->dragging);
2531 Window w = SCROLL_BAR_X_WINDOW (bar);
2532 GC gc = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))->display.x->normal_gc;
2534 /* If the display is already accurate, do nothing. */
2535 if (! rebuild
2536 && start == XINT (bar->start)
2537 && end == XINT (bar->end))
2538 return;
2540 BLOCK_INPUT;
2543 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (XINT (bar->width));
2544 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (XINT (bar->height));
2545 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (XINT (bar->height));
2547 /* Make sure the values are reasonable, and try to preserve
2548 the distance between start and end. */
2550 int length = end - start;
2552 if (start < 0)
2553 start = 0;
2554 else if (start > top_range)
2555 start = top_range;
2556 end = start + length;
2558 if (end < start)
2559 end = start;
2560 else if (end > top_range && ! dragging)
2561 end = top_range;
2564 /* Store the adjusted setting in the scroll bar. */
2565 XSET (bar->start, Lisp_Int, start);
2566 XSET (bar->end, Lisp_Int, end);
2568 /* Clip the end position, just for display. */
2569 if (end > top_range)
2570 end = top_range;
2572 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
2573 below top positions, to make sure the handle is always at least
2574 that many pixels tall. */
2575 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
2577 /* Draw the empty space above the handle. Note that we can't clear
2578 zero-height areas; that means "clear to end of window." */
2579 if (0 < start)
2580 XClearArea (x_current_display, w,
2582 /* x, y, width, height, and exposures. */
2583 VERTICAL_SCROLL_BAR_LEFT_BORDER,
2584 VERTICAL_SCROLL_BAR_TOP_BORDER,
2585 inside_width, start,
2586 False);
2588 /* Draw the handle itself. */
2589 XFillRectangle (x_current_display, w, gc,
2591 /* x, y, width, height */
2592 VERTICAL_SCROLL_BAR_LEFT_BORDER,
2593 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
2594 inside_width, end - start);
2597 /* Draw the empty space below the handle. Note that we can't
2598 clear zero-height areas; that means "clear to end of window." */
2599 if (end < inside_height)
2600 XClearArea (x_current_display, w,
2602 /* x, y, width, height, and exposures. */
2603 VERTICAL_SCROLL_BAR_LEFT_BORDER,
2604 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
2605 inside_width, inside_height - end,
2606 False);
2610 UNBLOCK_INPUT;
2613 /* Move a scroll bar around on the screen, to accommodate changing
2614 window configurations. */
2615 static void
2616 x_scroll_bar_move (bar, top, left, width, height)
2617 struct scroll_bar *bar;
2618 int top, left, width, height;
2620 BLOCK_INPUT;
2623 XWindowChanges wc;
2624 unsigned int mask = 0;
2626 wc.x = left;
2627 wc.y = top;
2628 wc.width = width;
2629 wc.height = height;
2631 if (left != XINT (bar->left)) mask |= CWX;
2632 if (top != XINT (bar->top)) mask |= CWY;
2633 if (width != XINT (bar->width)) mask |= CWWidth;
2634 if (height != XINT (bar->height)) mask |= CWHeight;
2636 if (mask)
2637 XConfigureWindow (x_current_display, SCROLL_BAR_X_WINDOW (bar),
2638 mask, &wc);
2641 XSET (bar->left, Lisp_Int, left);
2642 XSET (bar->top, Lisp_Int, top);
2643 XSET (bar->width, Lisp_Int, width);
2644 XSET (bar->height, Lisp_Int, height);
2646 UNBLOCK_INPUT;
2649 /* Destroy the X window for BAR, and set its Emacs window's scroll bar
2650 to nil. */
2651 static void
2652 x_scroll_bar_remove (bar)
2653 struct scroll_bar *bar;
2655 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
2657 BLOCK_INPUT;
2659 /* Destroy the window. */
2660 XDestroyWindow (x_current_display, SCROLL_BAR_X_WINDOW (bar));
2662 /* Disassociate this scroll bar from its window. */
2663 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
2665 UNBLOCK_INPUT;
2668 /* Set the handle of the vertical scroll bar for WINDOW to indicate
2669 that we are displaying PORTION characters out of a total of WHOLE
2670 characters, starting at POSITION. If WINDOW has no scroll bar,
2671 create one. */
2672 static void
2673 XTset_vertical_scroll_bar (window, portion, whole, position)
2674 struct window *window;
2675 int portion, whole, position;
2677 FRAME_PTR f = XFRAME (WINDOW_FRAME (window));
2678 int top = XINT (window->top);
2679 int left = WINDOW_VERTICAL_SCROLL_BAR_COLUMN (window);
2680 int height = WINDOW_VERTICAL_SCROLL_BAR_HEIGHT (window);
2682 /* Where should this scroll bar be, pixelwise? */
2683 int pixel_top = CHAR_TO_PIXEL_ROW (f, top);
2684 int pixel_left = CHAR_TO_PIXEL_COL (f, left);
2685 int pixel_width = VERTICAL_SCROLL_BAR_PIXEL_WIDTH (f);
2686 int pixel_height = VERTICAL_SCROLL_BAR_PIXEL_HEIGHT (f, height);
2688 struct scroll_bar *bar;
2690 /* Does the scroll bar exist yet? */
2691 if (NILP (window->vertical_scroll_bar))
2692 bar = x_scroll_bar_create (window,
2693 pixel_top, pixel_left,
2694 pixel_width, pixel_height);
2695 else
2697 /* It may just need to be moved and resized. */
2698 bar = XSCROLL_BAR (window->vertical_scroll_bar);
2699 x_scroll_bar_move (bar, pixel_top, pixel_left, pixel_width, pixel_height);
2702 /* Set the scroll bar's current state, unless we're currently being
2703 dragged. */
2704 if (NILP (bar->dragging))
2706 int top_range =
2707 VERTICAL_SCROLL_BAR_TOP_RANGE (pixel_height);
2709 if (whole == 0)
2710 x_scroll_bar_set_handle (bar, 0, top_range, 0);
2711 else
2713 int start = ((double) position * top_range) / whole;
2714 int end = ((double) (position + portion) * top_range) / whole;
2716 x_scroll_bar_set_handle (bar, start, end, 0);
2720 XSET (window->vertical_scroll_bar, Lisp_Vector, bar);
2724 /* The following three hooks are used when we're doing a thorough
2725 redisplay of the frame. We don't explicitly know which scroll bars
2726 are going to be deleted, because keeping track of when windows go
2727 away is a real pain - "Can you say set-window-configuration, boys
2728 and girls?" Instead, we just assert at the beginning of redisplay
2729 that *all* scroll bars are to be removed, and then save a scroll bar
2730 from the fiery pit when we actually redisplay its window. */
2732 /* Arrange for all scroll bars on FRAME to be removed at the next call
2733 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
2734 `*redeem_scroll_bar_hook' is applied to its window before the judgement. */
2735 static void
2736 XTcondemn_scroll_bars (frame)
2737 FRAME_PTR frame;
2739 /* The condemned list should be empty at this point; if it's not,
2740 then the rest of Emacs isn't using the condemn/redeem/judge
2741 protocol correctly. */
2742 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
2743 abort ();
2745 /* Move them all to the "condemned" list. */
2746 FRAME_CONDEMNED_SCROLL_BARS (frame) = FRAME_SCROLL_BARS (frame);
2747 FRAME_SCROLL_BARS (frame) = Qnil;
2750 /* Unmark WINDOW's scroll bar for deletion in this judgement cycle.
2751 Note that WINDOW isn't necessarily condemned at all. */
2752 static void
2753 XTredeem_scroll_bar (window)
2754 struct window *window;
2756 struct scroll_bar *bar;
2758 /* We can't redeem this window's scroll bar if it doesn't have one. */
2759 if (NILP (window->vertical_scroll_bar))
2760 abort ();
2762 bar = XSCROLL_BAR (window->vertical_scroll_bar);
2764 /* Unlink it from the condemned list. */
2766 FRAME_PTR f = XFRAME (WINDOW_FRAME (window));
2768 if (NILP (bar->prev))
2770 /* If the prev pointer is nil, it must be the first in one of
2771 the lists. */
2772 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
2773 /* It's not condemned. Everything's fine. */
2774 return;
2775 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
2776 window->vertical_scroll_bar))
2777 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
2778 else
2779 /* If its prev pointer is nil, it must be at the front of
2780 one or the other! */
2781 abort ();
2783 else
2784 XSCROLL_BAR (bar->prev)->next = bar->next;
2786 if (! NILP (bar->next))
2787 XSCROLL_BAR (bar->next)->prev = bar->prev;
2789 bar->next = FRAME_SCROLL_BARS (f);
2790 bar->prev = Qnil;
2791 XSET (FRAME_SCROLL_BARS (f), Lisp_Vector, bar);
2792 if (! NILP (bar->next))
2793 XSET (XSCROLL_BAR (bar->next)->prev, Lisp_Vector, bar);
2797 /* Remove all scroll bars on FRAME that haven't been saved since the
2798 last call to `*condemn_scroll_bars_hook'. */
2799 static void
2800 XTjudge_scroll_bars (f)
2801 FRAME_PTR f;
2803 Lisp_Object bar, next;
2805 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
2807 /* Clear out the condemned list now so we won't try to process any
2808 more events on the hapless scroll bars. */
2809 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
2811 for (; ! NILP (bar); bar = next)
2813 struct scroll_bar *b = XSCROLL_BAR (bar);
2815 x_scroll_bar_remove (b);
2817 next = b->next;
2818 b->next = b->prev = Qnil;
2821 /* Now there should be no references to the condemned scroll bars,
2822 and they should get garbage-collected. */
2826 /* Handle an Expose or GraphicsExpose event on a scroll bar.
2828 This may be called from a signal handler, so we have to ignore GC
2829 mark bits. */
2830 static void
2831 x_scroll_bar_expose (bar, event)
2832 struct scroll_bar *bar;
2833 XEvent *event;
2835 Window w = SCROLL_BAR_X_WINDOW (bar);
2836 GC gc = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))->display.x->normal_gc;
2838 BLOCK_INPUT;
2840 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
2842 /* Draw a one-pixel border just inside the edges of the scroll bar. */
2843 XDrawRectangle (x_current_display, w, gc,
2845 /* x, y, width, height */
2846 0, 0, XINT (bar->width) - 1, XINT (bar->height) - 1);
2848 UNBLOCK_INPUT;
2851 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
2852 is set to something other than no_event, it is enqueued.
2854 This may be called from a signal handler, so we have to ignore GC
2855 mark bits. */
2856 static void
2857 x_scroll_bar_handle_click (bar, event, emacs_event)
2858 struct scroll_bar *bar;
2859 XEvent *event;
2860 struct input_event *emacs_event;
2862 if (XGCTYPE (bar->window) != Lisp_Window)
2863 abort ();
2865 emacs_event->kind = scroll_bar_click;
2866 emacs_event->code = event->xbutton.button - Button1;
2867 emacs_event->modifiers =
2868 (x_x_to_emacs_modifiers (event->xbutton.state)
2869 | (event->type == ButtonRelease
2870 ? up_modifier
2871 : down_modifier));
2872 emacs_event->frame_or_window = bar->window;
2873 emacs_event->timestamp = event->xbutton.time;
2875 int internal_height =
2876 VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (XINT (bar->height));
2877 int top_range =
2878 VERTICAL_SCROLL_BAR_TOP_RANGE (XINT (bar->height));
2879 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
2881 if (y < 0) y = 0;
2882 if (y > top_range) y = top_range;
2884 if (y < XINT (bar->start))
2885 emacs_event->part = scroll_bar_above_handle;
2886 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
2887 emacs_event->part = scroll_bar_handle;
2888 else
2889 emacs_event->part = scroll_bar_below_handle;
2891 /* Just because the user has clicked on the handle doesn't mean
2892 they want to drag it. Lisp code needs to be able to decide
2893 whether or not we're dragging. */
2894 #if 0
2895 /* If the user has just clicked on the handle, record where they're
2896 holding it. */
2897 if (event->type == ButtonPress
2898 && emacs_event->part == scroll_bar_handle)
2899 XSET (bar->dragging, Lisp_Int, y - XINT (bar->start));
2900 #endif
2902 /* If the user has released the handle, set it to its final position. */
2903 if (event->type == ButtonRelease
2904 && ! NILP (bar->dragging))
2906 int new_start = y - XINT (bar->dragging);
2907 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
2909 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
2910 bar->dragging = Qnil;
2913 /* Same deal here as the other #if 0. */
2914 #if 0
2915 /* Clicks on the handle are always reported as occurring at the top of
2916 the handle. */
2917 if (emacs_event->part == scroll_bar_handle)
2918 emacs_event->x = bar->start;
2919 else
2920 XSET (emacs_event->x, Lisp_Int, y);
2921 #else
2922 XSET (emacs_event->x, Lisp_Int, y);
2923 #endif
2925 XSET (emacs_event->y, Lisp_Int, top_range);
2929 /* Handle some mouse motion while someone is dragging the scroll bar.
2931 This may be called from a signal handler, so we have to ignore GC
2932 mark bits. */
2933 static void
2934 x_scroll_bar_note_movement (bar, event)
2935 struct scroll_bar *bar;
2936 XEvent *event;
2938 last_mouse_movement_time = event->xmotion.time;
2940 mouse_moved = 1;
2941 XSET (last_mouse_scroll_bar, Lisp_Vector, bar);
2943 /* If we're dragging the bar, display it. */
2944 if (! GC_NILP (bar->dragging))
2946 /* Where should the handle be now? */
2947 int new_start = event->xmotion.y - XINT (bar->dragging);
2949 if (new_start != XINT (bar->start))
2951 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
2953 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
2957 /* Call XQueryPointer so we'll get an event the next time the mouse
2958 moves and we can see *still* on the same position. */
2960 int dummy;
2962 XQueryPointer (event->xmotion.display, event->xmotion.window,
2963 (Window *) &dummy, (Window *) &dummy,
2964 &dummy, &dummy, &dummy, &dummy,
2965 (unsigned int *) &dummy);
2969 /* Return information to the user about the current position of the mouse
2970 on the scroll bar. */
2971 static void
2972 x_scroll_bar_report_motion (f, bar_window, part, x, y, time)
2973 FRAME_PTR *f;
2974 Lisp_Object *bar_window;
2975 enum scroll_bar_part *part;
2976 Lisp_Object *x, *y;
2977 unsigned long *time;
2979 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
2980 int win_x, win_y;
2981 Window dummy_window;
2982 int dummy_coord;
2983 unsigned int dummy_mask;
2985 BLOCK_INPUT;
2987 /* Get the mouse's position relative to the scroll bar window, and
2988 report that. */
2989 if (! XQueryPointer (x_current_display,
2990 SCROLL_BAR_X_WINDOW (bar),
2992 /* Root, child, root x and root y. */
2993 &dummy_window, &dummy_window,
2994 &dummy_coord, &dummy_coord,
2996 /* Position relative to scroll bar. */
2997 &win_x, &win_y,
2999 /* Mouse buttons and modifier keys. */
3000 &dummy_mask))
3001 *f = 0;
3002 else
3004 int inside_height
3005 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (XINT (bar->height));
3006 int top_range
3007 = VERTICAL_SCROLL_BAR_TOP_RANGE (XINT (bar->height));
3009 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
3011 if (! NILP (bar->dragging))
3012 win_y -= XINT (bar->dragging);
3014 if (win_y < 0)
3015 win_y = 0;
3016 if (win_y > top_range)
3017 win_y = top_range;
3019 *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3020 *bar_window = bar->window;
3022 if (! NILP (bar->dragging))
3023 *part = scroll_bar_handle;
3024 else if (win_y < XINT (bar->start))
3025 *part = scroll_bar_above_handle;
3026 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
3027 *part = scroll_bar_handle;
3028 else
3029 *part = scroll_bar_below_handle;
3031 XSET (*x, Lisp_Int, win_y);
3032 XSET (*y, Lisp_Int, top_range);
3034 mouse_moved = 0;
3035 last_mouse_scroll_bar = Qnil;
3038 *time = last_mouse_movement_time;
3040 UNBLOCK_INPUT;
3044 /* The screen has been cleared so we may have changed foreground or
3045 background colors, and the scroll bars may need to be redrawn.
3046 Clear out the scroll bars, and ask for expose events, so we can
3047 redraw them. */
3049 x_scroll_bar_clear (f)
3050 FRAME_PTR f;
3052 Lisp_Object bar;
3054 for (bar = FRAME_SCROLL_BARS (f);
3055 XTYPE (bar) == Lisp_Vector;
3056 bar = XSCROLL_BAR (bar)->next)
3057 XClearArea (x_current_display, SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
3058 0, 0, 0, 0, True);
3061 /* This processes Expose events from the menubar specific X event
3062 loop in menubar.c. This allows to redisplay the frame if necessary
3063 when handling menubar or popup items. */
3065 void
3066 process_expose_from_menu (event)
3067 XEvent event;
3069 FRAME_PTR f;
3071 BLOCK_INPUT;
3073 f = x_window_to_frame (event.xexpose.window);
3074 if (f)
3076 if (f->async_visible == 0)
3078 f->async_visible = 1;
3079 f->async_iconified = 0;
3080 SET_FRAME_GARBAGED (f);
3082 else
3084 dumprectangle (x_window_to_frame (event.xexpose.window),
3085 event.xexpose.x, event.xexpose.y,
3086 event.xexpose.width, event.xexpose.height);
3089 else
3091 struct scroll_bar *bar
3092 = x_window_to_scroll_bar (event.xexpose.window);
3094 if (bar)
3095 x_scroll_bar_expose (bar, &event);
3098 UNBLOCK_INPUT;
3102 /* The main X event-reading loop - XTread_socket. */
3104 /* Timestamp of enter window event. This is only used by XTread_socket,
3105 but we have to put it out here, since static variables within functions
3106 sometimes don't work. */
3107 static Time enter_timestamp;
3109 /* This holds the state XLookupString needs to implement dead keys
3110 and other tricks known as "compose processing". _X Window System_
3111 says that a portable program can't use this, but Stephen Gildea assures
3112 me that letting the compiler initialize it to zeros will work okay.
3114 This must be defined outside of XTread_socket, for the same reasons
3115 given for enter_timestamp, above. */
3116 static XComposeStatus compose_status;
3118 /* Communication with window managers. */
3119 Atom Xatom_wm_protocols;
3121 /* Kinds of protocol things we may receive. */
3122 Atom Xatom_wm_take_focus;
3123 Atom Xatom_wm_save_yourself;
3124 Atom Xatom_wm_delete_window;
3126 /* Other WM communication */
3127 Atom Xatom_wm_configure_denied; /* When our config request is denied */
3128 Atom Xatom_wm_window_moved; /* When the WM moves us. */
3130 /* Window manager communication. */
3131 Atom Xatom_wm_change_state;
3133 /* Record the last 100 characters stored
3134 to help debug the loss-of-chars-during-GC problem. */
3135 int temp_index;
3136 short temp_buffer[100];
3138 /* Read events coming from the X server.
3139 This routine is called by the SIGIO handler.
3140 We return as soon as there are no more events to be read.
3142 Events representing keys are stored in buffer BUFP,
3143 which can hold up to NUMCHARS characters.
3144 We return the number of characters stored into the buffer,
3145 thus pretending to be `read'.
3147 WAITP is nonzero if we should block until input arrives.
3148 EXPECTED is nonzero if the caller knows input is available. */
3151 XTread_socket (sd, bufp, numchars, waitp, expected)
3152 register int sd;
3153 register struct input_event *bufp;
3154 register int numchars;
3155 int waitp;
3156 int expected;
3158 int count = 0;
3159 int nbytes = 0;
3160 int mask;
3161 int items_pending; /* How many items are in the X queue. */
3162 XEvent event;
3163 struct frame *f;
3164 int event_found = 0;
3165 int prefix;
3166 Lisp_Object part;
3168 if (interrupt_input_blocked)
3170 interrupt_input_pending = 1;
3171 return -1;
3174 interrupt_input_pending = 0;
3175 BLOCK_INPUT;
3177 if (numchars <= 0)
3178 abort (); /* Don't think this happens. */
3180 #ifdef FIOSNBIO
3181 /* If available, Xlib uses FIOSNBIO to make the socket
3182 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
3183 FIOSNBIO is ignored, and instead of signalling EWOULDBLOCK,
3184 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
3185 fcntl (fileno (stdin), F_SETFL, 0);
3186 #endif /* ! defined (FIOSNBIO) */
3188 #ifndef SIGIO
3189 #ifndef HAVE_SELECT
3190 if (! (fcntl (fileno (stdin), F_GETFL, 0) & O_NDELAY))
3192 extern int read_alarm_should_throw;
3193 read_alarm_should_throw = 1;
3194 XPeekEvent (XDISPLAY &event);
3195 read_alarm_should_throw = 0;
3197 #endif /* HAVE_SELECT */
3198 #endif /* SIGIO */
3200 while (XStuffPending () != 0)
3202 XNextEvent (XDISPLAY &event);
3203 event_found = 1;
3205 switch (event.type)
3207 #ifdef HAVE_X11
3208 case ClientMessage:
3210 if (event.xclient.message_type == Xatom_wm_protocols
3211 && event.xclient.format == 32)
3213 if (event.xclient.data.l[0] == Xatom_wm_take_focus)
3215 #ifdef USE_X_TOOLKIT
3216 /* f = x_any_window_to_frame (event.xclient.window); */
3217 f = x_window_to_frame (event.xclient.window);
3218 #else
3219 f = x_window_to_frame (event.xclient.window);
3220 #endif
3221 if (f)
3222 x_focus_on_frame (f);
3223 /* Not certain about handling scroll bars here */
3225 else if (event.xclient.data.l[0] == Xatom_wm_save_yourself)
3227 /* Save state modify the WM_COMMAND property to
3228 something which can reinstate us. This notifies
3229 the session manager, who's looking for such a
3230 PropertyNotify. Can restart processing when
3231 a keyboard or mouse event arrives. */
3232 if (numchars > 0)
3236 else if (event.xclient.data.l[0] == Xatom_wm_delete_window)
3238 struct frame *f = x_any_window_to_frame (event.xclient.window);
3240 if (f)
3242 if (numchars == 0)
3243 abort ();
3245 bufp->kind = delete_window_event;
3246 XSET (bufp->frame_or_window, Lisp_Frame, f);
3247 bufp++;
3249 count += 1;
3250 numchars -= 1;
3254 else if (event.xclient.message_type == Xatom_wm_configure_denied)
3257 else if (event.xclient.message_type == Xatom_wm_window_moved)
3259 int new_x, new_y;
3260 struct frame *f = x_window_to_frame (event.xclient.window);
3262 new_x = event.xclient.data.s[0];
3263 new_y = event.xclient.data.s[1];
3265 if (f)
3267 f->display.x->left_pos = new_x;
3268 f->display.x->top_pos = new_y;
3272 break;
3274 #ifdef NEW_SELECTIONS
3275 case SelectionNotify:
3276 #ifdef USE_X_TOOLKIT
3277 if (x_window_to_frame (event.xselection.requestor))
3278 x_handle_selection_notify (&event);
3279 else
3280 goto OTHER;
3281 #else /* not USE_X_TOOLKIT */
3282 x_handle_selection_notify (&event);
3283 #endif /* not USE_X_TOOLKIT */
3284 break;
3285 #endif /* NEW_SELECTIONS */
3287 case SelectionClear: /* Someone has grabbed ownership. */
3288 #ifdef NEW_SELECTIONS
3290 #ifdef USE_X_TOOLKIT
3291 if (x_window_to_frame (event.xselectionclear.window))
3293 #endif /* USE_X_TOOLKIT */
3294 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
3296 if (numchars == 0)
3297 abort ();
3299 bufp->kind = selection_clear_event;
3300 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
3301 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
3302 SELECTION_EVENT_TIME (bufp) = eventp->time;
3303 bufp++;
3305 count += 1;
3306 numchars -= 1;
3307 #ifdef USE_X_TOOLKIT
3309 else
3310 goto OTHER;
3311 #endif /* USE_X_TOOLKIT */
3313 #else /* not NEW_SELECTIONS */
3314 x_disown_selection (event.xselectionclear.window,
3315 event.xselectionclear.selection,
3316 event.xselectionclear.time);
3317 #endif /* not NEW_SELECTIONS */
3318 break;
3320 case SelectionRequest: /* Someone wants our selection. */
3321 #ifdef NEW_SELECTIONS
3323 #ifdef USE_X_TOOLKIT
3324 if (x_window_to_frame (event.xselectionrequest.owner))
3326 #endif /* USE_X_TOOLKIT */
3327 XSelectionRequestEvent *eventp = (XSelectionRequestEvent *) &event;
3329 if (numchars == 0)
3330 abort ();
3332 bufp->kind = selection_request_event;
3333 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
3334 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
3335 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
3336 SELECTION_EVENT_TARGET (bufp) = eventp->target;
3337 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
3338 SELECTION_EVENT_TIME (bufp) = eventp->time;
3339 bufp++;
3341 count += 1;
3342 numchars -= 1;
3343 #ifdef USE_X_TOOLKIT
3345 else
3346 goto OTHER;
3347 #endif /* USE_X_TOOLKIT */
3349 #else /* not NEW_SELECTIONS */
3350 x_answer_selection_request (event);
3351 #endif /* not NEW_SELECTIONS */
3352 break;
3354 case PropertyNotify:
3355 #ifdef NEW_SELECTIONS
3356 #ifdef USE_X_TOOLKIT
3357 if (x_any_window_to_frame (event.xproperty.window))
3358 x_handle_property_notify (&event);
3359 else
3360 goto OTHER;
3361 #else /* not USE_X_TOOLKIT */
3362 x_handle_property_notify (&event);
3363 #endif /* not USE_X_TOOLKIT */
3364 #else /* not NEW_SELECTIONS */
3365 /* If we're being told about a root window property, then it's
3366 a cut buffer change. */
3367 if (event.xproperty.window == ROOT_WINDOW)
3368 x_invalidate_cut_buffer_cache (&event.xproperty);
3370 /* Otherwise, we're probably handling an incremental
3371 selection transmission. */
3372 else
3374 /* If we were to do this synchronously, there'd be no worry
3375 about re-selecting. */
3376 x_send_incremental (event);
3378 #endif /* not NEW_SELECTIONS */
3379 break;
3381 case ReparentNotify:
3382 f = x_window_to_frame (event.xreparent.window);
3383 if (f)
3384 f->display.x->parent_desc = event.xreparent.parent;
3385 break;
3387 case Expose:
3388 f = x_window_to_frame (event.xexpose.window);
3389 if (f)
3391 if (f->async_visible == 0)
3393 f->async_visible = 1;
3394 f->async_iconified = 0;
3395 SET_FRAME_GARBAGED (f);
3397 else
3399 dumprectangle (x_window_to_frame (event.xexpose.window),
3400 event.xexpose.x, event.xexpose.y,
3401 event.xexpose.width, event.xexpose.height);
3404 else
3406 struct scroll_bar *bar
3407 = x_window_to_scroll_bar (event.xexpose.window);
3409 if (bar)
3410 x_scroll_bar_expose (bar, &event);
3411 #ifdef USE_X_TOOLKIT
3412 else
3413 goto OTHER;
3414 #endif /* USE_X_TOOLKIT */
3416 break;
3418 case GraphicsExpose: /* This occurs when an XCopyArea's
3419 source area was obscured or not
3420 available.*/
3421 f = x_window_to_frame (event.xgraphicsexpose.drawable);
3422 if (f)
3424 dumprectangle (f,
3425 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
3426 event.xgraphicsexpose.width,
3427 event.xgraphicsexpose.height);
3429 #ifdef USE_X_TOOLKIT
3430 else
3431 goto OTHER;
3432 #endif /* USE_X_TOOLKIT */
3433 break;
3435 case NoExpose: /* This occurs when an XCopyArea's
3436 source area was completely
3437 available */
3438 break;
3439 #else /* ! defined (HAVE_X11) */
3440 case ExposeWindow:
3441 if (event.subwindow != 0)
3442 break; /* duplicate event */
3443 f = x_window_to_frame (event.window);
3444 if (event.window == f->display.x->icon_desc)
3446 refreshicon (f);
3447 f->async_iconified = 1;
3449 if (event.window == FRAME_X_WINDOW (f))
3451 /* Say must check all windows' needs_exposure flags. */
3452 expose_all_windows = 1;
3453 f->display.x->needs_exposure = 1;
3454 f->async_visible = 1;
3456 break;
3458 case ExposeRegion:
3459 if (event.subwindow != 0)
3460 break; /* duplicate event */
3461 f = x_window_to_frame (event.window);
3462 if (event.window == f->display.x->icon_desc)
3464 refreshicon (f);
3465 break;
3467 /* If window already needs full redraw, ignore this rectangle. */
3468 if (expose_all_windows && f->display.x->needs_exposure)
3469 break;
3470 /* Put the event on the queue of rectangles to redraw. */
3471 if (enqueue_event (&event, &x_expose_queue))
3472 /* If it is full, we can't record the rectangle,
3473 so redraw this entire window. */
3475 /* Say must check all windows' needs_exposure flags. */
3476 expose_all_windows = 1;
3477 f->display.x->needs_exposure = 1;
3479 break;
3481 case ExposeCopy:
3482 /* This should happen only when we are expecting it,
3483 in x_read_exposes. */
3484 abort ();
3485 #endif /* ! defined (HAVE_X11) */
3487 #ifdef HAVE_X11
3488 case UnmapNotify:
3489 f = x_window_to_frame (event.xunmap.window);
3490 if (f) /* F may no longer exist if
3491 the frame was deleted. */
3493 /* While a frame is unmapped, display generation is
3494 disabled; you don't want to spend time updating a
3495 display that won't ever be seen. */
3496 f->async_visible = 0;
3497 /* The window manager never makes a window invisible
3498 ("withdrawn"); all it does is switch between visible
3499 and iconified. Frames get into the invisible state
3500 only through x_make_frame_invisible. */
3501 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
3502 f->async_iconified = 1;
3504 #ifdef USE_X_TOOLKIT
3505 goto OTHER;
3506 #endif /* USE_X_TOOLKIT */
3507 break;
3509 case MapNotify:
3510 #ifdef USE_X_TOOLKIT
3511 f = x_any_window_to_frame (event.xmap.window);
3512 #else /* not USE_X_TOOLKIT */
3513 f = x_window_to_frame (event.xmap.window);
3514 #endif /* not USE_X_TOOLKIT */
3515 if (f)
3517 f->async_visible = 1;
3518 f->async_iconified = 0;
3520 /* wait_reading_process_input will notice this and update
3521 the frame's display structures. */
3522 SET_FRAME_GARBAGED (f);
3524 #ifdef USE_X_TOOLKIT
3525 goto OTHER;
3526 #endif /* USE_X_TOOLKIT */
3527 break;
3529 /* Turn off processing if we become fully obscured. */
3530 case VisibilityNotify:
3531 break;
3533 #else /* ! defined (HAVE_X11) */
3534 case UnmapWindow:
3535 f = x_window_to_frame (event.window);
3536 if (event.window == f->display.x->icon_desc)
3537 f->async_iconified = 0;
3538 if (event.window == FRAME_X_WINDOW (f))
3539 f->async_visible = 0;
3540 break;
3541 #endif /* ! defined (HAVE_X11) */
3543 #ifdef HAVE_X11
3544 case KeyPress:
3545 f = x_any_window_to_frame (event.xkey.window);
3547 if (f != 0)
3549 KeySym keysym, orig_keysym;
3550 /* al%imercury@uunet.uu.net says that making this 81 instead of
3551 80 fixed a bug whereby meta chars made his Emacs hang. */
3552 unsigned char copy_buffer[81];
3553 int modifiers;
3555 event.xkey.state
3556 |= x_emacs_to_x_modifiers (extra_keyboard_modifiers);
3557 modifiers = event.xkey.state;
3559 /* This will have to go some day... */
3561 /* make_lispy_event turns chars into control chars.
3562 Don't do it here because XLookupString is too eager. */
3563 event.xkey.state &= ~ControlMask;
3564 nbytes =
3565 XLookupString (&event.xkey, copy_buffer, 80, &keysym,
3566 &compose_status);
3568 /* Strip off the vendor-specific keysym bit, and take a shot
3569 at recognizing the codes. HP servers have extra keysyms
3570 that fit into the MiscFunctionKey category. */
3571 orig_keysym = keysym;
3572 keysym &= ~(1<<28);
3574 if (numchars > 1)
3576 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
3577 || keysym == XK_Delete
3578 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
3579 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
3580 #ifdef HPUX
3581 /* This recognizes the "extended function keys".
3582 It seems there's no cleaner way.
3583 Test IsModifierKey to avoid handling mode_switch
3584 incorrectly. */
3585 || ((unsigned) (keysym) >= XK_Select
3586 && (unsigned)(keysym) < XK_KP_Space)
3587 #endif
3588 #ifdef XK_dead_circumflex
3589 || orig_keysym == XK_dead_circumflex
3590 #endif
3591 #ifdef XK_dead_grave
3592 || orig_keysym == XK_dead_grave
3593 #endif
3594 #ifdef XK_dead_tilde
3595 || orig_keysym == XK_dead_tilde
3596 #endif
3597 #ifdef XK_dead_diaeresis
3598 || orig_keysym == XK_dead_diaeresis
3599 #endif
3600 #ifdef XK_dead_macron
3601 || orig_keysym == XK_dead_macron
3602 #endif
3603 #ifdef XK_dead_degree
3604 || orig_keysym == XK_dead_degree
3605 #endif
3606 #ifdef XK_dead_acute
3607 || orig_keysym == XK_dead_acute
3608 #endif
3609 #ifdef XK_dead_cedilla
3610 || orig_keysym == XK_dead_cedilla
3611 #endif
3612 #ifdef XK_dead_breve
3613 || orig_keysym == XK_dead_breve
3614 #endif
3615 #ifdef XK_dead_ogonek
3616 || orig_keysym == XK_dead_ogonek
3617 #endif
3618 #ifdef XK_dead_caron
3619 || orig_keysym == XK_dead_caron
3620 #endif
3621 #ifdef XK_dead_doubleacute
3622 || orig_keysym == XK_dead_doubleacute
3623 #endif
3624 #ifdef XK_dead_abovedot
3625 || orig_keysym == XK_dead_abovedot
3626 #endif
3627 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
3628 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
3629 || x_is_vendor_fkey (orig_keysym))
3630 && ! (IsModifierKey (orig_keysym)
3631 #ifndef HAVE_X11R5
3632 #ifdef XK_Mode_switch
3633 || ((unsigned)(orig_keysym) == XK_Mode_switch)
3634 #endif
3635 #ifdef XK_Num_Lock
3636 || ((unsigned)(orig_keysym) == XK_Num_Lock)
3637 #endif
3638 #endif /* not HAVE_X11R5 */
3641 if (temp_index == sizeof temp_buffer / sizeof (short))
3642 temp_index = 0;
3643 temp_buffer[temp_index++] = keysym;
3644 bufp->kind = non_ascii_keystroke;
3645 bufp->code = keysym;
3646 XSET (bufp->frame_or_window, Lisp_Frame, f);
3647 bufp->modifiers = x_x_to_emacs_modifiers (modifiers);
3648 bufp->timestamp = event.xkey.time;
3649 bufp++;
3650 count++;
3651 numchars--;
3653 else if (numchars > nbytes)
3655 register int i;
3657 for (i = 0; i < nbytes; i++)
3659 if (temp_index == sizeof temp_buffer / sizeof (short))
3660 temp_index = 0;
3661 temp_buffer[temp_index++] = copy_buffer[i];
3662 bufp->kind = ascii_keystroke;
3663 bufp->code = copy_buffer[i];
3664 XSET (bufp->frame_or_window, Lisp_Frame, f);
3665 bufp->modifiers = x_x_to_emacs_modifiers (modifiers);
3666 bufp->timestamp = event.xkey.time;
3667 bufp++;
3670 count += nbytes;
3671 numchars -= nbytes;
3673 else
3674 abort ();
3676 else
3677 abort ();
3679 break;
3680 #else /* ! defined (HAVE_X11) */
3681 case KeyPressed:
3683 register char *where_mapping;
3685 f = x_window_to_frame (event.window);
3686 /* Ignore keys typed on icon windows. */
3687 if (f != 0 && event.window == f->display.x->icon_desc)
3688 break;
3689 where_mapping = XLookupMapping (&event, &nbytes);
3690 /* Nasty fix for arrow keys */
3691 if (!nbytes && IsCursorKey (event.detail & 0xff))
3693 switch (event.detail & 0xff)
3695 case KC_CURSOR_LEFT:
3696 where_mapping = "\002";
3697 break;
3698 case KC_CURSOR_RIGHT:
3699 where_mapping = "\006";
3700 break;
3701 case KC_CURSOR_UP:
3702 where_mapping = "\020";
3703 break;
3704 case KC_CURSOR_DOWN:
3705 where_mapping = "\016";
3706 break;
3708 nbytes = 1;
3710 if (numchars - nbytes > 0)
3712 register int i;
3714 for (i = 0; i < nbytes; i++)
3716 bufp->kind = ascii_keystroke;
3717 bufp->code = where_mapping[i];
3718 XSET (bufp->time, Lisp_Int, event.xkey.time);
3719 XSET (bufp->frame_or_window, Lisp_Frame, f);
3720 bufp++;
3722 count += nbytes;
3723 numchars -= nbytes;
3726 break;
3727 #endif /* ! defined (HAVE_X11) */
3729 #ifdef HAVE_X11
3731 /* Here's a possible interpretation of the whole
3732 FocusIn-EnterNotify FocusOut-LeaveNotify mess. If you get a
3733 FocusIn event, you have to get a FocusOut event before you
3734 relinquish the focus. If you haven't received a FocusIn event,
3735 then a mere LeaveNotify is enough to free you. */
3737 case EnterNotify:
3738 f = x_any_window_to_frame (event.xcrossing.window);
3740 if (event.xcrossing.focus) /* Entered Window */
3742 /* Avoid nasty pop/raise loops. */
3743 if (f && (!(f->auto_raise)
3744 || !(f->auto_lower)
3745 || (event.xcrossing.time - enter_timestamp) > 500))
3747 x_new_focus_frame (f);
3748 enter_timestamp = event.xcrossing.time;
3751 else if (f == x_focus_frame)
3752 x_new_focus_frame (0);
3753 #ifdef USE_X_TOOLKIT
3754 goto OTHER;
3755 #endif /* USE_X_TOOLKIT */
3756 break;
3758 case FocusIn:
3759 f = x_any_window_to_frame (event.xfocus.window);
3760 if (event.xfocus.detail != NotifyPointer)
3761 x_focus_event_frame = f;
3762 if (f)
3763 x_new_focus_frame (f);
3764 #ifdef USE_X_TOOLKIT
3765 goto OTHER;
3766 #endif /* USE_X_TOOLKIT */
3767 break;
3770 case LeaveNotify:
3771 f = x_any_window_to_frame (event.xcrossing.window);
3773 if (f == mouse_face_mouse_frame)
3774 /* If we move outside the frame,
3775 then we're certainly no longer on any text in the frame. */
3776 clear_mouse_face ();
3778 if (event.xcrossing.focus)
3780 if (! x_focus_event_frame)
3781 x_new_focus_frame (0);
3782 else
3783 x_new_focus_frame (f);
3785 else
3787 if (f == x_focus_event_frame)
3788 x_focus_event_frame = 0;
3789 if (f == x_focus_frame)
3790 x_new_focus_frame (0);
3792 #ifdef USE_X_TOOLKIT
3793 goto OTHER;
3794 #endif /* USE_X_TOOLKIT */
3795 break;
3797 case FocusOut:
3798 f = x_any_window_to_frame (event.xfocus.window);
3799 if (event.xfocus.detail != NotifyPointer
3800 && f == x_focus_event_frame)
3801 x_focus_event_frame = 0;
3802 if (f && f == x_focus_frame)
3803 x_new_focus_frame (0);
3804 #ifdef USE_X_TOOLKIT
3805 goto OTHER;
3806 #endif /* USE_X_TOOLKIT */
3807 break;
3809 #else /* ! defined (HAVE_X11) */
3811 case EnterWindow:
3812 if ((event.detail & 0xFF) == 1)
3813 break; /* Coming from our own subwindow */
3814 if (event.subwindow != 0)
3815 break; /* Entering our own subwindow. */
3818 f = x_window_to_frame (event.window);
3819 x_mouse_frame = f;
3821 x_new_focus_frame (f);
3823 break;
3825 case LeaveWindow:
3826 if ((event.detail & 0xFF) == 1)
3827 break; /* Entering our own subwindow */
3828 if (event.subwindow != 0)
3829 break; /* Leaving our own subwindow. */
3831 x_mouse_frame = 0;
3832 if (x_focus_frame == 0
3833 && x_input_frame != 0
3834 && x_input_frame == x_window_to_frame (event.window)
3835 && event.window == FRAME_X_WINDOW (x_input_frame))
3837 f = x_input_frame;
3838 x_input_frame = 0;
3839 if (f)
3840 frame_unhighlight (f);
3842 break;
3843 #endif /* ! defined (HAVE_X11) */
3845 #ifdef HAVE_X11
3846 case MotionNotify:
3848 if (x_mouse_grabbed)
3849 f = last_mouse_frame;
3850 else
3851 f = x_window_to_frame (event.xmotion.window);
3852 if (f)
3853 note_mouse_movement (f, &event.xmotion);
3854 else
3856 struct scroll_bar *bar
3857 = x_window_to_scroll_bar (event.xmotion.window);
3859 if (bar)
3860 x_scroll_bar_note_movement (bar, &event);
3862 /* If we move outside the frame,
3863 then we're certainly no longer on any text in the frame. */
3864 clear_mouse_face ();
3867 #ifdef USE_X_TOOLKIT
3868 goto OTHER;
3869 #endif /* USE_X_TOOLKIT */
3870 break;
3872 case ConfigureNotify:
3873 f = x_any_window_to_frame (event.xconfigure.window);
3874 #ifdef USE_X_TOOLKIT
3875 if (f
3876 && ! event.xconfigure.send_event
3877 && (event.xconfigure.window == XtWindow (f->display.x->widget)))
3879 Window win, child;
3880 int win_x, win_y;
3882 /* Find the position of the outside upper-left corner of
3883 the window, in the root coordinate system. Don't
3884 refer to the parent window here; we may be processing
3885 this event after the window manager has changed our
3886 parent, but before we have reached the ReparentNotify. */
3887 XTranslateCoordinates (x_current_display,
3889 /* From-window, to-window. */
3890 XtWindow (f->display.x->widget),
3891 ROOT_WINDOW,
3893 /* From-position, to-position. */
3894 -event.xconfigure.border_width,
3895 -event.xconfigure.border_width,
3896 &win_x, &win_y,
3898 /* Child of win. */
3899 &child);
3900 event.xconfigure.x = win_x;
3901 event.xconfigure.y = win_y;
3903 f->display.x->pixel_width = event.xconfigure.width;
3904 f->display.x->pixel_height = event.xconfigure.height;
3905 f->display.x->left_pos = event.xconfigure.x;
3906 f->display.x->top_pos = event.xconfigure.y;
3908 goto OTHER;
3909 #else /* not USE_X_TOOLKIT */
3910 if (f)
3912 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
3913 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
3915 /* Even if the number of character rows and columns has
3916 not changed, the font size may have changed, so we need
3917 to check the pixel dimensions as well. */
3918 if (columns != f->width
3919 || rows != f->height
3920 || event.xconfigure.width != f->display.x->pixel_width
3921 || event.xconfigure.height != f->display.x->pixel_height)
3923 change_frame_size (f, rows, columns, 0, 1);
3924 SET_FRAME_GARBAGED (f);
3927 if (! event.xconfigure.send_event)
3929 Window win, child;
3930 int win_x, win_y;
3932 /* Find the position of the outside upper-left corner of
3933 the window, in the root coordinate system. Don't
3934 refer to the parent window here; we may be processing
3935 this event after the window manager has changed our
3936 parent, but before we have reached the ReparentNotify. */
3937 XTranslateCoordinates (x_current_display,
3939 /* From-window, to-window. */
3940 f->display.x->window_desc,
3941 ROOT_WINDOW,
3943 /* From-position, to-position. */
3944 -event.xconfigure.border_width,
3945 -event.xconfigure.border_width,
3946 &win_x, &win_y,
3948 /* Child of win. */
3949 &child);
3950 event.xconfigure.x = win_x;
3951 event.xconfigure.y = win_y;
3954 f->display.x->pixel_width = event.xconfigure.width;
3955 f->display.x->pixel_height = event.xconfigure.height;
3956 f->display.x->left_pos = event.xconfigure.x;
3957 f->display.x->top_pos = event.xconfigure.y;
3959 #endif /* not USE_X_TOOLKIT */
3960 break;
3962 case ButtonPress:
3963 case ButtonRelease:
3965 /* If we decide we want to generate an event to be seen
3966 by the rest of Emacs, we put it here. */
3967 struct input_event emacs_event;
3968 emacs_event.kind = no_event;
3970 f = x_window_to_frame (event.xbutton.window);
3971 if (f)
3973 if (!x_focus_frame || (f == x_focus_frame))
3974 construct_mouse_click (&emacs_event, &event, f);
3976 else
3978 struct scroll_bar *bar =
3979 x_window_to_scroll_bar (event.xbutton.window);
3981 if (bar)
3982 x_scroll_bar_handle_click (bar, &event, &emacs_event);
3983 #ifdef USE_X_TOOLKIT
3984 else
3986 f = x_any_window_to_frame (event.xbutton.window);
3987 if (f && event.type == ButtonPress)
3988 construct_menu_click (&emacs_event,
3989 &event, f);
3991 #endif /* USE_X_TOOLKIT */
3994 if (numchars >= 1 && emacs_event.kind != no_event)
3996 bcopy (&emacs_event, bufp, sizeof (struct input_event));
3997 bufp++;
3998 count++;
3999 numchars--;
4002 #ifdef USE_X_TOOLKIT
4003 goto OTHER;
4004 #endif /* USE_X_TOOLKIT */
4006 break;
4008 #else /* ! defined (HAVE_X11) */
4009 case ButtonPressed:
4010 case ButtonReleased:
4011 f = x_window_to_frame (event.window);
4012 if (f)
4014 if (event.window == f->display.x->icon_desc)
4016 x_make_frame_visible (f);
4018 if (warp_mouse_on_deiconify)
4019 XWarpMouse (FRAME_X_WINDOW (f), 10, 10);
4020 break;
4022 if (event.window == FRAME_X_WINDOW (f))
4024 if (f->auto_raise)
4025 x_raise_frame (f);
4028 enqueue_event (&event, &x_mouse_queue);
4029 if (numchars >= 2)
4031 bufp->kind = ascii_keystroke;
4032 bufp->code = 'X' & 037; /* C-x */
4033 XSET (bufp->frame_or_window, Lisp_Frame, f);
4034 XSET (bufp->time, Lisp_Int, event.xkey.time);
4035 bufp++;
4037 bufp->kind = ascii_keystroke;
4038 bufp->code = 0; /* C-@ */
4039 XSET (bufp->frame_or_window, Lisp_Frame, f);
4040 XSET (bufp->time, Lisp_Int, event.xkey.time);
4041 bufp++;
4043 count += 2;
4044 numchars -= 2;
4046 break;
4047 #endif /* ! defined (HAVE_X11) */
4049 #ifdef HAVE_X11
4051 case CirculateNotify:
4052 break;
4053 case CirculateRequest:
4054 break;
4056 #endif /* ! defined (HAVE_X11) */
4058 case MappingNotify:
4059 /* Someone has changed the keyboard mapping - update the
4060 local cache. */
4061 switch (event.xmapping.request)
4063 case MappingModifier:
4064 x_find_modifier_meanings ();
4065 /* This is meant to fall through. */
4066 case MappingKeyboard:
4067 XRefreshKeyboardMapping (&event.xmapping);
4069 #ifdef USE_X_TOOLKIT
4070 goto OTHER;
4071 #endif /* USE_X_TOOLKIT */
4072 break;
4074 default:
4075 #ifdef USE_X_TOOLKIT
4076 OTHER:
4077 BLOCK_INPUT;
4078 XtDispatchEvent (&event);
4079 UNBLOCK_INPUT;
4080 #endif /* USE_X_TOOLKIT */
4081 break;
4085 #ifdef X_IO_BUG
4086 if (! event_found)
4087 /* On some systems, an X bug causes Emacs to get no more events
4088 when the window is destroyed. Detect that. (1994.) */
4089 XNoOp (x_current_display);
4090 #endif /* X_IO_BUG */
4092 #if 0 /* This fails for serial-line connections to the X server,
4093 because the characters arrive one by one, and a partial
4094 command makes select return but gives nothing to read.
4095 We'll have to hope that the bug that this tried to fix
4096 in 1988 has been fixed in Xlib or the X server. */
4097 #ifdef HAVE_SELECT
4098 if (expected && ! event_found)
4100 /* AOJ 880406: if select returns true but XPending doesn't, it means that
4101 there is an EOF condition; in other words, that X has died.
4102 Act as if there had been a hangup. */
4103 int fd = ConnectionNumber (x_current_display);
4104 SELECT_TYPE mask, junk1, junk2;
4105 EMACS_TIME timeout;
4107 FD_ZERO (&mask);
4108 FD_SET (fd, &mask);
4109 EMACS_SET_SECS_USECS (timeout, 0, 0);
4110 FD_ZERO (&junk1);
4111 FD_ZERO (&junk2);
4112 if (0 != select (fd + 1, &mask, &junk1, &junk2, &timeout)
4113 && !XStuffPending ())
4114 kill (getpid (), SIGHUP);
4116 #endif /* HAVE_SELECT */
4117 #endif /* 0 */
4119 #ifndef HAVE_X11
4120 if (updating_frame == 0)
4121 x_do_pending_expose ();
4122 #endif
4124 UNBLOCK_INPUT;
4125 return count;
4128 #ifndef HAVE_X11
4129 /* Read and process only Expose events
4130 until we get an ExposeCopy event; then return.
4131 This is used in insert/delete line.
4132 We assume input is already blocked. */
4134 static void
4135 x_read_exposes ()
4137 struct frame *f;
4138 XKeyPressedEvent event;
4140 while (1)
4142 /* while there are more events*/
4143 XMaskEvent (ExposeWindow | ExposeRegion | ExposeCopy, &event);
4144 switch (event.type)
4146 case ExposeWindow:
4147 if (event.subwindow != 0)
4148 break; /* duplicate event */
4149 f = x_window_to_frame (event.window);
4150 if (event.window == f->display.x->icon_desc)
4152 refreshicon (f);
4153 break;
4155 if (event.window == FRAME_X_WINDOW (f))
4157 expose_all_windows = 1;
4158 f->display.x->needs_exposure = 1;
4159 break;
4161 break;
4163 case ExposeRegion:
4164 if (event.subwindow != 0)
4165 break; /* duplicate event */
4166 f = x_window_to_frame (event.window);
4167 if (event.window == f->display.x->icon_desc)
4169 refreshicon (f);
4170 break;
4172 /* If window already needs full redraw, ignore this rectangle. */
4173 if (expose_all_windows && f->display.x->needs_exposure)
4174 break;
4175 /* Put the event on the queue of rectangles to redraw. */
4176 if (enqueue_event (&event, &x_expose_queue))
4177 /* If it is full, we can't record the rectangle,
4178 so redraw this entire window. */
4180 /* Say must check all windows' needs_exposure flags. */
4181 expose_all_windows = 1;
4182 f->display.x->needs_exposure = 1;
4184 break;
4186 case ExposeCopy:
4187 return;
4191 #endif /* HAVE_X11 */
4194 /* Drawing the cursor. */
4197 /* Draw a hollow box cursor. Don't change the inside of the box. */
4199 static void
4200 x_draw_box (f)
4201 struct frame *f;
4203 int left = CHAR_TO_PIXEL_COL (f, f->cursor_x);
4204 int top = CHAR_TO_PIXEL_ROW (f, f->cursor_y);
4205 int width = FONT_WIDTH (f->display.x->font);
4206 int height = FONT_HEIGHT (f->display.x->font);
4208 #ifdef HAVE_X11
4209 XDrawRectangle (x_current_display, FRAME_X_WINDOW (f),
4210 f->display.x->cursor_gc,
4211 left, top, width - 1, height - 1);
4212 #else /* ! defined (HAVE_X11) */
4213 XPixSet (FRAME_X_WINDOW (f),
4214 left, top, width, 1,
4215 f->display.x->cursor_pixel);
4217 XPixSet (FRAME_X_WINDOW (f),
4218 left, top, 1, height,
4219 f->display.x->cursor_pixel);
4221 XPixSet (FRAME_X_WINDOW (f),
4222 left+width-1, top, 1, height,
4223 f->display.x->cursor_pixel);
4225 XPixSet (FRAME_X_WINDOW (f),
4226 left, top+height-1, width, 1,
4227 f->display.x->cursor_pixel);
4228 #endif /* ! defined (HAVE_X11) */
4231 /* Clear the cursor of frame F to background color,
4232 and mark the cursor as not shown.
4233 This is used when the text where the cursor is
4234 is about to be rewritten. */
4236 static void
4237 clear_cursor (f)
4238 struct frame *f;
4240 int mask;
4242 if (! FRAME_VISIBLE_P (f)
4243 || f->phys_cursor_x < 0)
4244 return;
4246 #ifdef HAVE_X11
4247 x_display_cursor (f, 0);
4248 #else /* ! defined (HAVE_X11) */
4249 XPixSet (FRAME_X_WINDOW (f),
4250 CHAR_TO_PIXEL_COL (f, f->phys_cursor_x),
4251 CHAR_TO_PIXEL_ROW (f, f->phys_cursor_y),
4252 FONT_WIDTH (f->display.x->font), FONT_HEIGHT (f->display.x->font),
4253 f->display.x->background_pixel);
4254 #endif /* ! defined (HAVE_X11) */
4255 f->phys_cursor_x = -1;
4258 /* Redraw the glyph at ROW, COLUMN on frame F, in the style
4259 HIGHLIGHT. HIGHLIGHT is as defined for dumpglyphs. Return the
4260 glyph drawn. */
4262 static void
4263 x_draw_single_glyph (f, row, column, glyph, highlight)
4264 struct frame *f;
4265 int row, column;
4266 GLYPH glyph;
4267 int highlight;
4269 dumpglyphs (f,
4270 CHAR_TO_PIXEL_COL (f, column),
4271 CHAR_TO_PIXEL_ROW (f, row),
4272 &glyph, 1, highlight);
4275 static void
4276 x_display_bar_cursor (f, on)
4277 struct frame *f;
4278 int on;
4280 struct frame_glyphs *current_glyphs = FRAME_CURRENT_GLYPHS (f);
4282 /* This is pointless on invisible frames, and dangerous on garbaged
4283 frames; in the latter case, the frame may be in the midst of
4284 changing its size, and curs_x and curs_y may be off the frame. */
4285 if (! FRAME_VISIBLE_P (f) || FRAME_GARBAGED_P (f))
4286 return;
4288 if (! on && f->phys_cursor_x < 0)
4289 return;
4291 /* If we're not updating, then we want to use the current frame's
4292 cursor position, not our local idea of where the cursor ought to be. */
4293 if (f != updating_frame)
4295 curs_x = FRAME_CURSOR_X (f);
4296 curs_y = FRAME_CURSOR_Y (f);
4299 /* If there is anything wrong with the current cursor state, remove it. */
4300 if (f->phys_cursor_x >= 0
4301 && (!on
4302 || f->phys_cursor_x != curs_x
4303 || f->phys_cursor_y != curs_y
4304 || f->display.x->current_cursor != bar_cursor))
4306 /* Erase the cursor by redrawing the character underneath it. */
4307 x_draw_single_glyph (f, f->phys_cursor_y, f->phys_cursor_x,
4308 f->phys_cursor_glyph,
4309 current_glyphs->highlight[f->phys_cursor_y]);
4310 f->phys_cursor_x = -1;
4313 /* If we now need a cursor in the new place or in the new form, do it so. */
4314 if (on
4315 && (f->phys_cursor_x < 0
4316 || (f->display.x->current_cursor != bar_cursor)))
4318 f->phys_cursor_glyph
4319 = ((current_glyphs->enable[curs_y]
4320 && curs_x < current_glyphs->used[curs_y])
4321 ? current_glyphs->glyphs[curs_y][curs_x]
4322 : SPACEGLYPH);
4323 XFillRectangle (x_current_display, FRAME_X_WINDOW (f),
4324 f->display.x->cursor_gc,
4325 CHAR_TO_PIXEL_COL (f, curs_x),
4326 CHAR_TO_PIXEL_ROW (f, curs_y),
4327 1, FONT_HEIGHT (f->display.x->font));
4329 f->phys_cursor_x = curs_x;
4330 f->phys_cursor_y = curs_y;
4332 f->display.x->current_cursor = bar_cursor;
4335 if (updating_frame != f)
4336 XFlushQueue ();
4340 /* Turn the displayed cursor of frame F on or off according to ON.
4341 If ON is nonzero, where to put the cursor is specified
4342 by F->cursor_x and F->cursor_y. */
4344 static void
4345 x_display_box_cursor (f, on)
4346 struct frame *f;
4347 int on;
4349 struct frame_glyphs *current_glyphs = FRAME_CURRENT_GLYPHS (f);
4351 /* This is pointless on invisible frames, and dangerous on garbaged
4352 frames; in the latter case, the frame may be in the midst of
4353 changing its size, and curs_x and curs_y may be off the frame. */
4354 if (! FRAME_VISIBLE_P (f) || FRAME_GARBAGED_P (f))
4355 return;
4357 /* If cursor is off and we want it off, return quickly. */
4358 if (!on && f->phys_cursor_x < 0)
4359 return;
4361 /* If we're not updating, then we want to use the current frame's
4362 cursor position, not our local idea of where the cursor ought to be. */
4363 if (f != updating_frame)
4365 curs_x = FRAME_CURSOR_X (f);
4366 curs_y = FRAME_CURSOR_Y (f);
4369 /* If cursor is currently being shown and we don't want it to be
4370 or it is in the wrong place,
4371 or we want a hollow box and it's not so, (pout!)
4372 erase it. */
4373 if (f->phys_cursor_x >= 0
4374 && (!on
4375 || f->phys_cursor_x != curs_x
4376 || f->phys_cursor_y != curs_y
4377 || (f->display.x->current_cursor != hollow_box_cursor
4378 && (f != x_highlight_frame))))
4380 /* Erase the cursor by redrawing the character underneath it. */
4381 x_draw_single_glyph (f, f->phys_cursor_y, f->phys_cursor_x,
4382 f->phys_cursor_glyph,
4383 current_glyphs->highlight[f->phys_cursor_y]);
4384 f->phys_cursor_x = -1;
4387 /* If we want to show a cursor,
4388 or we want a box cursor and it's not so,
4389 write it in the right place. */
4390 if (on
4391 && (f->phys_cursor_x < 0
4392 || (f->display.x->current_cursor != filled_box_cursor
4393 && f == x_highlight_frame)))
4395 f->phys_cursor_glyph
4396 = ((current_glyphs->enable[curs_y]
4397 && curs_x < current_glyphs->used[curs_y])
4398 ? current_glyphs->glyphs[curs_y][curs_x]
4399 : SPACEGLYPH);
4400 if (f != x_highlight_frame)
4402 x_draw_box (f);
4403 f->display.x->current_cursor = hollow_box_cursor;
4405 else
4407 x_draw_single_glyph (f, curs_y, curs_x,
4408 f->phys_cursor_glyph, 2);
4409 f->display.x->current_cursor = filled_box_cursor;
4412 f->phys_cursor_x = curs_x;
4413 f->phys_cursor_y = curs_y;
4416 if (updating_frame != f)
4417 XFlushQueue ();
4420 x_display_cursor (f, on)
4421 struct frame *f;
4422 int on;
4424 BLOCK_INPUT;
4426 if (FRAME_DESIRED_CURSOR (f) == filled_box_cursor)
4427 x_display_box_cursor (f, on);
4428 else if (FRAME_DESIRED_CURSOR (f) == bar_cursor)
4429 x_display_bar_cursor (f, on);
4430 else
4431 /* Those are the only two we have implemented! */
4432 abort ();
4434 UNBLOCK_INPUT;
4437 /* Icons. */
4439 /* Refresh bitmap kitchen sink icon for frame F
4440 when we get an expose event for it. */
4442 refreshicon (f)
4443 struct frame *f;
4445 #ifdef HAVE_X11
4446 /* Normally, the window manager handles this function. */
4447 #else /* ! defined (HAVE_X11) */
4448 int mask;
4450 if (f->display.x->icon_bitmap_flag)
4451 XBitmapBitsPut (f->display.x->icon_desc, 0, 0, sink_width, sink_height,
4452 sink_bits, BlackPixel, WHITE_PIX_DEFAULT,
4453 icon_bitmap, GXcopy, AllPlanes);
4454 else
4456 extern struct frame *selected_frame;
4457 struct Lisp_String *str;
4458 unsigned char *string;
4460 string
4461 = XSTRING (XBUFFER (XWINDOW (f->selected_window)->buffer)->name)->data;
4463 if (f->display.x->icon_label != string)
4465 f->display.x->icon_label = string;
4466 XChangeWindow (f->display.x->icon_desc,
4467 XQueryWidth (string, icon_font_info->id) + 10,
4468 icon_font_info->height + 10);
4471 XText (f->display.x->icon_desc, 5, 5, string,
4472 str->size, icon_font_info->id,
4473 BLACK_PIX_DEFAULT, WHITE_PIX_DEFAULT);
4475 XFlushQueue ();
4476 #endif /* ! defined (HAVE_X11) */
4479 /* Make the x-window of frame F use the gnu icon bitmap. */
4482 x_bitmap_icon (f)
4483 struct frame *f;
4485 int mask;
4486 Window icon_window;
4488 if (FRAME_X_WINDOW (f) == 0)
4489 return 1;
4491 #ifdef HAVE_X11
4492 if (! icon_bitmap)
4493 icon_bitmap =
4494 XCreateBitmapFromData (x_current_display, FRAME_X_WINDOW (f),
4495 gnu_bits, gnu_width, gnu_height);
4496 x_wm_set_icon_pixmap (f, icon_bitmap);
4497 f->display.x->icon_bitmap_flag = 1;
4498 #else /* ! defined (HAVE_X11) */
4499 if (f->display.x->icon_desc)
4501 XClearIconWindow (FRAME_X_WINDOW (f));
4502 XDestroyWindow (f->display.x->icon_desc);
4505 icon_window = XCreateWindow (f->display.x->parent_desc,
4506 0, 0, sink_width, sink_height,
4507 2, WhitePixmap, (Pixmap) NULL);
4509 if (icon_window == 0)
4510 return 1;
4512 XSetIconWindow (FRAME_X_WINDOW (f), icon_window);
4513 XSelectInput (icon_window, ExposeWindow | UnmapWindow);
4515 f->display.x->icon_desc = icon_window;
4516 f->display.x->icon_bitmap_flag = 1;
4518 if (icon_bitmap == 0)
4519 icon_bitmap
4520 = XStoreBitmap (sink_mask_width, sink_mask_height, sink_mask_bits);
4521 #endif /* ! defined (HAVE_X11) */
4523 return 0;
4527 /* Make the x-window of frame F use a rectangle with text. */
4530 x_text_icon (f, icon_name)
4531 struct frame *f;
4532 char *icon_name;
4534 #ifndef HAVE_X11
4535 int mask;
4536 int width;
4537 Window icon_window;
4538 char *X_DefaultValue;
4539 Bitmap b1;
4541 #ifndef WhitePixel
4542 #define WhitePixel 1
4543 #endif /* WhitePixel */
4545 #ifndef BlackPixel
4546 #define BlackPixel 0
4547 #endif /* BlackPixel */
4548 #endif /* HAVE_X11 */
4550 if (FRAME_X_WINDOW (f) == 0)
4551 return 1;
4553 #ifdef HAVE_X11
4554 if (icon_name)
4555 f->display.x->icon_label = icon_name;
4556 else
4557 if (! f->display.x->icon_label)
4558 f->display.x->icon_label = " *emacs* ";
4560 #if 0
4561 XSetIconName (x_current_display, FRAME_X_WINDOW (f),
4562 (char *) f->display.x->icon_label);
4563 #endif
4565 f->display.x->icon_bitmap_flag = 0;
4566 x_wm_set_icon_pixmap (f, 0);
4567 #else /* ! defined (HAVE_X11) */
4568 if (icon_font_info == 0)
4569 icon_font_info
4570 = XGetFont (XGetDefault (XDISPLAY
4571 (char *) XSTRING (Vinvocation_name)->data,
4572 "BodyFont"));
4574 if (f->display.x->icon_desc)
4576 XClearIconWindow (XDISPLAY FRAME_X_WINDOW (f));
4577 XDestroyWindow (XDISPLAY f->display.x->icon_desc);
4580 if (icon_name)
4581 f->display.x->icon_label = (unsigned char *) icon_name;
4582 else
4583 if (! f->display.x->icon_label)
4584 f->display.x->icon_label = XSTRING (f->name)->data;
4586 width = XStringWidth (f->display.x->icon_label, icon_font_info, 0, 0);
4587 icon_window = XCreateWindow (f->display.x->parent_desc,
4588 f->display.x->left_pos,
4589 f->display.x->top_pos,
4590 width + 10, icon_font_info->height + 10,
4591 2, BlackPixmap, WhitePixmap);
4593 if (icon_window == 0)
4594 return 1;
4596 XSetIconWindow (FRAME_X_WINDOW (f), icon_window);
4597 XSelectInput (icon_window, ExposeWindow | ExposeRegion | UnmapWindow | ButtonPressed);
4599 f->display.x->icon_desc = icon_window;
4600 f->display.x->icon_bitmap_flag = 0;
4601 f->display.x->icon_label = 0;
4602 #endif /* ! defined (HAVE_X11) */
4604 return 0;
4607 /* Handling X errors. */
4609 /* Shut down Emacs in an orderly fashion, because of a SIGPIPE on the
4610 X server's connection, or an error reported via the X protocol. */
4612 static SIGTYPE
4613 x_connection_closed ()
4615 if (_Xdebug)
4616 abort ();
4618 shut_down_emacs (0, 1, Qnil);
4620 exit (70);
4623 /* An X error handler which prints an error message and then kills
4624 Emacs. This is what's normally installed as Xlib's handler for
4625 protocol errors. */
4626 static int
4627 x_error_quitter (display, error)
4628 Display *display;
4629 XErrorEvent *error;
4631 char buf[256];
4633 /* Note that there is no real way portable across R3/R4 to get the
4634 original error handler. */
4636 XGetErrorText (display, error->error_code, buf, sizeof (buf));
4637 fprintf (stderr, "X protocol error: %s on protocol request %d\n",
4638 buf, error->request_code);
4640 #if 0
4641 /* While we're testing Emacs 19, we'll just dump core whenever we
4642 get an X error, so we can figure out why it happened. */
4643 abort ();
4644 #endif
4646 x_connection_closed ();
4649 /* A handler for X IO errors which prints an error message and then
4650 kills Emacs. This is what is always installed as Xlib's handler
4651 for I/O errors. */
4652 static int
4653 x_io_error_quitter (display)
4654 Display *display;
4656 fprintf (stderr, "Connection to X server %s lost.\n",
4657 XDisplayName (DisplayString (display)));
4659 #if 0
4660 /* While we're testing Emacs 19, we'll just dump core whenever we
4661 get an X error, so we can figure out why it happened. */
4662 abort ();
4663 #endif
4665 x_connection_closed ();
4668 /* A buffer for storing X error messages. */
4669 static char *x_caught_error_message;
4670 #define X_CAUGHT_ERROR_MESSAGE_SIZE 200
4672 /* An X error handler which stores the error message in
4673 x_caught_error_message. This is what's installed when
4674 x_catch_errors is in effect. */
4675 static int
4676 x_error_catcher (display, error)
4677 Display *display;
4678 XErrorEvent *error;
4680 XGetErrorText (display, error->error_code,
4681 x_caught_error_message, X_CAUGHT_ERROR_MESSAGE_SIZE);
4685 /* Begin trapping X errors.
4687 After calling this function, X protocol errors no longer cause
4688 Emacs to exit; instead, they are recorded in x_cfc_error_message.
4690 Calling x_check_errors signals an Emacs error if an X error has
4691 occurred since the last call to x_catch_errors or x_check_errors.
4693 Calling x_uncatch_errors resumes the normal error handling. */
4695 void x_catch_errors (), x_check_errors (), x_uncatch_errors ();
4697 void
4698 x_catch_errors ()
4700 /* Make sure any errors from previous requests have been dealt with. */
4701 XSync (x_current_display, False);
4703 /* Set up the error buffer. */
4704 x_caught_error_message
4705 = (char*) xmalloc (X_CAUGHT_ERROR_MESSAGE_SIZE);
4706 x_caught_error_message[0] = '\0';
4708 /* Install our little error handler. */
4709 XHandleError (x_error_catcher);
4712 /* If any X protocol errors have arrived since the last call to
4713 x_catch_errors or x_check_errors, signal an Emacs error using
4714 sprintf (a buffer, FORMAT, the x error message text) as the text. */
4716 void
4717 x_check_errors (format)
4718 char *format;
4720 /* Make sure to catch any errors incurred so far. */
4721 XSync (x_current_display, False);
4723 if (x_caught_error_message[0])
4725 char buf[X_CAUGHT_ERROR_MESSAGE_SIZE + 56];
4727 sprintf (buf, format, x_caught_error_message);
4728 x_uncatch_errors ();
4729 error (buf);
4733 /* Nonzero if we had any X protocol errors since we did x_catch_errors. */
4736 x_had_errors_p ()
4738 /* Make sure to catch any errors incurred so far. */
4739 XSync (x_current_display, False);
4741 return x_caught_error_message[0] != 0;
4744 /* Stop catching X protocol errors and let them make Emacs die. */
4746 void
4747 x_uncatch_errors ()
4749 xfree (x_caught_error_message);
4750 x_caught_error_message = 0;
4751 XHandleError (x_error_quitter);
4754 #if 0
4755 static unsigned int x_wire_count;
4756 x_trace_wire ()
4758 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
4760 #endif /* ! 0 */
4763 /* Changing the font of the frame. */
4765 /* Set the font of the x-window specified by frame F
4766 to the font named NEWNAME. This is safe to use
4767 even before F has an actual x-window. */
4769 #ifdef HAVE_X11
4771 struct font_info
4773 XFontStruct *font;
4774 char *name;
4777 /* A table of all the fonts we have already loaded. */
4778 static struct font_info *x_font_table;
4780 /* The current capacity of x_font_table. */
4781 static int x_font_table_size;
4783 /* The number of fonts actually stored in x_font_table.
4784 x_font_table[n] is used and valid iff 0 <= n < n_fonts.
4785 0 <= n_fonts <= x_font_table_size. */
4786 static int n_fonts;
4788 Lisp_Object
4789 x_new_font (f, fontname)
4790 struct frame *f;
4791 register char *fontname;
4793 int already_loaded;
4794 int n_matching_fonts;
4795 XFontStruct *font_info;
4796 char **font_names;
4798 /* Get a list of all the fonts that match this name. Once we
4799 have a list of matching fonts, we compare them against the fonts
4800 we already have by comparing font ids. */
4801 font_names = (char **) XListFonts (x_current_display, fontname,
4802 1024, &n_matching_fonts);
4803 /* Apparently it doesn't set n_matching_fonts to zero when it can't
4804 find any matches; font_names == 0 is the only clue. */
4805 if (! font_names)
4806 n_matching_fonts = 0;
4808 /* Don't just give up if n_matching_fonts is 0.
4809 Apparently there's a bug on Suns: XListFontsWithInfo can
4810 fail to find a font, but XLoadQueryFont may still find it. */
4812 /* See if we've already loaded a matching font. */
4813 already_loaded = -1;
4814 if (n_matching_fonts != 0)
4816 int i, j;
4818 for (i = 0; i < n_fonts; i++)
4819 for (j = 0; j < n_matching_fonts; j++)
4820 if (!strcmp (x_font_table[i].name, font_names[j]))
4822 already_loaded = i;
4823 fontname = font_names[j];
4824 goto found_font;
4827 found_font:
4829 /* If we have, just return it from the table. */
4830 if (already_loaded >= 0)
4831 f->display.x->font = x_font_table[already_loaded].font;
4833 /* Otherwise, load the font and add it to the table. */
4834 else
4836 int i;
4837 XFontStruct *font;
4839 /* Try to find a character-cell font in the list. */
4840 #if 0
4841 /* A laudable goal, but this isn't how to do it. */
4842 for (i = 0; i < n_matching_fonts; i++)
4843 if (! font_info[i].per_char)
4844 break;
4845 #else
4846 i = 0;
4847 #endif
4849 /* See comment above. */
4850 if (n_matching_fonts != 0)
4851 fontname = font_names[i];
4853 font = (XFontStruct *) XLoadQueryFont (x_current_display, fontname);
4854 if (! font)
4856 /* Free the information from XListFonts. */
4857 if (n_matching_fonts)
4858 XFreeFontNames (font_names);
4859 return Qnil;
4862 /* Do we need to create the table? */
4863 if (x_font_table_size == 0)
4865 x_font_table_size = 16;
4866 x_font_table
4867 = (struct font_info *) xmalloc (x_font_table_size
4868 * sizeof (x_font_table[0]));
4870 /* Do we need to grow the table? */
4871 else if (n_fonts >= x_font_table_size)
4873 x_font_table_size *= 2;
4874 x_font_table
4875 = (struct font_info *) xrealloc (x_font_table,
4876 (x_font_table_size
4877 * sizeof (x_font_table[0])));
4880 x_font_table[n_fonts].name = (char *) xmalloc (strlen (fontname) + 1);
4881 bcopy (fontname, x_font_table[n_fonts].name, strlen (fontname) + 1);
4882 f->display.x->font = x_font_table[n_fonts++].font = font;
4885 /* Now make the frame display the given font. */
4886 if (FRAME_X_WINDOW (f) != 0)
4888 XSetFont (x_current_display, f->display.x->normal_gc,
4889 f->display.x->font->fid);
4890 XSetFont (x_current_display, f->display.x->reverse_gc,
4891 f->display.x->font->fid);
4892 XSetFont (x_current_display, f->display.x->cursor_gc,
4893 f->display.x->font->fid);
4895 x_set_window_size (f, 0, f->width, f->height);
4899 Lisp_Object lispy_name;
4901 lispy_name = build_string (fontname);
4903 /* Free the information from XListFonts. The data
4904 we actually retain comes from XLoadQueryFont. */
4905 XFreeFontNames (font_names);
4907 return lispy_name;
4910 #else /* ! defined (HAVE_X11) */
4911 x_new_font (f, newname)
4912 struct frame *f;
4913 register char *newname;
4915 FONT_TYPE *temp;
4916 int mask;
4918 temp = XGetFont (newname);
4919 if (temp == (FONT_TYPE *) 0)
4920 return 1;
4922 if (f->display.x->font)
4923 XLoseFont (f->display.x->font);
4925 f->display.x->font = temp;
4927 if (FRAME_X_WINDOW (f) != 0)
4928 x_set_window_size (f, 0, f->width, f->height);
4930 return 0;
4932 #endif /* ! defined (HAVE_X11) */
4934 x_calc_absolute_position (f)
4935 struct frame *f;
4937 #ifdef HAVE_X11
4938 Window win, child;
4939 int win_x = 0, win_y = 0;
4941 /* Find the position of the outside upper-left corner of
4942 the inner window, with respect to the outer window. */
4943 if (f->display.x->parent_desc != ROOT_WINDOW)
4945 BLOCK_INPUT;
4946 XTranslateCoordinates (x_current_display,
4948 /* From-window, to-window. */
4949 f->display.x->window_desc,
4950 f->display.x->parent_desc,
4952 /* From-position, to-position. */
4953 0, 0, &win_x, &win_y,
4955 /* Child of win. */
4956 &child);
4957 UNBLOCK_INPUT;
4960 /* Treat negative positions as relative to the leftmost bottommost
4961 position that fits on the screen. */
4962 if (f->display.x->left_pos < 0)
4963 f->display.x->left_pos = (x_screen_width
4964 - 2 * f->display.x->border_width - win_x
4965 - PIXEL_WIDTH (f)
4966 + f->display.x->left_pos);
4968 if (f->display.x->top_pos < 0)
4969 f->display.x->top_pos = (x_screen_height
4970 - 2 * f->display.x->border_width - win_y
4971 - PIXEL_HEIGHT (f)
4972 + f->display.x->top_pos);
4974 #else /* ! defined (HAVE_X11) */
4975 WINDOWINFO_TYPE parentinfo;
4977 XGetWindowInfo (FRAME_X_WINDOW (f), &parentinfo);
4979 if (f->display.x->left_pos < 0)
4980 f->display.x->left_pos = parentinfo.width + (f->display.x->left_pos + 1)
4981 - PIXEL_WIDTH (f) - 2 * f->display.x->internal_border_width;
4983 if (f->display.x->top_pos < 0)
4984 f->display.x->top_pos = parentinfo.height + (f->display.x->top_pos + 1)
4985 - PIXEL_HEIGHT (f) - 2 * f->display.x->internal_border_width;
4986 #endif /* ! defined (HAVE_X11) */
4989 x_set_offset (f, xoff, yoff)
4990 struct frame *f;
4991 register int xoff, yoff;
4993 f->display.x->top_pos = yoff;
4994 f->display.x->left_pos = xoff;
4995 x_calc_absolute_position (f);
4997 BLOCK_INPUT;
4998 #ifdef USE_X_TOOLKIT
4999 XMoveWindow (XDISPLAY XtWindow (f->display.x->widget),
5000 f->display.x->left_pos, f->display.x->top_pos);
5001 #else /* not USE_X_TOOLKIT */
5002 XMoveWindow (XDISPLAY FRAME_X_WINDOW (f),
5003 f->display.x->left_pos, f->display.x->top_pos);
5004 #endif /* not USE_X_TOOLKIT */
5005 #ifdef HAVE_X11
5006 x_wm_set_size_hint (f, 0, 1, xoff, yoff);
5007 #endif /* ! defined (HAVE_X11) */
5008 UNBLOCK_INPUT;
5011 /* Call this to change the size of frame F's x-window.
5012 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
5013 for this size change and subsequent size changes.
5014 Otherwise we leave the window gravity unchanged. */
5016 x_set_window_size (f, change_gravity, cols, rows)
5017 struct frame *f;
5018 int change_gravity;
5019 int cols, rows;
5021 int pixelwidth, pixelheight;
5022 int mask;
5024 #ifdef USE_X_TOOLKIT
5025 BLOCK_INPUT;
5026 EmacsFrameSetCharSize (f->display.x->edit_widget, cols, rows);
5027 UNBLOCK_INPUT;
5029 #else /* not USE_X_TOOLKIT */
5031 BLOCK_INPUT;
5033 check_frame_size (f, &rows, &cols);
5034 f->display.x->vertical_scroll_bar_extra
5035 = (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
5036 ? VERTICAL_SCROLL_BAR_PIXEL_WIDTH (f)
5037 : 0);
5038 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
5039 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
5041 #ifdef HAVE_X11
5042 x_wm_set_size_hint (f, 0, change_gravity, 0, 0);
5043 #endif /* ! defined (HAVE_X11) */
5044 XChangeWindowSize (FRAME_X_WINDOW (f), pixelwidth, pixelheight);
5046 /* Now, strictly speaking, we can't be sure that this is accurate,
5047 but the window manager will get around to dealing with the size
5048 change request eventually, and we'll hear how it went when the
5049 ConfigureNotify event gets here.
5051 We could just not bother storing any of this information here,
5052 and let the ConfigureNotify event set everything up, but that
5053 might be kind of confusing to the lisp code, since size changes
5054 wouldn't be reported in the frame parameters until some random
5055 point in the future when the ConfigureNotify event arrives. */
5056 change_frame_size (f, rows, cols, 0, 0);
5057 PIXEL_WIDTH (f) = pixelwidth;
5058 PIXEL_HEIGHT (f) = pixelheight;
5060 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
5061 receive in the ConfigureNotify event; if we get what we asked
5062 for, then the event won't cause the screen to become garbaged, so
5063 we have to make sure to do it here. */
5064 SET_FRAME_GARBAGED (f);
5066 XFlushQueue ();
5067 UNBLOCK_INPUT;
5068 #endif /* not USE_X_TOOLKIT */
5071 #ifndef HAVE_X11
5072 x_set_resize_hint (f)
5073 struct frame *f;
5075 XSetResizeHint (FRAME_X_WINDOW (f),
5076 2 * f->display.x->internal_border_width,
5077 2 * f->display.x->internal_border_width,
5078 FONT_WIDTH (f->display.x->font),
5079 FONT_HEIGHT (f->display.x->font));
5081 #endif /* HAVE_X11 */
5083 /* Mouse warping, focus shifting, raising and lowering. */
5085 x_set_mouse_position (f, x, y)
5086 struct frame *f;
5087 int x, y;
5089 int pix_x, pix_y;
5091 #if 0 /* Let the user ask for this if he wants it. */
5092 x_raise_frame (f);
5093 #endif
5095 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->display.x->font) / 2;
5096 pix_y = CHAR_TO_PIXEL_ROW (f, y) + FONT_HEIGHT (f->display.x->font) / 2;
5098 if (pix_x < 0) pix_x = 0;
5099 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
5101 if (pix_y < 0) pix_y = 0;
5102 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
5104 BLOCK_INPUT;
5106 XWarpMousePointer (FRAME_X_WINDOW (f), pix_x, pix_y);
5107 UNBLOCK_INPUT;
5110 #ifdef HAVE_X11
5111 x_focus_on_frame (f)
5112 struct frame *f;
5114 #if 0 /* This proves to be unpleasant. */
5115 x_raise_frame (f);
5116 #endif
5117 #if 0
5118 /* I don't think that the ICCCM allows programs to do things like this
5119 without the interaction of the window manager. Whatever you end up
5120 doing with this code, do it to x_unfocus_frame too. */
5121 XSetInputFocus (x_current_display, FRAME_X_WINDOW (f),
5122 RevertToPointerRoot, CurrentTime);
5123 #endif /* ! 0 */
5126 x_unfocus_frame (f)
5127 struct frame *f;
5129 #if 0
5130 /* Look at the remarks in x_focus_on_frame. */
5131 if (x_focus_frame == f)
5132 XSetInputFocus (x_current_display, PointerRoot,
5133 RevertToPointerRoot, CurrentTime);
5134 #endif /* ! 0 */
5137 #endif /* ! defined (HAVE_X11) */
5139 /* Raise frame F. */
5141 x_raise_frame (f)
5142 struct frame *f;
5144 if (f->async_visible)
5146 BLOCK_INPUT;
5147 #ifdef USE_X_TOOLKIT
5148 XRaiseWindow (XDISPLAY XtWindow (f->display.x->widget));
5149 #else /* not USE_X_TOOLKIT */
5150 XRaiseWindow (XDISPLAY FRAME_X_WINDOW (f));
5151 #endif /* not USE_X_TOOLKIT */
5152 XFlushQueue ();
5153 UNBLOCK_INPUT;
5157 /* Lower frame F. */
5159 x_lower_frame (f)
5160 struct frame *f;
5162 if (f->async_visible)
5164 BLOCK_INPUT;
5165 #ifdef USE_X_TOOLKIT
5166 XLowerWindow (XDISPLAY XtWindow (f->display.x->widget));
5167 #else /* not USE_X_TOOLKIT */
5168 XLowerWindow (XDISPLAY FRAME_X_WINDOW (f));
5169 #endif /* not USE_X_TOOLKIT */
5170 XFlushQueue ();
5171 UNBLOCK_INPUT;
5175 static void
5176 XTframe_raise_lower (f, raise)
5177 FRAME_PTR f;
5178 int raise;
5180 if (raise)
5181 x_raise_frame (f);
5182 else
5183 x_lower_frame (f);
5187 /* Change from withdrawn state to mapped state. */
5189 x_make_frame_visible (f)
5190 struct frame *f;
5192 int mask;
5194 BLOCK_INPUT;
5196 if (! FRAME_VISIBLE_P (f))
5198 #ifdef HAVE_X11
5199 if (! EQ (Vx_no_window_manager, Qt))
5200 x_wm_set_window_state (f, NormalState);
5201 #ifdef USE_X_TOOLKIT
5202 XtPopup (f->display.x->widget, XtGrabNone);
5203 #else /* not USE_X_TOOLKIT */
5204 XMapWindow (XDISPLAY FRAME_X_WINDOW (f));
5205 #endif /* not USE_X_TOOLKIT */
5206 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5207 XMapSubwindows (x_current_display, FRAME_X_WINDOW (f));
5208 #else /* ! defined (HAVE_X11) */
5209 XMapWindow (XDISPLAY FRAME_X_WINDOW (f));
5210 if (f->display.x->icon_desc != 0)
5211 XUnmapWindow (f->display.x->icon_desc);
5213 /* Handled by the MapNotify event for X11 */
5214 f->async_visible = 1;
5215 f->async_iconified = 0;
5217 /* NOTE: this may cause problems for the first frame. */
5218 XTcursor_to (0, 0);
5219 #endif /* ! defined (HAVE_X11) */
5222 XFlushQueue ();
5224 UNBLOCK_INPUT;
5227 /* Change from mapped state to withdrawn state. */
5229 x_make_frame_invisible (f)
5230 struct frame *f;
5232 int mask;
5234 /* Don't keep the highlight on an invisible frame. */
5235 if (x_highlight_frame == f)
5236 x_highlight_frame = 0;
5238 if (! f->async_visible && ! f->async_iconified)
5239 return;
5241 BLOCK_INPUT;
5243 #ifdef HAVE_X11R4
5245 #ifdef USE_X_TOOLKIT
5246 if (! XWithdrawWindow (x_current_display, XtWindow (f->display.x->widget),
5247 DefaultScreen (x_current_display)))
5248 #else /* not USE_X_TOOLKIT */
5249 if (! XWithdrawWindow (x_current_display, FRAME_X_WINDOW (f),
5250 DefaultScreen (x_current_display)))
5251 #endif /* not USE_X_TOOLKIT */
5253 UNBLOCK_INPUT_RESIGNAL;
5254 error ("can't notify window manager of window withdrawal");
5257 #else /* ! defined (HAVE_X11R4) */
5258 #ifdef HAVE_X11
5260 /* Tell the window manager what we're going to do. */
5261 if (! EQ (Vx_no_window_manager, Qt))
5263 XEvent unmap;
5265 unmap.xunmap.type = UnmapNotify;
5266 #ifdef USE_X_TOOLKIT
5267 unmap.xunmap.window = XtWindow (f->display.x->widget);
5268 #else /* not USE_X_TOOLKIT */
5269 unmap.xunmap.window = FRAME_X_WINDOW (f);
5270 #endif /* not USE_X_TOOLKIT */
5271 unmap.xunmap.event = DefaultRootWindow (x_current_display);
5272 unmap.xunmap.from_configure = False;
5273 if (! XSendEvent (x_current_display,
5274 DefaultRootWindow (x_current_display),
5275 False,
5276 SubstructureRedirectMask|SubstructureNotifyMask,
5277 &unmap))
5279 UNBLOCK_INPUT_RESIGNAL;
5280 error ("can't notify window manager of withdrawal");
5284 /* Unmap the window ourselves. Cheeky! */
5285 #ifdef USE_X_TOOLKIT
5286 XUnmapWindow (x_current_display, XtWindow (f->display.x->widget));
5287 #else /* not USE_X_TOOLKIT */
5288 XUnmapWindow (x_current_display, FRAME_X_WINDOW (f));
5289 #endif /* not USE_X_TOOLKIT */
5290 #else /* ! defined (HAVE_X11) */
5292 XUnmapWindow (FRAME_X_WINDOW (f));
5293 f->async_visible = 0; /* Handled by the UnMap event for X11 */
5294 if (f->display.x->icon_desc != 0)
5295 XUnmapWindow (f->display.x->icon_desc);
5297 #endif /* ! defined (HAVE_X11) */
5298 #endif /* ! defined (HAVE_X11R4) */
5300 XFlushQueue ();
5301 UNBLOCK_INPUT;
5304 /* Change window state from mapped to iconified. */
5306 x_iconify_frame (f)
5307 struct frame *f;
5309 int mask;
5310 int result;
5312 /* Don't keep the highlight on an invisible frame. */
5313 if (x_highlight_frame == f)
5314 x_highlight_frame = 0;
5316 if (f->async_iconified)
5317 return;
5319 #ifdef USE_X_TOOLKIT
5320 BLOCK_INPUT;
5321 result = XIconifyWindow (x_current_display,
5322 XtWindow (f->display.x->widget),
5323 DefaultScreen (x_current_display));
5324 UNBLOCK_INPUT;
5326 if (!result)
5327 error ("Can't notify window manager of iconification.");
5329 f->async_iconified = 1;
5331 BLOCK_INPUT;
5332 XFlushQueue ();
5333 UNBLOCK_INPUT;
5334 #else /* not USE_X_TOOLKIT */
5336 BLOCK_INPUT;
5338 #ifdef HAVE_X11
5339 /* Since we don't know which revision of X we're running, we'll use both
5340 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
5342 /* X11R4: send a ClientMessage to the window manager using the
5343 WM_CHANGE_STATE type. */
5345 XEvent message;
5347 message.xclient.window = FRAME_X_WINDOW (f);
5348 message.xclient.type = ClientMessage;
5349 message.xclient.message_type = Xatom_wm_change_state;
5350 message.xclient.format = 32;
5351 message.xclient.data.l[0] = IconicState;
5353 if (! XSendEvent (x_current_display,
5354 DefaultRootWindow (x_current_display),
5355 False,
5356 SubstructureRedirectMask | SubstructureNotifyMask,
5357 &message))
5359 UNBLOCK_INPUT_RESIGNAL;
5360 error ("Can't notify window manager of iconification.");
5364 /* X11R3: set the initial_state field of the window manager hints to
5365 IconicState. */
5366 x_wm_set_window_state (f, IconicState);
5368 if (!FRAME_VISIBLE_P (f))
5370 /* If the frame was withdrawn, before, we must map it. */
5371 XMapWindow (XDISPLAY FRAME_X_WINDOW (f));
5372 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5373 XMapSubwindows (x_current_display, FRAME_X_WINDOW (f));
5376 f->async_iconified = 1;
5377 #else /* ! defined (HAVE_X11) */
5378 XUnmapWindow (XDISPLAY FRAME_X_WINDOW (f));
5380 f->async_visible = 0; /* Handled in the UnMap event for X11. */
5381 if (f->display.x->icon_desc != 0)
5383 XMapWindow (XDISPLAY f->display.x->icon_desc);
5384 refreshicon (f);
5386 #endif /* ! defined (HAVE_X11) */
5388 XFlushQueue ();
5389 UNBLOCK_INPUT;
5390 #endif /* not USE_X_TOOLKIT */
5393 /* Destroy the X window of frame F. */
5395 x_destroy_window (f)
5396 struct frame *f;
5398 BLOCK_INPUT;
5400 if (f->display.x->icon_desc != 0)
5401 XDestroyWindow (XDISPLAY f->display.x->icon_desc);
5402 XDestroyWindow (XDISPLAY f->display.x->window_desc);
5403 #ifdef USE_X_TOOLKIT
5404 XtDestroyWidget (f->display.x->widget);
5405 free_frame_menubar (f);
5406 #endif /* USE_X_TOOLKIT */
5408 free_frame_faces (f);
5409 XFlushQueue ();
5411 xfree (f->display.x);
5412 f->display.x = 0;
5413 if (f == x_focus_frame)
5414 x_focus_frame = 0;
5415 if (f == x_highlight_frame)
5416 x_highlight_frame = 0;
5418 UNBLOCK_INPUT;
5421 /* Manage event queues for X10. */
5423 #ifndef HAVE_X11
5425 /* Manage event queues.
5427 This code is only used by the X10 support.
5429 We cannot leave events in the X queue and get them when we are ready
5430 because X does not provide a subroutine to get only a certain kind
5431 of event but not block if there are no queued events of that kind.
5433 Therefore, we must examine events as they come in and copy events
5434 of certain kinds into our private queues.
5436 All ExposeRegion events are put in x_expose_queue.
5437 All ButtonPress and ButtonRelease events are put in x_mouse_queue. */
5440 /* Write the event *P_XREP into the event queue *QUEUE.
5441 If the queue is full, do nothing, but return nonzero. */
5444 enqueue_event (p_xrep, queue)
5445 register XEvent *p_xrep;
5446 register struct event_queue *queue;
5448 int newindex = queue->windex + 1;
5449 if (newindex == EVENT_BUFFER_SIZE)
5450 newindex = 0;
5451 if (newindex == queue->rindex)
5452 return -1;
5453 queue->xrep[queue->windex] = *p_xrep;
5454 queue->windex = newindex;
5455 return 0;
5458 /* Fetch the next event from queue *QUEUE and store it in *P_XREP.
5459 If *QUEUE is empty, do nothing and return 0. */
5462 dequeue_event (p_xrep, queue)
5463 register XEvent *p_xrep;
5464 register struct event_queue *queue;
5466 if (queue->windex == queue->rindex)
5467 return 0;
5468 *p_xrep = queue->xrep[queue->rindex++];
5469 if (queue->rindex == EVENT_BUFFER_SIZE)
5470 queue->rindex = 0;
5471 return 1;
5474 /* Return the number of events buffered in *QUEUE. */
5477 queue_event_count (queue)
5478 register struct event_queue *queue;
5480 int tem = queue->windex - queue->rindex;
5481 if (tem >= 0)
5482 return tem;
5483 return EVENT_BUFFER_SIZE + tem;
5486 /* Return nonzero if mouse input is pending. */
5489 mouse_event_pending_p ()
5491 return queue_event_count (&x_mouse_queue);
5493 #endif /* HAVE_X11 */
5495 /* Setting window manager hints. */
5497 #ifdef HAVE_X11
5499 /* Record the gravity used previously, in case CHANGE_GRAVITY is 0. */
5500 static int previous_gravity;
5502 /* SPEC_X and SPEC_Y are the specified positions.
5503 We look only at their sign, to decide the gravity.
5504 If CHANGE_GRAVITY is 0, we ignore SPEC_X and SPEC_Y
5505 and leave the gravity unchanged. */
5507 x_wm_set_size_hint (f, prompting, change_gravity, spec_x, spec_y)
5508 struct frame *f;
5509 long prompting;
5510 int change_gravity;
5511 int spec_x, spec_y;
5513 XSizeHints size_hints;
5515 #ifdef USE_X_TOOLKIT
5516 Arg al[2];
5517 int ac = 0;
5518 Dimension widget_width, widget_height;
5519 Window window = XtWindow (f->display.x->widget);
5520 #else /* not USE_X_TOOLKIT */
5521 Window window = FRAME_X_WINDOW (f);
5522 #endif /* not USE_X_TOOLKIT */
5524 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
5526 flexlines = f->height;
5528 size_hints.x = f->display.x->left_pos;
5529 size_hints.y = f->display.x->top_pos;
5530 #ifdef USE_X_TOOLKIT
5531 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
5532 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
5533 XtGetValues (f->display.x->column_widget, al, ac);
5534 size_hints.height = widget_height;
5535 size_hints.width = widget_width;
5536 #else /* not USE_X_TOOLKIT */
5537 size_hints.height = PIXEL_HEIGHT (f);
5538 size_hints.width = PIXEL_WIDTH (f);
5539 #endif /* not USE_X_TOOLKIT */
5540 size_hints.width_inc = FONT_WIDTH (f->display.x->font);
5541 size_hints.height_inc = FONT_HEIGHT (f->display.x->font);
5542 #if 0
5543 size_hints.max_width = x_screen_width - CHAR_TO_PIXEL_WIDTH (f, 0);
5544 size_hints.max_height = x_screen_height - CHAR_TO_PIXEL_HEIGHT (f, 0);
5545 #endif
5547 int base_width, base_height;
5549 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
5550 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
5553 int min_rows = 0, min_cols = 0;
5554 check_frame_size (f, &min_rows, &min_cols);
5556 /* The window manager uses the base width hints to calculate the
5557 current number of rows and columns in the frame while
5558 resizing; min_width and min_height aren't useful for this
5559 purpose, since they might not give the dimensions for a
5560 zero-row, zero-column frame.
5562 We use the base_width and base_height members if we have
5563 them; otherwise, we set the min_width and min_height members
5564 to the size for a zero x zero frame. */
5566 #ifdef HAVE_X11R4
5567 size_hints.flags |= PBaseSize;
5568 size_hints.base_width = base_width;
5569 size_hints.base_height = base_height;
5570 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
5571 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
5572 #else
5573 size_hints.min_width = base_width;
5574 size_hints.min_height = base_height;
5575 #endif
5580 if (prompting)
5581 size_hints.flags |= prompting;
5582 else
5584 XSizeHints hints; /* Sometimes I hate X Windows... */
5586 if (XGetNormalHints (x_current_display, window, &hints) == 0)
5587 hints.flags = 0;
5588 if (hints.flags & PSize)
5589 size_hints.flags |= PSize;
5590 if (hints.flags & PPosition)
5591 size_hints.flags |= PPosition;
5592 if (hints.flags & USPosition)
5593 size_hints.flags |= USPosition;
5594 if (hints.flags & USSize)
5595 size_hints.flags |= USSize;
5597 #if defined (PWinGravity)
5598 if (change_gravity)
5600 switch (((spec_x < 0) << 1) + (spec_y < 0))
5602 case 0:
5603 size_hints.win_gravity = NorthWestGravity;
5604 break;
5605 case 1:
5606 size_hints.win_gravity = NorthEastGravity;
5607 break;
5608 case 2:
5609 size_hints.win_gravity = SouthWestGravity;
5610 break;
5611 case 3:
5612 size_hints.win_gravity = SouthEastGravity;
5613 break;
5615 previous_gravity = size_hints.win_gravity;
5617 else
5618 size_hints.win_gravity = previous_gravity;
5620 size_hints.flags |= PWinGravity;
5621 #endif /* PWinGravity */
5623 #ifdef HAVE_X11R4
5624 XSetWMNormalHints (x_current_display, window, &size_hints);
5625 #else
5626 XSetNormalHints (x_current_display, window, &size_hints);
5627 #endif
5630 /* Used for IconicState or NormalState */
5631 x_wm_set_window_state (f, state)
5632 struct frame *f;
5633 int state;
5635 #ifdef USE_X_TOOLKIT
5636 Window window = XtWindow (f->display.x->widget);
5637 #else /* not USE_X_TOOLKIT */
5638 Window window = FRAME_X_WINDOW (f);
5639 #endif /* not USE_X_TOOLKIT */
5641 f->display.x->wm_hints.flags |= StateHint;
5642 f->display.x->wm_hints.initial_state = state;
5644 XSetWMHints (x_current_display, window, &f->display.x->wm_hints);
5647 x_wm_set_icon_pixmap (f, icon_pixmap)
5648 struct frame *f;
5649 Pixmap icon_pixmap;
5651 Window window = FRAME_X_WINDOW (f);
5653 if (icon_pixmap)
5655 f->display.x->wm_hints.icon_pixmap = icon_pixmap;
5656 f->display.x->wm_hints.flags |= IconPixmapHint;
5658 else
5659 f->display.x->wm_hints.flags &= ~IconPixmapHint;
5661 XSetWMHints (x_current_display, window, &f->display.x->wm_hints);
5664 x_wm_set_icon_position (f, icon_x, icon_y)
5665 struct frame *f;
5666 int icon_x, icon_y;
5668 Window window = FRAME_X_WINDOW (f);
5670 f->display.x->wm_hints.flags |= IconPositionHint;
5671 f->display.x->wm_hints.icon_x = icon_x;
5672 f->display.x->wm_hints.icon_y = icon_y;
5674 XSetWMHints (x_current_display, window, &f->display.x->wm_hints);
5678 /* Initialization. */
5680 #ifdef USE_X_TOOLKIT
5681 static XrmOptionDescRec emacs_options[] = {
5682 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
5683 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
5685 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
5686 XrmoptionSepArg, NULL},
5687 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
5689 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
5690 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
5691 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
5692 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
5693 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
5694 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
5695 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
5697 #endif /* USE_X_TOOLKIT */
5699 void
5700 x_term_init (display_name)
5701 char *display_name;
5703 Lisp_Object frame;
5704 char *defaultvalue;
5705 int argc = 0;
5706 char** argv = 0;
5707 #ifndef F_SETOWN_BUG
5708 #ifdef F_SETOWN
5709 extern int old_fcntl_owner;
5710 #endif /* ! defined (F_SETOWN) */
5711 #endif /* F_SETOWN_BUG */
5713 x_focus_frame = x_highlight_frame = 0;
5715 #ifdef USE_X_TOOLKIT
5716 argv = (char **) XtMalloc (3 * sizeof (char *));
5717 argv [0] = "";
5718 argv [1] = "-display";
5719 argv [2] = display_name;
5720 argc = 3;
5721 Xt_app_shell = XtAppInitialize (&Xt_app_con, "Emacs",
5722 emacs_options, XtNumber (emacs_options),
5723 &argc, argv,
5724 NULL, NULL, 0);
5725 XtFree (argv);
5726 x_current_display = XtDisplay (Xt_app_shell);
5728 #else /* not USE_X_TOOLKIT */
5729 x_current_display = XOpenDisplay (display_name);
5730 #endif /* not USE_X_TOOLKIT */
5731 if (x_current_display == 0)
5732 fatal ("X server %s not responding.\n\
5733 Check the DISPLAY environment variable or use \"-d\"\n",
5734 display_name);
5736 #ifdef HAVE_X11
5738 #if 0
5739 XSetAfterFunction (x_current_display, x_trace_wire);
5740 #endif /* ! 0 */
5741 hostname = get_system_name ();
5742 x_id_name = (char *) xmalloc (XSTRING (Vinvocation_name)->size
5743 + strlen (hostname)
5744 + 2);
5745 sprintf (x_id_name, "%s@%s", XSTRING (Vinvocation_name)->data, hostname);
5748 /* Figure out which modifier bits mean what. */
5749 x_find_modifier_meanings ();
5751 /* Get the scroll bar cursor. */
5752 x_vertical_scroll_bar_cursor
5753 = XCreateFontCursor (x_current_display, XC_sb_v_double_arrow);
5755 #if 0
5756 /* Watch for PropertyNotify events on the root window; we use them
5757 to figure out when to invalidate our cache of the cut buffers. */
5758 x_watch_cut_buffer_cache ();
5759 #endif
5761 if (ConnectionNumber (x_current_display) != 0)
5762 change_keyboard_wait_descriptor (ConnectionNumber (x_current_display));
5763 change_input_fd (ConnectionNumber (x_current_display));
5765 #endif /* ! defined (HAVE_X11) */
5767 #ifndef F_SETOWN_BUG
5768 #ifdef F_SETOWN
5769 old_fcntl_owner = fcntl (ConnectionNumber (x_current_display), F_GETOWN, 0);
5770 #ifdef F_SETOWN_SOCK_NEG
5771 /* stdin is a socket here */
5772 fcntl (ConnectionNumber (x_current_display), F_SETOWN, -getpid ());
5773 #else /* ! defined (F_SETOWN_SOCK_NEG) */
5774 fcntl (ConnectionNumber (x_current_display), F_SETOWN, getpid ());
5775 #endif /* ! defined (F_SETOWN_SOCK_NEG) */
5776 #endif /* ! defined (F_SETOWN) */
5777 #endif /* F_SETOWN_BUG */
5779 #ifdef SIGIO
5780 init_sigio ();
5781 #endif /* ! defined (SIGIO) */
5783 expose_all_windows = 0;
5785 clear_frame_hook = XTclear_frame;
5786 clear_end_of_line_hook = XTclear_end_of_line;
5787 ins_del_lines_hook = XTins_del_lines;
5788 change_line_highlight_hook = XTchange_line_highlight;
5789 insert_glyphs_hook = XTinsert_glyphs;
5790 write_glyphs_hook = XTwrite_glyphs;
5791 delete_glyphs_hook = XTdelete_glyphs;
5792 ring_bell_hook = XTring_bell;
5793 reset_terminal_modes_hook = XTreset_terminal_modes;
5794 set_terminal_modes_hook = XTset_terminal_modes;
5795 update_begin_hook = XTupdate_begin;
5796 update_end_hook = XTupdate_end;
5797 set_terminal_window_hook = XTset_terminal_window;
5798 read_socket_hook = XTread_socket;
5799 frame_up_to_date_hook = XTframe_up_to_date;
5800 cursor_to_hook = XTcursor_to;
5801 reassert_line_highlight_hook = XTreassert_line_highlight;
5802 mouse_position_hook = XTmouse_position;
5803 frame_rehighlight_hook = XTframe_rehighlight;
5804 frame_raise_lower_hook = XTframe_raise_lower;
5805 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
5806 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
5807 redeem_scroll_bar_hook = XTredeem_scroll_bar;
5808 judge_scroll_bars_hook = XTjudge_scroll_bars;
5810 scroll_region_ok = 1; /* we'll scroll partial frames */
5811 char_ins_del_ok = 0; /* just as fast to write the line */
5812 line_ins_del_ok = 1; /* we'll just blt 'em */
5813 fast_clear_end_of_line = 1; /* X does this well */
5814 memory_below_frame = 0; /* we don't remember what scrolls
5815 off the bottom */
5816 baud_rate = 19200;
5818 /* Try to use interrupt input; if we can't, then start polling. */
5819 Fset_input_mode (Qt, Qnil, Qt, Qnil);
5821 /* Note that there is no real way portable across R3/R4 to get the
5822 original error handler. */
5823 XHandleError (x_error_quitter);
5824 XHandleIOError (x_io_error_quitter);
5826 /* Disable Window Change signals; they are handled by X events. */
5827 #ifdef SIGWINCH
5828 signal (SIGWINCH, SIG_DFL);
5829 #endif /* ! defined (SIGWINCH) */
5831 signal (SIGPIPE, x_connection_closed);
5834 void
5835 syms_of_xterm ()
5837 staticpro (&last_mouse_scroll_bar);
5838 last_mouse_scroll_bar = Qnil;
5839 staticpro (&mouse_face_window);
5840 mouse_face_window = Qnil;
5842 #endif /* ! defined (HAVE_X11) */
5843 #endif /* ! defined (HAVE_X_WINDOWS) */