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