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