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