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