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