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