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