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