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