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