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