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