Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Tree: 422281c0da
Fetching contributors…

Cannot retrieve contributors at this time

11124 lines (9257 sloc) 329.193 kB
/* X Communication module for terminals which understand the X protocol.
Copyright (C) 1989, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
Free Software Foundation, Inc.
This file is part of GNU Emacs.
GNU Emacs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
/* New display code by Gerd Moellmann <gerd@gnu.org>. */
/* Xt features made by Fred Pierresteguy. */
#include <config.h>
/* On 4.3 these lose if they come after xterm.h. */
/* Putting these at the beginning seems to be standard for other .c files. */
#include <signal.h>
#include <stdio.h>
#include <setjmp.h>
#ifdef HAVE_X_WINDOWS
#include "lisp.h"
#include "blockinput.h"
/* Need syssignal.h for various externs and definitions that may be required
by some configurations for calls to signal later in this source file. */
#include "syssignal.h"
/* This may include sys/types.h, and that somehow loses
if this is not done before the other system files. */
#include "xterm.h"
#include <X11/cursorfont.h>
/* Load sys/types.h if not already loaded.
In some systems loading it twice is suicidal. */
#ifndef makedev
#include <sys/types.h>
#endif /* makedev */
#ifdef BSD_SYSTEM
#include <sys/ioctl.h>
#endif /* ! defined (BSD_SYSTEM) */
#include "systime.h"
#ifndef INCLUDED_FCNTL
#include <fcntl.h>
#endif
#include <ctype.h>
#include <errno.h>
#include <setjmp.h>
#include <sys/stat.h>
/* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
/* #include <sys/param.h> */
#include "charset.h"
#include "character.h"
#include "coding.h"
#include "frame.h"
#include "dispextern.h"
#include "fontset.h"
#include "termhooks.h"
#include "termopts.h"
#include "termchar.h"
#include "emacs-icon.h"
#include "disptab.h"
#include "buffer.h"
#include "window.h"
#include "keyboard.h"
#include "intervals.h"
#include "process.h"
#include "atimer.h"
#include "keymap.h"
#include "font.h"
#include "fontset.h"
#include "xsettings.h"
#include "xgselect.h"
#include "sysselect.h"
#ifdef USE_X_TOOLKIT
#include <X11/Shell.h>
#endif
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef USE_GTK
#include "gtkutil.h"
#endif
#ifdef USE_LUCID
extern int xlwmenu_window_p P_ ((Widget w, Window window));
extern void xlwmenu_redisplay P_ ((Widget));
#endif
#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
extern void free_frame_menubar P_ ((struct frame *));
#endif
#ifdef USE_X_TOOLKIT
#if !defined(NO_EDITRES)
#define HACK_EDITRES
extern void _XEditResCheckMessages ();
#endif /* not NO_EDITRES */
/* Include toolkit specific headers for the scroll bar widget. */
#ifdef USE_TOOLKIT_SCROLL_BARS
#if defined USE_MOTIF
#include <Xm/Xm.h> /* for LESSTIF_VERSION */
#include <Xm/ScrollBar.h>
#else /* !USE_MOTIF i.e. use Xaw */
#ifdef HAVE_XAW3D
#include <X11/Xaw3d/Simple.h>
#include <X11/Xaw3d/Scrollbar.h>
#include <X11/Xaw3d/ThreeD.h>
#else /* !HAVE_XAW3D */
#include <X11/Xaw/Simple.h>
#include <X11/Xaw/Scrollbar.h>
#endif /* !HAVE_XAW3D */
#ifndef XtNpickTop
#define XtNpickTop "pickTop"
#endif /* !XtNpickTop */
#endif /* !USE_MOTIF */
#endif /* USE_TOOLKIT_SCROLL_BARS */
#endif /* USE_X_TOOLKIT */
#ifdef USE_X_TOOLKIT
#include "widget.h"
#ifndef XtNinitialState
#define XtNinitialState "initialState"
#endif
#endif
/* Default to using XIM if available. */
#ifdef USE_XIM
int use_xim = 1;
#else
int use_xim = 0; /* configure --without-xim */
#endif
/* Non-nil means Emacs uses toolkit scroll bars. */
Lisp_Object Vx_toolkit_scroll_bars;
/* Non-zero means that a HELP_EVENT has been generated since Emacs
start. */
static int any_help_event_p;
/* Last window where we saw the mouse. Used by mouse-autoselect-window. */
static Lisp_Object last_window;
/* Non-zero means make use of UNDERLINE_POSITION font properties. */
int x_use_underline_position_properties;
/* Non-zero means to draw the underline at the same place as the descent line. */
int x_underline_at_descent_line;
/* This is a chain of structures for all the X displays currently in
use. */
struct x_display_info *x_display_list;
/* This is a list of cons cells, each of the form (NAME
. FONT-LIST-CACHE), one for each element of x_display_list and in
the same order. NAME is the name of the frame. FONT-LIST-CACHE
records previous values returned by x-list-fonts. */
Lisp_Object x_display_name_list;
/* Frame being updated by update_frame. This is declared in term.c.
This is set by update_begin and looked at by all the XT functions.
It is zero while not inside an update. In that case, the XT
functions assume that `selected_frame' is the frame to apply to. */
extern struct frame *updating_frame;
/* This is a frame waiting to be auto-raised, within XTread_socket. */
static struct frame *pending_autoraise_frame;
/* This is a frame waiting for an event matching mask, within XTread_socket. */
static struct {
struct frame *f;
int eventtype;
} pending_event_wait;
#ifdef USE_X_TOOLKIT
/* The application context for Xt use. */
XtAppContext Xt_app_con;
static String Xt_default_resources[] = {0};
#endif /* USE_X_TOOLKIT */
/* Non-zero means user is interacting with a toolkit scroll bar. */
static int toolkit_scroll_bar_interaction;
/* Non-zero means to not move point as a result of clicking on a
frame to focus it (when focus-follows-mouse is nil). */
int x_mouse_click_focus_ignore_position;
/* Non-zero timeout value means ignore next mouse click if it arrives
before that timeout elapses (i.e. as part of the same sequence of
events resulting from clicking on a frame to select it). */
static unsigned long ignore_next_mouse_click_timeout;
/* Mouse movement.
Formerly, we used PointerMotionHintMask (in standard_event_mask)
so that we would have to call XQueryPointer after each MotionNotify
event to ask for another such event. However, this made mouse tracking
slow, and there was a bug that made it eventually stop.
Simply asking for MotionNotify all the time seems to work better.
In order to avoid asking for motion events and then throwing most
of them away or busy-polling the server for mouse positions, we ask
the server for pointer motion hints. This means that we get only
one event per group of mouse movements. "Groups" are delimited by
other kinds of events (focus changes and button clicks, for
example), or by XQueryPointer calls; when one of these happens, we
get another MotionNotify event the next time the mouse moves. This
is at least as efficient as getting motion events when mouse
tracking is on, and I suspect only negligibly worse when tracking
is off. */
/* Where the mouse was last time we reported a mouse event. */
static XRectangle last_mouse_glyph;
static FRAME_PTR last_mouse_glyph_frame;
static Lisp_Object last_mouse_press_frame;
/* The scroll bar in which the last X motion event occurred.
If the last X motion event occurred in a scroll bar, we set this so
XTmouse_position can know whether to report a scroll bar motion or
an ordinary motion.
If the last X motion event didn't occur in a scroll bar, we set
this to Qnil, to tell XTmouse_position to return an ordinary motion
event. */
static Lisp_Object last_mouse_scroll_bar;
/* This is a hack. We would really prefer that XTmouse_position would
return the time associated with the position it returns, but there
doesn't seem to be any way to wrest the time-stamp from the server
along with the position query. So, we just keep track of the time
of the last movement we received, and return that in hopes that
it's somewhat accurate. */
static Time last_mouse_movement_time;
/* Time for last user interaction as returned in X events. */
static Time last_user_time;
/* Incremented by XTread_socket whenever it really tries to read
events. */
#ifdef __STDC__
static int volatile input_signal_count;
#else
static int input_signal_count;
#endif
/* Used locally within XTread_socket. */
static int x_noop_count;
/* Initial values of argv and argc. */
extern char **initial_argv;
extern int initial_argc;
extern Lisp_Object Vcommand_line_args, Vsystem_name;
/* Tells if a window manager is present or not. */
extern Lisp_Object Vx_no_window_manager;
extern Lisp_Object Qeql;
extern int errno;
/* A mask of extra modifier bits to put into every keyboard char. */
extern EMACS_INT extra_keyboard_modifiers;
/* The keysyms to use for the various modifiers. */
Lisp_Object Vx_alt_keysym, Vx_hyper_keysym, Vx_meta_keysym, Vx_super_keysym;
Lisp_Object Vx_keysym_table;
static Lisp_Object Qalt, Qhyper, Qmeta, Qsuper, Qmodifier_value;
static Lisp_Object Qvendor_specific_keysyms;
static Lisp_Object Qlatin_1;
#ifdef USE_GTK
/* The name of the Emacs icon file. */
static Lisp_Object xg_default_icon_file;
/* Used in gtkutil.c. */
Lisp_Object Qx_gtk_map_stock;
#endif
/* Used in x_flush. */
extern Lisp_Object Vinhibit_redisplay;
extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
extern int x_bitmap_mask P_ ((FRAME_PTR, int));
static int x_alloc_nearest_color_1 P_ ((Display *, Colormap, XColor *));
static void x_set_window_size_1 P_ ((struct frame *, int, int, int));
static const XColor *x_color_cells P_ ((Display *, int *));
static void x_update_window_end P_ ((struct window *, int, int));
static int x_io_error_quitter P_ ((Display *));
static struct terminal *x_create_terminal P_ ((struct x_display_info *));
void x_delete_terminal P_ ((struct terminal *));
static void x_update_end P_ ((struct frame *));
static void XTframe_up_to_date P_ ((struct frame *));
static void XTset_terminal_modes P_ ((struct terminal *));
static void XTreset_terminal_modes P_ ((struct terminal *));
static void x_clear_frame P_ ((struct frame *));
static void frame_highlight P_ ((struct frame *));
static void frame_unhighlight P_ ((struct frame *));
static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
static void x_focus_changed P_ ((int, int, struct x_display_info *,
struct frame *, struct input_event *));
static void x_detect_focus_change P_ ((struct x_display_info *,
XEvent *, struct input_event *));
static void XTframe_rehighlight P_ ((struct frame *));
static void x_frame_rehighlight P_ ((struct x_display_info *));
static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int,
enum text_cursor_kinds));
static void x_clip_to_row P_ ((struct window *, struct glyph_row *, int, GC));
static void x_flush P_ ((struct frame *f));
static void x_update_begin P_ ((struct frame *));
static void x_update_window_begin P_ ((struct window *));
static void x_after_update_window_line P_ ((struct glyph_row *));
static struct scroll_bar *x_window_to_scroll_bar P_ ((Display *, Window));
static void x_scroll_bar_report_motion P_ ((struct frame **, Lisp_Object *,
enum scroll_bar_part *,
Lisp_Object *, Lisp_Object *,
unsigned long *));
static void x_handle_net_wm_state P_ ((struct frame *, XPropertyEvent *));
static void x_check_fullscreen P_ ((struct frame *));
static void x_check_expected_move P_ ((struct frame *, int, int));
static void x_sync_with_move P_ ((struct frame *, int, int, int));
static int handle_one_xevent P_ ((struct x_display_info *, XEvent *,
int *, struct input_event *));
/* Don't declare this NO_RETURN because we want no
interference with debugging failing X calls. */
static SIGTYPE x_connection_closed P_ ((Display *, char *));
/* Flush display of frame F, or of all frames if F is null. */
static void
x_flush (f)
struct frame *f;
{
/* Don't call XFlush when it is not safe to redisplay; the X
connection may be broken. */
if (!NILP (Vinhibit_redisplay))
return;
BLOCK_INPUT;
if (f == NULL)
{
Lisp_Object rest, frame;
FOR_EACH_FRAME (rest, frame)
if (FRAME_X_P (XFRAME (frame)))
x_flush (XFRAME (frame));
}
else if (FRAME_X_P (f))
XFlush (FRAME_X_DISPLAY (f));
UNBLOCK_INPUT;
}
/* Remove calls to XFlush by defining XFlush to an empty replacement.
Calls to XFlush should be unnecessary because the X output buffer
is flushed automatically as needed by calls to XPending,
XNextEvent, or XWindowEvent according to the XFlush man page.
XTread_socket calls XPending. Removing XFlush improves
performance. */
#define XFlush(DISPLAY) (void) 0
/***********************************************************************
Debugging
***********************************************************************/
#if 0
/* This is a function useful for recording debugging information about
the sequence of occurrences in this file. */
struct record
{
char *locus;
int type;
};
struct record event_record[100];
int event_record_index;
record_event (locus, type)
char *locus;
int type;
{
if (event_record_index == sizeof (event_record) / sizeof (struct record))
event_record_index = 0;
event_record[event_record_index].locus = locus;
event_record[event_record_index].type = type;
event_record_index++;
}
#endif /* 0 */
/* Return the struct x_display_info corresponding to DPY. */
struct x_display_info *
x_display_info_for_display (dpy)
Display *dpy;
{
struct x_display_info *dpyinfo;
for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
if (dpyinfo->display == dpy)
return dpyinfo;
return 0;
}
#define OPAQUE 0xffffffff
#define OPACITY "_NET_WM_WINDOW_OPACITY"
void
x_set_frame_alpha (f)
struct frame *f;
{
struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
Display *dpy = FRAME_X_DISPLAY (f);
Window win = FRAME_OUTER_WINDOW (f);
double alpha = 1.0;
double alpha_min = 1.0;
unsigned long opac;
if (FRAME_X_DISPLAY_INFO (f)->root_window != FRAME_X_OUTPUT (f)->parent_desc)
/* Since the WM decoration lies under the FRAME_OUTER_WINDOW,
we must treat the former instead of the latter. */
win = FRAME_X_OUTPUT(f)->parent_desc;
if (dpyinfo->x_highlight_frame == f)
alpha = f->alpha[0];
else
alpha = f->alpha[1];
if (FLOATP (Vframe_alpha_lower_limit))
alpha_min = XFLOAT_DATA (Vframe_alpha_lower_limit);
else if (INTEGERP (Vframe_alpha_lower_limit))
alpha_min = (XINT (Vframe_alpha_lower_limit)) / 100.0;
if (alpha < 0.0)
return;
else if (alpha > 1.0)
alpha = 1.0;
else if (0.0 <= alpha && alpha < alpha_min && alpha_min <= 1.0)
alpha = alpha_min;
opac = alpha * OPAQUE;
/* return unless necessary */
{
unsigned char *data;
Atom actual;
int rc, format;
unsigned long n, left;
x_catch_errors (dpy);
rc = XGetWindowProperty(dpy, win, XInternAtom(dpy, OPACITY, False),
0L, 1L, False, XA_CARDINAL,
&actual, &format, &n, &left,
&data);
if (rc == Success && actual != None)
if (*(unsigned long *)data == opac)
{
XFree ((void *) data);
x_uncatch_errors ();
return;
}
else
XFree ((void *) data);
x_uncatch_errors ();
}
x_catch_errors (dpy);
XChangeProperty (dpy, win, XInternAtom (dpy, OPACITY, False),
XA_CARDINAL, 32, PropModeReplace,
(unsigned char *) &opac, 1L);
x_uncatch_errors ();
}
int
x_display_pixel_height (dpyinfo)
struct x_display_info *dpyinfo;
{
return HeightOfScreen (dpyinfo->screen);
}
int
x_display_pixel_width (dpyinfo)
struct x_display_info *dpyinfo;
{
return WidthOfScreen (dpyinfo->screen);
}
/***********************************************************************
Starting and ending an update
***********************************************************************/
/* Start an update of frame F. This function is installed as a hook
for update_begin, i.e. it is called when update_begin is called.
This function is called prior to calls to x_update_window_begin for
each window being updated. Currently, there is nothing to do here
because all interesting stuff is done on a window basis. */
static void
x_update_begin (f)
struct frame *f;
{
/* Nothing to do. */
}
/* Start update of window W. Set the global variable updated_window
to the window being updated and set output_cursor to the cursor
position of W. */
static void
x_update_window_begin (w)
struct window *w;
{
struct frame *f = XFRAME (WINDOW_FRAME (w));
struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
updated_window = w;
set_output_cursor (&w->cursor);
BLOCK_INPUT;
if (f == display_info->mouse_face_mouse_frame)
{
/* Don't do highlighting for mouse motion during the update. */
display_info->mouse_face_defer = 1;
/* If F needs to be redrawn, simply forget about any prior mouse
highlighting. */
if (FRAME_GARBAGED_P (f))
display_info->mouse_face_window = Qnil;
}
UNBLOCK_INPUT;
}
/* Draw a vertical window border from (x,y0) to (x,y1) */
static void
x_draw_vertical_window_border (w, x, y0, y1)
struct window *w;
int x, y0, y1;
{
struct frame *f = XFRAME (WINDOW_FRAME (w));
struct face *face;
face = FACE_FROM_ID (f, VERTICAL_BORDER_FACE_ID);
if (face)
XSetForeground (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
face->foreground);
XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
f->output_data.x->normal_gc, x, y0, x, y1);
}
/* End update of window W (which is equal to updated_window).
Draw vertical borders between horizontally adjacent windows, and
display W's cursor if CURSOR_ON_P is non-zero.
MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
glyphs in mouse-face were overwritten. In that case we have to
make sure that the mouse-highlight is properly redrawn.
W may be a menu bar pseudo-window in case we don't have X toolkit
support. Such windows don't have a cursor, so don't display it
here. */
static void
x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
struct window *w;
int cursor_on_p, mouse_face_overwritten_p;
{
struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
if (!w->pseudo_window_p)
{
BLOCK_INPUT;
if (cursor_on_p)
display_and_set_cursor (w, 1, output_cursor.hpos,
output_cursor.vpos,
output_cursor.x, output_cursor.y);
if (draw_window_fringes (w, 1))
x_draw_vertical_border (w);
UNBLOCK_INPUT;
}
/* If a row with mouse-face was overwritten, arrange for
XTframe_up_to_date to redisplay the mouse highlight. */
if (mouse_face_overwritten_p)
{
dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
dpyinfo->mouse_face_window = Qnil;
}
updated_window = NULL;
}
/* End update of frame F. This function is installed as a hook in
update_end. */
static void
x_update_end (f)
struct frame *f;
{
/* Mouse highlight may be displayed again. */
FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
#ifndef XFlush
BLOCK_INPUT;
XFlush (FRAME_X_DISPLAY (f));
UNBLOCK_INPUT;
#endif
}
/* This function is called from various places in xdisp.c whenever a
complete update has been performed. The global variable
updated_window is not available here. */
static void
XTframe_up_to_date (f)
struct frame *f;
{
if (FRAME_X_P (f))
{
struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
if (dpyinfo->mouse_face_deferred_gc
|| f == dpyinfo->mouse_face_mouse_frame)
{
BLOCK_INPUT;
if (dpyinfo->mouse_face_mouse_frame)
note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
dpyinfo->mouse_face_mouse_x,
dpyinfo->mouse_face_mouse_y);
dpyinfo->mouse_face_deferred_gc = 0;
UNBLOCK_INPUT;
}
}
}
/* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
arrow bitmaps, or clear the fringes if no bitmaps are required
before DESIRED_ROW is made current. The window being updated is
found in updated_window. This function It is called from
update_window_line only if it is known that there are differences
between bitmaps to be drawn between current row and DESIRED_ROW. */
static void
x_after_update_window_line (desired_row)
struct glyph_row *desired_row;
{
struct window *w = updated_window;
struct frame *f;
int width, height;
xassert (w);
if (!desired_row->mode_line_p && !w->pseudo_window_p)
desired_row->redraw_fringe_bitmaps_p = 1;
/* When a window has disappeared, make sure that no rest of
full-width rows stays visible in the internal border. Could
check here if updated_window is the leftmost/rightmost window,
but I guess it's not worth doing since vertically split windows
are almost never used, internal border is rarely set, and the
overhead is very small. */
if (windows_or_buffers_changed
&& desired_row->full_width_p
&& (f = XFRAME (w->frame),
width = FRAME_INTERNAL_BORDER_WIDTH (f),
width != 0)
&& (height = desired_row->visible_height,
height > 0))
{
int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
BLOCK_INPUT;
x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
0, y, width, height, False);
x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
FRAME_PIXEL_WIDTH (f) - width,
y, width, height, False);
UNBLOCK_INPUT;
}
}
static void
x_draw_fringe_bitmap (w, row, p)
struct window *w;
struct glyph_row *row;
struct draw_fringe_bitmap_params *p;
{
struct frame *f = XFRAME (WINDOW_FRAME (w));
Display *display = FRAME_X_DISPLAY (f);
Window window = FRAME_X_WINDOW (f);
GC gc = f->output_data.x->normal_gc;
struct face *face = p->face;
/* Must clip because of partially visible lines. */
x_clip_to_row (w, row, -1, gc);
if (!p->overlay_p)
{
int bx = p->bx, by = p->by, nx = p->nx, ny = p->ny;
/* In case the same realized face is used for fringes and
for something displayed in the text (e.g. face `region' on
mono-displays, the fill style may have been changed to
FillSolid in x_draw_glyph_string_background. */
if (face->stipple)
XSetFillStyle (display, face->gc, FillOpaqueStippled);
else
XSetForeground (display, face->gc, face->background);
#ifdef USE_TOOLKIT_SCROLL_BARS
/* If the fringe is adjacent to the left (right) scroll bar of a
leftmost (rightmost, respectively) window, then extend its
background to the gap between the fringe and the bar. */
if ((WINDOW_LEFTMOST_P (w)
&& WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
|| (WINDOW_RIGHTMOST_P (w)
&& WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w)))
{
int sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
if (sb_width > 0)
{
int bar_area_x = WINDOW_SCROLL_BAR_AREA_X (w);
int bar_area_width = (WINDOW_CONFIG_SCROLL_BAR_COLS (w)
* FRAME_COLUMN_WIDTH (f));
if (bx < 0)
{
/* Bitmap fills the fringe. */
if (bar_area_x + bar_area_width == p->x)
bx = bar_area_x + sb_width;
else if (p->x + p->wd == bar_area_x)
bx = bar_area_x;
if (bx >= 0)
{
int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
nx = bar_area_width - sb_width;
by = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
row->y));
ny = row->visible_height;
}
}
else
{
if (bar_area_x + bar_area_width == bx)
{
bx = bar_area_x + sb_width;
nx += bar_area_width - sb_width;
}
else if (bx + nx == bar_area_x)
nx += bar_area_width - sb_width;
}
}
}
#endif
if (bx >= 0 && nx > 0)
XFillRectangle (display, window, face->gc, bx, by, nx, ny);
if (!face->stipple)
XSetForeground (display, face->gc, face->foreground);
}
if (p->which)
{
unsigned char *bits;
Pixmap pixmap, clipmask = (Pixmap) 0;
int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
XGCValues gcv;
if (p->wd > 8)
bits = (unsigned char *)(p->bits + p->dh);
else
bits = (unsigned char *)p->bits + p->dh;
/* Draw the bitmap. I believe these small pixmaps can be cached
by the server. */
pixmap = XCreatePixmapFromBitmapData (display, window, bits, p->wd, p->h,
(p->cursor_p
? (p->overlay_p ? face->background
: f->output_data.x->cursor_pixel)
: face->foreground),
face->background, depth);
if (p->overlay_p)
{
clipmask = XCreatePixmapFromBitmapData (display,
FRAME_X_DISPLAY_INFO (f)->root_window,
bits, p->wd, p->h,
1, 0, 1);
gcv.clip_mask = clipmask;
gcv.clip_x_origin = p->x;
gcv.clip_y_origin = p->y;
XChangeGC (display, gc, GCClipMask | GCClipXOrigin | GCClipYOrigin, &gcv);
}
XCopyArea (display, pixmap, window, gc, 0, 0,
p->wd, p->h, p->x, p->y);
XFreePixmap (display, pixmap);
if (p->overlay_p)
{
gcv.clip_mask = (Pixmap) 0;
XChangeGC (display, gc, GCClipMask, &gcv);
XFreePixmap (display, clipmask);
}
}
XSetClipMask (display, gc, None);
}
/* This is called when starting Emacs and when restarting after
suspend. When starting Emacs, no X window is mapped. And nothing
must be done to Emacs's own window if it is suspended (though that
rarely happens). */
static void
XTset_terminal_modes (struct terminal *terminal)
{
}
/* This is called when exiting or suspending Emacs. Exiting will make
the X-windows go away, and suspending requires no action. */
static void
XTreset_terminal_modes (struct terminal *terminal)
{
}
/***********************************************************************
Glyph display
***********************************************************************/
static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
static void x_set_glyph_string_gc P_ ((struct glyph_string *));
static void x_draw_glyph_string_background P_ ((struct glyph_string *,
int));
static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
static void x_draw_glyph_string_box P_ ((struct glyph_string *));
static void x_draw_glyph_string P_ ((struct glyph_string *));
static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
static void x_set_cursor_gc P_ ((struct glyph_string *));
static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
static void x_set_mouse_face_gc P_ ((struct glyph_string *));
static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
unsigned long *, double, int));
static void x_setup_relief_color P_ ((struct frame *, struct relief *,
double, int, unsigned long));
static void x_setup_relief_colors P_ ((struct glyph_string *));
static void x_draw_image_glyph_string P_ ((struct glyph_string *));
static void x_draw_image_relief P_ ((struct glyph_string *));
static void x_draw_image_foreground P_ ((struct glyph_string *));
static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
int, int, int));
static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
int, int, int, int, int, int,
XRectangle *));
static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
int, int, int, XRectangle *));
#if GLYPH_DEBUG
static void x_check_font P_ ((struct frame *, struct font *));
#endif
/* Set S->gc to a suitable GC for drawing glyph string S in cursor
face. */
static void
x_set_cursor_gc (s)
struct glyph_string *s;
{
if (s->font == FRAME_FONT (s->f)
&& s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
&& s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
&& !s->cmp)
s->gc = s->f->output_data.x->cursor_gc;
else
{
/* Cursor on non-default face: must merge. */
XGCValues xgcv;
unsigned long mask;
xgcv.background = s->f->output_data.x->cursor_pixel;
xgcv.foreground = s->face->background;
/* If the glyph would be invisible, try a different foreground. */
if (xgcv.foreground == xgcv.background)
xgcv.foreground = s->face->foreground;
if (xgcv.foreground == xgcv.background)
xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
if (xgcv.foreground == xgcv.background)
xgcv.foreground = s->face->foreground;
/* Make sure the cursor is distinct from text in this face. */
if (xgcv.background == s->face->background
&& xgcv.foreground == s->face->foreground)
{
xgcv.background = s->face->foreground;
xgcv.foreground = s->face->background;
}
IF_DEBUG (x_check_font (s->f, s->font));
xgcv.graphics_exposures = False;
mask = GCForeground | GCBackground | GCGraphicsExposures;
if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
mask, &xgcv);
else
FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
= XCreateGC (s->display, s->window, mask, &xgcv);
s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
}
}
/* Set up S->gc of glyph string S for drawing text in mouse face. */
static void
x_set_mouse_face_gc (s)
struct glyph_string *s;
{
int face_id;
struct face *face;
/* What face has to be used last for the mouse face? */
face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
face = FACE_FROM_ID (s->f, face_id);
if (face == NULL)
face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
if (s->first_glyph->type == CHAR_GLYPH)
face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch, -1, Qnil);
else
face_id = FACE_FOR_CHAR (s->f, face, 0, -1, Qnil);
s->face = FACE_FROM_ID (s->f, face_id);
PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
if (s->font == s->face->font)
s->gc = s->face->gc;
else
{
/* Otherwise construct scratch_cursor_gc with values from FACE
except for FONT. */
XGCValues xgcv;
unsigned long mask;
xgcv.background = s->face->background;
xgcv.foreground = s->face->foreground;
xgcv.graphics_exposures = False;
mask = GCForeground | GCBackground | GCGraphicsExposures;
if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
mask, &xgcv);
else
FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
= XCreateGC (s->display, s->window, mask, &xgcv);
s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
}
xassert (s->gc != 0);
}
/* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
Faces to use in the mode line have already been computed when the
matrix was built, so there isn't much to do, here. */
static INLINE void
x_set_mode_line_face_gc (s)
struct glyph_string *s;
{
s->gc = s->face->gc;
}
/* Set S->gc of glyph string S for drawing that glyph string. Set
S->stippled_p to a non-zero value if the face of S has a stipple
pattern. */
static INLINE void
x_set_glyph_string_gc (s)
struct glyph_string *s;
{
PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
if (s->hl == DRAW_NORMAL_TEXT)
{
s->gc = s->face->gc;
s->stippled_p = s->face->stipple != 0;
}
else if (s->hl == DRAW_INVERSE_VIDEO)
{
x_set_mode_line_face_gc (s);
s->stippled_p = s->face->stipple != 0;
}
else if (s->hl == DRAW_CURSOR)
{
x_set_cursor_gc (s);
s->stippled_p = 0;
}
else if (s->hl == DRAW_MOUSE_FACE)
{
x_set_mouse_face_gc (s);
s->stippled_p = s->face->stipple != 0;
}
else if (s->hl == DRAW_IMAGE_RAISED
|| s->hl == DRAW_IMAGE_SUNKEN)
{
s->gc = s->face->gc;
s->stippled_p = s->face->stipple != 0;
}
else
{
s->gc = s->face->gc;
s->stippled_p = s->face->stipple != 0;
}
/* GC must have been set. */
xassert (s->gc != 0);
}
/* Set clipping for output of glyph string S. S may be part of a mode
line or menu if we don't have X toolkit support. */
static INLINE void
x_set_glyph_string_clipping (s)
struct glyph_string *s;
{
XRectangle *r = s->clip;
int n = get_glyph_string_clip_rects (s, r, 2);
if (n > 0)
XSetClipRectangles (s->display, s->gc, 0, 0, r, n, Unsorted);
s->num_clips = n;
}
/* Set SRC's clipping for output of glyph string DST. This is called
when we are drawing DST's left_overhang or right_overhang only in
the area of SRC. */
static void
x_set_glyph_string_clipping_exactly (src, dst)
struct glyph_string *src, *dst;
{
XRectangle r;
r.x = src->x;
r.width = src->width;
r.y = src->y;
r.height = src->height;
dst->clip[0] = r;
dst->num_clips = 1;
XSetClipRectangles (dst->display, dst->gc, 0, 0, &r, 1, Unsorted);
}
/* RIF:
Compute left and right overhang of glyph string S. */
static void
x_compute_glyph_string_overhangs (s)
struct glyph_string *s;
{
if (s->cmp == NULL
&& (s->first_glyph->type == CHAR_GLYPH
|| s->first_glyph->type == COMPOSITE_GLYPH))
{
struct font_metrics metrics;
if (s->first_glyph->type == CHAR_GLYPH)
{
unsigned *code = alloca (sizeof (unsigned) * s->nchars);
struct font *font = s->font;
int i;
for (i = 0; i < s->nchars; i++)
code[i] = (s->char2b[i].byte1 << 8) | s->char2b[i].byte2;
font->driver->text_extents (font, code, s->nchars, &metrics);
}
else
{
Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
composition_gstring_width (gstring, s->cmp_from, s->cmp_to, &metrics);
}
s->right_overhang = (metrics.rbearing > metrics.width
? metrics.rbearing - metrics.width : 0);
s->left_overhang = metrics.lbearing < 0 ? - metrics.lbearing : 0;
}
else if (s->cmp)
{
s->right_overhang = s->cmp->rbearing - s->cmp->pixel_width;
s->left_overhang = - s->cmp->lbearing;
}
}
/* Fill rectangle X, Y, W, H with background color of glyph string S. */
static INLINE void
x_clear_glyph_string_rect (s, x, y, w, h)
struct glyph_string *s;
int x, y, w, h;
{
XGCValues xgcv;
XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
XSetForeground (s->display, s->gc, xgcv.background);
XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
XSetForeground (s->display, s->gc, xgcv.foreground);
}
/* Draw the background of glyph_string S. If S->background_filled_p
is non-zero don't draw it. FORCE_P non-zero means draw the
background even if it wouldn't be drawn normally. This is used
when a string preceding S draws into the background of S, or S
contains the first component of a composition. */
static void
x_draw_glyph_string_background (s, force_p)
struct glyph_string *s;
int force_p;
{
/* Nothing to do if background has already been drawn or if it
shouldn't be drawn in the first place. */
if (!s->background_filled_p)
{
int box_line_width = max (s->face->box_line_width, 0);
if (s->stippled_p)
{
/* Fill background with a stipple pattern. */
XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
XFillRectangle (s->display, s->window, s->gc, s->x,
s->y + box_line_width,
s->background_width,
s->height - 2 * box_line_width);
XSetFillStyle (s->display, s->gc, FillSolid);
s->background_filled_p = 1;
}
else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
|| s->font_not_found_p
|| s->extends_to_end_of_line_p
|| force_p)
{
x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
s->background_width,
s->height - 2 * box_line_width);
s->background_filled_p = 1;
}
}
}
/* Draw the foreground of glyph string S. */
static void
x_draw_glyph_string_foreground (s)
struct glyph_string *s;
{
int i, x;
/* If first glyph of S has a left box line, start drawing the text
of S to the right of that box line. */
if (s->face->box != FACE_NO_BOX
&& s->first_glyph->left_box_line_p)
x = s->x + eabs (s->face->box_line_width);
else
x = s->x;
/* Draw characters of S as rectangles if S's font could not be
loaded. */
if (s->font_not_found_p)
{
for (i = 0; i < s->nchars; ++i)
{
struct glyph *g = s->first_glyph + i;
XDrawRectangle (s->display, s->window,
s->gc, x, s->y, g->pixel_width - 1,
s->height - 1);
x += g->pixel_width;
}
}
else
{
struct font *font = s->font;
int boff = font->baseline_offset;
int y;
if (font->vertical_centering)
boff = VCENTER_BASELINE_OFFSET (font, s->f) - boff;
y = s->ybase - boff;
if (s->for_overlaps
|| (s->background_filled_p && s->hl != DRAW_CURSOR))
font->driver->draw (s, 0, s->nchars, x, y, 0);
else
font->driver->draw (s, 0, s->nchars, x, y, 1);
if (s->face->overstrike)
font->driver->draw (s, 0, s->nchars, x + 1, y, 0);
}
}
/* Draw the foreground of composite glyph string S. */
static void
x_draw_composite_glyph_string_foreground (s)
struct glyph_string *s;
{
int i, j, x;
struct font *font = s->font;
/* If first glyph of S has a left box line, start drawing the text
of S to the right of that box line. */
if (s->face && s->face->box != FACE_NO_BOX
&& s->first_glyph->left_box_line_p)
x = s->x + eabs (s->face->box_line_width);
else
x = s->x;
/* S is a glyph string for a composition. S->cmp_from is the index
of the first character drawn for glyphs of this composition.
S->cmp_from == 0 means we are drawing the very first character of
this composition. */
/* Draw a rectangle for the composition if the font for the very
first character of the composition could not be loaded. */
if (s->font_not_found_p)
{
if (s->cmp_from == 0)
XDrawRectangle (s->display, s->window, s->gc, x, s->y,
s->width - 1, s->height - 1);
}
else if (! s->first_glyph->u.cmp.automatic)
{
int y = s->ybase;
for (i = 0, j = s->cmp_from; i < s->nchars; i++, j++)
if (COMPOSITION_GLYPH (s->cmp, j) != '\t')
{
int xx = x + s->cmp->offsets[j * 2];
int yy = y - s->cmp->offsets[j * 2 + 1];
font->driver->draw (s, j, j + 1, xx, yy, 0);
if (s->face->overstrike)
font->driver->draw (s, j, j + 1, xx + 1, yy, 0);
}
}
else
{
Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
Lisp_Object glyph;
int y = s->ybase;
int width = 0;
for (i = j = s->cmp_from; i < s->cmp_to; i++)
{
glyph = LGSTRING_GLYPH (gstring, i);
if (NILP (LGLYPH_ADJUSTMENT (glyph)))
width += LGLYPH_WIDTH (glyph);
else
{
int xoff, yoff, wadjust;
if (j < i)
{
font->driver->draw (s, j, i, x, y, 0);
if (s->face->overstrike)
font->driver->draw (s, j, i, x + 1, y, 0);
x += width;
}
xoff = LGLYPH_XOFF (glyph);
yoff = LGLYPH_YOFF (glyph);
wadjust = LGLYPH_WADJUST (glyph);
font->driver->draw (s, i, i + 1, x + xoff, y + yoff, 0);
if (s->face->overstrike)
font->driver->draw (s, i, i + 1, x + xoff + 1, y + yoff, 0);
x += wadjust;
j = i + 1;
width = 0;
}
}
if (j < i)
{
font->driver->draw (s, j, i, x, y, 0);
if (s->face->overstrike)
font->driver->draw (s, j, i, x + 1, y, 0);
}
}
}
#ifdef USE_X_TOOLKIT
static struct frame *x_frame_of_widget P_ ((Widget));
static Boolean cvt_string_to_pixel P_ ((Display *, XrmValue *, Cardinal *,
XrmValue *, XrmValue *, XtPointer *));
static void cvt_pixel_dtor P_ ((XtAppContext, XrmValue *, XtPointer,
XrmValue *, Cardinal *));
/* Return the frame on which widget WIDGET is used.. Abort if frame
cannot be determined. */
static struct frame *
x_frame_of_widget (widget)
Widget widget;
{
struct x_display_info *dpyinfo;
Lisp_Object tail;
struct frame *f;
dpyinfo = x_display_info_for_display (XtDisplay (widget));
/* Find the top-level shell of the widget. Note that this function
can be called when the widget is not yet realized, so XtWindow
(widget) == 0. That's the reason we can't simply use
x_any_window_to_frame. */
while (!XtIsTopLevelShell (widget))
widget = XtParent (widget);
/* Look for a frame with that top-level widget. Allocate the color
on that frame to get the right gamma correction value. */
for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
if (FRAMEP (XCAR (tail))
&& (f = XFRAME (XCAR (tail)),
(FRAME_X_P (f)
&& f->output_data.nothing != 1
&& FRAME_X_DISPLAY_INFO (f) == dpyinfo))
&& f->output_data.x->widget == widget)
return f;
abort ();
}
/* Allocate the color COLOR->pixel on the screen and display of
widget WIDGET in colormap CMAP. If an exact match cannot be
allocated, try the nearest color available. Value is non-zero
if successful. This is called from lwlib. */
int
x_alloc_nearest_color_for_widget (widget, cmap, color)
Widget widget;
Colormap cmap;
XColor *color;
{
struct frame *f = x_frame_of_widget (widget);
return x_alloc_nearest_color (f, cmap, color);
}
/* Allocate a color which is lighter or darker than *PIXEL by FACTOR
or DELTA. Try a color with RGB values multiplied by FACTOR first.
If this produces the same color as PIXEL, try a color where all RGB
values have DELTA added. Return the allocated color in *PIXEL.
DISPLAY is the X display, CMAP is the colormap to operate on.
Value is non-zero if successful. */
int
x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
Widget widget;
Display *display;
Colormap cmap;
unsigned long *pixel;
double factor;
int delta;
{
struct frame *f = x_frame_of_widget (widget);
return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
}
/* Structure specifying which arguments should be passed by Xt to
cvt_string_to_pixel. We want the widget's screen and colormap. */
static XtConvertArgRec cvt_string_to_pixel_args[] =
{
{XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.screen),
sizeof (Screen *)},
{XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.colormap),
sizeof (Colormap)}
};
/* The address of this variable is returned by
cvt_string_to_pixel. */
static Pixel cvt_string_to_pixel_value;
/* Convert a color name to a pixel color.
DPY is the display we are working on.
ARGS is an array of *NARGS XrmValue structures holding additional
information about the widget for which the conversion takes place.
The contents of this array are determined by the specification
in cvt_string_to_pixel_args.
FROM is a pointer to an XrmValue which points to the color name to
convert. TO is an XrmValue in which to return the pixel color.
CLOSURE_RET is a pointer to user-data, in which we record if
we allocated the color or not.
Value is True if successful, False otherwise. */
static Boolean
cvt_string_to_pixel (dpy, args, nargs, from, to, closure_ret)
Display *dpy;
XrmValue *args;
Cardinal *nargs;
XrmValue *from, *to;
XtPointer *closure_ret;
{
Screen *screen;
Colormap cmap;
Pixel pixel;
String color_name;
XColor color;
if (*nargs != 2)
{
XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
"wrongParameters", "cvt_string_to_pixel",
"XtToolkitError",
"Screen and colormap args required", NULL, NULL);
return False;
}
screen = *(Screen **) args[0].addr;
cmap = *(Colormap *) args[1].addr;
color_name = (String) from->addr;
if (strcmp (color_name, XtDefaultBackground) == 0)
{
*closure_ret = (XtPointer) False;
pixel = WhitePixelOfScreen (screen);
}
else if (strcmp (color_name, XtDefaultForeground) == 0)
{
*closure_ret = (XtPointer) False;
pixel = BlackPixelOfScreen (screen);
}
else if (XParseColor (dpy, cmap, color_name, &color)
&& x_alloc_nearest_color_1 (dpy, cmap, &color))
{
pixel = color.pixel;
*closure_ret = (XtPointer) True;
}
else
{
String params[1];
Cardinal nparams = 1;
params[0] = color_name;
XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
"badValue", "cvt_string_to_pixel",
"XtToolkitError", "Invalid color `%s'",
params, &nparams);
return False;
}
if (to->addr != NULL)
{
if (to->size < sizeof (Pixel))
{
to->size = sizeof (Pixel);
return False;
}
*(Pixel *) to->addr = pixel;
}
else
{
cvt_string_to_pixel_value = pixel;
to->addr = (XtPointer) &cvt_string_to_pixel_value;
}
to->size = sizeof (Pixel);
return True;
}
/* Free a pixel color which was previously allocated via
cvt_string_to_pixel. This is registered as the destructor
for this type of resource via XtSetTypeConverter.
APP is the application context in which we work.
TO is a pointer to an XrmValue holding the color to free.
CLOSURE is the value we stored in CLOSURE_RET for this color
in cvt_string_to_pixel.
ARGS and NARGS are like for cvt_string_to_pixel. */
static void
cvt_pixel_dtor (app, to, closure, args, nargs)
XtAppContext app;
XrmValuePtr to;
XtPointer closure;
XrmValuePtr args;
Cardinal *nargs;
{
if (*nargs != 2)
{
XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
"XtToolkitError",
"Screen and colormap arguments required",
NULL, NULL);
}
else if (closure != NULL)
{
/* We did allocate the pixel, so free it. */
Screen *screen = *(Screen **) args[0].addr;
Colormap cmap = *(Colormap *) args[1].addr;
x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
(Pixel *) to->addr, 1);
}
}
#endif /* USE_X_TOOLKIT */
/* Value is an array of XColor structures for the contents of the
color map of display DPY. Set *NCELLS to the size of the array.
Note that this probably shouldn't be called for large color maps,
say a 24-bit TrueColor map. */
static const XColor *
x_color_cells (dpy, ncells)
Display *dpy;
int *ncells;
{
struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
if (dpyinfo->color_cells == NULL)
{
Screen *screen = dpyinfo->screen;
int i;
dpyinfo->ncolor_cells
= XDisplayCells (dpy, XScreenNumberOfScreen (screen));
dpyinfo->color_cells
= (XColor *) xmalloc (dpyinfo->ncolor_cells
* sizeof *dpyinfo->color_cells);
for (i = 0; i < dpyinfo->ncolor_cells; ++i)
dpyinfo->color_cells[i].pixel = i;
XQueryColors (dpy, dpyinfo->cmap,
dpyinfo->color_cells, dpyinfo->ncolor_cells);
}
*ncells = dpyinfo->ncolor_cells;
return dpyinfo->color_cells;
}
/* On frame F, translate pixel colors to RGB values for the NCOLORS
colors in COLORS. Use cached information, if available. */
void
x_query_colors (f, colors, ncolors)
struct frame *f;
XColor *colors;
int ncolors;
{
struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
if (dpyinfo->color_cells)
{
int i;
for (i = 0; i < ncolors; ++i)
{
unsigned long pixel = colors[i].pixel;
xassert (pixel < dpyinfo->ncolor_cells);
xassert (dpyinfo->color_cells[pixel].pixel == pixel);
colors[i] = dpyinfo->color_cells[pixel];
}
}
else
XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
}
/* On frame F, translate pixel color to RGB values for the color in
COLOR. Use cached information, if available. */
void
x_query_color (f, color)
struct frame *f;
XColor *color;
{
x_query_colors (f, color, 1);
}
/* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
exact match can't be allocated, try the nearest color available.
Value is non-zero if successful. Set *COLOR to the color
allocated. */
static int
x_alloc_nearest_color_1 (dpy, cmap, color)
Display *dpy;
Colormap cmap;
XColor *color;
{
int rc;
rc = XAllocColor (dpy, cmap, color);
if (rc == 0)
{
/* If we got to this point, the colormap is full, so we're going
to try to get the next closest color. The algorithm used is
a least-squares matching, which is what X uses for closest
color matching with StaticColor visuals. */
int nearest, i;
unsigned long nearest_delta = ~0;
int ncells;
const XColor *cells = x_color_cells (dpy, &ncells);
for (nearest = i = 0; i < ncells; ++i)
{
long dred = (color->red >> 8) - (cells[i].red >> 8);
long dgreen = (color->green >> 8) - (cells[i].green >> 8);
long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
if (delta < nearest_delta)
{
nearest = i;
nearest_delta = delta;
}
}
color->red = cells[nearest].red;
color->green = cells[nearest].green;
color->blue = cells[nearest].blue;
rc = XAllocColor (dpy, cmap, color);
}
else
{
/* If allocation succeeded, and the allocated pixel color is not
equal to a cached pixel color recorded earlier, there was a
change in the colormap, so clear the color cache. */
struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
XColor *cached_color;
if (dpyinfo->color_cells
&& (cached_color = &dpyinfo->color_cells[color->pixel],
(cached_color->red != color->red
|| cached_color->blue != color->blue
|| cached_color->green != color->green)))
{
xfree (dpyinfo->color_cells);
dpyinfo->color_cells = NULL;
dpyinfo->ncolor_cells = 0;
}
}
#ifdef DEBUG_X_COLORS
if (rc)
register_color (color->pixel);
#endif /* DEBUG_X_COLORS */
return rc;
}
/* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
exact match can't be allocated, try the nearest color available.
Value is non-zero if successful. Set *COLOR to the color
allocated. */
int
x_alloc_nearest_color (f, cmap, color)
struct frame *f;
Colormap cmap;
XColor *color;
{
gamma_correct (f, color);
return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
}
/* Allocate color PIXEL on frame F. PIXEL must already be allocated.
It's necessary to do this instead of just using PIXEL directly to
get color reference counts right. */
unsigned long
x_copy_color (f, pixel)
struct frame *f;
unsigned long pixel;
{
XColor color;
color.pixel = pixel;
BLOCK_INPUT;
x_query_color (f, &color);
XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
UNBLOCK_INPUT;
#ifdef DEBUG_X_COLORS
register_color (pixel);
#endif
return color.pixel;
}
/* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
It's necessary to do this instead of just using PIXEL directly to
get color reference counts right. */
unsigned long
x_copy_dpy_color (dpy, cmap, pixel)
Display *dpy;
Colormap cmap;
unsigned long pixel;
{
XColor color;
color.pixel = pixel;
BLOCK_INPUT;
XQueryColor (dpy, cmap, &color);
XAllocColor (dpy, cmap, &color);
UNBLOCK_INPUT;
#ifdef DEBUG_X_COLORS
register_color (pixel);
#endif
return color.pixel;
}
/* Brightness beyond which a color won't have its highlight brightness
boosted.
Nominally, highlight colors for `3d' faces are calculated by
brightening an object's color by a constant scale factor, but this
doesn't yield good results for dark colors, so for colors who's
brightness is less than this value (on a scale of 0-65535) have an
use an additional additive factor.
The value here is set so that the default menu-bar/mode-line color
(grey75) will not have its highlights changed at all. */
#define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
/* Allocate a color which is lighter or darker than *PIXEL by FACTOR
or DELTA. Try a color with RGB values multiplied by FACTOR first.
If this produces the same color as PIXEL, try a color where all RGB
values have DELTA added. Return the allocated color in *PIXEL.
DISPLAY is the X display, CMAP is the colormap to operate on.
Value is non-zero if successful. */
static int
x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
struct frame *f;
Display *display;
Colormap cmap;
unsigned long *pixel;
double factor;
int delta;
{
XColor color, new;
long bright;
int success_p;
/* Get RGB color values. */
color.pixel = *pixel;
x_query_color (f, &color);
/* Change RGB values by specified FACTOR. Avoid overflow! */
xassert (factor >= 0);
new.red = min (0xffff, factor * color.red);
new.green = min (0xffff, factor * color.green);
new.blue = min (0xffff, factor * color.blue);
/* Calculate brightness of COLOR. */
bright = (2 * color.red + 3 * color.green + color.blue) / 6;
/* We only boost colors that are darker than
HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
/* Make an additive adjustment to NEW, because it's dark enough so
that scaling by FACTOR alone isn't enough. */
{
/* How far below the limit this color is (0 - 1, 1 being darker). */
double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
/* The additive adjustment. */
int min_delta = delta * dimness * factor / 2;
if (factor < 1)
{
new.red = max (0, new.red - min_delta);
new.green = max (0, new.green - min_delta);
new.blue = max (0, new.blue - min_delta);
}
else
{
new.red = min (0xffff, min_delta + new.red);
new.green = min (0xffff, min_delta + new.green);
new.blue = min (0xffff, min_delta + new.blue);
}
}
/* Try to allocate the color. */
success_p = x_alloc_nearest_color (f, cmap, &new);
if (success_p)
{
if (new.pixel == *pixel)
{
/* If we end up with the same color as before, try adding
delta to the RGB values. */
x_free_colors (f, &new.pixel, 1);
new.red = min (0xffff, delta + color.red);
new.green = min (0xffff, delta + color.green);
new.blue = min (0xffff, delta + color.blue);
success_p = x_alloc_nearest_color (f, cmap, &new);
}
else
success_p = 1;
*pixel = new.pixel;
}
return success_p;
}
/* Set up the foreground color for drawing relief lines of glyph
string S. RELIEF is a pointer to a struct relief containing the GC
with which lines will be drawn. Use a color that is FACTOR or
DELTA lighter or darker than the relief's background which is found
in S->f->output_data.x->relief_background. If such a color cannot
be allocated, use DEFAULT_PIXEL, instead. */
static void
x_setup_relief_color (f, relief, factor, delta, default_pixel)
struct frame *f;
struct relief *relief;
double factor;
int delta;
unsigned long default_pixel;
{
XGCValues xgcv;
struct x_output *di = f->output_data.x;
unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
unsigned long pixel;
unsigned long background = di->relief_background;
Colormap cmap = FRAME_X_COLORMAP (f);
struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
Display *dpy = FRAME_X_DISPLAY (f);
xgcv.graphics_exposures = False;
xgcv.line_width = 1;
/* Free previously allocated color. The color cell will be reused
when it has been freed as many times as it was allocated, so this
doesn't affect faces using the same colors. */
if (relief->gc
&& relief->allocated_p)
{
x_free_colors (f, &relief->pixel, 1);
relief->allocated_p = 0;
}
/* Allocate new color. */
xgcv.foreground = default_pixel;
pixel = background;
if (dpyinfo->n_planes != 1
&& x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
{
relief->allocated_p = 1;
xgcv.foreground = relief->pixel = pixel;
}
if (relief->gc == 0)
{
xgcv.stipple = dpyinfo->gray;
mask |= GCStipple;
relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
}
else
XChangeGC (dpy, relief->gc, mask, &xgcv);
}
/* Set up colors for the relief lines around glyph string S. */
static void
x_setup_relief_colors (s)
struct glyph_string *s;
{
struct x_output *di = s->f->output_data.x;
unsigned long color;
if (s->face->use_box_color_for_shadows_p)
color = s->face->box_color;
else if (s->first_glyph->type == IMAGE_GLYPH
&& s->img->pixmap
&& !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
color = IMAGE_BACKGROUND (s->img, s->f, 0);
else
{
XGCValues xgcv;
/* Get the background color of the face. */
XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
color = xgcv.background;
}
if (di->white_relief.gc == 0
|| color != di->relief_background)
{
di->relief_background = color;
x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
WHITE_PIX_DEFAULT (s->f));
x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
BLACK_PIX_DEFAULT (s->f));
}
}
/* Draw a relief on frame F inside the rectangle given by LEFT_X,
TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
to draw, it must be >= 0. RAISED_P non-zero means draw a raised
relief. LEFT_P non-zero means draw a relief on the left side of
the rectangle. RIGHT_P non-zero means draw a relief on the right
side of the rectangle. CLIP_RECT is the clipping rectangle to use
when drawing. */
static void
x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
raised_p, top_p, bot_p, left_p, right_p, clip_rect)
struct frame *f;
int left_x, top_y, right_x, bottom_y, width;
int top_p, bot_p, left_p, right_p, raised_p;
XRectangle *clip_rect;
{
Display *dpy = FRAME_X_DISPLAY (f);
Window window = FRAME_X_WINDOW (f);
int i;
GC gc;
if (raised_p)
gc = f->output_data.x->white_relief.gc;
else
gc = f->output_data.x->black_relief.gc;
XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
/* Top. */
if (top_p)
for (i = 0; i < width; ++i)
XDrawLine (dpy, window, gc,
left_x + i * left_p, top_y + i,
right_x + 1 - i * right_p, top_y + i);
/* Left. */
if (left_p)
for (i = 0; i < width; ++i)
XDrawLine (dpy, window, gc,
left_x + i, top_y + i, left_x + i, bottom_y - i + 1);
XSetClipMask (dpy, gc, None);
if (raised_p)
gc = f->output_data.x->black_relief.gc;
else
gc = f->output_data.x->white_relief.gc;
XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
/* Bottom. */
if (bot_p)
for (i = 0; i < width; ++i)
XDrawLine (dpy, window, gc,
left_x + i * left_p, bottom_y - i,
right_x + 1 - i * right_p, bottom_y - i);
/* Right. */
if (right_p)
for (i = 0; i < width; ++i)
XDrawLine (dpy, window, gc,
right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
XSetClipMask (dpy, gc, None);
}
/* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
draw, it must be >= 0. LEFT_P non-zero means draw a line on the
left side of the rectangle. RIGHT_P non-zero means draw a line
on the right side of the rectangle. CLIP_RECT is the clipping
rectangle to use when drawing. */
static void
x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
left_p, right_p, clip_rect)
struct glyph_string *s;
int left_x, top_y, right_x, bottom_y, width, left_p, right_p;
XRectangle *clip_rect;
{
XGCValues xgcv;
XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
XSetForeground (s->display, s->gc, s->face->box_color);
XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
/* Top. */
XFillRectangle (s->display, s->window, s->gc,
left_x, top_y, right_x - left_x + 1, width);
/* Left. */
if (left_p)
XFillRectangle (s->display, s->window, s->gc,
left_x, top_y, width, bottom_y - top_y + 1);
/* Bottom. */
XFillRectangle (s->display, s->window, s->gc,
left_x, bottom_y - width + 1, right_x - left_x + 1, width);
/* Right. */
if (right_p)
XFillRectangle (s->display, s->window, s->gc,
right_x - width + 1, top_y, width, bottom_y - top_y + 1);
XSetForeground (s->display, s->gc, xgcv.foreground);
XSetClipMask (s->display, s->gc, None);
}
/* Draw a box around glyph string S. */
static void
x_draw_glyph_string_box (s)
struct glyph_string *s;
{
int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
int left_p, right_p;
struct glyph *last_glyph;
XRectangle clip_rect;
last_x = ((s->row->full_width_p && !s->w->pseudo_window_p)
? WINDOW_RIGHT_EDGE_X (s->w)
: window_box_right (s->w, s->area));
/* The glyph that may have a right box line. */
last_glyph = (s->cmp || s->img
? s->first_glyph
: s->first_glyph + s->nchars - 1);
width = eabs (s->face->box_line_width);
raised_p = s->face->box == FACE_RAISED_BOX;
left_x = s->x;
right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
? last_x - 1
: min (last_x, s->x + s->background_width) - 1);
top_y = s->y;
bottom_y = top_y + s->height - 1;
left_p = (s->first_glyph->left_box_line_p
|| (s->hl == DRAW_MOUSE_FACE
&& (s->prev == NULL
|| s->prev->hl != s->hl)));
right_p = (last_glyph->right_box_line_p
|| (s->hl == DRAW_MOUSE_FACE
&& (s->next == NULL
|| s->next->hl != s->hl)));
get_glyph_string_clip_rect (s, &clip_rect);
if (s->face->box == FACE_SIMPLE_BOX)
x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
left_p, right_p, &clip_rect);
else
{
x_setup_relief_colors (s);
x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
width, raised_p, 1, 1, left_p, right_p, &clip_rect);
}
}
/* Draw foreground of image glyph string S. */
static void
x_draw_image_foreground (s)
struct glyph_string *s;
{
int x = s->x;
int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
/* If first glyph of S has a left box line, start drawing it to the
right of that line. */
if (s->face->box != FACE_NO_BOX
&& s->first_glyph->left_box_line_p
&& s->slice.x == 0)
x += eabs (s->face->box_line_width);
/* If there is a margin around the image, adjust x- and y-position
by that margin. */
if (s->slice.x == 0)
x += s->img->hmargin;
if (s->slice.y == 0)
y += s->img->vmargin;
if (s->img->pixmap)
{
if (s->img->mask)
{
/* We can't set both a clip mask and use XSetClipRectangles
because the latter also sets a clip mask. We also can't
trust on the shape extension to be available
(XShapeCombineRegion). So, compute the rectangle to draw
manually. */
unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
| GCFunction);
XGCValues xgcv;
XRectangle clip_rect, image_rect, r;
xgcv.clip_mask = s->img->mask;
xgcv.clip_x_origin = x;
xgcv.clip_y_origin = y;
xgcv.function = GXcopy;
XChangeGC (s->display, s->gc, mask, &xgcv);
get_glyph_string_clip_rect (s, &clip_rect);
image_rect.x = x;
image_rect.y = y;
image_rect.width = s->slice.width;
image_rect.height = s->slice.height;
if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
s->slice.x + r.x - x, s->slice.y + r.y - y,
r.width, r.height, r.x, r.y);
}
else
{
XRectangle clip_rect, image_rect, r;
get_glyph_string_clip_rect (s, &clip_rect);
image_rect.x = x;
image_rect.y = y;
image_rect.width = s->slice.width;
image_rect.height = s->slice.height;
if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
s->slice.x + r.x - x, s->slice.y + r.y - y,
r.width, r.height, r.x, r.y);
/* When the image has a mask, we can expect that at
least part of a mouse highlight or a block cursor will
be visible. If the image doesn't have a mask, make
a block cursor visible by drawing a rectangle around
the image. I believe it's looking better if we do
nothing here for mouse-face. */
if (s->hl == DRAW_CURSOR)
{
int r = s->img->relief;
if (r < 0) r = -r;
XDrawRectangle (s->display, s->window, s->gc,
x - r, y - r,
s->slice.width + r*2 - 1,
s->slice.height + r*2 - 1);
}
}
}
else
/* Draw a rectangle if image could not be loaded. */
XDrawRectangle (s->display, s->window, s->gc, x, y,
s->slice.width - 1, s->slice.height - 1);
}
/* Draw a relief around the image glyph string S. */
static void
x_draw_image_relief (s)
struct glyph_string *s;
{
int x0, y0, x1, y1, thick, raised_p;
XRectangle r;
int x = s->x;
int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
/* If first glyph of S has a left box line, start drawing it to the
right of that line. */
if (s->face->box != FACE_NO_BOX
&& s->first_glyph->left_box_line_p
&& s->slice.x == 0)
x += eabs (s->face->box_line_width);
/* If there is a margin around the image, adjust x- and y-position
by that margin. */
if (s->slice.x == 0)
x += s->img->hmargin;
if (s->slice.y == 0)
y += s->img->vmargin;
if (s->hl == DRAW_IMAGE_SUNKEN
|| s->hl == DRAW_IMAGE_RAISED)
{
thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
raised_p = s->hl == DRAW_IMAGE_RAISED;
}
else
{
thick = eabs (s->img->relief);
raised_p = s->img->relief > 0;
}
x0 = x - thick;
y0 = y - thick;
x1 = x + s->slice.width + thick - 1;
y1 = y + s->slice.height + thick - 1;
x_setup_relief_colors (s);
get_glyph_string_clip_rect (s, &r);
x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p,
s->slice.y == 0,
s->slice.y + s->slice.height == s->img->height,
s->slice.x == 0,
s->slice.x + s->slice.width == s->img->width,
&r);
}
/* Draw the foreground of image glyph string S to PIXMAP. */
static void
x_draw_image_foreground_1 (s, pixmap)
struct glyph_string *s;
Pixmap pixmap;
{
int x = 0;
int y = s->ybase - s->y - image_ascent (s->img, s->face, &s->slice);
/* If first glyph of S has a left box line, start drawing it to the
right of that line. */
if (s->face->box != FACE_NO_BOX
&& s->first_glyph->left_box_line_p
&& s->slice.x == 0)
x += eabs (s->face->box_line_width);
/* If there is a margin around the image, adjust x- and y-position
by that margin. */
if (s->slice.x == 0)
x += s->img->hmargin;
if (s->slice.y == 0)
y += s->img->vmargin;
if (s->img->pixmap)
{
if (s->img->mask)
{
/* We can't set both a clip mask and use XSetClipRectangles
because the latter also sets a clip mask. We also can't
trust on the shape extension to be available
(XShapeCombineRegion). So, compute the rectangle to draw
manually. */
unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
| GCFunction);
XGCValues xgcv;
xgcv.clip_mask = s->img->mask;
xgcv.clip_x_origin = x - s->slice.x;
xgcv.clip_y_origin = y - s->slice.y;
xgcv.function = GXcopy;
XChangeGC (s->display, s->gc, mask, &xgcv);
XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
s->slice.x, s->slice.y,
s->slice.width, s->slice.height, x, y);
XSetClipMask (s->display, s->gc, None);
}
else
{
XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
s->slice.x, s->slice.y,
s->slice.width, s->slice.height, x, y);
/* When the image has a mask, we can expect that at
least part of a mouse highlight or a block cursor will
be visible. If the image doesn't have a mask, make
a block cursor visible by drawing a rectangle around
the image. I believe it's looking better if we do
nothing here for mouse-face. */
if (s->hl == DRAW_CURSOR)
{
int r = s->img->relief;
if (r < 0) r = -r;
XDrawRectangle (s->display, s->window, s->gc, x - r, y - r,
s->slice.width + r*2 - 1,
s->slice.height + r*2 - 1);
}
}
}
else
/* Draw a rectangle if image could not be loaded. */
XDrawRectangle (s->display, pixmap, s->gc, x, y,
s->slice.width - 1, s->slice.height - 1);
}
/* Draw part of the background of glyph string S. X, Y, W, and H
give the rectangle to draw. */
static void
x_draw_glyph_string_bg_rect (s, x, y, w, h)
struct glyph_string *s;
int x, y, w, h;
{
if (s->stippled_p)
{
/* Fill background with a stipple pattern. */
XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
XSetFillStyle (s->display, s->gc, FillSolid);
}
else
x_clear_glyph_string_rect (s, x, y, w, h);
}
/* Draw image glyph string S.
s->y
s->x +-------------------------
| s->face->box
|
| +-------------------------
| | s->img->margin
| |
| | +-------------------
| | | the image
*/
static void
x_draw_image_glyph_string (s)
struct glyph_string *s;
{
int box_line_hwidth = eabs (s->face->box_line_width);
int box_line_vwidth = max (s->face->box_line_width, 0);
int height;
Pixmap pixmap = None;
height = s->height;
if (s->slice.y == 0)
height -= box_line_vwidth;
if (s->slice.y + s->slice.height >= s->img->height)
height -= box_line_vwidth;
/* Fill background with face under the image. Do it only if row is
taller than image or if image has a clip mask to reduce
flickering. */
s->stippled_p = s->face->stipple != 0;
if (height > s->slice.height
|| s->img->hmargin
|| s->img->vmargin
|| s->img->mask
|| s->img->pixmap == 0
|| s->width != s->background_width)
{
if (s->img->mask)
{
/* Create a pixmap as large as the glyph string. Fill it
with the background color. Copy the image to it, using
its mask. Copy the temporary pixmap to the display. */
Screen *screen = FRAME_X_SCREEN (s->f);
int depth = DefaultDepthOfScreen (screen);
/* Create a pixmap as large as the glyph string. */
pixmap = XCreatePixmap (s->display, s->window,
s->background_width,
s->height, depth);
/* Don't clip in the following because we're working on the
pixmap. */
XSetClipMask (s->display, s->gc, None);
/* Fill the pixmap with the background color/stipple. */
if (s->stippled_p)
{
/* Fill background with a stipple pattern. */
XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
XSetTSOrigin (s->display, s->gc, - s->x, - s->y);
XFillRectangle (s->display, pixmap, s->gc,
0, 0, s->background_width, s->height);
XSetFillStyle (s->display, s->gc, FillSolid);
XSetTSOrigin (s->display, s->gc, 0, 0);
}
else
{
XGCValues xgcv;
XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
&xgcv);
XSetForeground (s->display, s->gc, xgcv.background);
XFillRectangle (s->display, pixmap, s->gc,
0, 0, s->background_width, s->height);
XSetForeground (s->display, s->gc, xgcv.foreground);
}
}
else
{
int x = s->x;
int y = s->y;
if (s->first_glyph->left_box_line_p
&& s->slice.x == 0)
x += box_line_hwidth;
if (s->slice.y == 0)
y += box_line_vwidth;
x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
}
s->background_filled_p = 1;
}
/* Draw the foreground. */
if (pixmap != None)
{
x_draw_image_foreground_1 (s, pixmap);
x_set_glyph_string_clipping (s);
XCopyArea (s->display, pixmap, s->window, s->gc,
0, 0, s->background_width, s->height, s->x, s->y);
XFreePixmap (s->display, pixmap);
}
else
x_draw_image_foreground (s);
/* If we must draw a relief around the image, do it. */
if (s->img->relief
|| s->hl == DRAW_IMAGE_RAISED
|| s->hl == DRAW_IMAGE_SUNKEN)
x_draw_image_relief (s);
}
/* Draw stretch glyph string S. */
static void
x_draw_stretch_glyph_string (s)
struct glyph_string *s;
{
xassert (s->first_glyph->type == STRETCH_GLYPH);
if (s->hl == DRAW_CURSOR
&& !x_stretch_cursor_p)
{
/* If `x-stretch-block-cursor' is nil, don't draw a block cursor
as wide as the stretch glyph. */
int width, background_width = s->background_width;
int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
if (x < left_x)
{
background_width -= left_x - x;
x = left_x;
}
width = min (FRAME_COLUMN_WIDTH (s->f), background_width);
/* Draw cursor. */
x_draw_glyph_string_bg_rect (s, x, s->y, width, s->height);
/* Clear rest using the GC of the original non-cursor face. */
if (width < background_width)
{
int y = s->y;
int w = background_width - width, h = s->height;
XRectangle r;
GC gc;
x += width;
if (s->row->mouse_face_p
&& cursor_in_mouse_face_p (s->w))
{
x_set_mouse_face_gc (s);
gc = s->gc;
}
else
gc = s->face->gc;
get_glyph_string_clip_rect (s, &r);
XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
if (s->face->stipple)
{
/* Fill background with a stipple pattern. */
XSetFillStyle (s->display, gc, FillOpaqueStippled);
XFillRectangle (s->display, s->window, gc, x, y, w, h);
XSetFillStyle (s->display, gc, FillSolid);
}
else
{
XGCValues xgcv;
XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
XSetForeground (s->display, gc, xgcv.background);
XFillRectangle (s->display, s->window, gc, x, y, w, h);
XSetForeground (s->display, gc, xgcv.foreground);
}
}
}
else if (!s->background_filled_p)
{
int background_width = s->background_width;
int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
/* Don't draw into left margin, fringe or scrollbar area
except for header line and mode line. */
if (x < left_x && !s->row->mode_line_p)
{
background_width -= left_x - x;
x = left_x;
}
if (background_width > 0)
x_draw_glyph_string_bg_rect (s, x, s->y, background_width, s->height);
}
s->background_filled_p = 1;
}
/* Draw glyph string S. */
static void
x_draw_glyph_string (s)
struct glyph_string *s;
{
int relief_drawn_p = 0;
/* If S draws into the background of its successors, draw the
background of the successors first so that S can draw into it.
This makes S->next use XDrawString instead of XDrawImageString. */
if (s->next && s->right_overhang && !s->for_overlaps)
{
int width;
struct glyph_string *next;
for (width = 0, next = s->next;
next && width < s->right_overhang;
width += next->width, next = next->next)
if (next->first_glyph->type != IMAGE_GLYPH)
{
x_set_glyph_string_gc (next);
x_set_glyph_string_clipping (next);
if (next->first_glyph->type == STRETCH_GLYPH)
x_draw_stretch_glyph_string (next);
else
x_draw_glyph_string_background (next, 1);
next->num_clips = 0;
}
}
/* Set up S->gc, set clipping and draw S. */
x_set_glyph_string_gc (s);
/* Draw relief (if any) in advance for char/composition so that the
glyph string can be drawn over it. */
if (!s->for_overlaps
&& s->face->box != FACE_NO_BOX
&& (s->first_glyph->type == CHAR_GLYPH
|| s->first_glyph->type == COMPOSITE_GLYPH))
{
x_set_glyph_string_clipping (s);
x_draw_glyph_string_background (s, 1);
x_draw_glyph_string_box (s);
x_set_glyph_string_clipping (s);
relief_drawn_p = 1;
}
else if (!s->clip_head /* draw_glyphs didn't specify a clip mask. */
&& !s->clip_tail
&& ((s->prev && s->prev->hl != s->hl && s->left_overhang)
|| (s->next && s->next->hl != s->hl && s->right_overhang)))
/* We must clip just this glyph. left_overhang part has already
drawn when s->prev was drawn, and right_overhang part will be
drawn later when s->next is drawn. */
x_set_glyph_string_clipping_exactly (s, s);
else
x_set_glyph_string_clipping (s);
switch (s->first_glyph->type)
{
case IMAGE_GLYPH:
x_draw_image_glyph_string (s);
break;
case STRETCH_GLYPH:
x_draw_stretch_glyph_string (s);
break;
case CHAR_GLYPH:
if (s->for_overlaps)
s->background_filled_p = 1;
else
x_draw_glyph_string_background (s, 0);
x_draw_glyph_string_foreground (s);
break;
case COMPOSITE_GLYPH:
if (s->for_overlaps || (s->cmp_from > 0
&& ! s->first_glyph->u.cmp.automatic))
s->background_filled_p = 1;
else
x_draw_glyph_string_background (s, 1);
x_draw_composite_glyph_string_foreground (s);
break;
default:
abort ();
}
if (!s->for_overlaps)
{
/* Draw underline. */
if (s->face->underline_p)
{
unsigned long thickness, position;
int y;
if (s->prev && s->prev->face->underline_p)
{
/* We use the same underline style as the previous one. */
thickness = s->prev->underline_thickness;
position = s->prev->underline_position;
}
else
{
/* Get the underline thickness. Default is 1 pixel. */
if (s->font && s->font->underline_thickness > 0)
thickness = s->font->underline_thickness;
else
thickness = 1;
if (x_underline_at_descent_line)
position = (s->height - thickness) - (s->ybase - s->y);
else
{
/* Get the underline position. This is the recommended
vertical offset in pixels from the baseline to the top of
the underline. This is a signed value according to the
specs, and its default is
ROUND ((maximum descent) / 2), with
ROUND(x) = floor (x + 0.5) */
if (x_use_underline_position_properties
&& s->font && s->font->underline_position >= 0)
position = s->font->underline_position;
else if (s->font)
position = (s->font->descent + 1) / 2;
else
position = underline_minimum_offset;
}
position = max (position, underline_minimum_offset);
}
/* Check the sanity of thickness and position. We should
avoid drawing underline out of the current line area. */
if (s->y + s->height <= s->ybase + position)
position = (s->height - 1) - (s->ybase - s->y);
if (s->y + s->height < s->ybase + position + thickness)
thickness = (s->y + s->height) - (s->ybase + position);
s->underline_thickness = thickness;
s->underline_position = position;
y = s->ybase + position;
if (s->face->underline_defaulted_p)
XFillRectangle (s->display, s->window, s->gc,
s->x, y, s->width, thickness);
else
{
XGCValues xgcv;
XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
XSetForeground (s->display, s->gc, s->face->underline_color);
XFillRectangle (s->display, s->window, s->gc,
s->x, y, s->width, thickness);
XSetForeground (s->display, s->gc, xgcv.foreground);
}
}
/* Draw overline. */
if (s->face->overline_p)
{
unsigned long dy = 0, h = 1;
if (s->face->overline_color_defaulted_p)
XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
s->width, h);
else
{
XGCValues xgcv;
XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
XSetForeground (s->display, s->gc, s->face->overline_color);
XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
s->width, h);
XSetForeground (s->display, s->gc, xgcv.foreground);
}
}
/* Draw strike-through. */
if (s->face->strike_through_p)
{
unsigned long h = 1;
unsigned long dy = (s->height - h) / 2;
if (s->face->strike_through_color_defaulted_p)
XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
s->width, h);
else
{
XGCValues xgcv;
XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
XSetForeground (s->display, s->gc, s->face->strike_through_color);
XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
s->width, h);
XSetForeground (s->display, s->gc, xgcv.foreground);
}
}
/* Draw relief if not yet drawn. */
if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
x_draw_glyph_string_box (s);
if (s->prev)
{
struct glyph_string *prev;
for (prev = s->prev; prev; prev = prev->prev)
if (prev->hl != s->hl
&& prev->x + prev->width + prev->right_overhang > s->x)
{
/* As prev was drawn while clipped to its own area, we
must draw the right_overhang part using s->hl now. */
enum draw_glyphs_face save = prev->hl;
prev->hl = s->hl;
x_set_glyph_string_gc (prev);
x_set_glyph_string_clipping_exactly (s, prev);
if (prev->first_glyph->type == CHAR_GLYPH)
x_draw_glyph_string_foreground (prev);
else
x_draw_composite_glyph_string_foreground (prev);
XSetClipMask (prev->display, prev->gc, None);
prev->hl = save;
prev->num_clips = 0;
}
}
if (s->next)
{
struct glyph_string *next;
for (next = s->next; next; next = next->next)
if (next->hl != s->hl
&& next->x - next->left_overhang < s->x + s->width)
{
/* As next will be drawn while clipped to its own area,
we must draw the left_overhang part using s->hl now. */
enum draw_glyphs_face save = next->hl;
next->hl = s->hl;
x_set_glyph_string_gc (next);
x_set_glyph_string_clipping_exactly (s, next);
if (next->first_glyph->type == CHAR_GLYPH)
x_draw_glyph_string_foreground (next);
else
x_draw_composite_glyph_string_foreground (next);
XSetClipMask (next->display, next->gc, None);
next->hl = save;
next->num_clips = 0;
}
}
}
/* Reset clipping. */
XSetClipMask (s->display, s->gc, None);
s->num_clips = 0;
}
/* Shift display to make room for inserted glyphs. */
void
x_shift_glyphs_for_insert (f, x, y, width, height, shift_by)
struct frame *f;
int x, y, width, height, shift_by;
{
XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
f->output_data.x->normal_gc,
x, y, width, height,
x + shift_by, y);
}
/* Delete N glyphs at the nominal cursor position. Not implemented
for X frames. */
static void
x_delete_glyphs (f, n)
struct frame *f;
register int n;
{
abort ();
}
/* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
If they are <= 0, this is probably an error. */
void
x_clear_area (dpy, window, x, y, width, height, exposures)
Display *dpy;
Window window;
int x, y;
int width, height;
int exposures;
{
xassert (width > 0 && height > 0);
XClearArea (dpy, window, x, y, width, height, exposures);
}
/* Clear an entire frame. */
static void
x_clear_frame (struct frame *f)
{
/* Clearing the frame will erase any cursor, so mark them all as no
longer visible. */
mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
output_cursor.hpos = output_cursor.vpos = 0;
output_cursor.x = -1;
/* We don't set the output cursor here because there will always
follow an explicit cursor_to. */
BLOCK_INPUT;
XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
/* We have to clear the scroll bars, too. If we have changed
colors or something like that, then they should be notified. */
x_scroll_bar_clear (f);
#if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
/* Make sure scroll bars are redrawn. As they aren't redrawn by
redisplay, do it here. */
gtk_widget_queue_draw (FRAME_GTK_WIDGET (f));
#endif
XFlush (FRAME_X_DISPLAY (f));
UNBLOCK_INPUT;
}
/* Invert the middle quarter of the frame for .15 sec. */
/* We use the select system call to do the waiting, so we have to make
sure it's available. If it isn't, we just won't do visual bells. */
#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
/* Subtract the `struct timeval' values X and Y, storing the result in
*RESULT. Return 1 if the difference is negative, otherwise 0. */
static int
timeval_subtract (result, x, y)
struct timeval *result, x, y;
{
/* Perform the carry for the later subtraction by updating y. This
is safer because on some systems the tv_sec member is unsigned. */
if (x.tv_usec < y.tv_usec)
{
int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
y.tv_usec -= 1000000 * nsec;
y.tv_sec += nsec;
}
if (x.tv_usec - y.tv_usec > 1000000)
{
int nsec = (y.tv_usec - x.tv_usec) / 1000000;
y.tv_usec += 1000000 * nsec;
y.tv_sec -= nsec;
}
/* Compute the time remaining to wait. tv_usec is certainly
positive. */
result->tv_sec = x.tv_sec - y.tv_sec;
result->tv_usec = x.tv_usec - y.tv_usec;
/* Return indication of whether the result should be considered
negative. */
return x.tv_sec < y.tv_sec;
}
void
XTflash (f)
struct frame *f;
{
BLOCK_INPUT;
{
#ifdef USE_GTK
/* Use Gdk routines to draw. This way, we won't draw over scroll bars
when the scroll bars and the edit widget share the same X window. */
GdkGCValues vals;
GdkGC *gc;
vals.foreground.pixel = (FRAME_FOREGROUND_PIXEL (f)
^ FRAME_BACKGROUND_PIXEL (f));
vals.function = GDK_XOR;
gc = gdk_gc_new_with_values (FRAME_GTK_WIDGET (f)->window,
&vals,
GDK_GC_FUNCTION
| GDK_GC_FOREGROUND);
#define XFillRectangle(d, win, gc, x, y, w, h) \
gdk_draw_rectangle (FRAME_GTK_WIDGET (f)->window, \
gc, TRUE, x, y, w, h)
#else
GC gc;
/* Create a GC that will use the GXxor function to flip foreground
pixels into background pixels. */
{
XGCValues values;
values.function = GXxor;
values.foreground = (FRAME_FOREGROUND_PIXEL (f)
^ FRAME_BACKGROUND_PIXEL (f));
gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
GCFunction | GCForeground, &values);
}
#endif
{
/* Get the height not including a menu bar widget. */
int height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, FRAME_LINES (f));
/* Height of each line to flash. */
int flash_height = FRAME_LINE_HEIGHT (f);
/* These will be the left and right margins of the rectangles. */
int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
int flash_right = FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
int width;
/* Don't flash the area between a scroll bar and the frame
edge it is next to. */
switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
{
case vertical_scroll_bar_left:
flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
break;
case vertical_scroll_bar_right:
flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
break;
default:
break;
}
width = flash_right - flash_left;
/* If window is tall, flash top and bottom line. */
if (height > 3 * FRAME_LINE_HEIGHT (f))
{
XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
flash_left,
(FRAME_INTERNAL_BORDER_WIDTH (f)
+ FRAME_TOP_MARGIN_HEIGHT (f)),
width, flash_height);
XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
flash_left,
(height - flash_height
- FRAME_INTERNAL_BORDER_WIDTH (f)),
width, flash_height);
}
else
/* If it is short, flash it all. */
XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
x_flush (f);
{
struct timeval wakeup;
EMACS_GET_TIME (wakeup);
/* Compute time to wait until, propagating carry from usecs. */
wakeup.tv_usec += 150000;
wakeup.tv_sec += (wakeup.tv_usec / 1000000);
wakeup.tv_usec %= 1000000;
/* Keep waiting until past the time wakeup or any input gets
available. */
while (! detect_input_pending ())
{
struct timeval current;
struct timeval timeout;
EMACS_GET_TIME (current);
/* Break if result would be negative. */
if (timeval_subtract (&current, wakeup, current))
break;
/* How long `select' should wait. */
timeout.tv_sec = 0;
timeout.tv_usec = 10000;
/* Try to wait that long--but we might wake up sooner. */
select (0, NULL, NULL, NULL, &timeout);
}
}
/* If window is tall, flash top and bottom line. */
if (height > 3 * FRAME_LINE_HEIGHT (f))
{
XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
flash_left,
(FRAME_INTERNAL_BORDER_WIDTH (f)
+ FRAME_TOP_MARGIN_HEIGHT (f)),
width, flash_height);
XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
flash_left,
(height - flash_height
- FRAME_INTERNAL_BORDER_WIDTH (f)),
width, flash_height);
}
else
/* If it is short, flash it all. */
XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
#ifdef USE_GTK
g_object_unref (G_OBJECT (gc));
#undef XFillRectangle
#else
XFreeGC (FRAME_X_DISPLAY (f), gc);
#endif
x_flush (f);
}
}
UNBLOCK_INPUT;
}
#endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
static void
XTtoggle_invisible_pointer (f, invisible)
FRAME_PTR f;
int invisible;
{
BLOCK_INPUT;
if (invisible)
{
if (FRAME_X_DISPLAY_INFO (f)->invisible_cursor != 0)
XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
FRAME_X_DISPLAY_INFO (f)->invisible_cursor);
}
else
XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
f->output_data.x->current_cursor);
f->pointer_invisible = invisible;
UNBLOCK_INPUT;
}
/* Make audible bell. */
void
XTring_bell ()
{
struct frame *f = SELECTED_FRAME ();
if (FRAME_X_DISPLAY (f))
{
#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
if (visible_bell)
XTflash (f);
else
#endif
{
BLOCK_INPUT;
XBell (FRAME_X_DISPLAY (f), 0);
XFlush (FRAME_X_DISPLAY (f));
UNBLOCK_INPUT;
}
}
}
/* Specify how many text lines, from the top of the window,
should be affected by insert-lines and delete-lines operations.
This, and those operations, are used only within an update
that is bounded by calls to x_update_begin and x_update_end. */
static void
XTset_terminal_window (n)
register int n;
{
/* This function intentionally left blank. */
}
/***********************************************************************
Line Dance
***********************************************************************/
/* Perform an insert-lines or delete-lines operation, inserting N
lines or deleting -N lines at vertical position VPOS. */
static void
x_ins_del_lines (f, vpos, n)
struct frame *f;
int vpos, n;
{
abort ();
}
/* Scroll part of the display as described by RUN. */
static void
x_scroll_run (w, run)
struct window *w;
struct run *run;
{
struct frame *f = XFRAME (w->frame);
int x, y, width, height, from_y, to_y, bottom_y;
/* Get frame-relative bounding box of the text display area of W,
without mode lines. Include in this box the left and right
fringe of W. */
window_box (w, -1, &x, &y, &width, &height);
#ifdef USE_TOOLKIT_SCROLL_BARS
/* If the fringe is adjacent to the left (right) scroll bar of a
leftmost (rightmost, respectively) window, then extend its
background to the gap between the fringe and the bar. */
if ((WINDOW_LEFTMOST_P (w)
&& WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
|| (WINDOW_RIGHTMOST_P (w)
&& WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w)))
{
int sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
if (sb_width > 0)
{
int bar_area_x = WINDOW_SCROLL_BAR_AREA_X (w);
int bar_area_width = (WINDOW_CONFIG_SCROLL_BAR_COLS (w)
* FRAME_COLUMN_WIDTH (f));
if (bar_area_x + bar_area_width == x)
{
x = bar_area_x + sb_width;
width += bar_area_width - sb_width;
}
else if (x + width == bar_area_x)
width += bar_area_width - sb_width;
}
}
#endif
from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
bottom_y = y + height;
if (to_y < from_y)
{
/* Scrolling up. Make sure we don't copy part of the mode
line at the bottom. */
if (from_y + run->height > bottom_y)
height = bottom_y - from_y;
else
height = run->height;
}
else
{
/* Scolling down. Make sure we don't copy over the mode line.
at the bottom. */
if (to_y + run->height > bottom_y)
height = bottom_y - to_y;
else
height = run->height;
}
BLOCK_INPUT;
/* Cursor off. Will be switched on again in x_update_window_end. */
updated_window = w;
x_clear_cursor (w);
XCopyArea (FRAME_X_DISPLAY (f),
FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
f->output_data.x->normal_gc,
x, from_y,
width, height,
x, to_y);
UNBLOCK_INPUT;
}
/***********************************************************************
Exposure Events
***********************************************************************/
static void
frame_highlight (f)
struct frame *f;
{
/* We used to only do this if Vx_no_window_manager was non-nil, but
the ICCCM (section 4.1.6) says that the window's border pixmap
and border pixel are window attributes which are "private to the
client", so we can always change it to whatever we want. */
BLOCK_INPUT;
XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
f->output_data.x->border_pixel);
UNBLOCK_INPUT;
x_update_cursor (f, 1);
x_set_frame_alpha (f);
}
static void
frame_unhighlight (f)
struct frame *f;
{
/* We used to only do this if Vx_no_window_manager was non-nil, but
the ICCCM (section 4.1.6) says that the window's border pixmap
and border pixel are window attributes which are "private to the
client", so we can always change it to whatever we want. */
BLOCK_INPUT;
XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
f->output_data.x->border_tile);
UNBLOCK_INPUT;
x_update_cursor (f, 1);
x_set_frame_alpha (f);
}
/* The focus has changed. Update the frames as necessary to reflect
the new situation. Note that we can't change the selected frame
here, because the Lisp code we are interrupting might become confused.
Each event gets marked with the frame in which it occurred, so the
Lisp code can tell when the switch took place by examining the events. */
static void
x_new_focus_frame (dpyinfo, frame)
struct x_display_info *dpyinfo;
struct frame *frame;
{
struct frame *old_focus = dpyinfo->x_focus_frame;
if (frame != dpyinfo->x_focus_frame)
{
/* Set this before calling other routines, so that they see
the correct value of x_focus_frame. */
dpyinfo->x_focus_frame = frame;
if (old_focus && old_focus->auto_lower)
x_lower_frame (old_focus);
if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
pending_autoraise_frame = dpyinfo->x_focus_frame;
else
pending_autoraise_frame = 0;
}
x_frame_rehighlight (dpyinfo);
}
/* Handle FocusIn and FocusOut state changes for FRAME.
If FRAME has focus and there exists more than one frame, puts
a FOCUS_IN_EVENT into *BUFP. */
static void
x_focus_changed (type, state, dpyinfo, frame, bufp)
int type;
int state;
struct x_display_info *dpyinfo;
struct frame *frame;
struct input_event *bufp;
{
if (type == FocusIn)
{
if (dpyinfo->x_focus_event_frame != frame)
{
x_new_focus_frame (dpyinfo, frame);
dpyinfo->x_focus_event_frame = frame;
/* Don't stop displaying the initial startup message
for a switch-frame event we don't need. */
if (NILP (Vterminal_frame)
&& CONSP (Vframe_list)
&& !NILP (XCDR (Vframe_list)))
{
bufp->kind = FOCUS_IN_EVENT;
XSETFRAME (bufp->frame_or_window, frame);
}
}
frame->output_data.x->focus_state |= state;
#ifdef HAVE_X_I18N
if (FRAME_XIC (frame))
XSetICFocus (FRAME_XIC (frame));
#endif
}
else if (type == FocusOut)
{
frame->output_data.x->focus_state &= ~state;
if (dpyinfo->x_focus_event_frame == frame)
{
dpyinfo->x_focus_event_frame = 0;
x_new_focus_frame (dpyinfo, 0);
}
#ifdef HAVE_X_I18N
if (FRAME_XIC (frame))
XUnsetICFocus (FRAME_XIC (frame));
#endif
if (frame->pointer_invisible)
XTtoggle_invisible_pointer (frame, 0);
}
}
/* The focus may have changed. Figure out if it is a real focus change,
by checking both FocusIn/Out and Enter/LeaveNotify events.
Returns FOCUS_IN_EVENT event in *BUFP. */
static void
x_detect_focus_change (dpyinfo, event, bufp)
struct x_display_info *dpyinfo;
XEvent *event;
struct input_event *bufp;
{
struct frame *frame;
frame = x_any_window_to_frame (dpyinfo, event->xany.window);
if (! frame)
return;
switch (event->type)
{
case EnterNotify:
case LeaveNotify:
{
struct frame *focus_frame = dpyinfo->x_focus_event_frame;
int focus_state
= focus_frame ? focus_frame->output_data.x->focus_state : 0;
if (event->xcrossing.detail != NotifyInferior
&& event->xcrossing.focus
&& ! (focus_state & FOCUS_EXPLICIT))
x_focus_changed ((event->type == EnterNotify ? FocusIn : FocusOut),
FOCUS_IMPLICIT,
dpyinfo, frame, bufp);
}
break;
case FocusIn:
case FocusOut:
x_focus_changed (event->type,
(event->xfocus.detail == NotifyPointer ?
FOCUS_IMPLICIT : FOCUS_EXPLICIT),
dpyinfo, frame, bufp);
break;
case ClientMessage:
if (event->xclient.message_type == dpyinfo->Xatom_XEMBED)
{
enum xembed_message msg = event->xclient.data.l[1];
x_focus_changed ((msg == XEMBED_FOCUS_IN ? FocusIn : FocusOut),
FOCUS_EXPLICIT, dpyinfo, frame, bufp);
}
break;
}
}
/* Handle an event saying the mouse has moved out of an Emacs frame. */
void
x_mouse_leave (dpyinfo)
struct x_display_info *dpyinfo;
{
x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
}
/* The focus has changed, or we have redirected a frame's focus to
another frame (this happens when a frame uses a surrogate
mini-buffer frame). Shift the highlight as appropriate.
The FRAME argument doesn't necessarily have anything to do with which
frame is being highlighted or un-highlighted; we only use it to find
the appropriate X display info. */
static void
XTframe_rehighlight (frame)
struct frame *frame;
{
x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
}
static void
x_frame_rehighlight (dpyinfo)
struct x_display_info *dpyinfo;
{
struct frame *old_highlight = dpyinfo->x_highlight_frame;
if (dpyinfo->x_focus_frame)
{
dpyinfo->x_highlight_frame
= ((FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
: dpyinfo->x_focus_frame);
if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
{
FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
}
}
else
dpyinfo->x_highlight_frame = 0;
if (dpyinfo->x_highlight_frame != old_highlight)
{
if (old_highlight)
frame_unhighlight (old_highlight);
if (dpyinfo->x_highlight_frame)
frame_highlight (dpyinfo->x_highlight_frame);
}
}
/* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
/* Initialize mode_switch_bit and modifier_meaning. */
static void
x_find_modifier_meanings (dpyinfo)
struct x_display_info *dpyinfo;
{
int min_code, max_code;
KeySym *syms;
int syms_per_code;
XModifierKeymap *mods;
dpyinfo->meta_mod_mask = 0;
dpyinfo->shift_lock_mask = 0;
dpyinfo->alt_mod_mask = 0;
dpyinfo->super_mod_mask = 0;
dpyinfo->hyper_mod_mask = 0;
XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
syms = XGetKeyboardMapping (dpyinfo->display,
min_code, max_code - min_code + 1,
&syms_per_code);
mods = XGetModifierMapping (dpyinfo->display);
/* Scan the modifier table to see which modifier bits the Meta and
Alt keysyms are on. */
{
int row, col; /* The row and column in the modifier table. */
int found_alt_or_meta;
for (row = 3; row < 8; row++)
{
found_alt_or_meta = 0;
for (col = 0; col < mods->max_keypermod; col++)
{
KeyCode code = mods->modifiermap[(row * mods->max_keypermod) + col];
/* Zeroes are used for filler. Skip them. */
if (code == 0)
continue;
/* Are any of this keycode's keysyms a meta key? */
{
int code_col;
for (code_col = 0; code_col < syms_per_code; code_col++)
{
int sym = syms[((code - min_code) * syms_per_code) + code_col];
switch (sym)
{
case XK_Meta_L:
case XK_Meta_R:
found_alt_or_meta = 1;
dpyinfo->meta_mod_mask |= (1 << row);
break;
case XK_Alt_L:
case XK_Alt_R:
found_alt_or_meta = 1;
dpyinfo->alt_mod_mask |= (1 << row);
break;
case XK_Hyper_L:
case XK_Hyper_R:
if (!found_alt_or_meta)
dpyinfo->hyper_mod_mask |= (1 << row);
code_col = syms_per_code;
col = mods->max_keypermod;
break;
case XK_Super_L:
case XK_Super_R:
if (!found_alt_or_meta)
dpyinfo->super_mod_mask |= (1 << row);
code_col = syms_per_code;
col = mods->max_keypermod;
break;
case XK_Shift_Lock:
/* Ignore this if it's not on the lock modifier. */
if (!found_alt_or_meta && ((1 << row) == LockMask))
dpyinfo->shift_lock_mask = LockMask;
code_col = syms_per_code;
col = mods->max_keypermod;
break;
}
}
}
}
}
}
/* If we couldn't find any meta keys, accept any alt keys as meta keys. */
if (! dpyinfo->meta_mod_mask)
{
dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
dpyinfo->alt_mod_mask = 0;
}
/* If some keys are both alt and meta,
make them just meta, not alt. */
if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
{
dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
}
XFree ((char *) syms);
XFreeModifiermap (mods);
}
/* Convert between the modifier bits X uses and the modifier bits
Emacs uses. */
unsigned int
x_x_to_emacs_modifiers (dpyinfo, state)
struct x_display_info *dpyinfo;
unsigned int state;
{
EMACS_UINT mod_meta = meta_modifier;
EMACS_UINT mod_alt = alt_modifier;
EMACS_UINT mod_hyper = hyper_modifier;
EMACS_UINT mod_super = super_modifier;
Lisp_Object tem;
tem = Fget (Vx_alt_keysym, Qmodifier_value);
if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
tem = Fget (Vx_meta_keysym, Qmodifier_value);
if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
tem = Fget (Vx_hyper_keysym, Qmodifier_value);
if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
tem = Fget (Vx_super_keysym, Qmodifier_value);
if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
| ((state & ControlMask) ? ctrl_modifier : 0)
| ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
| ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
| ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
| ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
}
static unsigned int
x_emacs_to_x_modifiers (dpyinfo, state)
struct x_display_info *dpyinfo;
unsigned int state;
{
EMACS_UINT mod_meta = meta_modifier;
EMACS_UINT mod_alt = alt_modifier;
EMACS_UINT mod_hyper = hyper_modifier;
EMACS_UINT mod_super = super_modifier;
Lisp_Object tem;
tem = Fget (Vx_alt_keysym, Qmodifier_value);
if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
tem = Fget (Vx_meta_keysym, Qmodifier_value);
if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
tem = Fget (Vx_hyper_keysym, Qmodifier_value);
if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
tem = Fget (Vx_super_keysym, Qmodifier_value);
if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
| ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
| ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
| ((state & shift_modifier) ? ShiftMask : 0)
| ((state & ctrl_modifier) ? ControlMask : 0)
| ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
}
/* Convert a keysym to its name. */
char *
x_get_keysym_name (keysym)
KeySym keysym;
{
char *value;
BLOCK_INPUT;
value = XKeysymToString (keysym);
UNBLOCK_INPUT;
return value;
}
/* Mouse clicks and mouse movement. Rah. */
/* Prepare a mouse-event in *RESULT for placement in the input queue.
If the event is a button press, then note that we have grabbed
the mouse. */
static Lisp_Object
construct_mouse_click (result, event, f)
struct input_event *result;
XButtonEvent *event;
struct frame *f;
{
/* Make the event type NO_EVENT; we'll change that when we decide
otherwise. */
result->kind = MOUSE_CLICK_EVENT;
result->code = event->button - Button1;
result->timestamp = event->time;
result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
event->state)
| (event->type == ButtonRelease
? up_modifier
: down_modifier));
XSETINT (result->x, event->x);
XSETINT (result->y, event->y);
XSETFRAME (result->frame_or_window, f);
result->arg = Qnil;
return Qnil;
}
/* Function to report a mouse movement to the mainstream Emacs code.
The input handler calls this.
We have received a mouse movement event, which is given in *event.
If the mouse is over a different glyph than it was last time, tell
the mainstream emacs code by setting mouse_moved. If not, ask for
another motion event, so we can check again the next time it moves. */
static XMotionEvent last_mouse_motion_event;
static Lisp_Object last_mouse_motion_frame;
static int
note_mouse_movement (frame, event)
FRAME_PTR frame;
XMotionEvent *event;
{
last_mouse_movement_time = event->time;
last_mouse_motion_event = *event;
XSETFRAME (last_mouse_motion_frame, frame);
if (!FRAME_X_OUTPUT (frame))
return 0;
if (event->window != FRAME_X_WINDOW (frame))
{
frame->mouse_moved = 1;
last_mouse_scroll_bar = Qnil;
note_mouse_highlight (frame, -1, -1);
last_mouse_glyph_frame = 0;
return 1;
}
/* Has the mouse moved off the glyph it was on at the last sighting? */
if (frame != last_mouse_glyph_frame
|| event->x < last_mouse_glyph.x
|| event->x >= last_mouse_glyph.x + last_mouse_glyph.width
|| event->y < last_mouse_glyph.y
|| event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
{
frame->mouse_moved = 1;
last_mouse_scroll_bar = Qnil;
note_mouse_highlight (frame, event->x, event->y);
/* Remember which glyph we're now on. */
remember_mouse_glyph (frame, event->x, event->y, &last_mouse_glyph);
last_mouse_glyph_frame = frame;
return 1;
}
return 0;
}
/************************************************************************
Mouse Face
************************************************************************/
static void
redo_mouse_highlight ()
{
if (!NILP (last_mouse_motion_frame)
&& FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
note_mouse_highlight (XFRAME (last_mouse_motion_frame),
last_mouse_motion_event.x,
last_mouse_motion_event.y);
}
/* Return the current position of the mouse.
*FP should be a frame which indicates which display to ask about.
If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
and *PART to the frame, window, and scroll bar part that the mouse
is over. Set *X and *Y to the portion and whole of the mouse's
position on the scroll bar.
If the mouse movement started elsewhere, set *FP to the frame the
mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
the mouse is over.
Set *TIME to the server time-stamp for the time at which the mouse
was at this position.
Don't store anything if we don't have a valid set of values to report.
This clears the mouse_moved flag, so we can wait for the next mouse
movement. */
static void
XTmouse_position (fp, insist, bar_window, part, x, y, time)
FRAME_PTR *fp;
int insist;
Lisp_Object *bar_window;
enum scroll_bar_part *part;
Lisp_Object *x, *y;
unsigned long *time;
{
FRAME_PTR f1;
BLOCK_INPUT;
if (! NILP (last_mouse_scroll_bar) && insist == 0)
x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
else
{
Window root;
int root_x, root_y;
Window dummy_window;
int dummy;
Lisp_Object frame, tail;
/* Clear the mouse-moved flag for every frame on this display. */
FOR_EACH_FRAME (tail, frame)
if (FRAME_X_P (XFRAME (frame))
&& FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
XFRAME (frame)->mouse_moved = 0;
last_mouse_scroll_bar = Qnil;
/* Figure out which root window we're on. */
XQueryPointer (FRAME_X_DISPLAY (*fp),
DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
/* The root window which contains the pointer. */
&root,
/* Trash which we can't trust if the pointer is on
a different screen. */
&dummy_window,
/* The position on that root window. */
&root_x, &root_y,
/* More trash we can't trust. */
&dummy, &dummy,
/* Modifier keys and pointer buttons, about which
we don't care. */
(unsigned int *) &dummy);
/* Now we have a position on the root; find the innermost window
containing the pointer. */
{
Window win, child;
int win_x, win_y;
int parent_x = 0, parent_y = 0;
win = root;
/* XTranslateCoordinates can get errors if the window
structure is changing at the same time this function
is running. So at least we must not crash from them. */
x_catch_errors (FRAME_X_DISPLAY (*fp));
if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
&& FRAME_LIVE_P (last_mouse_frame))
{
/* If mouse was grabbed on a frame, give coords for that frame
even if the mouse is now outside it. */
XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
/* From-window, to-window. */
root, FRAME_X_WINDOW (last_mouse_frame),
/* From-position, to-position. */
root_x, root_y, &win_x, &win_y,
/* Child of win. */
&child);
f1 = last_mouse_frame;
}
else
{
while (1)
{
XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
/* From-window, to-window. */
root, win,
/* From-position, to-position. */
root_x, root_y, &win_x, &win_y,
/* Child of win. */
&child);
if (child == None || child == win)
break;
#ifdef USE_GTK
/* We don't wan't to know the innermost window. We
want the edit window. For non-Gtk+ the innermost
window is the edit window. For Gtk+ it might not
be. It might be the tool bar for example. */
if (x_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win))
break;
#endif
win = child;
parent_x = win_x;
parent_y = win_y;
}
/* Now we know that:
win is the innermost window containing the pointer
(XTC says it has no child containing the pointer),
win_x and win_y are the pointer's position in it
(XTC did this the last time through), and
parent_x and parent_y are the pointer's position in win's parent.
(They are what win_x and win_y were when win was child.
If win is the root window, it has no parent, and
parent_{x,y} are invalid, but that's okay, because we'll
never use them in that case.) */
#ifdef USE_GTK
/* We don't wan't to know the innermost window. We
want the edit window. */
f1 = x_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
#else
/* Is win one of our frames? */
f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
#endif
#ifdef USE_X_TOOLKIT
/* If we end up with the menu bar window, say it's not
on the frame. */
if (f1 != NULL
&& f1->output_data.x->menubar_widget
&& win == XtWindow (f1->output_data.x->menubar_widget))
f1 = NULL;
#endif /* USE_X_TOOLKIT */
}
if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
f1 = 0;
x_uncatch_errors ();
/* If not, is it one of our scroll bars? */
if (! f1)
{
struct scroll_bar *bar;
bar = x_window_to_scroll_bar (FRAME_X_DISPLAY (*fp), win);
if (bar)
{
f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));