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