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