]> code.delx.au - gnu-emacs/blob - src/xterm.c
* src/puresize.h (BASE_PURESIZE): Increase it some more.
[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 (at
10 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 #include "xwidget.h"
66 #include "fontset.h"
67 #include "termhooks.h"
68 #include "termopts.h"
69 #include "termchar.h"
70 #include "emacs-icon.h"
71 #include "buffer.h"
72 #include "window.h"
73 #include "keyboard.h"
74 #include "atimer.h"
75 #include "font.h"
76 #include "xsettings.h"
77 #include "sysselect.h"
78 #include "menu.h"
79
80 #ifdef USE_X_TOOLKIT
81 #include <X11/Shell.h>
82 #endif
83
84 #include <unistd.h>
85
86 #ifdef USE_GTK
87 #include "gtkutil.h"
88 #ifdef HAVE_GTK3
89 #include <X11/Xproto.h>
90 #endif
91 #endif
92
93 #if defined (USE_LUCID) || defined (USE_MOTIF)
94 #include "../lwlib/xlwmenu.h"
95 #endif
96
97 #ifdef USE_X_TOOLKIT
98 #if !defined (NO_EDITRES)
99 #define HACK_EDITRES
100 extern void _XEditResCheckMessages (Widget, XtPointer, XEvent *, Boolean *);
101 #endif /* not NO_EDITRES */
102
103 /* Include toolkit specific headers for the scroll bar widget. */
104
105 #ifdef USE_TOOLKIT_SCROLL_BARS
106 #if defined USE_MOTIF
107 #include <Xm/Xm.h> /* For LESSTIF_VERSION */
108 #include <Xm/ScrollBar.h>
109 #else /* !USE_MOTIF i.e. use Xaw */
110
111 #ifdef HAVE_XAW3D
112 #include <X11/Xaw3d/Simple.h>
113 #include <X11/Xaw3d/Scrollbar.h>
114 #include <X11/Xaw3d/ThreeD.h>
115 #else /* !HAVE_XAW3D */
116 #include <X11/Xaw/Simple.h>
117 #include <X11/Xaw/Scrollbar.h>
118 #endif /* !HAVE_XAW3D */
119 #ifndef XtNpickTop
120 #define XtNpickTop "pickTop"
121 #endif /* !XtNpickTop */
122 #endif /* !USE_MOTIF */
123 #endif /* USE_TOOLKIT_SCROLL_BARS */
124
125 #endif /* USE_X_TOOLKIT */
126
127 #ifdef USE_X_TOOLKIT
128 #include "widget.h"
129 #ifndef XtNinitialState
130 #define XtNinitialState "initialState"
131 #endif
132 #endif
133
134 #include "bitmaps/gray.xbm"
135
136 #ifdef HAVE_XKB
137 #include <X11/XKBlib.h>
138 #endif
139
140 /* Default to using XIM if available. */
141 #ifdef USE_XIM
142 bool use_xim = true;
143 #else
144 bool use_xim = false; /* configure --without-xim */
145 #endif
146
147 /* Non-zero means that a HELP_EVENT has been generated since Emacs
148 start. */
149
150 static bool any_help_event_p;
151
152 /* This is a chain of structures for all the X displays currently in
153 use. */
154
155 struct x_display_info *x_display_list;
156
157 #ifdef USE_X_TOOLKIT
158
159 /* The application context for Xt use. */
160 XtAppContext Xt_app_con;
161 static String Xt_default_resources[] = {0};
162
163 /* Non-zero means user is interacting with a toolkit scroll bar. */
164 static bool toolkit_scroll_bar_interaction;
165
166 #endif /* USE_X_TOOLKIT */
167
168 /* Non-zero timeout value means ignore next mouse click if it arrives
169 before that timeout elapses (i.e. as part of the same sequence of
170 events resulting from clicking on a frame to select it). */
171
172 static Time ignore_next_mouse_click_timeout;
173
174 /* Used locally within XTread_socket. */
175
176 static int x_noop_count;
177
178 #ifdef USE_GTK
179 /* The name of the Emacs icon file. */
180 static Lisp_Object xg_default_icon_file;
181 #endif
182
183 /* Some functions take this as char *, not const char *. */
184 static char emacs_class[] = EMACS_CLASS;
185
186 enum xembed_info
187 {
188 XEMBED_MAPPED = 1 << 0
189 };
190
191 enum xembed_message
192 {
193 XEMBED_EMBEDDED_NOTIFY = 0,
194 XEMBED_WINDOW_ACTIVATE = 1,
195 XEMBED_WINDOW_DEACTIVATE = 2,
196 XEMBED_REQUEST_FOCUS = 3,
197 XEMBED_FOCUS_IN = 4,
198 XEMBED_FOCUS_OUT = 5,
199 XEMBED_FOCUS_NEXT = 6,
200 XEMBED_FOCUS_PREV = 7,
201
202 XEMBED_MODALITY_ON = 10,
203 XEMBED_MODALITY_OFF = 11,
204 XEMBED_REGISTER_ACCELERATOR = 12,
205 XEMBED_UNREGISTER_ACCELERATOR = 13,
206 XEMBED_ACTIVATE_ACCELERATOR = 14
207 };
208
209 static void x_free_cr_resources (struct frame *);
210 static bool x_alloc_nearest_color_1 (Display *, Colormap, XColor *);
211 static void x_raise_frame (struct frame *);
212 static void x_lower_frame (struct frame *);
213 static int x_io_error_quitter (Display *);
214 static struct terminal *x_create_terminal (struct x_display_info *);
215 static void x_frame_rehighlight (struct x_display_info *);
216
217 static void x_clip_to_row (struct window *, struct glyph_row *,
218 enum glyph_row_area, GC);
219 static struct scroll_bar *x_window_to_scroll_bar (Display *, Window, int);
220 static void x_scroll_bar_report_motion (struct frame **, Lisp_Object *,
221 enum scroll_bar_part *,
222 Lisp_Object *, Lisp_Object *,
223 Time *);
224 static void x_horizontal_scroll_bar_report_motion (struct frame **, Lisp_Object *,
225 enum scroll_bar_part *,
226 Lisp_Object *, Lisp_Object *,
227 Time *);
228 static bool x_handle_net_wm_state (struct frame *, const XPropertyEvent *);
229 static void x_check_fullscreen (struct frame *);
230 static void x_check_expected_move (struct frame *, int, int);
231 static void x_sync_with_move (struct frame *, int, int, bool);
232 static int handle_one_xevent (struct x_display_info *,
233 const XEvent *, int *,
234 struct input_event *);
235 #if ! (defined USE_X_TOOLKIT || defined USE_MOTIF)
236 static int x_dispatch_event (XEvent *, Display *);
237 #endif
238 static void x_wm_set_window_state (struct frame *, int);
239 static void x_wm_set_icon_pixmap (struct frame *, ptrdiff_t);
240 static void x_initialize (void);
241
242 static bool get_current_wm_state (struct frame *, Window, int *, bool *);
243
244 /* Flush display of frame F. */
245
246 static void
247 x_flush (struct frame *f)
248 {
249 eassert (f && FRAME_X_P (f));
250 /* Don't call XFlush when it is not safe to redisplay; the X
251 connection may be broken. */
252 if (!NILP (Vinhibit_redisplay))
253 return;
254
255 block_input ();
256 XFlush (FRAME_X_DISPLAY (f));
257 unblock_input ();
258 }
259
260
261 /* Remove calls to XFlush by defining XFlush to an empty replacement.
262 Calls to XFlush should be unnecessary because the X output buffer
263 is flushed automatically as needed by calls to XPending,
264 XNextEvent, or XWindowEvent according to the XFlush man page.
265 XTread_socket calls XPending. Removing XFlush improves
266 performance. */
267
268 #define XFlush(DISPLAY) (void) 0
269
270 \f
271 /***********************************************************************
272 Debugging
273 ***********************************************************************/
274
275 #if false
276
277 /* This is a function useful for recording debugging information about
278 the sequence of occurrences in this file. */
279
280 struct record
281 {
282 char *locus;
283 int type;
284 };
285
286 struct record event_record[100];
287
288 int event_record_index;
289
290 void
291 record_event (char *locus, int type)
292 {
293 if (event_record_index == ARRAYELTS (event_record))
294 event_record_index = 0;
295
296 event_record[event_record_index].locus = locus;
297 event_record[event_record_index].type = type;
298 event_record_index++;
299 }
300
301 #endif
302
303 #ifdef USE_CAIRO
304
305 #define FRAME_CR_CONTEXT(f) ((f)->output_data.x->cr_context)
306 #define FRAME_CR_SURFACE(f) ((f)->output_data.x->cr_surface)
307
308 static struct x_gc_ext_data *
309 x_gc_get_ext_data (struct frame *f, GC gc, int create_if_not_found_p)
310 {
311 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
312 XEDataObject object;
313 XExtData **head, *ext_data;
314
315 object.gc = gc;
316 head = XEHeadOfExtensionList (object);
317 ext_data = XFindOnExtensionList (head, dpyinfo->ext_codes->extension);
318 if (ext_data == NULL)
319 {
320 if (!create_if_not_found_p)
321 return NULL;
322 else
323 {
324 ext_data = xzalloc (sizeof (*ext_data));
325 ext_data->number = dpyinfo->ext_codes->extension;
326 ext_data->private_data = xzalloc (sizeof (struct x_gc_ext_data));
327 XAddToExtensionList (head, ext_data);
328 }
329 }
330 return (struct x_gc_ext_data *) ext_data->private_data;
331 }
332
333 static void
334 x_extension_initialize (struct x_display_info *dpyinfo)
335 {
336 XExtCodes *ext_codes = XAddExtension (dpyinfo->display);
337
338 dpyinfo->ext_codes = ext_codes;
339 }
340
341 static void
342 x_cr_destroy_surface (struct frame *f)
343 {
344 if (FRAME_CR_SURFACE (f))
345 {
346 cairo_t *cr = FRAME_CR_CONTEXT (f);
347 cairo_surface_destroy (FRAME_CR_SURFACE (f));
348 FRAME_CR_SURFACE (f) = 0;
349 if (cr) cairo_destroy (cr);
350 FRAME_CR_CONTEXT (f) = NULL;
351 }
352 }
353
354 cairo_t *
355 x_begin_cr_clip (struct frame *f, GC gc)
356 {
357 cairo_t *cr = FRAME_CR_CONTEXT (f);
358
359 if (!cr)
360 {
361
362 if (! FRAME_CR_SURFACE (f))
363 {
364 cairo_surface_t *surface;
365 surface = cairo_xlib_surface_create (FRAME_X_DISPLAY (f),
366 FRAME_X_WINDOW (f),
367 FRAME_DISPLAY_INFO (f)->visual,
368 FRAME_PIXEL_WIDTH (f),
369 FRAME_PIXEL_HEIGHT (f));
370 cr = cairo_create (surface);
371 cairo_surface_destroy (surface);
372 }
373 else
374 cr = cairo_create (FRAME_CR_SURFACE (f));
375 FRAME_CR_CONTEXT (f) = cr;
376 }
377 cairo_save (cr);
378
379 if (gc)
380 {
381 struct x_gc_ext_data *gc_ext = x_gc_get_ext_data (f, gc, 0);
382
383 if (gc_ext && gc_ext->n_clip_rects)
384 {
385 int i;
386
387 for (i = 0; i < gc_ext->n_clip_rects; i++)
388 cairo_rectangle (cr, gc_ext->clip_rects[i].x,
389 gc_ext->clip_rects[i].y,
390 gc_ext->clip_rects[i].width,
391 gc_ext->clip_rects[i].height);
392 cairo_clip (cr);
393 }
394 }
395
396 return cr;
397 }
398
399 void
400 x_end_cr_clip (struct frame *f)
401 {
402 cairo_restore (FRAME_CR_CONTEXT (f));
403 }
404
405 void
406 x_set_cr_source_with_gc_foreground (struct frame *f, GC gc)
407 {
408 XGCValues xgcv;
409 XColor color;
410
411 XGetGCValues (FRAME_X_DISPLAY (f), gc, GCForeground, &xgcv);
412 color.pixel = xgcv.foreground;
413 x_query_color (f, &color);
414 cairo_set_source_rgb (FRAME_CR_CONTEXT (f), color.red / 65535.0,
415 color.green / 65535.0, color.blue / 65535.0);
416 }
417
418 void
419 x_set_cr_source_with_gc_background (struct frame *f, GC gc)
420 {
421 XGCValues xgcv;
422 XColor color;
423
424 XGetGCValues (FRAME_X_DISPLAY (f), gc, GCBackground, &xgcv);
425 color.pixel = xgcv.background;
426 x_query_color (f, &color);
427 cairo_set_source_rgb (FRAME_CR_CONTEXT (f), color.red / 65535.0,
428 color.green / 65535.0, color.blue / 65535.0);
429 }
430
431 /* Fringe bitmaps. */
432
433 static int max_fringe_bmp = 0;
434 static cairo_pattern_t **fringe_bmp = 0;
435
436 static void
437 x_cr_define_fringe_bitmap (int which, unsigned short *bits, int h, int wd)
438 {
439 int i, stride;
440 cairo_surface_t *surface;
441 unsigned char *data;
442 cairo_pattern_t *pattern;
443
444 if (which >= max_fringe_bmp)
445 {
446 i = max_fringe_bmp;
447 max_fringe_bmp = which + 20;
448 fringe_bmp = (cairo_pattern_t **) xrealloc (fringe_bmp, max_fringe_bmp * sizeof (cairo_pattern_t *));
449 while (i < max_fringe_bmp)
450 fringe_bmp[i++] = 0;
451 }
452
453 block_input ();
454
455 surface = cairo_image_surface_create (CAIRO_FORMAT_A1, wd, h);
456 stride = cairo_image_surface_get_stride (surface);
457 data = cairo_image_surface_get_data (surface);
458
459 for (i = 0; i < h; i++)
460 {
461 *((unsigned short *) data) = bits[i];
462 data += stride;
463 }
464
465 cairo_surface_mark_dirty (surface);
466 pattern = cairo_pattern_create_for_surface (surface);
467 cairo_surface_destroy (surface);
468
469 unblock_input ();
470
471 fringe_bmp[which] = pattern;
472 }
473
474 static void
475 x_cr_destroy_fringe_bitmap (int which)
476 {
477 if (which >= max_fringe_bmp)
478 return;
479
480 if (fringe_bmp[which])
481 {
482 block_input ();
483 cairo_pattern_destroy (fringe_bmp[which]);
484 unblock_input ();
485 }
486 fringe_bmp[which] = 0;
487 }
488
489 static void
490 x_cr_draw_image (struct frame *f, GC gc, cairo_pattern_t *image,
491 int src_x, int src_y, int width, int height,
492 int dest_x, int dest_y, bool overlay_p)
493 {
494 cairo_t *cr;
495 cairo_matrix_t matrix;
496 cairo_surface_t *surface;
497 cairo_format_t format;
498
499 cr = x_begin_cr_clip (f, gc);
500 if (overlay_p)
501 cairo_rectangle (cr, dest_x, dest_y, width, height);
502 else
503 {
504 x_set_cr_source_with_gc_background (f, gc);
505 cairo_rectangle (cr, dest_x, dest_y, width, height);
506 cairo_fill_preserve (cr);
507 }
508 cairo_clip (cr);
509 cairo_matrix_init_translate (&matrix, src_x - dest_x, src_y - dest_y);
510 cairo_pattern_set_matrix (image, &matrix);
511 cairo_pattern_get_surface (image, &surface);
512 format = cairo_image_surface_get_format (surface);
513 if (format != CAIRO_FORMAT_A8 && format != CAIRO_FORMAT_A1)
514 {
515 cairo_set_source (cr, image);
516 cairo_fill (cr);
517 }
518 else
519 {
520 x_set_cr_source_with_gc_foreground (f, gc);
521 cairo_mask (cr, image);
522 }
523 x_end_cr_clip (f);
524 }
525
526 void
527 x_cr_draw_frame (cairo_t *cr, struct frame *f)
528 {
529 int width, height;
530
531 width = FRAME_PIXEL_WIDTH (f);
532 height = FRAME_PIXEL_HEIGHT (f);
533
534 x_free_cr_resources (f);
535 FRAME_CR_CONTEXT (f) = cr;
536 x_clear_area (f, 0, 0, width, height);
537 expose_frame (f, 0, 0, width, height);
538 FRAME_CR_CONTEXT (f) = NULL;
539 }
540
541 static cairo_status_t
542 x_cr_accumulate_data (void *closure, const unsigned char *data,
543 unsigned int length)
544 {
545 Lisp_Object *acc = (Lisp_Object *) closure;
546
547 *acc = Fcons (make_unibyte_string ((char const *) data, length), *acc);
548
549 return CAIRO_STATUS_SUCCESS;
550 }
551
552 static void
553 x_cr_destroy (Lisp_Object arg)
554 {
555 cairo_t *cr = (cairo_t *) XSAVE_POINTER (arg, 0);
556
557 block_input ();
558 cairo_destroy (cr);
559 unblock_input ();
560 }
561
562 Lisp_Object
563 x_cr_export_frames (Lisp_Object frames, cairo_surface_type_t surface_type)
564 {
565 struct frame *f;
566 cairo_surface_t *surface;
567 cairo_t *cr;
568 int width, height;
569 void (*surface_set_size_func) (cairo_surface_t *, double, double) = NULL;
570 Lisp_Object acc = Qnil;
571 int count = SPECPDL_INDEX ();
572
573 specbind (Qredisplay_dont_pause, Qt);
574 redisplay_preserve_echo_area (31);
575
576 f = XFRAME (XCAR (frames));
577 frames = XCDR (frames);
578 width = FRAME_PIXEL_WIDTH (f);
579 height = FRAME_PIXEL_HEIGHT (f);
580
581 block_input ();
582 #ifdef CAIRO_HAS_PDF_SURFACE
583 if (surface_type == CAIRO_SURFACE_TYPE_PDF)
584 {
585 surface = cairo_pdf_surface_create_for_stream (x_cr_accumulate_data, &acc,
586 width, height);
587 surface_set_size_func = cairo_pdf_surface_set_size;
588 }
589 else
590 #endif
591 #ifdef CAIRO_HAS_PNG_FUNCTIONS
592 if (surface_type == CAIRO_SURFACE_TYPE_IMAGE)
593 surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, width, height);
594 else
595 #endif
596 #ifdef CAIRO_HAS_PS_SURFACE
597 if (surface_type == CAIRO_SURFACE_TYPE_PS)
598 {
599 surface = cairo_ps_surface_create_for_stream (x_cr_accumulate_data, &acc,
600 width, height);
601 surface_set_size_func = cairo_ps_surface_set_size;
602 }
603 else
604 #endif
605 #ifdef CAIRO_HAS_SVG_SURFACE
606 if (surface_type == CAIRO_SURFACE_TYPE_SVG)
607 surface = cairo_svg_surface_create_for_stream (x_cr_accumulate_data, &acc,
608 width, height);
609 else
610 #endif
611 abort ();
612
613 cr = cairo_create (surface);
614 cairo_surface_destroy (surface);
615 record_unwind_protect (x_cr_destroy, make_save_ptr (cr));
616
617 while (1)
618 {
619 x_free_cr_resources (f);
620 FRAME_CR_CONTEXT (f) = cr;
621 x_clear_area (f, 0, 0, width, height);
622 expose_frame (f, 0, 0, width, height);
623 FRAME_CR_CONTEXT (f) = NULL;
624
625 if (NILP (frames))
626 break;
627
628 cairo_surface_show_page (surface);
629 f = XFRAME (XCAR (frames));
630 frames = XCDR (frames);
631 width = FRAME_PIXEL_WIDTH (f);
632 height = FRAME_PIXEL_HEIGHT (f);
633 if (surface_set_size_func)
634 (*surface_set_size_func) (surface, width, height);
635
636 unblock_input ();
637 QUIT;
638 block_input ();
639 }
640
641 #ifdef CAIRO_HAS_PNG_FUNCTIONS
642 if (surface_type == CAIRO_SURFACE_TYPE_IMAGE)
643 {
644 cairo_surface_flush (surface);
645 cairo_surface_write_to_png_stream (surface, x_cr_accumulate_data, &acc);
646 }
647 #endif
648 unblock_input ();
649
650 unbind_to (count, Qnil);
651
652 return CALLN (Fapply, intern ("concat"), Fnreverse (acc));
653 }
654
655 #endif /* USE_CAIRO */
656
657 static void
658 x_free_cr_resources (struct frame *f)
659 {
660 #ifdef USE_CAIRO
661 if (f == NULL)
662 {
663 Lisp_Object rest, frame;
664 FOR_EACH_FRAME (rest, frame)
665 if (FRAME_X_P (XFRAME (frame)))
666 x_free_cr_resources (XFRAME (frame));
667 }
668 else
669 {
670 cairo_t *cr = FRAME_CR_CONTEXT (f);
671
672 if (cr)
673 {
674 cairo_surface_t *surface = cairo_get_target (cr);
675
676 if (cairo_surface_get_type (surface) == CAIRO_SURFACE_TYPE_XLIB)
677 {
678 cairo_destroy (cr);
679 FRAME_CR_CONTEXT (f) = NULL;
680 }
681 }
682 }
683 #endif
684 }
685
686 static void
687 x_set_clip_rectangles (struct frame *f, GC gc, XRectangle *rectangles, int n)
688 {
689 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, rectangles, n, Unsorted);
690 #ifdef USE_CAIRO
691 eassert (n >= 0 && n <= MAX_CLIP_RECTS);
692
693 {
694 struct x_gc_ext_data *gc_ext = x_gc_get_ext_data (f, gc, 1);
695
696 gc_ext->n_clip_rects = n;
697 memcpy (gc_ext->clip_rects, rectangles, sizeof (XRectangle) * n);
698 }
699 #endif
700 }
701
702 static void
703 x_reset_clip_rectangles (struct frame *f, GC gc)
704 {
705 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
706 #ifdef USE_CAIRO
707 {
708 struct x_gc_ext_data *gc_ext = x_gc_get_ext_data (f, gc, 0);
709
710 if (gc_ext)
711 gc_ext->n_clip_rects = 0;
712 }
713 #endif
714 }
715
716 static void
717 x_fill_rectangle (struct frame *f, GC gc, int x, int y, int width, int height)
718 {
719 #ifdef USE_CAIRO
720 cairo_t *cr;
721
722 cr = x_begin_cr_clip (f, gc);
723 x_set_cr_source_with_gc_foreground (f, gc);
724 cairo_rectangle (cr, x, y, width, height);
725 cairo_fill (cr);
726 x_end_cr_clip (f);
727 #else
728 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
729 gc, x, y, width, height);
730 #endif
731 }
732
733 static void
734 x_draw_rectangle (struct frame *f, GC gc, int x, int y, int width, int height)
735 {
736 #ifdef USE_CAIRO
737 cairo_t *cr;
738
739 cr = x_begin_cr_clip (f, gc);
740 x_set_cr_source_with_gc_foreground (f, gc);
741 cairo_rectangle (cr, x + 0.5, y + 0.5, width, height);
742 cairo_set_line_width (cr, 1);
743 cairo_stroke (cr);
744 x_end_cr_clip (f);
745 #else
746 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
747 gc, x, y, width, height);
748 #endif
749 }
750
751 static void
752 x_clear_window (struct frame *f)
753 {
754 #ifdef USE_CAIRO
755 cairo_t *cr;
756
757 cr = x_begin_cr_clip (f, NULL);
758 x_set_cr_source_with_gc_background (f, f->output_data.x->normal_gc);
759 cairo_paint (cr);
760 x_end_cr_clip (f);
761 #else
762 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
763 #endif
764 }
765
766 #ifdef USE_CAIRO
767 static void
768 x_fill_trapezoid_for_relief (struct frame *f, GC gc, int x, int y,
769 int width, int height, int top_p)
770 {
771 cairo_t *cr;
772
773 cr = x_begin_cr_clip (f, gc);
774 x_set_cr_source_with_gc_foreground (f, gc);
775 cairo_move_to (cr, top_p ? x : x + height, y);
776 cairo_line_to (cr, x, y + height);
777 cairo_line_to (cr, top_p ? x + width - height : x + width, y + height);
778 cairo_line_to (cr, x + width, y);
779 cairo_fill (cr);
780 x_end_cr_clip (f);
781 }
782
783 enum corners
784 {
785 CORNER_BOTTOM_RIGHT, /* 0 -> pi/2 */
786 CORNER_BOTTOM_LEFT, /* pi/2 -> pi */
787 CORNER_TOP_LEFT, /* pi -> 3pi/2 */
788 CORNER_TOP_RIGHT, /* 3pi/2 -> 2pi */
789 CORNER_LAST
790 };
791
792 static void
793 x_erase_corners_for_relief (struct frame *f, GC gc, int x, int y,
794 int width, int height,
795 double radius, double margin, int corners)
796 {
797 cairo_t *cr;
798 int i;
799
800 cr = x_begin_cr_clip (f, gc);
801 x_set_cr_source_with_gc_background (f, gc);
802 for (i = 0; i < CORNER_LAST; i++)
803 if (corners & (1 << i))
804 {
805 double xm, ym, xc, yc;
806
807 if (i == CORNER_TOP_LEFT || i == CORNER_BOTTOM_LEFT)
808 xm = x - margin, xc = xm + radius;
809 else
810 xm = x + width + margin, xc = xm - radius;
811 if (i == CORNER_TOP_LEFT || i == CORNER_TOP_RIGHT)
812 ym = y - margin, yc = ym + radius;
813 else
814 ym = y + height + margin, yc = ym - radius;
815
816 cairo_move_to (cr, xm, ym);
817 cairo_arc (cr, xc, yc, radius, i * M_PI_2, (i + 1) * M_PI_2);
818 }
819 cairo_clip (cr);
820 cairo_rectangle (cr, x, y, width, height);
821 cairo_fill (cr);
822 x_end_cr_clip (f);
823 }
824
825 static void
826 x_draw_horizontal_wave (struct frame *f, GC gc, int x, int y,
827 int width, int height, int wave_length)
828 {
829 cairo_t *cr;
830 double dx = wave_length, dy = height - 1;
831 int xoffset, n;
832
833 cr = x_begin_cr_clip (f, gc);
834 x_set_cr_source_with_gc_foreground (f, gc);
835 cairo_rectangle (cr, x, y, width, height);
836 cairo_clip (cr);
837
838 if (x >= 0)
839 {
840 xoffset = x % (wave_length * 2);
841 if (xoffset == 0)
842 xoffset = wave_length * 2;
843 }
844 else
845 xoffset = x % (wave_length * 2) + wave_length * 2;
846 n = (width + xoffset) / wave_length + 1;
847 if (xoffset > wave_length)
848 {
849 xoffset -= wave_length;
850 --n;
851 y += height - 1;
852 dy = -dy;
853 }
854
855 cairo_move_to (cr, x - xoffset + 0.5, y + 0.5);
856 while (--n >= 0)
857 {
858 cairo_rel_line_to (cr, dx, dy);
859 dy = -dy;
860 }
861 cairo_set_line_width (cr, 1);
862 cairo_stroke (cr);
863 x_end_cr_clip (f);
864 }
865 #endif
866
867 \f
868 /* Return the struct x_display_info corresponding to DPY. */
869
870 struct x_display_info *
871 x_display_info_for_display (Display *dpy)
872 {
873 struct x_display_info *dpyinfo;
874
875 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
876 if (dpyinfo->display == dpy)
877 return dpyinfo;
878
879 return 0;
880 }
881
882 static Window
883 x_find_topmost_parent (struct frame *f)
884 {
885 struct x_output *x = f->output_data.x;
886 Window win = None, wi = x->parent_desc;
887 Display *dpy = FRAME_X_DISPLAY (f);
888
889 while (wi != FRAME_DISPLAY_INFO (f)->root_window)
890 {
891 Window root;
892 Window *children;
893 unsigned int nchildren;
894
895 win = wi;
896 if (XQueryTree (dpy, win, &root, &wi, &children, &nchildren))
897 XFree (children);
898 else
899 break;
900 }
901
902 return win;
903 }
904
905 #define OPAQUE 0xffffffff
906
907 void
908 x_set_frame_alpha (struct frame *f)
909 {
910 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
911 Display *dpy = FRAME_X_DISPLAY (f);
912 Window win = FRAME_OUTER_WINDOW (f);
913 double alpha = 1.0;
914 double alpha_min = 1.0;
915 unsigned long opac;
916 Window parent;
917
918 if (dpyinfo->x_highlight_frame == f)
919 alpha = f->alpha[0];
920 else
921 alpha = f->alpha[1];
922
923 if (FLOATP (Vframe_alpha_lower_limit))
924 alpha_min = XFLOAT_DATA (Vframe_alpha_lower_limit);
925 else if (INTEGERP (Vframe_alpha_lower_limit))
926 alpha_min = (XINT (Vframe_alpha_lower_limit)) / 100.0;
927
928 if (alpha < 0.0)
929 return;
930 else if (alpha > 1.0)
931 alpha = 1.0;
932 else if (0.0 <= alpha && alpha < alpha_min && alpha_min <= 1.0)
933 alpha = alpha_min;
934
935 opac = alpha * OPAQUE;
936
937 x_catch_errors (dpy);
938
939 /* If there is a parent from the window manager, put the property there
940 also, to work around broken window managers that fail to do that.
941 Do this unconditionally as this function is called on reparent when
942 alpha has not changed on the frame. */
943
944 parent = x_find_topmost_parent (f);
945 if (parent != None)
946 XChangeProperty (dpy, parent, dpyinfo->Xatom_net_wm_window_opacity,
947 XA_CARDINAL, 32, PropModeReplace,
948 (unsigned char *) &opac, 1);
949
950 /* return unless necessary */
951 {
952 unsigned char *data;
953 Atom actual;
954 int rc, format;
955 unsigned long n, left;
956
957 rc = XGetWindowProperty (dpy, win, dpyinfo->Xatom_net_wm_window_opacity,
958 0, 1, False, XA_CARDINAL,
959 &actual, &format, &n, &left,
960 &data);
961
962 if (rc == Success && actual != None)
963 {
964 unsigned long value = *(unsigned long *)data;
965 XFree (data);
966 if (value == opac)
967 {
968 x_uncatch_errors ();
969 return;
970 }
971 }
972 }
973
974 XChangeProperty (dpy, win, dpyinfo->Xatom_net_wm_window_opacity,
975 XA_CARDINAL, 32, PropModeReplace,
976 (unsigned char *) &opac, 1);
977 x_uncatch_errors ();
978 }
979
980 /***********************************************************************
981 Starting and ending an update
982 ***********************************************************************/
983
984 /* Start an update of frame F. This function is installed as a hook
985 for update_begin, i.e. it is called when update_begin is called.
986 This function is called prior to calls to x_update_window_begin for
987 each window being updated. Currently, there is nothing to do here
988 because all interesting stuff is done on a window basis. */
989
990 static void
991 x_update_begin (struct frame *f)
992 {
993 #ifdef USE_CAIRO
994 if (! NILP (tip_frame) && XFRAME (tip_frame) == f
995 && ! FRAME_VISIBLE_P (f))
996 return;
997
998 if (! FRAME_CR_SURFACE (f))
999 {
1000 int width, height;
1001 #ifdef USE_GTK
1002 if (FRAME_GTK_WIDGET (f))
1003 {
1004 GdkWindow *w = gtk_widget_get_window (FRAME_GTK_WIDGET (f));
1005 width = gdk_window_get_width (w);
1006 height = gdk_window_get_height (w);
1007 }
1008 else
1009 #endif
1010 {
1011 width = FRAME_PIXEL_WIDTH (f);
1012 height = FRAME_PIXEL_HEIGHT (f);
1013 if (! FRAME_EXTERNAL_TOOL_BAR (f))
1014 height += FRAME_TOOL_BAR_HEIGHT (f);
1015 if (! FRAME_EXTERNAL_MENU_BAR (f))
1016 height += FRAME_MENU_BAR_HEIGHT (f);
1017 }
1018
1019 if (width > 0 && height > 0)
1020 {
1021 block_input();
1022 FRAME_CR_SURFACE (f) = cairo_image_surface_create
1023 (CAIRO_FORMAT_ARGB32, width, height);
1024 unblock_input();
1025 }
1026 }
1027 #endif /* USE_CAIRO */
1028 }
1029
1030 /* Start update of window W. */
1031
1032 static void
1033 x_update_window_begin (struct window *w)
1034 {
1035 struct frame *f = XFRAME (WINDOW_FRAME (w));
1036 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
1037
1038 w->output_cursor = w->cursor;
1039
1040 block_input ();
1041
1042 if (f == hlinfo->mouse_face_mouse_frame)
1043 {
1044 /* Don't do highlighting for mouse motion during the update. */
1045 hlinfo->mouse_face_defer = true;
1046
1047 /* If F needs to be redrawn, simply forget about any prior mouse
1048 highlighting. */
1049 if (FRAME_GARBAGED_P (f))
1050 hlinfo->mouse_face_window = Qnil;
1051 }
1052
1053 unblock_input ();
1054 }
1055
1056
1057 /* Draw a vertical window border from (x,y0) to (x,y1) */
1058
1059 static void
1060 x_draw_vertical_window_border (struct window *w, int x, int y0, int y1)
1061 {
1062 struct frame *f = XFRAME (WINDOW_FRAME (w));
1063 struct face *face;
1064
1065 face = FACE_OPT_FROM_ID (f, VERTICAL_BORDER_FACE_ID);
1066 if (face)
1067 XSetForeground (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
1068 face->foreground);
1069
1070 #ifdef USE_CAIRO
1071 x_fill_rectangle (f, f->output_data.x->normal_gc, x, y0, 1, y1 - y0);
1072 #else
1073 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1074 f->output_data.x->normal_gc, x, y0, x, y1);
1075 #endif
1076 }
1077
1078 /* Draw a window divider from (x0,y0) to (x1,y1) */
1079
1080 static void
1081 x_draw_window_divider (struct window *w, int x0, int x1, int y0, int y1)
1082 {
1083 struct frame *f = XFRAME (WINDOW_FRAME (w));
1084 struct face *face = FACE_OPT_FROM_ID (f, WINDOW_DIVIDER_FACE_ID);
1085 struct face *face_first
1086 = FACE_OPT_FROM_ID (f, WINDOW_DIVIDER_FIRST_PIXEL_FACE_ID);
1087 struct face *face_last
1088 = FACE_OPT_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 GC gc = f->output_data.x->normal_gc;
1319 struct face *face = p->face;
1320
1321 /* Must clip because of partially visible lines. */
1322 x_clip_to_row (w, row, ANY_AREA, gc);
1323
1324 if (p->bx >= 0 && !p->overlay_p)
1325 {
1326 /* In case the same realized face is used for fringes and
1327 for something displayed in the text (e.g. face `region' on
1328 mono-displays, the fill style may have been changed to
1329 FillSolid in x_draw_glyph_string_background. */
1330 if (face->stipple)
1331 XSetFillStyle (display, face->gc, FillOpaqueStippled);
1332 else
1333 XSetForeground (display, face->gc, face->background);
1334
1335 x_fill_rectangle (f, face->gc, p->bx, p->by, p->nx, p->ny);
1336
1337 if (!face->stipple)
1338 XSetForeground (display, face->gc, face->foreground);
1339 }
1340
1341 #ifdef USE_CAIRO
1342 if (p->which && p->which < max_fringe_bmp)
1343 {
1344 XGCValues gcv;
1345
1346 XGetGCValues (display, gc, GCForeground | GCBackground, &gcv);
1347 XSetForeground (display, gc, (p->cursor_p
1348 ? (p->overlay_p ? face->background
1349 : f->output_data.x->cursor_pixel)
1350 : face->foreground));
1351 XSetBackground (display, gc, face->background);
1352 x_cr_draw_image (f, gc, fringe_bmp[p->which], 0, p->dh,
1353 p->wd, p->h, p->x, p->y, p->overlay_p);
1354 XSetForeground (display, gc, gcv.foreground);
1355 XSetBackground (display, gc, gcv.background);
1356 }
1357 #else /* not USE_CAIRO */
1358 if (p->which)
1359 {
1360 Window window = FRAME_X_WINDOW (f);
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_OPT_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 eassume (dpyinfo);
2162
2163 if (dpyinfo->color_cells == NULL)
2164 {
2165 Screen *screen = dpyinfo->screen;
2166 int ncolor_cells = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
2167 int i;
2168
2169 dpyinfo->color_cells = xnmalloc (ncolor_cells,
2170 sizeof *dpyinfo->color_cells);
2171 dpyinfo->ncolor_cells = ncolor_cells;
2172
2173 for (i = 0; i < ncolor_cells; ++i)
2174 dpyinfo->color_cells[i].pixel = i;
2175
2176 XQueryColors (dpy, dpyinfo->cmap,
2177 dpyinfo->color_cells, ncolor_cells);
2178 }
2179
2180 *ncells = dpyinfo->ncolor_cells;
2181 return dpyinfo->color_cells;
2182 }
2183
2184
2185 /* On frame F, translate pixel colors to RGB values for the NCOLORS
2186 colors in COLORS. Use cached information, if available. */
2187
2188 void
2189 x_query_colors (struct frame *f, XColor *colors, int ncolors)
2190 {
2191 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2192
2193 if (dpyinfo->red_bits > 0)
2194 {
2195 /* For TrueColor displays, we can decompose the RGB value
2196 directly. */
2197 int i;
2198 unsigned int rmult, gmult, bmult;
2199 unsigned int rmask, gmask, bmask;
2200
2201 rmask = (1 << dpyinfo->red_bits) - 1;
2202 gmask = (1 << dpyinfo->green_bits) - 1;
2203 bmask = (1 << dpyinfo->blue_bits) - 1;
2204 /* If we're widening, for example, 8 bits in the pixel value to
2205 16 bits for the separate-color representation, we want to
2206 extrapolate the lower bits based on those bits available --
2207 in other words, we'd like 0xff to become 0xffff instead of
2208 the 0xff00 we'd get by just zero-filling the lower bits.
2209
2210 We generate a 32-bit scaled-up value and shift it, in case
2211 the bit count doesn't divide 16 evenly (e.g., when dealing
2212 with a 3-3-2 bit RGB display), to get more of the lower bits
2213 correct.
2214
2215 Should we cache the multipliers in dpyinfo? Maybe
2216 special-case the 8-8-8 common case? */
2217 rmult = 0xffffffff / rmask;
2218 gmult = 0xffffffff / gmask;
2219 bmult = 0xffffffff / bmask;
2220
2221 for (i = 0; i < ncolors; ++i)
2222 {
2223 unsigned int r, g, b;
2224 unsigned long pixel = colors[i].pixel;
2225
2226 r = (pixel >> dpyinfo->red_offset) & rmask;
2227 g = (pixel >> dpyinfo->green_offset) & gmask;
2228 b = (pixel >> dpyinfo->blue_offset) & bmask;
2229
2230 colors[i].red = (r * rmult) >> 16;
2231 colors[i].green = (g * gmult) >> 16;
2232 colors[i].blue = (b * bmult) >> 16;
2233 }
2234 return;
2235 }
2236
2237 if (dpyinfo->color_cells)
2238 {
2239 int i;
2240 for (i = 0; i < ncolors; ++i)
2241 {
2242 unsigned long pixel = colors[i].pixel;
2243 eassert (pixel < dpyinfo->ncolor_cells);
2244 eassert (dpyinfo->color_cells[pixel].pixel == pixel);
2245 colors[i] = dpyinfo->color_cells[pixel];
2246 }
2247 return;
2248 }
2249
2250 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
2251 }
2252
2253
2254 /* On frame F, translate pixel color to RGB values for the color in
2255 COLOR. Use cached information, if available. */
2256
2257 void
2258 x_query_color (struct frame *f, XColor *color)
2259 {
2260 x_query_colors (f, color, 1);
2261 }
2262
2263
2264 /* On frame F, translate the color name to RGB values. Use cached
2265 information, if possible.
2266
2267 Note that there is currently no way to clean old entries out of the
2268 cache. However, it is limited to names in the server's database,
2269 and names we've actually looked up; list-colors-display is probably
2270 the most color-intensive case we're likely to hit. */
2271
2272 Status x_parse_color (struct frame *f, const char *color_name,
2273 XColor *color)
2274 {
2275 Display *dpy = FRAME_X_DISPLAY (f);
2276 Colormap cmap = FRAME_X_COLORMAP (f);
2277 struct color_name_cache_entry *cache_entry;
2278
2279 if (color_name[0] == '#')
2280 {
2281 /* The hex form is parsed directly by XParseColor without
2282 talking to the X server. No need for caching. */
2283 return XParseColor (dpy, cmap, color_name, color);
2284 }
2285
2286 for (cache_entry = FRAME_DISPLAY_INFO (f)->color_names; cache_entry;
2287 cache_entry = cache_entry->next)
2288 {
2289 if (!xstrcasecmp(cache_entry->name, color_name))
2290 {
2291 *color = cache_entry->rgb;
2292 return 1;
2293 }
2294 }
2295
2296 if (XParseColor (dpy, cmap, color_name, color) == 0)
2297 /* No caching of negative results, currently. */
2298 return 0;
2299
2300 cache_entry = xzalloc (sizeof *cache_entry);
2301 cache_entry->rgb = *color;
2302 cache_entry->name = xstrdup (color_name);
2303 cache_entry->next = FRAME_DISPLAY_INFO (f)->color_names;
2304 FRAME_DISPLAY_INFO (f)->color_names = cache_entry;
2305 return 1;
2306 }
2307
2308
2309 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
2310 exact match can't be allocated, try the nearest color available.
2311 Value is true if successful. Set *COLOR to the color
2312 allocated. */
2313
2314 static bool
2315 x_alloc_nearest_color_1 (Display *dpy, Colormap cmap, XColor *color)
2316 {
2317 bool rc;
2318
2319 rc = XAllocColor (dpy, cmap, color) != 0;
2320 if (rc == 0)
2321 {
2322 /* If we got to this point, the colormap is full, so we're going
2323 to try to get the next closest color. The algorithm used is
2324 a least-squares matching, which is what X uses for closest
2325 color matching with StaticColor visuals. */
2326 int nearest, i;
2327 int max_color_delta = 255;
2328 int max_delta = 3 * max_color_delta;
2329 int nearest_delta = max_delta + 1;
2330 int ncells;
2331 const XColor *cells = x_color_cells (dpy, &ncells);
2332
2333 for (nearest = i = 0; i < ncells; ++i)
2334 {
2335 int dred = (color->red >> 8) - (cells[i].red >> 8);
2336 int dgreen = (color->green >> 8) - (cells[i].green >> 8);
2337 int dblue = (color->blue >> 8) - (cells[i].blue >> 8);
2338 int delta = dred * dred + dgreen * dgreen + dblue * dblue;
2339
2340 if (delta < nearest_delta)
2341 {
2342 nearest = i;
2343 nearest_delta = delta;
2344 }
2345 }
2346
2347 color->red = cells[nearest].red;
2348 color->green = cells[nearest].green;
2349 color->blue = cells[nearest].blue;
2350 rc = XAllocColor (dpy, cmap, color) != 0;
2351 }
2352 else
2353 {
2354 /* If allocation succeeded, and the allocated pixel color is not
2355 equal to a cached pixel color recorded earlier, there was a
2356 change in the colormap, so clear the color cache. */
2357 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
2358 eassume (dpyinfo);
2359
2360 if (dpyinfo->color_cells)
2361 {
2362 XColor *cached_color = &dpyinfo->color_cells[color->pixel];
2363 if (cached_color->red != color->red
2364 || cached_color->blue != color->blue
2365 || cached_color->green != color->green)
2366 {
2367 xfree (dpyinfo->color_cells);
2368 dpyinfo->color_cells = NULL;
2369 dpyinfo->ncolor_cells = 0;
2370 }
2371 }
2372 }
2373
2374 #ifdef DEBUG_X_COLORS
2375 if (rc)
2376 register_color (color->pixel);
2377 #endif /* DEBUG_X_COLORS */
2378
2379 return rc;
2380 }
2381
2382
2383 /* Allocate the color COLOR->pixel on frame F, colormap CMAP, after
2384 gamma correction. If an exact match can't be allocated, try the
2385 nearest color available. Value is true if successful. Set *COLOR
2386 to the color allocated. */
2387
2388 bool
2389 x_alloc_nearest_color (struct frame *f, Colormap cmap, XColor *color)
2390 {
2391 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2392
2393 gamma_correct (f, color);
2394
2395 if (dpyinfo->red_bits > 0)
2396 {
2397 color->pixel = x_make_truecolor_pixel (dpyinfo,
2398 color->red,
2399 color->green,
2400 color->blue);
2401 return true;
2402 }
2403
2404 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
2405 }
2406
2407
2408 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
2409 It's necessary to do this instead of just using PIXEL directly to
2410 get color reference counts right. */
2411
2412 unsigned long
2413 x_copy_color (struct frame *f, unsigned long pixel)
2414 {
2415 XColor color;
2416
2417 /* If display has an immutable color map, freeing colors is not
2418 necessary and some servers don't allow it. Since we won't free a
2419 color once we've allocated it, we don't need to re-allocate it to
2420 maintain the server's reference count. */
2421 if (!x_mutable_colormap (FRAME_X_VISUAL (f)))
2422 return pixel;
2423
2424 color.pixel = pixel;
2425 block_input ();
2426 /* The color could still be found in the color_cells array. */
2427 x_query_color (f, &color);
2428 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
2429 unblock_input ();
2430 #ifdef DEBUG_X_COLORS
2431 register_color (pixel);
2432 #endif
2433 return color.pixel;
2434 }
2435
2436
2437 /* Brightness beyond which a color won't have its highlight brightness
2438 boosted.
2439
2440 Nominally, highlight colors for `3d' faces are calculated by
2441 brightening an object's color by a constant scale factor, but this
2442 doesn't yield good results for dark colors, so for colors who's
2443 brightness is less than this value (on a scale of 0-65535) have an
2444 use an additional additive factor.
2445
2446 The value here is set so that the default menu-bar/mode-line color
2447 (grey75) will not have its highlights changed at all. */
2448 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
2449
2450
2451 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
2452 or DELTA. Try a color with RGB values multiplied by FACTOR first.
2453 If this produces the same color as PIXEL, try a color where all RGB
2454 values have DELTA added. Return the allocated color in *PIXEL.
2455 DISPLAY is the X display, CMAP is the colormap to operate on.
2456 Value is non-zero if successful. */
2457
2458 static bool
2459 x_alloc_lighter_color (struct frame *f, Display *display, Colormap cmap,
2460 unsigned long *pixel, double factor, int delta)
2461 {
2462 XColor color, new;
2463 long bright;
2464 bool success_p;
2465
2466 /* Get RGB color values. */
2467 color.pixel = *pixel;
2468 x_query_color (f, &color);
2469
2470 /* Change RGB values by specified FACTOR. Avoid overflow! */
2471 eassert (factor >= 0);
2472 new.red = min (0xffff, factor * color.red);
2473 new.green = min (0xffff, factor * color.green);
2474 new.blue = min (0xffff, factor * color.blue);
2475
2476 /* Calculate brightness of COLOR. */
2477 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
2478
2479 /* We only boost colors that are darker than
2480 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
2481 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
2482 /* Make an additive adjustment to NEW, because it's dark enough so
2483 that scaling by FACTOR alone isn't enough. */
2484 {
2485 /* How far below the limit this color is (0 - 1, 1 being darker). */
2486 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
2487 /* The additive adjustment. */
2488 int min_delta = delta * dimness * factor / 2;
2489
2490 if (factor < 1)
2491 {
2492 new.red = max (0, new.red - min_delta);
2493 new.green = max (0, new.green - min_delta);
2494 new.blue = max (0, new.blue - min_delta);
2495 }
2496 else
2497 {
2498 new.red = min (0xffff, min_delta + new.red);
2499 new.green = min (0xffff, min_delta + new.green);
2500 new.blue = min (0xffff, min_delta + new.blue);
2501 }
2502 }
2503
2504 /* Try to allocate the color. */
2505 success_p = x_alloc_nearest_color (f, cmap, &new);
2506 if (success_p)
2507 {
2508 if (new.pixel == *pixel)
2509 {
2510 /* If we end up with the same color as before, try adding
2511 delta to the RGB values. */
2512 x_free_colors (f, &new.pixel, 1);
2513
2514 new.red = min (0xffff, delta + color.red);
2515 new.green = min (0xffff, delta + color.green);
2516 new.blue = min (0xffff, delta + color.blue);
2517 success_p = x_alloc_nearest_color (f, cmap, &new);
2518 }
2519 else
2520 success_p = true;
2521 *pixel = new.pixel;
2522 }
2523
2524 return success_p;
2525 }
2526
2527
2528 /* Set up the foreground color for drawing relief lines of glyph
2529 string S. RELIEF is a pointer to a struct relief containing the GC
2530 with which lines will be drawn. Use a color that is FACTOR or
2531 DELTA lighter or darker than the relief's background which is found
2532 in S->f->output_data.x->relief_background. If such a color cannot
2533 be allocated, use DEFAULT_PIXEL, instead. */
2534
2535 static void
2536 x_setup_relief_color (struct frame *f, struct relief *relief, double factor,
2537 int delta, unsigned long default_pixel)
2538 {
2539 XGCValues xgcv;
2540 struct x_output *di = f->output_data.x;
2541 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
2542 unsigned long pixel;
2543 unsigned long background = di->relief_background;
2544 Colormap cmap = FRAME_X_COLORMAP (f);
2545 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2546 Display *dpy = FRAME_X_DISPLAY (f);
2547
2548 xgcv.graphics_exposures = False;
2549 xgcv.line_width = 1;
2550
2551 /* Free previously allocated color. The color cell will be reused
2552 when it has been freed as many times as it was allocated, so this
2553 doesn't affect faces using the same colors. */
2554 if (relief->gc && relief->pixel != -1)
2555 {
2556 x_free_colors (f, &relief->pixel, 1);
2557 relief->pixel = -1;
2558 }
2559
2560 /* Allocate new color. */
2561 xgcv.foreground = default_pixel;
2562 pixel = background;
2563 if (dpyinfo->n_planes != 1
2564 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
2565 xgcv.foreground = relief->pixel = pixel;
2566
2567 if (relief->gc == 0)
2568 {
2569 xgcv.stipple = dpyinfo->gray;
2570 mask |= GCStipple;
2571 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
2572 }
2573 else
2574 XChangeGC (dpy, relief->gc, mask, &xgcv);
2575 }
2576
2577
2578 /* Set up colors for the relief lines around glyph string S. */
2579
2580 static void
2581 x_setup_relief_colors (struct glyph_string *s)
2582 {
2583 struct x_output *di = s->f->output_data.x;
2584 unsigned long color;
2585
2586 if (s->face->use_box_color_for_shadows_p)
2587 color = s->face->box_color;
2588 else if (s->first_glyph->type == IMAGE_GLYPH
2589 && s->img->pixmap
2590 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
2591 color = IMAGE_BACKGROUND (s->img, s->f, 0);
2592 else
2593 {
2594 XGCValues xgcv;
2595
2596 /* Get the background color of the face. */
2597 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
2598 color = xgcv.background;
2599 }
2600
2601 if (di->white_relief.gc == 0
2602 || color != di->relief_background)
2603 {
2604 di->relief_background = color;
2605 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
2606 WHITE_PIX_DEFAULT (s->f));
2607 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
2608 BLACK_PIX_DEFAULT (s->f));
2609 }
2610 }
2611
2612
2613 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
2614 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
2615 to draw, it must be >= 0. RAISED_P means draw a raised
2616 relief. LEFT_P means draw a relief on the left side of
2617 the rectangle. RIGHT_P means draw a relief on the right
2618 side of the rectangle. CLIP_RECT is the clipping rectangle to use
2619 when drawing. */
2620
2621 static void
2622 x_draw_relief_rect (struct frame *f,
2623 int left_x, int top_y, int right_x, int bottom_y,
2624 int width, bool raised_p, bool top_p, bool bot_p,
2625 bool left_p, bool right_p,
2626 XRectangle *clip_rect)
2627 {
2628 #ifdef USE_CAIRO
2629 GC top_left_gc, bottom_right_gc;
2630 int corners = 0;
2631
2632 if (raised_p)
2633 {
2634 top_left_gc = f->output_data.x->white_relief.gc;
2635 bottom_right_gc = f->output_data.x->black_relief.gc;
2636 }
2637 else
2638 {
2639 top_left_gc = f->output_data.x->black_relief.gc;
2640 bottom_right_gc = f->output_data.x->white_relief.gc;
2641 }
2642
2643 x_set_clip_rectangles (f, top_left_gc, clip_rect, 1);
2644 x_set_clip_rectangles (f, bottom_right_gc, clip_rect, 1);
2645
2646 if (left_p)
2647 {
2648 x_fill_rectangle (f, top_left_gc, left_x, top_y,
2649 width, bottom_y + 1 - top_y);
2650 if (top_p)
2651 corners |= 1 << CORNER_TOP_LEFT;
2652 if (bot_p)
2653 corners |= 1 << CORNER_BOTTOM_LEFT;
2654 }
2655 if (right_p)
2656 {
2657 x_fill_rectangle (f, bottom_right_gc, right_x + 1 - width, top_y,
2658 width, bottom_y + 1 - top_y);
2659 if (top_p)
2660 corners |= 1 << CORNER_TOP_RIGHT;
2661 if (bot_p)
2662 corners |= 1 << CORNER_BOTTOM_RIGHT;
2663 }
2664 if (top_p)
2665 {
2666 if (!right_p)
2667 x_fill_rectangle (f, top_left_gc, left_x, top_y,
2668 right_x + 1 - left_x, width);
2669 else
2670 x_fill_trapezoid_for_relief (f, top_left_gc, left_x, top_y,
2671 right_x + 1 - left_x, width, 1);
2672 }
2673 if (bot_p)
2674 {
2675 if (!left_p)
2676 x_fill_rectangle (f, bottom_right_gc, left_x, bottom_y + 1 - width,
2677 right_x + 1 - left_x, width);
2678 else
2679 x_fill_trapezoid_for_relief (f, bottom_right_gc,
2680 left_x, bottom_y + 1 - width,
2681 right_x + 1 - left_x, width, 0);
2682 }
2683 if (left_p && width != 1)
2684 x_fill_rectangle (f, bottom_right_gc, left_x, top_y,
2685 1, bottom_y + 1 - top_y);
2686 if (top_p && width != 1)
2687 x_fill_rectangle (f, bottom_right_gc, left_x, top_y,
2688 right_x + 1 - left_x, 1);
2689 if (corners)
2690 {
2691 XSetBackground (FRAME_X_DISPLAY (f), top_left_gc,
2692 FRAME_BACKGROUND_PIXEL (f));
2693 x_erase_corners_for_relief (f, top_left_gc, left_x, top_y,
2694 right_x - left_x + 1, bottom_y - top_y + 1,
2695 6, 1, corners);
2696 }
2697
2698 x_reset_clip_rectangles (f, top_left_gc);
2699 x_reset_clip_rectangles (f, bottom_right_gc);
2700 #else
2701 Display *dpy = FRAME_X_DISPLAY (f);
2702 Window window = FRAME_X_WINDOW (f);
2703 int i;
2704 GC gc;
2705
2706 if (raised_p)
2707 gc = f->output_data.x->white_relief.gc;
2708 else
2709 gc = f->output_data.x->black_relief.gc;
2710 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2711
2712 /* This code is more complicated than it has to be, because of two
2713 minor hacks to make the boxes look nicer: (i) if width > 1, draw
2714 the outermost line using the black relief. (ii) Omit the four
2715 corner pixels. */
2716
2717 /* Top. */
2718 if (top_p)
2719 {
2720 if (width == 1)
2721 XDrawLine (dpy, window, gc,
2722 left_x + left_p, top_y,
2723 right_x + !right_p, top_y);
2724
2725 for (i = 1; i < width; ++i)
2726 XDrawLine (dpy, window, gc,
2727 left_x + i * left_p, top_y + i,
2728 right_x + 1 - i * right_p, top_y + i);
2729 }
2730
2731 /* Left. */
2732 if (left_p)
2733 {
2734 if (width == 1)
2735 XDrawLine (dpy, window, gc, left_x, top_y + 1, left_x, bottom_y);
2736
2737 XClearArea (dpy, window, left_x, top_y, 1, 1, False);
2738 XClearArea (dpy, window, left_x, bottom_y, 1, 1, False);
2739
2740 for (i = (width > 1 ? 1 : 0); i < width; ++i)
2741 XDrawLine (dpy, window, gc,
2742 left_x + i, top_y + (i + 1) * top_p,
2743 left_x + i, bottom_y + 1 - (i + 1) * bot_p);
2744 }
2745
2746 XSetClipMask (dpy, gc, None);
2747 if (raised_p)
2748 gc = f->output_data.x->black_relief.gc;
2749 else
2750 gc = f->output_data.x->white_relief.gc;
2751 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2752
2753 if (width > 1)
2754 {
2755 /* Outermost top line. */
2756 if (top_p)
2757 XDrawLine (dpy, window, gc,
2758 left_x + left_p, top_y,
2759 right_x + !right_p, top_y);
2760
2761 /* Outermost left line. */
2762 if (left_p)
2763 XDrawLine (dpy, window, gc, left_x, top_y + 1, left_x, bottom_y);
2764 }
2765
2766 /* Bottom. */
2767 if (bot_p)
2768 {
2769 XDrawLine (dpy, window, gc,
2770 left_x + left_p, bottom_y,
2771 right_x + !right_p, bottom_y);
2772 for (i = 1; i < width; ++i)
2773 XDrawLine (dpy, window, gc,
2774 left_x + i * left_p, bottom_y - i,
2775 right_x + 1 - i * right_p, bottom_y - i);
2776 }
2777
2778 /* Right. */
2779 if (right_p)
2780 {
2781 XClearArea (dpy, window, right_x, top_y, 1, 1, False);
2782 XClearArea (dpy, window, right_x, bottom_y, 1, 1, False);
2783 for (i = 0; i < width; ++i)
2784 XDrawLine (dpy, window, gc,
2785 right_x - i, top_y + (i + 1) * top_p,
2786 right_x - i, bottom_y + 1 - (i + 1) * bot_p);
2787 }
2788
2789 x_reset_clip_rectangles (f, gc);
2790
2791 #endif
2792 }
2793
2794
2795 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
2796 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
2797 draw, it must be >= 0. LEFT_P means draw a line on the
2798 left side of the rectangle. RIGHT_P means draw a line
2799 on the right side of the rectangle. CLIP_RECT is the clipping
2800 rectangle to use when drawing. */
2801
2802 static void
2803 x_draw_box_rect (struct glyph_string *s,
2804 int left_x, int top_y, int right_x, int bottom_y, int width,
2805 bool left_p, bool right_p, XRectangle *clip_rect)
2806 {
2807 XGCValues xgcv;
2808
2809 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2810 XSetForeground (s->display, s->gc, s->face->box_color);
2811 x_set_clip_rectangles (s->f, s->gc, clip_rect, 1);
2812
2813 /* Top. */
2814 x_fill_rectangle (s->f, s->gc,
2815 left_x, top_y, right_x - left_x + 1, width);
2816
2817 /* Left. */
2818 if (left_p)
2819 x_fill_rectangle (s->f, s->gc,
2820 left_x, top_y, width, bottom_y - top_y + 1);
2821
2822 /* Bottom. */
2823 x_fill_rectangle (s->f, s->gc,
2824 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
2825
2826 /* Right. */
2827 if (right_p)
2828 x_fill_rectangle (s->f, s->gc,
2829 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
2830
2831 XSetForeground (s->display, s->gc, xgcv.foreground);
2832 x_reset_clip_rectangles (s->f, s->gc);
2833 }
2834
2835
2836 /* Draw a box around glyph string S. */
2837
2838 static void
2839 x_draw_glyph_string_box (struct glyph_string *s)
2840 {
2841 int width, left_x, right_x, top_y, bottom_y, last_x;
2842 bool raised_p, left_p, right_p;
2843 struct glyph *last_glyph;
2844 XRectangle clip_rect;
2845
2846 last_x = ((s->row->full_width_p && !s->w->pseudo_window_p)
2847 ? WINDOW_RIGHT_EDGE_X (s->w)
2848 : window_box_right (s->w, s->area));
2849
2850 /* The glyph that may have a right box line. */
2851 last_glyph = (s->cmp || s->img
2852 ? s->first_glyph
2853 : s->first_glyph + s->nchars - 1);
2854
2855 width = eabs (s->face->box_line_width);
2856 raised_p = s->face->box == FACE_RAISED_BOX;
2857 left_x = s->x;
2858 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
2859 ? last_x - 1
2860 : min (last_x, s->x + s->background_width) - 1);
2861 top_y = s->y;
2862 bottom_y = top_y + s->height - 1;
2863
2864 left_p = (s->first_glyph->left_box_line_p
2865 || (s->hl == DRAW_MOUSE_FACE
2866 && (s->prev == NULL
2867 || s->prev->hl != s->hl)));
2868 right_p = (last_glyph->right_box_line_p
2869 || (s->hl == DRAW_MOUSE_FACE
2870 && (s->next == NULL
2871 || s->next->hl != s->hl)));
2872
2873 get_glyph_string_clip_rect (s, &clip_rect);
2874
2875 if (s->face->box == FACE_SIMPLE_BOX)
2876 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2877 left_p, right_p, &clip_rect);
2878 else
2879 {
2880 x_setup_relief_colors (s);
2881 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
2882 width, raised_p, true, true, left_p, right_p,
2883 &clip_rect);
2884 }
2885 }
2886
2887
2888 /* Draw foreground of image glyph string S. */
2889
2890 static void
2891 x_draw_image_foreground (struct glyph_string *s)
2892 {
2893 int x = s->x;
2894 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2895
2896 /* If first glyph of S has a left box line, start drawing it to the
2897 right of that line. */
2898 if (s->face->box != FACE_NO_BOX
2899 && s->first_glyph->left_box_line_p
2900 && s->slice.x == 0)
2901 x += eabs (s->face->box_line_width);
2902
2903 /* If there is a margin around the image, adjust x- and y-position
2904 by that margin. */
2905 if (s->slice.x == 0)
2906 x += s->img->hmargin;
2907 if (s->slice.y == 0)
2908 y += s->img->vmargin;
2909
2910 if (s->img->pixmap)
2911 {
2912 if (s->img->mask)
2913 {
2914 /* We can't set both a clip mask and use XSetClipRectangles
2915 because the latter also sets a clip mask. We also can't
2916 trust on the shape extension to be available
2917 (XShapeCombineRegion). So, compute the rectangle to draw
2918 manually. */
2919 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2920 | GCFunction);
2921 XGCValues xgcv;
2922 XRectangle clip_rect, image_rect, r;
2923
2924 xgcv.clip_mask = s->img->mask;
2925 xgcv.clip_x_origin = x;
2926 xgcv.clip_y_origin = y;
2927 xgcv.function = GXcopy;
2928 XChangeGC (s->display, s->gc, mask, &xgcv);
2929
2930 get_glyph_string_clip_rect (s, &clip_rect);
2931 image_rect.x = x;
2932 image_rect.y = y;
2933 image_rect.width = s->slice.width;
2934 image_rect.height = s->slice.height;
2935 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2936 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2937 s->slice.x + r.x - x, s->slice.y + r.y - y,
2938 r.width, r.height, r.x, r.y);
2939 }
2940 else
2941 {
2942 XRectangle clip_rect, image_rect, r;
2943
2944 get_glyph_string_clip_rect (s, &clip_rect);
2945 image_rect.x = x;
2946 image_rect.y = y;
2947 image_rect.width = s->slice.width;
2948 image_rect.height = s->slice.height;
2949 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2950 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2951 s->slice.x + r.x - x, s->slice.y + r.y - y,
2952 r.width, r.height, r.x, r.y);
2953
2954 /* When the image has a mask, we can expect that at
2955 least part of a mouse highlight or a block cursor will
2956 be visible. If the image doesn't have a mask, make
2957 a block cursor visible by drawing a rectangle around
2958 the image. I believe it's looking better if we do
2959 nothing here for mouse-face. */
2960 if (s->hl == DRAW_CURSOR)
2961 {
2962 int relief = eabs (s->img->relief);
2963 x_draw_rectangle (s->f, s->gc,
2964 x - relief, y - relief,
2965 s->slice.width + relief*2 - 1,
2966 s->slice.height + relief*2 - 1);
2967 }
2968 }
2969 }
2970 else
2971 /* Draw a rectangle if image could not be loaded. */
2972 x_draw_rectangle (s->f, s->gc, x, y,
2973 s->slice.width - 1, s->slice.height - 1);
2974 }
2975
2976
2977 /* Draw a relief around the image glyph string S. */
2978
2979 static void
2980 x_draw_image_relief (struct glyph_string *s)
2981 {
2982 int x1, y1, thick;
2983 bool raised_p, top_p, bot_p, left_p, right_p;
2984 int extra_x, extra_y;
2985 XRectangle r;
2986 int x = s->x;
2987 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2988
2989 /* If first glyph of S has a left box line, start drawing it to the
2990 right of that line. */
2991 if (s->face->box != FACE_NO_BOX
2992 && s->first_glyph->left_box_line_p
2993 && s->slice.x == 0)
2994 x += eabs (s->face->box_line_width);
2995
2996 /* If there is a margin around the image, adjust x- and y-position
2997 by that margin. */
2998 if (s->slice.x == 0)
2999 x += s->img->hmargin;
3000 if (s->slice.y == 0)
3001 y += s->img->vmargin;
3002
3003 if (s->hl == DRAW_IMAGE_SUNKEN
3004 || s->hl == DRAW_IMAGE_RAISED)
3005 {
3006 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
3007 raised_p = s->hl == DRAW_IMAGE_RAISED;
3008 }
3009 else
3010 {
3011 thick = eabs (s->img->relief);
3012 raised_p = s->img->relief > 0;
3013 }
3014
3015 x1 = x + s->slice.width - 1;
3016 y1 = y + s->slice.height - 1;
3017
3018 extra_x = extra_y = 0;
3019 if (s->face->id == TOOL_BAR_FACE_ID)
3020 {
3021 if (CONSP (Vtool_bar_button_margin)
3022 && INTEGERP (XCAR (Vtool_bar_button_margin))
3023 && INTEGERP (XCDR (Vtool_bar_button_margin)))
3024 {
3025 extra_x = XINT (XCAR (Vtool_bar_button_margin));
3026 extra_y = XINT (XCDR (Vtool_bar_button_margin));
3027 }
3028 else if (INTEGERP (Vtool_bar_button_margin))
3029 extra_x = extra_y = XINT (Vtool_bar_button_margin);
3030 }
3031
3032 top_p = bot_p = left_p = right_p = false;
3033
3034 if (s->slice.x == 0)
3035 x -= thick + extra_x, left_p = true;
3036 if (s->slice.y == 0)
3037 y -= thick + extra_y, top_p = true;
3038 if (s->slice.x + s->slice.width == s->img->width)
3039 x1 += thick + extra_x, right_p = true;
3040 if (s->slice.y + s->slice.height == s->img->height)
3041 y1 += thick + extra_y, bot_p = true;
3042
3043 x_setup_relief_colors (s);
3044 get_glyph_string_clip_rect (s, &r);
3045 x_draw_relief_rect (s->f, x, y, x1, y1, thick, raised_p,
3046 top_p, bot_p, left_p, right_p, &r);
3047 }
3048
3049
3050 /* Draw the foreground of image glyph string S to PIXMAP. */
3051
3052 static void
3053 x_draw_image_foreground_1 (struct glyph_string *s, Pixmap pixmap)
3054 {
3055 int x = 0;
3056 int y = s->ybase - s->y - image_ascent (s->img, s->face, &s->slice);
3057
3058 /* If first glyph of S has a left box line, start drawing it to the
3059 right of that line. */
3060 if (s->face->box != FACE_NO_BOX
3061 && s->first_glyph->left_box_line_p
3062 && s->slice.x == 0)
3063 x += eabs (s->face->box_line_width);
3064
3065 /* If there is a margin around the image, adjust x- and y-position
3066 by that margin. */
3067 if (s->slice.x == 0)
3068 x += s->img->hmargin;
3069 if (s->slice.y == 0)
3070 y += s->img->vmargin;
3071
3072 if (s->img->pixmap)
3073 {
3074 if (s->img->mask)
3075 {
3076 /* We can't set both a clip mask and use XSetClipRectangles
3077 because the latter also sets a clip mask. We also can't
3078 trust on the shape extension to be available
3079 (XShapeCombineRegion). So, compute the rectangle to draw
3080 manually. */
3081 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3082 | GCFunction);
3083 XGCValues xgcv;
3084
3085 xgcv.clip_mask = s->img->mask;
3086 xgcv.clip_x_origin = x - s->slice.x;
3087 xgcv.clip_y_origin = y - s->slice.y;
3088 xgcv.function = GXcopy;
3089 XChangeGC (s->display, s->gc, mask, &xgcv);
3090
3091 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3092 s->slice.x, s->slice.y,
3093 s->slice.width, s->slice.height, x, y);
3094 XSetClipMask (s->display, s->gc, None);
3095 }
3096 else
3097 {
3098 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3099 s->slice.x, s->slice.y,
3100 s->slice.width, s->slice.height, x, y);
3101
3102 /* When the image has a mask, we can expect that at
3103 least part of a mouse highlight or a block cursor will
3104 be visible. If the image doesn't have a mask, make
3105 a block cursor visible by drawing a rectangle around
3106 the image. I believe it's looking better if we do
3107 nothing here for mouse-face. */
3108 if (s->hl == DRAW_CURSOR)
3109 {
3110 int r = eabs (s->img->relief);
3111 x_draw_rectangle (s->f, s->gc, x - r, y - r,
3112 s->slice.width + r*2 - 1,
3113 s->slice.height + r*2 - 1);
3114 }
3115 }
3116 }
3117 else
3118 /* Draw a rectangle if image could not be loaded. */
3119 x_draw_rectangle (s->f, s->gc, x, y,
3120 s->slice.width - 1, s->slice.height - 1);
3121 }
3122
3123
3124 /* Draw part of the background of glyph string S. X, Y, W, and H
3125 give the rectangle to draw. */
3126
3127 static void
3128 x_draw_glyph_string_bg_rect (struct glyph_string *s, int x, int y, int w, int h)
3129 {
3130 if (s->stippled_p)
3131 {
3132 /* Fill background with a stipple pattern. */
3133 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3134 x_fill_rectangle (s->f, s->gc, x, y, w, h);
3135 XSetFillStyle (s->display, s->gc, FillSolid);
3136 }
3137 else
3138 x_clear_glyph_string_rect (s, x, y, w, h);
3139 }
3140
3141
3142 /* Draw image glyph string S.
3143
3144 s->y
3145 s->x +-------------------------
3146 | s->face->box
3147 |
3148 | +-------------------------
3149 | | s->img->margin
3150 | |
3151 | | +-------------------
3152 | | | the image
3153
3154 */
3155
3156 static void
3157 x_draw_image_glyph_string (struct glyph_string *s)
3158 {
3159 int box_line_hwidth = eabs (s->face->box_line_width);
3160 int box_line_vwidth = max (s->face->box_line_width, 0);
3161 int height;
3162 Pixmap pixmap = None;
3163
3164 height = s->height;
3165 if (s->slice.y == 0)
3166 height -= box_line_vwidth;
3167 if (s->slice.y + s->slice.height >= s->img->height)
3168 height -= box_line_vwidth;
3169
3170 /* Fill background with face under the image. Do it only if row is
3171 taller than image or if image has a clip mask to reduce
3172 flickering. */
3173 s->stippled_p = s->face->stipple != 0;
3174 if (height > s->slice.height
3175 || s->img->hmargin
3176 || s->img->vmargin
3177 || s->img->mask
3178 || s->img->pixmap == 0
3179 || s->width != s->background_width)
3180 {
3181 if (s->img->mask)
3182 {
3183 /* Create a pixmap as large as the glyph string. Fill it
3184 with the background color. Copy the image to it, using
3185 its mask. Copy the temporary pixmap to the display. */
3186 Screen *screen = FRAME_X_SCREEN (s->f);
3187 int depth = DefaultDepthOfScreen (screen);
3188
3189 /* Create a pixmap as large as the glyph string. */
3190 pixmap = XCreatePixmap (s->display, s->window,
3191 s->background_width,
3192 s->height, depth);
3193
3194 /* Don't clip in the following because we're working on the
3195 pixmap. */
3196 XSetClipMask (s->display, s->gc, None);
3197
3198 /* Fill the pixmap with the background color/stipple. */
3199 if (s->stippled_p)
3200 {
3201 /* Fill background with a stipple pattern. */
3202 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3203 XSetTSOrigin (s->display, s->gc, - s->x, - s->y);
3204 XFillRectangle (s->display, pixmap, s->gc,
3205 0, 0, s->background_width, s->height);
3206 XSetFillStyle (s->display, s->gc, FillSolid);
3207 XSetTSOrigin (s->display, s->gc, 0, 0);
3208 }
3209 else
3210 {
3211 XGCValues xgcv;
3212 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
3213 &xgcv);
3214 XSetForeground (s->display, s->gc, xgcv.background);
3215 XFillRectangle (s->display, pixmap, s->gc,
3216 0, 0, s->background_width, s->height);
3217 XSetForeground (s->display, s->gc, xgcv.foreground);
3218 }
3219 }
3220 else
3221 {
3222 int x = s->x;
3223 int y = s->y;
3224 int width = s->background_width;
3225
3226 if (s->first_glyph->left_box_line_p
3227 && s->slice.x == 0)
3228 {
3229 x += box_line_hwidth;
3230 width -= box_line_hwidth;
3231 }
3232
3233 if (s->slice.y == 0)
3234 y += box_line_vwidth;
3235
3236 x_draw_glyph_string_bg_rect (s, x, y, width, height);
3237 }
3238
3239 s->background_filled_p = true;
3240 }
3241
3242 /* Draw the foreground. */
3243 #ifdef USE_CAIRO
3244 if (s->img->cr_data)
3245 {
3246 cairo_t *cr = x_begin_cr_clip (s->f, s->gc);
3247
3248 int x = s->x + s->img->hmargin;
3249 int y = s->y + s->img->vmargin;
3250 int width = s->background_width;
3251
3252 cairo_set_source_surface (cr, s->img->cr_data,
3253 x - s->slice.x,
3254 y - s->slice.y);
3255 cairo_rectangle (cr, x, y, width, height);
3256 cairo_fill (cr);
3257 x_end_cr_clip (s->f);
3258 }
3259 else
3260 #endif
3261 if (pixmap != None)
3262 {
3263 x_draw_image_foreground_1 (s, pixmap);
3264 x_set_glyph_string_clipping (s);
3265 XCopyArea (s->display, pixmap, s->window, s->gc,
3266 0, 0, s->background_width, s->height, s->x, s->y);
3267 XFreePixmap (s->display, pixmap);
3268 }
3269 else
3270 x_draw_image_foreground (s);
3271
3272 /* If we must draw a relief around the image, do it. */
3273 if (s->img->relief
3274 || s->hl == DRAW_IMAGE_RAISED
3275 || s->hl == DRAW_IMAGE_SUNKEN)
3276 x_draw_image_relief (s);
3277 }
3278
3279
3280 /* Draw stretch glyph string S. */
3281
3282 static void
3283 x_draw_stretch_glyph_string (struct glyph_string *s)
3284 {
3285 eassert (s->first_glyph->type == STRETCH_GLYPH);
3286
3287 if (s->hl == DRAW_CURSOR
3288 && !x_stretch_cursor_p)
3289 {
3290 /* If `x-stretch-cursor' is nil, don't draw a block cursor as
3291 wide as the stretch glyph. */
3292 int width, background_width = s->background_width;
3293 int x = s->x;
3294
3295 if (!s->row->reversed_p)
3296 {
3297 int left_x = window_box_left_offset (s->w, TEXT_AREA);
3298
3299 if (x < left_x)
3300 {
3301 background_width -= left_x - x;
3302 x = left_x;
3303 }
3304 }
3305 else
3306 {
3307 /* In R2L rows, draw the cursor on the right edge of the
3308 stretch glyph. */
3309 int right_x = window_box_right (s->w, TEXT_AREA);
3310
3311 if (x + background_width > right_x)
3312 background_width -= x - right_x;
3313 x += background_width;
3314 }
3315 width = min (FRAME_COLUMN_WIDTH (s->f), background_width);
3316 if (s->row->reversed_p)
3317 x -= width;
3318
3319 /* Draw cursor. */
3320 x_draw_glyph_string_bg_rect (s, x, s->y, width, s->height);
3321
3322 /* Clear rest using the GC of the original non-cursor face. */
3323 if (width < background_width)
3324 {
3325 int y = s->y;
3326 int w = background_width - width, h = s->height;
3327 XRectangle r;
3328 GC gc;
3329
3330 if (!s->row->reversed_p)
3331 x += width;
3332 else
3333 x = s->x;
3334 if (s->row->mouse_face_p
3335 && cursor_in_mouse_face_p (s->w))
3336 {
3337 x_set_mouse_face_gc (s);
3338 gc = s->gc;
3339 }
3340 else
3341 gc = s->face->gc;
3342
3343 get_glyph_string_clip_rect (s, &r);
3344 x_set_clip_rectangles (s->f, gc, &r, 1);
3345
3346 if (s->face->stipple)
3347 {
3348 /* Fill background with a stipple pattern. */
3349 XSetFillStyle (s->display, gc, FillOpaqueStippled);
3350 x_fill_rectangle (s->f, gc, x, y, w, h);
3351 XSetFillStyle (s->display, gc, FillSolid);
3352 }
3353 else
3354 {
3355 XGCValues xgcv;
3356 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
3357 XSetForeground (s->display, gc, xgcv.background);
3358 x_fill_rectangle (s->f, gc, x, y, w, h);
3359 XSetForeground (s->display, gc, xgcv.foreground);
3360 }
3361
3362 x_reset_clip_rectangles (s->f, gc);
3363 }
3364 }
3365 else if (!s->background_filled_p)
3366 {
3367 int background_width = s->background_width;
3368 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
3369
3370 /* Don't draw into left margin, fringe or scrollbar area
3371 except for header line and mode line. */
3372 if (x < left_x && !s->row->mode_line_p)
3373 {
3374 background_width -= left_x - x;
3375 x = left_x;
3376 }
3377 if (background_width > 0)
3378 x_draw_glyph_string_bg_rect (s, x, s->y, background_width, s->height);
3379 }
3380
3381 s->background_filled_p = true;
3382 }
3383
3384 /*
3385 Draw a wavy line under S. The wave fills wave_height pixels from y0.
3386
3387 x0 wave_length = 2
3388 --
3389 y0 * * * * *
3390 |* * * * * * * * *
3391 wave_height = 3 | * * * *
3392
3393 */
3394
3395 static void
3396 x_draw_underwave (struct glyph_string *s)
3397 {
3398 int wave_height = 3, wave_length = 2;
3399 #ifdef USE_CAIRO
3400 x_draw_horizontal_wave (s->f, s->gc, s->x, s->ybase - wave_height + 3,
3401 s->width, wave_height, wave_length);
3402 #else /* not USE_CAIRO */
3403 int dx, dy, x0, y0, width, x1, y1, x2, y2, xmax;
3404 bool odd;
3405 XRectangle wave_clip, string_clip, final_clip;
3406
3407 dx = wave_length;
3408 dy = wave_height - 1;
3409 x0 = s->x;
3410 y0 = s->ybase - wave_height + 3;
3411 width = s->width;
3412 xmax = x0 + width;
3413
3414 /* Find and set clipping rectangle */
3415
3416 wave_clip.x = x0;
3417 wave_clip.y = y0;
3418 wave_clip.width = width;
3419 wave_clip.height = wave_height;
3420 get_glyph_string_clip_rect (s, &string_clip);
3421
3422 if (!x_intersect_rectangles (&wave_clip, &string_clip, &final_clip))
3423 return;
3424
3425 XSetClipRectangles (s->display, s->gc, 0, 0, &final_clip, 1, Unsorted);
3426
3427 /* Draw the waves */
3428
3429 x1 = x0 - (x0 % dx);
3430 x2 = x1 + dx;
3431 odd = (x1 / dx) & 1;
3432 y1 = y2 = y0;
3433
3434 if (odd)
3435 y1 += dy;
3436 else
3437 y2 += dy;
3438
3439 if (INT_MAX - dx < xmax)
3440 emacs_abort ();
3441
3442 while (x1 <= xmax)
3443 {
3444 XDrawLine (s->display, s->window, s->gc, x1, y1, x2, y2);
3445 x1 = x2, y1 = y2;
3446 x2 += dx, y2 = y0 + odd*dy;
3447 odd = !odd;
3448 }
3449
3450 /* Restore previous clipping rectangle(s) */
3451 XSetClipRectangles (s->display, s->gc, 0, 0, s->clip, s->num_clips, Unsorted);
3452 #endif /* not USE_CAIRO */
3453 }
3454
3455
3456 /* Draw glyph string S. */
3457
3458 static void
3459 x_draw_glyph_string (struct glyph_string *s)
3460 {
3461 bool relief_drawn_p = false;
3462
3463 /* If S draws into the background of its successors, draw the
3464 background of the successors first so that S can draw into it.
3465 This makes S->next use XDrawString instead of XDrawImageString. */
3466 if (s->next && s->right_overhang && !s->for_overlaps)
3467 {
3468 int width;
3469 struct glyph_string *next;
3470
3471 for (width = 0, next = s->next;
3472 next && width < s->right_overhang;
3473 width += next->width, next = next->next)
3474 if (next->first_glyph->type != IMAGE_GLYPH)
3475 {
3476 x_set_glyph_string_gc (next);
3477 x_set_glyph_string_clipping (next);
3478 if (next->first_glyph->type == STRETCH_GLYPH)
3479 x_draw_stretch_glyph_string (next);
3480 else
3481 x_draw_glyph_string_background (next, true);
3482 next->num_clips = 0;
3483 }
3484 }
3485
3486 /* Set up S->gc, set clipping and draw S. */
3487 x_set_glyph_string_gc (s);
3488
3489 /* Draw relief (if any) in advance for char/composition so that the
3490 glyph string can be drawn over it. */
3491 if (!s->for_overlaps
3492 && s->face->box != FACE_NO_BOX
3493 && (s->first_glyph->type == CHAR_GLYPH
3494 || s->first_glyph->type == COMPOSITE_GLYPH))
3495
3496 {
3497 x_set_glyph_string_clipping (s);
3498 x_draw_glyph_string_background (s, true);
3499 x_draw_glyph_string_box (s);
3500 x_set_glyph_string_clipping (s);
3501 relief_drawn_p = true;
3502 }
3503 else if (!s->clip_head /* draw_glyphs didn't specify a clip mask. */
3504 && !s->clip_tail
3505 && ((s->prev && s->prev->hl != s->hl && s->left_overhang)
3506 || (s->next && s->next->hl != s->hl && s->right_overhang)))
3507 /* We must clip just this glyph. left_overhang part has already
3508 drawn when s->prev was drawn, and right_overhang part will be
3509 drawn later when s->next is drawn. */
3510 x_set_glyph_string_clipping_exactly (s, s);
3511 else
3512 x_set_glyph_string_clipping (s);
3513
3514 switch (s->first_glyph->type)
3515 {
3516 case IMAGE_GLYPH:
3517 x_draw_image_glyph_string (s);
3518 break;
3519
3520 case XWIDGET_GLYPH:
3521 x_draw_xwidget_glyph_string (s);
3522 break;
3523
3524 case STRETCH_GLYPH:
3525 x_draw_stretch_glyph_string (s);
3526 break;
3527
3528 case CHAR_GLYPH:
3529 if (s->for_overlaps)
3530 s->background_filled_p = true;
3531 else
3532 x_draw_glyph_string_background (s, false);
3533 x_draw_glyph_string_foreground (s);
3534 break;
3535
3536 case COMPOSITE_GLYPH:
3537 if (s->for_overlaps || (s->cmp_from > 0
3538 && ! s->first_glyph->u.cmp.automatic))
3539 s->background_filled_p = true;
3540 else
3541 x_draw_glyph_string_background (s, true);
3542 x_draw_composite_glyph_string_foreground (s);
3543 break;
3544
3545 case GLYPHLESS_GLYPH:
3546 if (s->for_overlaps)
3547 s->background_filled_p = true;
3548 else
3549 x_draw_glyph_string_background (s, true);
3550 x_draw_glyphless_glyph_string_foreground (s);
3551 break;
3552
3553 default:
3554 emacs_abort ();
3555 }
3556
3557 if (!s->for_overlaps)
3558 {
3559 /* Draw underline. */
3560 if (s->face->underline_p)
3561 {
3562 if (s->face->underline_type == FACE_UNDER_WAVE)
3563 {
3564 if (s->face->underline_defaulted_p)
3565 x_draw_underwave (s);
3566 else
3567 {
3568 XGCValues xgcv;
3569 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3570 XSetForeground (s->display, s->gc, s->face->underline_color);
3571 x_draw_underwave (s);
3572 XSetForeground (s->display, s->gc, xgcv.foreground);
3573 }
3574 }
3575 else if (s->face->underline_type == FACE_UNDER_LINE)
3576 {
3577 unsigned long thickness, position;
3578 int y;
3579
3580 if (s->prev && s->prev->face->underline_p
3581 && s->prev->face->underline_type == FACE_UNDER_LINE)
3582 {
3583 /* We use the same underline style as the previous one. */
3584 thickness = s->prev->underline_thickness;
3585 position = s->prev->underline_position;
3586 }
3587 else
3588 {
3589 /* Get the underline thickness. Default is 1 pixel. */
3590 if (s->font && s->font->underline_thickness > 0)
3591 thickness = s->font->underline_thickness;
3592 else
3593 thickness = 1;
3594 if (x_underline_at_descent_line)
3595 position = (s->height - thickness) - (s->ybase - s->y);
3596 else
3597 {
3598 /* Get the underline position. This is the recommended
3599 vertical offset in pixels from the baseline to the top of
3600 the underline. This is a signed value according to the
3601 specs, and its default is
3602
3603 ROUND ((maximum descent) / 2), with
3604 ROUND(x) = floor (x + 0.5) */
3605
3606 if (x_use_underline_position_properties
3607 && s->font && s->font->underline_position >= 0)
3608 position = s->font->underline_position;
3609 else if (s->font)
3610 position = (s->font->descent + 1) / 2;
3611 else
3612 position = underline_minimum_offset;
3613 }
3614 position = max (position, underline_minimum_offset);
3615 }
3616 /* Check the sanity of thickness and position. We should
3617 avoid drawing underline out of the current line area. */
3618 if (s->y + s->height <= s->ybase + position)
3619 position = (s->height - 1) - (s->ybase - s->y);
3620 if (s->y + s->height < s->ybase + position + thickness)
3621 thickness = (s->y + s->height) - (s->ybase + position);
3622 s->underline_thickness = thickness;
3623 s->underline_position = position;
3624 y = s->ybase + position;
3625 if (s->face->underline_defaulted_p)
3626 x_fill_rectangle (s->f, s->gc,
3627 s->x, y, s->width, thickness);
3628 else
3629 {
3630 XGCValues xgcv;
3631 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3632 XSetForeground (s->display, s->gc, s->face->underline_color);
3633 x_fill_rectangle (s->f, s->gc,
3634 s->x, y, s->width, thickness);
3635 XSetForeground (s->display, s->gc, xgcv.foreground);
3636 }
3637 }
3638 }
3639 /* Draw overline. */
3640 if (s->face->overline_p)
3641 {
3642 unsigned long dy = 0, h = 1;
3643
3644 if (s->face->overline_color_defaulted_p)
3645 x_fill_rectangle (s->f, s->gc, s->x, s->y + dy,
3646 s->width, h);
3647 else
3648 {
3649 XGCValues xgcv;
3650 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3651 XSetForeground (s->display, s->gc, s->face->overline_color);
3652 x_fill_rectangle (s->f, s->gc, s->x, s->y + dy,
3653 s->width, h);
3654 XSetForeground (s->display, s->gc, xgcv.foreground);
3655 }
3656 }
3657
3658 /* Draw strike-through. */
3659 if (s->face->strike_through_p)
3660 {
3661 unsigned long h = 1;
3662 unsigned long dy = (s->height - h) / 2;
3663
3664 if (s->face->strike_through_color_defaulted_p)
3665 x_fill_rectangle (s->f, s->gc, s->x, s->y + dy,
3666 s->width, h);
3667 else
3668 {
3669 XGCValues xgcv;
3670 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3671 XSetForeground (s->display, s->gc, s->face->strike_through_color);
3672 x_fill_rectangle (s->f, s->gc, s->x, s->y + dy,
3673 s->width, h);
3674 XSetForeground (s->display, s->gc, xgcv.foreground);
3675 }
3676 }
3677
3678 /* Draw relief if not yet drawn. */
3679 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
3680 x_draw_glyph_string_box (s);
3681
3682 if (s->prev)
3683 {
3684 struct glyph_string *prev;
3685
3686 for (prev = s->prev; prev; prev = prev->prev)
3687 if (prev->hl != s->hl
3688 && prev->x + prev->width + prev->right_overhang > s->x)
3689 {
3690 /* As prev was drawn while clipped to its own area, we
3691 must draw the right_overhang part using s->hl now. */
3692 enum draw_glyphs_face save = prev->hl;
3693
3694 prev->hl = s->hl;
3695 x_set_glyph_string_gc (prev);
3696 x_set_glyph_string_clipping_exactly (s, prev);
3697 if (prev->first_glyph->type == CHAR_GLYPH)
3698 x_draw_glyph_string_foreground (prev);
3699 else
3700 x_draw_composite_glyph_string_foreground (prev);
3701 x_reset_clip_rectangles (prev->f, prev->gc);
3702 prev->hl = save;
3703 prev->num_clips = 0;
3704 }
3705 }
3706
3707 if (s->next)
3708 {
3709 struct glyph_string *next;
3710
3711 for (next = s->next; next; next = next->next)
3712 if (next->hl != s->hl
3713 && next->x - next->left_overhang < s->x + s->width)
3714 {
3715 /* As next will be drawn while clipped to its own area,
3716 we must draw the left_overhang part using s->hl now. */
3717 enum draw_glyphs_face save = next->hl;
3718
3719 next->hl = s->hl;
3720 x_set_glyph_string_gc (next);
3721 x_set_glyph_string_clipping_exactly (s, next);
3722 if (next->first_glyph->type == CHAR_GLYPH)
3723 x_draw_glyph_string_foreground (next);
3724 else
3725 x_draw_composite_glyph_string_foreground (next);
3726 x_reset_clip_rectangles (next->f, next->gc);
3727 next->hl = save;
3728 next->num_clips = 0;
3729 next->clip_head = s->next;
3730 }
3731 }
3732 }
3733
3734 /* Reset clipping. */
3735 x_reset_clip_rectangles (s->f, s->gc);
3736 s->num_clips = 0;
3737 }
3738
3739 /* Shift display to make room for inserted glyphs. */
3740
3741 static void
3742 x_shift_glyphs_for_insert (struct frame *f, int x, int y, int width, int height, int shift_by)
3743 {
3744 /* Never called on a GUI frame, see
3745 http://lists.gnu.org/archive/html/emacs-devel/2015-05/msg00456.html
3746 */
3747 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
3748 f->output_data.x->normal_gc,
3749 x, y, width, height,
3750 x + shift_by, y);
3751 }
3752
3753 /* Delete N glyphs at the nominal cursor position. Not implemented
3754 for X frames. */
3755
3756 static void
3757 x_delete_glyphs (struct frame *f, register int n)
3758 {
3759 emacs_abort ();
3760 }
3761
3762
3763 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
3764 If they are <= 0, this is probably an error. */
3765
3766 static ATTRIBUTE_UNUSED void
3767 x_clear_area1 (Display *dpy, Window window,
3768 int x, int y, int width, int height, int exposures)
3769 {
3770 eassert (width > 0 && height > 0);
3771 XClearArea (dpy, window, x, y, width, height, exposures);
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 goto OTHER;
7905
7906 case KeyPress:
7907
7908 x_display_set_last_user_time (dpyinfo, event->xkey.time);
7909 ignore_next_mouse_click_timeout = 0;
7910
7911 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
7912 /* Dispatch KeyPress events when in menu. */
7913 if (popup_activated ())
7914 goto OTHER;
7915 #endif
7916
7917 f = any;
7918
7919 /* If mouse-highlight is an integer, input clears out
7920 mouse highlighting. */
7921 if (!hlinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
7922 #if ! defined (USE_GTK)
7923 && (f == 0
7924 || !EQ (f->tool_bar_window, hlinfo->mouse_face_window))
7925 #endif
7926 )
7927 {
7928 clear_mouse_face (hlinfo);
7929 hlinfo->mouse_face_hidden = true;
7930 }
7931
7932 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
7933 if (f == 0)
7934 {
7935 /* Scroll bars consume key events, but we want
7936 the keys to go to the scroll bar's frame. */
7937 Widget widget = XtWindowToWidget (dpyinfo->display,
7938 event->xkey.window);
7939 if (widget && XmIsScrollBar (widget))
7940 {
7941 widget = XtParent (widget);
7942 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
7943 }
7944 }
7945 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
7946
7947 if (f != 0)
7948 {
7949 KeySym keysym, orig_keysym;
7950 /* al%imercury@uunet.uu.net says that making this 81
7951 instead of 80 fixed a bug whereby meta chars made
7952 his Emacs hang.
7953
7954 It seems that some version of XmbLookupString has
7955 a bug of not returning XBufferOverflow in
7956 status_return even if the input is too long to
7957 fit in 81 bytes. So, we must prepare sufficient
7958 bytes for copy_buffer. 513 bytes (256 chars for
7959 two-byte character set) seems to be a fairly good
7960 approximation. -- 2000.8.10 handa@etl.go.jp */
7961 unsigned char copy_buffer[513];
7962 unsigned char *copy_bufptr = copy_buffer;
7963 int copy_bufsiz = sizeof (copy_buffer);
7964 int modifiers;
7965 Lisp_Object coding_system = Qlatin_1;
7966 Lisp_Object c;
7967 /* Event will be modified. */
7968 XKeyEvent xkey = event->xkey;
7969
7970 #ifdef USE_GTK
7971 /* Don't pass keys to GTK. A Tab will shift focus to the
7972 tool bar in GTK 2.4. Keys will still go to menus and
7973 dialogs because in that case popup_activated is nonzero
7974 (see above). */
7975 *finish = X_EVENT_DROP;
7976 #endif
7977
7978 xkey.state |= x_emacs_to_x_modifiers (FRAME_DISPLAY_INFO (f),
7979 extra_keyboard_modifiers);
7980 modifiers = xkey.state;
7981
7982 /* This will have to go some day... */
7983
7984 /* make_lispy_event turns chars into control chars.
7985 Don't do it here because XLookupString is too eager. */
7986 xkey.state &= ~ControlMask;
7987 xkey.state &= ~(dpyinfo->meta_mod_mask
7988 | dpyinfo->super_mod_mask
7989 | dpyinfo->hyper_mod_mask
7990 | dpyinfo->alt_mod_mask);
7991
7992 /* In case Meta is ComposeCharacter,
7993 clear its status. According to Markus Ehrnsperger
7994 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
7995 this enables ComposeCharacter to work whether or
7996 not it is combined with Meta. */
7997 if (modifiers & dpyinfo->meta_mod_mask)
7998 memset (&compose_status, 0, sizeof (compose_status));
7999
8000 #ifdef HAVE_X_I18N
8001 if (FRAME_XIC (f))
8002 {
8003 Status status_return;
8004
8005 coding_system = Vlocale_coding_system;
8006 nbytes = XmbLookupString (FRAME_XIC (f),
8007 &xkey, (char *) copy_bufptr,
8008 copy_bufsiz, &keysym,
8009 &status_return);
8010 if (status_return == XBufferOverflow)
8011 {
8012 copy_bufsiz = nbytes + 1;
8013 copy_bufptr = alloca (copy_bufsiz);
8014 nbytes = XmbLookupString (FRAME_XIC (f),
8015 &xkey, (char *) copy_bufptr,
8016 copy_bufsiz, &keysym,
8017 &status_return);
8018 }
8019 /* Xutf8LookupString is a new but already deprecated interface. -stef */
8020 if (status_return == XLookupNone)
8021 break;
8022 else if (status_return == XLookupChars)
8023 {
8024 keysym = NoSymbol;
8025 modifiers = 0;
8026 }
8027 else if (status_return != XLookupKeySym
8028 && status_return != XLookupBoth)
8029 emacs_abort ();
8030 }
8031 else
8032 nbytes = XLookupString (&xkey, (char *) copy_bufptr,
8033 copy_bufsiz, &keysym,
8034 &compose_status);
8035 #else
8036 nbytes = XLookupString (&xkey, (char *) copy_bufptr,
8037 copy_bufsiz, &keysym,
8038 &compose_status);
8039 #endif
8040
8041 /* If not using XIM/XIC, and a compose sequence is in progress,
8042 we break here. Otherwise, chars_matched is always 0. */
8043 if (compose_status.chars_matched > 0 && nbytes == 0)
8044 break;
8045
8046 memset (&compose_status, 0, sizeof (compose_status));
8047 orig_keysym = keysym;
8048
8049 /* Common for all keysym input events. */
8050 XSETFRAME (inev.ie.frame_or_window, f);
8051 inev.ie.modifiers
8052 = x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO (f), modifiers);
8053 inev.ie.timestamp = xkey.time;
8054
8055 /* First deal with keysyms which have defined
8056 translations to characters. */
8057 if (keysym >= 32 && keysym < 128)
8058 /* Avoid explicitly decoding each ASCII character. */
8059 {
8060 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
8061 inev.ie.code = keysym;
8062 goto done_keysym;
8063 }
8064
8065 /* Keysyms directly mapped to Unicode characters. */
8066 if (keysym >= 0x01000000 && keysym <= 0x0110FFFF)
8067 {
8068 if (keysym < 0x01000080)
8069 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
8070 else
8071 inev.ie.kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
8072 inev.ie.code = keysym & 0xFFFFFF;
8073 goto done_keysym;
8074 }
8075
8076 /* Now non-ASCII. */
8077 if (HASH_TABLE_P (Vx_keysym_table)
8078 && (c = Fgethash (make_number (keysym),
8079 Vx_keysym_table,
8080 Qnil),
8081 NATNUMP (c)))
8082 {
8083 inev.ie.kind = (SINGLE_BYTE_CHAR_P (XFASTINT (c))
8084 ? ASCII_KEYSTROKE_EVENT
8085 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
8086 inev.ie.code = XFASTINT (c);
8087 goto done_keysym;
8088 }
8089
8090 /* Random non-modifier sorts of keysyms. */
8091 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
8092 || keysym == XK_Delete
8093 #ifdef XK_ISO_Left_Tab
8094 || (keysym >= XK_ISO_Left_Tab
8095 && keysym <= XK_ISO_Enter)
8096 #endif
8097 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
8098 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
8099 #ifdef HPUX
8100 /* This recognizes the "extended function
8101 keys". It seems there's no cleaner way.
8102 Test IsModifierKey to avoid handling
8103 mode_switch incorrectly. */
8104 || (XK_Select <= keysym && keysym < XK_KP_Space)
8105 #endif
8106 #ifdef XK_dead_circumflex
8107 || orig_keysym == XK_dead_circumflex
8108 #endif
8109 #ifdef XK_dead_grave
8110 || orig_keysym == XK_dead_grave
8111 #endif
8112 #ifdef XK_dead_tilde
8113 || orig_keysym == XK_dead_tilde
8114 #endif
8115 #ifdef XK_dead_diaeresis
8116 || orig_keysym == XK_dead_diaeresis
8117 #endif
8118 #ifdef XK_dead_macron
8119 || orig_keysym == XK_dead_macron
8120 #endif
8121 #ifdef XK_dead_degree
8122 || orig_keysym == XK_dead_degree
8123 #endif
8124 #ifdef XK_dead_acute
8125 || orig_keysym == XK_dead_acute
8126 #endif
8127 #ifdef XK_dead_cedilla
8128 || orig_keysym == XK_dead_cedilla
8129 #endif
8130 #ifdef XK_dead_breve
8131 || orig_keysym == XK_dead_breve
8132 #endif
8133 #ifdef XK_dead_ogonek
8134 || orig_keysym == XK_dead_ogonek
8135 #endif
8136 #ifdef XK_dead_caron
8137 || orig_keysym == XK_dead_caron
8138 #endif
8139 #ifdef XK_dead_doubleacute
8140 || orig_keysym == XK_dead_doubleacute
8141 #endif
8142 #ifdef XK_dead_abovedot
8143 || orig_keysym == XK_dead_abovedot
8144 #endif
8145 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
8146 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
8147 /* Any "vendor-specific" key is ok. */
8148 || (orig_keysym & (1 << 28))
8149 || (keysym != NoSymbol && nbytes == 0))
8150 && ! (IsModifierKey (orig_keysym)
8151 /* The symbols from XK_ISO_Lock
8152 to XK_ISO_Last_Group_Lock
8153 don't have real modifiers but
8154 should be treated similarly to
8155 Mode_switch by Emacs. */
8156 #if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
8157 || (XK_ISO_Lock <= orig_keysym
8158 && orig_keysym <= XK_ISO_Last_Group_Lock)
8159 #endif
8160 ))
8161 {
8162 STORE_KEYSYM_FOR_DEBUG (keysym);
8163 /* make_lispy_event will convert this to a symbolic
8164 key. */
8165 inev.ie.kind = NON_ASCII_KEYSTROKE_EVENT;
8166 inev.ie.code = keysym;
8167 goto done_keysym;
8168 }
8169
8170 { /* Raw bytes, not keysym. */
8171 ptrdiff_t i;
8172 int nchars, len;
8173
8174 for (i = 0, nchars = 0; i < nbytes; i++)
8175 {
8176 if (ASCII_CHAR_P (copy_bufptr[i]))
8177 nchars++;
8178 STORE_KEYSYM_FOR_DEBUG (copy_bufptr[i]);
8179 }
8180
8181 if (nchars < nbytes)
8182 {
8183 /* Decode the input data. */
8184
8185 /* The input should be decoded with `coding_system'
8186 which depends on which X*LookupString function
8187 we used just above and the locale. */
8188 setup_coding_system (coding_system, &coding);
8189 coding.src_multibyte = false;
8190 coding.dst_multibyte = true;
8191 /* The input is converted to events, thus we can't
8192 handle composition. Anyway, there's no XIM that
8193 gives us composition information. */
8194 coding.common_flags &= ~CODING_ANNOTATION_MASK;
8195
8196 SAFE_NALLOCA (coding.destination, MAX_MULTIBYTE_LENGTH,
8197 nbytes);
8198 coding.dst_bytes = MAX_MULTIBYTE_LENGTH * nbytes;
8199 coding.mode |= CODING_MODE_LAST_BLOCK;
8200 decode_coding_c_string (&coding, copy_bufptr, nbytes, Qnil);
8201 nbytes = coding.produced;
8202 nchars = coding.produced_char;
8203 copy_bufptr = coding.destination;
8204 }
8205
8206 /* Convert the input data to a sequence of
8207 character events. */
8208 for (i = 0; i < nbytes; i += len)
8209 {
8210 int ch;
8211 if (nchars == nbytes)
8212 ch = copy_bufptr[i], len = 1;
8213 else
8214 ch = STRING_CHAR_AND_LENGTH (copy_bufptr + i, len);
8215 inev.ie.kind = (SINGLE_BYTE_CHAR_P (ch)
8216 ? ASCII_KEYSTROKE_EVENT
8217 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
8218 inev.ie.code = ch;
8219 kbd_buffer_store_buffered_event (&inev, hold_quit);
8220 }
8221
8222 count += nchars;
8223
8224 inev.ie.kind = NO_EVENT; /* Already stored above. */
8225
8226 if (keysym == NoSymbol)
8227 break;
8228 }
8229 /* FIXME: check side effects and remove this. */
8230 ((XEvent *) event)->xkey = xkey;
8231 }
8232 done_keysym:
8233 #ifdef HAVE_X_I18N
8234 /* Don't dispatch this event since XtDispatchEvent calls
8235 XFilterEvent, and two calls in a row may freeze the
8236 client. */
8237 break;
8238 #else
8239 goto OTHER;
8240 #endif
8241
8242 case KeyRelease:
8243 x_display_set_last_user_time (dpyinfo, event->xkey.time);
8244 #ifdef HAVE_X_I18N
8245 /* Don't dispatch this event since XtDispatchEvent calls
8246 XFilterEvent, and two calls in a row may freeze the
8247 client. */
8248 break;
8249 #else
8250 goto OTHER;
8251 #endif
8252
8253 case EnterNotify:
8254 x_display_set_last_user_time (dpyinfo, event->xcrossing.time);
8255 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
8256
8257 f = any;
8258
8259 if (f && x_mouse_click_focus_ignore_position)
8260 ignore_next_mouse_click_timeout = event->xmotion.time + 200;
8261
8262 /* EnterNotify counts as mouse movement,
8263 so update things that depend on mouse position. */
8264 if (f && !f->output_data.x->hourglass_p)
8265 note_mouse_movement (f, &event->xmotion);
8266 #ifdef USE_GTK
8267 /* We may get an EnterNotify on the buttons in the toolbar. In that
8268 case we moved out of any highlighted area and need to note this. */
8269 if (!f && dpyinfo->last_mouse_glyph_frame)
8270 note_mouse_movement (dpyinfo->last_mouse_glyph_frame, &event->xmotion);
8271 #endif
8272 goto OTHER;
8273
8274 case FocusIn:
8275 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
8276 goto OTHER;
8277
8278 case LeaveNotify:
8279 x_display_set_last_user_time (dpyinfo, event->xcrossing.time);
8280 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
8281
8282 f = x_top_window_to_frame (dpyinfo, event->xcrossing.window);
8283 if (f)
8284 {
8285 if (f == hlinfo->mouse_face_mouse_frame)
8286 {
8287 /* If we move outside the frame, then we're
8288 certainly no longer on any text in the frame. */
8289 clear_mouse_face (hlinfo);
8290 hlinfo->mouse_face_mouse_frame = 0;
8291 }
8292
8293 /* Generate a nil HELP_EVENT to cancel a help-echo.
8294 Do it only if there's something to cancel.
8295 Otherwise, the startup message is cleared when
8296 the mouse leaves the frame. */
8297 if (any_help_event_p)
8298 do_help = -1;
8299 }
8300 #ifdef USE_GTK
8301 /* See comment in EnterNotify above */
8302 else if (dpyinfo->last_mouse_glyph_frame)
8303 note_mouse_movement (dpyinfo->last_mouse_glyph_frame, &event->xmotion);
8304 #endif
8305 goto OTHER;
8306
8307 case FocusOut:
8308 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
8309 goto OTHER;
8310
8311 case MotionNotify:
8312 {
8313 x_display_set_last_user_time (dpyinfo, event->xmotion.time);
8314 previous_help_echo_string = help_echo_string;
8315 help_echo_string = Qnil;
8316
8317 f = (x_mouse_grabbed (dpyinfo) ? dpyinfo->last_mouse_frame
8318 : x_window_to_frame (dpyinfo, event->xmotion.window));
8319
8320 if (hlinfo->mouse_face_hidden)
8321 {
8322 hlinfo->mouse_face_hidden = false;
8323 clear_mouse_face (hlinfo);
8324 }
8325
8326 #ifdef USE_GTK
8327 if (f && xg_event_is_for_scrollbar (f, event))
8328 f = 0;
8329 #endif
8330 if (f)
8331 {
8332
8333 /* Generate SELECT_WINDOW_EVENTs when needed.
8334 Don't let popup menus influence things (bug#1261). */
8335 if (!NILP (Vmouse_autoselect_window) && !popup_activated ())
8336 {
8337 static Lisp_Object last_mouse_window;
8338 Lisp_Object window = window_from_coordinates
8339 (f, event->xmotion.x, event->xmotion.y, 0, false);
8340
8341 /* Window will be selected only when it is not selected now and
8342 last mouse movement event was not in it. Minibuffer window
8343 will be selected only when it is active. */
8344 if (WINDOWP (window)
8345 && !EQ (window, last_mouse_window)
8346 && !EQ (window, selected_window)
8347 /* For click-to-focus window managers
8348 create event iff we don't leave the
8349 selected frame. */
8350 && (focus_follows_mouse
8351 || (EQ (XWINDOW (window)->frame,
8352 XWINDOW (selected_window)->frame))))
8353 {
8354 inev.ie.kind = SELECT_WINDOW_EVENT;
8355 inev.ie.frame_or_window = window;
8356 }
8357 /* Remember the last window where we saw the mouse. */
8358 last_mouse_window = window;
8359 }
8360 if (!note_mouse_movement (f, &event->xmotion))
8361 help_echo_string = previous_help_echo_string;
8362 }
8363 else
8364 {
8365 #ifndef USE_TOOLKIT_SCROLL_BARS
8366 struct scroll_bar *bar
8367 = x_window_to_scroll_bar (event->xmotion.display,
8368 event->xmotion.window, 2);
8369
8370 if (bar)
8371 x_scroll_bar_note_movement (bar, &event->xmotion);
8372 #endif /* USE_TOOLKIT_SCROLL_BARS */
8373
8374 /* If we move outside the frame, then we're
8375 certainly no longer on any text in the frame. */
8376 clear_mouse_face (hlinfo);
8377 }
8378
8379 /* If the contents of the global variable help_echo_string
8380 has changed, generate a HELP_EVENT. */
8381 if (!NILP (help_echo_string)
8382 || !NILP (previous_help_echo_string))
8383 do_help = 1;
8384 goto OTHER;
8385 }
8386
8387 case ConfigureNotify:
8388 /* An opaque move can generate a stream of events as the window
8389 is dragged around. If the connection round trip time isn't
8390 really short, they may come faster than we can respond to
8391 them, given the multiple queries we can do to check window
8392 manager state, translate coordinates, etc.
8393
8394 So if this ConfigureNotify is immediately followed by another
8395 for the same window, use the info from the latest update, and
8396 consider the events all handled. */
8397 /* Opaque resize may be trickier; ConfigureNotify events are
8398 mixed with Expose events for multiple windows. */
8399 configureEvent = *event;
8400 while (XPending (dpyinfo->display))
8401 {
8402 XNextEvent (dpyinfo->display, &next_event);
8403 if (next_event.type != ConfigureNotify
8404 || next_event.xconfigure.window != event->xconfigure.window
8405 /* Skipping events with different sizes can lead to a
8406 mispositioned mode line at initial window creation.
8407 Only drop window motion events for now. */
8408 || next_event.xconfigure.width != event->xconfigure.width
8409 || next_event.xconfigure.height != event->xconfigure.height)
8410 {
8411 XPutBackEvent (dpyinfo->display, &next_event);
8412 break;
8413 }
8414 else
8415 configureEvent = next_event;
8416 }
8417 f = x_top_window_to_frame (dpyinfo, configureEvent.xconfigure.window);
8418 #ifdef USE_CAIRO
8419 if (f) x_cr_destroy_surface (f);
8420 #endif
8421 #ifdef USE_GTK
8422 if (!f
8423 && (f = any)
8424 && configureEvent.xconfigure.window == FRAME_X_WINDOW (f))
8425 {
8426 xg_frame_resized (f, configureEvent.xconfigure.width,
8427 configureEvent.xconfigure.height);
8428 #ifdef USE_CAIRO
8429 x_cr_destroy_surface (f);
8430 #endif
8431 f = 0;
8432 }
8433 #endif
8434 if (f)
8435 {
8436 x_net_wm_state (f, configureEvent.xconfigure.window);
8437
8438 #ifdef USE_X_TOOLKIT
8439 /* Tip frames are pure X window, set size for them. */
8440 if (! NILP (tip_frame) && XFRAME (tip_frame) == f)
8441 {
8442 if (FRAME_PIXEL_HEIGHT (f) != configureEvent.xconfigure.height
8443 || FRAME_PIXEL_WIDTH (f) != configureEvent.xconfigure.width)
8444 SET_FRAME_GARBAGED (f);
8445 FRAME_PIXEL_HEIGHT (f) = configureEvent.xconfigure.height;
8446 FRAME_PIXEL_WIDTH (f) = configureEvent.xconfigure.width;
8447 }
8448 #endif
8449
8450 #ifndef USE_X_TOOLKIT
8451 #ifndef USE_GTK
8452 int width =
8453 FRAME_PIXEL_TO_TEXT_WIDTH (f, configureEvent.xconfigure.width);
8454 int height =
8455 FRAME_PIXEL_TO_TEXT_HEIGHT (f, configureEvent.xconfigure.height);
8456
8457 /* In the toolkit version, change_frame_size
8458 is called by the code that handles resizing
8459 of the EmacsFrame widget. */
8460
8461 /* Even if the number of character rows and columns has
8462 not changed, the font size may have changed, so we need
8463 to check the pixel dimensions as well. */
8464 if (width != FRAME_TEXT_WIDTH (f)
8465 || height != FRAME_TEXT_HEIGHT (f)
8466 || configureEvent.xconfigure.width != FRAME_PIXEL_WIDTH (f)
8467 || configureEvent.xconfigure.height != FRAME_PIXEL_HEIGHT (f))
8468 {
8469 change_frame_size (f, width, height, false, true, false, true);
8470 x_clear_under_internal_border (f);
8471 SET_FRAME_GARBAGED (f);
8472 cancel_mouse_face (f);
8473 }
8474 #endif /* not USE_GTK */
8475 #endif
8476
8477 #ifdef USE_GTK
8478 /* GTK creates windows but doesn't map them.
8479 Only get real positions when mapped. */
8480 if (FRAME_GTK_OUTER_WIDGET (f)
8481 && gtk_widget_get_mapped (FRAME_GTK_OUTER_WIDGET (f)))
8482 #endif
8483 x_real_positions (f, &f->left_pos, &f->top_pos);
8484
8485 #ifdef HAVE_X_I18N
8486 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
8487 xic_set_statusarea (f);
8488 #endif
8489
8490 }
8491 goto OTHER;
8492
8493 case ButtonRelease:
8494 case ButtonPress:
8495 {
8496 /* If we decide we want to generate an event to be seen
8497 by the rest of Emacs, we put it here. */
8498 bool tool_bar_p = false;
8499
8500 memset (&compose_status, 0, sizeof (compose_status));
8501 dpyinfo->last_mouse_glyph_frame = NULL;
8502 x_display_set_last_user_time (dpyinfo, event->xbutton.time);
8503
8504 f = (x_mouse_grabbed (dpyinfo) ? dpyinfo->last_mouse_frame
8505 : x_window_to_frame (dpyinfo, event->xbutton.window));
8506
8507 #ifdef USE_GTK
8508 if (f && xg_event_is_for_scrollbar (f, event))
8509 f = 0;
8510 #endif
8511 if (f)
8512 {
8513 #if ! defined (USE_GTK)
8514 /* Is this in the tool-bar? */
8515 if (WINDOWP (f->tool_bar_window)
8516 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
8517 {
8518 Lisp_Object window;
8519 int x = event->xbutton.x;
8520 int y = event->xbutton.y;
8521
8522 window = window_from_coordinates (f, x, y, 0, true);
8523 tool_bar_p = EQ (window, f->tool_bar_window);
8524
8525 if (tool_bar_p && event->xbutton.button < 4)
8526 handle_tool_bar_click
8527 (f, x, y, event->xbutton.type == ButtonPress,
8528 x_x_to_emacs_modifiers (dpyinfo, event->xbutton.state));
8529 }
8530 #endif /* !USE_GTK */
8531
8532 if (!tool_bar_p)
8533 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
8534 if (! popup_activated ())
8535 #endif
8536 {
8537 if (ignore_next_mouse_click_timeout)
8538 {
8539 if (event->type == ButtonPress
8540 && event->xbutton.time > ignore_next_mouse_click_timeout)
8541 {
8542 ignore_next_mouse_click_timeout = 0;
8543 construct_mouse_click (&inev.ie, &event->xbutton, f);
8544 }
8545 if (event->type == ButtonRelease)
8546 ignore_next_mouse_click_timeout = 0;
8547 }
8548 else
8549 construct_mouse_click (&inev.ie, &event->xbutton, f);
8550 }
8551 if (FRAME_X_EMBEDDED_P (f))
8552 xembed_send_message (f, event->xbutton.time,
8553 XEMBED_REQUEST_FOCUS, 0, 0, 0);
8554 }
8555 else
8556 {
8557 struct scroll_bar *bar
8558 = x_window_to_scroll_bar (event->xbutton.display,
8559 event->xbutton.window, 2);
8560
8561 #ifdef USE_TOOLKIT_SCROLL_BARS
8562 /* Make the "Ctrl-Mouse-2 splits window" work for toolkit
8563 scroll bars. */
8564 if (bar && event->xbutton.state & ControlMask)
8565 {
8566 x_scroll_bar_handle_click (bar, event, &inev.ie);
8567 *finish = X_EVENT_DROP;
8568 }
8569 #else /* not USE_TOOLKIT_SCROLL_BARS */
8570 if (bar)
8571 x_scroll_bar_handle_click (bar, event, &inev.ie);
8572 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8573 }
8574
8575 if (event->type == ButtonPress)
8576 {
8577 dpyinfo->grabbed |= (1 << event->xbutton.button);
8578 dpyinfo->last_mouse_frame = f;
8579 #if ! defined (USE_GTK)
8580 if (f && !tool_bar_p)
8581 f->last_tool_bar_item = -1;
8582 #endif /* not USE_GTK */
8583 }
8584 else
8585 dpyinfo->grabbed &= ~(1 << event->xbutton.button);
8586
8587 /* Ignore any mouse motion that happened before this event;
8588 any subsequent mouse-movement Emacs events should reflect
8589 only motion after the ButtonPress/Release. */
8590 if (f != 0)
8591 f->mouse_moved = false;
8592
8593 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
8594 f = x_menubar_window_to_frame (dpyinfo, event);
8595 /* For a down-event in the menu bar,
8596 don't pass it to Xt right now.
8597 Instead, save it away
8598 and we will pass it to Xt from kbd_buffer_get_event.
8599 That way, we can run some Lisp code first. */
8600 if (! popup_activated ()
8601 #ifdef USE_GTK
8602 /* Gtk+ menus only react to the first three buttons. */
8603 && event->xbutton.button < 3
8604 #endif
8605 && f && event->type == ButtonPress
8606 /* Verify the event is really within the menu bar
8607 and not just sent to it due to grabbing. */
8608 && event->xbutton.x >= 0
8609 && event->xbutton.x < FRAME_PIXEL_WIDTH (f)
8610 && event->xbutton.y >= 0
8611 && event->xbutton.y < FRAME_MENUBAR_HEIGHT (f)
8612 && event->xbutton.same_screen)
8613 {
8614 if (!f->output_data.x->saved_menu_event)
8615 f->output_data.x->saved_menu_event = xmalloc (sizeof *event);
8616 *f->output_data.x->saved_menu_event = *event;
8617 inev.ie.kind = MENU_BAR_ACTIVATE_EVENT;
8618 XSETFRAME (inev.ie.frame_or_window, f);
8619 *finish = X_EVENT_DROP;
8620 }
8621 else
8622 goto OTHER;
8623 #endif /* USE_X_TOOLKIT || USE_GTK */
8624 }
8625 break;
8626
8627 case CirculateNotify:
8628 goto OTHER;
8629
8630 case CirculateRequest:
8631 goto OTHER;
8632
8633 case VisibilityNotify:
8634 goto OTHER;
8635
8636 case MappingNotify:
8637 /* Someone has changed the keyboard mapping - update the
8638 local cache. */
8639 switch (event->xmapping.request)
8640 {
8641 case MappingModifier:
8642 x_find_modifier_meanings (dpyinfo);
8643 /* This is meant to fall through. */
8644 case MappingKeyboard:
8645 XRefreshKeyboardMapping ((XMappingEvent *) &event->xmapping);
8646 }
8647 goto OTHER;
8648
8649 case DestroyNotify:
8650 xft_settings_event (dpyinfo, event);
8651 break;
8652
8653 default:
8654 OTHER:
8655 #ifdef USE_X_TOOLKIT
8656 block_input ();
8657 if (*finish != X_EVENT_DROP)
8658 XtDispatchEvent ((XEvent *) event);
8659 unblock_input ();
8660 #endif /* USE_X_TOOLKIT */
8661 break;
8662 }
8663
8664 done:
8665 if (inev.ie.kind != NO_EVENT)
8666 {
8667 kbd_buffer_store_buffered_event (&inev, hold_quit);
8668 count++;
8669 }
8670
8671 if (do_help
8672 && !(hold_quit && hold_quit->kind != NO_EVENT))
8673 {
8674 Lisp_Object frame;
8675
8676 if (f)
8677 XSETFRAME (frame, f);
8678 else
8679 frame = Qnil;
8680
8681 if (do_help > 0)
8682 {
8683 any_help_event_p = true;
8684 gen_help_event (help_echo_string, frame, help_echo_window,
8685 help_echo_object, help_echo_pos);
8686 }
8687 else
8688 {
8689 help_echo_string = Qnil;
8690 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
8691 }
8692 count++;
8693 }
8694
8695 SAFE_FREE ();
8696 return count;
8697 }
8698
8699 /* Handles the XEvent EVENT on display DISPLAY.
8700 This is used for event loops outside the normal event handling,
8701 i.e. looping while a popup menu or a dialog is posted.
8702
8703 Returns the value handle_one_xevent sets in the finish argument. */
8704 int
8705 x_dispatch_event (XEvent *event, Display *display)
8706 {
8707 struct x_display_info *dpyinfo;
8708 int finish = X_EVENT_NORMAL;
8709
8710 dpyinfo = x_display_info_for_display (display);
8711
8712 if (dpyinfo)
8713 handle_one_xevent (dpyinfo, event, &finish, 0);
8714
8715 return finish;
8716 }
8717
8718 /* Read events coming from the X server.
8719 Return as soon as there are no more events to be read.
8720
8721 Return the number of characters stored into the buffer,
8722 thus pretending to be `read' (except the characters we store
8723 in the keyboard buffer can be multibyte, so are not necessarily
8724 C chars). */
8725
8726 static int
8727 XTread_socket (struct terminal *terminal, struct input_event *hold_quit)
8728 {
8729 int count = 0;
8730 bool event_found = false;
8731 struct x_display_info *dpyinfo = terminal->display_info.x;
8732
8733 block_input ();
8734
8735 /* For debugging, this gives a way to fake an I/O error. */
8736 if (dpyinfo == XTread_socket_fake_io_error)
8737 {
8738 XTread_socket_fake_io_error = 0;
8739 x_io_error_quitter (dpyinfo->display);
8740 }
8741
8742 #ifndef USE_GTK
8743 while (XPending (dpyinfo->display))
8744 {
8745 int finish;
8746 XEvent event;
8747
8748 XNextEvent (dpyinfo->display, &event);
8749
8750 #ifdef HAVE_X_I18N
8751 /* Filter events for the current X input method. */
8752 if (x_filter_event (dpyinfo, &event))
8753 continue;
8754 #endif
8755 event_found = true;
8756
8757 count += handle_one_xevent (dpyinfo, &event, &finish, hold_quit);
8758
8759 if (finish == X_EVENT_GOTO_OUT)
8760 break;
8761 }
8762
8763 #else /* USE_GTK */
8764
8765 /* For GTK we must use the GTK event loop. But XEvents gets passed
8766 to our filter function above, and then to the big event switch.
8767 We use a bunch of globals to communicate with our filter function,
8768 that is kind of ugly, but it works.
8769
8770 There is no way to do one display at the time, GTK just does events
8771 from all displays. */
8772
8773 while (gtk_events_pending ())
8774 {
8775 current_count = count;
8776 current_hold_quit = hold_quit;
8777
8778 gtk_main_iteration ();
8779
8780 count = current_count;
8781 current_count = -1;
8782 current_hold_quit = 0;
8783
8784 if (current_finish == X_EVENT_GOTO_OUT)
8785 break;
8786 }
8787 #endif /* USE_GTK */
8788
8789 /* On some systems, an X bug causes Emacs to get no more events
8790 when the window is destroyed. Detect that. (1994.) */
8791 if (! event_found)
8792 {
8793 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
8794 One XNOOP in 100 loops will make Emacs terminate.
8795 B. Bretthauer, 1994 */
8796 x_noop_count++;
8797 if (x_noop_count >= 100)
8798 {
8799 x_noop_count=0;
8800
8801 if (next_noop_dpyinfo == 0)
8802 next_noop_dpyinfo = x_display_list;
8803
8804 XNoOp (next_noop_dpyinfo->display);
8805
8806 /* Each time we get here, cycle through the displays now open. */
8807 next_noop_dpyinfo = next_noop_dpyinfo->next;
8808 }
8809 }
8810
8811 /* If the focus was just given to an auto-raising frame,
8812 raise it now. FIXME: handle more than one such frame. */
8813 if (dpyinfo->x_pending_autoraise_frame)
8814 {
8815 x_raise_frame (dpyinfo->x_pending_autoraise_frame);
8816 dpyinfo->x_pending_autoraise_frame = NULL;
8817 }
8818
8819 unblock_input ();
8820
8821 return count;
8822 }
8823
8824
8825
8826 \f
8827 /***********************************************************************
8828 Text Cursor
8829 ***********************************************************************/
8830
8831 /* Set clipping for output in glyph row ROW. W is the window in which
8832 we operate. GC is the graphics context to set clipping in.
8833
8834 ROW may be a text row or, e.g., a mode line. Text rows must be
8835 clipped to the interior of the window dedicated to text display,
8836 mode lines must be clipped to the whole window. */
8837
8838 static void
8839 x_clip_to_row (struct window *w, struct glyph_row *row,
8840 enum glyph_row_area area, GC gc)
8841 {
8842 struct frame *f = XFRAME (WINDOW_FRAME (w));
8843 XRectangle clip_rect;
8844 int window_x, window_y, window_width;
8845
8846 window_box (w, area, &window_x, &window_y, &window_width, 0);
8847
8848 clip_rect.x = window_x;
8849 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, row->y));
8850 clip_rect.y = max (clip_rect.y, window_y);
8851 clip_rect.width = window_width;
8852 clip_rect.height = row->visible_height;
8853
8854 x_set_clip_rectangles (f, gc, &clip_rect, 1);
8855 }
8856
8857
8858 /* Draw a hollow box cursor on window W in glyph row ROW. */
8859
8860 static void
8861 x_draw_hollow_cursor (struct window *w, struct glyph_row *row)
8862 {
8863 struct frame *f = XFRAME (WINDOW_FRAME (w));
8864 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
8865 Display *dpy = FRAME_X_DISPLAY (f);
8866 int x, y, wd, h;
8867 XGCValues xgcv;
8868 struct glyph *cursor_glyph;
8869 GC gc;
8870
8871 /* Get the glyph the cursor is on. If we can't tell because
8872 the current matrix is invalid or such, give up. */
8873 cursor_glyph = get_phys_cursor_glyph (w);
8874 if (cursor_glyph == NULL)
8875 return;
8876
8877 /* Compute frame-relative coordinates for phys cursor. */
8878 get_phys_cursor_geometry (w, row, cursor_glyph, &x, &y, &h);
8879 wd = w->phys_cursor_width - 1;
8880
8881 /* The foreground of cursor_gc is typically the same as the normal
8882 background color, which can cause the cursor box to be invisible. */
8883 xgcv.foreground = f->output_data.x->cursor_pixel;
8884 if (dpyinfo->scratch_cursor_gc)
8885 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
8886 else
8887 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
8888 GCForeground, &xgcv);
8889 gc = dpyinfo->scratch_cursor_gc;
8890
8891 /* When on R2L character, show cursor at the right edge of the
8892 glyph, unless the cursor box is as wide as the glyph or wider
8893 (the latter happens when x-stretch-cursor is non-nil). */
8894 if ((cursor_glyph->resolved_level & 1) != 0
8895 && cursor_glyph->pixel_width > wd)
8896 {
8897 x += cursor_glyph->pixel_width - wd;
8898 if (wd > 0)
8899 wd -= 1;
8900 }
8901 /* Set clipping, draw the rectangle, and reset clipping again. */
8902 x_clip_to_row (w, row, TEXT_AREA, gc);
8903 x_draw_rectangle (f, gc, x, y, wd, h - 1);
8904 x_reset_clip_rectangles (f, gc);
8905 }
8906
8907
8908 /* Draw a bar cursor on window W in glyph row ROW.
8909
8910 Implementation note: One would like to draw a bar cursor with an
8911 angle equal to the one given by the font property XA_ITALIC_ANGLE.
8912 Unfortunately, I didn't find a font yet that has this property set.
8913 --gerd. */
8914
8915 static void
8916 x_draw_bar_cursor (struct window *w, struct glyph_row *row, int width, enum text_cursor_kinds kind)
8917 {
8918 struct frame *f = XFRAME (w->frame);
8919 struct glyph *cursor_glyph;
8920
8921 /* If cursor is out of bounds, don't draw garbage. This can happen
8922 in mini-buffer windows when switching between echo area glyphs
8923 and mini-buffer. */
8924 cursor_glyph = get_phys_cursor_glyph (w);
8925 if (cursor_glyph == NULL)
8926 return;
8927
8928 /* Experimental avoidance of cursor on xwidget. */
8929 if (cursor_glyph->type == XWIDGET_GLYPH)
8930 return;
8931
8932 /* If on an image, draw like a normal cursor. That's usually better
8933 visible than drawing a bar, esp. if the image is large so that
8934 the bar might not be in the window. */
8935 if (cursor_glyph->type == IMAGE_GLYPH)
8936 {
8937 struct glyph_row *r;
8938 r = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
8939 draw_phys_cursor_glyph (w, r, DRAW_CURSOR);
8940 }
8941 else
8942 {
8943 Display *dpy = FRAME_X_DISPLAY (f);
8944 Window window = FRAME_X_WINDOW (f);
8945 GC gc = FRAME_DISPLAY_INFO (f)->scratch_cursor_gc;
8946 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
8947 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
8948 XGCValues xgcv;
8949
8950 /* If the glyph's background equals the color we normally draw
8951 the bars cursor in, the bar cursor in its normal color is
8952 invisible. Use the glyph's foreground color instead in this
8953 case, on the assumption that the glyph's colors are chosen so
8954 that the glyph is legible. */
8955 if (face->background == f->output_data.x->cursor_pixel)
8956 xgcv.background = xgcv.foreground = face->foreground;
8957 else
8958 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
8959 xgcv.graphics_exposures = False;
8960
8961 if (gc)
8962 XChangeGC (dpy, gc, mask, &xgcv);
8963 else
8964 {
8965 gc = XCreateGC (dpy, window, mask, &xgcv);
8966 FRAME_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
8967 }
8968
8969 x_clip_to_row (w, row, TEXT_AREA, gc);
8970
8971 if (kind == BAR_CURSOR)
8972 {
8973 int x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
8974
8975 if (width < 0)
8976 width = FRAME_CURSOR_WIDTH (f);
8977 width = min (cursor_glyph->pixel_width, width);
8978
8979 w->phys_cursor_width = width;
8980
8981 /* If the character under cursor is R2L, draw the bar cursor
8982 on the right of its glyph, rather than on the left. */
8983 if ((cursor_glyph->resolved_level & 1) != 0)
8984 x += cursor_glyph->pixel_width - width;
8985
8986 x_fill_rectangle (f, gc, x,
8987 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
8988 width, row->height);
8989 }
8990 else /* HBAR_CURSOR */
8991 {
8992 int dummy_x, dummy_y, dummy_h;
8993 int x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
8994
8995 if (width < 0)
8996 width = row->height;
8997
8998 width = min (row->height, width);
8999
9000 get_phys_cursor_geometry (w, row, cursor_glyph, &dummy_x,
9001 &dummy_y, &dummy_h);
9002
9003 if ((cursor_glyph->resolved_level & 1) != 0
9004 && cursor_glyph->pixel_width > w->phys_cursor_width - 1)
9005 x += cursor_glyph->pixel_width - w->phys_cursor_width + 1;
9006 x_fill_rectangle (f, gc, x,
9007 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
9008 row->height - width),
9009 w->phys_cursor_width - 1, width);
9010 }
9011
9012 x_reset_clip_rectangles (f, gc);
9013 }
9014 }
9015
9016
9017 /* RIF: Define cursor CURSOR on frame F. */
9018
9019 static void
9020 x_define_frame_cursor (struct frame *f, Cursor cursor)
9021 {
9022 if (!f->pointer_invisible
9023 && f->output_data.x->current_cursor != cursor)
9024 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
9025 f->output_data.x->current_cursor = cursor;
9026 }
9027
9028
9029 /* RIF: Clear area on frame F. */
9030
9031 static void
9032 x_clear_frame_area (struct frame *f, int x, int y, int width, int height)
9033 {
9034 x_clear_area (f, x, y, width, height);
9035 #ifdef USE_GTK
9036 /* Must queue a redraw, because scroll bars might have been cleared. */
9037 if (FRAME_GTK_WIDGET (f))
9038 gtk_widget_queue_draw (FRAME_GTK_WIDGET (f));
9039 #endif
9040 }
9041
9042
9043 /* RIF: Draw cursor on window W. */
9044
9045 static void
9046 x_draw_window_cursor (struct window *w, struct glyph_row *glyph_row, int x,
9047 int y, enum text_cursor_kinds cursor_type,
9048 int cursor_width, bool on_p, bool active_p)
9049 {
9050 struct frame *f = XFRAME (WINDOW_FRAME (w));
9051
9052 if (on_p)
9053 {
9054 w->phys_cursor_type = cursor_type;
9055 w->phys_cursor_on_p = true;
9056
9057 if (glyph_row->exact_window_width_line_p
9058 && (glyph_row->reversed_p
9059 ? (w->phys_cursor.hpos < 0)
9060 : (w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])))
9061 {
9062 glyph_row->cursor_in_fringe_p = true;
9063 draw_fringe_bitmap (w, glyph_row, glyph_row->reversed_p);
9064 }
9065 else
9066 {
9067 switch (cursor_type)
9068 {
9069 case HOLLOW_BOX_CURSOR:
9070 x_draw_hollow_cursor (w, glyph_row);
9071 break;
9072
9073 case FILLED_BOX_CURSOR:
9074 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
9075 break;
9076
9077 case BAR_CURSOR:
9078 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
9079 break;
9080
9081 case HBAR_CURSOR:
9082 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
9083 break;
9084
9085 case NO_CURSOR:
9086 w->phys_cursor_width = 0;
9087 break;
9088
9089 default:
9090 emacs_abort ();
9091 }
9092 }
9093
9094 #ifdef HAVE_X_I18N
9095 if (w == XWINDOW (f->selected_window))
9096 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
9097 xic_set_preeditarea (w, x, y);
9098 #endif
9099 }
9100
9101 XFlush (FRAME_X_DISPLAY (f));
9102 }
9103
9104 \f
9105 /* Icons. */
9106
9107 /* Make the x-window of frame F use the gnu icon bitmap. */
9108
9109 bool
9110 x_bitmap_icon (struct frame *f, Lisp_Object file)
9111 {
9112 ptrdiff_t bitmap_id;
9113
9114 if (FRAME_X_WINDOW (f) == 0)
9115 return true;
9116
9117 /* Free up our existing icon bitmap and mask if any. */
9118 if (f->output_data.x->icon_bitmap > 0)
9119 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
9120 f->output_data.x->icon_bitmap = 0;
9121
9122 if (STRINGP (file))
9123 {
9124 #ifdef USE_GTK
9125 /* Use gtk_window_set_icon_from_file () if available,
9126 It's not restricted to bitmaps */
9127 if (xg_set_icon (f, file))
9128 return false;
9129 #endif /* USE_GTK */
9130 bitmap_id = x_create_bitmap_from_file (f, file);
9131 x_create_bitmap_mask (f, bitmap_id);
9132 }
9133 else
9134 {
9135 /* Create the GNU bitmap and mask if necessary. */
9136 if (FRAME_DISPLAY_INFO (f)->icon_bitmap_id < 0)
9137 {
9138 ptrdiff_t rc = -1;
9139
9140 #ifdef USE_GTK
9141
9142 if (xg_set_icon (f, xg_default_icon_file)
9143 || xg_set_icon_from_xpm_data (f, gnu_xpm_bits))
9144 {
9145 FRAME_DISPLAY_INFO (f)->icon_bitmap_id = -2;
9146 return false;
9147 }
9148
9149 #elif defined (HAVE_XPM) && defined (HAVE_X_WINDOWS)
9150
9151 rc = x_create_bitmap_from_xpm_data (f, gnu_xpm_bits);
9152 if (rc != -1)
9153 FRAME_DISPLAY_INFO (f)->icon_bitmap_id = rc;
9154
9155 #endif
9156
9157 /* If all else fails, use the (black and white) xbm image. */
9158 if (rc == -1)
9159 {
9160 rc = x_create_bitmap_from_data (f, (char *) gnu_xbm_bits,
9161 gnu_xbm_width, gnu_xbm_height);
9162 if (rc == -1)
9163 return true;
9164
9165 FRAME_DISPLAY_INFO (f)->icon_bitmap_id = rc;
9166 x_create_bitmap_mask (f, FRAME_DISPLAY_INFO (f)->icon_bitmap_id);
9167 }
9168 }
9169
9170 /* The first time we create the GNU bitmap and mask,
9171 this increments the ref-count one extra time.
9172 As a result, the GNU bitmap and mask are never freed.
9173 That way, we don't have to worry about allocating it again. */
9174 x_reference_bitmap (f, FRAME_DISPLAY_INFO (f)->icon_bitmap_id);
9175
9176 bitmap_id = FRAME_DISPLAY_INFO (f)->icon_bitmap_id;
9177 }
9178
9179 x_wm_set_icon_pixmap (f, bitmap_id);
9180 f->output_data.x->icon_bitmap = bitmap_id;
9181
9182 return false;
9183 }
9184
9185
9186 /* Make the x-window of frame F use a rectangle with text.
9187 Use ICON_NAME as the text. */
9188
9189 bool
9190 x_text_icon (struct frame *f, const char *icon_name)
9191 {
9192 if (FRAME_X_WINDOW (f) == 0)
9193 return true;
9194
9195 {
9196 XTextProperty text;
9197 text.value = (unsigned char *) icon_name;
9198 text.encoding = XA_STRING;
9199 text.format = 8;
9200 text.nitems = strlen (icon_name);
9201 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
9202 }
9203
9204 if (f->output_data.x->icon_bitmap > 0)
9205 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
9206 f->output_data.x->icon_bitmap = 0;
9207 x_wm_set_icon_pixmap (f, 0);
9208
9209 return false;
9210 }
9211 \f
9212 #define X_ERROR_MESSAGE_SIZE 200
9213
9214 /* If non-nil, this should be a string.
9215 It means catch X errors and store the error message in this string.
9216
9217 The reason we use a stack is that x_catch_error/x_uncatch_error can
9218 be called from a signal handler.
9219 */
9220
9221 struct x_error_message_stack {
9222 char string[X_ERROR_MESSAGE_SIZE];
9223 Display *dpy;
9224 x_special_error_handler handler;
9225 void *handler_data;
9226 struct x_error_message_stack *prev;
9227 };
9228 static struct x_error_message_stack *x_error_message;
9229
9230 /* An X error handler which stores the error message in
9231 *x_error_message. This is called from x_error_handler if
9232 x_catch_errors is in effect. */
9233
9234 static void
9235 x_error_catcher (Display *display, XErrorEvent *event)
9236 {
9237 XGetErrorText (display, event->error_code,
9238 x_error_message->string,
9239 X_ERROR_MESSAGE_SIZE);
9240 if (x_error_message->handler)
9241 x_error_message->handler (display, event, x_error_message->string,
9242 x_error_message->handler_data);
9243 }
9244
9245 /* Begin trapping X errors for display DPY. Actually we trap X errors
9246 for all displays, but DPY should be the display you are actually
9247 operating on.
9248
9249 After calling this function, X protocol errors no longer cause
9250 Emacs to exit; instead, they are recorded in the string
9251 stored in *x_error_message.
9252
9253 Calling x_check_errors signals an Emacs error if an X error has
9254 occurred since the last call to x_catch_errors or x_check_errors.
9255
9256 Calling x_uncatch_errors resumes the normal error handling.
9257 Calling x_uncatch_errors_after_check is similar, but skips an XSync
9258 to the server, and should be used only immediately after
9259 x_had_errors_p or x_check_errors. */
9260
9261 void
9262 x_catch_errors_with_handler (Display *dpy, x_special_error_handler handler,
9263 void *handler_data)
9264 {
9265 struct x_error_message_stack *data = xmalloc (sizeof *data);
9266
9267 /* Make sure any errors from previous requests have been dealt with. */
9268 XSync (dpy, False);
9269
9270 data->dpy = dpy;
9271 data->string[0] = 0;
9272 data->handler = handler;
9273 data->handler_data = handler_data;
9274 data->prev = x_error_message;
9275 x_error_message = data;
9276 }
9277
9278 void
9279 x_catch_errors (Display *dpy)
9280 {
9281 x_catch_errors_with_handler (dpy, NULL, NULL);
9282 }
9283
9284 /* Undo the last x_catch_errors call.
9285 DPY should be the display that was passed to x_catch_errors.
9286
9287 This version should be used only if the immediately preceding
9288 X-protocol-related thing was x_check_errors or x_had_error_p, both
9289 of which issue XSync calls, so we don't need to re-sync here. */
9290
9291 void
9292 x_uncatch_errors_after_check (void)
9293 {
9294 struct x_error_message_stack *tmp;
9295
9296 block_input ();
9297 tmp = x_error_message;
9298 x_error_message = x_error_message->prev;
9299 xfree (tmp);
9300 unblock_input ();
9301 }
9302
9303 /* Undo the last x_catch_errors call.
9304 DPY should be the display that was passed to x_catch_errors. */
9305
9306 void
9307 x_uncatch_errors (void)
9308 {
9309 struct x_error_message_stack *tmp;
9310
9311 block_input ();
9312
9313 /* The display may have been closed before this function is called.
9314 Check if it is still open before calling XSync. */
9315 if (x_display_info_for_display (x_error_message->dpy) != 0)
9316 XSync (x_error_message->dpy, False);
9317
9318 tmp = x_error_message;
9319 x_error_message = x_error_message->prev;
9320 xfree (tmp);
9321 unblock_input ();
9322 }
9323
9324 /* If any X protocol errors have arrived since the last call to
9325 x_catch_errors or x_check_errors, signal an Emacs error using
9326 sprintf (a buffer, FORMAT, the x error message text) as the text. */
9327
9328 void
9329 x_check_errors (Display *dpy, const char *format)
9330 {
9331 /* Make sure to catch any errors incurred so far. */
9332 XSync (dpy, False);
9333
9334 if (x_error_message->string[0])
9335 {
9336 char string[X_ERROR_MESSAGE_SIZE];
9337 memcpy (string, x_error_message->string, X_ERROR_MESSAGE_SIZE);
9338 x_uncatch_errors ();
9339 error (format, string);
9340 }
9341 }
9342
9343 /* Nonzero if we had any X protocol errors
9344 since we did x_catch_errors on DPY. */
9345
9346 bool
9347 x_had_errors_p (Display *dpy)
9348 {
9349 /* Make sure to catch any errors incurred so far. */
9350 XSync (dpy, False);
9351
9352 return x_error_message->string[0] != 0;
9353 }
9354
9355 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
9356
9357 void
9358 x_clear_errors (Display *dpy)
9359 {
9360 x_error_message->string[0] = 0;
9361 }
9362
9363 #if false
9364 /* See comment in unwind_to_catch why calling this is a bad
9365 * idea. --lorentey */
9366 /* Close off all unclosed x_catch_errors calls. */
9367
9368 void
9369 x_fully_uncatch_errors (void)
9370 {
9371 while (x_error_message)
9372 x_uncatch_errors ();
9373 }
9374 #endif
9375
9376 #if false
9377 static unsigned int x_wire_count;
9378 x_trace_wire (void)
9379 {
9380 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
9381 }
9382 #endif
9383
9384 \f
9385 /************************************************************************
9386 Handling X errors
9387 ************************************************************************/
9388
9389 /* Error message passed to x_connection_closed. */
9390
9391 static char *error_msg;
9392
9393 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
9394 the text of an error message that lead to the connection loss. */
9395
9396 static void
9397 x_connection_closed (Display *dpy, const char *error_message, bool ioerror)
9398 {
9399 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
9400 Lisp_Object frame, tail;
9401 ptrdiff_t idx = SPECPDL_INDEX ();
9402
9403 error_msg = alloca (strlen (error_message) + 1);
9404 strcpy (error_msg, error_message);
9405
9406 /* Inhibit redisplay while frames are being deleted. */
9407 specbind (Qinhibit_redisplay, Qt);
9408
9409 if (dpyinfo)
9410 {
9411 /* Protect display from being closed when we delete the last
9412 frame on it. */
9413 dpyinfo->reference_count++;
9414 dpyinfo->terminal->reference_count++;
9415 }
9416 if (ioerror) dpyinfo->display = 0;
9417
9418 /* First delete frames whose mini-buffers are on frames
9419 that are on the dead display. */
9420 FOR_EACH_FRAME (tail, frame)
9421 {
9422 Lisp_Object minibuf_frame;
9423 minibuf_frame
9424 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
9425 if (FRAME_X_P (XFRAME (frame))
9426 && FRAME_X_P (XFRAME (minibuf_frame))
9427 && ! EQ (frame, minibuf_frame)
9428 && FRAME_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
9429 delete_frame (frame, Qnoelisp);
9430 }
9431
9432 /* Now delete all remaining frames on the dead display.
9433 We are now sure none of these is used as the mini-buffer
9434 for another frame that we need to delete. */
9435 FOR_EACH_FRAME (tail, frame)
9436 if (FRAME_X_P (XFRAME (frame))
9437 && FRAME_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
9438 {
9439 /* Set this to t so that delete_frame won't get confused
9440 trying to find a replacement. */
9441 kset_default_minibuffer_frame (FRAME_KBOARD (XFRAME (frame)), Qt);
9442 delete_frame (frame, Qnoelisp);
9443 }
9444
9445 /* If DPYINFO is null, this means we didn't open the display in the
9446 first place, so don't try to close it. */
9447 if (dpyinfo)
9448 {
9449 /* We can not call XtCloseDisplay here because it calls XSync.
9450 XSync inside the error handler apparently hangs Emacs. On
9451 current Xt versions, this isn't needed either. */
9452 #ifdef USE_GTK
9453 /* A long-standing GTK bug prevents proper disconnect handling
9454 (https://bugzilla.gnome.org/show_bug.cgi?id=85715). Once,
9455 the resulting Glib error message loop filled a user's disk.
9456 To avoid this, kill Emacs unconditionally on disconnect. */
9457 shut_down_emacs (0, Qnil);
9458 fprintf (stderr, "%s\n\
9459 When compiled with GTK, Emacs cannot recover from X disconnects.\n\
9460 This is a GTK bug: https://bugzilla.gnome.org/show_bug.cgi?id=85715\n\
9461 For details, see etc/PROBLEMS.\n",
9462 error_msg);
9463 emacs_abort ();
9464 #endif /* USE_GTK */
9465
9466 /* Indicate that this display is dead. */
9467 dpyinfo->display = 0;
9468
9469 dpyinfo->reference_count--;
9470 dpyinfo->terminal->reference_count--;
9471 if (dpyinfo->reference_count != 0)
9472 /* We have just closed all frames on this display. */
9473 emacs_abort ();
9474
9475 {
9476 Lisp_Object tmp;
9477 XSETTERMINAL (tmp, dpyinfo->terminal);
9478 Fdelete_terminal (tmp, Qnoelisp);
9479 }
9480 }
9481
9482 if (terminal_list == 0)
9483 {
9484 fprintf (stderr, "%s\n", error_msg);
9485 Fkill_emacs (make_number (70));
9486 /* NOTREACHED */
9487 }
9488
9489 totally_unblock_input ();
9490
9491 unbind_to (idx, Qnil);
9492 clear_waiting_for_input ();
9493
9494 /* Tell GCC not to suggest attribute 'noreturn' for this function. */
9495 IF_LINT (if (! terminal_list) return; )
9496
9497 /* Here, we absolutely have to use a non-local exit (e.g. signal, throw,
9498 longjmp), because returning from this function would get us back into
9499 Xlib's code which will directly call `exit'. */
9500 error ("%s", error_msg);
9501 }
9502
9503 /* We specifically use it before defining it, so that gcc doesn't inline it,
9504 otherwise gdb doesn't know how to properly put a breakpoint on it. */
9505 static void x_error_quitter (Display *, XErrorEvent *);
9506
9507 /* This is the first-level handler for X protocol errors.
9508 It calls x_error_quitter or x_error_catcher. */
9509
9510 static int
9511 x_error_handler (Display *display, XErrorEvent *event)
9512 {
9513 #if defined USE_GTK && defined HAVE_GTK3
9514 if ((event->error_code == BadMatch || event->error_code == BadWindow)
9515 && event->request_code == X_SetInputFocus)
9516 {
9517 return 0;
9518 }
9519 #endif
9520
9521 if (x_error_message)
9522 x_error_catcher (display, event);
9523 else
9524 x_error_quitter (display, event);
9525 return 0;
9526 }
9527
9528 /* This is the usual handler for X protocol errors.
9529 It kills all frames on the display that we got the error for.
9530 If that was the only one, it prints an error message and kills Emacs. */
9531
9532 /* .gdbinit puts a breakpoint here, so make sure it is not inlined. */
9533
9534 /* On older GCC versions, just putting x_error_quitter
9535 after x_error_handler prevents inlining into the former. */
9536
9537 static void NO_INLINE
9538 x_error_quitter (Display *display, XErrorEvent *event)
9539 {
9540 char buf[256], buf1[356];
9541
9542 /* Ignore BadName errors. They can happen because of fonts
9543 or colors that are not defined. */
9544
9545 if (event->error_code == BadName)
9546 return;
9547
9548 /* Note that there is no real way portable across R3/R4 to get the
9549 original error handler. */
9550
9551 XGetErrorText (display, event->error_code, buf, sizeof (buf));
9552 sprintf (buf1, "X protocol error: %s on protocol request %d",
9553 buf, event->request_code);
9554 x_connection_closed (display, buf1, false);
9555 }
9556
9557
9558 /* This is the handler for X IO errors, always.
9559 It kills all frames on the display that we lost touch with.
9560 If that was the only one, it prints an error message and kills Emacs. */
9561
9562 static int
9563 x_io_error_quitter (Display *display)
9564 {
9565 char buf[256];
9566
9567 snprintf (buf, sizeof buf, "Connection lost to X server '%s'",
9568 DisplayString (display));
9569 x_connection_closed (display, buf, true);
9570 return 0;
9571 }
9572 \f
9573 /* Changing the font of the frame. */
9574
9575 /* Give frame F the font FONT-OBJECT as its default font. The return
9576 value is FONT-OBJECT. FONTSET is an ID of the fontset for the
9577 frame. If it is negative, generate a new fontset from
9578 FONT-OBJECT. */
9579
9580 Lisp_Object
9581 x_new_font (struct frame *f, Lisp_Object font_object, int fontset)
9582 {
9583 struct font *font = XFONT_OBJECT (font_object);
9584 int unit, font_ascent, font_descent;
9585 #ifndef USE_X_TOOLKIT
9586 int old_menu_bar_height = FRAME_MENU_BAR_HEIGHT (f);
9587 Lisp_Object fullscreen;
9588 #endif
9589
9590 if (fontset < 0)
9591 fontset = fontset_from_font (font_object);
9592 FRAME_FONTSET (f) = fontset;
9593 if (FRAME_FONT (f) == font)
9594 /* This font is already set in frame F. There's nothing more to
9595 do. */
9596 return font_object;
9597
9598 FRAME_FONT (f) = font;
9599 FRAME_BASELINE_OFFSET (f) = font->baseline_offset;
9600 FRAME_COLUMN_WIDTH (f) = font->average_width;
9601 get_font_ascent_descent (font, &font_ascent, &font_descent);
9602 FRAME_LINE_HEIGHT (f) = font_ascent + font_descent;
9603
9604 #ifndef USE_X_TOOLKIT
9605 FRAME_MENU_BAR_HEIGHT (f) = FRAME_MENU_BAR_LINES (f) * FRAME_LINE_HEIGHT (f);
9606 #endif
9607
9608 /* Compute character columns occupied by scrollbar.
9609
9610 Don't do things differently for non-toolkit scrollbars
9611 (Bug#17163). */
9612 unit = FRAME_COLUMN_WIDTH (f);
9613 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
9614 FRAME_CONFIG_SCROLL_BAR_COLS (f)
9615 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + unit - 1) / unit;
9616 else
9617 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + unit - 1) / unit;
9618
9619 if (FRAME_X_WINDOW (f) != 0)
9620 {
9621 /* Don't change the size of a tip frame; there's no point in
9622 doing it because it's done in Fx_show_tip, and it leads to
9623 problems because the tip frame has no widget. */
9624 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
9625 {
9626 adjust_frame_size (f, FRAME_COLS (f) * FRAME_COLUMN_WIDTH (f),
9627 FRAME_LINES (f) * FRAME_LINE_HEIGHT (f), 3,
9628 false, Qfont);
9629 #ifndef USE_X_TOOLKIT
9630 if (FRAME_MENU_BAR_HEIGHT (f) != old_menu_bar_height
9631 && !f->after_make_frame
9632 && (EQ (frame_inhibit_implied_resize, Qt)
9633 || (CONSP (frame_inhibit_implied_resize)
9634 && NILP (Fmemq (Qfont, frame_inhibit_implied_resize))))
9635 && (NILP (fullscreen = get_frame_param (f, Qfullscreen))
9636 || EQ (fullscreen, Qfullwidth)))
9637 /* If the menu bar height changes, try to keep text height
9638 constant. */
9639 adjust_frame_size
9640 (f, -1, FRAME_TEXT_HEIGHT (f) + FRAME_MENU_BAR_HEIGHT (f)
9641 - old_menu_bar_height, 1, false, Qfont);
9642 #endif /* USE_X_TOOLKIT */
9643 }
9644 }
9645
9646 #ifdef HAVE_X_I18N
9647 if (FRAME_XIC (f)
9648 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
9649 {
9650 block_input ();
9651 xic_set_xfontset (f, SSDATA (fontset_ascii (fontset)));
9652 unblock_input ();
9653 }
9654 #endif
9655
9656 return font_object;
9657 }
9658
9659 \f
9660 /***********************************************************************
9661 X Input Methods
9662 ***********************************************************************/
9663
9664 #ifdef HAVE_X_I18N
9665
9666 #ifdef HAVE_X11R6
9667
9668 /* XIM destroy callback function, which is called whenever the
9669 connection to input method XIM dies. CLIENT_DATA contains a
9670 pointer to the x_display_info structure corresponding to XIM. */
9671
9672 static void
9673 xim_destroy_callback (XIM xim, XPointer client_data, XPointer call_data)
9674 {
9675 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
9676 Lisp_Object frame, tail;
9677
9678 block_input ();
9679
9680 /* No need to call XDestroyIC.. */
9681 FOR_EACH_FRAME (tail, frame)
9682 {
9683 struct frame *f = XFRAME (frame);
9684 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo)
9685 {
9686 FRAME_XIC (f) = NULL;
9687 xic_free_xfontset (f);
9688 }
9689 }
9690
9691 /* No need to call XCloseIM. */
9692 dpyinfo->xim = NULL;
9693 XFree (dpyinfo->xim_styles);
9694 unblock_input ();
9695 }
9696
9697 #endif /* HAVE_X11R6 */
9698
9699 /* Open the connection to the XIM server on display DPYINFO.
9700 RESOURCE_NAME is the resource name Emacs uses. */
9701
9702 static void
9703 xim_open_dpy (struct x_display_info *dpyinfo, char *resource_name)
9704 {
9705 XIM xim;
9706
9707 #ifdef HAVE_XIM
9708 if (use_xim)
9709 {
9710 if (dpyinfo->xim)
9711 XCloseIM (dpyinfo->xim);
9712 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name,
9713 emacs_class);
9714 dpyinfo->xim = xim;
9715
9716 if (xim)
9717 {
9718 #ifdef HAVE_X11R6
9719 XIMCallback destroy;
9720 #endif
9721
9722 /* Get supported styles and XIM values. */
9723 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
9724
9725 #ifdef HAVE_X11R6
9726 destroy.callback = xim_destroy_callback;
9727 destroy.client_data = (XPointer)dpyinfo;
9728 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
9729 #endif
9730 }
9731 }
9732
9733 else
9734 #endif /* HAVE_XIM */
9735 dpyinfo->xim = NULL;
9736 }
9737
9738
9739 #ifdef HAVE_X11R6_XIM
9740
9741 /* XIM instantiate callback function, which is called whenever an XIM
9742 server is available. DISPLAY is the display of the XIM.
9743 CLIENT_DATA contains a pointer to an xim_inst_t structure created
9744 when the callback was registered. */
9745
9746 static void
9747 xim_instantiate_callback (Display *display, XPointer client_data, XPointer call_data)
9748 {
9749 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
9750 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
9751
9752 /* We don't support multiple XIM connections. */
9753 if (dpyinfo->xim)
9754 return;
9755
9756 xim_open_dpy (dpyinfo, xim_inst->resource_name);
9757
9758 /* Create XIC for the existing frames on the same display, as long
9759 as they have no XIC. */
9760 if (dpyinfo->xim && dpyinfo->reference_count > 0)
9761 {
9762 Lisp_Object tail, frame;
9763
9764 block_input ();
9765 FOR_EACH_FRAME (tail, frame)
9766 {
9767 struct frame *f = XFRAME (frame);
9768
9769 if (FRAME_X_P (f)
9770 && FRAME_DISPLAY_INFO (f) == xim_inst->dpyinfo)
9771 if (FRAME_XIC (f) == NULL)
9772 {
9773 create_frame_xic (f);
9774 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
9775 xic_set_statusarea (f);
9776 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
9777 {
9778 struct window *w = XWINDOW (f->selected_window);
9779 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
9780 }
9781 }
9782 }
9783
9784 unblock_input ();
9785 }
9786 }
9787
9788 #endif /* HAVE_X11R6_XIM */
9789
9790
9791 /* Open a connection to the XIM server on display DPYINFO.
9792 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
9793 connection only at the first time. On X11R6, open the connection
9794 in the XIM instantiate callback function. */
9795
9796 static void
9797 xim_initialize (struct x_display_info *dpyinfo, char *resource_name)
9798 {
9799 dpyinfo->xim = NULL;
9800 #ifdef HAVE_XIM
9801 if (use_xim)
9802 {
9803 #ifdef HAVE_X11R6_XIM
9804 struct xim_inst_t *xim_inst = xmalloc (sizeof *xim_inst);
9805 Bool ret;
9806
9807 dpyinfo->xim_callback_data = xim_inst;
9808 xim_inst->dpyinfo = dpyinfo;
9809 xim_inst->resource_name = xstrdup (resource_name);
9810 ret = XRegisterIMInstantiateCallback
9811 (dpyinfo->display, dpyinfo->xrdb, xim_inst->resource_name,
9812 emacs_class, xim_instantiate_callback,
9813 /* This is XPointer in XFree86 but (XPointer *)
9814 on Tru64, at least, hence the configure test. */
9815 (XRegisterIMInstantiateCallback_arg6) xim_inst);
9816 eassert (ret == True);
9817 #else /* not HAVE_X11R6_XIM */
9818 xim_open_dpy (dpyinfo, resource_name);
9819 #endif /* not HAVE_X11R6_XIM */
9820 }
9821 #endif /* HAVE_XIM */
9822 }
9823
9824
9825 /* Close the connection to the XIM server on display DPYINFO. */
9826
9827 static void
9828 xim_close_dpy (struct x_display_info *dpyinfo)
9829 {
9830 #ifdef HAVE_XIM
9831 if (use_xim)
9832 {
9833 #ifdef HAVE_X11R6_XIM
9834 struct xim_inst_t *xim_inst = dpyinfo->xim_callback_data;
9835
9836 if (dpyinfo->display)
9837 {
9838 Bool ret = XUnregisterIMInstantiateCallback
9839 (dpyinfo->display, dpyinfo->xrdb, xim_inst->resource_name,
9840 emacs_class, xim_instantiate_callback,
9841 (XRegisterIMInstantiateCallback_arg6) xim_inst);
9842 eassert (ret == True);
9843 }
9844 xfree (xim_inst->resource_name);
9845 xfree (xim_inst);
9846 #endif /* HAVE_X11R6_XIM */
9847 if (dpyinfo->display)
9848 XCloseIM (dpyinfo->xim);
9849 dpyinfo->xim = NULL;
9850 XFree (dpyinfo->xim_styles);
9851 }
9852 #endif /* HAVE_XIM */
9853 }
9854
9855 #endif /* not HAVE_X11R6_XIM */
9856
9857
9858 \f
9859 /* Calculate the absolute position in frame F
9860 from its current recorded position values and gravity. */
9861
9862 static void
9863 x_calc_absolute_position (struct frame *f)
9864 {
9865 int flags = f->size_hint_flags;
9866
9867 /* We have nothing to do if the current position
9868 is already for the top-left corner. */
9869 if (! ((flags & XNegative) || (flags & YNegative)))
9870 return;
9871
9872 /* Treat negative positions as relative to the leftmost bottommost
9873 position that fits on the screen. */
9874 if (flags & XNegative)
9875 f->left_pos = x_display_pixel_width (FRAME_DISPLAY_INFO (f))
9876 - FRAME_PIXEL_WIDTH (f) + f->left_pos;
9877
9878 {
9879 int height = FRAME_PIXEL_HEIGHT (f);
9880
9881 #if defined USE_X_TOOLKIT && defined USE_MOTIF
9882 /* Something is fishy here. When using Motif, starting Emacs with
9883 `-g -0-0', the frame appears too low by a few pixels.
9884
9885 This seems to be so because initially, while Emacs is starting,
9886 the column widget's height and the frame's pixel height are
9887 different. The column widget's height is the right one. In
9888 later invocations, when Emacs is up, the frame's pixel height
9889 is right, though.
9890
9891 It's not obvious where the initial small difference comes from.
9892 2000-12-01, gerd. */
9893
9894 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
9895 #endif
9896
9897 if (flags & YNegative)
9898 f->top_pos = x_display_pixel_height (FRAME_DISPLAY_INFO (f))
9899 - height + f->top_pos;
9900 }
9901
9902 /* The left_pos and top_pos
9903 are now relative to the top and left screen edges,
9904 so the flags should correspond. */
9905 f->size_hint_flags &= ~ (XNegative | YNegative);
9906 }
9907
9908 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
9909 to really change the position, and 0 when calling from
9910 x_make_frame_visible (in that case, XOFF and YOFF are the current
9911 position values). It is -1 when calling from x_set_frame_parameters,
9912 which means, do adjust for borders but don't change the gravity. */
9913
9914 void
9915 x_set_offset (struct frame *f, register int xoff, register int yoff, int change_gravity)
9916 {
9917 int modified_top, modified_left;
9918
9919 if (change_gravity > 0)
9920 {
9921 f->top_pos = yoff;
9922 f->left_pos = xoff;
9923 f->size_hint_flags &= ~ (XNegative | YNegative);
9924 if (xoff < 0)
9925 f->size_hint_flags |= XNegative;
9926 if (yoff < 0)
9927 f->size_hint_flags |= YNegative;
9928 f->win_gravity = NorthWestGravity;
9929 }
9930 x_calc_absolute_position (f);
9931
9932 block_input ();
9933 x_wm_set_size_hint (f, 0, false);
9934
9935 modified_left = f->left_pos;
9936 modified_top = f->top_pos;
9937
9938 if (change_gravity != 0 && FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A)
9939 {
9940 /* Some WMs (twm, wmaker at least) has an offset that is smaller
9941 than the WM decorations. So we use the calculated offset instead
9942 of the WM decoration sizes here (x/y_pixels_outer_diff). */
9943 modified_left += FRAME_X_OUTPUT (f)->move_offset_left;
9944 modified_top += FRAME_X_OUTPUT (f)->move_offset_top;
9945 }
9946
9947 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9948 modified_left, modified_top);
9949
9950 x_sync_with_move (f, f->left_pos, f->top_pos,
9951 FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN);
9952
9953 /* change_gravity is non-zero when this function is called from Lisp to
9954 programmatically move a frame. In that case, we call
9955 x_check_expected_move to discover if we have a "Type A" or "Type B"
9956 window manager, and, for a "Type A" window manager, adjust the position
9957 of the frame.
9958
9959 We call x_check_expected_move if a programmatic move occurred, and
9960 either the window manager type (A/B) is unknown or it is Type A but we
9961 need to compute the top/left offset adjustment for this frame. */
9962
9963 if (change_gravity != 0
9964 && (FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
9965 || (FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A
9966 && (FRAME_X_OUTPUT (f)->move_offset_left == 0
9967 && FRAME_X_OUTPUT (f)->move_offset_top == 0))))
9968 x_check_expected_move (f, modified_left, modified_top);
9969
9970 unblock_input ();
9971 }
9972
9973 /* Return true if _NET_SUPPORTING_WM_CHECK window exists and _NET_SUPPORTED
9974 on the root window for frame F contains ATOMNAME.
9975 This is how a WM check shall be done according to the Window Manager
9976 Specification/Extended Window Manager Hints at
9977 http://freedesktop.org/wiki/Specifications/wm-spec. */
9978
9979 bool
9980 x_wm_supports (struct frame *f, Atom want_atom)
9981 {
9982 Atom actual_type;
9983 unsigned long actual_size, bytes_remaining;
9984 int i, rc, actual_format;
9985 bool ret;
9986 Window wmcheck_window;
9987 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
9988 Window target_window = dpyinfo->root_window;
9989 int max_len = 65536;
9990 Display *dpy = FRAME_X_DISPLAY (f);
9991 unsigned char *tmp_data = NULL;
9992 Atom target_type = XA_WINDOW;
9993
9994 block_input ();
9995
9996 x_catch_errors (dpy);
9997 rc = XGetWindowProperty (dpy, target_window,
9998 dpyinfo->Xatom_net_supporting_wm_check,
9999 0, max_len, False, target_type,
10000 &actual_type, &actual_format, &actual_size,
10001 &bytes_remaining, &tmp_data);
10002
10003 if (rc != Success || actual_type != XA_WINDOW || x_had_errors_p (dpy))
10004 {
10005 if (tmp_data) XFree (tmp_data);
10006 x_uncatch_errors ();
10007 unblock_input ();
10008 return false;
10009 }
10010
10011 wmcheck_window = *(Window *) tmp_data;
10012 XFree (tmp_data);
10013
10014 /* Check if window exists. */
10015 XSelectInput (dpy, wmcheck_window, StructureNotifyMask);
10016 if (x_had_errors_p (dpy))
10017 {
10018 x_uncatch_errors_after_check ();
10019 unblock_input ();
10020 return false;
10021 }
10022
10023 if (dpyinfo->net_supported_window != wmcheck_window)
10024 {
10025 /* Window changed, reload atoms */
10026 if (dpyinfo->net_supported_atoms != NULL)
10027 XFree (dpyinfo->net_supported_atoms);
10028 dpyinfo->net_supported_atoms = NULL;
10029 dpyinfo->nr_net_supported_atoms = 0;
10030 dpyinfo->net_supported_window = 0;
10031
10032 target_type = XA_ATOM;
10033 tmp_data = NULL;
10034 rc = XGetWindowProperty (dpy, target_window,
10035 dpyinfo->Xatom_net_supported,
10036 0, max_len, False, target_type,
10037 &actual_type, &actual_format, &actual_size,
10038 &bytes_remaining, &tmp_data);
10039
10040 if (rc != Success || actual_type != XA_ATOM || x_had_errors_p (dpy))
10041 {
10042 if (tmp_data) XFree (tmp_data);
10043 x_uncatch_errors ();
10044 unblock_input ();
10045 return false;
10046 }
10047
10048 dpyinfo->net_supported_atoms = (Atom *)tmp_data;
10049 dpyinfo->nr_net_supported_atoms = actual_size;
10050 dpyinfo->net_supported_window = wmcheck_window;
10051 }
10052
10053 ret = false;
10054
10055 for (i = 0; !ret && i < dpyinfo->nr_net_supported_atoms; ++i)
10056 ret = dpyinfo->net_supported_atoms[i] == want_atom;
10057
10058 x_uncatch_errors ();
10059 unblock_input ();
10060
10061 return ret;
10062 }
10063
10064 static void
10065 set_wm_state (Lisp_Object frame, bool add, Atom atom, Atom value)
10066 {
10067 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (XFRAME (frame));
10068
10069 x_send_client_event (frame, make_number (0), frame,
10070 dpyinfo->Xatom_net_wm_state,
10071 make_number (32),
10072 /* 1 = add, 0 = remove */
10073 Fcons
10074 (make_number (add),
10075 Fcons
10076 (make_fixnum_or_float (atom),
10077 (value != 0
10078 ? list1 (make_fixnum_or_float (value))
10079 : Qnil))));
10080 }
10081
10082 void
10083 x_set_sticky (struct frame *f, Lisp_Object new_value, Lisp_Object old_value)
10084 {
10085 Lisp_Object frame;
10086 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10087
10088 XSETFRAME (frame, f);
10089
10090 set_wm_state (frame, !NILP (new_value),
10091 dpyinfo->Xatom_net_wm_state_sticky, None);
10092 }
10093
10094 /* Return the current _NET_WM_STATE.
10095 SIZE_STATE is set to one of the FULLSCREEN_* values.
10096 Set *STICKY to the sticky state.
10097
10098 Return true iff we are not hidden. */
10099
10100 static bool
10101 get_current_wm_state (struct frame *f,
10102 Window window,
10103 int *size_state,
10104 bool *sticky)
10105 {
10106 unsigned long actual_size;
10107 int i;
10108 bool is_hidden = false;
10109 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10110 long max_len = 65536;
10111 Atom target_type = XA_ATOM;
10112 /* If XCB is available, we can avoid three XSync calls. */
10113 #ifdef USE_XCB
10114 xcb_get_property_cookie_t prop_cookie;
10115 xcb_get_property_reply_t *prop;
10116 xcb_atom_t *reply_data;
10117 #else
10118 Display *dpy = FRAME_X_DISPLAY (f);
10119 unsigned long bytes_remaining;
10120 int rc, actual_format;
10121 Atom actual_type;
10122 unsigned char *tmp_data = NULL;
10123 Atom *reply_data;
10124 #endif
10125
10126 *sticky = false;
10127 *size_state = FULLSCREEN_NONE;
10128
10129 block_input ();
10130
10131 #ifdef USE_XCB
10132 prop_cookie = xcb_get_property (dpyinfo->xcb_connection, 0, window,
10133 dpyinfo->Xatom_net_wm_state,
10134 target_type, 0, max_len);
10135 prop = xcb_get_property_reply (dpyinfo->xcb_connection, prop_cookie, NULL);
10136 if (prop && prop->type == target_type)
10137 {
10138 int actual_bytes = xcb_get_property_value_length (prop);
10139 eassume (0 <= actual_bytes);
10140 actual_size = actual_bytes / sizeof *reply_data;
10141 reply_data = xcb_get_property_value (prop);
10142 }
10143 else
10144 {
10145 actual_size = 0;
10146 is_hidden = FRAME_ICONIFIED_P (f);
10147 }
10148 #else
10149 x_catch_errors (dpy);
10150 rc = XGetWindowProperty (dpy, window, dpyinfo->Xatom_net_wm_state,
10151 0, max_len, False, target_type,
10152 &actual_type, &actual_format, &actual_size,
10153 &bytes_remaining, &tmp_data);
10154
10155 if (rc == Success && actual_type == target_type && ! x_had_errors_p (dpy))
10156 reply_data = (Atom *) tmp_data;
10157 else
10158 {
10159 actual_size = 0;
10160 is_hidden = FRAME_ICONIFIED_P (f);
10161 }
10162
10163 x_uncatch_errors ();
10164 #endif
10165
10166 for (i = 0; i < actual_size; ++i)
10167 {
10168 Atom a = reply_data[i];
10169 if (a == dpyinfo->Xatom_net_wm_state_hidden)
10170 is_hidden = true;
10171 else if (a == dpyinfo->Xatom_net_wm_state_maximized_horz)
10172 {
10173 if (*size_state == FULLSCREEN_HEIGHT)
10174 *size_state = FULLSCREEN_MAXIMIZED;
10175 else
10176 *size_state = FULLSCREEN_WIDTH;
10177 }
10178 else if (a == dpyinfo->Xatom_net_wm_state_maximized_vert)
10179 {
10180 if (*size_state == FULLSCREEN_WIDTH)
10181 *size_state = FULLSCREEN_MAXIMIZED;
10182 else
10183 *size_state = FULLSCREEN_HEIGHT;
10184 }
10185 else if (a == dpyinfo->Xatom_net_wm_state_fullscreen)
10186 *size_state = FULLSCREEN_BOTH;
10187 else if (a == dpyinfo->Xatom_net_wm_state_sticky)
10188 *sticky = true;
10189 }
10190
10191 #ifdef USE_XCB
10192 free (prop);
10193 #else
10194 if (tmp_data) XFree (tmp_data);
10195 #endif
10196
10197 unblock_input ();
10198 return ! is_hidden;
10199 }
10200
10201 /* Do fullscreen as specified in extended window manager hints */
10202
10203 static bool
10204 do_ewmh_fullscreen (struct frame *f)
10205 {
10206 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10207 bool have_net_atom = x_wm_supports (f, dpyinfo->Xatom_net_wm_state);
10208 int cur;
10209 bool dummy;
10210
10211 get_current_wm_state (f, FRAME_OUTER_WINDOW (f), &cur, &dummy);
10212
10213 /* Some window managers don't say they support _NET_WM_STATE, but they do say
10214 they support _NET_WM_STATE_FULLSCREEN. Try that also. */
10215 if (!have_net_atom)
10216 have_net_atom = x_wm_supports (f, dpyinfo->Xatom_net_wm_state_fullscreen);
10217
10218 if (have_net_atom && cur != f->want_fullscreen)
10219 {
10220 Lisp_Object frame;
10221
10222 XSETFRAME (frame, f);
10223
10224 /* Keep number of calls to set_wm_state as low as possible.
10225 Some window managers, or possible Gtk+, hangs when too many
10226 are sent at once. */
10227 switch (f->want_fullscreen)
10228 {
10229 case FULLSCREEN_BOTH:
10230 if (cur != FULLSCREEN_BOTH)
10231 set_wm_state (frame, true, dpyinfo->Xatom_net_wm_state_fullscreen,
10232 None);
10233 break;
10234 case FULLSCREEN_WIDTH:
10235 if (x_frame_normalize_before_maximize && cur == FULLSCREEN_MAXIMIZED)
10236 {
10237 set_wm_state (frame, false,
10238 dpyinfo->Xatom_net_wm_state_maximized_horz,
10239 dpyinfo->Xatom_net_wm_state_maximized_vert);
10240 set_wm_state (frame, true,
10241 dpyinfo->Xatom_net_wm_state_maximized_horz, None);
10242 }
10243 else
10244 {
10245 if (cur == FULLSCREEN_BOTH || cur == FULLSCREEN_HEIGHT
10246 || cur == FULLSCREEN_MAXIMIZED)
10247 set_wm_state (frame, false, dpyinfo->Xatom_net_wm_state_fullscreen,
10248 dpyinfo->Xatom_net_wm_state_maximized_vert);
10249 if (cur != FULLSCREEN_MAXIMIZED || x_frame_normalize_before_maximize)
10250 set_wm_state (frame, true,
10251 dpyinfo->Xatom_net_wm_state_maximized_horz, None);
10252 }
10253 break;
10254 case FULLSCREEN_HEIGHT:
10255 if (x_frame_normalize_before_maximize && cur == FULLSCREEN_MAXIMIZED)
10256 {
10257 set_wm_state (frame, false,
10258 dpyinfo->Xatom_net_wm_state_maximized_horz,
10259 dpyinfo->Xatom_net_wm_state_maximized_vert);
10260 set_wm_state (frame, true,
10261 dpyinfo->Xatom_net_wm_state_maximized_vert, None);
10262 }
10263 else
10264 {
10265 if (cur == FULLSCREEN_BOTH || cur == FULLSCREEN_WIDTH
10266 || cur == FULLSCREEN_MAXIMIZED)
10267 set_wm_state (frame, false, dpyinfo->Xatom_net_wm_state_fullscreen,
10268 dpyinfo->Xatom_net_wm_state_maximized_horz);
10269 if (cur != FULLSCREEN_MAXIMIZED || x_frame_normalize_before_maximize)
10270 set_wm_state (frame, true,
10271 dpyinfo->Xatom_net_wm_state_maximized_vert, None);
10272 }
10273 break;
10274 case FULLSCREEN_MAXIMIZED:
10275 if (x_frame_normalize_before_maximize && cur == FULLSCREEN_BOTH)
10276 {
10277 set_wm_state (frame, false,
10278 dpyinfo->Xatom_net_wm_state_fullscreen, None);
10279 set_wm_state (frame, true,
10280 dpyinfo->Xatom_net_wm_state_maximized_horz,
10281 dpyinfo->Xatom_net_wm_state_maximized_vert);
10282 }
10283 else if (x_frame_normalize_before_maximize && cur == FULLSCREEN_WIDTH)
10284 {
10285 set_wm_state (frame, false,
10286 dpyinfo->Xatom_net_wm_state_maximized_horz, None);
10287 set_wm_state (frame, true,
10288 dpyinfo->Xatom_net_wm_state_maximized_horz,
10289 dpyinfo->Xatom_net_wm_state_maximized_vert);
10290 }
10291 else if (x_frame_normalize_before_maximize && cur == FULLSCREEN_HEIGHT)
10292 {
10293 set_wm_state (frame, false,
10294 dpyinfo->Xatom_net_wm_state_maximized_vert, None);
10295 set_wm_state (frame, true,
10296 dpyinfo->Xatom_net_wm_state_maximized_horz,
10297 dpyinfo->Xatom_net_wm_state_maximized_vert);
10298 }
10299 else
10300 {
10301 if (cur == FULLSCREEN_BOTH)
10302 set_wm_state (frame, false, dpyinfo->Xatom_net_wm_state_fullscreen,
10303 None);
10304 else if (cur == FULLSCREEN_HEIGHT)
10305 set_wm_state (frame, true,
10306 dpyinfo->Xatom_net_wm_state_maximized_horz, None);
10307 else if (cur == FULLSCREEN_WIDTH)
10308 set_wm_state (frame, true, None,
10309 dpyinfo->Xatom_net_wm_state_maximized_vert);
10310 else
10311 set_wm_state (frame, true,
10312 dpyinfo->Xatom_net_wm_state_maximized_horz,
10313 dpyinfo->Xatom_net_wm_state_maximized_vert);
10314 }
10315 break;
10316 case FULLSCREEN_NONE:
10317 if (cur == FULLSCREEN_BOTH)
10318 set_wm_state (frame, false, dpyinfo->Xatom_net_wm_state_fullscreen,
10319 None);
10320 else
10321 set_wm_state (frame, false,
10322 dpyinfo->Xatom_net_wm_state_maximized_horz,
10323 dpyinfo->Xatom_net_wm_state_maximized_vert);
10324 }
10325
10326 f->want_fullscreen = FULLSCREEN_NONE;
10327
10328 }
10329
10330 return have_net_atom;
10331 }
10332
10333 static void
10334 XTfullscreen_hook (struct frame *f)
10335 {
10336 if (FRAME_VISIBLE_P (f))
10337 {
10338 block_input ();
10339 x_check_fullscreen (f);
10340 x_sync (f);
10341 unblock_input ();
10342 }
10343 }
10344
10345
10346 static bool
10347 x_handle_net_wm_state (struct frame *f, const XPropertyEvent *event)
10348 {
10349 int value = FULLSCREEN_NONE;
10350 Lisp_Object lval;
10351 bool sticky = false;
10352 bool not_hidden = get_current_wm_state (f, event->window, &value, &sticky);
10353
10354 lval = Qnil;
10355 switch (value)
10356 {
10357 case FULLSCREEN_WIDTH:
10358 lval = Qfullwidth;
10359 break;
10360 case FULLSCREEN_HEIGHT:
10361 lval = Qfullheight;
10362 break;
10363 case FULLSCREEN_BOTH:
10364 lval = Qfullboth;
10365 break;
10366 case FULLSCREEN_MAXIMIZED:
10367 lval = Qmaximized;
10368 break;
10369 }
10370
10371 frame_size_history_add
10372 (f, Qx_handle_net_wm_state, 0, 0,
10373 list2 (get_frame_param (f, Qfullscreen), lval));
10374
10375 store_frame_param (f, Qfullscreen, lval);
10376 store_frame_param (f, Qsticky, sticky ? Qt : Qnil);
10377
10378 return not_hidden;
10379 }
10380
10381 /* Check if we need to resize the frame due to a fullscreen request.
10382 If so needed, resize the frame. */
10383 static void
10384 x_check_fullscreen (struct frame *f)
10385 {
10386 Lisp_Object lval = Qnil;
10387
10388 if (do_ewmh_fullscreen (f))
10389 return;
10390
10391 if (f->output_data.x->parent_desc != FRAME_DISPLAY_INFO (f)->root_window)
10392 return; /* Only fullscreen without WM or with EWM hints (above). */
10393
10394 /* Setting fullscreen to nil doesn't do anything. We could save the
10395 last non-fullscreen size and restore it, but it seems like a
10396 lot of work for this unusual case (no window manager running). */
10397
10398 if (f->want_fullscreen != FULLSCREEN_NONE)
10399 {
10400 int width = FRAME_PIXEL_WIDTH (f), height = FRAME_PIXEL_HEIGHT (f);
10401 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10402
10403 switch (f->want_fullscreen)
10404 {
10405 /* No difference between these two when there is no WM */
10406 case FULLSCREEN_MAXIMIZED:
10407 lval = Qmaximized;
10408 width = x_display_pixel_width (dpyinfo);
10409 height = x_display_pixel_height (dpyinfo);
10410 break;
10411 case FULLSCREEN_BOTH:
10412 lval = Qfullboth;
10413 width = x_display_pixel_width (dpyinfo);
10414 height = x_display_pixel_height (dpyinfo);
10415 break;
10416 case FULLSCREEN_WIDTH:
10417 lval = Qfullwidth;
10418 width = x_display_pixel_width (dpyinfo);
10419 height = height + FRAME_MENUBAR_HEIGHT (f);
10420 break;
10421 case FULLSCREEN_HEIGHT:
10422 lval = Qfullheight;
10423 height = x_display_pixel_height (dpyinfo);
10424 break;
10425 default:
10426 emacs_abort ();
10427 }
10428
10429 frame_size_history_add
10430 (f, Qx_check_fullscreen, width, height, Qnil);
10431
10432 x_wm_set_size_hint (f, 0, false);
10433
10434 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10435 width, height);
10436
10437 if (FRAME_VISIBLE_P (f))
10438 x_wait_for_event (f, ConfigureNotify);
10439 else
10440 {
10441 change_frame_size (f, width, height - FRAME_MENUBAR_HEIGHT (f),
10442 false, true, false, true);
10443 x_sync (f);
10444 }
10445 }
10446
10447 /* `x_net_wm_state' might have reset the fullscreen frame parameter,
10448 restore it. */
10449 store_frame_param (f, Qfullscreen, lval);
10450 }
10451
10452 /* This function is called by x_set_offset to determine whether the window
10453 manager interfered with the positioning of the frame. Type A window
10454 managers position the surrounding window manager decorations a small
10455 amount above and left of the user-supplied position. Type B window
10456 managers position the surrounding window manager decorations at the
10457 user-specified position. If we detect a Type A window manager, we
10458 compensate by moving the window right and down by the proper amount. */
10459
10460 static void
10461 x_check_expected_move (struct frame *f, int expected_left, int expected_top)
10462 {
10463 int current_left = 0, current_top = 0;
10464
10465 /* x_real_positions returns the left and top offsets of the outermost
10466 window manager window around the frame. */
10467
10468 x_real_positions (f, &current_left, &current_top);
10469
10470 if (current_left != expected_left || current_top != expected_top)
10471 {
10472 /* It's a "Type A" window manager. */
10473
10474 int adjusted_left;
10475 int adjusted_top;
10476
10477 FRAME_DISPLAY_INFO (f)->wm_type = X_WMTYPE_A;
10478 FRAME_X_OUTPUT (f)->move_offset_left = expected_left - current_left;
10479 FRAME_X_OUTPUT (f)->move_offset_top = expected_top - current_top;
10480
10481 /* Now fix the mispositioned frame's location. */
10482
10483 adjusted_left = expected_left + FRAME_X_OUTPUT (f)->move_offset_left;
10484 adjusted_top = expected_top + FRAME_X_OUTPUT (f)->move_offset_top;
10485
10486 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10487 adjusted_left, adjusted_top);
10488
10489 x_sync_with_move (f, expected_left, expected_top, false);
10490 }
10491 else
10492 /* It's a "Type B" window manager. We don't have to adjust the
10493 frame's position. */
10494
10495 FRAME_DISPLAY_INFO (f)->wm_type = X_WMTYPE_B;
10496 }
10497
10498
10499 /* Wait for XGetGeometry to return up-to-date position information for a
10500 recently-moved frame. Call this immediately after calling XMoveWindow.
10501 If FUZZY is non-zero, then LEFT and TOP are just estimates of where the
10502 frame has been moved to, so we use a fuzzy position comparison instead
10503 of an exact comparison. */
10504
10505 static void
10506 x_sync_with_move (struct frame *f, int left, int top, bool fuzzy)
10507 {
10508 int count = 0;
10509
10510 while (count++ < 50)
10511 {
10512 int current_left = 0, current_top = 0;
10513
10514 /* In theory, this call to XSync only needs to happen once, but in
10515 practice, it doesn't seem to work, hence the need for the surrounding
10516 loop. */
10517
10518 XSync (FRAME_X_DISPLAY (f), False);
10519 x_real_positions (f, &current_left, &current_top);
10520
10521 if (fuzzy)
10522 {
10523 /* The left fuzz-factor is 10 pixels. The top fuzz-factor is 40
10524 pixels. */
10525
10526 if (eabs (current_left - left) <= 10
10527 && eabs (current_top - top) <= 40)
10528 return;
10529 }
10530 else if (current_left == left && current_top == top)
10531 return;
10532 }
10533
10534 /* As a last resort, just wait 0.5 seconds and hope that XGetGeometry
10535 will then return up-to-date position info. */
10536
10537 wait_reading_process_output (0, 500000000, 0, false, Qnil, NULL, 0);
10538 }
10539
10540
10541 /* Wait for an event on frame F matching EVENTTYPE. */
10542 void
10543 x_wait_for_event (struct frame *f, int eventtype)
10544 {
10545 int level = interrupt_input_blocked;
10546
10547 fd_set fds;
10548 struct timespec tmo, tmo_at, time_now;
10549 int fd = ConnectionNumber (FRAME_X_DISPLAY (f));
10550
10551 f->wait_event_type = eventtype;
10552
10553 /* Set timeout to 0.1 second. Hopefully not noticeable.
10554 Maybe it should be configurable. */
10555 tmo = make_timespec (0, 100 * 1000 * 1000);
10556 tmo_at = timespec_add (current_timespec (), tmo);
10557
10558 while (f->wait_event_type)
10559 {
10560 pending_signals = true;
10561 totally_unblock_input ();
10562 /* XTread_socket is called after unblock. */
10563 block_input ();
10564 interrupt_input_blocked = level;
10565
10566 FD_ZERO (&fds);
10567 FD_SET (fd, &fds);
10568
10569 time_now = current_timespec ();
10570 if (timespec_cmp (tmo_at, time_now) < 0)
10571 break;
10572
10573 tmo = timespec_sub (tmo_at, time_now);
10574 if (pselect (fd + 1, &fds, NULL, NULL, &tmo, NULL) == 0)
10575 break; /* Timeout */
10576 }
10577
10578 f->wait_event_type = 0;
10579 }
10580
10581
10582 /* Change the size of frame F's X window to WIDTH/HEIGHT in the case F
10583 doesn't have a widget. If CHANGE_GRAVITY, change to
10584 top-left-corner window gravity for this size change and subsequent
10585 size changes. Otherwise leave the window gravity unchanged. */
10586
10587 static void
10588 x_set_window_size_1 (struct frame *f, bool change_gravity,
10589 int width, int height)
10590 {
10591 int pixelwidth = FRAME_TEXT_TO_PIXEL_WIDTH (f, width);
10592 int pixelheight = FRAME_TEXT_TO_PIXEL_HEIGHT (f, height);
10593 int old_width = FRAME_PIXEL_WIDTH (f);
10594 int old_height = FRAME_PIXEL_HEIGHT (f);
10595 Lisp_Object fullscreen = get_frame_param (f, Qfullscreen);
10596
10597 if (change_gravity) f->win_gravity = NorthWestGravity;
10598 x_wm_set_size_hint (f, 0, false);
10599
10600 /* When the frame is fullheight and we only want to change the width
10601 or it is fullwidth and we only want to change the height we should
10602 be able to preserve the fullscreen property. However, due to the
10603 fact that we have to send a resize request anyway, the window
10604 manager will abolish it. At least the respective size should
10605 remain unchanged but giving the frame back its normal size will
10606 be broken ... */
10607 if (EQ (fullscreen, Qfullwidth) && width == FRAME_TEXT_WIDTH (f))
10608 {
10609 frame_size_history_add
10610 (f, Qx_set_window_size_1, width, height,
10611 list2 (make_number (old_height),
10612 make_number (pixelheight + FRAME_MENUBAR_HEIGHT (f))));
10613
10614 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10615 old_width, pixelheight + FRAME_MENUBAR_HEIGHT (f));
10616 }
10617 else if (EQ (fullscreen, Qfullheight) && height == FRAME_TEXT_HEIGHT (f))
10618 {
10619 frame_size_history_add
10620 (f, Qx_set_window_size_2, width, height,
10621 list2 (make_number (old_width), make_number (pixelwidth)));
10622
10623 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10624 pixelwidth, old_height);
10625 }
10626
10627 else
10628 {
10629 frame_size_history_add
10630 (f, Qx_set_window_size_3, width, height,
10631 list3 (make_number (pixelwidth + FRAME_TOOLBAR_WIDTH (f)),
10632 make_number (pixelheight + FRAME_TOOLBAR_HEIGHT (f)
10633 + FRAME_MENUBAR_HEIGHT (f)),
10634 make_number (FRAME_MENUBAR_HEIGHT (f))));
10635
10636 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10637 pixelwidth, pixelheight + FRAME_MENUBAR_HEIGHT (f));
10638 fullscreen = Qnil;
10639 }
10640
10641
10642
10643 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
10644 receive in the ConfigureNotify event; if we get what we asked
10645 for, then the event won't cause the screen to become garbaged, so
10646 we have to make sure to do it here. */
10647 SET_FRAME_GARBAGED (f);
10648
10649 /* Now, strictly speaking, we can't be sure that this is accurate,
10650 but the window manager will get around to dealing with the size
10651 change request eventually, and we'll hear how it went when the
10652 ConfigureNotify event gets here.
10653
10654 We could just not bother storing any of this information here,
10655 and let the ConfigureNotify event set everything up, but that
10656 might be kind of confusing to the Lisp code, since size changes
10657 wouldn't be reported in the frame parameters until some random
10658 point in the future when the ConfigureNotify event arrives.
10659
10660 Pass true for DELAY since we can't run Lisp code inside of
10661 a BLOCK_INPUT. */
10662
10663 /* But the ConfigureNotify may in fact never arrive, and then this is
10664 not right if the frame is visible. Instead wait (with timeout)
10665 for the ConfigureNotify. */
10666 if (FRAME_VISIBLE_P (f))
10667 {
10668 x_wait_for_event (f, ConfigureNotify);
10669
10670 if (!NILP (fullscreen))
10671 /* Try to restore fullscreen state. */
10672 {
10673 store_frame_param (f, Qfullscreen, fullscreen);
10674 x_set_fullscreen (f, fullscreen, fullscreen);
10675 }
10676 }
10677 else
10678 {
10679 change_frame_size (f, width, height, false, true, false, true);
10680 x_sync (f);
10681 }
10682 }
10683
10684
10685 /* Call this to change the size of frame F's x-window.
10686 If CHANGE_GRAVITY, change to top-left-corner window gravity
10687 for this size change and subsequent size changes.
10688 Otherwise we leave the window gravity unchanged. */
10689
10690 void
10691 x_set_window_size (struct frame *f, bool change_gravity,
10692 int width, int height, bool pixelwise)
10693 {
10694 block_input ();
10695
10696 /* The following breaks our calculations. If it's really needed,
10697 think of something else. */
10698 #if false
10699 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
10700 {
10701 int text_width, text_height;
10702
10703 /* When the frame is maximized/fullscreen or running under for
10704 example Xmonad, x_set_window_size_1 will be a no-op.
10705 In that case, the right thing to do is extend rows/width to
10706 the current frame size. We do that first if x_set_window_size_1
10707 turns out to not be a no-op (there is no way to know).
10708 The size will be adjusted again if the frame gets a
10709 ConfigureNotify event as a result of x_set_window_size. */
10710 int pixelh = FRAME_PIXEL_HEIGHT (f);
10711 #ifdef USE_X_TOOLKIT
10712 /* The menu bar is not part of text lines. The tool bar
10713 is however. */
10714 pixelh -= FRAME_MENUBAR_HEIGHT (f);
10715 #endif
10716 text_width = FRAME_PIXEL_TO_TEXT_WIDTH (f, FRAME_PIXEL_WIDTH (f));
10717 text_height = FRAME_PIXEL_TO_TEXT_HEIGHT (f, pixelh);
10718
10719 change_frame_size (f, text_width, text_height, false, true, false, true);
10720 }
10721 #endif
10722
10723 /* Pixelize width and height, if necessary. */
10724 if (! pixelwise)
10725 {
10726 width = width * FRAME_COLUMN_WIDTH (f);
10727 height = height * FRAME_LINE_HEIGHT (f);
10728 }
10729
10730 #ifdef USE_GTK
10731 if (FRAME_GTK_WIDGET (f))
10732 xg_frame_set_char_size (f, width, height);
10733 else
10734 x_set_window_size_1 (f, change_gravity, width, height);
10735 #else /* not USE_GTK */
10736 x_set_window_size_1 (f, change_gravity, width, height);
10737 x_clear_under_internal_border (f);
10738 #endif /* not USE_GTK */
10739
10740 /* If cursor was outside the new size, mark it as off. */
10741 mark_window_cursors_off (XWINDOW (f->root_window));
10742
10743 /* Clear out any recollection of where the mouse highlighting was,
10744 since it might be in a place that's outside the new frame size.
10745 Actually checking whether it is outside is a pain in the neck,
10746 so don't try--just let the highlighting be done afresh with new size. */
10747 cancel_mouse_face (f);
10748
10749 unblock_input ();
10750
10751 do_pending_window_change (false);
10752 }
10753
10754 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
10755
10756 void
10757 frame_set_mouse_pixel_position (struct frame *f, int pix_x, int pix_y)
10758 {
10759 block_input ();
10760
10761 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
10762 0, 0, 0, 0, pix_x, pix_y);
10763 unblock_input ();
10764 }
10765 \f
10766 /* Raise frame F. */
10767
10768 void
10769 x_raise_frame (struct frame *f)
10770 {
10771 block_input ();
10772 if (FRAME_VISIBLE_P (f))
10773 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
10774 XFlush (FRAME_X_DISPLAY (f));
10775 unblock_input ();
10776 }
10777
10778 /* Lower frame F. */
10779
10780 static void
10781 x_lower_frame (struct frame *f)
10782 {
10783 if (FRAME_VISIBLE_P (f))
10784 {
10785 block_input ();
10786 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
10787 XFlush (FRAME_X_DISPLAY (f));
10788 unblock_input ();
10789 }
10790 }
10791
10792 /* Request focus with XEmbed */
10793
10794 void
10795 xembed_request_focus (struct frame *f)
10796 {
10797 /* See XEmbed Protocol Specification at
10798 http://freedesktop.org/wiki/Specifications/xembed-spec */
10799 if (FRAME_VISIBLE_P (f))
10800 xembed_send_message (f, CurrentTime,
10801 XEMBED_REQUEST_FOCUS, 0, 0, 0);
10802 }
10803
10804 /* Activate frame with Extended Window Manager Hints */
10805
10806 void
10807 x_ewmh_activate_frame (struct frame *f)
10808 {
10809 /* See Window Manager Specification/Extended Window Manager Hints at
10810 http://freedesktop.org/wiki/Specifications/wm-spec */
10811
10812 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10813
10814 if (FRAME_VISIBLE_P (f) && x_wm_supports (f, dpyinfo->Xatom_net_active_window))
10815 {
10816 Lisp_Object frame;
10817 XSETFRAME (frame, f);
10818 x_send_client_event (frame, make_number (0), frame,
10819 dpyinfo->Xatom_net_active_window,
10820 make_number (32),
10821 list2i (1, dpyinfo->last_user_time));
10822 }
10823 }
10824
10825 static void
10826 XTframe_raise_lower (struct frame *f, bool raise_flag)
10827 {
10828 if (raise_flag)
10829 x_raise_frame (f);
10830 else
10831 x_lower_frame (f);
10832 }
10833 \f
10834 /* XEmbed implementation. */
10835
10836 #if defined USE_X_TOOLKIT || ! defined USE_GTK
10837
10838 /* XEmbed implementation. */
10839
10840 #define XEMBED_VERSION 0
10841
10842 static void
10843 xembed_set_info (struct frame *f, enum xembed_info flags)
10844 {
10845 unsigned long data[2];
10846 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10847
10848 data[0] = XEMBED_VERSION;
10849 data[1] = flags;
10850
10851 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10852 dpyinfo->Xatom_XEMBED_INFO, dpyinfo->Xatom_XEMBED_INFO,
10853 32, PropModeReplace, (unsigned char *) data, 2);
10854 }
10855 #endif /* defined USE_X_TOOLKIT || ! defined USE_GTK */
10856
10857 static void
10858 xembed_send_message (struct frame *f, Time t, enum xembed_message msg,
10859 long int detail, long int data1, long int data2)
10860 {
10861 XEvent event;
10862
10863 event.xclient.type = ClientMessage;
10864 event.xclient.window = FRAME_X_OUTPUT (f)->parent_desc;
10865 event.xclient.message_type = FRAME_DISPLAY_INFO (f)->Xatom_XEMBED;
10866 event.xclient.format = 32;
10867 event.xclient.data.l[0] = t;
10868 event.xclient.data.l[1] = msg;
10869 event.xclient.data.l[2] = detail;
10870 event.xclient.data.l[3] = data1;
10871 event.xclient.data.l[4] = data2;
10872
10873 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_OUTPUT (f)->parent_desc,
10874 False, NoEventMask, &event);
10875 XSync (FRAME_X_DISPLAY (f), False);
10876 }
10877 \f
10878 /* Change of visibility. */
10879
10880 /* This tries to wait until the frame is really visible.
10881 However, if the window manager asks the user where to position
10882 the frame, this will return before the user finishes doing that.
10883 The frame will not actually be visible at that time,
10884 but it will become visible later when the window manager
10885 finishes with it. */
10886
10887 void
10888 x_make_frame_visible (struct frame *f)
10889 {
10890 int original_top, original_left;
10891 int tries = 0;
10892
10893 block_input ();
10894
10895 x_set_bitmap_icon (f);
10896
10897 if (! FRAME_VISIBLE_P (f))
10898 {
10899 /* We test FRAME_GARBAGED_P here to make sure we don't
10900 call x_set_offset a second time
10901 if we get to x_make_frame_visible a second time
10902 before the window gets really visible. */
10903 if (! FRAME_ICONIFIED_P (f)
10904 && ! FRAME_X_EMBEDDED_P (f)
10905 && ! f->output_data.x->asked_for_visible)
10906 x_set_offset (f, f->left_pos, f->top_pos, 0);
10907
10908 f->output_data.x->asked_for_visible = true;
10909
10910 if (! EQ (Vx_no_window_manager, Qt))
10911 x_wm_set_window_state (f, NormalState);
10912 #ifdef USE_X_TOOLKIT
10913 if (FRAME_X_EMBEDDED_P (f))
10914 xembed_set_info (f, XEMBED_MAPPED);
10915 else
10916 {
10917 /* This was XtPopup, but that did nothing for an iconified frame. */
10918 XtMapWidget (f->output_data.x->widget);
10919 }
10920 #else /* not USE_X_TOOLKIT */
10921 #ifdef USE_GTK
10922 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
10923 gtk_window_deiconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
10924 #else
10925 if (FRAME_X_EMBEDDED_P (f))
10926 xembed_set_info (f, XEMBED_MAPPED);
10927 else
10928 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
10929 #endif /* not USE_GTK */
10930 #endif /* not USE_X_TOOLKIT */
10931 }
10932
10933 XFlush (FRAME_X_DISPLAY (f));
10934
10935 /* Synchronize to ensure Emacs knows the frame is visible
10936 before we do anything else. We do this loop with input not blocked
10937 so that incoming events are handled. */
10938 {
10939 Lisp_Object frame;
10940 /* This must be before UNBLOCK_INPUT
10941 since events that arrive in response to the actions above
10942 will set it when they are handled. */
10943 bool previously_visible = f->output_data.x->has_been_visible;
10944
10945 original_left = f->left_pos;
10946 original_top = f->top_pos;
10947
10948 /* This must come after we set COUNT. */
10949 unblock_input ();
10950
10951 /* We unblock here so that arriving X events are processed. */
10952
10953 /* Now move the window back to where it was "supposed to be".
10954 But don't do it if the gravity is negative.
10955 When the gravity is negative, this uses a position
10956 that is 3 pixels too low. Perhaps that's really the border width.
10957
10958 Don't do this if the window has never been visible before,
10959 because the window manager may choose the position
10960 and we don't want to override it. */
10961
10962 if (! FRAME_VISIBLE_P (f)
10963 && ! FRAME_ICONIFIED_P (f)
10964 && ! FRAME_X_EMBEDDED_P (f)
10965 && f->win_gravity == NorthWestGravity
10966 && previously_visible)
10967 {
10968 Drawable rootw;
10969 int x, y;
10970 unsigned int width, height, border, depth;
10971
10972 block_input ();
10973
10974 /* On some window managers (such as FVWM) moving an existing
10975 window, even to the same place, causes the window manager
10976 to introduce an offset. This can cause the window to move
10977 to an unexpected location. Check the geometry (a little
10978 slow here) and then verify that the window is in the right
10979 place. If the window is not in the right place, move it
10980 there, and take the potential window manager hit. */
10981 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10982 &rootw, &x, &y, &width, &height, &border, &depth);
10983
10984 if (original_left != x || original_top != y)
10985 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10986 original_left, original_top);
10987
10988 unblock_input ();
10989 }
10990
10991 XSETFRAME (frame, f);
10992
10993 /* Process X events until a MapNotify event has been seen. */
10994 while (!FRAME_VISIBLE_P (f))
10995 {
10996 /* Force processing of queued events. */
10997 x_sync (f);
10998
10999 /* If on another desktop, the deiconify/map may be ignored and the
11000 frame never becomes visible. XMonad does this.
11001 Prevent an endless loop. */
11002 if (FRAME_ICONIFIED_P (f) && ++tries > 100)
11003 break;
11004
11005 /* This hack is still in use at least for Cygwin. See
11006 http://lists.gnu.org/archive/html/emacs-devel/2013-12/msg00351.html.
11007
11008 Machines that do polling rather than SIGIO have been
11009 observed to go into a busy-wait here. So we'll fake an
11010 alarm signal to let the handler know that there's something
11011 to be read. We used to raise a real alarm, but it seems
11012 that the handler isn't always enabled here. This is
11013 probably a bug. */
11014 if (input_polling_used ())
11015 {
11016 /* It could be confusing if a real alarm arrives while
11017 processing the fake one. Turn it off and let the
11018 handler reset it. */
11019 int old_poll_suppress_count = poll_suppress_count;
11020 poll_suppress_count = 1;
11021 poll_for_input_1 ();
11022 poll_suppress_count = old_poll_suppress_count;
11023 }
11024
11025 if (XPending (FRAME_X_DISPLAY (f)))
11026 {
11027 XEvent xev;
11028 XNextEvent (FRAME_X_DISPLAY (f), &xev);
11029 x_dispatch_event (&xev, FRAME_X_DISPLAY (f));
11030 }
11031 }
11032 }
11033 }
11034
11035 /* Change from mapped state to withdrawn state. */
11036
11037 /* Make the frame visible (mapped and not iconified). */
11038
11039 void
11040 x_make_frame_invisible (struct frame *f)
11041 {
11042 Window window;
11043
11044 /* Use the frame's outermost window, not the one we normally draw on. */
11045 window = FRAME_OUTER_WINDOW (f);
11046
11047 /* Don't keep the highlight on an invisible frame. */
11048 if (FRAME_DISPLAY_INFO (f)->x_highlight_frame == f)
11049 FRAME_DISPLAY_INFO (f)->x_highlight_frame = 0;
11050
11051 block_input ();
11052
11053 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
11054 that the current position of the window is user-specified, rather than
11055 program-specified, so that when the window is mapped again, it will be
11056 placed at the same location, without forcing the user to position it
11057 by hand again (they have already done that once for this window.) */
11058 x_wm_set_size_hint (f, 0, true);
11059
11060 #ifdef USE_GTK
11061 if (FRAME_GTK_OUTER_WIDGET (f))
11062 gtk_widget_hide (FRAME_GTK_OUTER_WIDGET (f));
11063 else
11064 #else
11065 if (FRAME_X_EMBEDDED_P (f))
11066 xembed_set_info (f, 0);
11067 else
11068 #endif
11069 {
11070
11071 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
11072 DefaultScreen (FRAME_X_DISPLAY (f))))
11073 {
11074 unblock_input ();
11075 error ("Can't notify window manager of window withdrawal");
11076 }
11077 }
11078
11079 /* We can't distinguish this from iconification
11080 just by the event that we get from the server.
11081 So we can't win using the usual strategy of letting
11082 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
11083 and synchronize with the server to make sure we agree. */
11084 SET_FRAME_VISIBLE (f, 0);
11085 SET_FRAME_ICONIFIED (f, false);
11086
11087 x_sync (f);
11088
11089 unblock_input ();
11090 }
11091
11092 /* Change window state from mapped to iconified. */
11093
11094 void
11095 x_iconify_frame (struct frame *f)
11096 {
11097 #ifdef USE_X_TOOLKIT
11098 int result;
11099 #endif
11100
11101 /* Don't keep the highlight on an invisible frame. */
11102 if (FRAME_DISPLAY_INFO (f)->x_highlight_frame == f)
11103 FRAME_DISPLAY_INFO (f)->x_highlight_frame = 0;
11104
11105 if (FRAME_ICONIFIED_P (f))
11106 return;
11107
11108 block_input ();
11109
11110 x_set_bitmap_icon (f);
11111
11112 #if defined (USE_GTK)
11113 if (FRAME_GTK_OUTER_WIDGET (f))
11114 {
11115 if (! FRAME_VISIBLE_P (f))
11116 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
11117
11118 gtk_window_iconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
11119 SET_FRAME_VISIBLE (f, 0);
11120 SET_FRAME_ICONIFIED (f, true);
11121 unblock_input ();
11122 return;
11123 }
11124 #endif
11125
11126 #ifdef USE_X_TOOLKIT
11127
11128 if (! FRAME_VISIBLE_P (f))
11129 {
11130 if (! EQ (Vx_no_window_manager, Qt))
11131 x_wm_set_window_state (f, IconicState);
11132 /* This was XtPopup, but that did nothing for an iconified frame. */
11133 XtMapWidget (f->output_data.x->widget);
11134 /* The server won't give us any event to indicate
11135 that an invisible frame was changed to an icon,
11136 so we have to record it here. */
11137 SET_FRAME_VISIBLE (f, 0);
11138 SET_FRAME_ICONIFIED (f, true);
11139 unblock_input ();
11140 return;
11141 }
11142
11143 result = XIconifyWindow (FRAME_X_DISPLAY (f),
11144 XtWindow (f->output_data.x->widget),
11145 DefaultScreen (FRAME_X_DISPLAY (f)));
11146 unblock_input ();
11147
11148 if (!result)
11149 error ("Can't notify window manager of iconification");
11150
11151 SET_FRAME_ICONIFIED (f, true);
11152 SET_FRAME_VISIBLE (f, 0);
11153
11154 block_input ();
11155 XFlush (FRAME_X_DISPLAY (f));
11156 unblock_input ();
11157 #else /* not USE_X_TOOLKIT */
11158
11159 /* Make sure the X server knows where the window should be positioned,
11160 in case the user deiconifies with the window manager. */
11161 if (! FRAME_VISIBLE_P (f)
11162 && ! FRAME_ICONIFIED_P (f)
11163 && ! FRAME_X_EMBEDDED_P (f))
11164 x_set_offset (f, f->left_pos, f->top_pos, 0);
11165
11166 /* Since we don't know which revision of X we're running, we'll use both
11167 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
11168
11169 /* X11R4: send a ClientMessage to the window manager using the
11170 WM_CHANGE_STATE type. */
11171 {
11172 XEvent msg;
11173
11174 msg.xclient.window = FRAME_X_WINDOW (f);
11175 msg.xclient.type = ClientMessage;
11176 msg.xclient.message_type = FRAME_DISPLAY_INFO (f)->Xatom_wm_change_state;
11177 msg.xclient.format = 32;
11178 msg.xclient.data.l[0] = IconicState;
11179
11180 if (! XSendEvent (FRAME_X_DISPLAY (f),
11181 DefaultRootWindow (FRAME_X_DISPLAY (f)),
11182 False,
11183 SubstructureRedirectMask | SubstructureNotifyMask,
11184 &msg))
11185 {
11186 unblock_input ();
11187 error ("Can't notify window manager of iconification");
11188 }
11189 }
11190
11191 /* X11R3: set the initial_state field of the window manager hints to
11192 IconicState. */
11193 x_wm_set_window_state (f, IconicState);
11194
11195 if (!FRAME_VISIBLE_P (f))
11196 {
11197 /* If the frame was withdrawn, before, we must map it. */
11198 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11199 }
11200
11201 SET_FRAME_ICONIFIED (f, true);
11202 SET_FRAME_VISIBLE (f, 0);
11203
11204 XFlush (FRAME_X_DISPLAY (f));
11205 unblock_input ();
11206 #endif /* not USE_X_TOOLKIT */
11207 }
11208
11209 \f
11210 /* Free X resources of frame F. */
11211
11212 void
11213 x_free_frame_resources (struct frame *f)
11214 {
11215 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
11216 Mouse_HLInfo *hlinfo = &dpyinfo->mouse_highlight;
11217 #ifdef USE_X_TOOLKIT
11218 Lisp_Object bar;
11219 struct scroll_bar *b;
11220 #endif
11221
11222 block_input ();
11223
11224 /* If a display connection is dead, don't try sending more
11225 commands to the X server. */
11226 if (dpyinfo->display)
11227 {
11228 /* Always exit with visible pointer to avoid weird issue
11229 with Xfixes (Bug#17609). */
11230 if (f->pointer_invisible)
11231 FRAME_DISPLAY_INFO (f)->toggle_visible_pointer (f, 0);
11232
11233 /* We must free faces before destroying windows because some
11234 font-driver (e.g. xft) access a window while finishing a
11235 face. */
11236 free_frame_faces (f);
11237
11238 if (f->output_data.x->icon_desc)
11239 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
11240
11241 #ifdef USE_X_TOOLKIT
11242 /* Explicitly destroy the scroll bars of the frame. Without
11243 this, we get "BadDrawable" errors from the toolkit later on,
11244 presumably from expose events generated for the disappearing
11245 toolkit scroll bars. */
11246 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
11247 {
11248 b = XSCROLL_BAR (bar);
11249 x_scroll_bar_remove (b);
11250 }
11251 #endif
11252
11253 #ifdef HAVE_X_I18N
11254 if (FRAME_XIC (f))
11255 free_frame_xic (f);
11256 #endif
11257
11258 x_free_cr_resources (f);
11259 #ifdef USE_X_TOOLKIT
11260 if (f->output_data.x->widget)
11261 {
11262 XtDestroyWidget (f->output_data.x->widget);
11263 f->output_data.x->widget = NULL;
11264 }
11265 /* Tooltips don't have widgets, only a simple X window, even if
11266 we are using a toolkit. */
11267 else if (FRAME_X_WINDOW (f))
11268 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11269
11270 free_frame_menubar (f);
11271
11272 if (f->shell_position)
11273 xfree (f->shell_position);
11274 #else /* !USE_X_TOOLKIT */
11275
11276 #ifdef USE_GTK
11277 xg_free_frame_widgets (f);
11278 #endif /* USE_GTK */
11279
11280 if (FRAME_X_WINDOW (f))
11281 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11282 #endif /* !USE_X_TOOLKIT */
11283
11284 unload_color (f, FRAME_FOREGROUND_PIXEL (f));
11285 unload_color (f, FRAME_BACKGROUND_PIXEL (f));
11286 unload_color (f, f->output_data.x->cursor_pixel);
11287 unload_color (f, f->output_data.x->cursor_foreground_pixel);
11288 unload_color (f, f->output_data.x->border_pixel);
11289 unload_color (f, f->output_data.x->mouse_pixel);
11290
11291 if (f->output_data.x->scroll_bar_background_pixel != -1)
11292 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
11293 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
11294 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
11295 #if defined (USE_LUCID) && defined (USE_TOOLKIT_SCROLL_BARS)
11296 /* Scrollbar shadow colors. */
11297 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
11298 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
11299 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
11300 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
11301 #endif /* USE_LUCID && USE_TOOLKIT_SCROLL_BARS */
11302 if (f->output_data.x->white_relief.pixel != -1)
11303 unload_color (f, f->output_data.x->white_relief.pixel);
11304 if (f->output_data.x->black_relief.pixel != -1)
11305 unload_color (f, f->output_data.x->black_relief.pixel);
11306
11307 x_free_gcs (f);
11308
11309 /* Free extra GCs allocated by x_setup_relief_colors. */
11310 if (f->output_data.x->white_relief.gc)
11311 {
11312 XFreeGC (dpyinfo->display, f->output_data.x->white_relief.gc);
11313 f->output_data.x->white_relief.gc = 0;
11314 }
11315 if (f->output_data.x->black_relief.gc)
11316 {
11317 XFreeGC (dpyinfo->display, f->output_data.x->black_relief.gc);
11318 f->output_data.x->black_relief.gc = 0;
11319 }
11320
11321 /* Free cursors. */
11322 if (f->output_data.x->text_cursor != 0)
11323 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->text_cursor);
11324 if (f->output_data.x->nontext_cursor != 0)
11325 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->nontext_cursor);
11326 if (f->output_data.x->modeline_cursor != 0)
11327 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->modeline_cursor);
11328 if (f->output_data.x->hand_cursor != 0)
11329 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->hand_cursor);
11330 if (f->output_data.x->hourglass_cursor != 0)
11331 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->hourglass_cursor);
11332 if (f->output_data.x->horizontal_drag_cursor != 0)
11333 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->horizontal_drag_cursor);
11334 if (f->output_data.x->vertical_drag_cursor != 0)
11335 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->vertical_drag_cursor);
11336
11337 XFlush (FRAME_X_DISPLAY (f));
11338 }
11339
11340 xfree (f->output_data.x->saved_menu_event);
11341 xfree (f->output_data.x);
11342 f->output_data.x = NULL;
11343
11344 if (f == dpyinfo->x_focus_frame)
11345 dpyinfo->x_focus_frame = 0;
11346 if (f == dpyinfo->x_focus_event_frame)
11347 dpyinfo->x_focus_event_frame = 0;
11348 if (f == dpyinfo->x_highlight_frame)
11349 dpyinfo->x_highlight_frame = 0;
11350 if (f == hlinfo->mouse_face_mouse_frame)
11351 reset_mouse_highlight (hlinfo);
11352
11353 unblock_input ();
11354 }
11355
11356
11357 /* Destroy the X window of frame F. */
11358
11359 static void
11360 x_destroy_window (struct frame *f)
11361 {
11362 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
11363
11364 /* If a display connection is dead, don't try sending more
11365 commands to the X server. */
11366 if (dpyinfo->display != 0)
11367 x_free_frame_resources (f);
11368
11369 dpyinfo->reference_count--;
11370 }
11371
11372 \f
11373 /* Setting window manager hints. */
11374
11375 /* Set the normal size hints for the window manager, for frame F.
11376 FLAGS is the flags word to use--or 0 meaning preserve the flags
11377 that the window now has.
11378 If USER_POSITION, set the USPosition
11379 flag (this is useful when FLAGS is 0).
11380 The GTK version is in gtkutils.c. */
11381
11382 #ifndef USE_GTK
11383 void
11384 x_wm_set_size_hint (struct frame *f, long flags, bool user_position)
11385 {
11386 XSizeHints size_hints;
11387 Window window = FRAME_OUTER_WINDOW (f);
11388
11389 if (!window)
11390 return;
11391
11392 #ifdef USE_X_TOOLKIT
11393 if (f->output_data.x->widget)
11394 {
11395 widget_update_wm_size_hints (f->output_data.x->widget);
11396 return;
11397 }
11398 #endif
11399
11400 /* Setting PMaxSize caused various problems. */
11401 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
11402
11403 size_hints.x = f->left_pos;
11404 size_hints.y = f->top_pos;
11405
11406 size_hints.width = FRAME_PIXEL_WIDTH (f);
11407 size_hints.height = FRAME_PIXEL_HEIGHT (f);
11408
11409 size_hints.width_inc = frame_resize_pixelwise ? 1 : FRAME_COLUMN_WIDTH (f);
11410 size_hints.height_inc = frame_resize_pixelwise ? 1 : FRAME_LINE_HEIGHT (f);
11411
11412 size_hints.max_width = x_display_pixel_width (FRAME_DISPLAY_INFO (f))
11413 - FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
11414 size_hints.max_height = x_display_pixel_height (FRAME_DISPLAY_INFO (f))
11415 - FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
11416
11417 /* Calculate the base and minimum sizes. */
11418 {
11419 int base_width, base_height;
11420
11421 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
11422 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
11423
11424 /* The window manager uses the base width hints to calculate the
11425 current number of rows and columns in the frame while
11426 resizing; min_width and min_height aren't useful for this
11427 purpose, since they might not give the dimensions for a
11428 zero-row, zero-column frame. */
11429
11430 size_hints.flags |= PBaseSize;
11431 size_hints.base_width = base_width;
11432 size_hints.base_height = base_height + FRAME_MENUBAR_HEIGHT (f);
11433 size_hints.min_width = base_width;
11434 size_hints.min_height = base_height;
11435 }
11436
11437 /* If we don't need the old flags, we don't need the old hint at all. */
11438 if (flags)
11439 {
11440 size_hints.flags |= flags;
11441 goto no_read;
11442 }
11443
11444 {
11445 XSizeHints hints; /* Sometimes I hate X Windows... */
11446 long supplied_return;
11447 int value;
11448
11449 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
11450 &supplied_return);
11451
11452 if (flags)
11453 size_hints.flags |= flags;
11454 else
11455 {
11456 if (value == 0)
11457 hints.flags = 0;
11458 if (hints.flags & PSize)
11459 size_hints.flags |= PSize;
11460 if (hints.flags & PPosition)
11461 size_hints.flags |= PPosition;
11462 if (hints.flags & USPosition)
11463 size_hints.flags |= USPosition;
11464 if (hints.flags & USSize)
11465 size_hints.flags |= USSize;
11466 }
11467 }
11468
11469 no_read:
11470
11471 #ifdef PWinGravity
11472 size_hints.win_gravity = f->win_gravity;
11473 size_hints.flags |= PWinGravity;
11474
11475 if (user_position)
11476 {
11477 size_hints.flags &= ~ PPosition;
11478 size_hints.flags |= USPosition;
11479 }
11480 #endif /* PWinGravity */
11481
11482 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
11483 }
11484 #endif /* not USE_GTK */
11485
11486 /* Used for IconicState or NormalState */
11487
11488 static void
11489 x_wm_set_window_state (struct frame *f, int state)
11490 {
11491 #ifdef USE_X_TOOLKIT
11492 Arg al[1];
11493
11494 XtSetArg (al[0], XtNinitialState, state);
11495 XtSetValues (f->output_data.x->widget, al, 1);
11496 #else /* not USE_X_TOOLKIT */
11497 Window window = FRAME_X_WINDOW (f);
11498
11499 f->output_data.x->wm_hints.flags |= StateHint;
11500 f->output_data.x->wm_hints.initial_state = state;
11501
11502 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
11503 #endif /* not USE_X_TOOLKIT */
11504 }
11505
11506 static void
11507 x_wm_set_icon_pixmap (struct frame *f, ptrdiff_t pixmap_id)
11508 {
11509 Pixmap icon_pixmap, icon_mask;
11510
11511 #if !defined USE_X_TOOLKIT && !defined USE_GTK
11512 Window window = FRAME_OUTER_WINDOW (f);
11513 #endif
11514
11515 if (pixmap_id > 0)
11516 {
11517 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
11518 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
11519 icon_mask = x_bitmap_mask (f, pixmap_id);
11520 f->output_data.x->wm_hints.icon_mask = icon_mask;
11521 }
11522 else
11523 {
11524 /* It seems there is no way to turn off use of an icon
11525 pixmap. */
11526 return;
11527 }
11528
11529
11530 #ifdef USE_GTK
11531 {
11532 xg_set_frame_icon (f, icon_pixmap, icon_mask);
11533 return;
11534 }
11535
11536 #elif defined (USE_X_TOOLKIT) /* same as in x_wm_set_window_state. */
11537
11538 {
11539 Arg al[1];
11540 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
11541 XtSetValues (f->output_data.x->widget, al, 1);
11542 XtSetArg (al[0], XtNiconMask, icon_mask);
11543 XtSetValues (f->output_data.x->widget, al, 1);
11544 }
11545
11546 #else /* not USE_X_TOOLKIT && not USE_GTK */
11547
11548 f->output_data.x->wm_hints.flags |= (IconPixmapHint | IconMaskHint);
11549 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
11550
11551 #endif /* not USE_X_TOOLKIT && not USE_GTK */
11552 }
11553
11554 void
11555 x_wm_set_icon_position (struct frame *f, int icon_x, int icon_y)
11556 {
11557 Window window = FRAME_OUTER_WINDOW (f);
11558
11559 f->output_data.x->wm_hints.flags |= IconPositionHint;
11560 f->output_data.x->wm_hints.icon_x = icon_x;
11561 f->output_data.x->wm_hints.icon_y = icon_y;
11562
11563 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
11564 }
11565
11566 \f
11567 /***********************************************************************
11568 Fonts
11569 ***********************************************************************/
11570
11571 #ifdef GLYPH_DEBUG
11572
11573 /* Check that FONT is valid on frame F. It is if it can be found in F's
11574 font table. */
11575
11576 static void
11577 x_check_font (struct frame *f, struct font *font)
11578 {
11579 eassert (font != NULL && ! NILP (font->props[FONT_TYPE_INDEX]));
11580 if (font->driver->check)
11581 eassert (font->driver->check (f, font) == 0);
11582 }
11583
11584 #endif /* GLYPH_DEBUG */
11585
11586 \f
11587 /***********************************************************************
11588 Initialization
11589 ***********************************************************************/
11590
11591 #ifdef USE_X_TOOLKIT
11592 static XrmOptionDescRec emacs_options[] = {
11593 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
11594 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
11595
11596 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
11597 XrmoptionSepArg, NULL},
11598 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
11599
11600 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
11601 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
11602 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
11603 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
11604 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
11605 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
11606 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
11607 };
11608
11609 /* Whether atimer for Xt timeouts is activated or not. */
11610
11611 static bool x_timeout_atimer_activated_flag;
11612
11613 #endif /* USE_X_TOOLKIT */
11614
11615 static int x_initialized;
11616
11617 /* Test whether two display-name strings agree up to the dot that separates
11618 the screen number from the server number. */
11619 static bool
11620 same_x_server (const char *name1, const char *name2)
11621 {
11622 bool seen_colon = false;
11623 Lisp_Object sysname = Fsystem_name ();
11624 const char *system_name = SSDATA (sysname);
11625 ptrdiff_t system_name_length = SBYTES (sysname);
11626 ptrdiff_t length_until_period = 0;
11627
11628 while (system_name[length_until_period] != 0
11629 && system_name[length_until_period] != '.')
11630 length_until_period++;
11631
11632 /* Treat `unix' like an empty host name. */
11633 if (! strncmp (name1, "unix:", 5))
11634 name1 += 4;
11635 if (! strncmp (name2, "unix:", 5))
11636 name2 += 4;
11637 /* Treat this host's name like an empty host name. */
11638 if (! strncmp (name1, system_name, system_name_length)
11639 && name1[system_name_length] == ':')
11640 name1 += system_name_length;
11641 if (! strncmp (name2, system_name, system_name_length)
11642 && name2[system_name_length] == ':')
11643 name2 += system_name_length;
11644 /* Treat this host's domainless name like an empty host name. */
11645 if (! strncmp (name1, system_name, length_until_period)
11646 && name1[length_until_period] == ':')
11647 name1 += length_until_period;
11648 if (! strncmp (name2, system_name, length_until_period)
11649 && name2[length_until_period] == ':')
11650 name2 += length_until_period;
11651
11652 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
11653 {
11654 if (*name1 == ':')
11655 seen_colon = true;
11656 if (seen_colon && *name1 == '.')
11657 return true;
11658 }
11659 return (seen_colon
11660 && (*name1 == '.' || *name1 == '\0')
11661 && (*name2 == '.' || *name2 == '\0'));
11662 }
11663
11664 /* Count number of set bits in mask and number of bits to shift to
11665 get to the first bit. With MASK 0x7e0, *BITS is set to 6, and *OFFSET
11666 to 5. */
11667 static void
11668 get_bits_and_offset (unsigned long mask, int *bits, int *offset)
11669 {
11670 int nr = 0;
11671 int off = 0;
11672
11673 while (!(mask & 1))
11674 {
11675 off++;
11676 mask >>= 1;
11677 }
11678
11679 while (mask & 1)
11680 {
11681 nr++;
11682 mask >>= 1;
11683 }
11684
11685 *offset = off;
11686 *bits = nr;
11687 }
11688
11689 /* Return true iff display DISPLAY is available for use.
11690 But don't permanently open it, just test its availability. */
11691
11692 bool
11693 x_display_ok (const char *display)
11694 {
11695 /* XOpenDisplay fails if it gets a signal. Block SIGIO which may arrive. */
11696 unrequest_sigio ();
11697 Display *dpy = XOpenDisplay (display);
11698 request_sigio ();
11699 if (!dpy)
11700 return false;
11701 XCloseDisplay (dpy);
11702 return true;
11703 }
11704
11705 #ifdef USE_GTK
11706 static void
11707 my_log_handler (const gchar *log_domain, GLogLevelFlags log_level,
11708 const gchar *msg, gpointer user_data)
11709 {
11710 if (!strstr (msg, "g_set_prgname"))
11711 fprintf (stderr, "%s-WARNING **: %s\n", log_domain, msg);
11712 }
11713 #endif
11714
11715 /* Create invisible cursor on X display referred by DPYINFO. */
11716
11717 static Cursor
11718 make_invisible_cursor (struct x_display_info *dpyinfo)
11719 {
11720 Display *dpy = dpyinfo->display;
11721 static char const no_data[] = { 0 };
11722 Pixmap pix;
11723 XColor col;
11724 Cursor c = 0;
11725
11726 x_catch_errors (dpy);
11727 pix = XCreateBitmapFromData (dpy, dpyinfo->root_window, no_data, 1, 1);
11728 if (! x_had_errors_p (dpy) && pix != None)
11729 {
11730 Cursor pixc;
11731 col.pixel = 0;
11732 col.red = col.green = col.blue = 0;
11733 col.flags = DoRed | DoGreen | DoBlue;
11734 pixc = XCreatePixmapCursor (dpy, pix, pix, &col, &col, 0, 0);
11735 if (! x_had_errors_p (dpy) && pixc != None)
11736 c = pixc;
11737 XFreePixmap (dpy, pix);
11738 }
11739
11740 x_uncatch_errors ();
11741
11742 return c;
11743 }
11744
11745 /* True if DPY supports Xfixes extension >= 4. */
11746
11747 static bool
11748 x_probe_xfixes_extension (Display *dpy)
11749 {
11750 #ifdef HAVE_XFIXES
11751 int major, minor;
11752 return XFixesQueryVersion (dpy, &major, &minor) && major >= 4;
11753 #else
11754 return false;
11755 #endif /* HAVE_XFIXES */
11756 }
11757
11758 /* Toggle mouse pointer visibility on frame F by using Xfixes functions. */
11759
11760 static void
11761 xfixes_toggle_visible_pointer (struct frame *f, bool invisible)
11762 {
11763 #ifdef HAVE_XFIXES
11764 if (invisible)
11765 XFixesHideCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11766 else
11767 XFixesShowCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11768 f->pointer_invisible = invisible;
11769 #else
11770 emacs_abort ();
11771 #endif /* HAVE_XFIXES */
11772 }
11773
11774 /* Toggle mouse pointer visibility on frame F by using invisible cursor. */
11775
11776 static void
11777 x_toggle_visible_pointer (struct frame *f, bool invisible)
11778 {
11779 eassert (FRAME_DISPLAY_INFO (f)->invisible_cursor != 0);
11780 if (invisible)
11781 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11782 FRAME_DISPLAY_INFO (f)->invisible_cursor);
11783 else
11784 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11785 f->output_data.x->current_cursor);
11786 f->pointer_invisible = invisible;
11787 }
11788
11789 /* Setup pointer blanking, prefer Xfixes if available. */
11790
11791 static void
11792 x_setup_pointer_blanking (struct x_display_info *dpyinfo)
11793 {
11794 /* FIXME: the brave tester should set EMACS_XFIXES because we're suspecting
11795 X server bug, see http://debbugs.gnu.org/cgi/bugreport.cgi?bug=17609. */
11796 if (egetenv ("EMACS_XFIXES") && x_probe_xfixes_extension (dpyinfo->display))
11797 dpyinfo->toggle_visible_pointer = xfixes_toggle_visible_pointer;
11798 else
11799 {
11800 dpyinfo->toggle_visible_pointer = x_toggle_visible_pointer;
11801 dpyinfo->invisible_cursor = make_invisible_cursor (dpyinfo);
11802 }
11803 }
11804
11805 /* Current X display connection identifier. Incremented for each next
11806 connection established. */
11807 static unsigned x_display_id;
11808
11809 /* Open a connection to X display DISPLAY_NAME, and return
11810 the structure that describes the open display.
11811 If we cannot contact the display, return null. */
11812
11813 struct x_display_info *
11814 x_term_init (Lisp_Object display_name, char *xrm_option, char *resource_name)
11815 {
11816 Display *dpy;
11817 struct terminal *terminal;
11818 struct x_display_info *dpyinfo;
11819 XrmDatabase xrdb;
11820 #ifdef USE_XCB
11821 xcb_connection_t *xcb_conn;
11822 #endif
11823
11824 block_input ();
11825
11826 if (!x_initialized)
11827 {
11828 x_initialize ();
11829 ++x_initialized;
11830 }
11831
11832 if (! x_display_ok (SSDATA (display_name)))
11833 error ("Display %s can't be opened", SSDATA (display_name));
11834
11835 #ifdef USE_GTK
11836 {
11837 #define NUM_ARGV 10
11838 int argc;
11839 char *argv[NUM_ARGV];
11840 char **argv2 = argv;
11841 guint id;
11842
11843 if (x_initialized++ > 1)
11844 {
11845 xg_display_open (SSDATA (display_name), &dpy);
11846 }
11847 else
11848 {
11849 static char display_opt[] = "--display";
11850 static char name_opt[] = "--name";
11851
11852 for (argc = 0; argc < NUM_ARGV; ++argc)
11853 argv[argc] = 0;
11854
11855 argc = 0;
11856 argv[argc++] = initial_argv[0];
11857
11858 if (! NILP (display_name))
11859 {
11860 argv[argc++] = display_opt;
11861 argv[argc++] = SSDATA (display_name);
11862 }
11863
11864 argv[argc++] = name_opt;
11865 argv[argc++] = resource_name;
11866
11867 XSetLocaleModifiers ("");
11868
11869 /* Work around GLib bug that outputs a faulty warning. See
11870 https://bugzilla.gnome.org/show_bug.cgi?id=563627. */
11871 id = g_log_set_handler ("GLib", G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL
11872 | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
11873
11874 /* NULL window -> events for all windows go to our function.
11875 Call before gtk_init so Gtk+ event filters comes after our. */
11876 gdk_window_add_filter (NULL, event_handler_gdk, NULL);
11877
11878 /* gtk_init does set_locale. Fix locale before and after. */
11879 fixup_locale ();
11880 unrequest_sigio (); /* See comment in x_display_ok. */
11881 gtk_init (&argc, &argv2);
11882 request_sigio ();
11883 fixup_locale ();
11884
11885 g_log_remove_handler ("GLib", id);
11886
11887 xg_initialize ();
11888
11889 dpy = DEFAULT_GDK_DISPLAY ();
11890
11891 #if ! GTK_CHECK_VERSION (2, 90, 0)
11892 /* Load our own gtkrc if it exists. */
11893 {
11894 const char *file = "~/.emacs.d/gtkrc";
11895 Lisp_Object s, abs_file;
11896
11897 s = build_string (file);
11898 abs_file = Fexpand_file_name (s, Qnil);
11899
11900 if (! NILP (abs_file) && !NILP (Ffile_readable_p (abs_file)))
11901 gtk_rc_parse (SSDATA (abs_file));
11902 }
11903 #endif
11904
11905 XSetErrorHandler (x_error_handler);
11906 XSetIOErrorHandler (x_io_error_quitter);
11907 }
11908 }
11909 #else /* not USE_GTK */
11910 #ifdef USE_X_TOOLKIT
11911 /* weiner@footloose.sps.mot.com reports that this causes
11912 errors with X11R5:
11913 X protocol error: BadAtom (invalid Atom parameter)
11914 on protocol request 18skiloaf.
11915 So let's not use it until R6. */
11916 #ifdef HAVE_X11XTR6
11917 XtSetLanguageProc (NULL, NULL, NULL);
11918 #endif
11919
11920 {
11921 int argc = 0;
11922 char *argv[3];
11923
11924 argv[0] = "";
11925 argc = 1;
11926 if (xrm_option)
11927 {
11928 argv[argc++] = "-xrm";
11929 argv[argc++] = xrm_option;
11930 }
11931 turn_on_atimers (false);
11932 unrequest_sigio (); /* See comment in x_display_ok. */
11933 dpy = XtOpenDisplay (Xt_app_con, SSDATA (display_name),
11934 resource_name, EMACS_CLASS,
11935 emacs_options, XtNumber (emacs_options),
11936 &argc, argv);
11937 request_sigio ();
11938 turn_on_atimers (true);
11939
11940 #ifdef HAVE_X11XTR6
11941 /* I think this is to compensate for XtSetLanguageProc. */
11942 fixup_locale ();
11943 #endif
11944 }
11945
11946 #else /* not USE_X_TOOLKIT */
11947 XSetLocaleModifiers ("");
11948 unrequest_sigio (); // See comment in x_display_ok.
11949 dpy = XOpenDisplay (SSDATA (display_name));
11950 request_sigio ();
11951 #endif /* not USE_X_TOOLKIT */
11952 #endif /* not USE_GTK*/
11953
11954 /* Detect failure. */
11955 if (dpy == 0)
11956 {
11957 unblock_input ();
11958 return 0;
11959 }
11960
11961 #ifdef USE_XCB
11962 xcb_conn = XGetXCBConnection (dpy);
11963 if (xcb_conn == 0)
11964 {
11965 #ifdef USE_GTK
11966 xg_display_close (dpy);
11967 #else
11968 #ifdef USE_X_TOOLKIT
11969 XtCloseDisplay (dpy);
11970 #else
11971 XCloseDisplay (dpy);
11972 #endif
11973 #endif /* ! USE_GTK */
11974
11975 unblock_input ();
11976 return 0;
11977 }
11978 #endif
11979
11980 /* We have definitely succeeded. Record the new connection. */
11981
11982 dpyinfo = xzalloc (sizeof *dpyinfo);
11983 terminal = x_create_terminal (dpyinfo);
11984
11985 {
11986 struct x_display_info *share;
11987
11988 for (share = x_display_list; share; share = share->next)
11989 if (same_x_server (SSDATA (XCAR (share->name_list_element)),
11990 SSDATA (display_name)))
11991 break;
11992 if (share)
11993 terminal->kboard = share->terminal->kboard;
11994 else
11995 {
11996 terminal->kboard = allocate_kboard (Qx);
11997
11998 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
11999 {
12000 char *vendor = ServerVendor (dpy);
12001
12002 /* Temporarily hide the partially initialized terminal. */
12003 terminal_list = terminal->next_terminal;
12004 unblock_input ();
12005 kset_system_key_alist
12006 (terminal->kboard,
12007 call1 (Qvendor_specific_keysyms,
12008 vendor ? build_string (vendor) : empty_unibyte_string));
12009 block_input ();
12010 terminal->next_terminal = terminal_list;
12011 terminal_list = terminal;
12012 }
12013
12014 /* Don't let the initial kboard remain current longer than necessary.
12015 That would cause problems if a file loaded on startup tries to
12016 prompt in the mini-buffer. */
12017 if (current_kboard == initial_kboard)
12018 current_kboard = terminal->kboard;
12019 }
12020 terminal->kboard->reference_count++;
12021 }
12022
12023 /* Put this display on the chain. */
12024 dpyinfo->next = x_display_list;
12025 x_display_list = dpyinfo;
12026
12027 dpyinfo->name_list_element = Fcons (display_name, Qnil);
12028 dpyinfo->display = dpy;
12029 dpyinfo->connection = ConnectionNumber (dpyinfo->display);
12030 #ifdef USE_XCB
12031 dpyinfo->xcb_connection = xcb_conn;
12032 #endif
12033
12034 /* http://lists.gnu.org/archive/html/emacs-devel/2015-11/msg00194.html */
12035 dpyinfo->smallest_font_height = 1;
12036 dpyinfo->smallest_char_width = 1;
12037
12038 /* Set the name of the terminal. */
12039 terminal->name = xlispstrdup (display_name);
12040
12041 #if false
12042 XSetAfterFunction (x_current_display, x_trace_wire);
12043 #endif
12044
12045 Lisp_Object system_name = Fsystem_name ();
12046 ptrdiff_t nbytes;
12047 if (INT_ADD_WRAPV (SBYTES (Vinvocation_name), SBYTES (system_name) + 2,
12048 &nbytes))
12049 memory_full (SIZE_MAX);
12050 dpyinfo->x_id = ++x_display_id;
12051 dpyinfo->x_id_name = xmalloc (nbytes);
12052 char *nametail = lispstpcpy (dpyinfo->x_id_name, Vinvocation_name);
12053 *nametail++ = '@';
12054 lispstpcpy (nametail, system_name);
12055
12056 /* Figure out which modifier bits mean what. */
12057 x_find_modifier_meanings (dpyinfo);
12058
12059 /* Get the scroll bar cursor. */
12060 #ifdef USE_GTK
12061 /* We must create a GTK cursor, it is required for GTK widgets. */
12062 dpyinfo->xg_cursor = xg_create_default_cursor (dpyinfo->display);
12063 #endif /* USE_GTK */
12064
12065 dpyinfo->vertical_scroll_bar_cursor
12066 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
12067
12068 dpyinfo->horizontal_scroll_bar_cursor
12069 = XCreateFontCursor (dpyinfo->display, XC_sb_h_double_arrow);
12070
12071 xrdb = x_load_resources (dpyinfo->display, xrm_option,
12072 resource_name, EMACS_CLASS);
12073 #ifdef HAVE_XRMSETDATABASE
12074 XrmSetDatabase (dpyinfo->display, xrdb);
12075 #else
12076 dpyinfo->display->db = xrdb;
12077 #endif
12078 /* Put the rdb where we can find it in a way that works on
12079 all versions. */
12080 dpyinfo->xrdb = xrdb;
12081
12082 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
12083 DefaultScreen (dpyinfo->display));
12084 select_visual (dpyinfo);
12085 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
12086 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
12087 dpyinfo->icon_bitmap_id = -1;
12088 dpyinfo->wm_type = X_WMTYPE_UNKNOWN;
12089
12090 reset_mouse_highlight (&dpyinfo->mouse_highlight);
12091
12092 /* See if we can construct pixel values from RGB values. */
12093 if (dpyinfo->visual->class == TrueColor)
12094 {
12095 get_bits_and_offset (dpyinfo->visual->red_mask,
12096 &dpyinfo->red_bits, &dpyinfo->red_offset);
12097 get_bits_and_offset (dpyinfo->visual->blue_mask,
12098 &dpyinfo->blue_bits, &dpyinfo->blue_offset);
12099 get_bits_and_offset (dpyinfo->visual->green_mask,
12100 &dpyinfo->green_bits, &dpyinfo->green_offset);
12101 }
12102
12103 /* See if a private colormap is requested. */
12104 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
12105 {
12106 if (dpyinfo->visual->class == PseudoColor)
12107 {
12108 AUTO_STRING (privateColormap, "privateColormap");
12109 AUTO_STRING (PrivateColormap, "PrivateColormap");
12110 Lisp_Object value
12111 = display_x_get_resource (dpyinfo, privateColormap,
12112 PrivateColormap, Qnil, Qnil);
12113 if (STRINGP (value)
12114 && (!strcmp (SSDATA (value), "true")
12115 || !strcmp (SSDATA (value), "on")))
12116 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
12117 }
12118 }
12119 else
12120 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
12121 dpyinfo->visual, AllocNone);
12122
12123 #ifdef HAVE_XFT
12124 {
12125 /* If we are using Xft, the following precautions should be made:
12126
12127 1. Make sure that the Xrender extension is added before the Xft one.
12128 Otherwise, the close-display hook set by Xft is called after the one
12129 for Xrender, and the former tries to re-add the latter. This results
12130 in inconsistency of internal states and leads to X protocol error when
12131 one reconnects to the same X server (Bug#1696).
12132
12133 2. Check dpi value in X resources. It is better we use it as well,
12134 since Xft will use it, as will all Gnome applications. If our real DPI
12135 is smaller or larger than the one Xft uses, our font will look smaller
12136 or larger than other for other applications, even if it is the same
12137 font name (monospace-10 for example). */
12138
12139 int event_base, error_base;
12140 char *v;
12141 double d;
12142
12143 XRenderQueryExtension (dpyinfo->display, &event_base, &error_base);
12144
12145 v = XGetDefault (dpyinfo->display, "Xft", "dpi");
12146 if (v != NULL && sscanf (v, "%lf", &d) == 1)
12147 dpyinfo->resy = dpyinfo->resx = d;
12148 }
12149 #endif
12150
12151 if (dpyinfo->resy < 1)
12152 {
12153 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
12154 double pixels = DisplayHeight (dpyinfo->display, screen_number);
12155 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
12156 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
12157 dpyinfo->resy = (mm < 1) ? 100 : pixels * 25.4 / mm;
12158 pixels = DisplayWidth (dpyinfo->display, screen_number);
12159 mm = DisplayWidthMM (dpyinfo->display, screen_number);
12160 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
12161 dpyinfo->resx = (mm < 1) ? 100 : pixels * 25.4 / mm;
12162 }
12163
12164 {
12165 static const struct
12166 {
12167 const char *name;
12168 int offset;
12169 } atom_refs[] = {
12170 #define ATOM_REFS_INIT(string, member) \
12171 { string, offsetof (struct x_display_info, member) },
12172 ATOM_REFS_INIT ("WM_PROTOCOLS", Xatom_wm_protocols)
12173 ATOM_REFS_INIT ("WM_TAKE_FOCUS", Xatom_wm_take_focus)
12174 ATOM_REFS_INIT ("WM_SAVE_YOURSELF", Xatom_wm_save_yourself)
12175 ATOM_REFS_INIT ("WM_DELETE_WINDOW", Xatom_wm_delete_window)
12176 ATOM_REFS_INIT ("WM_CHANGE_STATE", Xatom_wm_change_state)
12177 ATOM_REFS_INIT ("WM_CONFIGURE_DENIED", Xatom_wm_configure_denied)
12178 ATOM_REFS_INIT ("WM_MOVED", Xatom_wm_window_moved)
12179 ATOM_REFS_INIT ("WM_CLIENT_LEADER", Xatom_wm_client_leader)
12180 ATOM_REFS_INIT ("Editres", Xatom_editres)
12181 ATOM_REFS_INIT ("CLIPBOARD", Xatom_CLIPBOARD)
12182 ATOM_REFS_INIT ("TIMESTAMP", Xatom_TIMESTAMP)
12183 ATOM_REFS_INIT ("TEXT", Xatom_TEXT)
12184 ATOM_REFS_INIT ("COMPOUND_TEXT", Xatom_COMPOUND_TEXT)
12185 ATOM_REFS_INIT ("UTF8_STRING", Xatom_UTF8_STRING)
12186 ATOM_REFS_INIT ("DELETE", Xatom_DELETE)
12187 ATOM_REFS_INIT ("MULTIPLE", Xatom_MULTIPLE)
12188 ATOM_REFS_INIT ("INCR", Xatom_INCR)
12189 ATOM_REFS_INIT ("_EMACS_TMP_", Xatom_EMACS_TMP)
12190 ATOM_REFS_INIT ("TARGETS", Xatom_TARGETS)
12191 ATOM_REFS_INIT ("NULL", Xatom_NULL)
12192 ATOM_REFS_INIT ("ATOM", Xatom_ATOM)
12193 ATOM_REFS_INIT ("ATOM_PAIR", Xatom_ATOM_PAIR)
12194 ATOM_REFS_INIT ("CLIPBOARD_MANAGER", Xatom_CLIPBOARD_MANAGER)
12195 ATOM_REFS_INIT ("_XEMBED_INFO", Xatom_XEMBED_INFO)
12196 /* For properties of font. */
12197 ATOM_REFS_INIT ("PIXEL_SIZE", Xatom_PIXEL_SIZE)
12198 ATOM_REFS_INIT ("AVERAGE_WIDTH", Xatom_AVERAGE_WIDTH)
12199 ATOM_REFS_INIT ("_MULE_BASELINE_OFFSET", Xatom_MULE_BASELINE_OFFSET)
12200 ATOM_REFS_INIT ("_MULE_RELATIVE_COMPOSE", Xatom_MULE_RELATIVE_COMPOSE)
12201 ATOM_REFS_INIT ("_MULE_DEFAULT_ASCENT", Xatom_MULE_DEFAULT_ASCENT)
12202 /* Ghostscript support. */
12203 ATOM_REFS_INIT ("DONE", Xatom_DONE)
12204 ATOM_REFS_INIT ("PAGE", Xatom_PAGE)
12205 ATOM_REFS_INIT ("SCROLLBAR", Xatom_Scrollbar)
12206 ATOM_REFS_INIT ("HORIZONTAL_SCROLLBAR", Xatom_Horizontal_Scrollbar)
12207 ATOM_REFS_INIT ("_XEMBED", Xatom_XEMBED)
12208 /* EWMH */
12209 ATOM_REFS_INIT ("_NET_WM_STATE", Xatom_net_wm_state)
12210 ATOM_REFS_INIT ("_NET_WM_STATE_FULLSCREEN", Xatom_net_wm_state_fullscreen)
12211 ATOM_REFS_INIT ("_NET_WM_STATE_MAXIMIZED_HORZ",
12212 Xatom_net_wm_state_maximized_horz)
12213 ATOM_REFS_INIT ("_NET_WM_STATE_MAXIMIZED_VERT",
12214 Xatom_net_wm_state_maximized_vert)
12215 ATOM_REFS_INIT ("_NET_WM_STATE_STICKY", Xatom_net_wm_state_sticky)
12216 ATOM_REFS_INIT ("_NET_WM_STATE_HIDDEN", Xatom_net_wm_state_hidden)
12217 ATOM_REFS_INIT ("_NET_WM_WINDOW_TYPE", Xatom_net_window_type)
12218 ATOM_REFS_INIT ("_NET_WM_WINDOW_TYPE_TOOLTIP",
12219 Xatom_net_window_type_tooltip)
12220 ATOM_REFS_INIT ("_NET_WM_ICON_NAME", Xatom_net_wm_icon_name)
12221 ATOM_REFS_INIT ("_NET_WM_NAME", Xatom_net_wm_name)
12222 ATOM_REFS_INIT ("_NET_SUPPORTED", Xatom_net_supported)
12223 ATOM_REFS_INIT ("_NET_SUPPORTING_WM_CHECK", Xatom_net_supporting_wm_check)
12224 ATOM_REFS_INIT ("_NET_WM_WINDOW_OPACITY", Xatom_net_wm_window_opacity)
12225 ATOM_REFS_INIT ("_NET_ACTIVE_WINDOW", Xatom_net_active_window)
12226 ATOM_REFS_INIT ("_NET_FRAME_EXTENTS", Xatom_net_frame_extents)
12227 ATOM_REFS_INIT ("_NET_CURRENT_DESKTOP", Xatom_net_current_desktop)
12228 ATOM_REFS_INIT ("_NET_WORKAREA", Xatom_net_workarea)
12229 /* Session management */
12230 ATOM_REFS_INIT ("SM_CLIENT_ID", Xatom_SM_CLIENT_ID)
12231 ATOM_REFS_INIT ("_XSETTINGS_SETTINGS", Xatom_xsettings_prop)
12232 ATOM_REFS_INIT ("MANAGER", Xatom_xsettings_mgr)
12233 };
12234
12235 int i;
12236 enum { atom_count = ARRAYELTS (atom_refs) };
12237 /* 1 for _XSETTINGS_SN. */
12238 enum { total_atom_count = 1 + atom_count };
12239 Atom atoms_return[total_atom_count];
12240 char *atom_names[total_atom_count];
12241 static char const xsettings_fmt[] = "_XSETTINGS_S%d";
12242 char xsettings_atom_name[sizeof xsettings_fmt - 2
12243 + INT_STRLEN_BOUND (int)];
12244
12245 for (i = 0; i < atom_count; i++)
12246 atom_names[i] = (char *) atom_refs[i].name;
12247
12248 /* Build _XSETTINGS_SN atom name. */
12249 sprintf (xsettings_atom_name, xsettings_fmt,
12250 XScreenNumberOfScreen (dpyinfo->screen));
12251 atom_names[i] = xsettings_atom_name;
12252
12253 XInternAtoms (dpyinfo->display, atom_names, total_atom_count,
12254 False, atoms_return);
12255
12256 for (i = 0; i < atom_count; i++)
12257 *(Atom *) ((char *) dpyinfo + atom_refs[i].offset) = atoms_return[i];
12258
12259 /* Manually copy last atom. */
12260 dpyinfo->Xatom_xsettings_sel = atoms_return[i];
12261 }
12262
12263 dpyinfo->x_dnd_atoms_size = 8;
12264 dpyinfo->x_dnd_atoms = xmalloc (sizeof *dpyinfo->x_dnd_atoms
12265 * dpyinfo->x_dnd_atoms_size);
12266 dpyinfo->gray
12267 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
12268 gray_bits, gray_width, gray_height,
12269 1, 0, 1);
12270
12271 x_setup_pointer_blanking (dpyinfo);
12272
12273 #ifdef HAVE_X_I18N
12274 xim_initialize (dpyinfo, resource_name);
12275 #endif
12276
12277 xsettings_initialize (dpyinfo);
12278
12279 /* This is only needed for distinguishing keyboard and process input. */
12280 if (dpyinfo->connection != 0)
12281 add_keyboard_wait_descriptor (dpyinfo->connection);
12282
12283 #ifdef F_SETOWN
12284 fcntl (dpyinfo->connection, F_SETOWN, getpid ());
12285 #endif /* ! defined (F_SETOWN) */
12286
12287 if (interrupt_input)
12288 init_sigio (dpyinfo->connection);
12289
12290 #ifdef USE_LUCID
12291 {
12292 XrmValue d, fr, to;
12293 Font font;
12294
12295 dpy = dpyinfo->display;
12296 d.addr = (XPointer)&dpy;
12297 d.size = sizeof (Display *);
12298 fr.addr = XtDefaultFont;
12299 fr.size = sizeof (XtDefaultFont);
12300 to.size = sizeof (Font *);
12301 to.addr = (XPointer)&font;
12302 x_catch_errors (dpy);
12303 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
12304 emacs_abort ();
12305 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
12306 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
12307 /* Do not free XFontStruct returned by the above call to XQueryFont.
12308 This leads to X protocol errors at XtCloseDisplay (Bug#18403). */
12309 x_uncatch_errors ();
12310 }
12311 #endif
12312
12313 /* See if we should run in synchronous mode. This is useful
12314 for debugging X code. */
12315 {
12316 AUTO_STRING (synchronous, "synchronous");
12317 AUTO_STRING (Synchronous, "Synchronous");
12318 Lisp_Object value = display_x_get_resource (dpyinfo, synchronous,
12319 Synchronous, Qnil, Qnil);
12320 if (STRINGP (value)
12321 && (!strcmp (SSDATA (value), "true")
12322 || !strcmp (SSDATA (value), "on")))
12323 XSynchronize (dpyinfo->display, True);
12324 }
12325
12326 {
12327 AUTO_STRING (useXIM, "useXIM");
12328 AUTO_STRING (UseXIM, "UseXIM");
12329 Lisp_Object value = display_x_get_resource (dpyinfo, useXIM, UseXIM,
12330 Qnil, Qnil);
12331 #ifdef USE_XIM
12332 if (STRINGP (value)
12333 && (!strcmp (SSDATA (value), "false")
12334 || !strcmp (SSDATA (value), "off")))
12335 use_xim = false;
12336 #else
12337 if (STRINGP (value)
12338 && (!strcmp (SSDATA (value), "true")
12339 || !strcmp (SSDATA (value), "on")))
12340 use_xim = true;
12341 #endif
12342 }
12343
12344 #ifdef HAVE_X_SM
12345 /* Only do this for the very first display in the Emacs session.
12346 Ignore X session management when Emacs was first started on a
12347 tty or started as a daemon. */
12348 if (terminal->id == 1 && ! IS_DAEMON)
12349 x_session_initialize (dpyinfo);
12350 #endif
12351
12352 #ifdef USE_CAIRO
12353 x_extension_initialize (dpyinfo);
12354 #endif
12355
12356 unblock_input ();
12357
12358 return dpyinfo;
12359 }
12360 \f
12361 /* Get rid of display DPYINFO, deleting all frames on it,
12362 and without sending any more commands to the X server. */
12363
12364 static void
12365 x_delete_display (struct x_display_info *dpyinfo)
12366 {
12367 struct terminal *t;
12368 struct color_name_cache_entry *color_entry, *next_color_entry;
12369
12370 /* Close all frames and delete the generic struct terminal for this
12371 X display. */
12372 for (t = terminal_list; t; t = t->next_terminal)
12373 if (t->type == output_x_window && t->display_info.x == dpyinfo)
12374 {
12375 #ifdef HAVE_X_SM
12376 /* Close X session management when we close its display. */
12377 if (t->id == 1 && x_session_have_connection ())
12378 x_session_close ();
12379 #endif
12380 delete_terminal (t);
12381 break;
12382 }
12383
12384 if (next_noop_dpyinfo == dpyinfo)
12385 next_noop_dpyinfo = dpyinfo->next;
12386
12387 if (x_display_list == dpyinfo)
12388 x_display_list = dpyinfo->next;
12389 else
12390 {
12391 struct x_display_info *tail;
12392
12393 for (tail = x_display_list; tail; tail = tail->next)
12394 if (tail->next == dpyinfo)
12395 tail->next = tail->next->next;
12396 }
12397
12398 for (color_entry = dpyinfo->color_names;
12399 color_entry;
12400 color_entry = next_color_entry)
12401 {
12402 next_color_entry = color_entry->next;
12403 xfree (color_entry->name);
12404 xfree (color_entry);
12405 }
12406
12407 xfree (dpyinfo->x_id_name);
12408 xfree (dpyinfo->x_dnd_atoms);
12409 xfree (dpyinfo->color_cells);
12410 xfree (dpyinfo);
12411 }
12412
12413 #ifdef USE_X_TOOLKIT
12414
12415 /* Atimer callback function for TIMER. Called every 0.1s to process
12416 Xt timeouts, if needed. We must avoid calling XtAppPending as
12417 much as possible because that function does an implicit XFlush
12418 that slows us down. */
12419
12420 static void
12421 x_process_timeouts (struct atimer *timer)
12422 {
12423 block_input ();
12424 x_timeout_atimer_activated_flag = false;
12425 if (toolkit_scroll_bar_interaction || popup_activated ())
12426 {
12427 while (XtAppPending (Xt_app_con) & XtIMTimer)
12428 XtAppProcessEvent (Xt_app_con, XtIMTimer);
12429 /* Reactivate the atimer for next time. */
12430 x_activate_timeout_atimer ();
12431 }
12432 unblock_input ();
12433 }
12434
12435 /* Install an asynchronous timer that processes Xt timeout events
12436 every 0.1s as long as either `toolkit_scroll_bar_interaction' or
12437 `popup_activated_flag' (in xmenu.c) is set. Make sure to call this
12438 function whenever these variables are set. This is necessary
12439 because some widget sets use timeouts internally, for example the
12440 LessTif menu bar, or the Xaw3d scroll bar. When Xt timeouts aren't
12441 processed, these widgets don't behave normally. */
12442
12443 void
12444 x_activate_timeout_atimer (void)
12445 {
12446 block_input ();
12447 if (!x_timeout_atimer_activated_flag)
12448 {
12449 struct timespec interval = make_timespec (0, 100 * 1000 * 1000);
12450 start_atimer (ATIMER_RELATIVE, interval, x_process_timeouts, 0);
12451 x_timeout_atimer_activated_flag = true;
12452 }
12453 unblock_input ();
12454 }
12455
12456 #endif /* USE_X_TOOLKIT */
12457
12458 \f
12459 /* Set up use of X before we make the first connection. */
12460
12461 static struct redisplay_interface x_redisplay_interface =
12462 {
12463 x_frame_parm_handlers,
12464 x_produce_glyphs,
12465 x_write_glyphs,
12466 x_insert_glyphs,
12467 x_clear_end_of_line,
12468 x_scroll_run,
12469 x_after_update_window_line,
12470 x_update_window_begin,
12471 x_update_window_end,
12472 x_flush,
12473 x_clear_window_mouse_face,
12474 x_get_glyph_overhangs,
12475 x_fix_overlapping_area,
12476 x_draw_fringe_bitmap,
12477 #ifdef USE_CAIRO
12478 x_cr_define_fringe_bitmap,
12479 x_cr_destroy_fringe_bitmap,
12480 #else
12481 0, /* define_fringe_bitmap */
12482 0, /* destroy_fringe_bitmap */
12483 #endif
12484 x_compute_glyph_string_overhangs,
12485 x_draw_glyph_string,
12486 x_define_frame_cursor,
12487 x_clear_frame_area,
12488 x_draw_window_cursor,
12489 x_draw_vertical_window_border,
12490 x_draw_window_divider,
12491 x_shift_glyphs_for_insert, /* Never called; see comment in function. */
12492 x_show_hourglass,
12493 x_hide_hourglass
12494 };
12495
12496
12497 /* This function is called when the last frame on a display is deleted. */
12498 void
12499 x_delete_terminal (struct terminal *terminal)
12500 {
12501 struct x_display_info *dpyinfo = terminal->display_info.x;
12502
12503 /* Protect against recursive calls. delete_frame in
12504 delete_terminal calls us back when it deletes our last frame. */
12505 if (!terminal->name)
12506 return;
12507
12508 block_input ();
12509 #ifdef HAVE_X_I18N
12510 /* We must close our connection to the XIM server before closing the
12511 X display. */
12512 if (dpyinfo->xim)
12513 xim_close_dpy (dpyinfo);
12514 #endif
12515
12516 /* Normally, the display is available... */
12517 if (dpyinfo->display)
12518 {
12519 x_destroy_all_bitmaps (dpyinfo);
12520 XSetCloseDownMode (dpyinfo->display, DestroyAll);
12521
12522 /* Whether or not XCloseDisplay destroys the associated resource
12523 database depends on the version of libX11. To avoid both
12524 crash and memory leak, we dissociate the database from the
12525 display and then destroy dpyinfo->xrdb ourselves.
12526
12527 Unfortunately, the above strategy does not work in some
12528 situations due to a bug in newer versions of libX11: because
12529 XrmSetDatabase doesn't clear the flag XlibDisplayDfltRMDB if
12530 dpy->db is NULL, XCloseDisplay destroys the associated
12531 database whereas it has not been created by XGetDefault
12532 (Bug#21974 in freedesktop.org Bugzilla). As a workaround, we
12533 don't destroy the database here in order to avoid the crash
12534 in the above situations for now, though that may cause memory
12535 leaks in other situations. */
12536 #if false
12537 #ifdef HAVE_XRMSETDATABASE
12538 XrmSetDatabase (dpyinfo->display, NULL);
12539 #else
12540 dpyinfo->display->db = NULL;
12541 #endif
12542 /* We used to call XrmDestroyDatabase from x_delete_display, but
12543 some older versions of libX11 crash if we call it after
12544 closing all the displays. */
12545 XrmDestroyDatabase (dpyinfo->xrdb);
12546 #endif
12547
12548 #ifdef USE_GTK
12549 xg_display_close (dpyinfo->display);
12550 #else
12551 #ifdef USE_X_TOOLKIT
12552 XtCloseDisplay (dpyinfo->display);
12553 #else
12554 XCloseDisplay (dpyinfo->display);
12555 #endif
12556 #endif /* ! USE_GTK */
12557 /* Do not close the connection here because it's already closed
12558 by X(t)CloseDisplay (Bug#18403). */
12559 dpyinfo->display = NULL;
12560 }
12561
12562 /* ...but if called from x_connection_closed, the display may already
12563 be closed and dpyinfo->display was set to 0 to indicate that. Since
12564 X server is most likely gone, explicit close is the only reliable
12565 way to continue and avoid Bug#19147. */
12566 else if (dpyinfo->connection >= 0)
12567 emacs_close (dpyinfo->connection);
12568
12569 /* No more input on this descriptor. */
12570 delete_keyboard_wait_descriptor (dpyinfo->connection);
12571 /* Mark as dead. */
12572 dpyinfo->connection = -1;
12573
12574 x_delete_display (dpyinfo);
12575 unblock_input ();
12576 }
12577
12578 /* Create a struct terminal, initialize it with the X11 specific
12579 functions and make DISPLAY->TERMINAL point to it. */
12580
12581 static struct terminal *
12582 x_create_terminal (struct x_display_info *dpyinfo)
12583 {
12584 struct terminal *terminal;
12585
12586 terminal = create_terminal (output_x_window, &x_redisplay_interface);
12587
12588 terminal->display_info.x = dpyinfo;
12589 dpyinfo->terminal = terminal;
12590
12591 /* kboard is initialized in x_term_init. */
12592
12593 terminal->clear_frame_hook = x_clear_frame;
12594 terminal->ins_del_lines_hook = x_ins_del_lines;
12595 terminal->delete_glyphs_hook = x_delete_glyphs;
12596 terminal->ring_bell_hook = XTring_bell;
12597 terminal->toggle_invisible_pointer_hook = XTtoggle_invisible_pointer;
12598 terminal->update_begin_hook = x_update_begin;
12599 terminal->update_end_hook = x_update_end;
12600 terminal->read_socket_hook = XTread_socket;
12601 terminal->frame_up_to_date_hook = XTframe_up_to_date;
12602 terminal->mouse_position_hook = XTmouse_position;
12603 terminal->frame_rehighlight_hook = XTframe_rehighlight;
12604 terminal->frame_raise_lower_hook = XTframe_raise_lower;
12605 terminal->fullscreen_hook = XTfullscreen_hook;
12606 terminal->menu_show_hook = x_menu_show;
12607 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
12608 terminal->popup_dialog_hook = xw_popup_dialog;
12609 #endif
12610 terminal->set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
12611 terminal->set_horizontal_scroll_bar_hook = XTset_horizontal_scroll_bar;
12612 terminal->condemn_scroll_bars_hook = XTcondemn_scroll_bars;
12613 terminal->redeem_scroll_bar_hook = XTredeem_scroll_bar;
12614 terminal->judge_scroll_bars_hook = XTjudge_scroll_bars;
12615 terminal->delete_frame_hook = x_destroy_window;
12616 terminal->delete_terminal_hook = x_delete_terminal;
12617 /* Other hooks are NULL by default. */
12618
12619 return terminal;
12620 }
12621
12622 static void
12623 x_initialize (void)
12624 {
12625 baud_rate = 19200;
12626
12627 x_noop_count = 0;
12628 any_help_event_p = false;
12629 ignore_next_mouse_click_timeout = 0;
12630
12631 #ifdef USE_GTK
12632 current_count = -1;
12633 #endif
12634
12635 /* Try to use interrupt input; if we can't, then start polling. */
12636 Fset_input_interrupt_mode (Qt);
12637
12638 #ifdef USE_X_TOOLKIT
12639 XtToolkitInitialize ();
12640
12641 Xt_app_con = XtCreateApplicationContext ();
12642
12643 /* Register a converter from strings to pixels, which uses
12644 Emacs' color allocation infrastructure. */
12645 XtAppSetTypeConverter (Xt_app_con,
12646 XtRString, XtRPixel, cvt_string_to_pixel,
12647 cvt_string_to_pixel_args,
12648 XtNumber (cvt_string_to_pixel_args),
12649 XtCacheByDisplay, cvt_pixel_dtor);
12650
12651 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
12652 #endif
12653
12654 #ifdef USE_TOOLKIT_SCROLL_BARS
12655 #ifndef USE_GTK
12656 xaw3d_arrow_scroll = False;
12657 xaw3d_pick_top = True;
12658 #endif
12659 #endif
12660
12661 #ifdef USE_CAIRO
12662 x_cr_init_fringe (&x_redisplay_interface);
12663 #endif
12664
12665 /* Note that there is no real way portable across R3/R4 to get the
12666 original error handler. */
12667 XSetErrorHandler (x_error_handler);
12668 XSetIOErrorHandler (x_io_error_quitter);
12669 }
12670
12671 #ifdef USE_GTK
12672 void
12673 init_xterm (void)
12674 {
12675 /* Emacs can handle only core input events, so make sure
12676 Gtk doesn't use Xinput or Xinput2 extensions. */
12677 xputenv ("GDK_CORE_DEVICE_EVENTS=1");
12678 }
12679 #endif
12680
12681 void
12682 syms_of_xterm (void)
12683 {
12684 x_error_message = NULL;
12685
12686 DEFSYM (Qvendor_specific_keysyms, "vendor-specific-keysyms");
12687 DEFSYM (Qlatin_1, "latin-1");
12688
12689 #ifdef USE_GTK
12690 xg_default_icon_file = build_pure_c_string ("icons/hicolor/scalable/apps/emacs.svg");
12691 staticpro (&xg_default_icon_file);
12692
12693 DEFSYM (Qx_gtk_map_stock, "x-gtk-map-stock");
12694 #endif
12695
12696 DEFVAR_BOOL ("x-use-underline-position-properties",
12697 x_use_underline_position_properties,
12698 doc: /* Non-nil means make use of UNDERLINE_POSITION font properties.
12699 A value of nil means ignore them. If you encounter fonts with bogus
12700 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
12701 to 4.1, set this to nil. You can also use `underline-minimum-offset'
12702 to override the font's UNDERLINE_POSITION for small font display
12703 sizes. */);
12704 x_use_underline_position_properties = true;
12705
12706 DEFVAR_BOOL ("x-underline-at-descent-line",
12707 x_underline_at_descent_line,
12708 doc: /* Non-nil means to draw the underline at the same place as the descent line.
12709 A value of nil means to draw the underline according to the value of the
12710 variable `x-use-underline-position-properties', which is usually at the
12711 baseline level. The default value is nil. */);
12712 x_underline_at_descent_line = false;
12713
12714 DEFVAR_BOOL ("x-mouse-click-focus-ignore-position",
12715 x_mouse_click_focus_ignore_position,
12716 doc: /* Non-nil means that a mouse click to focus a frame does not move point.
12717 This variable is only used when the window manager requires that you
12718 click on a frame to select it (give it focus). In that case, a value
12719 of nil, means that the selected window and cursor position changes to
12720 reflect the mouse click position, while a non-nil value means that the
12721 selected window or cursor position is preserved. */);
12722 x_mouse_click_focus_ignore_position = false;
12723
12724 DEFVAR_LISP ("x-toolkit-scroll-bars", Vx_toolkit_scroll_bars,
12725 doc: /* Which toolkit scroll bars Emacs uses, if any.
12726 A value of nil means Emacs doesn't use toolkit scroll bars.
12727 With the X Window system, the value is a symbol describing the
12728 X toolkit. Possible values are: gtk, motif, xaw, or xaw3d.
12729 With MS Windows or Nextstep, the value is t. */);
12730 #ifdef USE_TOOLKIT_SCROLL_BARS
12731 #ifdef USE_MOTIF
12732 Vx_toolkit_scroll_bars = intern_c_string ("motif");
12733 #elif defined HAVE_XAW3D
12734 Vx_toolkit_scroll_bars = intern_c_string ("xaw3d");
12735 #elif USE_GTK
12736 Vx_toolkit_scroll_bars = intern_c_string ("gtk");
12737 #else
12738 Vx_toolkit_scroll_bars = intern_c_string ("xaw");
12739 #endif
12740 #else
12741 Vx_toolkit_scroll_bars = Qnil;
12742 #endif
12743
12744 DEFSYM (Qmodifier_value, "modifier-value");
12745 DEFSYM (Qalt, "alt");
12746 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
12747 DEFSYM (Qhyper, "hyper");
12748 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
12749 DEFSYM (Qmeta, "meta");
12750 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
12751 DEFSYM (Qsuper, "super");
12752 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
12753
12754 DEFVAR_LISP ("x-alt-keysym", Vx_alt_keysym,
12755 doc: /* Which keys Emacs uses for the alt modifier.
12756 This should be one of the symbols `alt', `hyper', `meta', `super'.
12757 For example, `alt' means use the Alt_L and Alt_R keysyms. The default
12758 is nil, which is the same as `alt'. */);
12759 Vx_alt_keysym = Qnil;
12760
12761 DEFVAR_LISP ("x-hyper-keysym", Vx_hyper_keysym,
12762 doc: /* Which keys Emacs uses for the hyper modifier.
12763 This should be one of the symbols `alt', `hyper', `meta', `super'.
12764 For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
12765 default is nil, which is the same as `hyper'. */);
12766 Vx_hyper_keysym = Qnil;
12767
12768 DEFVAR_LISP ("x-meta-keysym", Vx_meta_keysym,
12769 doc: /* Which keys Emacs uses for the meta modifier.
12770 This should be one of the symbols `alt', `hyper', `meta', `super'.
12771 For example, `meta' means use the Meta_L and Meta_R keysyms. The
12772 default is nil, which is the same as `meta'. */);
12773 Vx_meta_keysym = Qnil;
12774
12775 DEFVAR_LISP ("x-super-keysym", Vx_super_keysym,
12776 doc: /* Which keys Emacs uses for the super modifier.
12777 This should be one of the symbols `alt', `hyper', `meta', `super'.
12778 For example, `super' means use the Super_L and Super_R keysyms. The
12779 default is nil, which is the same as `super'. */);
12780 Vx_super_keysym = Qnil;
12781
12782 DEFVAR_LISP ("x-keysym-table", Vx_keysym_table,
12783 doc: /* Hash table of character codes indexed by X keysym codes. */);
12784 Vx_keysym_table = make_hash_table (hashtest_eql, make_number (900),
12785 make_float (DEFAULT_REHASH_SIZE),
12786 make_float (DEFAULT_REHASH_THRESHOLD),
12787 Qnil);
12788
12789 DEFVAR_BOOL ("x-frame-normalize-before-maximize",
12790 x_frame_normalize_before_maximize,
12791 doc: /* Non-nil means normalize frame before maximizing.
12792 If this variable is t, Emacs first asks the window manager to give the
12793 frame its normal size, and only then the final state, whenever changing
12794 from a full-height, full-width or full-both state to the maximized one
12795 or when changing from the maximized to the full-height or full-width
12796 state.
12797
12798 Set this variable only if your window manager cannot handle the
12799 transition between the various maximization states. */);
12800 x_frame_normalize_before_maximize = false;
12801 }