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