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