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