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