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