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