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