* xfns.c (syms_of_xfns): Use make_pure_string instead of build_string.
[bpt/emacs.git] / src / xterm.c
1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1989, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
3 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4 Free Software Foundation, Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20
21 /* New display code by Gerd Moellmann <gerd@gnu.org>. */
22 /* Xt features made by Fred Pierresteguy. */
23
24 #include <config.h>
25
26 /* On 4.3 these lose if they come after xterm.h. */
27 /* Putting these at the beginning seems to be standard for other .c files. */
28 #include <signal.h>
29
30 #include <stdio.h>
31 #include <setjmp.h>
32
33 #ifdef HAVE_X_WINDOWS
34
35 #include "lisp.h"
36 #include "blockinput.h"
37
38 /* Need syssignal.h for various externs and definitions that may be required
39 by some configurations for calls to signal later in this source file. */
40 #include "syssignal.h"
41
42 /* This may include sys/types.h, and that somehow loses
43 if this is not done before the other system files. */
44 #include "xterm.h"
45 #include <X11/cursorfont.h>
46
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
53 #ifdef BSD_SYSTEM
54 #include <sys/ioctl.h>
55 #endif /* ! defined (BSD_SYSTEM) */
56
57 #include "systime.h"
58
59 #ifndef INCLUDED_FCNTL
60 #include <fcntl.h>
61 #endif
62 #include <ctype.h>
63 #include <errno.h>
64 #include <setjmp.h>
65 #include <sys/stat.h>
66 /* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
67 /* #include <sys/param.h> */
68
69 #include "charset.h"
70 #include "character.h"
71 #include "coding.h"
72 #include "frame.h"
73 #include "dispextern.h"
74 #include "fontset.h"
75 #include "termhooks.h"
76 #include "termopts.h"
77 #include "termchar.h"
78 #include "emacs-icon.h"
79 #include "disptab.h"
80 #include "buffer.h"
81 #include "window.h"
82 #include "keyboard.h"
83 #include "intervals.h"
84 #include "process.h"
85 #include "atimer.h"
86 #include "keymap.h"
87 #include "font.h"
88 #include "fontset.h"
89 #include "sysselect.h"
90
91 #ifdef USE_X_TOOLKIT
92 #include <X11/Shell.h>
93 #endif
94
95 #ifdef HAVE_SYS_TIME_H
96 #include <sys/time.h>
97 #endif
98 #ifdef HAVE_UNISTD_H
99 #include <unistd.h>
100 #endif
101
102 #ifdef USE_GTK
103 #include "gtkutil.h"
104 #endif
105
106 #ifdef USE_LUCID
107 extern int xlwmenu_window_p P_ ((Widget w, Window window));
108 extern void xlwmenu_redisplay P_ ((Widget));
109 #endif
110
111 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
112
113 extern void free_frame_menubar P_ ((struct frame *));
114 #endif
115
116 #ifdef USE_X_TOOLKIT
117 #if !defined(NO_EDITRES)
118 #define HACK_EDITRES
119 extern void _XEditResCheckMessages ();
120 #endif /* not NO_EDITRES */
121
122 /* Include toolkit specific headers for the scroll bar widget. */
123
124 #ifdef USE_TOOLKIT_SCROLL_BARS
125 #if defined USE_MOTIF
126 #include <Xm/Xm.h> /* for LESSTIF_VERSION */
127 #include <Xm/ScrollBar.h>
128 #else /* !USE_MOTIF i.e. use Xaw */
129
130 #ifdef HAVE_XAW3D
131 #include <X11/Xaw3d/Simple.h>
132 #include <X11/Xaw3d/Scrollbar.h>
133 #include <X11/Xaw3d/ThreeD.h>
134 #else /* !HAVE_XAW3D */
135 #include <X11/Xaw/Simple.h>
136 #include <X11/Xaw/Scrollbar.h>
137 #endif /* !HAVE_XAW3D */
138 #ifndef XtNpickTop
139 #define XtNpickTop "pickTop"
140 #endif /* !XtNpickTop */
141 #endif /* !USE_MOTIF */
142 #endif /* USE_TOOLKIT_SCROLL_BARS */
143
144 #endif /* USE_X_TOOLKIT */
145
146 #ifdef USE_X_TOOLKIT
147 #include "widget.h"
148 #ifndef XtNinitialState
149 #define XtNinitialState "initialState"
150 #endif
151 #endif
152
153 /* Default to using XIM if available. */
154 #ifdef USE_XIM
155 int use_xim = 1;
156 #else
157 int use_xim = 0; /* configure --without-xim */
158 #endif
159
160 \f
161
162 /* Non-nil means Emacs uses toolkit scroll bars. */
163
164 Lisp_Object Vx_toolkit_scroll_bars;
165
166 /* Non-zero means that a HELP_EVENT has been generated since Emacs
167 start. */
168
169 static int any_help_event_p;
170
171 /* Last window where we saw the mouse. Used by mouse-autoselect-window. */
172 static Lisp_Object last_window;
173
174 /* Non-zero means make use of UNDERLINE_POSITION font properties. */
175
176 int x_use_underline_position_properties;
177
178 /* Non-zero means to draw the underline at the same place as the descent line. */
179
180 int x_underline_at_descent_line;
181
182 /* This is a chain of structures for all the X displays currently in
183 use. */
184
185 struct x_display_info *x_display_list;
186
187 /* This is a list of cons cells, each of the form (NAME
188 . FONT-LIST-CACHE), one for each element of x_display_list and in
189 the same order. NAME is the name of the frame. FONT-LIST-CACHE
190 records previous values returned by x-list-fonts. */
191
192 Lisp_Object x_display_name_list;
193
194 /* Frame being updated by update_frame. This is declared in term.c.
195 This is set by update_begin and looked at by all the XT functions.
196 It is zero while not inside an update. In that case, the XT
197 functions assume that `selected_frame' is the frame to apply to. */
198
199 extern struct frame *updating_frame;
200
201 /* This is a frame waiting to be auto-raised, within XTread_socket. */
202
203 static struct frame *pending_autoraise_frame;
204
205 /* This is a frame waiting for an event matching mask, within XTread_socket. */
206
207 static struct {
208 struct frame *f;
209 int eventtype;
210 } pending_event_wait;
211
212 #ifdef USE_X_TOOLKIT
213 /* The application context for Xt use. */
214 XtAppContext Xt_app_con;
215 static String Xt_default_resources[] = {0};
216 #endif /* USE_X_TOOLKIT */
217
218 /* Non-zero means user is interacting with a toolkit scroll bar. */
219
220 static int toolkit_scroll_bar_interaction;
221
222 /* Non-zero means to not move point as a result of clicking on a
223 frame to focus it (when focus-follows-mouse is nil). */
224
225 int x_mouse_click_focus_ignore_position;
226
227 /* Non-zero timeout value means ignore next mouse click if it arrives
228 before that timeout elapses (i.e. as part of the same sequence of
229 events resulting from clicking on a frame to select it). */
230
231 static unsigned long ignore_next_mouse_click_timeout;
232
233 /* Mouse movement.
234
235 Formerly, we used PointerMotionHintMask (in standard_event_mask)
236 so that we would have to call XQueryPointer after each MotionNotify
237 event to ask for another such event. However, this made mouse tracking
238 slow, and there was a bug that made it eventually stop.
239
240 Simply asking for MotionNotify all the time seems to work better.
241
242 In order to avoid asking for motion events and then throwing most
243 of them away or busy-polling the server for mouse positions, we ask
244 the server for pointer motion hints. This means that we get only
245 one event per group of mouse movements. "Groups" are delimited by
246 other kinds of events (focus changes and button clicks, for
247 example), or by XQueryPointer calls; when one of these happens, we
248 get another MotionNotify event the next time the mouse moves. This
249 is at least as efficient as getting motion events when mouse
250 tracking is on, and I suspect only negligibly worse when tracking
251 is off. */
252
253 /* Where the mouse was last time we reported a mouse event. */
254
255 static XRectangle last_mouse_glyph;
256 static FRAME_PTR last_mouse_glyph_frame;
257 static Lisp_Object last_mouse_press_frame;
258
259 /* The scroll bar in which the last X motion event occurred.
260
261 If the last X motion event occurred in a scroll bar, we set this so
262 XTmouse_position can know whether to report a scroll bar motion or
263 an ordinary motion.
264
265 If the last X motion event didn't occur in a scroll bar, we set
266 this to Qnil, to tell XTmouse_position to return an ordinary motion
267 event. */
268
269 static Lisp_Object last_mouse_scroll_bar;
270
271 /* This is a hack. We would really prefer that XTmouse_position would
272 return the time associated with the position it returns, but there
273 doesn't seem to be any way to wrest the time-stamp from the server
274 along with the position query. So, we just keep track of the time
275 of the last movement we received, and return that in hopes that
276 it's somewhat accurate. */
277
278 static Time last_mouse_movement_time;
279
280 /* Time for last user interaction as returned in X events. */
281
282 static Time last_user_time;
283
284 /* Incremented by XTread_socket whenever it really tries to read
285 events. */
286
287 #ifdef __STDC__
288 static int volatile input_signal_count;
289 #else
290 static int input_signal_count;
291 #endif
292
293 /* Used locally within XTread_socket. */
294
295 static int x_noop_count;
296
297 /* Initial values of argv and argc. */
298
299 extern char **initial_argv;
300 extern int initial_argc;
301
302 extern Lisp_Object Vcommand_line_args, Vsystem_name;
303
304 /* Tells if a window manager is present or not. */
305
306 extern Lisp_Object Vx_no_window_manager;
307
308 extern Lisp_Object Qeql;
309
310 extern int errno;
311
312 /* A mask of extra modifier bits to put into every keyboard char. */
313
314 extern EMACS_INT extra_keyboard_modifiers;
315
316 /* The keysyms to use for the various modifiers. */
317
318 Lisp_Object Vx_alt_keysym, Vx_hyper_keysym, Vx_meta_keysym, Vx_super_keysym;
319 Lisp_Object Vx_keysym_table;
320 static Lisp_Object Qalt, Qhyper, Qmeta, Qsuper, Qmodifier_value;
321
322 static Lisp_Object Qvendor_specific_keysyms;
323 static Lisp_Object Qlatin_1;
324
325 #ifdef USE_GTK
326 /* The name of the Emacs icon file. */
327 static Lisp_Object xg_default_icon_file;
328
329 /* Used in gtkutil.c. */
330 Lisp_Object Qx_gtk_map_stock;
331 #endif
332
333 /* Used in x_flush. */
334
335 extern Lisp_Object Vinhibit_redisplay;
336
337 extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
338 extern int x_bitmap_mask P_ ((FRAME_PTR, int));
339
340 static int x_alloc_nearest_color_1 P_ ((Display *, Colormap, XColor *));
341 static void x_set_window_size_1 P_ ((struct frame *, int, int, int));
342 static const XColor *x_color_cells P_ ((Display *, int *));
343 static void x_update_window_end P_ ((struct window *, int, int));
344
345 static int x_io_error_quitter P_ ((Display *));
346 static struct terminal *x_create_terminal P_ ((struct x_display_info *));
347 void x_delete_terminal P_ ((struct terminal *));
348 static void x_update_end P_ ((struct frame *));
349 static void XTframe_up_to_date P_ ((struct frame *));
350 static void XTset_terminal_modes P_ ((struct terminal *));
351 static void XTreset_terminal_modes P_ ((struct terminal *));
352 static void x_clear_frame P_ ((struct frame *));
353 static void frame_highlight P_ ((struct frame *));
354 static void frame_unhighlight P_ ((struct frame *));
355 static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
356 static void x_focus_changed P_ ((int, int, struct x_display_info *,
357 struct frame *, struct input_event *));
358 static void x_detect_focus_change P_ ((struct x_display_info *,
359 XEvent *, struct input_event *));
360 static void XTframe_rehighlight P_ ((struct frame *));
361 static void x_frame_rehighlight P_ ((struct x_display_info *));
362 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
363 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int,
364 enum text_cursor_kinds));
365
366 static void x_clip_to_row P_ ((struct window *, struct glyph_row *, int, GC));
367 static void x_flush P_ ((struct frame *f));
368 static void x_update_begin P_ ((struct frame *));
369 static void x_update_window_begin P_ ((struct window *));
370 static void x_after_update_window_line P_ ((struct glyph_row *));
371 static struct scroll_bar *x_window_to_scroll_bar P_ ((Display *, Window));
372 static void x_scroll_bar_report_motion P_ ((struct frame **, Lisp_Object *,
373 enum scroll_bar_part *,
374 Lisp_Object *, Lisp_Object *,
375 unsigned long *));
376 static void x_handle_net_wm_state P_ ((struct frame *, XPropertyEvent *));
377 static void x_check_fullscreen P_ ((struct frame *));
378 static void x_check_expected_move P_ ((struct frame *, int, int));
379 static void x_sync_with_move P_ ((struct frame *, int, int, int));
380 static int handle_one_xevent P_ ((struct x_display_info *, XEvent *,
381 int *, struct input_event *));
382 /* Don't declare this NO_RETURN because we want no
383 interference with debugging failing X calls. */
384 static SIGTYPE x_connection_closed P_ ((Display *, char *));
385
386
387 /* Flush display of frame F, or of all frames if F is null. */
388
389 static void
390 x_flush (f)
391 struct frame *f;
392 {
393 /* Don't call XFlush when it is not safe to redisplay; the X
394 connection may be broken. */
395 if (!NILP (Vinhibit_redisplay))
396 return;
397
398 BLOCK_INPUT;
399 if (f == NULL)
400 {
401 Lisp_Object rest, frame;
402 FOR_EACH_FRAME (rest, frame)
403 if (FRAME_X_P (XFRAME (frame)))
404 x_flush (XFRAME (frame));
405 }
406 else if (FRAME_X_P (f))
407 XFlush (FRAME_X_DISPLAY (f));
408 UNBLOCK_INPUT;
409 }
410
411
412 /* Remove calls to XFlush by defining XFlush to an empty replacement.
413 Calls to XFlush should be unnecessary because the X output buffer
414 is flushed automatically as needed by calls to XPending,
415 XNextEvent, or XWindowEvent according to the XFlush man page.
416 XTread_socket calls XPending. Removing XFlush improves
417 performance. */
418
419 #define XFlush(DISPLAY) (void) 0
420
421 \f
422 /***********************************************************************
423 Debugging
424 ***********************************************************************/
425
426 #if 0
427
428 /* This is a function useful for recording debugging information about
429 the sequence of occurrences in this file. */
430
431 struct record
432 {
433 char *locus;
434 int type;
435 };
436
437 struct record event_record[100];
438
439 int event_record_index;
440
441 record_event (locus, type)
442 char *locus;
443 int type;
444 {
445 if (event_record_index == sizeof (event_record) / sizeof (struct record))
446 event_record_index = 0;
447
448 event_record[event_record_index].locus = locus;
449 event_record[event_record_index].type = type;
450 event_record_index++;
451 }
452
453 #endif /* 0 */
454
455
456 \f
457 /* Return the struct x_display_info corresponding to DPY. */
458
459 struct x_display_info *
460 x_display_info_for_display (dpy)
461 Display *dpy;
462 {
463 struct x_display_info *dpyinfo;
464
465 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
466 if (dpyinfo->display == dpy)
467 return dpyinfo;
468
469 return 0;
470 }
471
472 #define OPAQUE 0xffffffff
473 #define OPACITY "_NET_WM_WINDOW_OPACITY"
474
475 void
476 x_set_frame_alpha (f)
477 struct frame *f;
478 {
479 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
480 Display *dpy = FRAME_X_DISPLAY (f);
481 Window win = FRAME_OUTER_WINDOW (f);
482 double alpha = 1.0;
483 double alpha_min = 1.0;
484 unsigned long opac;
485
486 if (FRAME_X_DISPLAY_INFO (f)->root_window != FRAME_X_OUTPUT (f)->parent_desc)
487 /* Since the WM decoration lies under the FRAME_OUTER_WINDOW,
488 we must treat the former instead of the latter. */
489 win = FRAME_X_OUTPUT(f)->parent_desc;
490
491 if (dpyinfo->x_highlight_frame == f)
492 alpha = f->alpha[0];
493 else
494 alpha = f->alpha[1];
495
496 if (FLOATP (Vframe_alpha_lower_limit))
497 alpha_min = XFLOAT_DATA (Vframe_alpha_lower_limit);
498 else if (INTEGERP (Vframe_alpha_lower_limit))
499 alpha_min = (XINT (Vframe_alpha_lower_limit)) / 100.0;
500
501 if (alpha < 0.0)
502 return;
503 else if (alpha > 1.0)
504 alpha = 1.0;
505 else if (0.0 <= alpha && alpha < alpha_min && alpha_min <= 1.0)
506 alpha = alpha_min;
507
508 opac = alpha * OPAQUE;
509
510 /* return unless necessary */
511 {
512 unsigned char *data;
513 Atom actual;
514 int rc, format;
515 unsigned long n, left;
516
517 x_catch_errors (dpy);
518 rc = XGetWindowProperty(dpy, win, XInternAtom(dpy, OPACITY, False),
519 0L, 1L, False, XA_CARDINAL,
520 &actual, &format, &n, &left,
521 &data);
522
523 if (rc == Success && actual != None)
524 if (*(unsigned long *)data == opac)
525 {
526 XFree ((void *) data);
527 x_uncatch_errors ();
528 return;
529 }
530 else
531 XFree ((void *) data);
532 x_uncatch_errors ();
533 }
534
535 x_catch_errors (dpy);
536 XChangeProperty (dpy, win, XInternAtom (dpy, OPACITY, False),
537 XA_CARDINAL, 32, PropModeReplace,
538 (unsigned char *) &opac, 1L);
539 x_uncatch_errors ();
540 }
541
542 int
543 x_display_pixel_height (dpyinfo)
544 struct x_display_info *dpyinfo;
545 {
546 return HeightOfScreen (dpyinfo->screen);
547 }
548
549 int
550 x_display_pixel_width (dpyinfo)
551 struct x_display_info *dpyinfo;
552 {
553 return WidthOfScreen (dpyinfo->screen);
554 }
555
556 \f
557 /***********************************************************************
558 Starting and ending an update
559 ***********************************************************************/
560
561 /* Start an update of frame F. This function is installed as a hook
562 for update_begin, i.e. it is called when update_begin is called.
563 This function is called prior to calls to x_update_window_begin for
564 each window being updated. Currently, there is nothing to do here
565 because all interesting stuff is done on a window basis. */
566
567 static void
568 x_update_begin (f)
569 struct frame *f;
570 {
571 /* Nothing to do. */
572 }
573
574
575 /* Start update of window W. Set the global variable updated_window
576 to the window being updated and set output_cursor to the cursor
577 position of W. */
578
579 static void
580 x_update_window_begin (w)
581 struct window *w;
582 {
583 struct frame *f = XFRAME (WINDOW_FRAME (w));
584 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
585
586 updated_window = w;
587 set_output_cursor (&w->cursor);
588
589 BLOCK_INPUT;
590
591 if (f == display_info->mouse_face_mouse_frame)
592 {
593 /* Don't do highlighting for mouse motion during the update. */
594 display_info->mouse_face_defer = 1;
595
596 /* If F needs to be redrawn, simply forget about any prior mouse
597 highlighting. */
598 if (FRAME_GARBAGED_P (f))
599 display_info->mouse_face_window = Qnil;
600 }
601
602 UNBLOCK_INPUT;
603 }
604
605
606 /* Draw a vertical window border from (x,y0) to (x,y1) */
607
608 static void
609 x_draw_vertical_window_border (w, x, y0, y1)
610 struct window *w;
611 int x, y0, y1;
612 {
613 struct frame *f = XFRAME (WINDOW_FRAME (w));
614 struct face *face;
615
616 face = FACE_FROM_ID (f, VERTICAL_BORDER_FACE_ID);
617 if (face)
618 XSetForeground (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
619 face->foreground);
620
621 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
622 f->output_data.x->normal_gc, x, y0, x, y1);
623 }
624
625 /* End update of window W (which is equal to updated_window).
626
627 Draw vertical borders between horizontally adjacent windows, and
628 display W's cursor if CURSOR_ON_P is non-zero.
629
630 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
631 glyphs in mouse-face were overwritten. In that case we have to
632 make sure that the mouse-highlight is properly redrawn.
633
634 W may be a menu bar pseudo-window in case we don't have X toolkit
635 support. Such windows don't have a cursor, so don't display it
636 here. */
637
638 static void
639 x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
640 struct window *w;
641 int cursor_on_p, mouse_face_overwritten_p;
642 {
643 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
644
645 if (!w->pseudo_window_p)
646 {
647 BLOCK_INPUT;
648
649 if (cursor_on_p)
650 display_and_set_cursor (w, 1, output_cursor.hpos,
651 output_cursor.vpos,
652 output_cursor.x, output_cursor.y);
653
654 if (draw_window_fringes (w, 1))
655 x_draw_vertical_border (w);
656
657 UNBLOCK_INPUT;
658 }
659
660 /* If a row with mouse-face was overwritten, arrange for
661 XTframe_up_to_date to redisplay the mouse highlight. */
662 if (mouse_face_overwritten_p)
663 {
664 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
665 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
666 dpyinfo->mouse_face_window = Qnil;
667 }
668
669 updated_window = NULL;
670 }
671
672
673 /* End update of frame F. This function is installed as a hook in
674 update_end. */
675
676 static void
677 x_update_end (f)
678 struct frame *f;
679 {
680 /* Mouse highlight may be displayed again. */
681 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
682
683 #ifndef XFlush
684 BLOCK_INPUT;
685 XFlush (FRAME_X_DISPLAY (f));
686 UNBLOCK_INPUT;
687 #endif
688 }
689
690
691 /* This function is called from various places in xdisp.c whenever a
692 complete update has been performed. The global variable
693 updated_window is not available here. */
694
695 static void
696 XTframe_up_to_date (f)
697 struct frame *f;
698 {
699 if (FRAME_X_P (f))
700 {
701 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
702
703 if (dpyinfo->mouse_face_deferred_gc
704 || f == dpyinfo->mouse_face_mouse_frame)
705 {
706 BLOCK_INPUT;
707 if (dpyinfo->mouse_face_mouse_frame)
708 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
709 dpyinfo->mouse_face_mouse_x,
710 dpyinfo->mouse_face_mouse_y);
711 dpyinfo->mouse_face_deferred_gc = 0;
712 UNBLOCK_INPUT;
713 }
714 }
715 }
716
717
718 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
719 arrow bitmaps, or clear the fringes if no bitmaps are required
720 before DESIRED_ROW is made current. The window being updated is
721 found in updated_window. This function It is called from
722 update_window_line only if it is known that there are differences
723 between bitmaps to be drawn between current row and DESIRED_ROW. */
724
725 static void
726 x_after_update_window_line (desired_row)
727 struct glyph_row *desired_row;
728 {
729 struct window *w = updated_window;
730 struct frame *f;
731 int width, height;
732
733 xassert (w);
734
735 if (!desired_row->mode_line_p && !w->pseudo_window_p)
736 desired_row->redraw_fringe_bitmaps_p = 1;
737
738 /* When a window has disappeared, make sure that no rest of
739 full-width rows stays visible in the internal border. Could
740 check here if updated_window is the leftmost/rightmost window,
741 but I guess it's not worth doing since vertically split windows
742 are almost never used, internal border is rarely set, and the
743 overhead is very small. */
744 if (windows_or_buffers_changed
745 && desired_row->full_width_p
746 && (f = XFRAME (w->frame),
747 width = FRAME_INTERNAL_BORDER_WIDTH (f),
748 width != 0)
749 && (height = desired_row->visible_height,
750 height > 0))
751 {
752 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
753
754 /* Internal border is drawn below the tool bar. */
755 if (WINDOWP (f->tool_bar_window)
756 && w == XWINDOW (f->tool_bar_window))
757 y -= width;
758
759 BLOCK_INPUT;
760 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
761 0, y, width, height, False);
762 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
763 FRAME_PIXEL_WIDTH (f) - width,
764 y, width, height, False);
765 UNBLOCK_INPUT;
766 }
767 }
768
769 static void
770 x_draw_fringe_bitmap (w, row, p)
771 struct window *w;
772 struct glyph_row *row;
773 struct draw_fringe_bitmap_params *p;
774 {
775 struct frame *f = XFRAME (WINDOW_FRAME (w));
776 Display *display = FRAME_X_DISPLAY (f);
777 Window window = FRAME_X_WINDOW (f);
778 GC gc = f->output_data.x->normal_gc;
779 struct face *face = p->face;
780 int rowY;
781
782 /* Must clip because of partially visible lines. */
783 rowY = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
784 if (p->y < rowY)
785 {
786 /* Adjust position of "bottom aligned" bitmap on partially
787 visible last row. */
788 int oldY = row->y;
789 int oldVH = row->visible_height;
790 row->visible_height = p->h;
791 row->y -= rowY - p->y;
792 x_clip_to_row (w, row, -1, gc);
793 row->y = oldY;
794 row->visible_height = oldVH;
795 }
796 else
797 x_clip_to_row (w, row, -1, gc);
798
799 if (!p->overlay_p)
800 {
801 int bx = p->bx, by = p->by, nx = p->nx, ny = p->ny;
802
803 /* In case the same realized face is used for fringes and
804 for something displayed in the text (e.g. face `region' on
805 mono-displays, the fill style may have been changed to
806 FillSolid in x_draw_glyph_string_background. */
807 if (face->stipple)
808 XSetFillStyle (display, face->gc, FillOpaqueStippled);
809 else
810 XSetForeground (display, face->gc, face->background);
811
812 #ifdef USE_TOOLKIT_SCROLL_BARS
813 /* If the fringe is adjacent to the left (right) scroll bar of a
814 leftmost (rightmost, respectively) window, then extend its
815 background to the gap between the fringe and the bar. */
816 if ((WINDOW_LEFTMOST_P (w)
817 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
818 || (WINDOW_RIGHTMOST_P (w)
819 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w)))
820 {
821 int sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
822
823 if (sb_width > 0)
824 {
825 int left = WINDOW_SCROLL_BAR_AREA_X (w);
826 int width = (WINDOW_CONFIG_SCROLL_BAR_COLS (w)
827 * FRAME_COLUMN_WIDTH (f));
828
829 if (bx < 0)
830 {
831 /* Bitmap fills the fringe. */
832 if (left + width == p->x)
833 bx = left + sb_width;
834 else if (p->x + p->wd == left)
835 bx = left;
836 if (bx >= 0)
837 {
838 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
839
840 nx = width - sb_width;
841 by = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
842 row->y));
843 ny = row->visible_height;
844 }
845 }
846 else
847 {
848 if (left + width == bx)
849 {
850 bx = left + sb_width;
851 nx += width - sb_width;
852 }
853 else if (bx + nx == left)
854 nx += width - sb_width;
855 }
856 }
857 }
858 #endif
859 if (bx >= 0 && nx > 0)
860 XFillRectangle (display, window, face->gc, bx, by, nx, ny);
861
862 if (!face->stipple)
863 XSetForeground (display, face->gc, face->foreground);
864 }
865
866 if (p->which)
867 {
868 unsigned char *bits;
869 Pixmap pixmap, clipmask = (Pixmap) 0;
870 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
871 XGCValues gcv;
872
873 if (p->wd > 8)
874 bits = (unsigned char *)(p->bits + p->dh);
875 else
876 bits = (unsigned char *)p->bits + p->dh;
877
878 /* Draw the bitmap. I believe these small pixmaps can be cached
879 by the server. */
880 pixmap = XCreatePixmapFromBitmapData (display, window, bits, p->wd, p->h,
881 (p->cursor_p
882 ? (p->overlay_p ? face->background
883 : f->output_data.x->cursor_pixel)
884 : face->foreground),
885 face->background, depth);
886
887 if (p->overlay_p)
888 {
889 clipmask = XCreatePixmapFromBitmapData (display,
890 FRAME_X_DISPLAY_INFO (f)->root_window,
891 bits, p->wd, p->h,
892 1, 0, 1);
893 gcv.clip_mask = clipmask;
894 gcv.clip_x_origin = p->x;
895 gcv.clip_y_origin = p->y;
896 XChangeGC (display, gc, GCClipMask | GCClipXOrigin | GCClipYOrigin, &gcv);
897 }
898
899 XCopyArea (display, pixmap, window, gc, 0, 0,
900 p->wd, p->h, p->x, p->y);
901 XFreePixmap (display, pixmap);
902
903 if (p->overlay_p)
904 {
905 gcv.clip_mask = (Pixmap) 0;
906 XChangeGC (display, gc, GCClipMask, &gcv);
907 XFreePixmap (display, clipmask);
908 }
909 }
910
911 XSetClipMask (display, gc, None);
912 }
913
914 \f
915
916 /* This is called when starting Emacs and when restarting after
917 suspend. When starting Emacs, no X window is mapped. And nothing
918 must be done to Emacs's own window if it is suspended (though that
919 rarely happens). */
920
921 static void
922 XTset_terminal_modes (struct terminal *terminal)
923 {
924 }
925
926 /* This is called when exiting or suspending Emacs. Exiting will make
927 the X-windows go away, and suspending requires no action. */
928
929 static void
930 XTreset_terminal_modes (struct terminal *terminal)
931 {
932 }
933
934 \f
935 /***********************************************************************
936 Glyph display
937 ***********************************************************************/
938
939
940
941 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
942 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
943 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
944 int));
945 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
946 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
947 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
948 static void x_draw_glyph_string P_ ((struct glyph_string *));
949 static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
950 static void x_set_cursor_gc P_ ((struct glyph_string *));
951 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
952 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
953 static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
954 unsigned long *, double, int));
955 static void x_setup_relief_color P_ ((struct frame *, struct relief *,
956 double, int, unsigned long));
957 static void x_setup_relief_colors P_ ((struct glyph_string *));
958 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
959 static void x_draw_image_relief P_ ((struct glyph_string *));
960 static void x_draw_image_foreground P_ ((struct glyph_string *));
961 static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
962 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
963 int, int, int));
964 static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
965 int, int, int, int, int, int,
966 XRectangle *));
967 static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
968 int, int, int, XRectangle *));
969
970 #if GLYPH_DEBUG
971 static void x_check_font P_ ((struct frame *, struct font *));
972 #endif
973
974
975 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
976 face. */
977
978 static void
979 x_set_cursor_gc (s)
980 struct glyph_string *s;
981 {
982 if (s->font == FRAME_FONT (s->f)
983 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
984 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
985 && !s->cmp)
986 s->gc = s->f->output_data.x->cursor_gc;
987 else
988 {
989 /* Cursor on non-default face: must merge. */
990 XGCValues xgcv;
991 unsigned long mask;
992
993 xgcv.background = s->f->output_data.x->cursor_pixel;
994 xgcv.foreground = s->face->background;
995
996 /* If the glyph would be invisible, try a different foreground. */
997 if (xgcv.foreground == xgcv.background)
998 xgcv.foreground = s->face->foreground;
999 if (xgcv.foreground == xgcv.background)
1000 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
1001 if (xgcv.foreground == xgcv.background)
1002 xgcv.foreground = s->face->foreground;
1003
1004 /* Make sure the cursor is distinct from text in this face. */
1005 if (xgcv.background == s->face->background
1006 && xgcv.foreground == s->face->foreground)
1007 {
1008 xgcv.background = s->face->foreground;
1009 xgcv.foreground = s->face->background;
1010 }
1011
1012 IF_DEBUG (x_check_font (s->f, s->font));
1013 xgcv.graphics_exposures = False;
1014 mask = GCForeground | GCBackground | GCGraphicsExposures;
1015
1016 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1017 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1018 mask, &xgcv);
1019 else
1020 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
1021 = XCreateGC (s->display, s->window, mask, &xgcv);
1022
1023 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1024 }
1025 }
1026
1027
1028 /* Set up S->gc of glyph string S for drawing text in mouse face. */
1029
1030 static void
1031 x_set_mouse_face_gc (s)
1032 struct glyph_string *s;
1033 {
1034 int face_id;
1035 struct face *face;
1036
1037 /* What face has to be used last for the mouse face? */
1038 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
1039 face = FACE_FROM_ID (s->f, face_id);
1040 if (face == NULL)
1041 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
1042
1043 if (s->first_glyph->type == CHAR_GLYPH)
1044 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch, -1, Qnil);
1045 else
1046 face_id = FACE_FOR_CHAR (s->f, face, 0, -1, Qnil);
1047 s->face = FACE_FROM_ID (s->f, face_id);
1048 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1049
1050 if (s->font == s->face->font)
1051 s->gc = s->face->gc;
1052 else
1053 {
1054 /* Otherwise construct scratch_cursor_gc with values from FACE
1055 except for FONT. */
1056 XGCValues xgcv;
1057 unsigned long mask;
1058
1059 xgcv.background = s->face->background;
1060 xgcv.foreground = s->face->foreground;
1061 xgcv.graphics_exposures = False;
1062 mask = GCForeground | GCBackground | GCGraphicsExposures;
1063
1064 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1065 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1066 mask, &xgcv);
1067 else
1068 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
1069 = XCreateGC (s->display, s->window, mask, &xgcv);
1070
1071 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1072
1073 }
1074 xassert (s->gc != 0);
1075 }
1076
1077
1078 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
1079 Faces to use in the mode line have already been computed when the
1080 matrix was built, so there isn't much to do, here. */
1081
1082 static INLINE void
1083 x_set_mode_line_face_gc (s)
1084 struct glyph_string *s;
1085 {
1086 s->gc = s->face->gc;
1087 }
1088
1089
1090 /* Set S->gc of glyph string S for drawing that glyph string. Set
1091 S->stippled_p to a non-zero value if the face of S has a stipple
1092 pattern. */
1093
1094 static INLINE void
1095 x_set_glyph_string_gc (s)
1096 struct glyph_string *s;
1097 {
1098 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1099
1100 if (s->hl == DRAW_NORMAL_TEXT)
1101 {
1102 s->gc = s->face->gc;
1103 s->stippled_p = s->face->stipple != 0;
1104 }
1105 else if (s->hl == DRAW_INVERSE_VIDEO)
1106 {
1107 x_set_mode_line_face_gc (s);
1108 s->stippled_p = s->face->stipple != 0;
1109 }
1110 else if (s->hl == DRAW_CURSOR)
1111 {
1112 x_set_cursor_gc (s);
1113 s->stippled_p = 0;
1114 }
1115 else if (s->hl == DRAW_MOUSE_FACE)
1116 {
1117 x_set_mouse_face_gc (s);
1118 s->stippled_p = s->face->stipple != 0;
1119 }
1120 else if (s->hl == DRAW_IMAGE_RAISED
1121 || s->hl == DRAW_IMAGE_SUNKEN)
1122 {
1123 s->gc = s->face->gc;
1124 s->stippled_p = s->face->stipple != 0;
1125 }
1126 else
1127 {
1128 s->gc = s->face->gc;
1129 s->stippled_p = s->face->stipple != 0;
1130 }
1131
1132 /* GC must have been set. */
1133 xassert (s->gc != 0);
1134 }
1135
1136
1137 /* Set clipping for output of glyph string S. S may be part of a mode
1138 line or menu if we don't have X toolkit support. */
1139
1140 static INLINE void
1141 x_set_glyph_string_clipping (s)
1142 struct glyph_string *s;
1143 {
1144 XRectangle *r = s->clip;
1145 int n = get_glyph_string_clip_rects (s, r, 2);
1146
1147 if (n > 0)
1148 XSetClipRectangles (s->display, s->gc, 0, 0, r, n, Unsorted);
1149 s->num_clips = n;
1150 }
1151
1152
1153 /* Set SRC's clipping for output of glyph string DST. This is called
1154 when we are drawing DST's left_overhang or right_overhang only in
1155 the area of SRC. */
1156
1157 static void
1158 x_set_glyph_string_clipping_exactly (src, dst)
1159 struct glyph_string *src, *dst;
1160 {
1161 XRectangle r;
1162
1163 r.x = src->x;
1164 r.width = src->width;
1165 r.y = src->y;
1166 r.height = src->height;
1167 dst->clip[0] = r;
1168 dst->num_clips = 1;
1169 XSetClipRectangles (dst->display, dst->gc, 0, 0, &r, 1, Unsorted);
1170 }
1171
1172
1173 /* RIF:
1174 Compute left and right overhang of glyph string S. */
1175
1176 static void
1177 x_compute_glyph_string_overhangs (s)
1178 struct glyph_string *s;
1179 {
1180 if (s->cmp == NULL
1181 && (s->first_glyph->type == CHAR_GLYPH
1182 || s->first_glyph->type == COMPOSITE_GLYPH))
1183 {
1184 struct font_metrics metrics;
1185
1186 if (s->first_glyph->type == CHAR_GLYPH)
1187 {
1188 unsigned *code = alloca (sizeof (unsigned) * s->nchars);
1189 struct font *font = s->font;
1190 int i;
1191
1192 for (i = 0; i < s->nchars; i++)
1193 code[i] = (s->char2b[i].byte1 << 8) | s->char2b[i].byte2;
1194 font->driver->text_extents (font, code, s->nchars, &metrics);
1195 }
1196 else
1197 {
1198 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1199
1200 composition_gstring_width (gstring, s->cmp_from, s->cmp_to, &metrics);
1201 }
1202 s->right_overhang = (metrics.rbearing > metrics.width
1203 ? metrics.rbearing - metrics.width : 0);
1204 s->left_overhang = metrics.lbearing < 0 ? - metrics.lbearing : 0;
1205 }
1206 else if (s->cmp)
1207 {
1208 s->right_overhang = s->cmp->rbearing - s->cmp->pixel_width;
1209 s->left_overhang = - s->cmp->lbearing;
1210 }
1211 }
1212
1213
1214 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
1215
1216 static INLINE void
1217 x_clear_glyph_string_rect (s, x, y, w, h)
1218 struct glyph_string *s;
1219 int x, y, w, h;
1220 {
1221 XGCValues xgcv;
1222 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
1223 XSetForeground (s->display, s->gc, xgcv.background);
1224 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
1225 XSetForeground (s->display, s->gc, xgcv.foreground);
1226 }
1227
1228
1229 /* Draw the background of glyph_string S. If S->background_filled_p
1230 is non-zero don't draw it. FORCE_P non-zero means draw the
1231 background even if it wouldn't be drawn normally. This is used
1232 when a string preceding S draws into the background of S, or S
1233 contains the first component of a composition. */
1234
1235 static void
1236 x_draw_glyph_string_background (s, force_p)
1237 struct glyph_string *s;
1238 int force_p;
1239 {
1240 /* Nothing to do if background has already been drawn or if it
1241 shouldn't be drawn in the first place. */
1242 if (!s->background_filled_p)
1243 {
1244 int box_line_width = max (s->face->box_line_width, 0);
1245
1246 if (s->stippled_p)
1247 {
1248 /* Fill background with a stipple pattern. */
1249 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
1250 XFillRectangle (s->display, s->window, s->gc, s->x,
1251 s->y + box_line_width,
1252 s->background_width,
1253 s->height - 2 * box_line_width);
1254 XSetFillStyle (s->display, s->gc, FillSolid);
1255 s->background_filled_p = 1;
1256 }
1257 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
1258 || s->font_not_found_p
1259 || s->extends_to_end_of_line_p
1260 || force_p)
1261 {
1262 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
1263 s->background_width,
1264 s->height - 2 * box_line_width);
1265 s->background_filled_p = 1;
1266 }
1267 }
1268 }
1269
1270
1271 /* Draw the foreground of glyph string S. */
1272
1273 static void
1274 x_draw_glyph_string_foreground (s)
1275 struct glyph_string *s;
1276 {
1277 int i, x;
1278
1279 /* If first glyph of S has a left box line, start drawing the text
1280 of S to the right of that box line. */
1281 if (s->face->box != FACE_NO_BOX
1282 && s->first_glyph->left_box_line_p)
1283 x = s->x + eabs (s->face->box_line_width);
1284 else
1285 x = s->x;
1286
1287 /* Draw characters of S as rectangles if S's font could not be
1288 loaded. */
1289 if (s->font_not_found_p)
1290 {
1291 for (i = 0; i < s->nchars; ++i)
1292 {
1293 struct glyph *g = s->first_glyph + i;
1294 XDrawRectangle (s->display, s->window,
1295 s->gc, x, s->y, g->pixel_width - 1,
1296 s->height - 1);
1297 x += g->pixel_width;
1298 }
1299 }
1300 else
1301 {
1302 struct font *font = s->font;
1303 int boff = font->baseline_offset;
1304 int y;
1305
1306 if (font->vertical_centering)
1307 boff = VCENTER_BASELINE_OFFSET (font, s->f) - boff;
1308
1309 y = s->ybase - boff;
1310 if (s->for_overlaps
1311 || (s->background_filled_p && s->hl != DRAW_CURSOR))
1312 font->driver->draw (s, 0, s->nchars, x, y, 0);
1313 else
1314 font->driver->draw (s, 0, s->nchars, x, y, 1);
1315 if (s->face->overstrike)
1316 font->driver->draw (s, 0, s->nchars, x + 1, y, 0);
1317 }
1318 }
1319
1320 /* Draw the foreground of composite glyph string S. */
1321
1322 static void
1323 x_draw_composite_glyph_string_foreground (s)
1324 struct glyph_string *s;
1325 {
1326 int i, j, x;
1327 struct font *font = s->font;
1328
1329 /* If first glyph of S has a left box line, start drawing the text
1330 of S to the right of that box line. */
1331 if (s->face && s->face->box != FACE_NO_BOX
1332 && s->first_glyph->left_box_line_p)
1333 x = s->x + eabs (s->face->box_line_width);
1334 else
1335 x = s->x;
1336
1337 /* S is a glyph string for a composition. S->cmp_from is the index
1338 of the first character drawn for glyphs of this composition.
1339 S->cmp_from == 0 means we are drawing the very first character of
1340 this composition. */
1341
1342 /* Draw a rectangle for the composition if the font for the very
1343 first character of the composition could not be loaded. */
1344 if (s->font_not_found_p)
1345 {
1346 if (s->cmp_from == 0)
1347 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
1348 s->width - 1, s->height - 1);
1349 }
1350 else if (! s->first_glyph->u.cmp.automatic)
1351 {
1352 int y = s->ybase;
1353
1354 for (i = 0, j = s->cmp_from; i < s->nchars; i++, j++)
1355 if (COMPOSITION_GLYPH (s->cmp, j) != '\t')
1356 {
1357 int xx = x + s->cmp->offsets[j * 2];
1358 int yy = y - s->cmp->offsets[j * 2 + 1];
1359
1360 font->driver->draw (s, j, j + 1, xx, yy, 0);
1361 if (s->face->overstrike)
1362 font->driver->draw (s, j, j + 1, xx + 1, yy, 0);
1363 }
1364 }
1365 else
1366 {
1367 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1368 Lisp_Object glyph;
1369 int y = s->ybase;
1370 int width = 0;
1371
1372 for (i = j = s->cmp_from; i < s->cmp_to; i++)
1373 {
1374 glyph = LGSTRING_GLYPH (gstring, i);
1375 if (NILP (LGLYPH_ADJUSTMENT (glyph)))
1376 width += LGLYPH_WIDTH (glyph);
1377 else
1378 {
1379 int xoff, yoff, wadjust;
1380
1381 if (j < i)
1382 {
1383 font->driver->draw (s, j, i, x, y, 0);
1384 x += width;
1385 }
1386 xoff = LGLYPH_XOFF (glyph);
1387 yoff = LGLYPH_YOFF (glyph);
1388 wadjust = LGLYPH_WADJUST (glyph);
1389 font->driver->draw (s, i, i + 1, x + xoff, y + yoff, 0);
1390 x += wadjust;
1391 j = i + 1;
1392 width = 0;
1393 }
1394 }
1395 if (j < i)
1396 font->driver->draw (s, j, i, x, y, 0);
1397 }
1398 }
1399
1400
1401 #ifdef USE_X_TOOLKIT
1402
1403 static struct frame *x_frame_of_widget P_ ((Widget));
1404 static Boolean cvt_string_to_pixel P_ ((Display *, XrmValue *, Cardinal *,
1405 XrmValue *, XrmValue *, XtPointer *));
1406 static void cvt_pixel_dtor P_ ((XtAppContext, XrmValue *, XtPointer,
1407 XrmValue *, Cardinal *));
1408
1409
1410 /* Return the frame on which widget WIDGET is used.. Abort if frame
1411 cannot be determined. */
1412
1413 static struct frame *
1414 x_frame_of_widget (widget)
1415 Widget widget;
1416 {
1417 struct x_display_info *dpyinfo;
1418 Lisp_Object tail;
1419 struct frame *f;
1420
1421 dpyinfo = x_display_info_for_display (XtDisplay (widget));
1422
1423 /* Find the top-level shell of the widget. Note that this function
1424 can be called when the widget is not yet realized, so XtWindow
1425 (widget) == 0. That's the reason we can't simply use
1426 x_any_window_to_frame. */
1427 while (!XtIsTopLevelShell (widget))
1428 widget = XtParent (widget);
1429
1430 /* Look for a frame with that top-level widget. Allocate the color
1431 on that frame to get the right gamma correction value. */
1432 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
1433 if (FRAMEP (XCAR (tail))
1434 && (f = XFRAME (XCAR (tail)),
1435 (FRAME_X_P (f)
1436 && f->output_data.nothing != 1
1437 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
1438 && f->output_data.x->widget == widget)
1439 return f;
1440
1441 abort ();
1442 }
1443
1444
1445 /* Allocate the color COLOR->pixel on the screen and display of
1446 widget WIDGET in colormap CMAP. If an exact match cannot be
1447 allocated, try the nearest color available. Value is non-zero
1448 if successful. This is called from lwlib. */
1449
1450 int
1451 x_alloc_nearest_color_for_widget (widget, cmap, color)
1452 Widget widget;
1453 Colormap cmap;
1454 XColor *color;
1455 {
1456 struct frame *f = x_frame_of_widget (widget);
1457 return x_alloc_nearest_color (f, cmap, color);
1458 }
1459
1460
1461 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1462 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1463 If this produces the same color as PIXEL, try a color where all RGB
1464 values have DELTA added. Return the allocated color in *PIXEL.
1465 DISPLAY is the X display, CMAP is the colormap to operate on.
1466 Value is non-zero if successful. */
1467
1468 int
1469 x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
1470 Widget widget;
1471 Display *display;
1472 Colormap cmap;
1473 unsigned long *pixel;
1474 double factor;
1475 int delta;
1476 {
1477 struct frame *f = x_frame_of_widget (widget);
1478 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
1479 }
1480
1481
1482 /* Structure specifying which arguments should be passed by Xt to
1483 cvt_string_to_pixel. We want the widget's screen and colormap. */
1484
1485 static XtConvertArgRec cvt_string_to_pixel_args[] =
1486 {
1487 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.screen),
1488 sizeof (Screen *)},
1489 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.colormap),
1490 sizeof (Colormap)}
1491 };
1492
1493
1494 /* The address of this variable is returned by
1495 cvt_string_to_pixel. */
1496
1497 static Pixel cvt_string_to_pixel_value;
1498
1499
1500 /* Convert a color name to a pixel color.
1501
1502 DPY is the display we are working on.
1503
1504 ARGS is an array of *NARGS XrmValue structures holding additional
1505 information about the widget for which the conversion takes place.
1506 The contents of this array are determined by the specification
1507 in cvt_string_to_pixel_args.
1508
1509 FROM is a pointer to an XrmValue which points to the color name to
1510 convert. TO is an XrmValue in which to return the pixel color.
1511
1512 CLOSURE_RET is a pointer to user-data, in which we record if
1513 we allocated the color or not.
1514
1515 Value is True if successful, False otherwise. */
1516
1517 static Boolean
1518 cvt_string_to_pixel (dpy, args, nargs, from, to, closure_ret)
1519 Display *dpy;
1520 XrmValue *args;
1521 Cardinal *nargs;
1522 XrmValue *from, *to;
1523 XtPointer *closure_ret;
1524 {
1525 Screen *screen;
1526 Colormap cmap;
1527 Pixel pixel;
1528 String color_name;
1529 XColor color;
1530
1531 if (*nargs != 2)
1532 {
1533 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1534 "wrongParameters", "cvt_string_to_pixel",
1535 "XtToolkitError",
1536 "Screen and colormap args required", NULL, NULL);
1537 return False;
1538 }
1539
1540 screen = *(Screen **) args[0].addr;
1541 cmap = *(Colormap *) args[1].addr;
1542 color_name = (String) from->addr;
1543
1544 if (strcmp (color_name, XtDefaultBackground) == 0)
1545 {
1546 *closure_ret = (XtPointer) False;
1547 pixel = WhitePixelOfScreen (screen);
1548 }
1549 else if (strcmp (color_name, XtDefaultForeground) == 0)
1550 {
1551 *closure_ret = (XtPointer) False;
1552 pixel = BlackPixelOfScreen (screen);
1553 }
1554 else if (XParseColor (dpy, cmap, color_name, &color)
1555 && x_alloc_nearest_color_1 (dpy, cmap, &color))
1556 {
1557 pixel = color.pixel;
1558 *closure_ret = (XtPointer) True;
1559 }
1560 else
1561 {
1562 String params[1];
1563 Cardinal nparams = 1;
1564
1565 params[0] = color_name;
1566 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1567 "badValue", "cvt_string_to_pixel",
1568 "XtToolkitError", "Invalid color `%s'",
1569 params, &nparams);
1570 return False;
1571 }
1572
1573 if (to->addr != NULL)
1574 {
1575 if (to->size < sizeof (Pixel))
1576 {
1577 to->size = sizeof (Pixel);
1578 return False;
1579 }
1580
1581 *(Pixel *) to->addr = pixel;
1582 }
1583 else
1584 {
1585 cvt_string_to_pixel_value = pixel;
1586 to->addr = (XtPointer) &cvt_string_to_pixel_value;
1587 }
1588
1589 to->size = sizeof (Pixel);
1590 return True;
1591 }
1592
1593
1594 /* Free a pixel color which was previously allocated via
1595 cvt_string_to_pixel. This is registered as the destructor
1596 for this type of resource via XtSetTypeConverter.
1597
1598 APP is the application context in which we work.
1599
1600 TO is a pointer to an XrmValue holding the color to free.
1601 CLOSURE is the value we stored in CLOSURE_RET for this color
1602 in cvt_string_to_pixel.
1603
1604 ARGS and NARGS are like for cvt_string_to_pixel. */
1605
1606 static void
1607 cvt_pixel_dtor (app, to, closure, args, nargs)
1608 XtAppContext app;
1609 XrmValuePtr to;
1610 XtPointer closure;
1611 XrmValuePtr args;
1612 Cardinal *nargs;
1613 {
1614 if (*nargs != 2)
1615 {
1616 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
1617 "XtToolkitError",
1618 "Screen and colormap arguments required",
1619 NULL, NULL);
1620 }
1621 else if (closure != NULL)
1622 {
1623 /* We did allocate the pixel, so free it. */
1624 Screen *screen = *(Screen **) args[0].addr;
1625 Colormap cmap = *(Colormap *) args[1].addr;
1626 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
1627 (Pixel *) to->addr, 1);
1628 }
1629 }
1630
1631
1632 #endif /* USE_X_TOOLKIT */
1633
1634
1635 /* Value is an array of XColor structures for the contents of the
1636 color map of display DPY. Set *NCELLS to the size of the array.
1637 Note that this probably shouldn't be called for large color maps,
1638 say a 24-bit TrueColor map. */
1639
1640 static const XColor *
1641 x_color_cells (dpy, ncells)
1642 Display *dpy;
1643 int *ncells;
1644 {
1645 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1646
1647 if (dpyinfo->color_cells == NULL)
1648 {
1649 Screen *screen = dpyinfo->screen;
1650 int i;
1651
1652 dpyinfo->ncolor_cells
1653 = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
1654 dpyinfo->color_cells
1655 = (XColor *) xmalloc (dpyinfo->ncolor_cells
1656 * sizeof *dpyinfo->color_cells);
1657
1658 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
1659 dpyinfo->color_cells[i].pixel = i;
1660
1661 XQueryColors (dpy, dpyinfo->cmap,
1662 dpyinfo->color_cells, dpyinfo->ncolor_cells);
1663 }
1664
1665 *ncells = dpyinfo->ncolor_cells;
1666 return dpyinfo->color_cells;
1667 }
1668
1669
1670 /* On frame F, translate pixel colors to RGB values for the NCOLORS
1671 colors in COLORS. Use cached information, if available. */
1672
1673 void
1674 x_query_colors (f, colors, ncolors)
1675 struct frame *f;
1676 XColor *colors;
1677 int ncolors;
1678 {
1679 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1680
1681 if (dpyinfo->color_cells)
1682 {
1683 int i;
1684 for (i = 0; i < ncolors; ++i)
1685 {
1686 unsigned long pixel = colors[i].pixel;
1687 xassert (pixel < dpyinfo->ncolor_cells);
1688 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
1689 colors[i] = dpyinfo->color_cells[pixel];
1690 }
1691 }
1692 else
1693 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
1694 }
1695
1696
1697 /* On frame F, translate pixel color to RGB values for the color in
1698 COLOR. Use cached information, if available. */
1699
1700 void
1701 x_query_color (f, color)
1702 struct frame *f;
1703 XColor *color;
1704 {
1705 x_query_colors (f, color, 1);
1706 }
1707
1708
1709 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
1710 exact match can't be allocated, try the nearest color available.
1711 Value is non-zero if successful. Set *COLOR to the color
1712 allocated. */
1713
1714 static int
1715 x_alloc_nearest_color_1 (dpy, cmap, color)
1716 Display *dpy;
1717 Colormap cmap;
1718 XColor *color;
1719 {
1720 int rc;
1721
1722 rc = XAllocColor (dpy, cmap, color);
1723 if (rc == 0)
1724 {
1725 /* If we got to this point, the colormap is full, so we're going
1726 to try to get the next closest color. The algorithm used is
1727 a least-squares matching, which is what X uses for closest
1728 color matching with StaticColor visuals. */
1729 int nearest, i;
1730 unsigned long nearest_delta = ~0;
1731 int ncells;
1732 const XColor *cells = x_color_cells (dpy, &ncells);
1733
1734 for (nearest = i = 0; i < ncells; ++i)
1735 {
1736 long dred = (color->red >> 8) - (cells[i].red >> 8);
1737 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
1738 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
1739 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
1740
1741 if (delta < nearest_delta)
1742 {
1743 nearest = i;
1744 nearest_delta = delta;
1745 }
1746 }
1747
1748 color->red = cells[nearest].red;
1749 color->green = cells[nearest].green;
1750 color->blue = cells[nearest].blue;
1751 rc = XAllocColor (dpy, cmap, color);
1752 }
1753 else
1754 {
1755 /* If allocation succeeded, and the allocated pixel color is not
1756 equal to a cached pixel color recorded earlier, there was a
1757 change in the colormap, so clear the color cache. */
1758 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1759 XColor *cached_color;
1760
1761 if (dpyinfo->color_cells
1762 && (cached_color = &dpyinfo->color_cells[color->pixel],
1763 (cached_color->red != color->red
1764 || cached_color->blue != color->blue
1765 || cached_color->green != color->green)))
1766 {
1767 xfree (dpyinfo->color_cells);
1768 dpyinfo->color_cells = NULL;
1769 dpyinfo->ncolor_cells = 0;
1770 }
1771 }
1772
1773 #ifdef DEBUG_X_COLORS
1774 if (rc)
1775 register_color (color->pixel);
1776 #endif /* DEBUG_X_COLORS */
1777
1778 return rc;
1779 }
1780
1781
1782 /* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
1783 exact match can't be allocated, try the nearest color available.
1784 Value is non-zero if successful. Set *COLOR to the color
1785 allocated. */
1786
1787 int
1788 x_alloc_nearest_color (f, cmap, color)
1789 struct frame *f;
1790 Colormap cmap;
1791 XColor *color;
1792 {
1793 gamma_correct (f, color);
1794 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
1795 }
1796
1797
1798 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
1799 It's necessary to do this instead of just using PIXEL directly to
1800 get color reference counts right. */
1801
1802 unsigned long
1803 x_copy_color (f, pixel)
1804 struct frame *f;
1805 unsigned long pixel;
1806 {
1807 XColor color;
1808
1809 color.pixel = pixel;
1810 BLOCK_INPUT;
1811 x_query_color (f, &color);
1812 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
1813 UNBLOCK_INPUT;
1814 #ifdef DEBUG_X_COLORS
1815 register_color (pixel);
1816 #endif
1817 return color.pixel;
1818 }
1819
1820
1821 /* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
1822 It's necessary to do this instead of just using PIXEL directly to
1823 get color reference counts right. */
1824
1825 unsigned long
1826 x_copy_dpy_color (dpy, cmap, pixel)
1827 Display *dpy;
1828 Colormap cmap;
1829 unsigned long pixel;
1830 {
1831 XColor color;
1832
1833 color.pixel = pixel;
1834 BLOCK_INPUT;
1835 XQueryColor (dpy, cmap, &color);
1836 XAllocColor (dpy, cmap, &color);
1837 UNBLOCK_INPUT;
1838 #ifdef DEBUG_X_COLORS
1839 register_color (pixel);
1840 #endif
1841 return color.pixel;
1842 }
1843
1844
1845 /* Brightness beyond which a color won't have its highlight brightness
1846 boosted.
1847
1848 Nominally, highlight colors for `3d' faces are calculated by
1849 brightening an object's color by a constant scale factor, but this
1850 doesn't yield good results for dark colors, so for colors who's
1851 brightness is less than this value (on a scale of 0-65535) have an
1852 use an additional additive factor.
1853
1854 The value here is set so that the default menu-bar/mode-line color
1855 (grey75) will not have its highlights changed at all. */
1856 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
1857
1858
1859 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1860 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1861 If this produces the same color as PIXEL, try a color where all RGB
1862 values have DELTA added. Return the allocated color in *PIXEL.
1863 DISPLAY is the X display, CMAP is the colormap to operate on.
1864 Value is non-zero if successful. */
1865
1866 static int
1867 x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
1868 struct frame *f;
1869 Display *display;
1870 Colormap cmap;
1871 unsigned long *pixel;
1872 double factor;
1873 int delta;
1874 {
1875 XColor color, new;
1876 long bright;
1877 int success_p;
1878
1879 /* Get RGB color values. */
1880 color.pixel = *pixel;
1881 x_query_color (f, &color);
1882
1883 /* Change RGB values by specified FACTOR. Avoid overflow! */
1884 xassert (factor >= 0);
1885 new.red = min (0xffff, factor * color.red);
1886 new.green = min (0xffff, factor * color.green);
1887 new.blue = min (0xffff, factor * color.blue);
1888
1889 /* Calculate brightness of COLOR. */
1890 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
1891
1892 /* We only boost colors that are darker than
1893 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
1894 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
1895 /* Make an additive adjustment to NEW, because it's dark enough so
1896 that scaling by FACTOR alone isn't enough. */
1897 {
1898 /* How far below the limit this color is (0 - 1, 1 being darker). */
1899 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
1900 /* The additive adjustment. */
1901 int min_delta = delta * dimness * factor / 2;
1902
1903 if (factor < 1)
1904 {
1905 new.red = max (0, new.red - min_delta);
1906 new.green = max (0, new.green - min_delta);
1907 new.blue = max (0, new.blue - min_delta);
1908 }
1909 else
1910 {
1911 new.red = min (0xffff, min_delta + new.red);
1912 new.green = min (0xffff, min_delta + new.green);
1913 new.blue = min (0xffff, min_delta + new.blue);
1914 }
1915 }
1916
1917 /* Try to allocate the color. */
1918 success_p = x_alloc_nearest_color (f, cmap, &new);
1919 if (success_p)
1920 {
1921 if (new.pixel == *pixel)
1922 {
1923 /* If we end up with the same color as before, try adding
1924 delta to the RGB values. */
1925 x_free_colors (f, &new.pixel, 1);
1926
1927 new.red = min (0xffff, delta + color.red);
1928 new.green = min (0xffff, delta + color.green);
1929 new.blue = min (0xffff, delta + color.blue);
1930 success_p = x_alloc_nearest_color (f, cmap, &new);
1931 }
1932 else
1933 success_p = 1;
1934 *pixel = new.pixel;
1935 }
1936
1937 return success_p;
1938 }
1939
1940
1941 /* Set up the foreground color for drawing relief lines of glyph
1942 string S. RELIEF is a pointer to a struct relief containing the GC
1943 with which lines will be drawn. Use a color that is FACTOR or
1944 DELTA lighter or darker than the relief's background which is found
1945 in S->f->output_data.x->relief_background. If such a color cannot
1946 be allocated, use DEFAULT_PIXEL, instead. */
1947
1948 static void
1949 x_setup_relief_color (f, relief, factor, delta, default_pixel)
1950 struct frame *f;
1951 struct relief *relief;
1952 double factor;
1953 int delta;
1954 unsigned long default_pixel;
1955 {
1956 XGCValues xgcv;
1957 struct x_output *di = f->output_data.x;
1958 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
1959 unsigned long pixel;
1960 unsigned long background = di->relief_background;
1961 Colormap cmap = FRAME_X_COLORMAP (f);
1962 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1963 Display *dpy = FRAME_X_DISPLAY (f);
1964
1965 xgcv.graphics_exposures = False;
1966 xgcv.line_width = 1;
1967
1968 /* Free previously allocated color. The color cell will be reused
1969 when it has been freed as many times as it was allocated, so this
1970 doesn't affect faces using the same colors. */
1971 if (relief->gc
1972 && relief->allocated_p)
1973 {
1974 x_free_colors (f, &relief->pixel, 1);
1975 relief->allocated_p = 0;
1976 }
1977
1978 /* Allocate new color. */
1979 xgcv.foreground = default_pixel;
1980 pixel = background;
1981 if (dpyinfo->n_planes != 1
1982 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
1983 {
1984 relief->allocated_p = 1;
1985 xgcv.foreground = relief->pixel = pixel;
1986 }
1987
1988 if (relief->gc == 0)
1989 {
1990 xgcv.stipple = dpyinfo->gray;
1991 mask |= GCStipple;
1992 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
1993 }
1994 else
1995 XChangeGC (dpy, relief->gc, mask, &xgcv);
1996 }
1997
1998
1999 /* Set up colors for the relief lines around glyph string S. */
2000
2001 static void
2002 x_setup_relief_colors (s)
2003 struct glyph_string *s;
2004 {
2005 struct x_output *di = s->f->output_data.x;
2006 unsigned long color;
2007
2008 if (s->face->use_box_color_for_shadows_p)
2009 color = s->face->box_color;
2010 else if (s->first_glyph->type == IMAGE_GLYPH
2011 && s->img->pixmap
2012 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
2013 color = IMAGE_BACKGROUND (s->img, s->f, 0);
2014 else
2015 {
2016 XGCValues xgcv;
2017
2018 /* Get the background color of the face. */
2019 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
2020 color = xgcv.background;
2021 }
2022
2023 if (di->white_relief.gc == 0
2024 || color != di->relief_background)
2025 {
2026 di->relief_background = color;
2027 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
2028 WHITE_PIX_DEFAULT (s->f));
2029 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
2030 BLACK_PIX_DEFAULT (s->f));
2031 }
2032 }
2033
2034
2035 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
2036 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
2037 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
2038 relief. LEFT_P non-zero means draw a relief on the left side of
2039 the rectangle. RIGHT_P non-zero means draw a relief on the right
2040 side of the rectangle. CLIP_RECT is the clipping rectangle to use
2041 when drawing. */
2042
2043 static void
2044 x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
2045 raised_p, top_p, bot_p, left_p, right_p, clip_rect)
2046 struct frame *f;
2047 int left_x, top_y, right_x, bottom_y, width;
2048 int top_p, bot_p, left_p, right_p, raised_p;
2049 XRectangle *clip_rect;
2050 {
2051 Display *dpy = FRAME_X_DISPLAY (f);
2052 Window window = FRAME_X_WINDOW (f);
2053 int i;
2054 GC gc;
2055
2056 if (raised_p)
2057 gc = f->output_data.x->white_relief.gc;
2058 else
2059 gc = f->output_data.x->black_relief.gc;
2060 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2061
2062 /* Top. */
2063 if (top_p)
2064 for (i = 0; i < width; ++i)
2065 XDrawLine (dpy, window, gc,
2066 left_x + i * left_p, top_y + i,
2067 right_x + 1 - i * right_p, top_y + i);
2068
2069 /* Left. */
2070 if (left_p)
2071 for (i = 0; i < width; ++i)
2072 XDrawLine (dpy, window, gc,
2073 left_x + i, top_y + i, left_x + i, bottom_y - i + 1);
2074
2075 XSetClipMask (dpy, gc, None);
2076 if (raised_p)
2077 gc = f->output_data.x->black_relief.gc;
2078 else
2079 gc = f->output_data.x->white_relief.gc;
2080 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2081
2082 /* Bottom. */
2083 if (bot_p)
2084 for (i = 0; i < width; ++i)
2085 XDrawLine (dpy, window, gc,
2086 left_x + i * left_p, bottom_y - i,
2087 right_x + 1 - i * right_p, bottom_y - i);
2088
2089 /* Right. */
2090 if (right_p)
2091 for (i = 0; i < width; ++i)
2092 XDrawLine (dpy, window, gc,
2093 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
2094
2095 XSetClipMask (dpy, gc, None);
2096 }
2097
2098
2099 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
2100 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
2101 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
2102 left side of the rectangle. RIGHT_P non-zero means draw a line
2103 on the right side of the rectangle. CLIP_RECT is the clipping
2104 rectangle to use when drawing. */
2105
2106 static void
2107 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2108 left_p, right_p, clip_rect)
2109 struct glyph_string *s;
2110 int left_x, top_y, right_x, bottom_y, width, left_p, right_p;
2111 XRectangle *clip_rect;
2112 {
2113 XGCValues xgcv;
2114
2115 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2116 XSetForeground (s->display, s->gc, s->face->box_color);
2117 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
2118
2119 /* Top. */
2120 XFillRectangle (s->display, s->window, s->gc,
2121 left_x, top_y, right_x - left_x + 1, width);
2122
2123 /* Left. */
2124 if (left_p)
2125 XFillRectangle (s->display, s->window, s->gc,
2126 left_x, top_y, width, bottom_y - top_y + 1);
2127
2128 /* Bottom. */
2129 XFillRectangle (s->display, s->window, s->gc,
2130 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
2131
2132 /* Right. */
2133 if (right_p)
2134 XFillRectangle (s->display, s->window, s->gc,
2135 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
2136
2137 XSetForeground (s->display, s->gc, xgcv.foreground);
2138 XSetClipMask (s->display, s->gc, None);
2139 }
2140
2141
2142 /* Draw a box around glyph string S. */
2143
2144 static void
2145 x_draw_glyph_string_box (s)
2146 struct glyph_string *s;
2147 {
2148 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
2149 int left_p, right_p;
2150 struct glyph *last_glyph;
2151 XRectangle clip_rect;
2152
2153 last_x = ((s->row->full_width_p && !s->w->pseudo_window_p)
2154 ? WINDOW_RIGHT_EDGE_X (s->w)
2155 : window_box_right (s->w, s->area));
2156
2157 /* The glyph that may have a right box line. */
2158 last_glyph = (s->cmp || s->img
2159 ? s->first_glyph
2160 : s->first_glyph + s->nchars - 1);
2161
2162 width = eabs (s->face->box_line_width);
2163 raised_p = s->face->box == FACE_RAISED_BOX;
2164 left_x = s->x;
2165 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
2166 ? last_x - 1
2167 : min (last_x, s->x + s->background_width) - 1);
2168 top_y = s->y;
2169 bottom_y = top_y + s->height - 1;
2170
2171 left_p = (s->first_glyph->left_box_line_p
2172 || (s->hl == DRAW_MOUSE_FACE
2173 && (s->prev == NULL
2174 || s->prev->hl != s->hl)));
2175 right_p = (last_glyph->right_box_line_p
2176 || (s->hl == DRAW_MOUSE_FACE
2177 && (s->next == NULL
2178 || s->next->hl != s->hl)));
2179
2180 get_glyph_string_clip_rect (s, &clip_rect);
2181
2182 if (s->face->box == FACE_SIMPLE_BOX)
2183 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2184 left_p, right_p, &clip_rect);
2185 else
2186 {
2187 x_setup_relief_colors (s);
2188 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
2189 width, raised_p, 1, 1, left_p, right_p, &clip_rect);
2190 }
2191 }
2192
2193
2194 /* Draw foreground of image glyph string S. */
2195
2196 static void
2197 x_draw_image_foreground (s)
2198 struct glyph_string *s;
2199 {
2200 int x = s->x;
2201 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2202
2203 /* If first glyph of S has a left box line, start drawing it to the
2204 right of that line. */
2205 if (s->face->box != FACE_NO_BOX
2206 && s->first_glyph->left_box_line_p
2207 && s->slice.x == 0)
2208 x += eabs (s->face->box_line_width);
2209
2210 /* If there is a margin around the image, adjust x- and y-position
2211 by that margin. */
2212 if (s->slice.x == 0)
2213 x += s->img->hmargin;
2214 if (s->slice.y == 0)
2215 y += s->img->vmargin;
2216
2217 if (s->img->pixmap)
2218 {
2219 if (s->img->mask)
2220 {
2221 /* We can't set both a clip mask and use XSetClipRectangles
2222 because the latter also sets a clip mask. We also can't
2223 trust on the shape extension to be available
2224 (XShapeCombineRegion). So, compute the rectangle to draw
2225 manually. */
2226 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2227 | GCFunction);
2228 XGCValues xgcv;
2229 XRectangle clip_rect, image_rect, r;
2230
2231 xgcv.clip_mask = s->img->mask;
2232 xgcv.clip_x_origin = x;
2233 xgcv.clip_y_origin = y;
2234 xgcv.function = GXcopy;
2235 XChangeGC (s->display, s->gc, mask, &xgcv);
2236
2237 get_glyph_string_clip_rect (s, &clip_rect);
2238 image_rect.x = x;
2239 image_rect.y = y;
2240 image_rect.width = s->slice.width;
2241 image_rect.height = s->slice.height;
2242 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2243 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2244 s->slice.x + r.x - x, s->slice.y + r.y - y,
2245 r.width, r.height, r.x, r.y);
2246 }
2247 else
2248 {
2249 XRectangle clip_rect, image_rect, r;
2250
2251 get_glyph_string_clip_rect (s, &clip_rect);
2252 image_rect.x = x;
2253 image_rect.y = y;
2254 image_rect.width = s->slice.width;
2255 image_rect.height = s->slice.height;
2256 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2257 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2258 s->slice.x + r.x - x, s->slice.y + r.y - y,
2259 r.width, r.height, r.x, r.y);
2260
2261 /* When the image has a mask, we can expect that at
2262 least part of a mouse highlight or a block cursor will
2263 be visible. If the image doesn't have a mask, make
2264 a block cursor visible by drawing a rectangle around
2265 the image. I believe it's looking better if we do
2266 nothing here for mouse-face. */
2267 if (s->hl == DRAW_CURSOR)
2268 {
2269 int r = s->img->relief;
2270 if (r < 0) r = -r;
2271 XDrawRectangle (s->display, s->window, s->gc,
2272 x - r, y - r,
2273 s->slice.width + r*2 - 1,
2274 s->slice.height + r*2 - 1);
2275 }
2276 }
2277 }
2278 else
2279 /* Draw a rectangle if image could not be loaded. */
2280 XDrawRectangle (s->display, s->window, s->gc, x, y,
2281 s->slice.width - 1, s->slice.height - 1);
2282 }
2283
2284
2285 /* Draw a relief around the image glyph string S. */
2286
2287 static void
2288 x_draw_image_relief (s)
2289 struct glyph_string *s;
2290 {
2291 int x0, y0, x1, y1, thick, raised_p;
2292 XRectangle r;
2293 int x = s->x;
2294 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2295
2296 /* If first glyph of S has a left box line, start drawing it to the
2297 right of that line. */
2298 if (s->face->box != FACE_NO_BOX
2299 && s->first_glyph->left_box_line_p
2300 && s->slice.x == 0)
2301 x += eabs (s->face->box_line_width);
2302
2303 /* If there is a margin around the image, adjust x- and y-position
2304 by that margin. */
2305 if (s->slice.x == 0)
2306 x += s->img->hmargin;
2307 if (s->slice.y == 0)
2308 y += s->img->vmargin;
2309
2310 if (s->hl == DRAW_IMAGE_SUNKEN
2311 || s->hl == DRAW_IMAGE_RAISED)
2312 {
2313 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
2314 raised_p = s->hl == DRAW_IMAGE_RAISED;
2315 }
2316 else
2317 {
2318 thick = eabs (s->img->relief);
2319 raised_p = s->img->relief > 0;
2320 }
2321
2322 x0 = x - thick;
2323 y0 = y - thick;
2324 x1 = x + s->slice.width + thick - 1;
2325 y1 = y + s->slice.height + thick - 1;
2326
2327 x_setup_relief_colors (s);
2328 get_glyph_string_clip_rect (s, &r);
2329 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p,
2330 s->slice.y == 0,
2331 s->slice.y + s->slice.height == s->img->height,
2332 s->slice.x == 0,
2333 s->slice.x + s->slice.width == s->img->width,
2334 &r);
2335 }
2336
2337
2338 /* Draw the foreground of image glyph string S to PIXMAP. */
2339
2340 static void
2341 x_draw_image_foreground_1 (s, pixmap)
2342 struct glyph_string *s;
2343 Pixmap pixmap;
2344 {
2345 int x = 0;
2346 int y = s->ybase - s->y - image_ascent (s->img, s->face, &s->slice);
2347
2348 /* If first glyph of S has a left box line, start drawing it to the
2349 right of that line. */
2350 if (s->face->box != FACE_NO_BOX
2351 && s->first_glyph->left_box_line_p
2352 && s->slice.x == 0)
2353 x += eabs (s->face->box_line_width);
2354
2355 /* If there is a margin around the image, adjust x- and y-position
2356 by that margin. */
2357 if (s->slice.x == 0)
2358 x += s->img->hmargin;
2359 if (s->slice.y == 0)
2360 y += s->img->vmargin;
2361
2362 if (s->img->pixmap)
2363 {
2364 if (s->img->mask)
2365 {
2366 /* We can't set both a clip mask and use XSetClipRectangles
2367 because the latter also sets a clip mask. We also can't
2368 trust on the shape extension to be available
2369 (XShapeCombineRegion). So, compute the rectangle to draw
2370 manually. */
2371 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2372 | GCFunction);
2373 XGCValues xgcv;
2374
2375 xgcv.clip_mask = s->img->mask;
2376 xgcv.clip_x_origin = x - s->slice.x;
2377 xgcv.clip_y_origin = y - s->slice.y;
2378 xgcv.function = GXcopy;
2379 XChangeGC (s->display, s->gc, mask, &xgcv);
2380
2381 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2382 s->slice.x, s->slice.y,
2383 s->slice.width, s->slice.height, x, y);
2384 XSetClipMask (s->display, s->gc, None);
2385 }
2386 else
2387 {
2388 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2389 s->slice.x, s->slice.y,
2390 s->slice.width, s->slice.height, x, y);
2391
2392 /* When the image has a mask, we can expect that at
2393 least part of a mouse highlight or a block cursor will
2394 be visible. If the image doesn't have a mask, make
2395 a block cursor visible by drawing a rectangle around
2396 the image. I believe it's looking better if we do
2397 nothing here for mouse-face. */
2398 if (s->hl == DRAW_CURSOR)
2399 {
2400 int r = s->img->relief;
2401 if (r < 0) r = -r;
2402 XDrawRectangle (s->display, s->window, s->gc, x - r, y - r,
2403 s->slice.width + r*2 - 1,
2404 s->slice.height + r*2 - 1);
2405 }
2406 }
2407 }
2408 else
2409 /* Draw a rectangle if image could not be loaded. */
2410 XDrawRectangle (s->display, pixmap, s->gc, x, y,
2411 s->slice.width - 1, s->slice.height - 1);
2412 }
2413
2414
2415 /* Draw part of the background of glyph string S. X, Y, W, and H
2416 give the rectangle to draw. */
2417
2418 static void
2419 x_draw_glyph_string_bg_rect (s, x, y, w, h)
2420 struct glyph_string *s;
2421 int x, y, w, h;
2422 {
2423 if (s->stippled_p)
2424 {
2425 /* Fill background with a stipple pattern. */
2426 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2427 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
2428 XSetFillStyle (s->display, s->gc, FillSolid);
2429 }
2430 else
2431 x_clear_glyph_string_rect (s, x, y, w, h);
2432 }
2433
2434
2435 /* Draw image glyph string S.
2436
2437 s->y
2438 s->x +-------------------------
2439 | s->face->box
2440 |
2441 | +-------------------------
2442 | | s->img->margin
2443 | |
2444 | | +-------------------
2445 | | | the image
2446
2447 */
2448
2449 static void
2450 x_draw_image_glyph_string (s)
2451 struct glyph_string *s;
2452 {
2453 int box_line_hwidth = eabs (s->face->box_line_width);
2454 int box_line_vwidth = max (s->face->box_line_width, 0);
2455 int height;
2456 Pixmap pixmap = None;
2457
2458 height = s->height;
2459 if (s->slice.y == 0)
2460 height -= box_line_vwidth;
2461 if (s->slice.y + s->slice.height >= s->img->height)
2462 height -= box_line_vwidth;
2463
2464 /* Fill background with face under the image. Do it only if row is
2465 taller than image or if image has a clip mask to reduce
2466 flickering. */
2467 s->stippled_p = s->face->stipple != 0;
2468 if (height > s->slice.height
2469 || s->img->hmargin
2470 || s->img->vmargin
2471 || s->img->mask
2472 || s->img->pixmap == 0
2473 || s->width != s->background_width)
2474 {
2475 if (s->img->mask)
2476 {
2477 /* Create a pixmap as large as the glyph string. Fill it
2478 with the background color. Copy the image to it, using
2479 its mask. Copy the temporary pixmap to the display. */
2480 Screen *screen = FRAME_X_SCREEN (s->f);
2481 int depth = DefaultDepthOfScreen (screen);
2482
2483 /* Create a pixmap as large as the glyph string. */
2484 pixmap = XCreatePixmap (s->display, s->window,
2485 s->background_width,
2486 s->height, depth);
2487
2488 /* Don't clip in the following because we're working on the
2489 pixmap. */
2490 XSetClipMask (s->display, s->gc, None);
2491
2492 /* Fill the pixmap with the background color/stipple. */
2493 if (s->stippled_p)
2494 {
2495 /* Fill background with a stipple pattern. */
2496 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2497 XSetTSOrigin (s->display, s->gc, - s->x, - s->y);
2498 XFillRectangle (s->display, pixmap, s->gc,
2499 0, 0, s->background_width, s->height);
2500 XSetFillStyle (s->display, s->gc, FillSolid);
2501 XSetTSOrigin (s->display, s->gc, 0, 0);
2502 }
2503 else
2504 {
2505 XGCValues xgcv;
2506 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
2507 &xgcv);
2508 XSetForeground (s->display, s->gc, xgcv.background);
2509 XFillRectangle (s->display, pixmap, s->gc,
2510 0, 0, s->background_width, s->height);
2511 XSetForeground (s->display, s->gc, xgcv.foreground);
2512 }
2513 }
2514 else
2515 {
2516 int x = s->x;
2517 int y = s->y;
2518
2519 if (s->first_glyph->left_box_line_p
2520 && s->slice.x == 0)
2521 x += box_line_hwidth;
2522
2523 if (s->slice.y == 0)
2524 y += box_line_vwidth;
2525
2526 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
2527 }
2528
2529 s->background_filled_p = 1;
2530 }
2531
2532 /* Draw the foreground. */
2533 if (pixmap != None)
2534 {
2535 x_draw_image_foreground_1 (s, pixmap);
2536 x_set_glyph_string_clipping (s);
2537 XCopyArea (s->display, pixmap, s->window, s->gc,
2538 0, 0, s->background_width, s->height, s->x, s->y);
2539 XFreePixmap (s->display, pixmap);
2540 }
2541 else
2542 x_draw_image_foreground (s);
2543
2544 /* If we must draw a relief around the image, do it. */
2545 if (s->img->relief
2546 || s->hl == DRAW_IMAGE_RAISED
2547 || s->hl == DRAW_IMAGE_SUNKEN)
2548 x_draw_image_relief (s);
2549 }
2550
2551
2552 /* Draw stretch glyph string S. */
2553
2554 static void
2555 x_draw_stretch_glyph_string (s)
2556 struct glyph_string *s;
2557 {
2558 xassert (s->first_glyph->type == STRETCH_GLYPH);
2559
2560 if (s->hl == DRAW_CURSOR
2561 && !x_stretch_cursor_p)
2562 {
2563 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
2564 as wide as the stretch glyph. */
2565 int width, background_width = s->background_width;
2566 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2567
2568 if (x < left_x)
2569 {
2570 background_width -= left_x - x;
2571 x = left_x;
2572 }
2573 width = min (FRAME_COLUMN_WIDTH (s->f), background_width);
2574
2575 /* Draw cursor. */
2576 x_draw_glyph_string_bg_rect (s, x, s->y, width, s->height);
2577
2578 /* Clear rest using the GC of the original non-cursor face. */
2579 if (width < background_width)
2580 {
2581 int y = s->y;
2582 int w = background_width - width, h = s->height;
2583 XRectangle r;
2584 GC gc;
2585
2586 x += width;
2587 if (s->row->mouse_face_p
2588 && cursor_in_mouse_face_p (s->w))
2589 {
2590 x_set_mouse_face_gc (s);
2591 gc = s->gc;
2592 }
2593 else
2594 gc = s->face->gc;
2595
2596 get_glyph_string_clip_rect (s, &r);
2597 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
2598
2599 if (s->face->stipple)
2600 {
2601 /* Fill background with a stipple pattern. */
2602 XSetFillStyle (s->display, gc, FillOpaqueStippled);
2603 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2604 XSetFillStyle (s->display, gc, FillSolid);
2605 }
2606 else
2607 {
2608 XGCValues xgcv;
2609 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
2610 XSetForeground (s->display, gc, xgcv.background);
2611 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2612 XSetForeground (s->display, gc, xgcv.foreground);
2613 }
2614 }
2615 }
2616 else if (!s->background_filled_p)
2617 {
2618 int background_width = s->background_width;
2619 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2620
2621 /* Don't draw into left margin, fringe or scrollbar area
2622 except for header line and mode line. */
2623 if (x < left_x && !s->row->mode_line_p)
2624 {
2625 background_width -= left_x - x;
2626 x = left_x;
2627 }
2628 if (background_width > 0)
2629 x_draw_glyph_string_bg_rect (s, x, s->y, background_width, s->height);
2630 }
2631
2632 s->background_filled_p = 1;
2633 }
2634
2635
2636 /* Draw glyph string S. */
2637
2638 static void
2639 x_draw_glyph_string (s)
2640 struct glyph_string *s;
2641 {
2642 int relief_drawn_p = 0;
2643
2644 /* If S draws into the background of its successors, draw the
2645 background of the successors first so that S can draw into it.
2646 This makes S->next use XDrawString instead of XDrawImageString. */
2647 if (s->next && s->right_overhang && !s->for_overlaps)
2648 {
2649 int width;
2650 struct glyph_string *next;
2651
2652 for (width = 0, next = s->next;
2653 next && width < s->right_overhang;
2654 width += next->width, next = next->next)
2655 if (next->first_glyph->type != IMAGE_GLYPH)
2656 {
2657 x_set_glyph_string_gc (next);
2658 x_set_glyph_string_clipping (next);
2659 if (next->first_glyph->type == STRETCH_GLYPH)
2660 x_draw_stretch_glyph_string (next);
2661 else
2662 x_draw_glyph_string_background (next, 1);
2663 next->num_clips = 0;
2664 }
2665 }
2666
2667 /* Set up S->gc, set clipping and draw S. */
2668 x_set_glyph_string_gc (s);
2669
2670 /* Draw relief (if any) in advance for char/composition so that the
2671 glyph string can be drawn over it. */
2672 if (!s->for_overlaps
2673 && s->face->box != FACE_NO_BOX
2674 && (s->first_glyph->type == CHAR_GLYPH
2675 || s->first_glyph->type == COMPOSITE_GLYPH))
2676
2677 {
2678 x_set_glyph_string_clipping (s);
2679 x_draw_glyph_string_background (s, 1);
2680 x_draw_glyph_string_box (s);
2681 x_set_glyph_string_clipping (s);
2682 relief_drawn_p = 1;
2683 }
2684 else if (!s->clip_head /* draw_glyphs didn't specify a clip mask. */
2685 && !s->clip_tail
2686 && ((s->prev && s->prev->hl != s->hl && s->left_overhang)
2687 || (s->next && s->next->hl != s->hl && s->right_overhang)))
2688 /* We must clip just this glyph. left_overhang part has already
2689 drawn when s->prev was drawn, and right_overhang part will be
2690 drawn later when s->next is drawn. */
2691 x_set_glyph_string_clipping_exactly (s, s);
2692 else
2693 x_set_glyph_string_clipping (s);
2694
2695 switch (s->first_glyph->type)
2696 {
2697 case IMAGE_GLYPH:
2698 x_draw_image_glyph_string (s);
2699 break;
2700
2701 case STRETCH_GLYPH:
2702 x_draw_stretch_glyph_string (s);
2703 break;
2704
2705 case CHAR_GLYPH:
2706 if (s->for_overlaps)
2707 s->background_filled_p = 1;
2708 else
2709 x_draw_glyph_string_background (s, 0);
2710 x_draw_glyph_string_foreground (s);
2711 break;
2712
2713 case COMPOSITE_GLYPH:
2714 if (s->for_overlaps || (s->cmp_from > 0
2715 && ! s->first_glyph->u.cmp.automatic))
2716 s->background_filled_p = 1;
2717 else
2718 x_draw_glyph_string_background (s, 1);
2719 x_draw_composite_glyph_string_foreground (s);
2720 break;
2721
2722 default:
2723 abort ();
2724 }
2725
2726 if (!s->for_overlaps)
2727 {
2728 /* Draw underline. */
2729 if (s->face->underline_p)
2730 {
2731 unsigned long thickness, position;
2732 int y;
2733
2734 if (s->prev && s->prev->face->underline_p)
2735 {
2736 /* We use the same underline style as the previous one. */
2737 thickness = s->prev->underline_thickness;
2738 position = s->prev->underline_position;
2739 }
2740 else
2741 {
2742 /* Get the underline thickness. Default is 1 pixel. */
2743 if (s->font && s->font->underline_thickness > 0)
2744 thickness = s->font->underline_thickness;
2745 else
2746 thickness = 1;
2747 if (x_underline_at_descent_line)
2748 position = (s->height - thickness) - (s->ybase - s->y);
2749 else
2750 {
2751 /* Get the underline position. This is the recommended
2752 vertical offset in pixels from the baseline to the top of
2753 the underline. This is a signed value according to the
2754 specs, and its default is
2755
2756 ROUND ((maximum descent) / 2), with
2757 ROUND(x) = floor (x + 0.5) */
2758
2759 if (x_use_underline_position_properties
2760 && s->font && s->font->underline_position >= 0)
2761 position = s->font->underline_position;
2762 else if (s->font)
2763 position = (s->font->descent + 1) / 2;
2764 else
2765 position = underline_minimum_offset;
2766 }
2767 position = max (position, underline_minimum_offset);
2768 }
2769 /* Check the sanity of thickness and position. We should
2770 avoid drawing underline out of the current line area. */
2771 if (s->y + s->height <= s->ybase + position)
2772 position = (s->height - 1) - (s->ybase - s->y);
2773 if (s->y + s->height < s->ybase + position + thickness)
2774 thickness = (s->y + s->height) - (s->ybase + position);
2775 s->underline_thickness = thickness;
2776 s->underline_position = position;
2777 y = s->ybase + position;
2778 if (s->face->underline_defaulted_p)
2779 XFillRectangle (s->display, s->window, s->gc,
2780 s->x, y, s->width, thickness);
2781 else
2782 {
2783 XGCValues xgcv;
2784 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2785 XSetForeground (s->display, s->gc, s->face->underline_color);
2786 XFillRectangle (s->display, s->window, s->gc,
2787 s->x, y, s->width, thickness);
2788 XSetForeground (s->display, s->gc, xgcv.foreground);
2789 }
2790 }
2791
2792 /* Draw overline. */
2793 if (s->face->overline_p)
2794 {
2795 unsigned long dy = 0, h = 1;
2796
2797 if (s->face->overline_color_defaulted_p)
2798 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2799 s->width, h);
2800 else
2801 {
2802 XGCValues xgcv;
2803 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2804 XSetForeground (s->display, s->gc, s->face->overline_color);
2805 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2806 s->width, h);
2807 XSetForeground (s->display, s->gc, xgcv.foreground);
2808 }
2809 }
2810
2811 /* Draw strike-through. */
2812 if (s->face->strike_through_p)
2813 {
2814 unsigned long h = 1;
2815 unsigned long dy = (s->height - h) / 2;
2816
2817 if (s->face->strike_through_color_defaulted_p)
2818 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2819 s->width, h);
2820 else
2821 {
2822 XGCValues xgcv;
2823 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2824 XSetForeground (s->display, s->gc, s->face->strike_through_color);
2825 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2826 s->width, h);
2827 XSetForeground (s->display, s->gc, xgcv.foreground);
2828 }
2829 }
2830
2831 /* Draw relief if not yet drawn. */
2832 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
2833 x_draw_glyph_string_box (s);
2834
2835 if (s->prev)
2836 {
2837 struct glyph_string *prev;
2838
2839 for (prev = s->prev; prev; prev = prev->prev)
2840 if (prev->hl != s->hl
2841 && prev->x + prev->width + prev->right_overhang > s->x)
2842 {
2843 /* As prev was drawn while clipped to its own area, we
2844 must draw the right_overhang part using s->hl now. */
2845 enum draw_glyphs_face save = prev->hl;
2846
2847 prev->hl = s->hl;
2848 x_set_glyph_string_gc (prev);
2849 x_set_glyph_string_clipping_exactly (s, prev);
2850 if (prev->first_glyph->type == CHAR_GLYPH)
2851 x_draw_glyph_string_foreground (prev);
2852 else
2853 x_draw_composite_glyph_string_foreground (prev);
2854 XSetClipMask (prev->display, prev->gc, None);
2855 prev->hl = save;
2856 prev->num_clips = 0;
2857 }
2858 }
2859
2860 if (s->next)
2861 {
2862 struct glyph_string *next;
2863
2864 for (next = s->next; next; next = next->next)
2865 if (next->hl != s->hl
2866 && next->x - next->left_overhang < s->x + s->width)
2867 {
2868 /* As next will be drawn while clipped to its own area,
2869 we must draw the left_overhang part using s->hl now. */
2870 enum draw_glyphs_face save = next->hl;
2871
2872 next->hl = s->hl;
2873 x_set_glyph_string_gc (next);
2874 x_set_glyph_string_clipping_exactly (s, next);
2875 if (next->first_glyph->type == CHAR_GLYPH)
2876 x_draw_glyph_string_foreground (next);
2877 else
2878 x_draw_composite_glyph_string_foreground (next);
2879 XSetClipMask (next->display, next->gc, None);
2880 next->hl = save;
2881 next->num_clips = 0;
2882 }
2883 }
2884 }
2885
2886 /* Reset clipping. */
2887 XSetClipMask (s->display, s->gc, None);
2888 s->num_clips = 0;
2889 }
2890
2891 /* Shift display to make room for inserted glyphs. */
2892
2893 void
2894 x_shift_glyphs_for_insert (f, x, y, width, height, shift_by)
2895 struct frame *f;
2896 int x, y, width, height, shift_by;
2897 {
2898 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
2899 f->output_data.x->normal_gc,
2900 x, y, width, height,
2901 x + shift_by, y);
2902 }
2903
2904 /* Delete N glyphs at the nominal cursor position. Not implemented
2905 for X frames. */
2906
2907 static void
2908 x_delete_glyphs (f, n)
2909 struct frame *f;
2910 register int n;
2911 {
2912 abort ();
2913 }
2914
2915
2916 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
2917 If they are <= 0, this is probably an error. */
2918
2919 void
2920 x_clear_area (dpy, window, x, y, width, height, exposures)
2921 Display *dpy;
2922 Window window;
2923 int x, y;
2924 int width, height;
2925 int exposures;
2926 {
2927 xassert (width > 0 && height > 0);
2928 XClearArea (dpy, window, x, y, width, height, exposures);
2929 }
2930
2931
2932 /* Clear an entire frame. */
2933
2934 static void
2935 x_clear_frame (struct frame *f)
2936 {
2937 /* Clearing the frame will erase any cursor, so mark them all as no
2938 longer visible. */
2939 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
2940 output_cursor.hpos = output_cursor.vpos = 0;
2941 output_cursor.x = -1;
2942
2943 /* We don't set the output cursor here because there will always
2944 follow an explicit cursor_to. */
2945 BLOCK_INPUT;
2946 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
2947
2948 /* We have to clear the scroll bars, too. If we have changed
2949 colors or something like that, then they should be notified. */
2950 x_scroll_bar_clear (f);
2951
2952 XFlush (FRAME_X_DISPLAY (f));
2953
2954 UNBLOCK_INPUT;
2955 }
2956
2957
2958 \f
2959 /* Invert the middle quarter of the frame for .15 sec. */
2960
2961 /* We use the select system call to do the waiting, so we have to make
2962 sure it's available. If it isn't, we just won't do visual bells. */
2963
2964 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
2965
2966
2967 /* Subtract the `struct timeval' values X and Y, storing the result in
2968 *RESULT. Return 1 if the difference is negative, otherwise 0. */
2969
2970 static int
2971 timeval_subtract (result, x, y)
2972 struct timeval *result, x, y;
2973 {
2974 /* Perform the carry for the later subtraction by updating y. This
2975 is safer because on some systems the tv_sec member is unsigned. */
2976 if (x.tv_usec < y.tv_usec)
2977 {
2978 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
2979 y.tv_usec -= 1000000 * nsec;
2980 y.tv_sec += nsec;
2981 }
2982
2983 if (x.tv_usec - y.tv_usec > 1000000)
2984 {
2985 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
2986 y.tv_usec += 1000000 * nsec;
2987 y.tv_sec -= nsec;
2988 }
2989
2990 /* Compute the time remaining to wait. tv_usec is certainly
2991 positive. */
2992 result->tv_sec = x.tv_sec - y.tv_sec;
2993 result->tv_usec = x.tv_usec - y.tv_usec;
2994
2995 /* Return indication of whether the result should be considered
2996 negative. */
2997 return x.tv_sec < y.tv_sec;
2998 }
2999
3000 void
3001 XTflash (f)
3002 struct frame *f;
3003 {
3004 BLOCK_INPUT;
3005
3006 {
3007 GC gc;
3008
3009 /* Create a GC that will use the GXxor function to flip foreground
3010 pixels into background pixels. */
3011 {
3012 XGCValues values;
3013
3014 values.function = GXxor;
3015 values.foreground = (FRAME_FOREGROUND_PIXEL (f)
3016 ^ FRAME_BACKGROUND_PIXEL (f));
3017
3018 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3019 GCFunction | GCForeground, &values);
3020 }
3021
3022 {
3023 /* Get the height not including a menu bar widget. */
3024 int height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, FRAME_LINES (f));
3025 /* Height of each line to flash. */
3026 int flash_height = FRAME_LINE_HEIGHT (f);
3027 /* These will be the left and right margins of the rectangles. */
3028 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
3029 int flash_right = FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
3030
3031 int width;
3032
3033 /* Don't flash the area between a scroll bar and the frame
3034 edge it is next to. */
3035 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
3036 {
3037 case vertical_scroll_bar_left:
3038 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
3039 break;
3040
3041 case vertical_scroll_bar_right:
3042 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
3043 break;
3044
3045 default:
3046 break;
3047 }
3048
3049 width = flash_right - flash_left;
3050
3051 /* If window is tall, flash top and bottom line. */
3052 if (height > 3 * FRAME_LINE_HEIGHT (f))
3053 {
3054 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3055 flash_left,
3056 (FRAME_INTERNAL_BORDER_WIDTH (f)
3057 + FRAME_TOOL_BAR_LINES (f) * FRAME_LINE_HEIGHT (f)),
3058 width, flash_height);
3059 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3060 flash_left,
3061 (height - flash_height
3062 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3063 width, flash_height);
3064 }
3065 else
3066 /* If it is short, flash it all. */
3067 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3068 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3069 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3070
3071 x_flush (f);
3072
3073 {
3074 struct timeval wakeup;
3075
3076 EMACS_GET_TIME (wakeup);
3077
3078 /* Compute time to wait until, propagating carry from usecs. */
3079 wakeup.tv_usec += 150000;
3080 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
3081 wakeup.tv_usec %= 1000000;
3082
3083 /* Keep waiting until past the time wakeup or any input gets
3084 available. */
3085 while (! detect_input_pending ())
3086 {
3087 struct timeval current;
3088 struct timeval timeout;
3089
3090 EMACS_GET_TIME (current);
3091
3092 /* Break if result would be negative. */
3093 if (timeval_subtract (&current, wakeup, current))
3094 break;
3095
3096 /* How long `select' should wait. */
3097 timeout.tv_sec = 0;
3098 timeout.tv_usec = 10000;
3099
3100 /* Try to wait that long--but we might wake up sooner. */
3101 select (0, NULL, NULL, NULL, &timeout);
3102 }
3103 }
3104
3105 /* If window is tall, flash top and bottom line. */
3106 if (height > 3 * FRAME_LINE_HEIGHT (f))
3107 {
3108 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3109 flash_left,
3110 (FRAME_INTERNAL_BORDER_WIDTH (f)
3111 + FRAME_TOOL_BAR_LINES (f) * FRAME_LINE_HEIGHT (f)),
3112 width, flash_height);
3113 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3114 flash_left,
3115 (height - flash_height
3116 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3117 width, flash_height);
3118 }
3119 else
3120 /* If it is short, flash it all. */
3121 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3122 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3123 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3124
3125 XFreeGC (FRAME_X_DISPLAY (f), gc);
3126 x_flush (f);
3127 }
3128 }
3129
3130 UNBLOCK_INPUT;
3131 }
3132
3133 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
3134
3135
3136 static void
3137 XTtoggle_invisible_pointer (f, invisible)
3138 FRAME_PTR f;
3139 int invisible;
3140 {
3141 BLOCK_INPUT;
3142 if (invisible)
3143 {
3144 if (FRAME_X_DISPLAY_INFO (f)->invisible_cursor != 0)
3145 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3146 FRAME_X_DISPLAY_INFO (f)->invisible_cursor);
3147 }
3148 else
3149 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3150 f->output_data.x->current_cursor);
3151 f->pointer_invisible = invisible;
3152 UNBLOCK_INPUT;
3153 }
3154
3155
3156 /* Make audible bell. */
3157
3158 void
3159 XTring_bell ()
3160 {
3161 struct frame *f = SELECTED_FRAME ();
3162
3163 if (FRAME_X_DISPLAY (f))
3164 {
3165 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
3166 if (visible_bell)
3167 XTflash (f);
3168 else
3169 #endif
3170 {
3171 BLOCK_INPUT;
3172 XBell (FRAME_X_DISPLAY (f), 0);
3173 XFlush (FRAME_X_DISPLAY (f));
3174 UNBLOCK_INPUT;
3175 }
3176 }
3177 }
3178
3179 \f
3180 /* Specify how many text lines, from the top of the window,
3181 should be affected by insert-lines and delete-lines operations.
3182 This, and those operations, are used only within an update
3183 that is bounded by calls to x_update_begin and x_update_end. */
3184
3185 static void
3186 XTset_terminal_window (n)
3187 register int n;
3188 {
3189 /* This function intentionally left blank. */
3190 }
3191
3192
3193 \f
3194 /***********************************************************************
3195 Line Dance
3196 ***********************************************************************/
3197
3198 /* Perform an insert-lines or delete-lines operation, inserting N
3199 lines or deleting -N lines at vertical position VPOS. */
3200
3201 static void
3202 x_ins_del_lines (f, vpos, n)
3203 struct frame *f;
3204 int vpos, n;
3205 {
3206 abort ();
3207 }
3208
3209
3210 /* Scroll part of the display as described by RUN. */
3211
3212 static void
3213 x_scroll_run (w, run)
3214 struct window *w;
3215 struct run *run;
3216 {
3217 struct frame *f = XFRAME (w->frame);
3218 int x, y, width, height, from_y, to_y, bottom_y;
3219
3220 /* Get frame-relative bounding box of the text display area of W,
3221 without mode lines. Include in this box the left and right
3222 fringe of W. */
3223 window_box (w, -1, &x, &y, &width, &height);
3224
3225 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
3226 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
3227 bottom_y = y + height;
3228
3229 if (to_y < from_y)
3230 {
3231 /* Scrolling up. Make sure we don't copy part of the mode
3232 line at the bottom. */
3233 if (from_y + run->height > bottom_y)
3234 height = bottom_y - from_y;
3235 else
3236 height = run->height;
3237 }
3238 else
3239 {
3240 /* Scolling down. Make sure we don't copy over the mode line.
3241 at the bottom. */
3242 if (to_y + run->height > bottom_y)
3243 height = bottom_y - to_y;
3244 else
3245 height = run->height;
3246 }
3247
3248 BLOCK_INPUT;
3249
3250 /* Cursor off. Will be switched on again in x_update_window_end. */
3251 updated_window = w;
3252 x_clear_cursor (w);
3253
3254 XCopyArea (FRAME_X_DISPLAY (f),
3255 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
3256 f->output_data.x->normal_gc,
3257 x, from_y,
3258 width, height,
3259 x, to_y);
3260
3261 UNBLOCK_INPUT;
3262 }
3263
3264
3265 \f
3266 /***********************************************************************
3267 Exposure Events
3268 ***********************************************************************/
3269
3270 \f
3271 static void
3272 frame_highlight (f)
3273 struct frame *f;
3274 {
3275 /* We used to only do this if Vx_no_window_manager was non-nil, but
3276 the ICCCM (section 4.1.6) says that the window's border pixmap
3277 and border pixel are window attributes which are "private to the
3278 client", so we can always change it to whatever we want. */
3279 BLOCK_INPUT;
3280 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3281 f->output_data.x->border_pixel);
3282 UNBLOCK_INPUT;
3283 x_update_cursor (f, 1);
3284 x_set_frame_alpha (f);
3285 }
3286
3287 static void
3288 frame_unhighlight (f)
3289 struct frame *f;
3290 {
3291 /* We used to only do this if Vx_no_window_manager was non-nil, but
3292 the ICCCM (section 4.1.6) says that the window's border pixmap
3293 and border pixel are window attributes which are "private to the
3294 client", so we can always change it to whatever we want. */
3295 BLOCK_INPUT;
3296 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3297 f->output_data.x->border_tile);
3298 UNBLOCK_INPUT;
3299 x_update_cursor (f, 1);
3300 x_set_frame_alpha (f);
3301 }
3302
3303 /* The focus has changed. Update the frames as necessary to reflect
3304 the new situation. Note that we can't change the selected frame
3305 here, because the Lisp code we are interrupting might become confused.
3306 Each event gets marked with the frame in which it occurred, so the
3307 Lisp code can tell when the switch took place by examining the events. */
3308
3309 static void
3310 x_new_focus_frame (dpyinfo, frame)
3311 struct x_display_info *dpyinfo;
3312 struct frame *frame;
3313 {
3314 struct frame *old_focus = dpyinfo->x_focus_frame;
3315
3316 if (frame != dpyinfo->x_focus_frame)
3317 {
3318 /* Set this before calling other routines, so that they see
3319 the correct value of x_focus_frame. */
3320 dpyinfo->x_focus_frame = frame;
3321
3322 if (old_focus && old_focus->auto_lower)
3323 x_lower_frame (old_focus);
3324
3325 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
3326 pending_autoraise_frame = dpyinfo->x_focus_frame;
3327 else
3328 pending_autoraise_frame = 0;
3329 }
3330
3331 x_frame_rehighlight (dpyinfo);
3332 }
3333
3334 /* Handle FocusIn and FocusOut state changes for FRAME.
3335 If FRAME has focus and there exists more than one frame, puts
3336 a FOCUS_IN_EVENT into *BUFP. */
3337
3338 static void
3339 x_focus_changed (type, state, dpyinfo, frame, bufp)
3340 int type;
3341 int state;
3342 struct x_display_info *dpyinfo;
3343 struct frame *frame;
3344 struct input_event *bufp;
3345 {
3346 if (type == FocusIn)
3347 {
3348 if (dpyinfo->x_focus_event_frame != frame)
3349 {
3350 x_new_focus_frame (dpyinfo, frame);
3351 dpyinfo->x_focus_event_frame = frame;
3352
3353 /* Don't stop displaying the initial startup message
3354 for a switch-frame event we don't need. */
3355 if (NILP (Vterminal_frame)
3356 && CONSP (Vframe_list)
3357 && !NILP (XCDR (Vframe_list)))
3358 {
3359 bufp->kind = FOCUS_IN_EVENT;
3360 XSETFRAME (bufp->frame_or_window, frame);
3361 }
3362 }
3363
3364 frame->output_data.x->focus_state |= state;
3365
3366 #ifdef HAVE_X_I18N
3367 if (FRAME_XIC (frame))
3368 XSetICFocus (FRAME_XIC (frame));
3369 #endif
3370 }
3371 else if (type == FocusOut)
3372 {
3373 frame->output_data.x->focus_state &= ~state;
3374
3375 if (dpyinfo->x_focus_event_frame == frame)
3376 {
3377 dpyinfo->x_focus_event_frame = 0;
3378 x_new_focus_frame (dpyinfo, 0);
3379 }
3380
3381 #ifdef HAVE_X_I18N
3382 if (FRAME_XIC (frame))
3383 XUnsetICFocus (FRAME_XIC (frame));
3384 #endif
3385 if (frame->pointer_invisible)
3386 XTtoggle_invisible_pointer (frame, 0);
3387 }
3388 }
3389
3390 /* The focus may have changed. Figure out if it is a real focus change,
3391 by checking both FocusIn/Out and Enter/LeaveNotify events.
3392
3393 Returns FOCUS_IN_EVENT event in *BUFP. */
3394
3395 static void
3396 x_detect_focus_change (dpyinfo, event, bufp)
3397 struct x_display_info *dpyinfo;
3398 XEvent *event;
3399 struct input_event *bufp;
3400 {
3401 struct frame *frame;
3402
3403 frame = x_any_window_to_frame (dpyinfo, event->xany.window);
3404 if (! frame)
3405 return;
3406
3407 switch (event->type)
3408 {
3409 case EnterNotify:
3410 case LeaveNotify:
3411 {
3412 struct frame *focus_frame = dpyinfo->x_focus_event_frame;
3413 int focus_state
3414 = focus_frame ? focus_frame->output_data.x->focus_state : 0;
3415
3416 if (event->xcrossing.detail != NotifyInferior
3417 && event->xcrossing.focus
3418 && ! (focus_state & FOCUS_EXPLICIT))
3419 x_focus_changed ((event->type == EnterNotify ? FocusIn : FocusOut),
3420 FOCUS_IMPLICIT,
3421 dpyinfo, frame, bufp);
3422 }
3423 break;
3424
3425 case FocusIn:
3426 case FocusOut:
3427 x_focus_changed (event->type,
3428 (event->xfocus.detail == NotifyPointer ?
3429 FOCUS_IMPLICIT : FOCUS_EXPLICIT),
3430 dpyinfo, frame, bufp);
3431 break;
3432
3433 case ClientMessage:
3434 if (event->xclient.message_type == dpyinfo->Xatom_XEMBED)
3435 {
3436 enum xembed_message msg = event->xclient.data.l[1];
3437 x_focus_changed ((msg == XEMBED_FOCUS_IN ? FocusIn : FocusOut),
3438 FOCUS_EXPLICIT, dpyinfo, frame, bufp);
3439 }
3440 break;
3441 }
3442 }
3443
3444
3445 /* Handle an event saying the mouse has moved out of an Emacs frame. */
3446
3447 void
3448 x_mouse_leave (dpyinfo)
3449 struct x_display_info *dpyinfo;
3450 {
3451 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
3452 }
3453
3454 /* The focus has changed, or we have redirected a frame's focus to
3455 another frame (this happens when a frame uses a surrogate
3456 mini-buffer frame). Shift the highlight as appropriate.
3457
3458 The FRAME argument doesn't necessarily have anything to do with which
3459 frame is being highlighted or un-highlighted; we only use it to find
3460 the appropriate X display info. */
3461
3462 static void
3463 XTframe_rehighlight (frame)
3464 struct frame *frame;
3465 {
3466 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
3467 }
3468
3469 static void
3470 x_frame_rehighlight (dpyinfo)
3471 struct x_display_info *dpyinfo;
3472 {
3473 struct frame *old_highlight = dpyinfo->x_highlight_frame;
3474
3475 if (dpyinfo->x_focus_frame)
3476 {
3477 dpyinfo->x_highlight_frame
3478 = ((FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
3479 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
3480 : dpyinfo->x_focus_frame);
3481 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
3482 {
3483 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
3484 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
3485 }
3486 }
3487 else
3488 dpyinfo->x_highlight_frame = 0;
3489
3490 if (dpyinfo->x_highlight_frame != old_highlight)
3491 {
3492 if (old_highlight)
3493 frame_unhighlight (old_highlight);
3494 if (dpyinfo->x_highlight_frame)
3495 frame_highlight (dpyinfo->x_highlight_frame);
3496 }
3497 }
3498
3499
3500 \f
3501 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
3502
3503 /* Initialize mode_switch_bit and modifier_meaning. */
3504 static void
3505 x_find_modifier_meanings (dpyinfo)
3506 struct x_display_info *dpyinfo;
3507 {
3508 int min_code, max_code;
3509 KeySym *syms;
3510 int syms_per_code;
3511 XModifierKeymap *mods;
3512
3513 dpyinfo->meta_mod_mask = 0;
3514 dpyinfo->shift_lock_mask = 0;
3515 dpyinfo->alt_mod_mask = 0;
3516 dpyinfo->super_mod_mask = 0;
3517 dpyinfo->hyper_mod_mask = 0;
3518
3519 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
3520
3521 syms = XGetKeyboardMapping (dpyinfo->display,
3522 min_code, max_code - min_code + 1,
3523 &syms_per_code);
3524 mods = XGetModifierMapping (dpyinfo->display);
3525
3526 /* Scan the modifier table to see which modifier bits the Meta and
3527 Alt keysyms are on. */
3528 {
3529 int row, col; /* The row and column in the modifier table. */
3530 int found_alt_or_meta;
3531
3532 for (row = 3; row < 8; row++)
3533 {
3534 found_alt_or_meta = 0;
3535 for (col = 0; col < mods->max_keypermod; col++)
3536 {
3537 KeyCode code = mods->modifiermap[(row * mods->max_keypermod) + col];
3538
3539 /* Zeroes are used for filler. Skip them. */
3540 if (code == 0)
3541 continue;
3542
3543 /* Are any of this keycode's keysyms a meta key? */
3544 {
3545 int code_col;
3546
3547 for (code_col = 0; code_col < syms_per_code; code_col++)
3548 {
3549 int sym = syms[((code - min_code) * syms_per_code) + code_col];
3550
3551 switch (sym)
3552 {
3553 case XK_Meta_L:
3554 case XK_Meta_R:
3555 found_alt_or_meta = 1;
3556 dpyinfo->meta_mod_mask |= (1 << row);
3557 break;
3558
3559 case XK_Alt_L:
3560 case XK_Alt_R:
3561 found_alt_or_meta = 1;
3562 dpyinfo->alt_mod_mask |= (1 << row);
3563 break;
3564
3565 case XK_Hyper_L:
3566 case XK_Hyper_R:
3567 if (!found_alt_or_meta)
3568 dpyinfo->hyper_mod_mask |= (1 << row);
3569 code_col = syms_per_code;
3570 col = mods->max_keypermod;
3571 break;
3572
3573 case XK_Super_L:
3574 case XK_Super_R:
3575 if (!found_alt_or_meta)
3576 dpyinfo->super_mod_mask |= (1 << row);
3577 code_col = syms_per_code;
3578 col = mods->max_keypermod;
3579 break;
3580
3581 case XK_Shift_Lock:
3582 /* Ignore this if it's not on the lock modifier. */
3583 if (!found_alt_or_meta && ((1 << row) == LockMask))
3584 dpyinfo->shift_lock_mask = LockMask;
3585 code_col = syms_per_code;
3586 col = mods->max_keypermod;
3587 break;
3588 }
3589 }
3590 }
3591 }
3592 }
3593 }
3594
3595 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
3596 if (! dpyinfo->meta_mod_mask)
3597 {
3598 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
3599 dpyinfo->alt_mod_mask = 0;
3600 }
3601
3602 /* If some keys are both alt and meta,
3603 make them just meta, not alt. */
3604 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
3605 {
3606 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
3607 }
3608
3609 XFree ((char *) syms);
3610 XFreeModifiermap (mods);
3611 }
3612
3613 /* Convert between the modifier bits X uses and the modifier bits
3614 Emacs uses. */
3615
3616 unsigned int
3617 x_x_to_emacs_modifiers (dpyinfo, state)
3618 struct x_display_info *dpyinfo;
3619 unsigned int state;
3620 {
3621 EMACS_UINT mod_meta = meta_modifier;
3622 EMACS_UINT mod_alt = alt_modifier;
3623 EMACS_UINT mod_hyper = hyper_modifier;
3624 EMACS_UINT mod_super = super_modifier;
3625 Lisp_Object tem;
3626
3627 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3628 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
3629 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3630 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
3631 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3632 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
3633 tem = Fget (Vx_super_keysym, Qmodifier_value);
3634 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
3635
3636
3637 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
3638 | ((state & ControlMask) ? ctrl_modifier : 0)
3639 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
3640 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
3641 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
3642 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
3643 }
3644
3645 static unsigned int
3646 x_emacs_to_x_modifiers (dpyinfo, state)
3647 struct x_display_info *dpyinfo;
3648 unsigned int state;
3649 {
3650 EMACS_UINT mod_meta = meta_modifier;
3651 EMACS_UINT mod_alt = alt_modifier;
3652 EMACS_UINT mod_hyper = hyper_modifier;
3653 EMACS_UINT mod_super = super_modifier;
3654
3655 Lisp_Object tem;
3656
3657 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3658 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
3659 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3660 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
3661 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3662 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
3663 tem = Fget (Vx_super_keysym, Qmodifier_value);
3664 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
3665
3666
3667 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
3668 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
3669 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
3670 | ((state & shift_modifier) ? ShiftMask : 0)
3671 | ((state & ctrl_modifier) ? ControlMask : 0)
3672 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
3673 }
3674
3675 /* Convert a keysym to its name. */
3676
3677 char *
3678 x_get_keysym_name (keysym)
3679 KeySym keysym;
3680 {
3681 char *value;
3682
3683 BLOCK_INPUT;
3684 value = XKeysymToString (keysym);
3685 UNBLOCK_INPUT;
3686
3687 return value;
3688 }
3689
3690
3691 \f
3692 /* Mouse clicks and mouse movement. Rah. */
3693
3694 /* Prepare a mouse-event in *RESULT for placement in the input queue.
3695
3696 If the event is a button press, then note that we have grabbed
3697 the mouse. */
3698
3699 static Lisp_Object
3700 construct_mouse_click (result, event, f)
3701 struct input_event *result;
3702 XButtonEvent *event;
3703 struct frame *f;
3704 {
3705 /* Make the event type NO_EVENT; we'll change that when we decide
3706 otherwise. */
3707 result->kind = MOUSE_CLICK_EVENT;
3708 result->code = event->button - Button1;
3709 result->timestamp = event->time;
3710 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
3711 event->state)
3712 | (event->type == ButtonRelease
3713 ? up_modifier
3714 : down_modifier));
3715
3716 XSETINT (result->x, event->x);
3717 XSETINT (result->y, event->y);
3718 XSETFRAME (result->frame_or_window, f);
3719 result->arg = Qnil;
3720 return Qnil;
3721 }
3722
3723 \f
3724 /* Function to report a mouse movement to the mainstream Emacs code.
3725 The input handler calls this.
3726
3727 We have received a mouse movement event, which is given in *event.
3728 If the mouse is over a different glyph than it was last time, tell
3729 the mainstream emacs code by setting mouse_moved. If not, ask for
3730 another motion event, so we can check again the next time it moves. */
3731
3732 static XMotionEvent last_mouse_motion_event;
3733 static Lisp_Object last_mouse_motion_frame;
3734
3735 static int
3736 note_mouse_movement (frame, event)
3737 FRAME_PTR frame;
3738 XMotionEvent *event;
3739 {
3740 last_mouse_movement_time = event->time;
3741 last_mouse_motion_event = *event;
3742 XSETFRAME (last_mouse_motion_frame, frame);
3743
3744 if (!FRAME_X_OUTPUT (frame))
3745 return 0;
3746
3747 if (event->window != FRAME_X_WINDOW (frame))
3748 {
3749 frame->mouse_moved = 1;
3750 last_mouse_scroll_bar = Qnil;
3751 note_mouse_highlight (frame, -1, -1);
3752 last_mouse_glyph_frame = 0;
3753 return 1;
3754 }
3755
3756
3757 /* Has the mouse moved off the glyph it was on at the last sighting? */
3758 if (frame != last_mouse_glyph_frame
3759 || event->x < last_mouse_glyph.x
3760 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
3761 || event->y < last_mouse_glyph.y
3762 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
3763 {
3764 frame->mouse_moved = 1;
3765 last_mouse_scroll_bar = Qnil;
3766 note_mouse_highlight (frame, event->x, event->y);
3767 /* Remember which glyph we're now on. */
3768 remember_mouse_glyph (frame, event->x, event->y, &last_mouse_glyph);
3769 last_mouse_glyph_frame = frame;
3770 return 1;
3771 }
3772
3773 return 0;
3774 }
3775
3776 \f
3777 /************************************************************************
3778 Mouse Face
3779 ************************************************************************/
3780
3781 static void
3782 redo_mouse_highlight ()
3783 {
3784 if (!NILP (last_mouse_motion_frame)
3785 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
3786 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
3787 last_mouse_motion_event.x,
3788 last_mouse_motion_event.y);
3789 }
3790
3791
3792
3793 /* Return the current position of the mouse.
3794 *FP should be a frame which indicates which display to ask about.
3795
3796 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
3797 and *PART to the frame, window, and scroll bar part that the mouse
3798 is over. Set *X and *Y to the portion and whole of the mouse's
3799 position on the scroll bar.
3800
3801 If the mouse movement started elsewhere, set *FP to the frame the
3802 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
3803 the mouse is over.
3804
3805 Set *TIME to the server time-stamp for the time at which the mouse
3806 was at this position.
3807
3808 Don't store anything if we don't have a valid set of values to report.
3809
3810 This clears the mouse_moved flag, so we can wait for the next mouse
3811 movement. */
3812
3813 static void
3814 XTmouse_position (fp, insist, bar_window, part, x, y, time)
3815 FRAME_PTR *fp;
3816 int insist;
3817 Lisp_Object *bar_window;
3818 enum scroll_bar_part *part;
3819 Lisp_Object *x, *y;
3820 unsigned long *time;
3821 {
3822 FRAME_PTR f1;
3823
3824 BLOCK_INPUT;
3825
3826 if (! NILP (last_mouse_scroll_bar) && insist == 0)
3827 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
3828 else
3829 {
3830 Window root;
3831 int root_x, root_y;
3832
3833 Window dummy_window;
3834 int dummy;
3835
3836 Lisp_Object frame, tail;
3837
3838 /* Clear the mouse-moved flag for every frame on this display. */
3839 FOR_EACH_FRAME (tail, frame)
3840 if (FRAME_X_P (XFRAME (frame))
3841 && FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
3842 XFRAME (frame)->mouse_moved = 0;
3843
3844 last_mouse_scroll_bar = Qnil;
3845
3846 /* Figure out which root window we're on. */
3847 XQueryPointer (FRAME_X_DISPLAY (*fp),
3848 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
3849
3850 /* The root window which contains the pointer. */
3851 &root,
3852
3853 /* Trash which we can't trust if the pointer is on
3854 a different screen. */
3855 &dummy_window,
3856
3857 /* The position on that root window. */
3858 &root_x, &root_y,
3859
3860 /* More trash we can't trust. */
3861 &dummy, &dummy,
3862
3863 /* Modifier keys and pointer buttons, about which
3864 we don't care. */
3865 (unsigned int *) &dummy);
3866
3867 /* Now we have a position on the root; find the innermost window
3868 containing the pointer. */
3869 {
3870 Window win, child;
3871 int win_x, win_y;
3872 int parent_x = 0, parent_y = 0;
3873
3874 win = root;
3875
3876 /* XTranslateCoordinates can get errors if the window
3877 structure is changing at the same time this function
3878 is running. So at least we must not crash from them. */
3879
3880 x_catch_errors (FRAME_X_DISPLAY (*fp));
3881
3882 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
3883 && FRAME_LIVE_P (last_mouse_frame))
3884 {
3885 /* If mouse was grabbed on a frame, give coords for that frame
3886 even if the mouse is now outside it. */
3887 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
3888
3889 /* From-window, to-window. */
3890 root, FRAME_X_WINDOW (last_mouse_frame),
3891
3892 /* From-position, to-position. */
3893 root_x, root_y, &win_x, &win_y,
3894
3895 /* Child of win. */
3896 &child);
3897 f1 = last_mouse_frame;
3898 }
3899 else
3900 {
3901 while (1)
3902 {
3903 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
3904
3905 /* From-window, to-window. */
3906 root, win,
3907
3908 /* From-position, to-position. */
3909 root_x, root_y, &win_x, &win_y,
3910
3911 /* Child of win. */
3912 &child);
3913
3914 if (child == None || child == win)
3915 break;
3916 #ifdef USE_GTK
3917 /* We don't wan't to know the innermost window. We
3918 want the edit window. For non-Gtk+ the innermost
3919 window is the edit window. For Gtk+ it might not
3920 be. It might be the tool bar for example. */
3921 if (x_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win))
3922 break;
3923 #endif
3924 win = child;
3925 parent_x = win_x;
3926 parent_y = win_y;
3927 }
3928
3929 /* Now we know that:
3930 win is the innermost window containing the pointer
3931 (XTC says it has no child containing the pointer),
3932 win_x and win_y are the pointer's position in it
3933 (XTC did this the last time through), and
3934 parent_x and parent_y are the pointer's position in win's parent.
3935 (They are what win_x and win_y were when win was child.
3936 If win is the root window, it has no parent, and
3937 parent_{x,y} are invalid, but that's okay, because we'll
3938 never use them in that case.) */
3939
3940 #ifdef USE_GTK
3941 /* We don't wan't to know the innermost window. We
3942 want the edit window. */
3943 f1 = x_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
3944 #else
3945 /* Is win one of our frames? */
3946 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
3947 #endif
3948
3949 #ifdef USE_X_TOOLKIT
3950 /* If we end up with the menu bar window, say it's not
3951 on the frame. */
3952 if (f1 != NULL
3953 && f1->output_data.x->menubar_widget
3954 && win == XtWindow (f1->output_data.x->menubar_widget))
3955 f1 = NULL;
3956 #endif /* USE_X_TOOLKIT */
3957 }
3958
3959 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
3960 f1 = 0;
3961
3962 x_uncatch_errors ();
3963
3964 /* If not, is it one of our scroll bars? */
3965 if (! f1)
3966 {
3967 struct scroll_bar *bar;
3968
3969 bar = x_window_to_scroll_bar (FRAME_X_DISPLAY (*fp), win);
3970
3971 if (bar)
3972 {
3973 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3974 win_x = parent_x;
3975 win_y = parent_y;
3976 }
3977 }
3978
3979 if (f1 == 0 && insist > 0)
3980 f1 = SELECTED_FRAME ();
3981
3982 if (f1)
3983 {
3984 /* Ok, we found a frame. Store all the values.
3985 last_mouse_glyph is a rectangle used to reduce the
3986 generation of mouse events. To not miss any motion
3987 events, we must divide the frame into rectangles of the
3988 size of the smallest character that could be displayed
3989 on it, i.e. into the same rectangles that matrices on
3990 the frame are divided into. */
3991
3992 remember_mouse_glyph (f1, win_x, win_y, &last_mouse_glyph);
3993 last_mouse_glyph_frame = f1;
3994
3995 *bar_window = Qnil;
3996 *part = 0;
3997 *fp = f1;
3998 XSETINT (*x, win_x);
3999 XSETINT (*y, win_y);
4000 *time = last_mouse_movement_time;
4001 }
4002 }
4003 }
4004
4005 UNBLOCK_INPUT;
4006 }
4007
4008
4009 \f
4010 /***********************************************************************
4011 Scroll bars
4012 ***********************************************************************/
4013
4014 /* Scroll bar support. */
4015
4016 /* Given an X window ID and a DISPLAY, find the struct scroll_bar which
4017 manages it.
4018 This can be called in GC, so we have to make sure to strip off mark
4019 bits. */
4020
4021 static struct scroll_bar *
4022 x_window_to_scroll_bar (display, window_id)
4023 Display *display;
4024 Window window_id;
4025 {
4026 Lisp_Object tail;
4027
4028 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
4029 window_id = (Window) xg_get_scroll_id_for_window (display, window_id);
4030 #endif /* USE_GTK && USE_TOOLKIT_SCROLL_BARS */
4031
4032 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
4033 {
4034 Lisp_Object frame, bar, condemned;
4035
4036 frame = XCAR (tail);
4037 /* All elements of Vframe_list should be frames. */
4038 if (! FRAMEP (frame))
4039 abort ();
4040
4041 if (! FRAME_X_P (XFRAME (frame)))
4042 continue;
4043
4044 /* Scan this frame's scroll bar list for a scroll bar with the
4045 right window ID. */
4046 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
4047 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
4048 /* This trick allows us to search both the ordinary and
4049 condemned scroll bar lists with one loop. */
4050 ! NILP (bar) || (bar = condemned,
4051 condemned = Qnil,
4052 ! NILP (bar));
4053 bar = XSCROLL_BAR (bar)->next)
4054 if (XSCROLL_BAR (bar)->x_window == window_id &&
4055 FRAME_X_DISPLAY (XFRAME (frame)) == display)
4056 return XSCROLL_BAR (bar);
4057 }
4058
4059 return 0;
4060 }
4061
4062
4063 #if defined USE_LUCID
4064
4065 /* Return the Lucid menu bar WINDOW is part of. Return null
4066 if WINDOW is not part of a menu bar. */
4067
4068 static Widget
4069 x_window_to_menu_bar (window)
4070 Window window;
4071 {
4072 Lisp_Object tail;
4073
4074 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
4075 {
4076 if (FRAME_X_P (XFRAME (XCAR (tail))))
4077 {
4078 Lisp_Object frame = XCAR (tail);
4079 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
4080
4081 if (menu_bar && xlwmenu_window_p (menu_bar, window))
4082 return menu_bar;
4083 }
4084 }
4085
4086 return NULL;
4087 }
4088
4089 #endif /* USE_LUCID */
4090
4091 \f
4092 /************************************************************************
4093 Toolkit scroll bars
4094 ************************************************************************/
4095
4096 #ifdef USE_TOOLKIT_SCROLL_BARS
4097
4098 static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
4099 static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
4100 static void x_create_toolkit_scroll_bar P_ ((struct frame *,
4101 struct scroll_bar *));
4102 static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
4103 int, int, int));
4104
4105
4106 /* Lisp window being scrolled. Set when starting to interact with
4107 a toolkit scroll bar, reset to nil when ending the interaction. */
4108
4109 static Lisp_Object window_being_scrolled;
4110
4111 /* Last scroll bar part sent in xm_scroll_callback. */
4112
4113 static int last_scroll_bar_part;
4114
4115 /* Whether this is an Xaw with arrow-scrollbars. This should imply
4116 that movements of 1/20 of the screen size are mapped to up/down. */
4117
4118 #ifndef USE_GTK
4119 /* Id of action hook installed for scroll bars. */
4120
4121 static XtActionHookId action_hook_id;
4122
4123 static Boolean xaw3d_arrow_scroll;
4124
4125 /* Whether the drag scrolling maintains the mouse at the top of the
4126 thumb. If not, resizing the thumb needs to be done more carefully
4127 to avoid jerkyness. */
4128
4129 static Boolean xaw3d_pick_top;
4130
4131 /* Action hook installed via XtAppAddActionHook when toolkit scroll
4132 bars are used.. The hook is responsible for detecting when
4133 the user ends an interaction with the scroll bar, and generates
4134 a `end-scroll' SCROLL_BAR_CLICK_EVENT' event if so. */
4135
4136 static void
4137 xt_action_hook (widget, client_data, action_name, event, params,
4138 num_params)
4139 Widget widget;
4140 XtPointer client_data;
4141 String action_name;
4142 XEvent *event;
4143 String *params;
4144 Cardinal *num_params;
4145 {
4146 int scroll_bar_p;
4147 char *end_action;
4148
4149 #ifdef USE_MOTIF
4150 scroll_bar_p = XmIsScrollBar (widget);
4151 end_action = "Release";
4152 #else /* !USE_MOTIF i.e. use Xaw */
4153 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
4154 end_action = "EndScroll";
4155 #endif /* USE_MOTIF */
4156
4157 if (scroll_bar_p
4158 && strcmp (action_name, end_action) == 0
4159 && WINDOWP (window_being_scrolled))
4160 {
4161 struct window *w;
4162
4163 x_send_scroll_bar_event (window_being_scrolled,
4164 scroll_bar_end_scroll, 0, 0);
4165 w = XWINDOW (window_being_scrolled);
4166
4167 if (!NILP (XSCROLL_BAR (w->vertical_scroll_bar)->dragging))
4168 {
4169 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
4170 /* The thumb size is incorrect while dragging: fix it. */
4171 set_vertical_scroll_bar (w);
4172 }
4173 window_being_scrolled = Qnil;
4174 last_scroll_bar_part = -1;
4175
4176 /* Xt timeouts no longer needed. */
4177 toolkit_scroll_bar_interaction = 0;
4178 }
4179 }
4180 #endif /* not USE_GTK */
4181
4182 /* A vector of windows used for communication between
4183 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
4184
4185 static struct window **scroll_bar_windows;
4186 static int scroll_bar_windows_size;
4187
4188
4189 /* Send a client message with message type Xatom_Scrollbar for a
4190 scroll action to the frame of WINDOW. PART is a value identifying
4191 the part of the scroll bar that was clicked on. PORTION is the
4192 amount to scroll of a whole of WHOLE. */
4193
4194 static void
4195 x_send_scroll_bar_event (window, part, portion, whole)
4196 Lisp_Object window;
4197 int part, portion, whole;
4198 {
4199 XEvent event;
4200 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
4201 struct window *w = XWINDOW (window);
4202 struct frame *f = XFRAME (w->frame);
4203 int i;
4204
4205 BLOCK_INPUT;
4206
4207 /* Construct a ClientMessage event to send to the frame. */
4208 ev->type = ClientMessage;
4209 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
4210 ev->display = FRAME_X_DISPLAY (f);
4211 ev->window = FRAME_X_WINDOW (f);
4212 ev->format = 32;
4213
4214 /* We can only transfer 32 bits in the XClientMessageEvent, which is
4215 not enough to store a pointer or Lisp_Object on a 64 bit system.
4216 So, store the window in scroll_bar_windows and pass the index
4217 into that array in the event. */
4218 for (i = 0; i < scroll_bar_windows_size; ++i)
4219 if (scroll_bar_windows[i] == NULL)
4220 break;
4221
4222 if (i == scroll_bar_windows_size)
4223 {
4224 int new_size = max (10, 2 * scroll_bar_windows_size);
4225 size_t nbytes = new_size * sizeof *scroll_bar_windows;
4226 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
4227
4228 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
4229 nbytes);
4230 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
4231 scroll_bar_windows_size = new_size;
4232 }
4233
4234 scroll_bar_windows[i] = w;
4235 ev->data.l[0] = (long) i;
4236 ev->data.l[1] = (long) part;
4237 ev->data.l[2] = (long) 0;
4238 ev->data.l[3] = (long) portion;
4239 ev->data.l[4] = (long) whole;
4240
4241 /* Make Xt timeouts work while the scroll bar is active. */
4242 toolkit_scroll_bar_interaction = 1;
4243 #ifdef USE_X_TOOLKIT
4244 x_activate_timeout_atimer ();
4245 #endif
4246
4247 /* Setting the event mask to zero means that the message will
4248 be sent to the client that created the window, and if that
4249 window no longer exists, no event will be sent. */
4250 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
4251 UNBLOCK_INPUT;
4252 }
4253
4254
4255 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
4256 in *IEVENT. */
4257
4258 static void
4259 x_scroll_bar_to_input_event (event, ievent)
4260 XEvent *event;
4261 struct input_event *ievent;
4262 {
4263 XClientMessageEvent *ev = (XClientMessageEvent *) event;
4264 Lisp_Object window;
4265 struct frame *f;
4266 struct window *w;
4267
4268 w = scroll_bar_windows[ev->data.l[0]];
4269 scroll_bar_windows[ev->data.l[0]] = NULL;
4270
4271 XSETWINDOW (window, w);
4272 f = XFRAME (w->frame);
4273
4274 ievent->kind = SCROLL_BAR_CLICK_EVENT;
4275 ievent->frame_or_window = window;
4276 ievent->arg = Qnil;
4277 #ifdef USE_GTK
4278 ievent->timestamp = CurrentTime;
4279 #else
4280 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
4281 #endif
4282 ievent->part = ev->data.l[1];
4283 ievent->code = ev->data.l[2];
4284 ievent->x = make_number ((int) ev->data.l[3]);
4285 ievent->y = make_number ((int) ev->data.l[4]);
4286 ievent->modifiers = 0;
4287 }
4288
4289
4290 #ifdef USE_MOTIF
4291
4292 /* Minimum and maximum values used for Motif scroll bars. */
4293
4294 #define XM_SB_MAX 10000000
4295
4296
4297 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
4298 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
4299 CALL_DATA is a pointer to a XmScrollBarCallbackStruct. */
4300
4301 static void
4302 xm_scroll_callback (widget, client_data, call_data)
4303 Widget widget;
4304 XtPointer client_data, call_data;
4305 {
4306 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4307 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
4308 int part = -1, whole = 0, portion = 0;
4309
4310 switch (cs->reason)
4311 {
4312 case XmCR_DECREMENT:
4313 bar->dragging = Qnil;
4314 part = scroll_bar_up_arrow;
4315 break;
4316
4317 case XmCR_INCREMENT:
4318 bar->dragging = Qnil;
4319 part = scroll_bar_down_arrow;
4320 break;
4321
4322 case XmCR_PAGE_DECREMENT:
4323 bar->dragging = Qnil;
4324 part = scroll_bar_above_handle;
4325 break;
4326
4327 case XmCR_PAGE_INCREMENT:
4328 bar->dragging = Qnil;
4329 part = scroll_bar_below_handle;
4330 break;
4331
4332 case XmCR_TO_TOP:
4333 bar->dragging = Qnil;
4334 part = scroll_bar_to_top;
4335 break;
4336
4337 case XmCR_TO_BOTTOM:
4338 bar->dragging = Qnil;
4339 part = scroll_bar_to_bottom;
4340 break;
4341
4342 case XmCR_DRAG:
4343 {
4344 int slider_size;
4345
4346 /* Get the slider size. */
4347 BLOCK_INPUT;
4348 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
4349 UNBLOCK_INPUT;
4350
4351 whole = XM_SB_MAX - slider_size;
4352 portion = min (cs->value, whole);
4353 part = scroll_bar_handle;
4354 bar->dragging = make_number (cs->value);
4355 }
4356 break;
4357
4358 case XmCR_VALUE_CHANGED:
4359 break;
4360 };
4361
4362 if (part >= 0)
4363 {
4364 window_being_scrolled = bar->window;
4365 last_scroll_bar_part = part;
4366 x_send_scroll_bar_event (bar->window, part, portion, whole);
4367 }
4368 }
4369
4370 #elif defined USE_GTK
4371
4372 /* Scroll bar callback for GTK scroll bars. WIDGET is the scroll
4373 bar widget. DATA is a pointer to the scroll_bar structure. */
4374
4375 static void
4376 xg_scroll_callback (widget, data)
4377 GtkRange *widget;
4378 gpointer data;
4379 {
4380 struct scroll_bar *bar = (struct scroll_bar *) data;
4381 gdouble previous;
4382 gdouble position;
4383 gdouble *p;
4384 int diff;
4385
4386 int part = -1, whole = 0, portion = 0;
4387 GtkAdjustment *adj = GTK_ADJUSTMENT (gtk_range_get_adjustment (widget));
4388
4389 position = gtk_adjustment_get_value (adj);
4390
4391 p = g_object_get_data (G_OBJECT (widget), XG_LAST_SB_DATA);
4392 if (! p)
4393 {
4394 p = (gdouble*) xmalloc (sizeof (gdouble));
4395 *p = XG_SB_MIN;
4396 g_object_set_data (G_OBJECT (widget), XG_LAST_SB_DATA, p);
4397 }
4398
4399 previous = *p;
4400 *p = position;
4401
4402 if (xg_ignore_gtk_scrollbar) return;
4403
4404 diff = (int) (position - previous);
4405
4406 if (diff == (int) adj->step_increment)
4407 {
4408 part = scroll_bar_down_arrow;
4409 bar->dragging = Qnil;
4410 }
4411 else if (-diff == (int) adj->step_increment)
4412 {
4413 part = scroll_bar_up_arrow;
4414 bar->dragging = Qnil;
4415 }
4416 else if (diff == (int) adj->page_increment)
4417 {
4418 part = scroll_bar_below_handle;
4419 bar->dragging = Qnil;
4420 }
4421 else if (-diff == (int) adj->page_increment)
4422 {
4423 part = scroll_bar_above_handle;
4424 bar->dragging = Qnil;
4425 }
4426 else
4427 {
4428 part = scroll_bar_handle;
4429 whole = adj->upper - adj->page_size;
4430 portion = min ((int)position, whole);
4431 bar->dragging = make_number ((int)portion);
4432 }
4433
4434 if (part >= 0)
4435 {
4436 window_being_scrolled = bar->window;
4437 last_scroll_bar_part = part;
4438 x_send_scroll_bar_event (bar->window, part, portion, whole);
4439 }
4440 }
4441
4442 #else /* not USE_GTK and not USE_MOTIF */
4443
4444 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
4445 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
4446 scroll bar struct. CALL_DATA is a pointer to a float saying where
4447 the thumb is. */
4448
4449 static void
4450 xaw_jump_callback (widget, client_data, call_data)
4451 Widget widget;
4452 XtPointer client_data, call_data;
4453 {
4454 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4455 float top = *(float *) call_data;
4456 float shown;
4457 int whole, portion, height;
4458 int part;
4459
4460 /* Get the size of the thumb, a value between 0 and 1. */
4461 BLOCK_INPUT;
4462 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
4463 UNBLOCK_INPUT;
4464
4465 whole = 10000000;
4466 portion = shown < 1 ? top * whole : 0;
4467
4468 if (shown < 1 && (eabs (top + shown - 1) < 1.0/height))
4469 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
4470 the bottom, so we force the scrolling whenever we see that we're
4471 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
4472 we try to ensure that we always stay two pixels away from the
4473 bottom). */
4474 part = scroll_bar_down_arrow;
4475 else
4476 part = scroll_bar_handle;
4477
4478 window_being_scrolled = bar->window;
4479 bar->dragging = make_number (portion);
4480 last_scroll_bar_part = part;
4481 x_send_scroll_bar_event (bar->window, part, portion, whole);
4482 }
4483
4484
4485 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
4486 i.e. line or page up or down. WIDGET is the Xaw scroll bar
4487 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
4488 the scroll bar. CALL_DATA is an integer specifying the action that
4489 has taken place. Its magnitude is in the range 0..height of the
4490 scroll bar. Negative values mean scroll towards buffer start.
4491 Values < height of scroll bar mean line-wise movement. */
4492
4493 static void
4494 xaw_scroll_callback (widget, client_data, call_data)
4495 Widget widget;
4496 XtPointer client_data, call_data;
4497 {
4498 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4499 /* The position really is stored cast to a pointer. */
4500 int position = (long) call_data;
4501 Dimension height;
4502 int part;
4503
4504 /* Get the height of the scroll bar. */
4505 BLOCK_INPUT;
4506 XtVaGetValues (widget, XtNheight, &height, NULL);
4507 UNBLOCK_INPUT;
4508
4509 if (eabs (position) >= height)
4510 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
4511
4512 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
4513 it maps line-movement to call_data = max(5, height/20). */
4514 else if (xaw3d_arrow_scroll && eabs (position) <= max (5, height / 20))
4515 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
4516 else
4517 part = scroll_bar_move_ratio;
4518
4519 window_being_scrolled = bar->window;
4520 bar->dragging = Qnil;
4521 last_scroll_bar_part = part;
4522 x_send_scroll_bar_event (bar->window, part, position, height);
4523 }
4524
4525 #endif /* not USE_GTK and not USE_MOTIF */
4526
4527 #define SCROLL_BAR_NAME "verticalScrollBar"
4528
4529 /* Create the widget for scroll bar BAR on frame F. Record the widget
4530 and X window of the scroll bar in BAR. */
4531
4532 #ifdef USE_GTK
4533 static void
4534 x_create_toolkit_scroll_bar (f, bar)
4535 struct frame *f;
4536 struct scroll_bar *bar;
4537 {
4538 char *scroll_bar_name = SCROLL_BAR_NAME;
4539
4540 BLOCK_INPUT;
4541 xg_create_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
4542 scroll_bar_name);
4543 UNBLOCK_INPUT;
4544 }
4545
4546 #else /* not USE_GTK */
4547
4548 static void
4549 x_create_toolkit_scroll_bar (f, bar)
4550 struct frame *f;
4551 struct scroll_bar *bar;
4552 {
4553 Window xwindow;
4554 Widget widget;
4555 Arg av[20];
4556 int ac = 0;
4557 char *scroll_bar_name = SCROLL_BAR_NAME;
4558 unsigned long pixel;
4559
4560 BLOCK_INPUT;
4561
4562 #ifdef USE_MOTIF
4563 /* Set resources. Create the widget. */
4564 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4565 XtSetArg (av[ac], XmNminimum, 0); ++ac;
4566 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
4567 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
4568 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
4569 XtSetArg (av[ac], XmNincrement, 1); ++ac;
4570 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
4571
4572 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4573 if (pixel != -1)
4574 {
4575 XtSetArg (av[ac], XmNforeground, pixel);
4576 ++ac;
4577 }
4578
4579 pixel = f->output_data.x->scroll_bar_background_pixel;
4580 if (pixel != -1)
4581 {
4582 XtSetArg (av[ac], XmNbackground, pixel);
4583 ++ac;
4584 }
4585
4586 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
4587 scroll_bar_name, av, ac);
4588
4589 /* Add one callback for everything that can happen. */
4590 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
4591 (XtPointer) bar);
4592 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
4593 (XtPointer) bar);
4594 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
4595 (XtPointer) bar);
4596 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
4597 (XtPointer) bar);
4598 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
4599 (XtPointer) bar);
4600 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
4601 (XtPointer) bar);
4602 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
4603 (XtPointer) bar);
4604
4605 /* Realize the widget. Only after that is the X window created. */
4606 XtRealizeWidget (widget);
4607
4608 /* Set the cursor to an arrow. I didn't find a resource to do that.
4609 And I'm wondering why it hasn't an arrow cursor by default. */
4610 XDefineCursor (XtDisplay (widget), XtWindow (widget),
4611 f->output_data.x->nontext_cursor);
4612
4613 #else /* !USE_MOTIF i.e. use Xaw */
4614
4615 /* Set resources. Create the widget. The background of the
4616 Xaw3d scroll bar widget is a little bit light for my taste.
4617 We don't alter it here to let users change it according
4618 to their taste with `emacs*verticalScrollBar.background: xxx'. */
4619 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4620 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
4621 /* For smoother scrolling with Xaw3d -sm */
4622 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
4623
4624 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4625 if (pixel != -1)
4626 {
4627 XtSetArg (av[ac], XtNforeground, pixel);
4628 ++ac;
4629 }
4630
4631 pixel = f->output_data.x->scroll_bar_background_pixel;
4632 if (pixel != -1)
4633 {
4634 XtSetArg (av[ac], XtNbackground, pixel);
4635 ++ac;
4636 }
4637
4638 /* Top/bottom shadow colors. */
4639
4640 /* Allocate them, if necessary. */
4641 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
4642 {
4643 pixel = f->output_data.x->scroll_bar_background_pixel;
4644 if (pixel != -1)
4645 {
4646 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
4647 FRAME_X_COLORMAP (f),
4648 &pixel, 1.2, 0x8000))
4649 pixel = -1;
4650 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
4651 }
4652 }
4653 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4654 {
4655 pixel = f->output_data.x->scroll_bar_background_pixel;
4656 if (pixel != -1)
4657 {
4658 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
4659 FRAME_X_COLORMAP (f),
4660 &pixel, 0.6, 0x4000))
4661 pixel = -1;
4662 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
4663 }
4664 }
4665
4666 #ifdef XtNbeNiceToColormap
4667 /* Tell the toolkit about them. */
4668 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
4669 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4670 /* We tried to allocate a color for the top/bottom shadow, and
4671 failed, so tell Xaw3d to use dithering instead. */
4672 {
4673 XtSetArg (av[ac], XtNbeNiceToColormap, True);
4674 ++ac;
4675 }
4676 else
4677 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
4678 be more consistent with other emacs 3d colors, and since Xaw3d is
4679 not good at dealing with allocation failure. */
4680 {
4681 /* This tells Xaw3d to use real colors instead of dithering for
4682 the shadows. */
4683 XtSetArg (av[ac], XtNbeNiceToColormap, False);
4684 ++ac;
4685
4686 /* Specify the colors. */
4687 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
4688 if (pixel != -1)
4689 {
4690 XtSetArg (av[ac], XtNtopShadowPixel, pixel);
4691 ++ac;
4692 }
4693 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
4694 if (pixel != -1)
4695 {
4696 XtSetArg (av[ac], XtNbottomShadowPixel, pixel);
4697 ++ac;
4698 }
4699 }
4700 #endif
4701
4702 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
4703 f->output_data.x->edit_widget, av, ac);
4704
4705 {
4706 char *initial = "";
4707 char *val = initial;
4708 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
4709 #ifdef XtNarrowScrollbars
4710 XtNarrowScrollbars, (XtPointer) &xaw3d_arrow_scroll,
4711 #endif
4712 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
4713 if (xaw3d_arrow_scroll || val == initial)
4714 { /* ARROW_SCROLL */
4715 xaw3d_arrow_scroll = True;
4716 /* Isn't that just a personal preference ? --Stef */
4717 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
4718 }
4719 }
4720
4721 /* Define callbacks. */
4722 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
4723 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
4724 (XtPointer) bar);
4725
4726 /* Realize the widget. Only after that is the X window created. */
4727 XtRealizeWidget (widget);
4728
4729 #endif /* !USE_MOTIF */
4730
4731 /* Install an action hook that lets us detect when the user
4732 finishes interacting with a scroll bar. */
4733 if (action_hook_id == 0)
4734 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
4735
4736 /* Remember X window and widget in the scroll bar vector. */
4737 SET_SCROLL_BAR_X_WIDGET (bar, widget);
4738 xwindow = XtWindow (widget);
4739 bar->x_window = xwindow;
4740
4741 UNBLOCK_INPUT;
4742 }
4743 #endif /* not USE_GTK */
4744
4745
4746 /* Set the thumb size and position of scroll bar BAR. We are currently
4747 displaying PORTION out of a whole WHOLE, and our position POSITION. */
4748
4749 #ifdef USE_GTK
4750 static void
4751 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
4752 struct scroll_bar *bar;
4753 int portion, position, whole;
4754 {
4755 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
4756 }
4757
4758 #else /* not USE_GTK */
4759 static void
4760 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
4761 struct scroll_bar *bar;
4762 int portion, position, whole;
4763 {
4764 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4765 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
4766 float top, shown;
4767
4768 BLOCK_INPUT;
4769
4770 #ifdef USE_MOTIF
4771
4772 /* We use an estimate of 30 chars per line rather than the real
4773 `portion' value. This has the disadvantage that the thumb size
4774 is not very representative, but it makes our life a lot easier.
4775 Otherwise, we have to constantly adjust the thumb size, which
4776 we can't always do quickly enough: while dragging, the size of
4777 the thumb might prevent the user from dragging the thumb all the
4778 way to the end. but Motif and some versions of Xaw3d don't allow
4779 updating the thumb size while dragging. Also, even if we can update
4780 its size, the update will often happen too late.
4781 If you don't believe it, check out revision 1.650 of xterm.c to see
4782 what hoops we were going through and the still poor behavior we got. */
4783 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
4784 /* When the thumb is at the bottom, position == whole.
4785 So we need to increase `whole' to make space for the thumb. */
4786 whole += portion;
4787
4788 if (whole <= 0)
4789 top = 0, shown = 1;
4790 else
4791 {
4792 top = (float) position / whole;
4793 shown = (float) portion / whole;
4794 }
4795
4796 if (NILP (bar->dragging))
4797 {
4798 int size, value;
4799
4800 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
4801 is the scroll bar's maximum and MIN is the scroll bar's minimum
4802 value. */
4803 size = shown * XM_SB_MAX;
4804 size = min (size, XM_SB_MAX);
4805 size = max (size, 1);
4806
4807 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
4808 value = top * XM_SB_MAX;
4809 value = min (value, XM_SB_MAX - size);
4810
4811 XmScrollBarSetValues (widget, value, size, 0, 0, False);
4812 }
4813 #else /* !USE_MOTIF i.e. use Xaw */
4814
4815 if (whole == 0)
4816 top = 0, shown = 1;
4817 else
4818 {
4819 top = (float) position / whole;
4820 shown = (float) portion / whole;
4821 }
4822
4823 {
4824 float old_top, old_shown;
4825 Dimension height;
4826 XtVaGetValues (widget,
4827 XtNtopOfThumb, &old_top,
4828 XtNshown, &old_shown,
4829 XtNheight, &height,
4830 NULL);
4831
4832 /* Massage the top+shown values. */
4833 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
4834 top = max (0, min (1, top));
4835 else
4836 top = old_top;
4837 /* Keep two pixels available for moving the thumb down. */
4838 shown = max (0, min (1 - top - (2.0 / height), shown));
4839
4840 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
4841 check that your system's configuration file contains a define
4842 for `NARROWPROTO'. See s/freebsd.h for an example. */
4843 if (top != old_top || shown != old_shown)
4844 {
4845 if (NILP (bar->dragging))
4846 XawScrollbarSetThumb (widget, top, shown);
4847 else
4848 {
4849 /* Try to make the scrolling a tad smoother. */
4850 if (!xaw3d_pick_top)
4851 shown = min (shown, old_shown);
4852
4853 XawScrollbarSetThumb (widget, top, shown);
4854 }
4855 }
4856 }
4857 #endif /* !USE_MOTIF */
4858
4859 UNBLOCK_INPUT;
4860 }
4861 #endif /* not USE_GTK */
4862
4863 #endif /* USE_TOOLKIT_SCROLL_BARS */
4864
4865
4866 \f
4867 /************************************************************************
4868 Scroll bars, general
4869 ************************************************************************/
4870
4871 /* Create a scroll bar and return the scroll bar vector for it. W is
4872 the Emacs window on which to create the scroll bar. TOP, LEFT,
4873 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
4874 scroll bar. */
4875
4876 static struct scroll_bar *
4877 x_scroll_bar_create (w, top, left, width, height)
4878 struct window *w;
4879 int top, left, width, height;
4880 {
4881 struct frame *f = XFRAME (w->frame);
4882 struct scroll_bar *bar
4883 = ALLOCATE_PSEUDOVECTOR (struct scroll_bar, x_window, PVEC_OTHER);
4884
4885 BLOCK_INPUT;
4886
4887 #ifdef USE_TOOLKIT_SCROLL_BARS
4888 x_create_toolkit_scroll_bar (f, bar);
4889 #else /* not USE_TOOLKIT_SCROLL_BARS */
4890 {
4891 XSetWindowAttributes a;
4892 unsigned long mask;
4893 Window window;
4894
4895 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
4896 if (a.background_pixel == -1)
4897 a.background_pixel = FRAME_BACKGROUND_PIXEL (f);
4898
4899 a.event_mask = (ButtonPressMask | ButtonReleaseMask
4900 | ButtonMotionMask | PointerMotionHintMask
4901 | ExposureMask);
4902 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
4903
4904 mask = (CWBackPixel | CWEventMask | CWCursor);
4905
4906 /* Clear the area of W that will serve as a scroll bar. This is
4907 for the case that a window has been split horizontally. In
4908 this case, no clear_frame is generated to reduce flickering. */
4909 if (width > 0 && height > 0)
4910 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4911 left, top, width,
4912 window_box_height (w), False);
4913
4914 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4915 /* Position and size of scroll bar. */
4916 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4917 top,
4918 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4919 height,
4920 /* Border width, depth, class, and visual. */
4921 0,
4922 CopyFromParent,
4923 CopyFromParent,
4924 CopyFromParent,
4925 /* Attributes. */
4926 mask, &a);
4927 bar->x_window = window;
4928 }
4929 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4930
4931 XSETWINDOW (bar->window, w);
4932 bar->top = top;
4933 bar->left = left;
4934 bar->width = width;
4935 bar->height = height;
4936 bar->start = 0;
4937 bar->end = 0;
4938 bar->dragging = Qnil;
4939 bar->fringe_extended_p = 0;
4940
4941 /* Add bar to its frame's list of scroll bars. */
4942 bar->next = FRAME_SCROLL_BARS (f);
4943 bar->prev = Qnil;
4944 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
4945 if (!NILP (bar->next))
4946 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
4947
4948 /* Map the window/widget. */
4949 #ifdef USE_TOOLKIT_SCROLL_BARS
4950 {
4951 #ifdef USE_GTK
4952 xg_update_scrollbar_pos (f,
4953 bar->x_window,
4954 top,
4955 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4956 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4957 max (height, 1));
4958 xg_show_scroll_bar (bar->x_window);
4959 #else /* not USE_GTK */
4960 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
4961 XtConfigureWidget (scroll_bar,
4962 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4963 top,
4964 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4965 max (height, 1), 0);
4966 XtMapWidget (scroll_bar);
4967 #endif /* not USE_GTK */
4968 }
4969 #else /* not USE_TOOLKIT_SCROLL_BARS */
4970 XMapRaised (FRAME_X_DISPLAY (f), bar->x_window);
4971 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4972
4973 UNBLOCK_INPUT;
4974 return bar;
4975 }
4976
4977
4978 #ifndef USE_TOOLKIT_SCROLL_BARS
4979
4980 /* Draw BAR's handle in the proper position.
4981
4982 If the handle is already drawn from START to END, don't bother
4983 redrawing it, unless REBUILD is non-zero; in that case, always
4984 redraw it. (REBUILD is handy for drawing the handle after expose
4985 events.)
4986
4987 Normally, we want to constrain the start and end of the handle to
4988 fit inside its rectangle, but if the user is dragging the scroll
4989 bar handle, we want to let them drag it down all the way, so that
4990 the bar's top is as far down as it goes; otherwise, there's no way
4991 to move to the very end of the buffer. */
4992
4993 static void
4994 x_scroll_bar_set_handle (bar, start, end, rebuild)
4995 struct scroll_bar *bar;
4996 int start, end;
4997 int rebuild;
4998 {
4999 int dragging = ! NILP (bar->dragging);
5000 Window w = bar->x_window;
5001 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5002 GC gc = f->output_data.x->normal_gc;
5003
5004 /* If the display is already accurate, do nothing. */
5005 if (! rebuild
5006 && start == bar->start
5007 && end == bar->end)
5008 return;
5009
5010 BLOCK_INPUT;
5011
5012 {
5013 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, bar->width);
5014 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, bar->height);
5015 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
5016
5017 /* Make sure the values are reasonable, and try to preserve
5018 the distance between start and end. */
5019 {
5020 int length = end - start;
5021
5022 if (start < 0)
5023 start = 0;
5024 else if (start > top_range)
5025 start = top_range;
5026 end = start + length;
5027
5028 if (end < start)
5029 end = start;
5030 else if (end > top_range && ! dragging)
5031 end = top_range;
5032 }
5033
5034 /* Store the adjusted setting in the scroll bar. */
5035 bar->start = start;
5036 bar->end = end;
5037
5038 /* Clip the end position, just for display. */
5039 if (end > top_range)
5040 end = top_range;
5041
5042 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
5043 below top positions, to make sure the handle is always at least
5044 that many pixels tall. */
5045 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
5046
5047 /* Draw the empty space above the handle. Note that we can't clear
5048 zero-height areas; that means "clear to end of window." */
5049 if (0 < start)
5050 x_clear_area (FRAME_X_DISPLAY (f), w,
5051 /* x, y, width, height, and exposures. */
5052 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5053 VERTICAL_SCROLL_BAR_TOP_BORDER,
5054 inside_width, start,
5055 False);
5056
5057 /* Change to proper foreground color if one is specified. */
5058 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5059 XSetForeground (FRAME_X_DISPLAY (f), gc,
5060 f->output_data.x->scroll_bar_foreground_pixel);
5061
5062 /* Draw the handle itself. */
5063 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
5064 /* x, y, width, height */
5065 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5066 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
5067 inside_width, end - start);
5068
5069 /* Restore the foreground color of the GC if we changed it above. */
5070 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5071 XSetForeground (FRAME_X_DISPLAY (f), gc,
5072 FRAME_FOREGROUND_PIXEL (f));
5073
5074 /* Draw the empty space below the handle. Note that we can't
5075 clear zero-height areas; that means "clear to end of window." */
5076 if (end < inside_height)
5077 x_clear_area (FRAME_X_DISPLAY (f), w,
5078 /* x, y, width, height, and exposures. */
5079 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5080 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
5081 inside_width, inside_height - end,
5082 False);
5083
5084 }
5085
5086 UNBLOCK_INPUT;
5087 }
5088
5089 #endif /* !USE_TOOLKIT_SCROLL_BARS */
5090
5091 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
5092 nil. */
5093
5094 static void
5095 x_scroll_bar_remove (bar)
5096 struct scroll_bar *bar;
5097 {
5098 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5099 BLOCK_INPUT;
5100
5101 #ifdef USE_TOOLKIT_SCROLL_BARS
5102 #ifdef USE_GTK
5103 xg_remove_scroll_bar (f, bar->x_window);
5104 #else /* not USE_GTK */
5105 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
5106 #endif /* not USE_GTK */
5107 #else
5108 XDestroyWindow (FRAME_X_DISPLAY (f), bar->x_window);
5109 #endif
5110
5111 /* Disassociate this scroll bar from its window. */
5112 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
5113
5114 UNBLOCK_INPUT;
5115 }
5116
5117
5118 /* Set the handle of the vertical scroll bar for WINDOW to indicate
5119 that we are displaying PORTION characters out of a total of WHOLE
5120 characters, starting at POSITION. If WINDOW has no scroll bar,
5121 create one. */
5122
5123 static void
5124 XTset_vertical_scroll_bar (w, portion, whole, position)
5125 struct window *w;
5126 int portion, whole, position;
5127 {
5128 struct frame *f = XFRAME (w->frame);
5129 struct scroll_bar *bar;
5130 int top, height, left, sb_left, width, sb_width;
5131 int window_y, window_height;
5132 #ifdef USE_TOOLKIT_SCROLL_BARS
5133 int fringe_extended_p;
5134 #endif
5135
5136 /* Get window dimensions. */
5137 window_box (w, -1, 0, &window_y, 0, &window_height);
5138 top = window_y;
5139 width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
5140 height = window_height;
5141
5142 /* Compute the left edge of the scroll bar area. */
5143 left = WINDOW_SCROLL_BAR_AREA_X (w);
5144
5145 /* Compute the width of the scroll bar which might be less than
5146 the width of the area reserved for the scroll bar. */
5147 if (WINDOW_CONFIG_SCROLL_BAR_WIDTH (w) > 0)
5148 sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
5149 else
5150 sb_width = width;
5151
5152 /* Compute the left edge of the scroll bar. */
5153 #ifdef USE_TOOLKIT_SCROLL_BARS
5154 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
5155 sb_left = left + (WINDOW_RIGHTMOST_P (w) ? width - sb_width : 0);
5156 else
5157 sb_left = left + (WINDOW_LEFTMOST_P (w) ? 0 : width - sb_width);
5158 #else
5159 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
5160 sb_left = left + width - sb_width;
5161 else
5162 sb_left = left;
5163 #endif
5164
5165 #ifdef USE_TOOLKIT_SCROLL_BARS
5166 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
5167 fringe_extended_p = (WINDOW_LEFTMOST_P (w)
5168 && WINDOW_LEFT_FRINGE_WIDTH (w)
5169 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5170 || WINDOW_LEFT_MARGIN_COLS (w) == 0));
5171 else
5172 fringe_extended_p = (WINDOW_RIGHTMOST_P (w)
5173 && WINDOW_RIGHT_FRINGE_WIDTH (w)
5174 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5175 || WINDOW_RIGHT_MARGIN_COLS (w) == 0));
5176 #endif
5177
5178 /* Does the scroll bar exist yet? */
5179 if (NILP (w->vertical_scroll_bar))
5180 {
5181 if (width > 0 && height > 0)
5182 {
5183 BLOCK_INPUT;
5184 #ifdef USE_TOOLKIT_SCROLL_BARS
5185 if (fringe_extended_p)
5186 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5187 sb_left, top, sb_width, height, False);
5188 else
5189 #endif
5190 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5191 left, top, width, height, False);
5192 UNBLOCK_INPUT;
5193 }
5194
5195 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
5196 }
5197 else
5198 {
5199 /* It may just need to be moved and resized. */
5200 unsigned int mask = 0;
5201
5202 bar = XSCROLL_BAR (w->vertical_scroll_bar);
5203
5204 BLOCK_INPUT;
5205
5206 if (sb_left != bar->left)
5207 mask |= CWX;
5208 if (top != bar->top)
5209 mask |= CWY;
5210 if (sb_width != bar->width)
5211 mask |= CWWidth;
5212 if (height != bar->height)
5213 mask |= CWHeight;
5214
5215 #ifdef USE_TOOLKIT_SCROLL_BARS
5216
5217 /* Move/size the scroll bar widget. */
5218 if (mask || bar->fringe_extended_p != fringe_extended_p)
5219 {
5220 /* Since toolkit scroll bars are smaller than the space reserved
5221 for them on the frame, we have to clear "under" them. */
5222 if (width > 0 && height > 0)
5223 {
5224 if (fringe_extended_p)
5225 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5226 sb_left, top, sb_width, height, False);
5227 else
5228 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5229 left, top, width, height, False);
5230 }
5231 #ifdef USE_GTK
5232 xg_update_scrollbar_pos (f,
5233 bar->x_window,
5234 top,
5235 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5236 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM *2,
5237 max (height, 1));
5238 #else /* not USE_GTK */
5239 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
5240 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5241 top,
5242 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
5243 max (height, 1), 0);
5244 #endif /* not USE_GTK */
5245 }
5246 #else /* not USE_TOOLKIT_SCROLL_BARS */
5247
5248 /* Clear areas not covered by the scroll bar because of
5249 VERTICAL_SCROLL_BAR_WIDTH_TRIM. */
5250 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
5251 {
5252 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5253 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5254 height, False);
5255 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5256 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5257 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5258 height, False);
5259 }
5260
5261 /* Clear areas not covered by the scroll bar because it's not as
5262 wide as the area reserved for it. This makes sure a
5263 previous mode line display is cleared after C-x 2 C-x 1, for
5264 example. */
5265 {
5266 int area_width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
5267 int rest = area_width - sb_width;
5268 if (rest > 0 && height > 0)
5269 {
5270 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
5271 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5272 left + area_width - rest, top,
5273 rest, height, False);
5274 else
5275 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5276 left, top, rest, height, False);
5277 }
5278 }
5279
5280 /* Move/size the scroll bar window. */
5281 if (mask)
5282 {
5283 XWindowChanges wc;
5284
5285 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5286 wc.y = top;
5287 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
5288 wc.height = height;
5289 XConfigureWindow (FRAME_X_DISPLAY (f), bar->x_window,
5290 mask, &wc);
5291 }
5292
5293 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5294
5295 /* Remember new settings. */
5296 bar->left = sb_left;
5297 bar->top = top;
5298 bar->width = sb_width;
5299 bar->height = height;
5300
5301 UNBLOCK_INPUT;
5302 }
5303
5304 #ifdef USE_TOOLKIT_SCROLL_BARS
5305 bar->fringe_extended_p = fringe_extended_p;
5306
5307 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
5308 #else /* not USE_TOOLKIT_SCROLL_BARS */
5309 /* Set the scroll bar's current state, unless we're currently being
5310 dragged. */
5311 if (NILP (bar->dragging))
5312 {
5313 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
5314
5315 if (whole == 0)
5316 x_scroll_bar_set_handle (bar, 0, top_range, 0);
5317 else
5318 {
5319 int start = ((double) position * top_range) / whole;
5320 int end = ((double) (position + portion) * top_range) / whole;
5321 x_scroll_bar_set_handle (bar, start, end, 0);
5322 }
5323 }
5324 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5325
5326 XSETVECTOR (w->vertical_scroll_bar, bar);
5327 }
5328
5329
5330 /* The following three hooks are used when we're doing a thorough
5331 redisplay of the frame. We don't explicitly know which scroll bars
5332 are going to be deleted, because keeping track of when windows go
5333 away is a real pain - "Can you say set-window-configuration, boys
5334 and girls?" Instead, we just assert at the beginning of redisplay
5335 that *all* scroll bars are to be removed, and then save a scroll bar
5336 from the fiery pit when we actually redisplay its window. */
5337
5338 /* Arrange for all scroll bars on FRAME to be removed at the next call
5339 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
5340 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
5341
5342 static void
5343 XTcondemn_scroll_bars (frame)
5344 FRAME_PTR frame;
5345 {
5346 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
5347 while (! NILP (FRAME_SCROLL_BARS (frame)))
5348 {
5349 Lisp_Object bar;
5350 bar = FRAME_SCROLL_BARS (frame);
5351 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
5352 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
5353 XSCROLL_BAR (bar)->prev = Qnil;
5354 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
5355 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
5356 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
5357 }
5358 }
5359
5360
5361 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
5362 Note that WINDOW isn't necessarily condemned at all. */
5363
5364 static void
5365 XTredeem_scroll_bar (window)
5366 struct window *window;
5367 {
5368 struct scroll_bar *bar;
5369 struct frame *f;
5370
5371 /* We can't redeem this window's scroll bar if it doesn't have one. */
5372 if (NILP (window->vertical_scroll_bar))
5373 abort ();
5374
5375 bar = XSCROLL_BAR (window->vertical_scroll_bar);
5376
5377 /* Unlink it from the condemned list. */
5378 f = XFRAME (WINDOW_FRAME (window));
5379 if (NILP (bar->prev))
5380 {
5381 /* If the prev pointer is nil, it must be the first in one of
5382 the lists. */
5383 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
5384 /* It's not condemned. Everything's fine. */
5385 return;
5386 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
5387 window->vertical_scroll_bar))
5388 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
5389 else
5390 /* If its prev pointer is nil, it must be at the front of
5391 one or the other! */
5392 abort ();
5393 }
5394 else
5395 XSCROLL_BAR (bar->prev)->next = bar->next;
5396
5397 if (! NILP (bar->next))
5398 XSCROLL_BAR (bar->next)->prev = bar->prev;
5399
5400 bar->next = FRAME_SCROLL_BARS (f);
5401 bar->prev = Qnil;
5402 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
5403 if (! NILP (bar->next))
5404 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
5405 }
5406
5407 /* Remove all scroll bars on FRAME that haven't been saved since the
5408 last call to `*condemn_scroll_bars_hook'. */
5409
5410 static void
5411 XTjudge_scroll_bars (f)
5412 FRAME_PTR f;
5413 {
5414 Lisp_Object bar, next;
5415
5416 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
5417
5418 /* Clear out the condemned list now so we won't try to process any
5419 more events on the hapless scroll bars. */
5420 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
5421
5422 for (; ! NILP (bar); bar = next)
5423 {
5424 struct scroll_bar *b = XSCROLL_BAR (bar);
5425
5426 x_scroll_bar_remove (b);
5427
5428 next = b->next;
5429 b->next = b->prev = Qnil;
5430 }
5431
5432 /* Now there should be no references to the condemned scroll bars,
5433 and they should get garbage-collected. */
5434 }
5435
5436
5437 #ifndef USE_TOOLKIT_SCROLL_BARS
5438 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
5439 is a no-op when using toolkit scroll bars.
5440
5441 This may be called from a signal handler, so we have to ignore GC
5442 mark bits. */
5443
5444 static void
5445 x_scroll_bar_expose (bar, event)
5446 struct scroll_bar *bar;
5447 XEvent *event;
5448 {
5449 Window w = bar->x_window;
5450 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5451 GC gc = f->output_data.x->normal_gc;
5452 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5453
5454 BLOCK_INPUT;
5455
5456 x_scroll_bar_set_handle (bar, bar->start, bar->end, 1);
5457
5458 /* Switch to scroll bar foreground color. */
5459 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5460 XSetForeground (FRAME_X_DISPLAY (f), gc,
5461 f->output_data.x->scroll_bar_foreground_pixel);
5462
5463 /* Draw a one-pixel border just inside the edges of the scroll bar. */
5464 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
5465
5466 /* x, y, width, height */
5467 0, 0,
5468 bar->width - 1 - width_trim - width_trim,
5469 bar->height - 1);
5470
5471 /* Restore the foreground color of the GC if we changed it above. */
5472 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5473 XSetForeground (FRAME_X_DISPLAY (f), gc,
5474 FRAME_FOREGROUND_PIXEL (f));
5475
5476 UNBLOCK_INPUT;
5477
5478 }
5479 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5480
5481 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
5482 is set to something other than NO_EVENT, it is enqueued.
5483
5484 This may be called from a signal handler, so we have to ignore GC
5485 mark bits. */
5486
5487
5488 static void
5489 x_scroll_bar_handle_click (bar, event, emacs_event)
5490 struct scroll_bar *bar;
5491 XEvent *event;
5492 struct input_event *emacs_event;
5493 {
5494 if (! WINDOWP (bar->window))
5495 abort ();
5496
5497 emacs_event->kind = SCROLL_BAR_CLICK_EVENT;
5498 emacs_event->code = event->xbutton.button - Button1;
5499 emacs_event->modifiers
5500 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
5501 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
5502 event->xbutton.state)
5503 | (event->type == ButtonRelease
5504 ? up_modifier
5505 : down_modifier));
5506 emacs_event->frame_or_window = bar->window;
5507 emacs_event->arg = Qnil;
5508 emacs_event->timestamp = event->xbutton.time;
5509 {
5510 int top_range
5511 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
5512 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
5513
5514 if (y < 0) y = 0;
5515 if (y > top_range) y = top_range;
5516
5517 if (y < bar->start)
5518 emacs_event->part = scroll_bar_above_handle;
5519 else if (y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5520 emacs_event->part = scroll_bar_handle;
5521 else
5522 emacs_event->part = scroll_bar_below_handle;
5523
5524 #ifndef USE_TOOLKIT_SCROLL_BARS
5525 /* If the user has released the handle, set it to its final position. */
5526 if (event->type == ButtonRelease
5527 && ! NILP (bar->dragging))
5528 {
5529 int new_start = y - XINT (bar->dragging);
5530 int new_end = new_start + bar->end - bar->start;
5531
5532 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5533 bar->dragging = Qnil;
5534 }
5535 #endif
5536
5537 XSETINT (emacs_event->x, y);
5538 XSETINT (emacs_event->y, top_range);
5539 }
5540 }
5541
5542 #ifndef USE_TOOLKIT_SCROLL_BARS
5543
5544 /* Handle some mouse motion while someone is dragging the scroll bar.
5545
5546 This may be called from a signal handler, so we have to ignore GC
5547 mark bits. */
5548
5549 static void
5550 x_scroll_bar_note_movement (bar, event)
5551 struct scroll_bar *bar;
5552 XEvent *event;
5553 {
5554 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
5555
5556 last_mouse_movement_time = event->xmotion.time;
5557
5558 f->mouse_moved = 1;
5559 XSETVECTOR (last_mouse_scroll_bar, bar);
5560
5561 /* If we're dragging the bar, display it. */
5562 if (! NILP (bar->dragging))
5563 {
5564 /* Where should the handle be now? */
5565 int new_start = event->xmotion.y - XINT (bar->dragging);
5566
5567 if (new_start != bar->start)
5568 {
5569 int new_end = new_start + bar->end - bar->start;
5570
5571 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5572 }
5573 }
5574 }
5575
5576 #endif /* !USE_TOOLKIT_SCROLL_BARS */
5577
5578 /* Return information to the user about the current position of the mouse
5579 on the scroll bar. */
5580
5581 static void
5582 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
5583 FRAME_PTR *fp;
5584 Lisp_Object *bar_window;
5585 enum scroll_bar_part *part;
5586 Lisp_Object *x, *y;
5587 unsigned long *time;
5588 {
5589 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
5590 Window w = bar->x_window;
5591 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5592 int win_x, win_y;
5593 Window dummy_window;
5594 int dummy_coord;
5595 unsigned int dummy_mask;
5596
5597 BLOCK_INPUT;
5598
5599 /* Get the mouse's position relative to the scroll bar window, and
5600 report that. */
5601 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
5602
5603 /* Root, child, root x and root y. */
5604 &dummy_window, &dummy_window,
5605 &dummy_coord, &dummy_coord,
5606
5607 /* Position relative to scroll bar. */
5608 &win_x, &win_y,
5609
5610 /* Mouse buttons and modifier keys. */
5611 &dummy_mask))
5612 ;
5613 else
5614 {
5615 int top_range
5616 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
5617
5618 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
5619
5620 if (! NILP (bar->dragging))
5621 win_y -= XINT (bar->dragging);
5622
5623 if (win_y < 0)
5624 win_y = 0;
5625 if (win_y > top_range)
5626 win_y = top_range;
5627
5628 *fp = f;
5629 *bar_window = bar->window;
5630
5631 if (! NILP (bar->dragging))
5632 *part = scroll_bar_handle;
5633 else if (win_y < bar->start)
5634 *part = scroll_bar_above_handle;
5635 else if (win_y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5636 *part = scroll_bar_handle;
5637 else
5638 *part = scroll_bar_below_handle;
5639
5640 XSETINT (*x, win_y);
5641 XSETINT (*y, top_range);
5642
5643 f->mouse_moved = 0;
5644 last_mouse_scroll_bar = Qnil;
5645 }
5646
5647 *time = last_mouse_movement_time;
5648
5649 UNBLOCK_INPUT;
5650 }
5651
5652
5653 /* The screen has been cleared so we may have changed foreground or
5654 background colors, and the scroll bars may need to be redrawn.
5655 Clear out the scroll bars, and ask for expose events, so we can
5656 redraw them. */
5657
5658 void
5659 x_scroll_bar_clear (f)
5660 FRAME_PTR f;
5661 {
5662 #ifndef USE_TOOLKIT_SCROLL_BARS
5663 Lisp_Object bar;
5664
5665 /* We can have scroll bars even if this is 0,
5666 if we just turned off scroll bar mode.
5667 But in that case we should not clear them. */
5668 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5669 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
5670 bar = XSCROLL_BAR (bar)->next)
5671 XClearArea (FRAME_X_DISPLAY (f),
5672 XSCROLL_BAR (bar)->x_window,
5673 0, 0, 0, 0, True);
5674 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5675 }
5676
5677 \f
5678 /* The main X event-reading loop - XTread_socket. */
5679
5680 /* This holds the state XLookupString needs to implement dead keys
5681 and other tricks known as "compose processing". _X Window System_
5682 says that a portable program can't use this, but Stephen Gildea assures
5683 me that letting the compiler initialize it to zeros will work okay.
5684
5685 This must be defined outside of XTread_socket, for the same reasons
5686 given for enter_timestamp, above. */
5687
5688 static XComposeStatus compose_status;
5689
5690 /* Record the last 100 characters stored
5691 to help debug the loss-of-chars-during-GC problem. */
5692
5693 static int temp_index;
5694 static short temp_buffer[100];
5695
5696 #define STORE_KEYSYM_FOR_DEBUG(keysym) \
5697 if (temp_index == sizeof temp_buffer / sizeof (short)) \
5698 temp_index = 0; \
5699 temp_buffer[temp_index++] = (keysym)
5700
5701 /* Set this to nonzero to fake an "X I/O error"
5702 on a particular display. */
5703
5704 struct x_display_info *XTread_socket_fake_io_error;
5705
5706 /* When we find no input here, we occasionally do a no-op command
5707 to verify that the X server is still running and we can still talk with it.
5708 We try all the open displays, one by one.
5709 This variable is used for cycling thru the displays. */
5710
5711 static struct x_display_info *next_noop_dpyinfo;
5712
5713 #define SET_SAVED_MENU_EVENT(size) \
5714 do \
5715 { \
5716 if (f->output_data.x->saved_menu_event == 0) \
5717 f->output_data.x->saved_menu_event \
5718 = (XEvent *) xmalloc (sizeof (XEvent)); \
5719 bcopy (&event, f->output_data.x->saved_menu_event, size); \
5720 inev.ie.kind = MENU_BAR_ACTIVATE_EVENT; \
5721 XSETFRAME (inev.ie.frame_or_window, f); \
5722 } \
5723 while (0)
5724
5725 #define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
5726 #define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
5727
5728
5729 enum
5730 {
5731 X_EVENT_NORMAL,
5732 X_EVENT_GOTO_OUT,
5733 X_EVENT_DROP
5734 };
5735
5736 /* Filter events for the current X input method.
5737 DPYINFO is the display this event is for.
5738 EVENT is the X event to filter.
5739
5740 Returns non-zero if the event was filtered, caller shall not process
5741 this event further.
5742 Returns zero if event is wasn't filtered. */
5743
5744 #ifdef HAVE_X_I18N
5745 static int
5746 x_filter_event (dpyinfo, event)
5747 struct x_display_info *dpyinfo;
5748 XEvent *event;
5749 {
5750 /* XFilterEvent returns non-zero if the input method has
5751 consumed the event. We pass the frame's X window to
5752 XFilterEvent because that's the one for which the IC
5753 was created. */
5754
5755 struct frame *f1 = x_any_window_to_frame (dpyinfo,
5756 event->xclient.window);
5757
5758 return XFilterEvent (event, f1 ? FRAME_X_WINDOW (f1) : None);
5759 }
5760 #endif
5761
5762 #ifdef USE_GTK
5763 static int current_count;
5764 static int current_finish;
5765 static struct input_event *current_hold_quit;
5766
5767 /* This is the filter function invoked by the GTK event loop.
5768 It is invoked before the XEvent is translated to a GdkEvent,
5769 so we have a chance to act on the event before GTK. */
5770 static GdkFilterReturn
5771 event_handler_gdk (gxev, ev, data)
5772 GdkXEvent *gxev;
5773 GdkEvent *ev;
5774 gpointer data;
5775 {
5776 XEvent *xev = (XEvent *) gxev;
5777
5778 if (current_count >= 0)
5779 {
5780 struct x_display_info *dpyinfo;
5781
5782 dpyinfo = x_display_info_for_display (xev->xany.display);
5783
5784 #ifdef HAVE_X_I18N
5785 /* Filter events for the current X input method.
5786 GTK calls XFilterEvent but not for key press and release,
5787 so we do it here. */
5788 if (xev->type == KeyPress || xev->type == KeyRelease)
5789 if (dpyinfo && x_filter_event (dpyinfo, xev))
5790 return GDK_FILTER_REMOVE;
5791 #endif
5792
5793 if (! dpyinfo)
5794 current_finish = X_EVENT_NORMAL;
5795 else
5796 {
5797 current_count +=
5798 handle_one_xevent (dpyinfo, xev, &current_finish,
5799 current_hold_quit);
5800 }
5801 }
5802 else
5803 current_finish = x_dispatch_event (xev, xev->xany.display);
5804
5805 if (current_finish == X_EVENT_GOTO_OUT || current_finish == X_EVENT_DROP)
5806 return GDK_FILTER_REMOVE;
5807
5808 return GDK_FILTER_CONTINUE;
5809 }
5810 #endif /* USE_GTK */
5811
5812
5813 /* Handles the XEvent EVENT on display DPYINFO.
5814
5815 *FINISH is X_EVENT_GOTO_OUT if caller should stop reading events.
5816 *FINISH is zero if caller should continue reading events.
5817 *FINISH is X_EVENT_DROP if event should not be passed to the toolkit.
5818
5819 We return the number of characters stored into the buffer. */
5820
5821 static int
5822 handle_one_xevent (dpyinfo, eventp, finish, hold_quit)
5823 struct x_display_info *dpyinfo;
5824 XEvent *eventp;
5825 int *finish;
5826 struct input_event *hold_quit;
5827 {
5828 union {
5829 struct input_event ie;
5830 struct selection_input_event sie;
5831 } inev;
5832 int count = 0;
5833 int do_help = 0;
5834 int nbytes = 0;
5835 struct frame *f = NULL;
5836 struct coding_system coding;
5837 XEvent event = *eventp;
5838
5839 *finish = X_EVENT_NORMAL;
5840
5841 EVENT_INIT (inev.ie);
5842 inev.ie.kind = NO_EVENT;
5843 inev.ie.arg = Qnil;
5844
5845 if (pending_event_wait.eventtype == event.type)
5846 pending_event_wait.eventtype = 0; /* Indicates we got it. */
5847
5848 switch (event.type)
5849 {
5850 case ClientMessage:
5851 {
5852 if (event.xclient.message_type
5853 == dpyinfo->Xatom_wm_protocols
5854 && event.xclient.format == 32)
5855 {
5856 if (event.xclient.data.l[0]
5857 == dpyinfo->Xatom_wm_take_focus)
5858 {
5859 /* Use x_any_window_to_frame because this
5860 could be the shell widget window
5861 if the frame has no title bar. */
5862 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
5863 #ifdef HAVE_X_I18N
5864 /* Not quite sure this is needed -pd */
5865 if (f && FRAME_XIC (f))
5866 XSetICFocus (FRAME_XIC (f));
5867 #endif
5868 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
5869 instructs the WM to set the input focus automatically for
5870 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
5871 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
5872 it has set the focus. So, XSetInputFocus below is not
5873 needed.
5874
5875 The call to XSetInputFocus below has also caused trouble. In
5876 cases where the XSetInputFocus done by the WM and the one
5877 below are temporally close (on a fast machine), the call
5878 below can generate additional FocusIn events which confuse
5879 Emacs. */
5880
5881 /* Since we set WM_TAKE_FOCUS, we must call
5882 XSetInputFocus explicitly. But not if f is null,
5883 since that might be an event for a deleted frame. */
5884 if (f)
5885 {
5886 Display *d = event.xclient.display;
5887 /* Catch and ignore errors, in case window has been
5888 iconified by a window manager such as GWM. */
5889 x_catch_errors (d);
5890 XSetInputFocus (d, event.xclient.window,
5891 /* The ICCCM says this is
5892 the only valid choice. */
5893 RevertToParent,
5894 event.xclient.data.l[1]);
5895 /* This is needed to detect the error
5896 if there is an error. */
5897 XSync (d, False);
5898 x_uncatch_errors ();
5899 }
5900 /* Not certain about handling scroll bars here */
5901 #endif /* 0 */
5902 goto done;
5903 }
5904
5905 if (event.xclient.data.l[0]
5906 == dpyinfo->Xatom_wm_save_yourself)
5907 {
5908 /* Save state modify the WM_COMMAND property to
5909 something which can reinstate us. This notifies
5910 the session manager, who's looking for such a
5911 PropertyNotify. Can restart processing when
5912 a keyboard or mouse event arrives. */
5913 /* If we have a session manager, don't set this.
5914 KDE will then start two Emacsen, one for the
5915 session manager and one for this. */
5916 #ifdef HAVE_X_SM
5917 if (! x_session_have_connection ())
5918 #endif
5919 {
5920 f = x_top_window_to_frame (dpyinfo,
5921 event.xclient.window);
5922 /* This is just so we only give real data once
5923 for a single Emacs process. */
5924 if (f == SELECTED_FRAME ())
5925 XSetCommand (FRAME_X_DISPLAY (f),
5926 event.xclient.window,
5927 initial_argv, initial_argc);
5928 else if (f)
5929 XSetCommand (FRAME_X_DISPLAY (f),
5930 event.xclient.window,
5931 0, 0);
5932 }
5933 goto done;
5934 }
5935
5936 if (event.xclient.data.l[0]
5937 == dpyinfo->Xatom_wm_delete_window)
5938 {
5939 f = x_any_window_to_frame (dpyinfo,
5940 event.xclient.window);
5941 if (!f)
5942 goto OTHER; /* May be a dialog that is to be removed */
5943
5944 inev.ie.kind = DELETE_WINDOW_EVENT;
5945 XSETFRAME (inev.ie.frame_or_window, f);
5946 goto done;
5947 }
5948
5949 goto done;
5950 }
5951
5952 if (event.xclient.message_type
5953 == dpyinfo->Xatom_wm_configure_denied)
5954 {
5955 goto done;
5956 }
5957
5958 if (event.xclient.message_type
5959 == dpyinfo->Xatom_wm_window_moved)
5960 {
5961 int new_x, new_y;
5962 f = x_window_to_frame (dpyinfo, event.xclient.window);
5963
5964 new_x = event.xclient.data.s[0];
5965 new_y = event.xclient.data.s[1];
5966
5967 if (f)
5968 {
5969 f->left_pos = new_x;
5970 f->top_pos = new_y;
5971 }
5972 goto done;
5973 }
5974
5975 #ifdef HACK_EDITRES
5976 if (event.xclient.message_type
5977 == dpyinfo->Xatom_editres)
5978 {
5979 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
5980 if (f)
5981 _XEditResCheckMessages (f->output_data.x->widget, NULL,
5982 &event, NULL);
5983 goto done;
5984 }
5985 #endif /* HACK_EDITRES */
5986
5987 if ((event.xclient.message_type
5988 == dpyinfo->Xatom_DONE)
5989 || (event.xclient.message_type
5990 == dpyinfo->Xatom_PAGE))
5991 {
5992 /* Ghostview job completed. Kill it. We could
5993 reply with "Next" if we received "Page", but we
5994 currently never do because we are interested in
5995 images, only, which should have 1 page. */
5996 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
5997 f = x_window_to_frame (dpyinfo, event.xclient.window);
5998 if (!f)
5999 goto OTHER;
6000 x_kill_gs_process (pixmap, f);
6001 expose_frame (f, 0, 0, 0, 0);
6002 goto done;
6003 }
6004
6005 #ifdef USE_TOOLKIT_SCROLL_BARS
6006 /* Scroll bar callbacks send a ClientMessage from which
6007 we construct an input_event. */
6008 if (event.xclient.message_type
6009 == dpyinfo->Xatom_Scrollbar)
6010 {
6011 x_scroll_bar_to_input_event (&event, &inev.ie);
6012 *finish = X_EVENT_GOTO_OUT;
6013 goto done;
6014 }
6015 #endif /* USE_TOOLKIT_SCROLL_BARS */
6016
6017 /* XEmbed messages from the embedder (if any). */
6018 if (event.xclient.message_type
6019 == dpyinfo->Xatom_XEMBED)
6020 {
6021 enum xembed_message msg = event.xclient.data.l[1];
6022 if (msg == XEMBED_FOCUS_IN || msg == XEMBED_FOCUS_OUT)
6023 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6024
6025 *finish = X_EVENT_GOTO_OUT;
6026 goto done;
6027 }
6028
6029 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
6030 if (!f)
6031 goto OTHER;
6032 if (x_handle_dnd_message (f, &event.xclient, dpyinfo, &inev.ie))
6033 *finish = X_EVENT_DROP;
6034 }
6035 break;
6036
6037 case SelectionNotify:
6038 last_user_time = event.xselection.time;
6039 #ifdef USE_X_TOOLKIT
6040 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
6041 goto OTHER;
6042 #endif /* not USE_X_TOOLKIT */
6043 x_handle_selection_notify (&event.xselection);
6044 break;
6045
6046 case SelectionClear: /* Someone has grabbed ownership. */
6047 last_user_time = event.xselectionclear.time;
6048 #ifdef USE_X_TOOLKIT
6049 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
6050 goto OTHER;
6051 #endif /* USE_X_TOOLKIT */
6052 {
6053 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
6054
6055 inev.ie.kind = SELECTION_CLEAR_EVENT;
6056 SELECTION_EVENT_DISPLAY (&inev.sie) = eventp->display;
6057 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
6058 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
6059 inev.ie.frame_or_window = Qnil;
6060 }
6061 break;
6062
6063 case SelectionRequest: /* Someone wants our selection. */
6064 last_user_time = event.xselectionrequest.time;
6065 #ifdef USE_X_TOOLKIT
6066 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
6067 goto OTHER;
6068 #endif /* USE_X_TOOLKIT */
6069 {
6070 XSelectionRequestEvent *eventp
6071 = (XSelectionRequestEvent *) &event;
6072
6073 inev.ie.kind = SELECTION_REQUEST_EVENT;
6074 SELECTION_EVENT_DISPLAY (&inev.sie) = eventp->display;
6075 SELECTION_EVENT_REQUESTOR (&inev.sie) = eventp->requestor;
6076 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
6077 SELECTION_EVENT_TARGET (&inev.sie) = eventp->target;
6078 SELECTION_EVENT_PROPERTY (&inev.sie) = eventp->property;
6079 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
6080 inev.ie.frame_or_window = Qnil;
6081 }
6082 break;
6083
6084 case PropertyNotify:
6085 last_user_time = event.xproperty.time;
6086 f = x_top_window_to_frame (dpyinfo, event.xproperty.window);
6087 if (f && event.xproperty.atom == dpyinfo->Xatom_net_wm_state)
6088 x_handle_net_wm_state (f, &event.xproperty);
6089
6090 x_handle_property_notify (&event.xproperty);
6091 goto OTHER;
6092
6093 case ReparentNotify:
6094 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
6095 if (f)
6096 {
6097 int x, y;
6098 f->output_data.x->parent_desc = event.xreparent.parent;
6099 x_real_positions (f, &x, &y);
6100 f->left_pos = x;
6101 f->top_pos = y;
6102
6103 /* Perhaps reparented due to a WM restart. Reset this. */
6104 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_UNKNOWN;
6105 FRAME_X_DISPLAY_INFO (f)->net_supported_window = 0;
6106 }
6107 goto OTHER;
6108
6109 case Expose:
6110 f = x_window_to_frame (dpyinfo, event.xexpose.window);
6111 if (f)
6112 {
6113 #ifdef USE_GTK
6114 /* This seems to be needed for GTK 2.6. */
6115 x_clear_area (event.xexpose.display,
6116 event.xexpose.window,
6117 event.xexpose.x, event.xexpose.y,
6118 event.xexpose.width, event.xexpose.height,
6119 FALSE);
6120 #endif
6121 if (f->async_visible == 0)
6122 {
6123 f->async_visible = 1;
6124 f->async_iconified = 0;
6125 f->output_data.x->has_been_visible = 1;
6126 SET_FRAME_GARBAGED (f);
6127 }
6128 else
6129 expose_frame (f,
6130 event.xexpose.x, event.xexpose.y,
6131 event.xexpose.width, event.xexpose.height);
6132 }
6133 else
6134 {
6135 #ifndef USE_TOOLKIT_SCROLL_BARS
6136 struct scroll_bar *bar;
6137 #endif
6138 #if defined USE_LUCID
6139 /* Submenus of the Lucid menu bar aren't widgets
6140 themselves, so there's no way to dispatch events
6141 to them. Recognize this case separately. */
6142 {
6143 Widget widget
6144 = x_window_to_menu_bar (event.xexpose.window);
6145 if (widget)
6146 xlwmenu_redisplay (widget);
6147 }
6148 #endif /* USE_LUCID */
6149
6150 #ifdef USE_TOOLKIT_SCROLL_BARS
6151 /* Dispatch event to the widget. */
6152 goto OTHER;
6153 #else /* not USE_TOOLKIT_SCROLL_BARS */
6154 bar = x_window_to_scroll_bar (event.xexpose.display,
6155 event.xexpose.window);
6156
6157 if (bar)
6158 x_scroll_bar_expose (bar, &event);
6159 #ifdef USE_X_TOOLKIT
6160 else
6161 goto OTHER;
6162 #endif /* USE_X_TOOLKIT */
6163 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6164 }
6165 break;
6166
6167 case GraphicsExpose: /* This occurs when an XCopyArea's
6168 source area was obscured or not
6169 available. */
6170 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
6171 if (f)
6172 {
6173 expose_frame (f,
6174 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
6175 event.xgraphicsexpose.width,
6176 event.xgraphicsexpose.height);
6177 }
6178 #ifdef USE_X_TOOLKIT
6179 else
6180 goto OTHER;
6181 #endif /* USE_X_TOOLKIT */
6182 break;
6183
6184 case NoExpose: /* This occurs when an XCopyArea's
6185 source area was completely
6186 available. */
6187 break;
6188
6189 case UnmapNotify:
6190 /* Redo the mouse-highlight after the tooltip has gone. */
6191 if (event.xmap.window == tip_window)
6192 {
6193 tip_window = 0;
6194 redo_mouse_highlight ();
6195 }
6196
6197 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
6198 if (f) /* F may no longer exist if
6199 the frame was deleted. */
6200 {
6201 /* While a frame is unmapped, display generation is
6202 disabled; you don't want to spend time updating a
6203 display that won't ever be seen. */
6204 f->async_visible = 0;
6205 /* We can't distinguish, from the event, whether the window
6206 has become iconified or invisible. So assume, if it
6207 was previously visible, than now it is iconified.
6208 But x_make_frame_invisible clears both
6209 the visible flag and the iconified flag;
6210 and that way, we know the window is not iconified now. */
6211 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
6212 {
6213 f->async_iconified = 1;
6214
6215 inev.ie.kind = ICONIFY_EVENT;
6216 XSETFRAME (inev.ie.frame_or_window, f);
6217 }
6218 }
6219 goto OTHER;
6220
6221 case MapNotify:
6222 if (event.xmap.window == tip_window)
6223 /* The tooltip has been drawn already. Avoid
6224 the SET_FRAME_GARBAGED below. */
6225 goto OTHER;
6226
6227 /* We use x_top_window_to_frame because map events can
6228 come for sub-windows and they don't mean that the
6229 frame is visible. */
6230 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
6231 if (f)
6232 {
6233 /* wait_reading_process_output will notice this and update
6234 the frame's display structures.
6235 If we where iconified, we should not set garbaged,
6236 because that stops redrawing on Expose events. This looks
6237 bad if we are called from a recursive event loop
6238 (x_dispatch_event), for example when a dialog is up. */
6239 if (! f->async_iconified)
6240 SET_FRAME_GARBAGED (f);
6241
6242 /* Check if fullscreen was specified before we where mapped the
6243 first time, i.e. from the command line. */
6244 if (!f->output_data.x->has_been_visible)
6245 x_check_fullscreen (f);
6246
6247 f->async_visible = 1;
6248 f->async_iconified = 0;
6249 f->output_data.x->has_been_visible = 1;
6250
6251 if (f->iconified)
6252 {
6253 inev.ie.kind = DEICONIFY_EVENT;
6254 XSETFRAME (inev.ie.frame_or_window, f);
6255 }
6256 else if (! NILP (Vframe_list)
6257 && ! NILP (XCDR (Vframe_list)))
6258 /* Force a redisplay sooner or later
6259 to update the frame titles
6260 in case this is the second frame. */
6261 record_asynch_buffer_change ();
6262
6263 #ifdef USE_GTK
6264 xg_frame_resized (f, -1, -1);
6265 #endif
6266 }
6267 goto OTHER;
6268
6269 case KeyPress:
6270
6271 last_user_time = event.xkey.time;
6272 ignore_next_mouse_click_timeout = 0;
6273
6274 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6275 /* Dispatch KeyPress events when in menu. */
6276 if (popup_activated ())
6277 goto OTHER;
6278 #endif
6279
6280 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
6281
6282 /* If mouse-highlight is an integer, input clears out
6283 mouse highlighting. */
6284 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
6285 && (f == 0
6286 || !EQ (f->tool_bar_window, dpyinfo->mouse_face_window)))
6287 {
6288 clear_mouse_face (dpyinfo);
6289 dpyinfo->mouse_face_hidden = 1;
6290 }
6291
6292 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
6293 if (f == 0)
6294 {
6295 /* Scroll bars consume key events, but we want
6296 the keys to go to the scroll bar's frame. */
6297 Widget widget = XtWindowToWidget (dpyinfo->display,
6298 event.xkey.window);
6299 if (widget && XmIsScrollBar (widget))
6300 {
6301 widget = XtParent (widget);
6302 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
6303 }
6304 }
6305 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
6306
6307 if (f != 0)
6308 {
6309 KeySym keysym, orig_keysym;
6310 /* al%imercury@uunet.uu.net says that making this 81
6311 instead of 80 fixed a bug whereby meta chars made
6312 his Emacs hang.
6313
6314 It seems that some version of XmbLookupString has
6315 a bug of not returning XBufferOverflow in
6316 status_return even if the input is too long to
6317 fit in 81 bytes. So, we must prepare sufficient
6318 bytes for copy_buffer. 513 bytes (256 chars for
6319 two-byte character set) seems to be a fairly good
6320 approximation. -- 2000.8.10 handa@etl.go.jp */
6321 unsigned char copy_buffer[513];
6322 unsigned char *copy_bufptr = copy_buffer;
6323 int copy_bufsiz = sizeof (copy_buffer);
6324 int modifiers;
6325 Lisp_Object coding_system = Qlatin_1;
6326 Lisp_Object c;
6327
6328 #ifdef USE_GTK
6329 /* Don't pass keys to GTK. A Tab will shift focus to the
6330 tool bar in GTK 2.4. Keys will still go to menus and
6331 dialogs because in that case popup_activated is TRUE
6332 (see above). */
6333 *finish = X_EVENT_DROP;
6334 #endif
6335
6336 event.xkey.state
6337 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
6338 extra_keyboard_modifiers);
6339 modifiers = event.xkey.state;
6340
6341 /* This will have to go some day... */
6342
6343 /* make_lispy_event turns chars into control chars.
6344 Don't do it here because XLookupString is too eager. */
6345 event.xkey.state &= ~ControlMask;
6346 event.xkey.state &= ~(dpyinfo->meta_mod_mask
6347 | dpyinfo->super_mod_mask
6348 | dpyinfo->hyper_mod_mask
6349 | dpyinfo->alt_mod_mask);
6350
6351 /* In case Meta is ComposeCharacter,
6352 clear its status. According to Markus Ehrnsperger
6353 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
6354 this enables ComposeCharacter to work whether or
6355 not it is combined with Meta. */
6356 if (modifiers & dpyinfo->meta_mod_mask)
6357 bzero (&compose_status, sizeof (compose_status));
6358
6359 #ifdef HAVE_X_I18N
6360 if (FRAME_XIC (f))
6361 {
6362 Status status_return;
6363
6364 coding_system = Vlocale_coding_system;
6365 nbytes = XmbLookupString (FRAME_XIC (f),
6366 &event.xkey, copy_bufptr,
6367 copy_bufsiz, &keysym,
6368 &status_return);
6369 if (status_return == XBufferOverflow)
6370 {
6371 copy_bufsiz = nbytes + 1;
6372 copy_bufptr = (unsigned char *) alloca (copy_bufsiz);
6373 nbytes = XmbLookupString (FRAME_XIC (f),
6374 &event.xkey, copy_bufptr,
6375 copy_bufsiz, &keysym,
6376 &status_return);
6377 }
6378 /* Xutf8LookupString is a new but already deprecated interface. -stef */
6379 if (status_return == XLookupNone)
6380 break;
6381 else if (status_return == XLookupChars)
6382 {
6383 keysym = NoSymbol;
6384 modifiers = 0;
6385 }
6386 else if (status_return != XLookupKeySym
6387 && status_return != XLookupBoth)
6388 abort ();
6389 }
6390 else
6391 nbytes = XLookupString (&event.xkey, copy_bufptr,
6392 copy_bufsiz, &keysym,
6393 &compose_status);
6394 #else
6395 nbytes = XLookupString (&event.xkey, copy_bufptr,
6396 copy_bufsiz, &keysym,
6397 &compose_status);
6398 #endif
6399
6400 /* If not using XIM/XIC, and a compose sequence is in progress,
6401 we break here. Otherwise, chars_matched is always 0. */
6402 if (compose_status.chars_matched > 0 && nbytes == 0)
6403 break;
6404
6405 bzero (&compose_status, sizeof (compose_status));
6406 orig_keysym = keysym;
6407
6408 /* Common for all keysym input events. */
6409 XSETFRAME (inev.ie.frame_or_window, f);
6410 inev.ie.modifiers
6411 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f), modifiers);
6412 inev.ie.timestamp = event.xkey.time;
6413
6414 /* First deal with keysyms which have defined
6415 translations to characters. */
6416 if (keysym >= 32 && keysym < 128)
6417 /* Avoid explicitly decoding each ASCII character. */
6418 {
6419 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
6420 inev.ie.code = keysym;
6421 goto done_keysym;
6422 }
6423
6424 /* Keysyms directly mapped to Unicode characters. */
6425 if (keysym >= 0x01000000 && keysym <= 0x0110FFFF)
6426 {
6427 if (keysym < 0x01000080)
6428 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
6429 else
6430 inev.ie.kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
6431 inev.ie.code = keysym & 0xFFFFFF;
6432 goto done_keysym;
6433 }
6434
6435 /* Now non-ASCII. */
6436 if (HASH_TABLE_P (Vx_keysym_table)
6437 && (NATNUMP (c = Fgethash (make_number (keysym),
6438 Vx_keysym_table,
6439 Qnil))))
6440 {
6441 inev.ie.kind = (SINGLE_BYTE_CHAR_P (XFASTINT (c))
6442 ? ASCII_KEYSTROKE_EVENT
6443 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6444 inev.ie.code = XFASTINT (c);
6445 goto done_keysym;
6446 }
6447
6448 /* Random non-modifier sorts of keysyms. */
6449 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
6450 || keysym == XK_Delete
6451 #ifdef XK_ISO_Left_Tab
6452 || (keysym >= XK_ISO_Left_Tab
6453 && keysym <= XK_ISO_Enter)
6454 #endif
6455 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
6456 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
6457 #ifdef HPUX
6458 /* This recognizes the "extended function
6459 keys". It seems there's no cleaner way.
6460 Test IsModifierKey to avoid handling
6461 mode_switch incorrectly. */
6462 || ((unsigned) (keysym) >= XK_Select
6463 && (unsigned)(keysym) < XK_KP_Space)
6464 #endif
6465 #ifdef XK_dead_circumflex
6466 || orig_keysym == XK_dead_circumflex
6467 #endif
6468 #ifdef XK_dead_grave
6469 || orig_keysym == XK_dead_grave
6470 #endif
6471 #ifdef XK_dead_tilde
6472 || orig_keysym == XK_dead_tilde
6473 #endif
6474 #ifdef XK_dead_diaeresis
6475 || orig_keysym == XK_dead_diaeresis
6476 #endif
6477 #ifdef XK_dead_macron
6478 || orig_keysym == XK_dead_macron
6479 #endif
6480 #ifdef XK_dead_degree
6481 || orig_keysym == XK_dead_degree
6482 #endif
6483 #ifdef XK_dead_acute
6484 || orig_keysym == XK_dead_acute
6485 #endif
6486 #ifdef XK_dead_cedilla
6487 || orig_keysym == XK_dead_cedilla
6488 #endif
6489 #ifdef XK_dead_breve
6490 || orig_keysym == XK_dead_breve
6491 #endif
6492 #ifdef XK_dead_ogonek
6493 || orig_keysym == XK_dead_ogonek
6494 #endif
6495 #ifdef XK_dead_caron
6496 || orig_keysym == XK_dead_caron
6497 #endif
6498 #ifdef XK_dead_doubleacute
6499 || orig_keysym == XK_dead_doubleacute
6500 #endif
6501 #ifdef XK_dead_abovedot
6502 || orig_keysym == XK_dead_abovedot
6503 #endif
6504 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
6505 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
6506 /* Any "vendor-specific" key is ok. */
6507 || (orig_keysym & (1 << 28))
6508 || (keysym != NoSymbol && nbytes == 0))
6509 && ! (IsModifierKey (orig_keysym)
6510 /* The symbols from XK_ISO_Lock
6511 to XK_ISO_Last_Group_Lock
6512 don't have real modifiers but
6513 should be treated similarly to
6514 Mode_switch by Emacs. */
6515 #if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
6516 || ((unsigned)(orig_keysym)
6517 >= XK_ISO_Lock
6518 && (unsigned)(orig_keysym)
6519 <= XK_ISO_Last_Group_Lock)
6520 #endif
6521 ))
6522 {
6523 STORE_KEYSYM_FOR_DEBUG (keysym);
6524 /* make_lispy_event will convert this to a symbolic
6525 key. */
6526 inev.ie.kind = NON_ASCII_KEYSTROKE_EVENT;
6527 inev.ie.code = keysym;
6528 goto done_keysym;
6529 }
6530
6531 { /* Raw bytes, not keysym. */
6532 register int i;
6533 register int c;
6534 int nchars, len;
6535
6536 for (i = 0, nchars = 0; i < nbytes; i++)
6537 {
6538 if (ASCII_BYTE_P (copy_bufptr[i]))
6539 nchars++;
6540 STORE_KEYSYM_FOR_DEBUG (copy_bufptr[i]);
6541 }
6542
6543 if (nchars < nbytes)
6544 {
6545 /* Decode the input data. */
6546 int require;
6547 unsigned char *p;
6548
6549 /* The input should be decoded with `coding_system'
6550 which depends on which X*LookupString function
6551 we used just above and the locale. */
6552 setup_coding_system (coding_system, &coding);
6553 coding.src_multibyte = 0;
6554 coding.dst_multibyte = 1;
6555 /* The input is converted to events, thus we can't
6556 handle composition. Anyway, there's no XIM that
6557 gives us composition information. */
6558 coding.common_flags &= ~CODING_ANNOTATION_MASK;
6559
6560 require = MAX_MULTIBYTE_LENGTH * nbytes;
6561 coding.destination = alloca (require);
6562 coding.dst_bytes = require;
6563 coding.mode |= CODING_MODE_LAST_BLOCK;
6564 decode_coding_c_string (&coding, copy_bufptr, nbytes, Qnil);
6565 nbytes = coding.produced;
6566 nchars = coding.produced_char;
6567 copy_bufptr = coding.destination;
6568 }
6569
6570 /* Convert the input data to a sequence of
6571 character events. */
6572 for (i = 0; i < nbytes; i += len)
6573 {
6574 if (nchars == nbytes)
6575 c = copy_bufptr[i], len = 1;
6576 else
6577 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
6578 nbytes - i, len);
6579 inev.ie.kind = (SINGLE_BYTE_CHAR_P (c)
6580 ? ASCII_KEYSTROKE_EVENT
6581 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6582 inev.ie.code = c;
6583 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
6584 }
6585
6586 count += nchars;
6587
6588 inev.ie.kind = NO_EVENT; /* Already stored above. */
6589
6590 if (keysym == NoSymbol)
6591 break;
6592 }
6593 }
6594 done_keysym:
6595 #ifdef HAVE_X_I18N
6596 /* Don't dispatch this event since XtDispatchEvent calls
6597 XFilterEvent, and two calls in a row may freeze the
6598 client. */
6599 break;
6600 #else
6601 goto OTHER;
6602 #endif
6603
6604 case KeyRelease:
6605 last_user_time = event.xkey.time;
6606 #ifdef HAVE_X_I18N
6607 /* Don't dispatch this event since XtDispatchEvent calls
6608 XFilterEvent, and two calls in a row may freeze the
6609 client. */
6610 break;
6611 #else
6612 goto OTHER;
6613 #endif
6614
6615 case EnterNotify:
6616 last_user_time = event.xcrossing.time;
6617 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6618
6619 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
6620
6621 if (f && x_mouse_click_focus_ignore_position)
6622 ignore_next_mouse_click_timeout = event.xmotion.time + 200;
6623
6624 /* EnterNotify counts as mouse movement,
6625 so update things that depend on mouse position. */
6626 if (f && !f->output_data.x->hourglass_p)
6627 note_mouse_movement (f, &event.xmotion);
6628 #ifdef USE_GTK
6629 /* We may get an EnterNotify on the buttons in the toolbar. In that
6630 case we moved out of any highlighted area and need to note this. */
6631 if (!f && last_mouse_glyph_frame)
6632 note_mouse_movement (last_mouse_glyph_frame, &event.xmotion);
6633 #endif
6634 goto OTHER;
6635
6636 case FocusIn:
6637 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6638 goto OTHER;
6639
6640 case LeaveNotify:
6641 last_user_time = event.xcrossing.time;
6642 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6643
6644 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
6645 if (f)
6646 {
6647 if (f == dpyinfo->mouse_face_mouse_frame)
6648 {
6649 /* If we move outside the frame, then we're
6650 certainly no longer on any text in the frame. */
6651 clear_mouse_face (dpyinfo);
6652 dpyinfo->mouse_face_mouse_frame = 0;
6653 }
6654
6655 /* Generate a nil HELP_EVENT to cancel a help-echo.
6656 Do it only if there's something to cancel.
6657 Otherwise, the startup message is cleared when
6658 the mouse leaves the frame. */
6659 if (any_help_event_p)
6660 do_help = -1;
6661 }
6662 #ifdef USE_GTK
6663 /* See comment in EnterNotify above */
6664 else if (last_mouse_glyph_frame)
6665 note_mouse_movement (last_mouse_glyph_frame, &event.xmotion);
6666 #endif
6667 goto OTHER;
6668
6669 case FocusOut:
6670 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6671 goto OTHER;
6672
6673 case MotionNotify:
6674 {
6675 last_user_time = event.xmotion.time;
6676 previous_help_echo_string = help_echo_string;
6677 help_echo_string = Qnil;
6678
6679 if (dpyinfo->grabbed && last_mouse_frame
6680 && FRAME_LIVE_P (last_mouse_frame))
6681 f = last_mouse_frame;
6682 else
6683 f = x_window_to_frame (dpyinfo, event.xmotion.window);
6684
6685 if (dpyinfo->mouse_face_hidden)
6686 {
6687 dpyinfo->mouse_face_hidden = 0;
6688 clear_mouse_face (dpyinfo);
6689 }
6690
6691 #ifdef USE_GTK
6692 if (f && xg_event_is_for_scrollbar (f, &event))
6693 f = 0;
6694 #endif
6695 if (f)
6696 {
6697
6698 /* Generate SELECT_WINDOW_EVENTs when needed.
6699 Don't let popup menus influence things (bug#1261). */
6700 if (!NILP (Vmouse_autoselect_window) && !popup_activated ())
6701 {
6702 Lisp_Object window;
6703
6704 window = window_from_coordinates (f,
6705 event.xmotion.x, event.xmotion.y,
6706 0, 0, 0, 0);
6707
6708 /* Window will be selected only when it is not selected now and
6709 last mouse movement event was not in it. Minibuffer window
6710 will be selected only when it is active. */
6711 if (WINDOWP (window)
6712 && !EQ (window, last_window)
6713 && !EQ (window, selected_window)
6714 /* For click-to-focus window managers
6715 create event iff we don't leave the
6716 selected frame. */
6717 && (focus_follows_mouse
6718 || (EQ (XWINDOW (window)->frame,
6719 XWINDOW (selected_window)->frame))))
6720 {
6721 inev.ie.kind = SELECT_WINDOW_EVENT;
6722 inev.ie.frame_or_window = window;
6723 }
6724
6725 last_window=window;
6726 }
6727 if (!note_mouse_movement (f, &event.xmotion))
6728 help_echo_string = previous_help_echo_string;
6729 }
6730 else
6731 {
6732 #ifndef USE_TOOLKIT_SCROLL_BARS
6733 struct scroll_bar *bar
6734 = x_window_to_scroll_bar (event.xmotion.display,
6735 event.xmotion.window);
6736
6737 if (bar)
6738 x_scroll_bar_note_movement (bar, &event);
6739 #endif /* USE_TOOLKIT_SCROLL_BARS */
6740
6741 /* If we move outside the frame, then we're
6742 certainly no longer on any text in the frame. */
6743 clear_mouse_face (dpyinfo);
6744 }
6745
6746 /* If the contents of the global variable help_echo_string
6747 has changed, generate a HELP_EVENT. */
6748 if (!NILP (help_echo_string)
6749 || !NILP (previous_help_echo_string))
6750 do_help = 1;
6751 goto OTHER;
6752 }
6753
6754 case ConfigureNotify:
6755 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
6756 #ifdef USE_GTK
6757 if (!f
6758 && (f = x_any_window_to_frame (dpyinfo, event.xconfigure.window))
6759 && event.xconfigure.window == FRAME_X_WINDOW (f))
6760 {
6761 xg_frame_resized (f, event.xconfigure.width,
6762 event.xconfigure.height);
6763 f = 0;
6764 }
6765 #endif
6766 if (f)
6767 {
6768 #ifndef USE_X_TOOLKIT
6769 #ifndef USE_GTK
6770 int rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, event.xconfigure.height);
6771 int columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, event.xconfigure.width);
6772
6773 /* In the toolkit version, change_frame_size
6774 is called by the code that handles resizing
6775 of the EmacsFrame widget. */
6776
6777 /* Even if the number of character rows and columns has
6778 not changed, the font size may have changed, so we need
6779 to check the pixel dimensions as well. */
6780 if (columns != FRAME_COLS (f)
6781 || rows != FRAME_LINES (f)
6782 || event.xconfigure.width != FRAME_PIXEL_WIDTH (f)
6783 || event.xconfigure.height != FRAME_PIXEL_HEIGHT (f))
6784 {
6785 change_frame_size (f, rows, columns, 0, 1, 0);
6786 SET_FRAME_GARBAGED (f);
6787 cancel_mouse_face (f);
6788 }
6789
6790 FRAME_PIXEL_WIDTH (f) = event.xconfigure.width;
6791 FRAME_PIXEL_HEIGHT (f) = event.xconfigure.height;
6792 #endif /* not USE_GTK */
6793 #endif
6794
6795 #ifdef USE_GTK
6796 /* GTK creates windows but doesn't map them.
6797 Only get real positions when mapped. */
6798 if (FRAME_GTK_OUTER_WIDGET (f)
6799 && GTK_WIDGET_MAPPED (FRAME_GTK_OUTER_WIDGET (f)))
6800 #endif
6801 {
6802 x_real_positions (f, &f->left_pos, &f->top_pos);
6803 }
6804
6805 #ifdef HAVE_X_I18N
6806 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
6807 xic_set_statusarea (f);
6808 #endif
6809
6810 }
6811 goto OTHER;
6812
6813 case ButtonRelease:
6814 case ButtonPress:
6815 {
6816 /* If we decide we want to generate an event to be seen
6817 by the rest of Emacs, we put it here. */
6818 int tool_bar_p = 0;
6819
6820 bzero (&compose_status, sizeof (compose_status));
6821 last_mouse_glyph_frame = 0;
6822 last_user_time = event.xbutton.time;
6823
6824 if (dpyinfo->grabbed
6825 && last_mouse_frame
6826 && FRAME_LIVE_P (last_mouse_frame))
6827 f = last_mouse_frame;
6828 else
6829 f = x_window_to_frame (dpyinfo, event.xbutton.window);
6830
6831 #ifdef USE_GTK
6832 if (f && xg_event_is_for_scrollbar (f, &event))
6833 f = 0;
6834 #endif
6835 if (f)
6836 {
6837 /* Is this in the tool-bar? */
6838 if (WINDOWP (f->tool_bar_window)
6839 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
6840 {
6841 Lisp_Object window;
6842 int x = event.xbutton.x;
6843 int y = event.xbutton.y;
6844
6845 window = window_from_coordinates (f, x, y, 0, 0, 0, 1);
6846 tool_bar_p = EQ (window, f->tool_bar_window);
6847
6848 if (tool_bar_p && event.xbutton.button < 4)
6849 {
6850 handle_tool_bar_click (f, x, y,
6851 event.xbutton.type == ButtonPress,
6852 x_x_to_emacs_modifiers (dpyinfo,
6853 event.xbutton.state));
6854 }
6855 }
6856
6857 if (!tool_bar_p)
6858 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6859 if (! popup_activated ())
6860 #endif
6861 {
6862 if (ignore_next_mouse_click_timeout)
6863 {
6864 if (event.type == ButtonPress
6865 && (int)(event.xbutton.time - ignore_next_mouse_click_timeout) > 0)
6866 {
6867 ignore_next_mouse_click_timeout = 0;
6868 construct_mouse_click (&inev.ie, &event.xbutton, f);
6869 }
6870 if (event.type == ButtonRelease)
6871 ignore_next_mouse_click_timeout = 0;
6872 }
6873 else
6874 construct_mouse_click (&inev.ie, &event.xbutton, f);
6875 }
6876 if (FRAME_X_EMBEDDED_P (f))
6877 xembed_send_message (f, event.xbutton.time,
6878 XEMBED_REQUEST_FOCUS, 0, 0, 0);
6879 }
6880 else
6881 {
6882 struct scroll_bar *bar
6883 = x_window_to_scroll_bar (event.xbutton.display,
6884 event.xbutton.window);
6885
6886 #ifdef USE_TOOLKIT_SCROLL_BARS
6887 /* Make the "Ctrl-Mouse-2 splits window" work for toolkit
6888 scroll bars. */
6889 if (bar && event.xbutton.state & ControlMask)
6890 {
6891 x_scroll_bar_handle_click (bar, &event, &inev.ie);
6892 *finish = X_EVENT_DROP;
6893 }
6894 #else /* not USE_TOOLKIT_SCROLL_BARS */
6895 if (bar)
6896 x_scroll_bar_handle_click (bar, &event, &inev.ie);
6897 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6898 }
6899
6900 if (event.type == ButtonPress)
6901 {
6902 dpyinfo->grabbed |= (1 << event.xbutton.button);
6903 last_mouse_frame = f;
6904
6905 if (!tool_bar_p)
6906 last_tool_bar_item = -1;
6907 }
6908 else
6909 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
6910
6911 /* Ignore any mouse motion that happened before this event;
6912 any subsequent mouse-movement Emacs events should reflect
6913 only motion after the ButtonPress/Release. */
6914 if (f != 0)
6915 f->mouse_moved = 0;
6916
6917 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6918 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
6919 /* For a down-event in the menu bar,
6920 don't pass it to Xt right now.
6921 Instead, save it away
6922 and we will pass it to Xt from kbd_buffer_get_event.
6923 That way, we can run some Lisp code first. */
6924 if (
6925 #ifdef USE_GTK
6926 ! popup_activated ()
6927 /* Gtk+ menus only react to the first three buttons. */
6928 && event.xbutton.button < 3
6929 &&
6930 #endif
6931 f && event.type == ButtonPress
6932 /* Verify the event is really within the menu bar
6933 and not just sent to it due to grabbing. */
6934 && event.xbutton.x >= 0
6935 && event.xbutton.x < FRAME_PIXEL_WIDTH (f)
6936 && event.xbutton.y >= 0
6937 && event.xbutton.y < f->output_data.x->menubar_height
6938 && event.xbutton.same_screen)
6939 {
6940 SET_SAVED_BUTTON_EVENT;
6941 XSETFRAME (last_mouse_press_frame, f);
6942 #ifdef USE_GTK
6943 *finish = X_EVENT_DROP;
6944 #endif
6945 }
6946 else if (event.type == ButtonPress)
6947 {
6948 last_mouse_press_frame = Qnil;
6949 goto OTHER;
6950 }
6951
6952 #ifdef USE_MOTIF /* This should do not harm for Lucid,
6953 but I am trying to be cautious. */
6954 else if (event.type == ButtonRelease)
6955 {
6956 if (!NILP (last_mouse_press_frame))
6957 {
6958 f = XFRAME (last_mouse_press_frame);
6959 if (f->output_data.x)
6960 SET_SAVED_BUTTON_EVENT;
6961 }
6962 else
6963 goto OTHER;
6964 }
6965 #endif /* USE_MOTIF */
6966 else
6967 goto OTHER;
6968 #endif /* USE_X_TOOLKIT || USE_GTK */
6969 }
6970 break;
6971
6972 case CirculateNotify:
6973 goto OTHER;
6974
6975 case CirculateRequest:
6976 goto OTHER;
6977
6978 case VisibilityNotify:
6979 goto OTHER;
6980
6981 case MappingNotify:
6982 /* Someone has changed the keyboard mapping - update the
6983 local cache. */
6984 switch (event.xmapping.request)
6985 {
6986 case MappingModifier:
6987 x_find_modifier_meanings (dpyinfo);
6988 /* This is meant to fall through. */
6989 case MappingKeyboard:
6990 XRefreshKeyboardMapping (&event.xmapping);
6991 }
6992 goto OTHER;
6993
6994 default:
6995 OTHER:
6996 #ifdef USE_X_TOOLKIT
6997 BLOCK_INPUT;
6998 if (*finish != X_EVENT_DROP)
6999 XtDispatchEvent (&event);
7000 UNBLOCK_INPUT;
7001 #endif /* USE_X_TOOLKIT */
7002 break;
7003 }
7004
7005 done:
7006 if (inev.ie.kind != NO_EVENT)
7007 {
7008 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
7009 count++;
7010 }
7011
7012 if (do_help
7013 && !(hold_quit && hold_quit->kind != NO_EVENT))
7014 {
7015 Lisp_Object frame;
7016
7017 if (f)
7018 XSETFRAME (frame, f);
7019 else
7020 frame = Qnil;
7021
7022 if (do_help > 0)
7023 {
7024 any_help_event_p = 1;
7025 gen_help_event (help_echo_string, frame, help_echo_window,
7026 help_echo_object, help_echo_pos);
7027 }
7028 else
7029 {
7030 help_echo_string = Qnil;
7031 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
7032 }
7033 count++;
7034 }
7035
7036 *eventp = event;
7037 return count;
7038 }
7039
7040
7041 /* Handles the XEvent EVENT on display DISPLAY.
7042 This is used for event loops outside the normal event handling,
7043 i.e. looping while a popup menu or a dialog is posted.
7044
7045 Returns the value handle_one_xevent sets in the finish argument. */
7046 int
7047 x_dispatch_event (event, display)
7048 XEvent *event;
7049 Display *display;
7050 {
7051 struct x_display_info *dpyinfo;
7052 int finish = X_EVENT_NORMAL;
7053
7054 dpyinfo = x_display_info_for_display (display);
7055
7056 if (dpyinfo)
7057 handle_one_xevent (dpyinfo, event, &finish, 0);
7058
7059 return finish;
7060 }
7061
7062
7063 /* Read events coming from the X server.
7064 This routine is called by the SIGIO handler.
7065 We return as soon as there are no more events to be read.
7066
7067 We return the number of characters stored into the buffer,
7068 thus pretending to be `read' (except the characters we store
7069 in the keyboard buffer can be multibyte, so are not necessarily
7070 C chars).
7071
7072 EXPECTED is nonzero if the caller knows input is available. */
7073
7074 static int
7075 XTread_socket (terminal, expected, hold_quit)
7076 struct terminal *terminal;
7077 int expected;
7078 struct input_event *hold_quit;
7079 {
7080 int count = 0;
7081 XEvent event;
7082 int event_found = 0;
7083
7084 if (interrupt_input_blocked)
7085 {
7086 interrupt_input_pending = 1;
7087 #ifdef SYNC_INPUT
7088 pending_signals = 1;
7089 #endif
7090 return -1;
7091 }
7092
7093 interrupt_input_pending = 0;
7094 #ifdef SYNC_INPUT
7095 pending_signals = pending_atimers;
7096 #endif
7097 BLOCK_INPUT;
7098
7099 /* So people can tell when we have read the available input. */
7100 input_signal_count++;
7101
7102 ++handling_signal;
7103
7104 #ifdef HAVE_X_SM
7105 /* Only check session manager input for the primary display. */
7106 if (terminal->id == 1 && x_session_have_connection ())
7107 {
7108 struct input_event inev;
7109 BLOCK_INPUT;
7110 /* We don't need to EVENT_INIT (inev) here, as
7111 x_session_check_input copies an entire input_event. */
7112 if (x_session_check_input (&inev))
7113 {
7114 kbd_buffer_store_event_hold (&inev, hold_quit);
7115 count++;
7116 }
7117 UNBLOCK_INPUT;
7118 }
7119 #endif
7120
7121 /* For debugging, this gives a way to fake an I/O error. */
7122 if (terminal->display_info.x == XTread_socket_fake_io_error)
7123 {
7124 XTread_socket_fake_io_error = 0;
7125 x_io_error_quitter (terminal->display_info.x->display);
7126 }
7127
7128 #ifndef USE_GTK
7129 while (XPending (terminal->display_info.x->display))
7130 {
7131 int finish;
7132
7133 XNextEvent (terminal->display_info.x->display, &event);
7134
7135 #ifdef HAVE_X_I18N
7136 /* Filter events for the current X input method. */
7137 if (x_filter_event (terminal->display_info.x, &event))
7138 continue;
7139 #endif
7140 event_found = 1;
7141
7142 count += handle_one_xevent (terminal->display_info.x,
7143 &event, &finish, hold_quit);
7144
7145 if (finish == X_EVENT_GOTO_OUT)
7146 goto out;
7147 }
7148
7149 #else /* USE_GTK */
7150
7151 /* For GTK we must use the GTK event loop. But XEvents gets passed
7152 to our filter function above, and then to the big event switch.
7153 We use a bunch of globals to communicate with our filter function,
7154 that is kind of ugly, but it works.
7155
7156 There is no way to do one display at the time, GTK just does events
7157 from all displays. */
7158
7159 while (gtk_events_pending ())
7160 {
7161 current_count = count;
7162 current_hold_quit = hold_quit;
7163
7164 gtk_main_iteration ();
7165
7166 count = current_count;
7167 current_count = -1;
7168 current_hold_quit = 0;
7169
7170 if (current_finish == X_EVENT_GOTO_OUT)
7171 break;
7172 }
7173 #endif /* USE_GTK */
7174
7175 out:;
7176
7177 /* On some systems, an X bug causes Emacs to get no more events
7178 when the window is destroyed. Detect that. (1994.) */
7179 if (! event_found)
7180 {
7181 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
7182 One XNOOP in 100 loops will make Emacs terminate.
7183 B. Bretthauer, 1994 */
7184 x_noop_count++;
7185 if (x_noop_count >= 100)
7186 {
7187 x_noop_count=0;
7188
7189 if (next_noop_dpyinfo == 0)
7190 next_noop_dpyinfo = x_display_list;
7191
7192 XNoOp (next_noop_dpyinfo->display);
7193
7194 /* Each time we get here, cycle through the displays now open. */
7195 next_noop_dpyinfo = next_noop_dpyinfo->next;
7196 }
7197 }
7198
7199 /* If the focus was just given to an auto-raising frame,
7200 raise it now. */
7201 /* ??? This ought to be able to handle more than one such frame. */
7202 if (pending_autoraise_frame)
7203 {
7204 x_raise_frame (pending_autoraise_frame);
7205 pending_autoraise_frame = 0;
7206 }
7207
7208 --handling_signal;
7209 UNBLOCK_INPUT;
7210
7211 return count;
7212 }
7213
7214
7215
7216 \f
7217 /***********************************************************************
7218 Text Cursor
7219 ***********************************************************************/
7220
7221 /* Set clipping for output in glyph row ROW. W is the window in which
7222 we operate. GC is the graphics context to set clipping in.
7223
7224 ROW may be a text row or, e.g., a mode line. Text rows must be
7225 clipped to the interior of the window dedicated to text display,
7226 mode lines must be clipped to the whole window. */
7227
7228 static void
7229 x_clip_to_row (w, row, area, gc)
7230 struct window *w;
7231 struct glyph_row *row;
7232 int area;
7233 GC gc;
7234 {
7235 struct frame *f = XFRAME (WINDOW_FRAME (w));
7236 XRectangle clip_rect;
7237 int window_x, window_y, window_width;
7238
7239 window_box (w, area, &window_x, &window_y, &window_width, 0);
7240
7241 clip_rect.x = window_x;
7242 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, row->y));
7243 clip_rect.y = max (clip_rect.y, window_y);
7244 clip_rect.width = window_width;
7245 clip_rect.height = row->visible_height;
7246
7247 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
7248 }
7249
7250
7251 /* Draw a hollow box cursor on window W in glyph row ROW. */
7252
7253 static void
7254 x_draw_hollow_cursor (w, row)
7255 struct window *w;
7256 struct glyph_row *row;
7257 {
7258 struct frame *f = XFRAME (WINDOW_FRAME (w));
7259 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7260 Display *dpy = FRAME_X_DISPLAY (f);
7261 int x, y, wd, h;
7262 XGCValues xgcv;
7263 struct glyph *cursor_glyph;
7264 GC gc;
7265
7266 /* Get the glyph the cursor is on. If we can't tell because
7267 the current matrix is invalid or such, give up. */
7268 cursor_glyph = get_phys_cursor_glyph (w);
7269 if (cursor_glyph == NULL)
7270 return;
7271
7272 /* Compute frame-relative coordinates for phys cursor. */
7273 get_phys_cursor_geometry (w, row, cursor_glyph, &x, &y, &h);
7274 wd = w->phys_cursor_width;
7275
7276 /* The foreground of cursor_gc is typically the same as the normal
7277 background color, which can cause the cursor box to be invisible. */
7278 xgcv.foreground = f->output_data.x->cursor_pixel;
7279 if (dpyinfo->scratch_cursor_gc)
7280 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
7281 else
7282 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
7283 GCForeground, &xgcv);
7284 gc = dpyinfo->scratch_cursor_gc;
7285
7286 /* Set clipping, draw the rectangle, and reset clipping again. */
7287 x_clip_to_row (w, row, TEXT_AREA, gc);
7288 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h - 1);
7289 XSetClipMask (dpy, gc, None);
7290 }
7291
7292
7293 /* Draw a bar cursor on window W in glyph row ROW.
7294
7295 Implementation note: One would like to draw a bar cursor with an
7296 angle equal to the one given by the font property XA_ITALIC_ANGLE.
7297 Unfortunately, I didn't find a font yet that has this property set.
7298 --gerd. */
7299
7300 static void
7301 x_draw_bar_cursor (w, row, width, kind)
7302 struct window *w;
7303 struct glyph_row *row;
7304 int width;
7305 enum text_cursor_kinds kind;
7306 {
7307 struct frame *f = XFRAME (w->frame);
7308 struct glyph *cursor_glyph;
7309
7310 /* If cursor is out of bounds, don't draw garbage. This can happen
7311 in mini-buffer windows when switching between echo area glyphs
7312 and mini-buffer. */
7313 cursor_glyph = get_phys_cursor_glyph (w);
7314 if (cursor_glyph == NULL)
7315 return;
7316
7317 /* If on an image, draw like a normal cursor. That's usually better
7318 visible than drawing a bar, esp. if the image is large so that
7319 the bar might not be in the window. */
7320 if (cursor_glyph->type == IMAGE_GLYPH)
7321 {
7322 struct glyph_row *row;
7323 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
7324 draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
7325 }
7326 else
7327 {
7328 Display *dpy = FRAME_X_DISPLAY (f);
7329 Window window = FRAME_X_WINDOW (f);
7330 GC gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
7331 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
7332 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
7333 XGCValues xgcv;
7334
7335 /* If the glyph's background equals the color we normally draw
7336 the bars cursor in, the bar cursor in its normal color is
7337 invisible. Use the glyph's foreground color instead in this
7338 case, on the assumption that the glyph's colors are chosen so
7339 that the glyph is legible. */
7340 if (face->background == f->output_data.x->cursor_pixel)
7341 xgcv.background = xgcv.foreground = face->foreground;
7342 else
7343 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
7344 xgcv.graphics_exposures = 0;
7345
7346 if (gc)
7347 XChangeGC (dpy, gc, mask, &xgcv);
7348 else
7349 {
7350 gc = XCreateGC (dpy, window, mask, &xgcv);
7351 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
7352 }
7353
7354 x_clip_to_row (w, row, TEXT_AREA, gc);
7355
7356 if (kind == BAR_CURSOR)
7357 {
7358 if (width < 0)
7359 width = FRAME_CURSOR_WIDTH (f);
7360 width = min (cursor_glyph->pixel_width, width);
7361
7362 w->phys_cursor_width = width;
7363
7364 XFillRectangle (dpy, window, gc,
7365 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7366 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
7367 width, row->height);
7368 }
7369 else
7370 {
7371 int dummy_x, dummy_y, dummy_h;
7372
7373 if (width < 0)
7374 width = row->height;
7375
7376 width = min (row->height, width);
7377
7378 get_phys_cursor_geometry (w, row, cursor_glyph, &dummy_x,
7379 &dummy_y, &dummy_h);
7380
7381 XFillRectangle (dpy, window, gc,
7382 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7383 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
7384 row->height - width),
7385 w->phys_cursor_width, width);
7386 }
7387
7388 XSetClipMask (dpy, gc, None);
7389 }
7390 }
7391
7392
7393 /* RIF: Define cursor CURSOR on frame F. */
7394
7395 static void
7396 x_define_frame_cursor (f, cursor)
7397 struct frame *f;
7398 Cursor cursor;
7399 {
7400 if (!f->pointer_invisible
7401 && f->output_data.x->current_cursor != cursor)
7402 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
7403 f->output_data.x->current_cursor = cursor;
7404 }
7405
7406
7407 /* RIF: Clear area on frame F. */
7408
7409 static void
7410 x_clear_frame_area (f, x, y, width, height)
7411 struct frame *f;
7412 int x, y, width, height;
7413 {
7414 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7415 x, y, width, height, False);
7416 }
7417
7418
7419 /* RIF: Draw cursor on window W. */
7420
7421 static void
7422 x_draw_window_cursor (w, glyph_row, x, y, cursor_type, cursor_width, on_p, active_p)
7423 struct window *w;
7424 struct glyph_row *glyph_row;
7425 int x, y;
7426 int cursor_type, cursor_width;
7427 int on_p, active_p;
7428 {
7429 struct frame *f = XFRAME (WINDOW_FRAME (w));
7430
7431 if (on_p)
7432 {
7433 w->phys_cursor_type = cursor_type;
7434 w->phys_cursor_on_p = 1;
7435
7436 if (glyph_row->exact_window_width_line_p
7437 && w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])
7438 {
7439 glyph_row->cursor_in_fringe_p = 1;
7440 draw_fringe_bitmap (w, glyph_row, 0);
7441 }
7442 else
7443 switch (cursor_type)
7444 {
7445 case HOLLOW_BOX_CURSOR:
7446 x_draw_hollow_cursor (w, glyph_row);
7447 break;
7448
7449 case FILLED_BOX_CURSOR:
7450 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
7451 break;
7452
7453 case BAR_CURSOR:
7454 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
7455 break;
7456
7457 case HBAR_CURSOR:
7458 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
7459 break;
7460
7461 case NO_CURSOR:
7462 w->phys_cursor_width = 0;
7463 break;
7464
7465 default:
7466 abort ();
7467 }
7468
7469 #ifdef HAVE_X_I18N
7470 if (w == XWINDOW (f->selected_window))
7471 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
7472 xic_set_preeditarea (w, x, y);
7473 #endif
7474 }
7475
7476 #ifndef XFlush
7477 XFlush (FRAME_X_DISPLAY (f));
7478 #endif
7479 }
7480
7481 \f
7482 /* Icons. */
7483
7484 /* Make the x-window of frame F use the gnu icon bitmap. */
7485
7486 int
7487 x_bitmap_icon (f, file)
7488 struct frame *f;
7489 Lisp_Object file;
7490 {
7491 int bitmap_id;
7492
7493 if (FRAME_X_WINDOW (f) == 0)
7494 return 1;
7495
7496 /* Free up our existing icon bitmap and mask if any. */
7497 if (f->output_data.x->icon_bitmap > 0)
7498 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7499 f->output_data.x->icon_bitmap = 0;
7500
7501 if (STRINGP (file))
7502 {
7503 #ifdef USE_GTK
7504 /* Use gtk_window_set_icon_from_file () if available,
7505 It's not restricted to bitmaps */
7506 if (xg_set_icon (f, file))
7507 return 0;
7508 #endif /* USE_GTK */
7509 bitmap_id = x_create_bitmap_from_file (f, file);
7510 x_create_bitmap_mask (f, bitmap_id);
7511 }
7512 else
7513 {
7514 /* Create the GNU bitmap and mask if necessary. */
7515 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
7516 {
7517 int rc = -1;
7518
7519 #ifdef USE_GTK
7520
7521 if (xg_set_icon (f, xg_default_icon_file)
7522 || xg_set_icon_from_xpm_data (f, gnu_xpm_bits))
7523 return 0;
7524
7525 #elif defined (HAVE_XPM) && defined (HAVE_X_WINDOWS)
7526
7527 rc = x_create_bitmap_from_xpm_data (f, gnu_xpm_bits);
7528 if (rc != -1)
7529 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id = rc;
7530
7531 #endif
7532
7533 /* If all else fails, use the (black and white) xbm image. */
7534 if (rc == -1)
7535 {
7536 rc = x_create_bitmap_from_data (f, gnu_xbm_bits,
7537 gnu_xbm_width, gnu_xbm_height);
7538 if (rc == -1)
7539 return 1;
7540
7541 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id = rc;
7542 x_create_bitmap_mask (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
7543 }
7544 }
7545
7546 /* The first time we create the GNU bitmap and mask,
7547 this increments the ref-count one extra time.
7548 As a result, the GNU bitmap and mask are never freed.
7549 That way, we don't have to worry about allocating it again. */
7550 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
7551
7552 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7553 }
7554
7555 x_wm_set_icon_pixmap (f, bitmap_id);
7556 f->output_data.x->icon_bitmap = bitmap_id;
7557
7558 return 0;
7559 }
7560
7561
7562 /* Make the x-window of frame F use a rectangle with text.
7563 Use ICON_NAME as the text. */
7564
7565 int
7566 x_text_icon (f, icon_name)
7567 struct frame *f;
7568 char *icon_name;
7569 {
7570 if (FRAME_X_WINDOW (f) == 0)
7571 return 1;
7572
7573 {
7574 XTextProperty text;
7575 text.value = (unsigned char *) icon_name;
7576 text.encoding = XA_STRING;
7577 text.format = 8;
7578 text.nitems = strlen (icon_name);
7579 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
7580 }
7581
7582 if (f->output_data.x->icon_bitmap > 0)
7583 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7584 f->output_data.x->icon_bitmap = 0;
7585 x_wm_set_icon_pixmap (f, 0);
7586
7587 return 0;
7588 }
7589 \f
7590 #define X_ERROR_MESSAGE_SIZE 200
7591
7592 /* If non-nil, this should be a string.
7593 It means catch X errors and store the error message in this string.
7594
7595 The reason we use a stack is that x_catch_error/x_uncatch_error can
7596 be called from a signal handler.
7597 */
7598
7599 struct x_error_message_stack {
7600 char string[X_ERROR_MESSAGE_SIZE];
7601 Display *dpy;
7602 struct x_error_message_stack *prev;
7603 };
7604 static struct x_error_message_stack *x_error_message;
7605
7606 /* An X error handler which stores the error message in
7607 *x_error_message. This is called from x_error_handler if
7608 x_catch_errors is in effect. */
7609
7610 static void
7611 x_error_catcher (display, error)
7612 Display *display;
7613 XErrorEvent *error;
7614 {
7615 XGetErrorText (display, error->error_code,
7616 x_error_message->string,
7617 X_ERROR_MESSAGE_SIZE);
7618 }
7619
7620 /* Begin trapping X errors for display DPY. Actually we trap X errors
7621 for all displays, but DPY should be the display you are actually
7622 operating on.
7623
7624 After calling this function, X protocol errors no longer cause
7625 Emacs to exit; instead, they are recorded in the string
7626 stored in *x_error_message.
7627
7628 Calling x_check_errors signals an Emacs error if an X error has
7629 occurred since the last call to x_catch_errors or x_check_errors.
7630
7631 Calling x_uncatch_errors resumes the normal error handling. */
7632
7633 void x_check_errors ();
7634
7635 void
7636 x_catch_errors (dpy)
7637 Display *dpy;
7638 {
7639 struct x_error_message_stack *data = xmalloc (sizeof (*data));
7640
7641 /* Make sure any errors from previous requests have been dealt with. */
7642 XSync (dpy, False);
7643
7644 data->dpy = dpy;
7645 data->string[0] = 0;
7646 data->prev = x_error_message;
7647 x_error_message = data;
7648 }
7649
7650 /* Undo the last x_catch_errors call.
7651 DPY should be the display that was passed to x_catch_errors. */
7652
7653 void
7654 x_uncatch_errors ()
7655 {
7656 struct x_error_message_stack *tmp;
7657
7658 BLOCK_INPUT;
7659
7660 /* The display may have been closed before this function is called.
7661 Check if it is still open before calling XSync. */
7662 if (x_display_info_for_display (x_error_message->dpy) != 0)
7663 XSync (x_error_message->dpy, False);
7664
7665 tmp = x_error_message;
7666 x_error_message = x_error_message->prev;
7667 xfree (tmp);
7668 UNBLOCK_INPUT;
7669 }
7670
7671 /* If any X protocol errors have arrived since the last call to
7672 x_catch_errors or x_check_errors, signal an Emacs error using
7673 sprintf (a buffer, FORMAT, the x error message text) as the text. */
7674
7675 void
7676 x_check_errors (dpy, format)
7677 Display *dpy;
7678 char *format;
7679 {
7680 /* Make sure to catch any errors incurred so far. */
7681 XSync (dpy, False);
7682
7683 if (x_error_message->string[0])
7684 {
7685 char string[X_ERROR_MESSAGE_SIZE];
7686 bcopy (x_error_message->string, string, X_ERROR_MESSAGE_SIZE);
7687 x_uncatch_errors ();
7688 error (format, string);
7689 }
7690 }
7691
7692 /* Nonzero if we had any X protocol errors
7693 since we did x_catch_errors on DPY. */
7694
7695 int
7696 x_had_errors_p (dpy)
7697 Display *dpy;
7698 {
7699 /* Make sure to catch any errors incurred so far. */
7700 XSync (dpy, False);
7701
7702 return x_error_message->string[0] != 0;
7703 }
7704
7705 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
7706
7707 void
7708 x_clear_errors (dpy)
7709 Display *dpy;
7710 {
7711 x_error_message->string[0] = 0;
7712 }
7713
7714 #if 0 /* See comment in unwind_to_catch why calling this is a bad
7715 * idea. --lorentey */
7716 /* Close off all unclosed x_catch_errors calls. */
7717
7718 void
7719 x_fully_uncatch_errors ()
7720 {
7721 while (x_error_message)
7722 x_uncatch_errors ();
7723 }
7724 #endif
7725
7726 /* Nonzero if x_catch_errors has been done and not yet canceled. */
7727
7728 int
7729 x_catching_errors ()
7730 {
7731 return x_error_message != 0;
7732 }
7733
7734 #if 0
7735 static unsigned int x_wire_count;
7736 x_trace_wire ()
7737 {
7738 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
7739 }
7740 #endif /* ! 0 */
7741
7742 \f
7743 /* Handle SIGPIPE, which can happen when the connection to a server
7744 simply goes away. SIGPIPE is handled by x_connection_signal.
7745 Don't need to do anything, because the write which caused the
7746 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
7747 which will do the appropriate cleanup for us. */
7748
7749 static SIGTYPE
7750 x_connection_signal (signalnum) /* If we don't have an argument, */
7751 int signalnum; /* some compilers complain in signal calls. */
7752 {
7753 #ifdef USG
7754 /* USG systems forget handlers when they are used;
7755 must reestablish each time */
7756 signal (signalnum, x_connection_signal);
7757 #endif /* USG */
7758 }
7759
7760 \f
7761 /************************************************************************
7762 Handling X errors
7763 ************************************************************************/
7764
7765 /* Error message passed to x_connection_closed. */
7766
7767 static char *error_msg;
7768
7769 /* Function installed as fatal_error_signal_hook in
7770 x_connection_closed. Print the X error message, and exit normally,
7771 instead of dumping core when XtCloseDisplay fails. */
7772
7773 static void
7774 x_fatal_error_signal ()
7775 {
7776 fprintf (stderr, "%s\n", error_msg);
7777 exit (70);
7778 }
7779
7780 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
7781 the text of an error message that lead to the connection loss. */
7782
7783 static SIGTYPE
7784 x_connection_closed (dpy, error_message)
7785 Display *dpy;
7786 char *error_message;
7787 {
7788 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
7789 Lisp_Object frame, tail;
7790 int index = SPECPDL_INDEX ();
7791
7792 error_msg = (char *) alloca (strlen (error_message) + 1);
7793 strcpy (error_msg, error_message);
7794 handling_signal = 0;
7795
7796 /* Prevent being called recursively because of an error condition
7797 below. Otherwise, we might end up with printing ``can't find per
7798 display information'' in the recursive call instead of printing
7799 the original message here. */
7800 x_catch_errors (dpy);
7801
7802 /* Inhibit redisplay while frames are being deleted. */
7803 specbind (Qinhibit_redisplay, Qt);
7804
7805 if (dpyinfo)
7806 {
7807 /* Protect display from being closed when we delete the last
7808 frame on it. */
7809 dpyinfo->reference_count++;
7810 dpyinfo->terminal->reference_count++;
7811 }
7812
7813 /* First delete frames whose mini-buffers are on frames
7814 that are on the dead display. */
7815 FOR_EACH_FRAME (tail, frame)
7816 {
7817 Lisp_Object minibuf_frame;
7818 minibuf_frame
7819 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
7820 if (FRAME_X_P (XFRAME (frame))
7821 && FRAME_X_P (XFRAME (minibuf_frame))
7822 && ! EQ (frame, minibuf_frame)
7823 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7824 delete_frame (frame, Qnoelisp);
7825 }
7826
7827 /* Now delete all remaining frames on the dead display.
7828 We are now sure none of these is used as the mini-buffer
7829 for another frame that we need to delete. */
7830 FOR_EACH_FRAME (tail, frame)
7831 if (FRAME_X_P (XFRAME (frame))
7832 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
7833 {
7834 /* Set this to t so that delete_frame won't get confused
7835 trying to find a replacement. */
7836 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
7837 delete_frame (frame, Qnoelisp);
7838 }
7839
7840 /* We have to close the display to inform Xt that it doesn't
7841 exist anymore. If we don't, Xt will continue to wait for
7842 events from the display. As a consequence, a sequence of
7843
7844 M-x make-frame-on-display RET :1 RET
7845 ...kill the new frame, so that we get an IO error...
7846 M-x make-frame-on-display RET :1 RET
7847
7848 will indefinitely wait in Xt for events for display `:1', opened
7849 in the first call to make-frame-on-display.
7850
7851 Closing the display is reported to lead to a bus error on
7852 OpenWindows in certain situations. I suspect that is a bug
7853 in OpenWindows. I don't know how to circumvent it here. */
7854
7855 if (dpyinfo)
7856 {
7857 #ifdef USE_X_TOOLKIT
7858 /* If DPYINFO is null, this means we didn't open the display
7859 in the first place, so don't try to close it. */
7860 {
7861 extern void (*fatal_error_signal_hook) P_ ((void));
7862 fatal_error_signal_hook = x_fatal_error_signal;
7863 XtCloseDisplay (dpy);
7864 fatal_error_signal_hook = NULL;
7865 }
7866 #endif
7867
7868 #ifdef USE_GTK
7869 /* Due to bugs in some Gtk+ versions, just exit here if this
7870 is the last display/terminal. */
7871 if (terminal_list->next_terminal == NULL)
7872 {
7873 fprintf (stderr, "%s\n", error_msg);
7874 shut_down_emacs (0, 0, Qnil);
7875 exit (70);
7876 }
7877 xg_display_close (dpyinfo->display);
7878 #endif
7879
7880 /* Indicate that this display is dead. */
7881 dpyinfo->display = 0;
7882
7883 dpyinfo->reference_count--;
7884 dpyinfo->terminal->reference_count--;
7885 if (dpyinfo->reference_count != 0)
7886 /* We have just closed all frames on this display. */
7887 abort ();
7888
7889 {
7890 Lisp_Object tmp;
7891 XSETTERMINAL (tmp, dpyinfo->terminal);
7892 Fdelete_terminal (tmp, Qnoelisp);
7893 }
7894 }
7895
7896 x_uncatch_errors ();
7897
7898 if (terminal_list == 0)
7899 {
7900 fprintf (stderr, "%s\n", error_msg);
7901 shut_down_emacs (0, 0, Qnil);
7902 exit (70);
7903 }
7904
7905 /* Ordinary stack unwind doesn't deal with these. */
7906 #ifdef SIGIO
7907 sigunblock (sigmask (SIGIO));
7908 #endif
7909 sigunblock (sigmask (SIGALRM));
7910 TOTALLY_UNBLOCK_INPUT;
7911
7912 unbind_to (index, Qnil);
7913 clear_waiting_for_input ();
7914 /* Here, we absolutely have to use a non-local exit (e.g. signal, throw,
7915 longjmp), because returning from this function would get us back into
7916 Xlib's code which will directly call `exit'. */
7917 error ("%s", error_msg);
7918 }
7919
7920 /* We specifically use it before defining it, so that gcc doesn't inline it,
7921 otherwise gdb doesn't know how to properly put a breakpoint on it. */
7922 static void x_error_quitter P_ ((Display *, XErrorEvent *));
7923
7924 /* This is the first-level handler for X protocol errors.
7925 It calls x_error_quitter or x_error_catcher. */
7926
7927 static int
7928 x_error_handler (display, error)
7929 Display *display;
7930 XErrorEvent *error;
7931 {
7932 if (x_error_message)
7933 x_error_catcher (display, error);
7934 else
7935 x_error_quitter (display, error);
7936 return 0;
7937 }
7938
7939 /* This is the usual handler for X protocol errors.
7940 It kills all frames on the display that we got the error for.
7941 If that was the only one, it prints an error message and kills Emacs. */
7942
7943 /* .gdbinit puts a breakpoint here, so make sure it is not inlined. */
7944
7945 #if __GNUC__ >= 3 /* On GCC 3.0 we might get a warning. */
7946 #define NO_INLINE __attribute__((noinline))
7947 #else
7948 #define NO_INLINE
7949 #endif
7950
7951 /* Some versions of GNU/Linux define noinline in their headers. */
7952
7953 #ifdef noinline
7954 #undef noinline
7955 #endif
7956
7957 /* On older GCC versions, just putting x_error_quitter
7958 after x_error_handler prevents inlining into the former. */
7959
7960 static void NO_INLINE
7961 x_error_quitter (display, error)
7962 Display *display;
7963 XErrorEvent *error;
7964 {
7965 char buf[256], buf1[356];
7966
7967 /* Ignore BadName errors. They can happen because of fonts
7968 or colors that are not defined. */
7969
7970 if (error->error_code == BadName)
7971 return;
7972
7973 /* Note that there is no real way portable across R3/R4 to get the
7974 original error handler. */
7975
7976 XGetErrorText (display, error->error_code, buf, sizeof (buf));
7977 sprintf (buf1, "X protocol error: %s on protocol request %d",
7978 buf, error->request_code);
7979 x_connection_closed (display, buf1);
7980 }
7981
7982
7983 /* This is the handler for X IO errors, always.
7984 It kills all frames on the display that we lost touch with.
7985 If that was the only one, it prints an error message and kills Emacs. */
7986
7987 static int
7988 x_io_error_quitter (display)
7989 Display *display;
7990 {
7991 char buf[256];
7992
7993 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
7994 x_connection_closed (display, buf);
7995 return 0;
7996 }
7997 \f
7998 /* Changing the font of the frame. */
7999
8000 /* Give frame F the font FONT-OBJECT as its default font. The return
8001 value is FONT-OBJECT. FONTSET is an ID of the fontset for the
8002 frame. If it is negative, generate a new fontset from
8003 FONT-OBJECT. */
8004
8005 Lisp_Object
8006 x_new_font (f, font_object, fontset)
8007 struct frame *f;
8008 Lisp_Object font_object;
8009 int fontset;
8010 {
8011 struct font *font = XFONT_OBJECT (font_object);
8012
8013 if (fontset < 0)
8014 fontset = fontset_from_font (font_object);
8015 FRAME_FONTSET (f) = fontset;
8016 if (FRAME_FONT (f) == font)
8017 /* This font is already set in frame F. There's nothing more to
8018 do. */
8019 return font_object;
8020
8021 FRAME_FONT (f) = font;
8022 FRAME_BASELINE_OFFSET (f) = font->baseline_offset;
8023 FRAME_COLUMN_WIDTH (f) = font->average_width;
8024 FRAME_SPACE_WIDTH (f) = font->space_width;
8025 FRAME_LINE_HEIGHT (f) = FONT_HEIGHT (font);
8026
8027 compute_fringe_widths (f, 1);
8028
8029 /* Compute the scroll bar width in character columns. */
8030 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
8031 {
8032 int wid = FRAME_COLUMN_WIDTH (f);
8033 FRAME_CONFIG_SCROLL_BAR_COLS (f)
8034 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + wid-1) / wid;
8035 }
8036 else
8037 {
8038 int wid = FRAME_COLUMN_WIDTH (f);
8039 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
8040 }
8041
8042 if (FRAME_X_WINDOW (f) != 0)
8043 {
8044 /* Don't change the size of a tip frame; there's no point in
8045 doing it because it's done in Fx_show_tip, and it leads to
8046 problems because the tip frame has no widget. */
8047 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
8048 {
8049 /* When the frame is maximized/fullscreen or running under for
8050 example Xmonad, x_set_window_size will be a no-op.
8051 In that case, the right thing to do is extend rows/cols to
8052 the current frame size. We do that first if x_set_window_size
8053 turns out to not be a no-op (there is no way to know).
8054 The size will be adjusted again if the frame gets a
8055 ConfigureNotify event as a result of x_set_window_size. */
8056 int pixelh = FRAME_PIXEL_HEIGHT (f);
8057 #ifdef USE_X_TOOLKIT
8058 /* The menu bar is not part of text lines. The tool bar
8059 is however. */
8060 pixelh -= FRAME_MENUBAR_HEIGHT (f);
8061 #endif
8062 int rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, pixelh);
8063 int cols = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, FRAME_PIXEL_WIDTH (f));
8064
8065 change_frame_size (f, rows, cols, 0, 1, 0);
8066 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
8067 }
8068 }
8069
8070 #ifdef HAVE_X_I18N
8071 if (FRAME_XIC (f)
8072 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
8073 {
8074 BLOCK_INPUT;
8075 xic_set_xfontset (f, SDATA (fontset_ascii (fontset)));
8076 UNBLOCK_INPUT;
8077 }
8078 #endif
8079
8080 return font_object;
8081 }
8082
8083 \f
8084 /***********************************************************************
8085 X Input Methods
8086 ***********************************************************************/
8087
8088 #ifdef HAVE_X_I18N
8089
8090 #ifdef HAVE_X11R6
8091
8092 /* XIM destroy callback function, which is called whenever the
8093 connection to input method XIM dies. CLIENT_DATA contains a
8094 pointer to the x_display_info structure corresponding to XIM. */
8095
8096 static void
8097 xim_destroy_callback (xim, client_data, call_data)
8098 XIM xim;
8099 XPointer client_data;
8100 XPointer call_data;
8101 {
8102 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
8103 Lisp_Object frame, tail;
8104
8105 BLOCK_INPUT;
8106
8107 /* No need to call XDestroyIC.. */
8108 FOR_EACH_FRAME (tail, frame)
8109 {
8110 struct frame *f = XFRAME (frame);
8111 if (FRAME_X_P (f) && FRAME_X_DISPLAY_INFO (f) == dpyinfo)
8112 {
8113 FRAME_XIC (f) = NULL;
8114 xic_free_xfontset (f);
8115 }
8116 }
8117
8118 /* No need to call XCloseIM. */
8119 dpyinfo->xim = NULL;
8120 XFree (dpyinfo->xim_styles);
8121 UNBLOCK_INPUT;
8122 }
8123
8124 #endif /* HAVE_X11R6 */
8125
8126 #ifdef HAVE_X11R6
8127 /* This isn't prototyped in OSF 5.0 or 5.1a. */
8128 extern char *XSetIMValues P_ ((XIM, ...));
8129 #endif
8130
8131 /* Open the connection to the XIM server on display DPYINFO.
8132 RESOURCE_NAME is the resource name Emacs uses. */
8133
8134 static void
8135 xim_open_dpy (dpyinfo, resource_name)
8136 struct x_display_info *dpyinfo;
8137 char *resource_name;
8138 {
8139 XIM xim;
8140
8141 #ifdef HAVE_XIM
8142 if (use_xim)
8143 {
8144 if (dpyinfo->xim)
8145 XCloseIM (dpyinfo->xim);
8146 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name,
8147 EMACS_CLASS);
8148 dpyinfo->xim = xim;
8149
8150 if (xim)
8151 {
8152 #ifdef HAVE_X11R6
8153 XIMCallback destroy;
8154 #endif
8155
8156 /* Get supported styles and XIM values. */
8157 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
8158
8159 #ifdef HAVE_X11R6
8160 destroy.callback = xim_destroy_callback;
8161 destroy.client_data = (XPointer)dpyinfo;
8162 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
8163 #endif
8164 }
8165 }
8166
8167 else
8168 #endif /* HAVE_XIM */
8169 dpyinfo->xim = NULL;
8170 }
8171
8172
8173 #ifdef HAVE_X11R6_XIM
8174
8175 /* XIM instantiate callback function, which is called whenever an XIM
8176 server is available. DISPLAY is the display of the XIM.
8177 CLIENT_DATA contains a pointer to an xim_inst_t structure created
8178 when the callback was registered. */
8179
8180 static void
8181 xim_instantiate_callback (display, client_data, call_data)
8182 Display *display;
8183 XPointer client_data;
8184 XPointer call_data;
8185 {
8186 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
8187 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
8188
8189 /* We don't support multiple XIM connections. */
8190 if (dpyinfo->xim)
8191 return;
8192
8193 xim_open_dpy (dpyinfo, xim_inst->resource_name);
8194
8195 /* Create XIC for the existing frames on the same display, as long
8196 as they have no XIC. */
8197 if (dpyinfo->xim && dpyinfo->reference_count > 0)
8198 {
8199 Lisp_Object tail, frame;
8200
8201 BLOCK_INPUT;
8202 FOR_EACH_FRAME (tail, frame)
8203 {
8204 struct frame *f = XFRAME (frame);
8205
8206 if (FRAME_X_P (f)
8207 && FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
8208 if (FRAME_XIC (f) == NULL)
8209 {
8210 create_frame_xic (f);
8211 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
8212 xic_set_statusarea (f);
8213 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
8214 {
8215 struct window *w = XWINDOW (f->selected_window);
8216 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
8217 }
8218 }
8219 }
8220
8221 UNBLOCK_INPUT;
8222 }
8223 }
8224
8225 #endif /* HAVE_X11R6_XIM */
8226
8227
8228 /* Open a connection to the XIM server on display DPYINFO.
8229 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
8230 connection only at the first time. On X11R6, open the connection
8231 in the XIM instantiate callback function. */
8232
8233 static void
8234 xim_initialize (dpyinfo, resource_name)
8235 struct x_display_info *dpyinfo;
8236 char *resource_name;
8237 {
8238 dpyinfo->xim = NULL;
8239 #ifdef HAVE_XIM
8240 if (use_xim)
8241 {
8242 #ifdef HAVE_X11R6_XIM
8243 struct xim_inst_t *xim_inst;
8244 int len;
8245
8246 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
8247 dpyinfo->xim_callback_data = xim_inst;
8248 xim_inst->dpyinfo = dpyinfo;
8249 len = strlen (resource_name);
8250 xim_inst->resource_name = (char *) xmalloc (len + 1);
8251 bcopy (resource_name, xim_inst->resource_name, len + 1);
8252 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8253 resource_name, EMACS_CLASS,
8254 xim_instantiate_callback,
8255 /* This is XPointer in XFree86
8256 but (XPointer *) on Tru64, at
8257 least, hence the configure test. */
8258 (XRegisterIMInstantiateCallback_arg6) xim_inst);
8259 #else /* not HAVE_X11R6_XIM */
8260 xim_open_dpy (dpyinfo, resource_name);
8261 #endif /* not HAVE_X11R6_XIM */
8262 }
8263 #endif /* HAVE_XIM */
8264 }
8265
8266
8267 /* Close the connection to the XIM server on display DPYINFO. */
8268
8269 static void
8270 xim_close_dpy (dpyinfo)
8271 struct x_display_info *dpyinfo;
8272 {
8273 #ifdef HAVE_XIM
8274 if (use_xim)
8275 {
8276 #ifdef HAVE_X11R6_XIM
8277 if (dpyinfo->display)
8278 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8279 NULL, EMACS_CLASS,
8280 xim_instantiate_callback, NULL);
8281 xfree (dpyinfo->xim_callback_data->resource_name);
8282 xfree (dpyinfo->xim_callback_data);
8283 #endif /* HAVE_X11R6_XIM */
8284 if (dpyinfo->display)
8285 XCloseIM (dpyinfo->xim);
8286 dpyinfo->xim = NULL;
8287 XFree (dpyinfo->xim_styles);
8288 }
8289 #endif /* HAVE_XIM */
8290 }
8291
8292 #endif /* not HAVE_X11R6_XIM */
8293
8294
8295 \f
8296 /* Calculate the absolute position in frame F
8297 from its current recorded position values and gravity. */
8298
8299 void
8300 x_calc_absolute_position (f)
8301 struct frame *f;
8302 {
8303 int flags = f->size_hint_flags;
8304
8305 /* We have nothing to do if the current position
8306 is already for the top-left corner. */
8307 if (! ((flags & XNegative) || (flags & YNegative)))
8308 return;
8309
8310 /* Treat negative positions as relative to the leftmost bottommost
8311 position that fits on the screen. */
8312 if (flags & XNegative)
8313 f->left_pos = x_display_pixel_width (FRAME_X_DISPLAY_INFO (f))
8314 - FRAME_PIXEL_WIDTH (f) + f->left_pos;
8315
8316 {
8317 int height = FRAME_PIXEL_HEIGHT (f);
8318
8319 #if defined USE_X_TOOLKIT && defined USE_MOTIF
8320 /* Something is fishy here. When using Motif, starting Emacs with
8321 `-g -0-0', the frame appears too low by a few pixels.
8322
8323 This seems to be so because initially, while Emacs is starting,
8324 the column widget's height and the frame's pixel height are
8325 different. The column widget's height is the right one. In
8326 later invocations, when Emacs is up, the frame's pixel height
8327 is right, though.
8328
8329 It's not obvious where the initial small difference comes from.
8330 2000-12-01, gerd. */
8331
8332 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
8333 #endif
8334
8335 if (flags & YNegative)
8336 f->top_pos = x_display_pixel_height (FRAME_X_DISPLAY_INFO (f))
8337 - height + f->top_pos;
8338 }
8339
8340 /* The left_pos and top_pos
8341 are now relative to the top and left screen edges,
8342 so the flags should correspond. */
8343 f->size_hint_flags &= ~ (XNegative | YNegative);
8344 }
8345
8346 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
8347 to really change the position, and 0 when calling from
8348 x_make_frame_visible (in that case, XOFF and YOFF are the current
8349 position values). It is -1 when calling from x_set_frame_parameters,
8350 which means, do adjust for borders but don't change the gravity. */
8351
8352 void
8353 x_set_offset (f, xoff, yoff, change_gravity)
8354 struct frame *f;
8355 register int xoff, yoff;
8356 int change_gravity;
8357 {
8358 int modified_top, modified_left;
8359
8360 if (change_gravity > 0)
8361 {
8362 FRAME_X_OUTPUT (f)->left_before_move = f->left_pos;
8363 FRAME_X_OUTPUT (f)->top_before_move = f->top_pos;
8364
8365 f->top_pos = yoff;
8366 f->left_pos = xoff;
8367 f->size_hint_flags &= ~ (XNegative | YNegative);
8368 if (xoff < 0)
8369 f->size_hint_flags |= XNegative;
8370 if (yoff < 0)
8371 f->size_hint_flags |= YNegative;
8372 f->win_gravity = NorthWestGravity;
8373 }
8374 x_calc_absolute_position (f);
8375
8376 BLOCK_INPUT;
8377 x_wm_set_size_hint (f, (long) 0, 0);
8378
8379 modified_left = f->left_pos;
8380 modified_top = f->top_pos;
8381
8382 if (change_gravity != 0 && FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A)
8383 {
8384 /* Some WMs (twm, wmaker at least) has an offset that is smaller
8385 than the WM decorations. So we use the calculated offset instead
8386 of the WM decoration sizes here (x/y_pixels_outer_diff). */
8387 modified_left += FRAME_X_OUTPUT (f)->move_offset_left;
8388 modified_top += FRAME_X_OUTPUT (f)->move_offset_top;
8389 }
8390
8391 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8392 modified_left, modified_top);
8393
8394 x_sync_with_move (f, f->left_pos, f->top_pos,
8395 FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
8396 ? 1 : 0);
8397
8398 /* change_gravity is non-zero when this function is called from Lisp to
8399 programmatically move a frame. In that case, we call
8400 x_check_expected_move to discover if we have a "Type A" or "Type B"
8401 window manager, and, for a "Type A" window manager, adjust the position
8402 of the frame.
8403
8404 We call x_check_expected_move if a programmatic move occurred, and
8405 either the window manager type (A/B) is unknown or it is Type A but we
8406 need to compute the top/left offset adjustment for this frame. */
8407
8408 if (change_gravity != 0 &&
8409 (FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
8410 || (FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A
8411 && (FRAME_X_OUTPUT (f)->move_offset_left == 0
8412 && FRAME_X_OUTPUT (f)->move_offset_top == 0))))
8413 x_check_expected_move (f, modified_left, modified_top);
8414
8415 UNBLOCK_INPUT;
8416 }
8417
8418 /* Return non-zero if _NET_SUPPORTING_WM_CHECK window exists and _NET_SUPPORTED
8419 on the root window for frame F contains ATOMNAME.
8420 This is how a WM check shall be done according to the Window Manager
8421 Specification/Extended Window Manager Hints at
8422 http://freedesktop.org/wiki/Specifications/wm-spec. */
8423
8424 static int
8425 wm_supports (f, atomname)
8426 struct frame *f;
8427 const char *atomname;
8428 {
8429 Atom actual_type;
8430 unsigned long actual_size, bytes_remaining;
8431 int i, rc, actual_format;
8432 Atom prop_atom;
8433 Window wmcheck_window;
8434 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8435 Window target_window = dpyinfo->root_window;
8436 long max_len = 65536;
8437 Display *dpy = FRAME_X_DISPLAY (f);
8438 unsigned char *tmp_data = NULL;
8439 Atom target_type = XA_WINDOW;
8440 Atom want_atom;
8441
8442 BLOCK_INPUT;
8443
8444 prop_atom = XInternAtom (dpy, "_NET_SUPPORTING_WM_CHECK", False);
8445
8446 x_catch_errors (dpy);
8447 rc = XGetWindowProperty (dpy, target_window,
8448 prop_atom, 0, max_len, False, target_type,
8449 &actual_type, &actual_format, &actual_size,
8450 &bytes_remaining, &tmp_data);
8451
8452 if (rc != Success || actual_type != XA_WINDOW || x_had_errors_p (dpy))
8453 {
8454 if (tmp_data) XFree (tmp_data);
8455 x_uncatch_errors ();
8456 UNBLOCK_INPUT;
8457 return 0;
8458 }
8459
8460 wmcheck_window = *(Window *) tmp_data;
8461 XFree (tmp_data);
8462
8463 /* Check if window exists. */
8464 XSelectInput (dpy, wmcheck_window, StructureNotifyMask);
8465 x_sync (f);
8466 if (x_had_errors_p (dpy))
8467 {
8468 x_uncatch_errors ();
8469 UNBLOCK_INPUT;
8470 return 0;
8471 }
8472
8473 if (dpyinfo->net_supported_window != wmcheck_window)
8474 {
8475 /* Window changed, reload atoms */
8476 if (dpyinfo->net_supported_atoms != NULL)
8477 XFree (dpyinfo->net_supported_atoms);
8478 dpyinfo->net_supported_atoms = NULL;
8479 dpyinfo->nr_net_supported_atoms = 0;
8480 dpyinfo->net_supported_window = 0;
8481
8482 target_type = XA_ATOM;
8483 prop_atom = XInternAtom (dpy, "_NET_SUPPORTED", False);
8484 tmp_data = NULL;
8485 rc = XGetWindowProperty (dpy, target_window,
8486 prop_atom, 0, max_len, False, target_type,
8487 &actual_type, &actual_format, &actual_size,
8488 &bytes_remaining, &tmp_data);
8489
8490 if (rc != Success || actual_type != XA_ATOM || x_had_errors_p (dpy))
8491 {
8492 if (tmp_data) XFree (tmp_data);
8493 x_uncatch_errors ();
8494 UNBLOCK_INPUT;
8495 return 0;
8496 }
8497
8498 dpyinfo->net_supported_atoms = (Atom *)tmp_data;
8499 dpyinfo->nr_net_supported_atoms = actual_size;
8500 dpyinfo->net_supported_window = wmcheck_window;
8501 }
8502
8503 rc = 0;
8504 want_atom = XInternAtom (dpy, atomname, False);
8505
8506 for (i = 0; rc == 0 && i < dpyinfo->nr_net_supported_atoms; ++i)
8507 rc = dpyinfo->net_supported_atoms[i] == want_atom;
8508
8509 x_uncatch_errors ();
8510 UNBLOCK_INPUT;
8511
8512 return rc;
8513 }
8514
8515 static void
8516 set_wm_state (frame, add, what, what2)
8517 Lisp_Object frame;
8518 int add;
8519 const char *what;
8520 const char *what2;
8521 {
8522 const char *atom = "_NET_WM_STATE";
8523 Fx_send_client_event (frame, make_number (0), frame,
8524 make_unibyte_string (atom, strlen (atom)),
8525 make_number (32),
8526 /* 1 = add, 0 = remove */
8527 Fcons
8528 (make_number (add ? 1 : 0),
8529 Fcons
8530 (make_unibyte_string (what, strlen (what)),
8531 what2 != 0
8532 ? Fcons (make_unibyte_string (what2, strlen (what2)),
8533 Qnil)
8534 : Qnil)));
8535 }
8536
8537 void
8538 x_set_sticky (f, new_value, old_value)
8539 struct frame *f;
8540 Lisp_Object new_value, old_value;
8541 {
8542 Lisp_Object frame;
8543
8544 XSETFRAME (frame, f);
8545 set_wm_state (frame, NILP (new_value) ? 0 : 1,
8546 "_NET_WM_STATE_STICKY", NULL);
8547 }
8548
8549 /* Do fullscreen as specified in extended window manager hints */
8550
8551 static int
8552 do_ewmh_fullscreen (f)
8553 struct frame *f;
8554 {
8555 int have_net_atom = wm_supports (f, "_NET_WM_STATE");
8556
8557 /* Some window managers don't say they support _NET_WM_STATE, but they do say
8558 they support _NET_WM_STATE_FULLSCREEN. Try that also. */
8559 if (!have_net_atom)
8560 have_net_atom = wm_supports (f, "_NET_WM_STATE_FULLSCREEN");
8561
8562 if (have_net_atom)
8563 {
8564 Lisp_Object frame;
8565 const char *fs = "_NET_WM_STATE_FULLSCREEN";
8566 const char *fw = "_NET_WM_STATE_MAXIMIZED_HORZ";
8567 const char *fh = "_NET_WM_STATE_MAXIMIZED_VERT";
8568
8569 XSETFRAME (frame, f);
8570
8571 set_wm_state (frame, 0, fs, NULL);
8572 set_wm_state (frame, 0, fh, NULL);
8573 set_wm_state (frame, 0, fw, NULL);
8574
8575 /* If there are _NET_ atoms we assume we have extended window manager
8576 hints. */
8577 switch (f->want_fullscreen)
8578 {
8579 case FULLSCREEN_BOTH:
8580 set_wm_state (frame, 1, fs, NULL);
8581 break;
8582 case FULLSCREEN_WIDTH:
8583 set_wm_state (frame, 1, fw, NULL);
8584 break;
8585 case FULLSCREEN_HEIGHT:
8586 set_wm_state (frame, 1, fh, NULL);
8587 break;
8588 case FULLSCREEN_MAXIMIZED:
8589 set_wm_state (frame, 1, fw, fh);
8590 break;
8591 }
8592
8593 f->want_fullscreen = FULLSCREEN_NONE;
8594
8595 }
8596
8597 return have_net_atom;
8598 }
8599
8600 static void
8601 XTfullscreen_hook (f)
8602 FRAME_PTR f;
8603 {
8604 if (f->async_visible)
8605 {
8606 BLOCK_INPUT;
8607 x_check_fullscreen (f);
8608 x_sync (f);
8609 UNBLOCK_INPUT;
8610 }
8611 }
8612
8613
8614 static void
8615 x_handle_net_wm_state (f, event)
8616 struct frame *f;
8617 XPropertyEvent *event;
8618 {
8619 Atom actual_type;
8620 unsigned long actual_size, bytes_remaining;
8621 int i, rc, actual_format, value = FULLSCREEN_NONE;
8622 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8623 long max_len = 65536;
8624 Display *dpy = FRAME_X_DISPLAY (f);
8625 unsigned char *tmp_data = NULL;
8626 Atom target_type = XA_ATOM;
8627 Lisp_Object lval;
8628 int sticky = 0;
8629
8630 BLOCK_INPUT;
8631 x_catch_errors (dpy);
8632 rc = XGetWindowProperty (dpy, event->window,
8633 event->atom, 0, max_len, False, target_type,
8634 &actual_type, &actual_format, &actual_size,
8635 &bytes_remaining, &tmp_data);
8636
8637 if (rc != Success || actual_type != target_type || x_had_errors_p (dpy))
8638 {
8639 if (tmp_data) XFree (tmp_data);
8640 x_uncatch_errors ();
8641 UNBLOCK_INPUT;
8642 return;
8643 }
8644
8645 x_uncatch_errors ();
8646
8647 for (i = 0; i < actual_size; ++i)
8648 {
8649 Atom a = ((Atom*)tmp_data)[i];
8650 if (a == dpyinfo->Xatom_net_wm_state_maximized_horz)
8651 {
8652 if (value == FULLSCREEN_HEIGHT)
8653 value = FULLSCREEN_MAXIMIZED;
8654 else
8655 value = FULLSCREEN_WIDTH;
8656 }
8657 else if (a == dpyinfo->Xatom_net_wm_state_maximized_vert)
8658 {
8659 if (value == FULLSCREEN_WIDTH)
8660 value = FULLSCREEN_MAXIMIZED;
8661 else
8662 value = FULLSCREEN_HEIGHT;
8663 }
8664 else if (a == dpyinfo->Xatom_net_wm_state_fullscreen_atom)
8665 value = FULLSCREEN_BOTH;
8666 else if (a == dpyinfo->Xatom_net_wm_state_sticky)
8667 sticky = 1;
8668 }
8669
8670 lval = Qnil;
8671 switch (value)
8672 {
8673 case FULLSCREEN_WIDTH:
8674 lval = Qfullwidth;
8675 break;
8676 case FULLSCREEN_HEIGHT:
8677 lval = Qfullheight;
8678 break;
8679 case FULLSCREEN_BOTH:
8680 lval = Qfullboth;
8681 break;
8682 case FULLSCREEN_MAXIMIZED:
8683 lval = Qmaximized;
8684 break;
8685 }
8686
8687 store_frame_param (f, Qfullscreen, lval);
8688 store_frame_param (f, Qsticky, sticky ? Qt : Qnil);
8689
8690 if (tmp_data) XFree (tmp_data);
8691 UNBLOCK_INPUT;
8692 }
8693
8694 /* Check if we need to resize the frame due to a fullscreen request.
8695 If so needed, resize the frame. */
8696 static void
8697 x_check_fullscreen (f)
8698 struct frame *f;
8699 {
8700 if (do_ewmh_fullscreen (f))
8701 return;
8702
8703 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
8704 return; /* Only fullscreen without WM or with EWM hints (above). */
8705
8706 if (f->want_fullscreen != FULLSCREEN_NONE)
8707 {
8708 int width = FRAME_COLS (f), height = FRAME_LINES (f);
8709 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8710
8711 switch (f->want_fullscreen)
8712 {
8713 /* No difference between these two when there is no WM */
8714 case FULLSCREEN_BOTH:
8715 case FULLSCREEN_MAXIMIZED:
8716 width = x_display_pixel_width (dpyinfo);
8717 height = x_display_pixel_height (dpyinfo);
8718 break;
8719 case FULLSCREEN_WIDTH:
8720 width = x_display_pixel_width (dpyinfo);
8721 break;
8722 case FULLSCREEN_HEIGHT:
8723 height = x_display_pixel_height (dpyinfo);
8724 }
8725
8726 if (FRAME_COLS (f) != width || FRAME_LINES (f) != height)
8727 {
8728 change_frame_size (f, height, width, 0, 1, 0);
8729 SET_FRAME_GARBAGED (f);
8730 cancel_mouse_face (f);
8731 }
8732 }
8733 }
8734
8735 /* This function is called by x_set_offset to determine whether the window
8736 manager interfered with the positioning of the frame. Type A window
8737 managers position the surrounding window manager decorations a small
8738 amount above and left of the user-supplied position. Type B window
8739 managers position the surrounding window manager decorations at the
8740 user-specified position. If we detect a Type A window manager, we
8741 compensate by moving the window right and down by the proper amount. */
8742
8743 static void
8744 x_check_expected_move (f, expected_left, expected_top)
8745 struct frame *f;
8746 int expected_left;
8747 int expected_top;
8748 {
8749 int current_left = 0, current_top = 0;
8750
8751 /* x_real_positions returns the left and top offsets of the outermost
8752 window manager window around the frame. */
8753
8754 x_real_positions (f, &current_left, &current_top);
8755
8756 if (current_left != expected_left || current_top != expected_top)
8757 {
8758 /* It's a "Type A" window manager. */
8759
8760 int adjusted_left;
8761 int adjusted_top;
8762
8763 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_A;
8764 FRAME_X_OUTPUT (f)->move_offset_left = expected_left - current_left;
8765 FRAME_X_OUTPUT (f)->move_offset_top = expected_top - current_top;
8766
8767 /* Now fix the mispositioned frame's location. */
8768
8769 adjusted_left = expected_left + FRAME_X_OUTPUT (f)->move_offset_left;
8770 adjusted_top = expected_top + FRAME_X_OUTPUT (f)->move_offset_top;
8771
8772 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8773 adjusted_left, adjusted_top);
8774
8775 x_sync_with_move (f, expected_left, expected_top, 0);
8776 }
8777 else
8778 /* It's a "Type B" window manager. We don't have to adjust the
8779 frame's position. */
8780
8781 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_B;
8782 }
8783
8784
8785 /* Wait for XGetGeometry to return up-to-date position information for a
8786 recently-moved frame. Call this immediately after calling XMoveWindow.
8787 If FUZZY is non-zero, then LEFT and TOP are just estimates of where the
8788 frame has been moved to, so we use a fuzzy position comparison instead
8789 of an exact comparison. */
8790
8791 static void
8792 x_sync_with_move (f, left, top, fuzzy)
8793 struct frame *f;
8794 int left, top, fuzzy;
8795 {
8796 int count = 0;
8797
8798 while (count++ < 50)
8799 {
8800 int current_left = 0, current_top = 0;
8801
8802 /* In theory, this call to XSync only needs to happen once, but in
8803 practice, it doesn't seem to work, hence the need for the surrounding
8804 loop. */
8805
8806 XSync (FRAME_X_DISPLAY (f), False);
8807 x_real_positions (f, &current_left, &current_top);
8808
8809 if (fuzzy)
8810 {
8811 /* The left fuzz-factor is 10 pixels. The top fuzz-factor is 40
8812 pixels. */
8813
8814 if (eabs (current_left - left) <= 10
8815 && eabs (current_top - top) <= 40)
8816 return;
8817 }
8818 else if (current_left == left && current_top == top)
8819 return;
8820 }
8821
8822 /* As a last resort, just wait 0.5 seconds and hope that XGetGeometry
8823 will then return up-to-date position info. */
8824
8825 wait_reading_process_output (0, 500000, 0, 0, Qnil, NULL, 0);
8826 }
8827
8828
8829 /* Wait for an event on frame F matching EVENTTYPE. */
8830 void
8831 x_wait_for_event (f, eventtype)
8832 struct frame *f;
8833 int eventtype;
8834 {
8835 int level = interrupt_input_blocked;
8836
8837 SELECT_TYPE fds;
8838 EMACS_TIME tmo, tmo_at, time_now;
8839 int fd = ConnectionNumber (FRAME_X_DISPLAY (f));
8840
8841 pending_event_wait.f = f;
8842 pending_event_wait.eventtype = eventtype;
8843
8844 /* Set timeout to 0.1 second. Hopefully not noticable.
8845 Maybe it should be configurable. */
8846 EMACS_SET_SECS_USECS (tmo, 0, 100000);
8847 EMACS_GET_TIME (tmo_at);
8848 EMACS_ADD_TIME (tmo_at, tmo_at, tmo);
8849
8850 while (pending_event_wait.eventtype)
8851 {
8852 interrupt_input_pending = 1;
8853 TOTALLY_UNBLOCK_INPUT;
8854 /* XTread_socket is called after unblock. */
8855 BLOCK_INPUT;
8856 interrupt_input_blocked = level;
8857
8858 FD_ZERO (&fds);
8859 FD_SET (fd, &fds);
8860
8861 EMACS_GET_TIME (time_now);
8862 EMACS_SUB_TIME (tmo, tmo_at, time_now);
8863
8864 if (EMACS_TIME_NEG_P (tmo) || select (fd+1, &fds, NULL, NULL, &tmo) == 0)
8865 break; /* Timeout */
8866 }
8867 pending_event_wait.f = 0;
8868 pending_event_wait.eventtype = 0;
8869 }
8870
8871
8872 /* Change the size of frame F's X window to COLS/ROWS in the case F
8873 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
8874 top-left-corner window gravity for this size change and subsequent
8875 size changes. Otherwise we leave the window gravity unchanged. */
8876
8877 static void
8878 x_set_window_size_1 (f, change_gravity, cols, rows)
8879 struct frame *f;
8880 int change_gravity;
8881 int cols, rows;
8882 {
8883 int pixelwidth, pixelheight;
8884
8885 check_frame_size (f, &rows, &cols);
8886 f->scroll_bar_actual_width
8887 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
8888 ? 0
8889 : FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0
8890 ? FRAME_CONFIG_SCROLL_BAR_WIDTH (f)
8891 : (FRAME_CONFIG_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f)));
8892
8893 compute_fringe_widths (f, 0);
8894
8895 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols)
8896 + 2*f->border_width;
8897 pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows)
8898 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f)
8899 + 2*f->border_width;
8900
8901 if (change_gravity) f->win_gravity = NorthWestGravity;
8902 x_wm_set_size_hint (f, (long) 0, 0);
8903 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8904 pixelwidth, pixelheight);
8905
8906
8907 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
8908 receive in the ConfigureNotify event; if we get what we asked
8909 for, then the event won't cause the screen to become garbaged, so
8910 we have to make sure to do it here. */
8911 SET_FRAME_GARBAGED (f);
8912
8913 /* Now, strictly speaking, we can't be sure that this is accurate,
8914 but the window manager will get around to dealing with the size
8915 change request eventually, and we'll hear how it went when the
8916 ConfigureNotify event gets here.
8917
8918 We could just not bother storing any of this information here,
8919 and let the ConfigureNotify event set everything up, but that
8920 might be kind of confusing to the Lisp code, since size changes
8921 wouldn't be reported in the frame parameters until some random
8922 point in the future when the ConfigureNotify event arrives.
8923
8924 We pass 1 for DELAY since we can't run Lisp code inside of
8925 a BLOCK_INPUT. */
8926
8927 /* But the ConfigureNotify may in fact never arrive, and then this is
8928 not right if the frame is visible. Instead wait (with timeout)
8929 for the ConfigureNotify. */
8930 if (f->async_visible)
8931 x_wait_for_event (f, ConfigureNotify);
8932 else
8933 {
8934 change_frame_size (f, rows, cols, 0, 1, 0);
8935 FRAME_PIXEL_WIDTH (f) = pixelwidth;
8936 FRAME_PIXEL_HEIGHT (f) = pixelheight;
8937 x_sync (f);
8938 }
8939 }
8940
8941
8942 /* Call this to change the size of frame F's x-window.
8943 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
8944 for this size change and subsequent size changes.
8945 Otherwise we leave the window gravity unchanged. */
8946
8947 void
8948 x_set_window_size (f, change_gravity, cols, rows)
8949 struct frame *f;
8950 int change_gravity;
8951 int cols, rows;
8952 {
8953 BLOCK_INPUT;
8954
8955 #ifdef USE_GTK
8956 if (FRAME_GTK_WIDGET (f))
8957 xg_frame_set_char_size (f, cols, rows);
8958 else
8959 x_set_window_size_1 (f, change_gravity, cols, rows);
8960 #else /* not USE_GTK */
8961
8962 x_set_window_size_1 (f, change_gravity, cols, rows);
8963
8964 #endif /* not USE_GTK */
8965
8966 /* If cursor was outside the new size, mark it as off. */
8967 mark_window_cursors_off (XWINDOW (f->root_window));
8968
8969 /* Clear out any recollection of where the mouse highlighting was,
8970 since it might be in a place that's outside the new frame size.
8971 Actually checking whether it is outside is a pain in the neck,
8972 so don't try--just let the highlighting be done afresh with new size. */
8973 cancel_mouse_face (f);
8974
8975 UNBLOCK_INPUT;
8976 }
8977 \f
8978 /* Mouse warping. */
8979
8980 void
8981 x_set_mouse_position (f, x, y)
8982 struct frame *f;
8983 int x, y;
8984 {
8985 int pix_x, pix_y;
8986
8987 pix_x = FRAME_COL_TO_PIXEL_X (f, x) + FRAME_COLUMN_WIDTH (f) / 2;
8988 pix_y = FRAME_LINE_TO_PIXEL_Y (f, y) + FRAME_LINE_HEIGHT (f) / 2;
8989
8990 if (pix_x < 0) pix_x = 0;
8991 if (pix_x > FRAME_PIXEL_WIDTH (f)) pix_x = FRAME_PIXEL_WIDTH (f);
8992
8993 if (pix_y < 0) pix_y = 0;
8994 if (pix_y > FRAME_PIXEL_HEIGHT (f)) pix_y = FRAME_PIXEL_HEIGHT (f);
8995
8996 BLOCK_INPUT;
8997
8998 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
8999 0, 0, 0, 0, pix_x, pix_y);
9000 UNBLOCK_INPUT;
9001 }
9002
9003 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
9004
9005 void
9006 x_set_mouse_pixel_position (f, pix_x, pix_y)
9007 struct frame *f;
9008 int pix_x, pix_y;
9009 {
9010 BLOCK_INPUT;
9011
9012 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
9013 0, 0, 0, 0, pix_x, pix_y);
9014 UNBLOCK_INPUT;
9015 }
9016 \f
9017 /* focus shifting, raising and lowering. */
9018
9019 void
9020 x_focus_on_frame (f)
9021 struct frame *f;
9022 {
9023 #if 0
9024 /* I don't think that the ICCCM allows programs to do things like this
9025 without the interaction of the window manager. Whatever you end up
9026 doing with this code, do it to x_unfocus_frame too. */
9027 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9028 RevertToPointerRoot, CurrentTime);
9029 #endif /* ! 0 */
9030 }
9031
9032 void
9033 x_unfocus_frame (f)
9034 struct frame *f;
9035 {
9036 #if 0
9037 /* Look at the remarks in x_focus_on_frame. */
9038 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
9039 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
9040 RevertToPointerRoot, CurrentTime);
9041 #endif /* ! 0 */
9042 }
9043
9044 /* Raise frame F. */
9045
9046 void
9047 x_raise_frame (f)
9048 struct frame *f;
9049 {
9050 BLOCK_INPUT;
9051 if (f->async_visible)
9052 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
9053
9054 XFlush (FRAME_X_DISPLAY (f));
9055 UNBLOCK_INPUT;
9056 }
9057
9058 /* Lower frame F. */
9059
9060 void
9061 x_lower_frame (f)
9062 struct frame *f;
9063 {
9064 if (f->async_visible)
9065 {
9066 BLOCK_INPUT;
9067 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
9068 XFlush (FRAME_X_DISPLAY (f));
9069 UNBLOCK_INPUT;
9070 }
9071 }
9072
9073 /* Activate frame with Extended Window Manager Hints */
9074
9075 void
9076 x_ewmh_activate_frame (f)
9077 FRAME_PTR f;
9078 {
9079 /* See Window Manager Specification/Extended Window Manager Hints at
9080 http://freedesktop.org/wiki/Specifications/wm-spec */
9081
9082 const char *atom = "_NET_ACTIVE_WINDOW";
9083 if (f->async_visible && wm_supports (f, atom))
9084 {
9085 Lisp_Object frame;
9086 XSETFRAME (frame, f);
9087 Fx_send_client_event (frame, make_number (0), frame,
9088 make_unibyte_string (atom, strlen (atom)),
9089 make_number (32),
9090 Fcons (make_number (1),
9091 Fcons (make_number (last_user_time),
9092 Qnil)));
9093 }
9094 }
9095
9096 static void
9097 XTframe_raise_lower (f, raise_flag)
9098 FRAME_PTR f;
9099 int raise_flag;
9100 {
9101 if (raise_flag)
9102 x_raise_frame (f);
9103 else
9104 x_lower_frame (f);
9105 }
9106 \f
9107 /* XEmbed implementation. */
9108
9109 void
9110 xembed_set_info (f, flags)
9111 struct frame *f;
9112 enum xembed_info flags;
9113 {
9114 Atom atom;
9115 unsigned long data[2];
9116
9117 atom = XInternAtom (FRAME_X_DISPLAY (f), "_XEMBED_INFO", False);
9118
9119 data[0] = XEMBED_VERSION;
9120 data[1] = flags;
9121
9122 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), atom, atom,
9123 32, PropModeReplace, (unsigned char *) data, 2);
9124 }
9125
9126 void
9127 xembed_send_message (f, time, message, detail, data1, data2)
9128 struct frame *f;
9129 Time time;
9130 enum xembed_message message;
9131 long detail;
9132 long data1;
9133 long data2;
9134 {
9135 XEvent event;
9136
9137 event.xclient.type = ClientMessage;
9138 event.xclient.window = FRAME_X_OUTPUT (f)->parent_desc;
9139 event.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_XEMBED;
9140 event.xclient.format = 32;
9141 event.xclient.data.l[0] = time;
9142 event.xclient.data.l[1] = message;
9143 event.xclient.data.l[2] = detail;
9144 event.xclient.data.l[3] = data1;
9145 event.xclient.data.l[4] = data2;
9146
9147 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_OUTPUT (f)->parent_desc,
9148 False, NoEventMask, &event);
9149 XSync (FRAME_X_DISPLAY (f), False);
9150 }
9151 \f
9152 /* Change of visibility. */
9153
9154 /* This tries to wait until the frame is really visible.
9155 However, if the window manager asks the user where to position
9156 the frame, this will return before the user finishes doing that.
9157 The frame will not actually be visible at that time,
9158 but it will become visible later when the window manager
9159 finishes with it. */
9160
9161 void
9162 x_make_frame_visible (f)
9163 struct frame *f;
9164 {
9165 Lisp_Object type;
9166 int original_top, original_left;
9167 int retry_count = 2;
9168
9169 retry:
9170
9171 BLOCK_INPUT;
9172
9173 type = x_icon_type (f);
9174 if (!NILP (type))
9175 x_bitmap_icon (f, type);
9176
9177 if (! FRAME_VISIBLE_P (f))
9178 {
9179 /* We test FRAME_GARBAGED_P here to make sure we don't
9180 call x_set_offset a second time
9181 if we get to x_make_frame_visible a second time
9182 before the window gets really visible. */
9183 if (! FRAME_ICONIFIED_P (f)
9184 && ! FRAME_X_EMBEDDED_P (f)
9185 && ! f->output_data.x->asked_for_visible)
9186 x_set_offset (f, f->left_pos, f->top_pos, 0);
9187
9188 f->output_data.x->asked_for_visible = 1;
9189
9190 if (! EQ (Vx_no_window_manager, Qt))
9191 x_wm_set_window_state (f, NormalState);
9192 #ifdef USE_X_TOOLKIT
9193 if (FRAME_X_EMBEDDED_P (f))
9194 xembed_set_info (f, XEMBED_MAPPED);
9195 else
9196 {
9197 /* This was XtPopup, but that did nothing for an iconified frame. */
9198 XtMapWidget (f->output_data.x->widget);
9199 }
9200 #else /* not USE_X_TOOLKIT */
9201 #ifdef USE_GTK
9202 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
9203 gtk_window_deiconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
9204 #else
9205 if (FRAME_X_EMBEDDED_P (f))
9206 xembed_set_info (f, XEMBED_MAPPED);
9207 else
9208 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9209 #endif /* not USE_GTK */
9210 #endif /* not USE_X_TOOLKIT */
9211 }
9212
9213 XFlush (FRAME_X_DISPLAY (f));
9214
9215 /* Synchronize to ensure Emacs knows the frame is visible
9216 before we do anything else. We do this loop with input not blocked
9217 so that incoming events are handled. */
9218 {
9219 Lisp_Object frame;
9220 int count;
9221 /* This must be before UNBLOCK_INPUT
9222 since events that arrive in response to the actions above
9223 will set it when they are handled. */
9224 int previously_visible = f->output_data.x->has_been_visible;
9225
9226 original_left = f->left_pos;
9227 original_top = f->top_pos;
9228
9229 /* This must come after we set COUNT. */
9230 UNBLOCK_INPUT;
9231
9232 /* We unblock here so that arriving X events are processed. */
9233
9234 /* Now move the window back to where it was "supposed to be".
9235 But don't do it if the gravity is negative.
9236 When the gravity is negative, this uses a position
9237 that is 3 pixels too low. Perhaps that's really the border width.
9238
9239 Don't do this if the window has never been visible before,
9240 because the window manager may choose the position
9241 and we don't want to override it. */
9242
9243 if (! FRAME_VISIBLE_P (f)
9244 && ! FRAME_ICONIFIED_P (f)
9245 && ! FRAME_X_EMBEDDED_P (f)
9246 && f->win_gravity == NorthWestGravity
9247 && previously_visible)
9248 {
9249 Drawable rootw;
9250 int x, y;
9251 unsigned int width, height, border, depth;
9252
9253 BLOCK_INPUT;
9254
9255 /* On some window managers (such as FVWM) moving an existing
9256 window, even to the same place, causes the window manager
9257 to introduce an offset. This can cause the window to move
9258 to an unexpected location. Check the geometry (a little
9259 slow here) and then verify that the window is in the right
9260 place. If the window is not in the right place, move it
9261 there, and take the potential window manager hit. */
9262 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9263 &rootw, &x, &y, &width, &height, &border, &depth);
9264
9265 if (original_left != x || original_top != y)
9266 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9267 original_left, original_top);
9268
9269 UNBLOCK_INPUT;
9270 }
9271
9272 XSETFRAME (frame, f);
9273
9274 /* Wait until the frame is visible. Process X events until a
9275 MapNotify event has been seen, or until we think we won't get a
9276 MapNotify at all.. */
9277 for (count = input_signal_count + 10;
9278 input_signal_count < count && !FRAME_VISIBLE_P (f);)
9279 {
9280 /* Force processing of queued events. */
9281 x_sync (f);
9282
9283 /* Machines that do polling rather than SIGIO have been
9284 observed to go into a busy-wait here. So we'll fake an
9285 alarm signal to let the handler know that there's something
9286 to be read. We used to raise a real alarm, but it seems
9287 that the handler isn't always enabled here. This is
9288 probably a bug. */
9289 if (input_polling_used ())
9290 {
9291 /* It could be confusing if a real alarm arrives while
9292 processing the fake one. Turn it off and let the
9293 handler reset it. */
9294 extern void poll_for_input_1 P_ ((void));
9295 int old_poll_suppress_count = poll_suppress_count;
9296 poll_suppress_count = 1;
9297 poll_for_input_1 ();
9298 poll_suppress_count = old_poll_suppress_count;
9299 }
9300
9301 /* See if a MapNotify event has been processed. */
9302 FRAME_SAMPLE_VISIBILITY (f);
9303 }
9304
9305 /* 2000-09-28: In
9306
9307 (let ((f (selected-frame)))
9308 (iconify-frame f)
9309 (raise-frame f))
9310
9311 the frame is not raised with various window managers on
9312 FreeBSD, GNU/Linux and Solaris. It turns out that, for some
9313 unknown reason, the call to XtMapWidget is completely ignored.
9314 Mapping the widget a second time works. */
9315
9316 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
9317 goto retry;
9318 }
9319 }
9320
9321 /* Change from mapped state to withdrawn state. */
9322
9323 /* Make the frame visible (mapped and not iconified). */
9324
9325 void
9326 x_make_frame_invisible (f)
9327 struct frame *f;
9328 {
9329 Window window;
9330
9331 /* Use the frame's outermost window, not the one we normally draw on. */
9332 window = FRAME_OUTER_WINDOW (f);
9333
9334 /* Don't keep the highlight on an invisible frame. */
9335 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
9336 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9337
9338 BLOCK_INPUT;
9339
9340 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
9341 that the current position of the window is user-specified, rather than
9342 program-specified, so that when the window is mapped again, it will be
9343 placed at the same location, without forcing the user to position it
9344 by hand again (they have already done that once for this window.) */
9345 x_wm_set_size_hint (f, (long) 0, 1);
9346
9347 #ifdef USE_GTK
9348 if (FRAME_GTK_OUTER_WIDGET (f))
9349 gtk_widget_hide (FRAME_GTK_OUTER_WIDGET (f));
9350 else
9351 #else
9352 if (FRAME_X_EMBEDDED_P (f))
9353 xembed_set_info (f, 0);
9354 else
9355 #endif
9356 {
9357
9358 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
9359 DefaultScreen (FRAME_X_DISPLAY (f))))
9360 {
9361 UNBLOCK_INPUT_RESIGNAL;
9362 error ("Can't notify window manager of window withdrawal");
9363 }
9364 }
9365
9366 /* We can't distinguish this from iconification
9367 just by the event that we get from the server.
9368 So we can't win using the usual strategy of letting
9369 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
9370 and synchronize with the server to make sure we agree. */
9371 f->visible = 0;
9372 FRAME_ICONIFIED_P (f) = 0;
9373 f->async_visible = 0;
9374 f->async_iconified = 0;
9375
9376 x_sync (f);
9377
9378 UNBLOCK_INPUT;
9379 }
9380
9381 /* Change window state from mapped to iconified. */
9382
9383 void
9384 x_iconify_frame (f)
9385 struct frame *f;
9386 {
9387 int result;
9388 Lisp_Object type;
9389
9390 /* Don't keep the highlight on an invisible frame. */
9391 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
9392 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9393
9394 if (f->async_iconified)
9395 return;
9396
9397 BLOCK_INPUT;
9398
9399 FRAME_SAMPLE_VISIBILITY (f);
9400
9401 type = x_icon_type (f);
9402 if (!NILP (type))
9403 x_bitmap_icon (f, type);
9404
9405 #ifdef USE_GTK
9406 if (FRAME_GTK_OUTER_WIDGET (f))
9407 {
9408 if (! FRAME_VISIBLE_P (f))
9409 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
9410
9411 gtk_window_iconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
9412 f->iconified = 1;
9413 f->visible = 1;
9414 f->async_iconified = 1;
9415 f->async_visible = 0;
9416 UNBLOCK_INPUT;
9417 return;
9418 }
9419 #endif
9420
9421 #ifdef USE_X_TOOLKIT
9422
9423 if (! FRAME_VISIBLE_P (f))
9424 {
9425 if (! EQ (Vx_no_window_manager, Qt))
9426 x_wm_set_window_state (f, IconicState);
9427 /* This was XtPopup, but that did nothing for an iconified frame. */
9428 XtMapWidget (f->output_data.x->widget);
9429 /* The server won't give us any event to indicate
9430 that an invisible frame was changed to an icon,
9431 so we have to record it here. */
9432 f->iconified = 1;
9433 f->visible = 1;
9434 f->async_iconified = 1;
9435 f->async_visible = 0;
9436 UNBLOCK_INPUT;
9437 return;
9438 }
9439
9440 result = XIconifyWindow (FRAME_X_DISPLAY (f),
9441 XtWindow (f->output_data.x->widget),
9442 DefaultScreen (FRAME_X_DISPLAY (f)));
9443 UNBLOCK_INPUT;
9444
9445 if (!result)
9446 error ("Can't notify window manager of iconification");
9447
9448 f->async_iconified = 1;
9449 f->async_visible = 0;
9450
9451
9452 BLOCK_INPUT;
9453 XFlush (FRAME_X_DISPLAY (f));
9454 UNBLOCK_INPUT;
9455 #else /* not USE_X_TOOLKIT */
9456
9457 /* Make sure the X server knows where the window should be positioned,
9458 in case the user deiconifies with the window manager. */
9459 if (! FRAME_VISIBLE_P (f)
9460 && ! FRAME_ICONIFIED_P (f)
9461 && ! FRAME_X_EMBEDDED_P (f))
9462 x_set_offset (f, f->left_pos, f->top_pos, 0);
9463
9464 /* Since we don't know which revision of X we're running, we'll use both
9465 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
9466
9467 /* X11R4: send a ClientMessage to the window manager using the
9468 WM_CHANGE_STATE type. */
9469 {
9470 XEvent message;
9471
9472 message.xclient.window = FRAME_X_WINDOW (f);
9473 message.xclient.type = ClientMessage;
9474 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
9475 message.xclient.format = 32;
9476 message.xclient.data.l[0] = IconicState;
9477
9478 if (! XSendEvent (FRAME_X_DISPLAY (f),
9479 DefaultRootWindow (FRAME_X_DISPLAY (f)),
9480 False,
9481 SubstructureRedirectMask | SubstructureNotifyMask,
9482 &message))
9483 {
9484 UNBLOCK_INPUT_RESIGNAL;
9485 error ("Can't notify window manager of iconification");
9486 }
9487 }
9488
9489 /* X11R3: set the initial_state field of the window manager hints to
9490 IconicState. */
9491 x_wm_set_window_state (f, IconicState);
9492
9493 if (!FRAME_VISIBLE_P (f))
9494 {
9495 /* If the frame was withdrawn, before, we must map it. */
9496 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9497 }
9498
9499 f->async_iconified = 1;
9500 f->async_visible = 0;
9501
9502 XFlush (FRAME_X_DISPLAY (f));
9503 UNBLOCK_INPUT;
9504 #endif /* not USE_X_TOOLKIT */
9505 }
9506
9507 \f
9508 /* Free X resources of frame F. */
9509
9510 void
9511 x_free_frame_resources (f)
9512 struct frame *f;
9513 {
9514 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9515 Lisp_Object bar;
9516 struct scroll_bar *b;
9517
9518 BLOCK_INPUT;
9519
9520 /* If a display connection is dead, don't try sending more
9521 commands to the X server. */
9522 if (dpyinfo->display)
9523 {
9524 /* We must free faces before destroying windows because some
9525 font-driver (e.g. xft) access a window while finishing a
9526 face. */
9527 if (FRAME_FACE_CACHE (f))
9528 free_frame_faces (f);
9529
9530 if (f->output_data.x->icon_desc)
9531 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
9532
9533 #ifdef USE_X_TOOLKIT
9534 /* Explicitly destroy the scroll bars of the frame. Without
9535 this, we get "BadDrawable" errors from the toolkit later on,
9536 presumably from expose events generated for the disappearing
9537 toolkit scroll bars. */
9538 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
9539 {
9540 b = XSCROLL_BAR (bar);
9541 x_scroll_bar_remove (b);
9542 }
9543 #endif
9544
9545 #ifdef HAVE_X_I18N
9546 if (FRAME_XIC (f))
9547 free_frame_xic (f);
9548 #endif
9549
9550 #ifdef USE_X_TOOLKIT
9551 if (f->output_data.x->widget)
9552 {
9553 XtDestroyWidget (f->output_data.x->widget);
9554 f->output_data.x->widget = NULL;
9555 }
9556 /* Tooltips don't have widgets, only a simple X window, even if
9557 we are using a toolkit. */
9558 else if (FRAME_X_WINDOW (f))
9559 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9560
9561 free_frame_menubar (f);
9562 #else /* !USE_X_TOOLKIT */
9563
9564 #ifdef USE_GTK
9565 /* In the GTK version, tooltips are normal X
9566 frames. We must check and free both types. */
9567 if (FRAME_GTK_OUTER_WIDGET (f))
9568 {
9569 gtk_widget_destroy (FRAME_GTK_OUTER_WIDGET (f));
9570 FRAME_X_WINDOW (f) = 0; /* Set to avoid XDestroyWindow below */
9571 FRAME_GTK_OUTER_WIDGET (f) = 0;
9572 }
9573 #endif /* USE_GTK */
9574
9575 if (FRAME_X_WINDOW (f))
9576 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9577 #endif /* !USE_X_TOOLKIT */
9578
9579 unload_color (f, FRAME_FOREGROUND_PIXEL (f));
9580 unload_color (f, FRAME_BACKGROUND_PIXEL (f));
9581 unload_color (f, f->output_data.x->cursor_pixel);
9582 unload_color (f, f->output_data.x->cursor_foreground_pixel);
9583 unload_color (f, f->output_data.x->border_pixel);
9584 unload_color (f, f->output_data.x->mouse_pixel);
9585
9586 if (f->output_data.x->scroll_bar_background_pixel != -1)
9587 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
9588 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9589 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
9590 #ifdef USE_TOOLKIT_SCROLL_BARS
9591 /* Scrollbar shadow colors. */
9592 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
9593 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
9594 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
9595 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
9596 #endif /* USE_TOOLKIT_SCROLL_BARS */
9597 if (f->output_data.x->white_relief.allocated_p)
9598 unload_color (f, f->output_data.x->white_relief.pixel);
9599 if (f->output_data.x->black_relief.allocated_p)
9600 unload_color (f, f->output_data.x->black_relief.pixel);
9601
9602 x_free_gcs (f);
9603 XFlush (FRAME_X_DISPLAY (f));
9604 }
9605
9606 xfree (f->output_data.x->saved_menu_event);
9607 xfree (f->output_data.x);
9608 f->output_data.x = NULL;
9609
9610 if (f == dpyinfo->x_focus_frame)
9611 dpyinfo->x_focus_frame = 0;
9612 if (f == dpyinfo->x_focus_event_frame)
9613 dpyinfo->x_focus_event_frame = 0;
9614 if (f == dpyinfo->x_highlight_frame)
9615 dpyinfo->x_highlight_frame = 0;
9616
9617 if (f == dpyinfo->mouse_face_mouse_frame)
9618 {
9619 dpyinfo->mouse_face_beg_row
9620 = dpyinfo->mouse_face_beg_col = -1;
9621 dpyinfo->mouse_face_end_row
9622 = dpyinfo->mouse_face_end_col = -1;
9623 dpyinfo->mouse_face_window = Qnil;
9624 dpyinfo->mouse_face_deferred_gc = 0;
9625 dpyinfo->mouse_face_mouse_frame = 0;
9626 }
9627
9628 UNBLOCK_INPUT;
9629 }
9630
9631
9632 /* Destroy the X window of frame F. */
9633
9634 void
9635 x_destroy_window (f)
9636 struct frame *f;
9637 {
9638 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9639
9640 /* If a display connection is dead, don't try sending more
9641 commands to the X server. */
9642 if (dpyinfo->display != 0)
9643 x_free_frame_resources (f);
9644
9645 dpyinfo->reference_count--;
9646 }
9647
9648 \f
9649 /* Setting window manager hints. */
9650
9651 /* Set the normal size hints for the window manager, for frame F.
9652 FLAGS is the flags word to use--or 0 meaning preserve the flags
9653 that the window now has.
9654 If USER_POSITION is nonzero, we set the USPosition
9655 flag (this is useful when FLAGS is 0).
9656 The GTK version is in gtkutils.c */
9657
9658 #ifndef USE_GTK
9659 void
9660 x_wm_set_size_hint (f, flags, user_position)
9661 struct frame *f;
9662 long flags;
9663 int user_position;
9664 {
9665 XSizeHints size_hints;
9666 Window window = FRAME_OUTER_WINDOW (f);
9667
9668 /* Setting PMaxSize caused various problems. */
9669 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
9670
9671 size_hints.x = f->left_pos;
9672 size_hints.y = f->top_pos;
9673
9674 size_hints.height = FRAME_PIXEL_HEIGHT (f);
9675 size_hints.width = FRAME_PIXEL_WIDTH (f);
9676
9677 size_hints.width_inc = FRAME_COLUMN_WIDTH (f);
9678 size_hints.height_inc = FRAME_LINE_HEIGHT (f);
9679 size_hints.max_width = x_display_pixel_width (FRAME_X_DISPLAY_INFO (f))
9680 - FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9681 size_hints.max_height = x_display_pixel_height (FRAME_X_DISPLAY_INFO (f))
9682 - FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
9683
9684 /* Calculate the base and minimum sizes. */
9685 {
9686 int base_width, base_height;
9687 int min_rows = 0, min_cols = 0;
9688
9689 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9690 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
9691
9692 check_frame_size (f, &min_rows, &min_cols);
9693
9694 /* The window manager uses the base width hints to calculate the
9695 current number of rows and columns in the frame while
9696 resizing; min_width and min_height aren't useful for this
9697 purpose, since they might not give the dimensions for a
9698 zero-row, zero-column frame.
9699
9700 We use the base_width and base_height members if we have
9701 them; otherwise, we set the min_width and min_height members
9702 to the size for a zero x zero frame. */
9703
9704 size_hints.flags |= PBaseSize;
9705 size_hints.base_width = base_width;
9706 size_hints.base_height = base_height + FRAME_MENUBAR_HEIGHT (f);
9707 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
9708 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
9709 }
9710
9711 /* If we don't need the old flags, we don't need the old hint at all. */
9712 if (flags)
9713 {
9714 size_hints.flags |= flags;
9715 goto no_read;
9716 }
9717
9718 {
9719 XSizeHints hints; /* Sometimes I hate X Windows... */
9720 long supplied_return;
9721 int value;
9722
9723 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
9724 &supplied_return);
9725
9726 if (flags)
9727 size_hints.flags |= flags;
9728 else
9729 {
9730 if (value == 0)
9731 hints.flags = 0;
9732 if (hints.flags & PSize)
9733 size_hints.flags |= PSize;
9734 if (hints.flags & PPosition)
9735 size_hints.flags |= PPosition;
9736 if (hints.flags & USPosition)
9737 size_hints.flags |= USPosition;
9738 if (hints.flags & USSize)
9739 size_hints.flags |= USSize;
9740 }
9741 }
9742
9743 no_read:
9744
9745 #ifdef PWinGravity
9746 size_hints.win_gravity = f->win_gravity;
9747 size_hints.flags |= PWinGravity;
9748
9749 if (user_position)
9750 {
9751 size_hints.flags &= ~ PPosition;
9752 size_hints.flags |= USPosition;
9753 }
9754 #endif /* PWinGravity */
9755
9756 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
9757 }
9758 #endif /* not USE_GTK */
9759
9760 /* Used for IconicState or NormalState */
9761
9762 void
9763 x_wm_set_window_state (f, state)
9764 struct frame *f;
9765 int state;
9766 {
9767 #ifdef USE_X_TOOLKIT
9768 Arg al[1];
9769
9770 XtSetArg (al[0], XtNinitialState, state);
9771 XtSetValues (f->output_data.x->widget, al, 1);
9772 #else /* not USE_X_TOOLKIT */
9773 Window window = FRAME_X_WINDOW (f);
9774
9775 f->output_data.x->wm_hints.flags |= StateHint;
9776 f->output_data.x->wm_hints.initial_state = state;
9777
9778 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9779 #endif /* not USE_X_TOOLKIT */
9780 }
9781
9782 void
9783 x_wm_set_icon_pixmap (f, pixmap_id)
9784 struct frame *f;
9785 int pixmap_id;
9786 {
9787 Pixmap icon_pixmap, icon_mask;
9788
9789 #ifndef USE_X_TOOLKIT
9790 Window window = FRAME_OUTER_WINDOW (f);
9791 #endif
9792
9793 if (pixmap_id > 0)
9794 {
9795 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
9796 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
9797 icon_mask = x_bitmap_mask (f, pixmap_id);
9798 f->output_data.x->wm_hints.icon_mask = icon_mask;
9799 }
9800 else
9801 {
9802 /* It seems there is no way to turn off use of an icon
9803 pixmap. */
9804 return;
9805 }
9806
9807
9808 #ifdef USE_GTK
9809 {
9810 xg_set_frame_icon (f, icon_pixmap, icon_mask);
9811 return;
9812 }
9813
9814 #elif defined (USE_X_TOOLKIT) /* same as in x_wm_set_window_state. */
9815
9816 {
9817 Arg al[1];
9818 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
9819 XtSetValues (f->output_data.x->widget, al, 1);
9820 XtSetArg (al[0], XtNiconMask, icon_mask);
9821 XtSetValues (f->output_data.x->widget, al, 1);
9822 }
9823
9824 #else /* not USE_X_TOOLKIT && not USE_GTK */
9825
9826 f->output_data.x->wm_hints.flags |= (IconPixmapHint | IconMaskHint);
9827 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9828
9829 #endif /* not USE_X_TOOLKIT && not USE_GTK */
9830 }
9831
9832 void
9833 x_wm_set_icon_position (f, icon_x, icon_y)
9834 struct frame *f;
9835 int icon_x, icon_y;
9836 {
9837 Window window = FRAME_OUTER_WINDOW (f);
9838
9839 f->output_data.x->wm_hints.flags |= IconPositionHint;
9840 f->output_data.x->wm_hints.icon_x = icon_x;
9841 f->output_data.x->wm_hints.icon_y = icon_y;
9842
9843 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9844 }
9845
9846 \f
9847 /***********************************************************************
9848 Fonts
9849 ***********************************************************************/
9850
9851 #if GLYPH_DEBUG
9852
9853 /* Check that FONT is valid on frame F. It is if it can be found in F's
9854 font table. */
9855
9856 static void
9857 x_check_font (f, font)
9858 struct frame *f;
9859 struct font *font;
9860 {
9861 Lisp_Object frame;
9862
9863 xassert (font != NULL && ! NILP (font->props[FONT_TYPE_INDEX]));
9864 if (font->driver->check)
9865 xassert (font->driver->check (f, font) == 0);
9866 }
9867
9868 #endif /* GLYPH_DEBUG != 0 */
9869
9870 \f
9871 /***********************************************************************
9872 Initialization
9873 ***********************************************************************/
9874
9875 #ifdef USE_X_TOOLKIT
9876 static XrmOptionDescRec emacs_options[] = {
9877 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
9878 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
9879
9880 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
9881 XrmoptionSepArg, NULL},
9882 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
9883
9884 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
9885 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
9886 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
9887 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
9888 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
9889 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
9890 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
9891 };
9892
9893 /* Whether atimer for Xt timeouts is activated or not. */
9894
9895 static int x_timeout_atimer_activated_flag;
9896
9897 #endif /* USE_X_TOOLKIT */
9898
9899 static int x_initialized;
9900
9901 #ifdef HAVE_X_SM
9902 static int x_session_initialized;
9903 #endif
9904
9905 /* Test whether two display-name strings agree up to the dot that separates
9906 the screen number from the server number. */
9907 static int
9908 same_x_server (name1, name2)
9909 const char *name1, *name2;
9910 {
9911 int seen_colon = 0;
9912 const unsigned char *system_name = SDATA (Vsystem_name);
9913 int system_name_length = strlen (system_name);
9914 int length_until_period = 0;
9915
9916 while (system_name[length_until_period] != 0
9917 && system_name[length_until_period] != '.')
9918 length_until_period++;
9919
9920 /* Treat `unix' like an empty host name. */
9921 if (! strncmp (name1, "unix:", 5))
9922 name1 += 4;
9923 if (! strncmp (name2, "unix:", 5))
9924 name2 += 4;
9925 /* Treat this host's name like an empty host name. */
9926 if (! strncmp (name1, system_name, system_name_length)
9927 && name1[system_name_length] == ':')
9928 name1 += system_name_length;
9929 if (! strncmp (name2, system_name, system_name_length)
9930 && name2[system_name_length] == ':')
9931 name2 += system_name_length;
9932 /* Treat this host's domainless name like an empty host name. */
9933 if (! strncmp (name1, system_name, length_until_period)
9934 && name1[length_until_period] == ':')
9935 name1 += length_until_period;
9936 if (! strncmp (name2, system_name, length_until_period)
9937 && name2[length_until_period] == ':')
9938 name2 += length_until_period;
9939
9940 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
9941 {
9942 if (*name1 == ':')
9943 seen_colon++;
9944 if (seen_colon && *name1 == '.')
9945 return 1;
9946 }
9947 return (seen_colon
9948 && (*name1 == '.' || *name1 == '\0')
9949 && (*name2 == '.' || *name2 == '\0'));
9950 }
9951
9952 /* Count number of set bits in mask and number of bits to shift to
9953 get to the first bit. With MASK 0x7e0, *BITS is set to 6, and *OFFSET
9954 to 5. */
9955 static void
9956 get_bits_and_offset (mask, bits, offset)
9957 unsigned long mask;
9958 int *bits;
9959 int *offset;
9960 {
9961 int nr = 0;
9962 int off = 0;
9963
9964 while (!(mask & 1))
9965 {
9966 off++;
9967 mask >>= 1;
9968 }
9969
9970 while (mask & 1)
9971 {
9972 nr++;
9973 mask >>= 1;
9974 }
9975
9976 *offset = off;
9977 *bits = nr;
9978 }
9979
9980 /* Return 1 if display DISPLAY is available for use, 0 otherwise.
9981 But don't permanently open it, just test its availability. */
9982
9983 int
9984 x_display_ok (display)
9985 const char *display;
9986 {
9987 int dpy_ok = 1;
9988 Display *dpy;
9989
9990 dpy = XOpenDisplay (display);
9991 if (dpy)
9992 XCloseDisplay (dpy);
9993 else
9994 dpy_ok = 0;
9995 return dpy_ok;
9996 }
9997
9998 /* Open a connection to X display DISPLAY_NAME, and return
9999 the structure that describes the open display.
10000 If we cannot contact the display, return null. */
10001
10002 struct x_display_info *
10003 x_term_init (display_name, xrm_option, resource_name)
10004 Lisp_Object display_name;
10005 char *xrm_option;
10006 char *resource_name;
10007 {
10008 int connection;
10009 Display *dpy;
10010 struct terminal *terminal;
10011 struct x_display_info *dpyinfo;
10012 XrmDatabase xrdb;
10013
10014 BLOCK_INPUT;
10015
10016 if (!x_initialized)
10017 {
10018 x_initialize ();
10019 ++x_initialized;
10020 }
10021
10022 if (! x_display_ok (SDATA (display_name)))
10023 error ("Display %s can't be opened", SDATA (display_name));
10024
10025 #ifdef USE_GTK
10026 {
10027 #define NUM_ARGV 10
10028 int argc;
10029 char *argv[NUM_ARGV];
10030 char **argv2 = argv;
10031 GdkAtom atom;
10032
10033 #ifndef HAVE_GTK_MULTIDISPLAY
10034 if (!EQ (Vinitial_window_system, Qx))
10035 error ("Sorry, you cannot connect to X servers with the GTK toolkit");
10036 #endif
10037
10038 if (x_initialized++ > 1)
10039 {
10040 #ifdef HAVE_GTK_MULTIDISPLAY
10041 /* Opening another display. If xg_display_open returns less
10042 than zero, we are probably on GTK 2.0, which can only handle
10043 one display. GTK 2.2 or later can handle more than one. */
10044 if (xg_display_open (SDATA (display_name), &dpy) < 0)
10045 #endif
10046 error ("Sorry, this version of GTK can only handle one display");
10047 }
10048 else
10049 {
10050 for (argc = 0; argc < NUM_ARGV; ++argc)
10051 argv[argc] = 0;
10052
10053 argc = 0;
10054 argv[argc++] = initial_argv[0];
10055
10056 if (! NILP (display_name))
10057 {
10058 argv[argc++] = "--display";
10059 argv[argc++] = SDATA (display_name);
10060 }
10061
10062 argv[argc++] = "--name";
10063 argv[argc++] = resource_name;
10064
10065 XSetLocaleModifiers ("");
10066
10067 gtk_init (&argc, &argv2);
10068
10069 /* gtk_init does set_locale. We must fix locale after calling it. */
10070 fixup_locale ();
10071 xg_initialize ();
10072
10073 dpy = GDK_DISPLAY ();
10074
10075 /* NULL window -> events for all windows go to our function */
10076 gdk_window_add_filter (NULL, event_handler_gdk, NULL);
10077
10078 /* Load our own gtkrc if it exists. */
10079 {
10080 char *file = "~/.emacs.d/gtkrc";
10081 Lisp_Object s, abs_file;
10082
10083 s = make_string (file, strlen (file));
10084 abs_file = Fexpand_file_name (s, Qnil);
10085
10086 if (! NILP (abs_file) && !NILP (Ffile_readable_p (abs_file)))
10087 gtk_rc_parse (SDATA (abs_file));
10088 }
10089
10090 XSetErrorHandler (x_error_handler);
10091 XSetIOErrorHandler (x_io_error_quitter);
10092 }
10093 }
10094 #else /* not USE_GTK */
10095 #ifdef USE_X_TOOLKIT
10096 /* weiner@footloose.sps.mot.com reports that this causes
10097 errors with X11R5:
10098 X protocol error: BadAtom (invalid Atom parameter)
10099 on protocol request 18skiloaf.
10100 So let's not use it until R6. */
10101 #ifdef HAVE_X11XTR6
10102 XtSetLanguageProc (NULL, NULL, NULL);
10103 #endif
10104
10105 {
10106 int argc = 0;
10107 char *argv[3];
10108
10109 argv[0] = "";
10110 argc = 1;
10111 if (xrm_option)
10112 {
10113 argv[argc++] = "-xrm";
10114 argv[argc++] = xrm_option;
10115 }
10116 turn_on_atimers (0);
10117 dpy = XtOpenDisplay (Xt_app_con, SDATA (display_name),
10118 resource_name, EMACS_CLASS,
10119 emacs_options, XtNumber (emacs_options),
10120 &argc, argv);
10121 turn_on_atimers (1);
10122
10123 #ifdef HAVE_X11XTR6
10124 /* I think this is to compensate for XtSetLanguageProc. */
10125 fixup_locale ();
10126 #endif
10127 }
10128
10129 #else /* not USE_X_TOOLKIT */
10130 XSetLocaleModifiers ("");
10131 dpy = XOpenDisplay (SDATA (display_name));
10132 #endif /* not USE_X_TOOLKIT */
10133 #endif /* not USE_GTK*/
10134
10135 /* Detect failure. */
10136 if (dpy == 0)
10137 {
10138 UNBLOCK_INPUT;
10139 return 0;
10140 }
10141
10142 /* We have definitely succeeded. Record the new connection. */
10143
10144 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
10145 bzero (dpyinfo, sizeof *dpyinfo);
10146
10147 terminal = x_create_terminal (dpyinfo);
10148
10149 {
10150 struct x_display_info *share;
10151 Lisp_Object tail;
10152
10153 for (share = x_display_list, tail = x_display_name_list; share;
10154 share = share->next, tail = XCDR (tail))
10155 if (same_x_server (SDATA (XCAR (XCAR (tail))),
10156 SDATA (display_name)))
10157 break;
10158 if (share)
10159 terminal->kboard = share->terminal->kboard;
10160 else
10161 {
10162 terminal->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
10163 init_kboard (terminal->kboard);
10164 terminal->kboard->Vwindow_system = Qx;
10165 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
10166 {
10167 char *vendor = ServerVendor (dpy);
10168 /* Temporarily hide the partially initialized terminal */
10169 terminal_list = terminal->next_terminal;
10170 UNBLOCK_INPUT;
10171 terminal->kboard->Vsystem_key_alist
10172 = call1 (Qvendor_specific_keysyms,
10173 vendor ? build_string (vendor) : empty_unibyte_string);
10174 BLOCK_INPUT;
10175 terminal->next_terminal = terminal_list;
10176 terminal_list = terminal;
10177 }
10178
10179 terminal->kboard->next_kboard = all_kboards;
10180 all_kboards = terminal->kboard;
10181 /* Don't let the initial kboard remain current longer than necessary.
10182 That would cause problems if a file loaded on startup tries to
10183 prompt in the mini-buffer. */
10184 if (current_kboard == initial_kboard)
10185 current_kboard = terminal->kboard;
10186 }
10187 terminal->kboard->reference_count++;
10188 }
10189
10190 /* Put this display on the chain. */
10191 dpyinfo->next = x_display_list;
10192 x_display_list = dpyinfo;
10193
10194 /* Put it on x_display_name_list as well, to keep them parallel. */
10195 x_display_name_list = Fcons (Fcons (display_name, Qnil),
10196 x_display_name_list);
10197 dpyinfo->name_list_element = XCAR (x_display_name_list);
10198
10199 dpyinfo->display = dpy;
10200
10201 /* Set the name of the terminal. */
10202 terminal->name = (char *) xmalloc (SBYTES (display_name) + 1);
10203 strncpy (terminal->name, SDATA (display_name), SBYTES (display_name));
10204 terminal->name[SBYTES (display_name)] = 0;
10205
10206 #if 0
10207 XSetAfterFunction (x_current_display, x_trace_wire);
10208 #endif /* ! 0 */
10209
10210 dpyinfo->x_id_name
10211 = (char *) xmalloc (SBYTES (Vinvocation_name)
10212 + SBYTES (Vsystem_name)
10213 + 2);
10214 sprintf (dpyinfo->x_id_name, "%s@%s",
10215 SDATA (Vinvocation_name), SDATA (Vsystem_name));
10216
10217 /* Figure out which modifier bits mean what. */
10218 x_find_modifier_meanings (dpyinfo);
10219
10220 /* Get the scroll bar cursor. */
10221 #ifdef USE_GTK
10222 /* We must create a GTK cursor, it is required for GTK widgets. */
10223 dpyinfo->xg_cursor = xg_create_default_cursor (dpyinfo->display);
10224 #endif /* USE_GTK */
10225
10226 dpyinfo->vertical_scroll_bar_cursor
10227 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
10228
10229 xrdb = x_load_resources (dpyinfo->display, xrm_option,
10230 resource_name, EMACS_CLASS);
10231 #ifdef HAVE_XRMSETDATABASE
10232 XrmSetDatabase (dpyinfo->display, xrdb);
10233 #else
10234 dpyinfo->display->db = xrdb;
10235 #endif
10236 /* Put the rdb where we can find it in a way that works on
10237 all versions. */
10238 dpyinfo->xrdb = xrdb;
10239
10240 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
10241 DefaultScreen (dpyinfo->display));
10242 select_visual (dpyinfo);
10243 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
10244 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
10245 dpyinfo->client_leader_window = 0;
10246 dpyinfo->grabbed = 0;
10247 dpyinfo->reference_count = 0;
10248 dpyinfo->icon_bitmap_id = -1;
10249 dpyinfo->n_fonts = 0;
10250 dpyinfo->bitmaps = 0;
10251 dpyinfo->bitmaps_size = 0;
10252 dpyinfo->bitmaps_last = 0;
10253 dpyinfo->scratch_cursor_gc = 0;
10254 dpyinfo->mouse_face_mouse_frame = 0;
10255 dpyinfo->mouse_face_deferred_gc = 0;
10256 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
10257 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
10258 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
10259 dpyinfo->mouse_face_window = Qnil;
10260 dpyinfo->mouse_face_overlay = Qnil;
10261 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
10262 dpyinfo->mouse_face_defer = 0;
10263 dpyinfo->mouse_face_hidden = 0;
10264 dpyinfo->x_focus_frame = 0;
10265 dpyinfo->x_focus_event_frame = 0;
10266 dpyinfo->x_highlight_frame = 0;
10267 dpyinfo->wm_type = X_WMTYPE_UNKNOWN;
10268
10269 /* See if we can construct pixel values from RGB values. */
10270 dpyinfo->red_bits = dpyinfo->blue_bits = dpyinfo->green_bits = 0;
10271 dpyinfo->red_offset = dpyinfo->blue_offset = dpyinfo->green_offset = 0;
10272
10273 if (dpyinfo->visual->class == TrueColor)
10274 {
10275 get_bits_and_offset (dpyinfo->visual->red_mask,
10276 &dpyinfo->red_bits, &dpyinfo->red_offset);
10277 get_bits_and_offset (dpyinfo->visual->blue_mask,
10278 &dpyinfo->blue_bits, &dpyinfo->blue_offset);
10279 get_bits_and_offset (dpyinfo->visual->green_mask,
10280 &dpyinfo->green_bits, &dpyinfo->green_offset);
10281 }
10282
10283 /* See if a private colormap is requested. */
10284 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
10285 {
10286 if (dpyinfo->visual->class == PseudoColor)
10287 {
10288 Lisp_Object value;
10289 value = display_x_get_resource (dpyinfo,
10290 build_string ("privateColormap"),
10291 build_string ("PrivateColormap"),
10292 Qnil, Qnil);
10293 if (STRINGP (value)
10294 && (!strcmp (SDATA (value), "true")
10295 || !strcmp (SDATA (value), "on")))
10296 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
10297 }
10298 }
10299 else
10300 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
10301 dpyinfo->visual, AllocNone);
10302
10303 {
10304 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
10305 double pixels = DisplayHeight (dpyinfo->display, screen_number);
10306 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
10307 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
10308 dpyinfo->resy = (mm < 1) ? 100 : pixels * 25.4 / mm;
10309 pixels = DisplayWidth (dpyinfo->display, screen_number);
10310 mm = DisplayWidthMM (dpyinfo->display, screen_number);
10311 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
10312 dpyinfo->resx = (mm < 1) ? 100 : pixels * 25.4 / mm;
10313 }
10314
10315 dpyinfo->Xatom_wm_protocols
10316 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
10317 dpyinfo->Xatom_wm_take_focus
10318 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
10319 dpyinfo->Xatom_wm_save_yourself
10320 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
10321 dpyinfo->Xatom_wm_delete_window
10322 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
10323 dpyinfo->Xatom_wm_change_state
10324 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
10325 dpyinfo->Xatom_wm_configure_denied
10326 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
10327 dpyinfo->Xatom_wm_window_moved
10328 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
10329 dpyinfo->Xatom_wm_client_leader
10330 = XInternAtom (dpyinfo->display, "WM_CLIENT_LEADER", False);
10331 dpyinfo->Xatom_editres
10332 = XInternAtom (dpyinfo->display, "Editres", False);
10333 dpyinfo->Xatom_CLIPBOARD
10334 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
10335 dpyinfo->Xatom_TIMESTAMP
10336 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
10337 dpyinfo->Xatom_TEXT
10338 = XInternAtom (dpyinfo->display, "TEXT", False);
10339 dpyinfo->Xatom_COMPOUND_TEXT
10340 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
10341 dpyinfo->Xatom_UTF8_STRING
10342 = XInternAtom (dpyinfo->display, "UTF8_STRING", False);
10343 dpyinfo->Xatom_DELETE
10344 = XInternAtom (dpyinfo->display, "DELETE", False);
10345 dpyinfo->Xatom_MULTIPLE
10346 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
10347 dpyinfo->Xatom_INCR
10348 = XInternAtom (dpyinfo->display, "INCR", False);
10349 dpyinfo->Xatom_EMACS_TMP
10350 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
10351 dpyinfo->Xatom_TARGETS
10352 = XInternAtom (dpyinfo->display, "TARGETS", False);
10353 dpyinfo->Xatom_NULL
10354 = XInternAtom (dpyinfo->display, "NULL", False);
10355 dpyinfo->Xatom_ATOM_PAIR
10356 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
10357 /* For properties of font. */
10358 dpyinfo->Xatom_PIXEL_SIZE
10359 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
10360 dpyinfo->Xatom_AVERAGE_WIDTH
10361 = XInternAtom (dpyinfo->display, "AVERAGE_WIDTH", False);
10362 dpyinfo->Xatom_MULE_BASELINE_OFFSET
10363 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
10364 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
10365 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
10366 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
10367 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
10368
10369 /* Ghostscript support. */
10370 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
10371 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
10372
10373 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
10374 False);
10375
10376 dpyinfo->Xatom_XEMBED = XInternAtom (dpyinfo->display, "_XEMBED",
10377 False);
10378
10379 dpyinfo->Xatom_net_wm_state
10380 = XInternAtom (dpyinfo->display, "_NET_WM_STATE", False);
10381 dpyinfo->Xatom_net_wm_state_fullscreen_atom
10382 = XInternAtom (dpyinfo->display, "_NET_WM_STATE_FULLSCREEN", False);
10383 dpyinfo->Xatom_net_wm_state_maximized_horz
10384 = XInternAtom (dpyinfo->display, "_NET_WM_STATE_MAXIMIZED_HORZ", False);
10385 dpyinfo->Xatom_net_wm_state_maximized_vert
10386 = XInternAtom (dpyinfo->display, "_NET_WM_STATE_MAXIMIZED_VERT", False);
10387 dpyinfo->Xatom_net_wm_state_sticky
10388 = XInternAtom (dpyinfo->display, "_NET_WM_STATE_STICKY", False);
10389
10390 dpyinfo->cut_buffers_initialized = 0;
10391
10392 dpyinfo->x_dnd_atoms_size = 8;
10393 dpyinfo->x_dnd_atoms_length = 0;
10394 dpyinfo->x_dnd_atoms = xmalloc (sizeof (*dpyinfo->x_dnd_atoms)
10395 * dpyinfo->x_dnd_atoms_size);
10396
10397 dpyinfo->net_supported_atoms = NULL;
10398 dpyinfo->nr_net_supported_atoms = 0;
10399 dpyinfo->net_supported_window = 0;
10400
10401 connection = ConnectionNumber (dpyinfo->display);
10402 dpyinfo->connection = connection;
10403
10404 {
10405 extern int gray_bitmap_width, gray_bitmap_height;
10406 extern char *gray_bitmap_bits;
10407 dpyinfo->gray
10408 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
10409 gray_bitmap_bits,
10410 gray_bitmap_width, gray_bitmap_height,
10411 (unsigned long) 1, (unsigned long) 0, 1);
10412 }
10413
10414 #ifdef HAVE_X_I18N
10415 xim_initialize (dpyinfo, resource_name);
10416 #endif
10417
10418 #ifdef subprocesses
10419 /* This is only needed for distinguishing keyboard and process input. */
10420 if (connection != 0)
10421 add_keyboard_wait_descriptor (connection);
10422 #endif
10423
10424 #ifdef F_SETOWN
10425 fcntl (connection, F_SETOWN, getpid ());
10426 #endif /* ! defined (F_SETOWN) */
10427
10428 #ifdef SIGIO
10429 if (interrupt_input)
10430 init_sigio (connection);
10431 #endif /* ! defined (SIGIO) */
10432
10433 #ifdef USE_LUCID
10434 {
10435 Display *dpy = dpyinfo->display;
10436 XrmValue d, fr, to;
10437 Font font;
10438
10439 d.addr = (XPointer)&dpy;
10440 d.size = sizeof (Display *);
10441 fr.addr = XtDefaultFont;
10442 fr.size = sizeof (XtDefaultFont);
10443 to.size = sizeof (Font *);
10444 to.addr = (XPointer)&font;
10445 x_catch_errors (dpy);
10446 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
10447 abort ();
10448 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
10449 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
10450 x_uncatch_errors ();
10451 }
10452 #endif
10453
10454 /* See if we should run in synchronous mode. This is useful
10455 for debugging X code. */
10456 {
10457 Lisp_Object value;
10458 value = display_x_get_resource (dpyinfo,
10459 build_string ("synchronous"),
10460 build_string ("Synchronous"),
10461 Qnil, Qnil);
10462 if (STRINGP (value)
10463 && (!strcmp (SDATA (value), "true")
10464 || !strcmp (SDATA (value), "on")))
10465 XSynchronize (dpyinfo->display, True);
10466 }
10467
10468 {
10469 Lisp_Object value;
10470 value = display_x_get_resource (dpyinfo,
10471 build_string ("useXIM"),
10472 build_string ("UseXIM"),
10473 Qnil, Qnil);
10474 #ifdef USE_XIM
10475 if (STRINGP (value)
10476 && (!strcmp (SDATA (value), "false")
10477 || !strcmp (SDATA (value), "off")))
10478 use_xim = 0;
10479 #else
10480 if (STRINGP (value)
10481 && (!strcmp (SDATA (value), "true")
10482 || !strcmp (SDATA (value), "on")))
10483 use_xim = 1;
10484 #endif
10485 }
10486
10487 #ifdef HAVE_X_SM
10488 /* Only do this for the very first display in the Emacs session.
10489 Ignore X session management when Emacs was first started on a
10490 tty. */
10491 if (terminal->id == 1)
10492 x_session_initialize (dpyinfo);
10493 #endif
10494
10495 UNBLOCK_INPUT;
10496
10497 return dpyinfo;
10498 }
10499 \f
10500 /* Get rid of display DPYINFO, deleting all frames on it,
10501 and without sending any more commands to the X server. */
10502
10503 void
10504 x_delete_display (dpyinfo)
10505 struct x_display_info *dpyinfo;
10506 {
10507 int i;
10508 struct terminal *t;
10509
10510 /* Close all frames and delete the generic struct terminal for this
10511 X display. */
10512 for (t = terminal_list; t; t = t->next_terminal)
10513 if (t->type == output_x_window && t->display_info.x == dpyinfo)
10514 {
10515 #ifdef HAVE_X_SM
10516 /* Close X session management when we close its display. */
10517 if (t->id == 1 && x_session_have_connection ())
10518 x_session_close();
10519 #endif
10520 delete_terminal (t);
10521 break;
10522 }
10523
10524 delete_keyboard_wait_descriptor (dpyinfo->connection);
10525
10526 /* Discard this display from x_display_name_list and x_display_list.
10527 We can't use Fdelq because that can quit. */
10528 if (! NILP (x_display_name_list)
10529 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
10530 x_display_name_list = XCDR (x_display_name_list);
10531 else
10532 {
10533 Lisp_Object tail;
10534
10535 tail = x_display_name_list;
10536 while (CONSP (tail) && CONSP (XCDR (tail)))
10537 {
10538 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
10539 {
10540 XSETCDR (tail, XCDR (XCDR (tail)));
10541 break;
10542 }
10543 tail = XCDR (tail);
10544 }
10545 }
10546
10547 if (next_noop_dpyinfo == dpyinfo)
10548 next_noop_dpyinfo = dpyinfo->next;
10549
10550 if (x_display_list == dpyinfo)
10551 x_display_list = dpyinfo->next;
10552 else
10553 {
10554 struct x_display_info *tail;
10555
10556 for (tail = x_display_list; tail; tail = tail->next)
10557 if (tail->next == dpyinfo)
10558 tail->next = tail->next->next;
10559 }
10560
10561 xfree (dpyinfo->x_id_name);
10562 xfree (dpyinfo->x_dnd_atoms);
10563 xfree (dpyinfo->color_cells);
10564 xfree (dpyinfo);
10565 }
10566
10567 #ifdef USE_X_TOOLKIT
10568
10569 /* Atimer callback function for TIMER. Called every 0.1s to process
10570 Xt timeouts, if needed. We must avoid calling XtAppPending as
10571 much as possible because that function does an implicit XFlush
10572 that slows us down. */
10573
10574 static void
10575 x_process_timeouts (timer)
10576 struct atimer *timer;
10577 {
10578 BLOCK_INPUT;
10579 x_timeout_atimer_activated_flag = 0;
10580 if (toolkit_scroll_bar_interaction || popup_activated ())
10581 {
10582 while (XtAppPending (Xt_app_con) & XtIMTimer)
10583 XtAppProcessEvent (Xt_app_con, XtIMTimer);
10584 /* Reactivate the atimer for next time. */
10585 x_activate_timeout_atimer ();
10586 }
10587 UNBLOCK_INPUT;
10588 }
10589
10590 /* Install an asynchronous timer that processes Xt timeout events
10591 every 0.1s as long as either `toolkit_scroll_bar_interaction' or
10592 `popup_activated_flag' (in xmenu.c) is set. Make sure to call this
10593 function whenever these variables are set. This is necessary
10594 because some widget sets use timeouts internally, for example the
10595 LessTif menu bar, or the Xaw3d scroll bar. When Xt timeouts aren't
10596 processed, these widgets don't behave normally. */
10597
10598 void
10599 x_activate_timeout_atimer ()
10600 {
10601 BLOCK_INPUT;
10602 if (!x_timeout_atimer_activated_flag)
10603 {
10604 EMACS_TIME interval;
10605
10606 EMACS_SET_SECS_USECS (interval, 0, 100000);
10607 start_atimer (ATIMER_RELATIVE, interval, x_process_timeouts, 0);
10608 x_timeout_atimer_activated_flag = 1;
10609 }
10610 UNBLOCK_INPUT;
10611 }
10612
10613 #endif /* USE_X_TOOLKIT */
10614
10615 \f
10616 /* Set up use of X before we make the first connection. */
10617
10618 extern frame_parm_handler x_frame_parm_handlers[];
10619
10620 static struct redisplay_interface x_redisplay_interface =
10621 {
10622 x_frame_parm_handlers,
10623 x_produce_glyphs,
10624 x_write_glyphs,
10625 x_insert_glyphs,
10626 x_clear_end_of_line,
10627 x_scroll_run,
10628 x_after_update_window_line,
10629 x_update_window_begin,
10630 x_update_window_end,
10631 x_cursor_to,
10632 x_flush,
10633 #ifdef XFlush
10634 x_flush,
10635 #else
10636 0, /* flush_display_optional */
10637 #endif
10638 x_clear_window_mouse_face,
10639 x_get_glyph_overhangs,
10640 x_fix_overlapping_area,
10641 x_draw_fringe_bitmap,
10642 0, /* define_fringe_bitmap */
10643 0, /* destroy_fringe_bitmap */
10644 x_compute_glyph_string_overhangs,
10645 x_draw_glyph_string,
10646 x_define_frame_cursor,
10647 x_clear_frame_area,
10648 x_draw_window_cursor,
10649 x_draw_vertical_window_border,
10650 x_shift_glyphs_for_insert
10651 };
10652
10653
10654 /* This function is called when the last frame on a display is deleted. */
10655 void
10656 x_delete_terminal (struct terminal *terminal)
10657 {
10658 struct x_display_info *dpyinfo = terminal->display_info.x;
10659 int i;
10660
10661 /* Protect against recursive calls. delete_frame in
10662 delete_terminal calls us back when it deletes our last frame. */
10663 if (!terminal->name)
10664 return;
10665
10666 BLOCK_INPUT;
10667 #ifdef HAVE_X_I18N
10668 /* We must close our connection to the XIM server before closing the
10669 X display. */
10670 if (dpyinfo->xim)
10671 xim_close_dpy (dpyinfo);
10672 #endif
10673
10674 /* If called from x_connection_closed, the display may already be closed
10675 and dpyinfo->display was set to 0 to indicate that. */
10676 if (dpyinfo->display)
10677 {
10678 x_destroy_all_bitmaps (dpyinfo);
10679 XSetCloseDownMode (dpyinfo->display, DestroyAll);
10680
10681 /* Whether or not XCloseDisplay destroys the associated resource
10682 database depends on the version of libX11. To avoid both
10683 crash and memory leak, we dissociate the database from the
10684 display and then destroy dpyinfo->xrdb ourselves.
10685
10686 Unfortunately, the above strategy does not work in some
10687 situations due to a bug in newer versions of libX11: because
10688 XrmSetDatabase doesn't clear the flag XlibDisplayDfltRMDB if
10689 dpy->db is NULL, XCloseDisplay destroys the associated
10690 database whereas it has not been created by XGetDefault
10691 (Bug#21974 in freedesktop.org Bugzilla). As a workaround, we
10692 don't destroy the database here in order to avoid the crash
10693 in the above situations for now, though that may cause memory
10694 leaks in other situations. */
10695 #if 0
10696 #ifdef HAVE_XRMSETDATABASE
10697 XrmSetDatabase (dpyinfo->display, NULL);
10698 #else
10699 dpyinfo->display->db = NULL;
10700 #endif
10701 /* We used to call XrmDestroyDatabase from x_delete_display, but
10702 some older versions of libX11 crash if we call it after
10703 closing all the displays. */
10704 XrmDestroyDatabase (dpyinfo->xrdb);
10705 #endif
10706
10707 #ifdef USE_GTK
10708 xg_display_close (dpyinfo->display);
10709 #else
10710 #ifdef USE_X_TOOLKIT
10711 XtCloseDisplay (dpyinfo->display);
10712 #else
10713 XCloseDisplay (dpyinfo->display);
10714 #endif
10715 #endif /* ! USE_GTK */
10716 }
10717
10718 x_delete_display (dpyinfo);
10719 UNBLOCK_INPUT;
10720 }
10721
10722 /* Create a struct terminal, initialize it with the X11 specific
10723 functions and make DISPLAY->TERMINAL point to it. */
10724
10725 static struct terminal *
10726 x_create_terminal (struct x_display_info *dpyinfo)
10727 {
10728 struct terminal *terminal;
10729
10730 terminal = create_terminal ();
10731
10732 terminal->type = output_x_window;
10733 terminal->display_info.x = dpyinfo;
10734 dpyinfo->terminal = terminal;
10735
10736 /* kboard is initialized in x_term_init. */
10737
10738 terminal->clear_frame_hook = x_clear_frame;
10739 terminal->ins_del_lines_hook = x_ins_del_lines;
10740 terminal->delete_glyphs_hook = x_delete_glyphs;
10741 terminal->ring_bell_hook = XTring_bell;
10742 terminal->toggle_invisible_pointer_hook = XTtoggle_invisible_pointer;
10743 terminal->reset_terminal_modes_hook = XTreset_terminal_modes;
10744 terminal->set_terminal_modes_hook = XTset_terminal_modes;
10745 terminal->update_begin_hook = x_update_begin;
10746 terminal->update_end_hook = x_update_end;
10747 terminal->set_terminal_window_hook = XTset_terminal_window;
10748 terminal->read_socket_hook = XTread_socket;
10749 terminal->frame_up_to_date_hook = XTframe_up_to_date;
10750 terminal->mouse_position_hook = XTmouse_position;
10751 terminal->frame_rehighlight_hook = XTframe_rehighlight;
10752 terminal->frame_raise_lower_hook = XTframe_raise_lower;
10753 terminal->fullscreen_hook = XTfullscreen_hook;
10754 terminal->set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
10755 terminal->condemn_scroll_bars_hook = XTcondemn_scroll_bars;
10756 terminal->redeem_scroll_bar_hook = XTredeem_scroll_bar;
10757 terminal->judge_scroll_bars_hook = XTjudge_scroll_bars;
10758
10759 terminal->delete_frame_hook = x_destroy_window;
10760 terminal->delete_terminal_hook = x_delete_terminal;
10761
10762 terminal->rif = &x_redisplay_interface;
10763 terminal->scroll_region_ok = 1; /* We'll scroll partial frames. */
10764 terminal->char_ins_del_ok = 1;
10765 terminal->line_ins_del_ok = 1; /* We'll just blt 'em. */
10766 terminal->fast_clear_end_of_line = 1; /* X does this well. */
10767 terminal->memory_below_frame = 0; /* We don't remember what scrolls
10768 off the bottom. */
10769
10770 return terminal;
10771 }
10772
10773 void
10774 x_initialize ()
10775 {
10776 baud_rate = 19200;
10777
10778 x_noop_count = 0;
10779 last_tool_bar_item = -1;
10780 any_help_event_p = 0;
10781 ignore_next_mouse_click_timeout = 0;
10782 #ifdef HAVE_X_SM
10783 x_session_initialized = 0;
10784 #endif
10785
10786 #ifdef USE_GTK
10787 current_count = -1;
10788 #endif
10789
10790 /* Try to use interrupt input; if we can't, then start polling. */
10791 Fset_input_interrupt_mode (Qt);
10792
10793 #ifdef USE_X_TOOLKIT
10794 XtToolkitInitialize ();
10795
10796 Xt_app_con = XtCreateApplicationContext ();
10797
10798 /* Register a converter from strings to pixels, which uses
10799 Emacs' color allocation infrastructure. */
10800 XtAppSetTypeConverter (Xt_app_con,
10801 XtRString, XtRPixel, cvt_string_to_pixel,
10802 cvt_string_to_pixel_args,
10803 XtNumber (cvt_string_to_pixel_args),
10804 XtCacheByDisplay, cvt_pixel_dtor);
10805
10806 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
10807 #endif
10808
10809 #ifdef USE_TOOLKIT_SCROLL_BARS
10810 #ifndef USE_GTK
10811 xaw3d_arrow_scroll = False;
10812 xaw3d_pick_top = True;
10813 #endif
10814 #endif
10815
10816 pending_autoraise_frame = 0;
10817 pending_event_wait.f = 0;
10818 pending_event_wait.eventtype = 0;
10819
10820 /* Note that there is no real way portable across R3/R4 to get the
10821 original error handler. */
10822 XSetErrorHandler (x_error_handler);
10823 XSetIOErrorHandler (x_io_error_quitter);
10824
10825 signal (SIGPIPE, x_connection_signal);
10826 }
10827
10828
10829 void
10830 syms_of_xterm ()
10831 {
10832 x_error_message = NULL;
10833
10834 staticpro (&x_display_name_list);
10835 x_display_name_list = Qnil;
10836
10837 staticpro (&last_mouse_scroll_bar);
10838 last_mouse_scroll_bar = Qnil;
10839
10840 staticpro (&Qvendor_specific_keysyms);
10841 Qvendor_specific_keysyms = intern_c_string ("vendor-specific-keysyms");
10842
10843 staticpro (&Qlatin_1);
10844 Qlatin_1 = intern_c_string ("latin-1");
10845
10846 staticpro (&last_mouse_press_frame);
10847 last_mouse_press_frame = Qnil;
10848
10849 #ifdef USE_GTK
10850 xg_default_icon_file = make_pure_c_string ("icons/hicolor/scalable/apps/emacs.svg");
10851 staticpro (&xg_default_icon_file);
10852
10853 Qx_gtk_map_stock = intern_c_string ("x-gtk-map-stock");
10854 staticpro (&Qx_gtk_map_stock);
10855 #endif
10856
10857 DEFVAR_BOOL ("x-use-underline-position-properties",
10858 &x_use_underline_position_properties,
10859 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
10860 A value of nil means ignore them. If you encounter fonts with bogus
10861 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
10862 to 4.1, set this to nil. You can also use `underline-minimum-offset'
10863 to override the font's UNDERLINE_POSITION for small font display
10864 sizes. */);
10865 x_use_underline_position_properties = 1;
10866
10867 DEFVAR_BOOL ("x-underline-at-descent-line",
10868 &x_underline_at_descent_line,
10869 doc: /* *Non-nil means to draw the underline at the same place as the descent line.
10870 A value of nil means to draw the underline according to the value of the
10871 variable `x-use-underline-position-properties', which is usually at the
10872 baseline level. The default value is nil. */);
10873 x_underline_at_descent_line = 0;
10874
10875 DEFVAR_BOOL ("x-mouse-click-focus-ignore-position",
10876 &x_mouse_click_focus_ignore_position,
10877 doc: /* Non-nil means that a mouse click to focus a frame does not move point.
10878 This variable is only used when the window manager requires that you
10879 click on a frame to select it (give it focus). In that case, a value
10880 of nil, means that the selected window and cursor position changes to
10881 reflect the mouse click position, while a non-nil value means that the
10882 selected window or cursor position is preserved. */);
10883 x_mouse_click_focus_ignore_position = 0;
10884
10885 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
10886 doc: /* What X toolkit scroll bars Emacs uses.
10887 A value of nil means Emacs doesn't use X toolkit scroll bars.
10888 Otherwise, value is a symbol describing the X toolkit. */);
10889 #ifdef USE_TOOLKIT_SCROLL_BARS
10890 #ifdef USE_MOTIF
10891 Vx_toolkit_scroll_bars = intern_c_string ("motif");
10892 #elif defined HAVE_XAW3D
10893 Vx_toolkit_scroll_bars = intern_c_string ("xaw3d");
10894 #elif USE_GTK
10895 Vx_toolkit_scroll_bars = intern_c_string ("gtk");
10896 #else
10897 Vx_toolkit_scroll_bars = intern_c_string ("xaw");
10898 #endif
10899 #else
10900 Vx_toolkit_scroll_bars = Qnil;
10901 #endif
10902
10903 staticpro (&last_mouse_motion_frame);
10904 last_mouse_motion_frame = Qnil;
10905
10906 Qmodifier_value = intern_c_string ("modifier-value");
10907 Qalt = intern_c_string ("alt");
10908 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
10909 Qhyper = intern_c_string ("hyper");
10910 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
10911 Qmeta = intern_c_string ("meta");
10912 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
10913 Qsuper = intern_c_string ("super");
10914 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
10915
10916 DEFVAR_LISP ("x-alt-keysym", &Vx_alt_keysym,
10917 doc: /* Which keys Emacs uses for the alt modifier.
10918 This should be one of the symbols `alt', `hyper', `meta', `super'.
10919 For example, `alt' means use the Alt_L and Alt_R keysyms. The default
10920 is nil, which is the same as `alt'. */);
10921 Vx_alt_keysym = Qnil;
10922
10923 DEFVAR_LISP ("x-hyper-keysym", &Vx_hyper_keysym,
10924 doc: /* Which keys Emacs uses for the hyper modifier.
10925 This should be one of the symbols `alt', `hyper', `meta', `super'.
10926 For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
10927 default is nil, which is the same as `hyper'. */);
10928 Vx_hyper_keysym = Qnil;
10929
10930 DEFVAR_LISP ("x-meta-keysym", &Vx_meta_keysym,
10931 doc: /* Which keys Emacs uses for the meta modifier.
10932 This should be one of the symbols `alt', `hyper', `meta', `super'.
10933 For example, `meta' means use the Meta_L and Meta_R keysyms. The
10934 default is nil, which is the same as `meta'. */);
10935 Vx_meta_keysym = Qnil;
10936
10937 DEFVAR_LISP ("x-super-keysym", &Vx_super_keysym,
10938 doc: /* Which keys Emacs uses for the super modifier.
10939 This should be one of the symbols `alt', `hyper', `meta', `super'.
10940 For example, `super' means use the Super_L and Super_R keysyms. The
10941 default is nil, which is the same as `super'. */);
10942 Vx_super_keysym = Qnil;
10943
10944 DEFVAR_LISP ("x-keysym-table", &Vx_keysym_table,
10945 doc: /* Hash table of character codes indexed by X keysym codes. */);
10946 Vx_keysym_table = make_hash_table (Qeql, make_number (900),
10947 make_float (DEFAULT_REHASH_SIZE),
10948 make_float (DEFAULT_REHASH_THRESHOLD),
10949 Qnil, Qnil, Qnil);
10950 }
10951
10952 #endif /* HAVE_X_WINDOWS */
10953
10954 /* arch-tag: 6d4e4cb7-abc1-4302-9585-d84dcfb09d0f
10955 (do not change this comment) */