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