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