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