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