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