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