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