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