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