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