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