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