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