xterm.c 168 KB
Newer Older
Jim Blandy's avatar
Jim Blandy committed
1
/* X Communication module for terminals which understand the X protocol.
Karl Heuer's avatar
Karl Heuer committed
2
   Copyright (C) 1989, 1993, 1994, 1995 Free Software Foundation, Inc.
Jim Blandy's avatar
Jim Blandy committed
3 4 5 6 7

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
Jim Blandy's avatar
Jim Blandy committed
8
the Free Software Foundation; either version 2, or (at your option)
Jim Blandy's avatar
Jim Blandy committed
9 10 11 12 13 14 15 16 17 18 19
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; see the file COPYING.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */

20 21
/* Xt features made by Fred Pierresteguy.  */

22 23 24 25 26
/* On 4.3 these lose if they come after xterm.h.  */
/* On HP-UX 8.0 signal.h loses if it comes after config.h.  */
/* Putting these at the beginning seems to be standard for other .c files.  */
#include <signal.h>

27
#include <config.h>
Jim Blandy's avatar
Jim Blandy committed
28

29 30
#include <stdio.h>

31
/* Need syssignal.h for various externs and definitions that may be required
32
   by some configurations for calls to signal later in this source file.  */
33 34
#include "syssignal.h"

Jim Blandy's avatar
Jim Blandy committed
35 36 37
#ifdef HAVE_X_WINDOWS

#include "lisp.h"
38
#include "blockinput.h"
Jim Blandy's avatar
Jim Blandy committed
39 40 41 42

/* This may include sys/types.h, and that somehow loses
   if this is not done before the other system files.  */
#include "xterm.h"
43
#include <X11/cursorfont.h>
Jim Blandy's avatar
Jim Blandy committed
44

45
#ifndef USG
Jim Blandy's avatar
Jim Blandy committed
46 47 48 49
/* Load sys/types.h if not already loaded.
   In some systems loading it twice is suicidal.  */
#ifndef makedev
#include <sys/types.h>
50 51
#endif /* makedev */
#endif /* USG */
Jim Blandy's avatar
Jim Blandy committed
52 53 54

#ifdef BSD
#include <sys/ioctl.h>
55
#endif /* ! defined (BSD) */
Jim Blandy's avatar
Jim Blandy committed
56

57
#include "systty.h"
Jim Blandy's avatar
Jim Blandy committed
58
#include "systime.h"
Jim Blandy's avatar
Jim Blandy committed
59

60
#ifndef INCLUDED_FCNTL
Jim Blandy's avatar
Jim Blandy committed
61
#include <fcntl.h>
62
#endif
Jim Blandy's avatar
Jim Blandy committed
63 64 65 66
#include <ctype.h>
#include <errno.h>
#include <setjmp.h>
#include <sys/stat.h>
Richard M. Stallman's avatar
Richard M. Stallman committed
67 68
/* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed.  */
/* #include <sys/param.h>  */
Jim Blandy's avatar
Jim Blandy committed
69

70
#include "frame.h"
Jim Blandy's avatar
Jim Blandy committed
71 72 73 74 75 76 77
#include "dispextern.h"
#include "termhooks.h"
#include "termopts.h"
#include "termchar.h"
#if 0
#include "sink.h"
#include "sinkmask.h"
78
#endif /* ! 0 */
Jim Blandy's avatar
Jim Blandy committed
79 80 81
#include "gnu.h"
#include "disptab.h"
#include "buffer.h"
82
#include "window.h"
Richard M. Stallman's avatar
Richard M. Stallman committed
83
#include "keyboard.h"
Karl Heuer's avatar
Karl Heuer committed
84
#include "intervals.h"
Jim Blandy's avatar
Jim Blandy committed
85

86
#ifdef USE_X_TOOLKIT
87
extern void free_frame_menubar ();
88
extern void _XEditResCheckMessages ();
89 90
#endif /* USE_X_TOOLKIT */

91 92
#ifndef USE_X_TOOLKIT
#define x_any_window_to_frame x_window_to_frame
93
#define x_top_window_to_frame x_window_to_frame
94 95
#endif

96
#ifdef USE_X_TOOLKIT
Karl Heuer's avatar
Karl Heuer committed
97
#include "widget.h"
98 99 100 101 102
#ifndef XtNinitialState
#define XtNinitialState "initialState"
#endif
#endif

Jim Blandy's avatar
Jim Blandy committed
103 104
#define min(a,b) ((a)<(b) ? (a) : (b))
#define max(a,b) ((a)>(b) ? (a) : (b))
105

106 107
/* This is a chain of structures for all the X displays currently in use.  */
struct x_display_info *x_display_list;
Jim Blandy's avatar
Jim Blandy committed
108

109 110 111 112 113
/* 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;
114

115
/* Frame being updated by update_frame.  This is declared in term.c.
116
   This is set by update_begin and looked at by all the
Jim Blandy's avatar
Jim Blandy committed
117
   XT functions.  It is zero while not inside an update.
Jim Blandy's avatar
Jim Blandy committed
118 119
   In that case, the XT functions assume that `selected_frame'
   is the frame to apply to.  */
120
extern struct frame *updating_frame;
Jim Blandy's avatar
Jim Blandy committed
121

122 123 124
/* This is a frame waiting to be autoraised, within XTread_socket.  */
struct frame *pending_autoraise_frame;

125 126 127 128 129
#ifdef USE_X_TOOLKIT
/* The application context for Xt use.  */
XtAppContext Xt_app_con;
#endif

Jim Blandy's avatar
Jim Blandy committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143
/* During an update, maximum vpos for ins/del line operations to affect.  */

static int flexlines;

/* During an update, nonzero if chars output now should be highlighted.  */

static int highlight;

/* Nominal cursor position -- where to draw output.
   During an update, these are different from the cursor-box position.  */

static int curs_x;
static int curs_y;

144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
/* Mouse movement.

   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.

   The silly O'Reilly & Associates Nutshell guides barely document
   pointer motion hints at all (I think you have to infer how they
   work from an example), and the description of XQueryPointer doesn't
   mention that calling it causes you to get another motion hint from
   the server, which is very important.  */

/* Where the mouse was last time we reported a mouse event.  */
static FRAME_PTR last_mouse_frame;
static XRectangle last_mouse_glyph;

/* 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 timestamp 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;

185 186 187 188 189 190 191
/* 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

192 193
/* Used locally within XTread_socket.  */
static int x_noop_count;
Jim Blandy's avatar
Jim Blandy committed
194

195 196 197
/* Initial values of argv and argc.  */
extern char **initial_argv;
extern int initial_argc;
Jim Blandy's avatar
Jim Blandy committed
198

199
extern Lisp_Object Vcommand_line_args, Vsystem_name;
Jim Blandy's avatar
Jim Blandy committed
200

201 202 203
/* Tells if a window manager is present or not. */

extern Lisp_Object Vx_no_window_manager;
Jim Blandy's avatar
Jim Blandy committed
204 205 206 207

/* Nonzero enables some debugging for the X interface code. */
extern int _Xdebug;

208
extern Lisp_Object Qface, Qmouse_face;
209

Jim Blandy's avatar
Jim Blandy committed
210 211
extern int errno;

212
/* A mask of extra modifier bits to put into every keyboard char.  */
213 214
extern int extra_keyboard_modifiers;

215 216
static Lisp_Object Qvendor_specific_keysyms;

217
extern XrmDatabase x_load_resources ();
Jim Blandy's avatar
Jim Blandy committed
218

219 220
extern Lisp_Object x_icon_type ();

221 222
void x_delete_display ();

223
static void redraw_previous_char ();
224
static void redraw_following_char ();
225
static unsigned int x_x_to_emacs_modifiers ();
Jim Blandy's avatar
Jim Blandy committed
226

227
static int fast_find_position ();
228 229 230
static void note_mouse_highlight ();
static void clear_mouse_face ();
static void show_mouse_face ();
231
static void do_line_dance ();
232

Jim Blandy's avatar
Jim Blandy committed
233 234
static int XTcursor_to ();
static int XTclear_end_of_line ();
235
static int x_io_error_quitter ();
236 237
void x_catch_errors ();
void x_uncatch_errors ();
238

239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
#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 */

267 268 269 270 271 272 273 274 275 276 277
/* 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;
278

279 280
  return 0;
}
Jim Blandy's avatar
Jim Blandy committed
281

282
/* Starting and ending updates.
283 284

   These hooks are called by update_frame at the beginning and end
Jim Blandy's avatar
Jim Blandy committed
285 286 287
   of a frame update.  We record in `updating_frame' the identity
   of the frame being updated, so that the XT... functions do not
   need to take a frame as argument.  Most of the XT... functions
Jim Blandy's avatar
Jim Blandy committed
288
   should never be called except during an update, the only exceptions
289
   being XTcursor_to, XTwrite_glyphs and XTreassert_line_highlight.  */
Jim Blandy's avatar
Jim Blandy committed
290 291

static
Jim Blandy's avatar
Jim Blandy committed
292 293
XTupdate_begin (f)
     struct frame *f;
294
{
Jim Blandy's avatar
Jim Blandy committed
295 296
  int mask;

Jim Blandy's avatar
Jim Blandy committed
297
  if (f == 0)
Jim Blandy's avatar
Jim Blandy committed
298 299
    abort ();

Jim Blandy's avatar
Jim Blandy committed
300
  flexlines = f->height;
Jim Blandy's avatar
Jim Blandy committed
301 302 303
  highlight = 0;

  BLOCK_INPUT;
304

305
  if (f == FRAME_X_DISPLAY_INFO (f)->mouse_face_mouse_frame)
306
    {
307
      /* Don't do highlighting for mouse motion during the update.  */
308
      FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 1;
309 310 311

      /* If the frame needs to be redrawn,
	 simply forget about any prior mouse highlighting.  */
312
      if (FRAME_GARBAGED_P (f))
313 314
	FRAME_X_DISPLAY_INFO (f)->mouse_face_window = Qnil;

315
      if (!NILP (FRAME_X_DISPLAY_INFO (f)->mouse_face_window))
316 317
	{
	  int firstline, lastline, i;
318
	  struct window *w = XWINDOW (FRAME_X_DISPLAY_INFO (f)->mouse_face_window);
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334

	  /* Find the first, and the last+1, lines affected by redisplay.  */
	  for (firstline = 0; firstline < f->height; firstline++)
	    if (FRAME_DESIRED_GLYPHS (f)->enable[firstline])
	      break;

	  lastline = f->height;
	  for (i = f->height - 1; i >= 0; i--)
	    {
	      if (FRAME_DESIRED_GLYPHS (f)->enable[i])
		break;
	      else
		lastline = i;
	    }

	  /* Can we tell that this update does not affect the window
335 336 337 338
	     where the mouse highlight is?  If so, no need to turn off.
	     Likewise, don't do anything if the frame is garbaged;
	     in that case, the FRAME_CURRENT_GLYPHS that we would use
	     are all wrong, and we will redisplay that line anyway.  */
339 340
	  if (! (firstline > (XFASTINT (w->top) + window_internal_height (w))
		 || lastline < XFASTINT (w->top)))
341
	    clear_mouse_face (FRAME_X_DISPLAY_INFO (f));
342
	}
343
    }
Richard M. Stallman's avatar
Richard M. Stallman committed
344

Jim Blandy's avatar
Jim Blandy committed
345 346 347 348
  UNBLOCK_INPUT;
}

static
Jim Blandy's avatar
Jim Blandy committed
349 350
XTupdate_end (f)
     struct frame *f;
351
{
Jim Blandy's avatar
Jim Blandy committed
352 353 354 355
  int mask;

  BLOCK_INPUT;

356
  do_line_dance ();
Jim Blandy's avatar
Jim Blandy committed
357
  x_display_cursor (f, 1);
Jim Blandy's avatar
Jim Blandy committed
358

359 360
  if (f == FRAME_X_DISPLAY_INFO (f)->mouse_face_mouse_frame)
    FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
361 362 363 364 365 366 367
#if 0
  /* This fails in the case of having updated only the echo area
     if we have switched buffers.  In that case, FRAME_CURRENT_GLYPHS
     has no relation to the current contents, and its charstarts
     have no relation to the contents of the window-buffer.
  I don't know a clean way to check
     for that case.  window_end_valid isn't set up yet.  */
368 369 370
  if (f == FRAME_X_DISPLAY_INFO (f)->mouse_face_mouse_frame)
    note_mouse_highlight (f, FRAME_X_DISPLAY_INFO (f)->mouse_face_mouse_x,
			  FRAME_X_DISPLAY_INFO (f)->mouse_face_mouse_y);
371 372
#endif

373
  XFlush (FRAME_X_DISPLAY (f));
Jim Blandy's avatar
Jim Blandy committed
374 375
  UNBLOCK_INPUT;
}
376

377
/* This is called after a redisplay on frame F.  */
378 379 380 381 382

static
XTframe_up_to_date (f)
     FRAME_PTR f;
{
383 384
  if (FRAME_X_DISPLAY_INFO (f)->mouse_face_deferred_gc
      || f == FRAME_X_DISPLAY_INFO (f)->mouse_face_mouse_frame)
385
    {
386 387 388 389
      note_mouse_highlight (FRAME_X_DISPLAY_INFO (f)->mouse_face_mouse_frame,
			    FRAME_X_DISPLAY_INFO (f)->mouse_face_mouse_x,
			    FRAME_X_DISPLAY_INFO (f)->mouse_face_mouse_y);
      FRAME_X_DISPLAY_INFO (f)->mouse_face_deferred_gc = 0;
390
    }
391
}
Jim Blandy's avatar
Jim Blandy committed
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411

/* External interface to control of standout mode.
   Call this when about to modify line at position VPOS
   and not change whether it is highlighted.  */

XTreassert_line_highlight (new, vpos)
     int new, vpos;
{
  highlight = new;
}

/* Call this when about to modify line at position VPOS
   and change whether it is highlighted.  */

static
XTchange_line_highlight (new_highlight, vpos, first_unused_hpos)
     int new_highlight, vpos, first_unused_hpos;
{
  highlight = new_highlight;
  XTcursor_to (vpos, 0);
Jim Blandy's avatar
Jim Blandy committed
412
  XTclear_end_of_line (updating_frame->width);
Jim Blandy's avatar
Jim Blandy committed
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
}

/* This is used 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
XTset_terminal_modes ()
{
}

/* This is called when exiting or suspending Emacs.
   Exiting will make the X-windows go away, and suspending
   requires no action.  */

static
XTreset_terminal_modes ()
{
Jim Blandy's avatar
Jim Blandy committed
431
/*  XTclear_frame ();  */
Jim Blandy's avatar
Jim Blandy committed
432 433
}

434 435
/* Set the nominal cursor position of the frame.
   This is where display update commands will take effect.
Jim Blandy's avatar
Jim Blandy committed
436 437
   This does not affect the place where the cursor-box is displayed.  */

Jim Blandy's avatar
Jim Blandy committed
438
static int
Jim Blandy's avatar
Jim Blandy committed
439 440 441 442 443 444 445 446 447
XTcursor_to (row, col)
     register int row, col;
{
  int mask;
  int orow = row;

  curs_x = col;
  curs_y = row;

Jim Blandy's avatar
Jim Blandy committed
448
  if (updating_frame == 0)
Jim Blandy's avatar
Jim Blandy committed
449 450
    {
      BLOCK_INPUT;
Jim Blandy's avatar
Jim Blandy committed
451
      x_display_cursor (selected_frame, 1);
452
      XFlush (FRAME_X_DISPLAY (selected_frame));
Jim Blandy's avatar
Jim Blandy committed
453 454 455 456 457 458
      UNBLOCK_INPUT;
    }
}

/* Display a sequence of N glyphs found at GP.
   WINDOW is the x-window to output to.  LEFT and TOP are starting coords.
459 460
   HL is 1 if this text is highlighted, 2 if the cursor is on it,
   3 if should appear in its mouse-face.
461 462
   JUST_FOREGROUND if 1 means draw only the foreground;
   don't alter the background.
Jim Blandy's avatar
Jim Blandy committed
463

464
   FONT is the default font to use (for glyphs whose font-code is 0).
Jim Blandy's avatar
Jim Blandy committed
465

466 467 468
   Since the display generation code is responsible for calling
   compute_char_face and compute_glyph_face on everything it puts in
   the display structure, we can assume that the face code on each
469
   glyph is a valid index into FRAME_COMPUTED_FACES (f), and the one
470 471
   to which we can actually apply intern_face.
   Call this function with input blocked.  */
Jim Blandy's avatar
Jim Blandy committed
472

473 474
#if 1
/* This is the multi-face code.  */
Jim Blandy's avatar
Jim Blandy committed
475 476

static void
477
dumpglyphs (f, left, top, gp, n, hl, just_foreground)
Jim Blandy's avatar
Jim Blandy committed
478
     struct frame *f;
Jim Blandy's avatar
Jim Blandy committed
479 480 481 482
     int left, top;
     register GLYPH *gp; /* Points to first GLYPH. */
     register int n;  /* Number of glyphs to display. */
     int hl;
483
     int just_foreground;
Jim Blandy's avatar
Jim Blandy committed
484
{
485 486
  /* Holds characters to be displayed. */
  char *buf = (char *) alloca (f->width * sizeof (*buf));
Jim Blandy's avatar
Jim Blandy committed
487 488 489
  register char *cp;		/* Steps through buf[]. */
  register int tlen = GLYPH_TABLE_LENGTH;
  register Lisp_Object *tbase = GLYPH_TABLE_BASE;
490
  Window window = FRAME_X_WINDOW (f);
491
  int orig_left = left;
Jim Blandy's avatar
Jim Blandy committed
492

493
  while (n > 0)
Jim Blandy's avatar
Jim Blandy committed
494 495 496 497 498
    {
      /* Get the face-code of the next GLYPH.  */
      int cf, len;
      int g = *gp;

499
      GLYPH_FOLLOW_ALIASES (tbase, tlen, g);
500
      cf = FAST_GLYPH_FACE (g);
Jim Blandy's avatar
Jim Blandy committed
501 502 503 504 505 506 507

      /* Find the run of consecutive glyphs with the same face-code.
	 Extract their character codes into BUF.  */
      cp = buf;
      while (n > 0)
	{
	  g = *gp;
508
	  GLYPH_FOLLOW_ALIASES (tbase, tlen, g);
509
	  if (FAST_GLYPH_FACE (g) != cf)
Jim Blandy's avatar
Jim Blandy committed
510 511
	    break;

512
	  *cp++ = FAST_GLYPH_CHAR (g);
Jim Blandy's avatar
Jim Blandy committed
513 514 515 516 517 518 519 520 521
	  --n;
	  ++gp;
	}

      /* LEN gets the length of the run.  */
      len = cp - buf;

      /* Now output this run of chars, with the font and pixel values
	 determined by the face code CF.  */
522 523
      {
	struct face *face = FRAME_DEFAULT_FACE (f);
524
	XFontStruct *font = FACE_FONT (face);
525
	GC gc = FACE_GC (face);
526
	int stippled = 0;
527

528 529
	/* HL = 3 means use a mouse face previously chosen.  */
	if (hl == 3)
530
	  cf = FRAME_X_DISPLAY_INFO (f)->mouse_face_face_id;
531

532 533
	/* First look at the face of the text itself.  */
	if (cf != 0)
534
	  {
535 536
	    /* It's possible for the display table to specify
	       a face code that is out of range.  Use 0 in that case.  */
537 538
	    if (cf < 0 || cf >= FRAME_N_COMPUTED_FACES (f)
		|| FRAME_COMPUTED_FACES (f) [cf] == 0)
539
	      cf = 0;
540 541 542 543

	    if (cf == 1)
	      face = FRAME_MODE_LINE_FACE (f);
	    else
544
	      face = intern_face (f, FRAME_COMPUTED_FACES (f) [cf]);
545 546
	    font = FACE_FONT (face);
	    gc = FACE_GC (face);
547 548
	    if (FACE_STIPPLE (face))
	      stippled = 1;
549
	  }
550 551

	/* Then comes the distinction between modeline and normal text.  */
552 553 554 555 556 557 558
	else if (hl == 0)
	  ;
	else if (hl == 1)
	  {
	    face = FRAME_MODE_LINE_FACE (f);
	    font = FACE_FONT (face);
	    gc   = FACE_GC   (face);
559 560
	    if (FACE_STIPPLE (face))
	      stippled = 1;
561 562 563 564 565
	  }

#define FACE_DEFAULT (~0)

	/* Now override that if the cursor's on this character.  */
566
	if (hl == 2)
567
	  {
568 569 570
	    /* The cursor overrides stippling.  */
	    stippled = 0;

571
	    if ((!face->font
572
		 || face->font == (XFontStruct *) FACE_DEFAULT
573 574 575
		 || face->font == f->display.x->font)
		&& face->background == f->display.x->background_pixel
		&& face->foreground == f->display.x->foreground_pixel)
576 577 578 579 580 581 582 583 584 585
	      {
		gc = f->display.x->cursor_gc;
	      }
	    /* Cursor on non-default face: must merge.  */
	    else
	      {
		XGCValues xgcv;
		unsigned long mask;

		xgcv.background = f->display.x->cursor_pixel;
586
		xgcv.foreground = face->background;
587 588 589
		/* If the glyph would be invisible,
		   try a different foreground.  */
		if (xgcv.foreground == xgcv.background)
590
		  xgcv.foreground = face->foreground;
591 592 593 594 595 596 597 598 599 600 601
		if (xgcv.foreground == xgcv.background)
		  xgcv.foreground = f->display.x->cursor_foreground_pixel;
		if (xgcv.foreground == xgcv.background)
		  xgcv.foreground = face->foreground;
		/* Make sure the cursor is distinct from text in this face.  */
		if (xgcv.background == face->background
		    && xgcv.foreground == face->foreground)
		  {
		    xgcv.background = face->foreground;
		    xgcv.foreground = face->background;
		  }
602 603 604
		xgcv.font = face->font->fid;
		xgcv.graphics_exposures = 0;
		mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
605 606 607 608
		if (FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc)
		  XChangeGC (FRAME_X_DISPLAY (f),
			     FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc,
			     mask, &xgcv);
609
		else
610
		  FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc
611
		    = XCreateGC (FRAME_X_DISPLAY (f), window, mask, &xgcv);
612
		gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
613
#if 0
614 615
/* If this code is restored, it must also reset to the default stipple
   if necessary. */
616
		if (face->stipple && face->stipple != FACE_DEFAULT)
617
		  XSetStipple (FRAME_X_DISPLAY (f), gc, face->stipple);
618
#endif
619
	      }
620 621
	  }

622
	if (font == (XFontStruct *) FACE_DEFAULT)
623 624
	  font = f->display.x->font;

625
	if (just_foreground)
626
	  XDrawString (FRAME_X_DISPLAY (f), window, gc,
627 628 629
		       left, top + FONT_BASE (font), buf, len);
	else
	  {
630 631 632 633 634 635
	    if (stippled)
	      {
		/* Turn stipple on.  */
		XSetFillStyle (FRAME_X_DISPLAY (f), gc, FillOpaqueStippled);

		/* Draw stipple on background.  */
636
		XFillRectangle (FRAME_X_DISPLAY (f), window, gc,
637 638 639 640 641 642 643 644
				left, top,
				FONT_WIDTH (font) * len,
				FONT_HEIGHT (font));

		/* Turn stipple off.  */
		XSetFillStyle (FRAME_X_DISPLAY (f), gc, FillSolid);

		/* Draw the text, solidly, onto the stipple pattern.  */
645
		XDrawString (FRAME_X_DISPLAY (f), window, gc,
646 647 648
			     left, top + FONT_BASE (font), buf, len);
	      }
	    else
649
	      XDrawImageString (FRAME_X_DISPLAY (f), window, gc,
650 651
				left, top + FONT_BASE (font), buf, len);

652 653
	    /* Clear the rest of the line's height.  */
	    if (f->display.x->line_height != FONT_HEIGHT (font))
654
	      XClearArea (FRAME_X_DISPLAY (f), window, left,
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
			  top + FONT_HEIGHT (font),
			  FONT_WIDTH (font) * len,
			  /* This is how many pixels of height
			     we have to clear.  */
			  f->display.x->line_height - FONT_HEIGHT (font),
			  False);
	  }

#if 0 /* Doesn't work, because it uses FRAME_CURRENT_GLYPHS,
	 which often is not up to date yet.  */
	if (!just_foreground)
	  {
	    if (left == orig_left)
	      redraw_previous_char (f, PIXEL_TO_CHAR_COL (f, left),
				    PIXEL_TO_CHAR_ROW (f, top), hl == 1);
	    if (n == 0)
	      redraw_following_char (f, PIXEL_TO_CHAR_COL (f, left + len * FONT_WIDTH (font)),
				     PIXEL_TO_CHAR_ROW (f, top), hl == 1);
	  }
#endif
675 676 677 678

	/* We should probably check for XA_UNDERLINE_POSITION and
	   XA_UNDERLINE_THICKNESS properties on the font, but let's
	   just get the thing working, and come back to that.  */
Jim Blandy's avatar
Jim Blandy committed
679
	{
680
	  int underline_position = 1;
681

682 683
	  if (font->descent <= underline_position)
	    underline_position = font->descent - 1;
684 685

	  if (face->underline)
686
	    XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
687 688 689 690 691
			    FACE_GC (face),
			    left, (top
				   + FONT_BASE (font)
				   + underline_position),
			    len * FONT_WIDTH (font), 1);
Jim Blandy's avatar
Jim Blandy committed
692
	}
693 694 695

	left += len * FONT_WIDTH (font);
      }
Jim Blandy's avatar
Jim Blandy committed
696 697
    }
}
698 699 700 701 702 703 704 705 706 707 708 709
#endif /* 1 */

#if 0
/* This is the old single-face code.  */

static void
dumpglyphs (f, left, top, gp, n, hl, font)
     struct frame *f;
     int left, top;
     register GLYPH *gp; /* Points to first GLYPH. */
     register int n;  /* Number of glyphs to display. */
     int hl;
710
     XFontStruct *font;
711 712 713 714 715 716 717 718
{
  register int len;
  Window window = FRAME_X_WINDOW (f);
  GC drawing_gc =   (hl == 2 ? f->display.x->cursor_gc
		             : (hl ? f->display.x->reverse_gc
				   : f->display.x->normal_gc));

  if (sizeof (GLYPH) == sizeof (XChar2b))
719
    XDrawImageString16 (FRAME_X_DISPLAY (f), window, drawing_gc,
720 721
			left, top + FONT_BASE (font), (XChar2b *) gp, n);
  else if (sizeof (GLYPH) == sizeof (unsigned char))
722
    XDrawImageString (FRAME_X_DISPLAY (f), window, drawing_gc,
723 724 725 726 727 728 729
		      left, top + FONT_BASE (font), (char *) gp, n);
  else
    /* What size of glyph ARE you using?  And does X have a function to
       draw them?  */
    abort ();
}
#endif
Jim Blandy's avatar
Jim Blandy committed
730

731 732
/* Output some text at the nominal frame cursor position.
   Advance the cursor over the text.
Jim Blandy's avatar
Jim Blandy committed
733 734 735 736 737 738 739 740 741 742 743 744
   Output LEN glyphs at START.

   `highlight', set up by XTreassert_line_highlight or XTchange_line_highlight,
   controls the pixel values used for foreground and background.  */

static
XTwrite_glyphs (start, len)
     register GLYPH *start;
     int len;
{
  register int temp_length;
  int mask;
Jim Blandy's avatar
Jim Blandy committed
745
  struct frame *f;
Jim Blandy's avatar
Jim Blandy committed
746 747 748

  BLOCK_INPUT;

749
  do_line_dance ();
Jim Blandy's avatar
Jim Blandy committed
750 751
  f = updating_frame;
  if (f == 0)
Jim Blandy's avatar
Jim Blandy committed
752
    {
Jim Blandy's avatar
Jim Blandy committed
753
      f = selected_frame;
Jim Blandy's avatar
Jim Blandy committed
754
      /* If not within an update,
Jim Blandy's avatar
Jim Blandy committed
755 756 757
	 output at the frame's visible cursor.  */
      curs_x = f->cursor_x;
      curs_y = f->cursor_y;
Jim Blandy's avatar
Jim Blandy committed
758 759
    }

Jim Blandy's avatar
Jim Blandy committed
760
  dumpglyphs (f,
761 762
	      CHAR_TO_PIXEL_COL (f, curs_x),
	      CHAR_TO_PIXEL_ROW (f, curs_y),
763
	      start, len, highlight, 0);
Jim Blandy's avatar
Jim Blandy committed
764 765

  /* If we drew on top of the cursor, note that it is turned off.  */
Jim Blandy's avatar
Jim Blandy committed
766 767 768 769
  if (curs_y == f->phys_cursor_y
      && curs_x <= f->phys_cursor_x
      && curs_x + len > f->phys_cursor_x)
    f->phys_cursor_x = -1;
770

Jim Blandy's avatar
Jim Blandy committed
771
  if (updating_frame == 0)
Jim Blandy's avatar
Jim Blandy committed
772
    {
Jim Blandy's avatar
Jim Blandy committed
773 774 775
      f->cursor_x += len;
      x_display_cursor (f, 1);
      f->cursor_x -= len;
Jim Blandy's avatar
Jim Blandy committed
776 777 778 779 780 781 782
    }
  else
    curs_x += len;

  UNBLOCK_INPUT;
}

783 784
/* Clear to the end of the line.
   Erase the current text line from the nominal cursor position (inclusive)
Jim Blandy's avatar
Jim Blandy committed
785 786
   to column FIRST_UNUSED (exclusive).  The idea is that everything
   from FIRST_UNUSED onward is already erased.  */
787

788
static
Jim Blandy's avatar
Jim Blandy committed
789 790 791
XTclear_end_of_line (first_unused)
     register int first_unused;
{
Jim Blandy's avatar
Jim Blandy committed
792
  struct frame *f = updating_frame;
Jim Blandy's avatar
Jim Blandy committed
793 794
  int mask;

Jim Blandy's avatar
Jim Blandy committed
795
  if (f == 0)
Jim Blandy's avatar
Jim Blandy committed
796 797
    abort ();

Jim Blandy's avatar
Jim Blandy committed
798
  if (curs_y < 0 || curs_y >= f->height)
Jim Blandy's avatar
Jim Blandy committed
799 800 801 802
    return;
  if (first_unused <= 0)
    return;

Jim Blandy's avatar
Jim Blandy committed
803 804
  if (first_unused >= f->width)
    first_unused = f->width;
Jim Blandy's avatar
Jim Blandy committed
805 806 807

  BLOCK_INPUT;

808 809
  do_line_dance ();

Jim Blandy's avatar
Jim Blandy committed
810
  /* Notice if the cursor will be cleared by this operation.  */
Jim Blandy's avatar
Jim Blandy committed
811 812 813 814
  if (curs_y == f->phys_cursor_y
      && curs_x <= f->phys_cursor_x
      && f->phys_cursor_x < first_unused)
    f->phys_cursor_x = -1;
Jim Blandy's avatar
Jim Blandy committed
815

816
  XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
817 818
	      CHAR_TO_PIXEL_COL (f, curs_x),
	      CHAR_TO_PIXEL_ROW (f, curs_y),
Jim Blandy's avatar
Jim Blandy committed
819
	      FONT_WIDTH (f->display.x->font) * (first_unused - curs_x),
820
	      f->display.x->line_height, False);
821
#if 0
822
  redraw_previous_char (f, curs_x, curs_y, highlight);
823
#endif
Jim Blandy's avatar
Jim Blandy committed
824 825 826 827

  UNBLOCK_INPUT;
}

828 829 830 831 832 833 834 835 836 837 838 839
static
XTclear_frame ()
{
  int mask;
  struct frame *f = updating_frame;

  if (f == 0)
    f = selected_frame;

  f->phys_cursor_x = -1;	/* Cursor not visible.  */
  curs_x = 0;			/* Nominal cursor position is top left.  */
  curs_y = 0;
840

841 842
  BLOCK_INPUT;

843
  XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
844 845 846 847 848

  /* 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);

849
  XFlush (FRAME_X_DISPLAY (f));
850 851 852 853 854 855 856 857 858 859 860 861
  UNBLOCK_INPUT;
}

#if 0
/* This currently does not work because FRAME_CURRENT_GLYPHS doesn't
   always contain the right glyphs to use.

   It also needs to be changed to look at the details of the font and
   see whether there is really overlap, and do nothing when there is
   not.  This can use font_char_overlap_left and font_char_overlap_right,
   but just how to use them is not clear.  */

862 863 864
/* Erase the character (if any) at the position just before X, Y in frame F,
   then redraw it and the character before it.
   This is necessary when we erase starting at X,
865 866
   in case the character after X overlaps into the one before X.
   Call this function with input blocked.  */
867 868

static void
869
redraw_previous_char (f, x, y, highlight_flag)
870 871
     FRAME_PTR f;
     int x, y;
872
     int highlight_flag;
873 874 875 876 877 878 879 880 881 882
{
  /* Erase the character before the new ones, in case
     what was here before overlaps it.
     Reoutput that character, and the previous character
     (in case the previous character overlaps it).  */
  if (x > 0)
    {
      int start_x = x - 2;
      if (start_x < 0)
	start_x = 0;
883
      XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
884 885 886
		  CHAR_TO_PIXEL_COL (f, x - 1),
		  CHAR_TO_PIXEL_ROW (f, y),
		  FONT_WIDTH (f->display.x->font),
887
		  f->display.x->line_height, False);
888 889 890 891

      dumpglyphs (f, CHAR_TO_PIXEL_COL (f, start_x),
		  CHAR_TO_PIXEL_ROW (f, y),
		  &FRAME_CURRENT_GLYPHS (f)->glyphs[y][start_x],
892
		  x - start_x, highlight_flag, 1);
893 894 895
    }
}

896 897 898 899 900 901 902 903 904 905 906
/* Erase the character (if any) at the position X, Y in frame F,
   then redraw it and the character after it.
   This is necessary when we erase endng at X,
   in case the character after X overlaps into the one before X.
   Call this function with input blocked.  */

static void
redraw_following_char (f, x, y, highlight_flag)
     FRAME_PTR f;
     int x, y;
     int highlight_flag;
Jim Blandy's avatar
Jim Blandy committed
907
{
908 909 910 911 912 913 914 915 916 917 918
  int limit = FRAME_CURRENT_GLYPHS (f)->used[y];
  /* Erase the character after the new ones, in case
     what was here before overlaps it.
     Reoutput that character, and the following character
     (in case the following character overlaps it).  */
  if (x < limit
      && FRAME_CURRENT_GLYPHS (f)->glyphs[y][x] != SPACEGLYPH)
    {
      int end_x = x + 2;
      if (end_x > limit)
	end_x = limit;
919
      XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
920 921 922 923
		  CHAR_TO_PIXEL_COL (f, x),
		  CHAR_TO_PIXEL_ROW (f, y),
		  FONT_WIDTH (f->display.x->font),
		  f->display.x->line_height, False);
Jim Blandy's avatar
Jim Blandy committed
924

925 926 927 928 929 930 931 932 933
      dumpglyphs (f, CHAR_TO_PIXEL_COL (f, x),
		  CHAR_TO_PIXEL_ROW (f, y),
		  &FRAME_CURRENT_GLYPHS (f)->glyphs[y][x],
		  end_x - x, highlight_flag, 1);
    }
}
#endif /* 0 */

#if 0 /* Not in use yet */
Jim Blandy's avatar
Jim Blandy committed
934

935
/* Return 1 if character C in font F extends past its left edge.  */
Jim Blandy's avatar
Jim Blandy committed
936

937 938 939 940 941 942
static int
font_char_overlap_left (font, c)
     XFontStruct *font;
     int c;
{
  XCharStruct *s;
Jim Blandy's avatar
Jim Blandy committed
943

944 945 946 947 948 949 950
  /* Find the bounding-box info for C.  */
  if (font->per_char == 0)
    s = &font->max_bounds;
  else
    {
      int rowlen = font->max_char_or_byte2 - font->min_char_or_byte2 + 1;
      int row, within;
951

952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975
      /* Decode char into row number (byte 1) and code within row (byte 2).  */
      row = c >> 8;
      within = c & 0177;
      if (!(within >= font->min_char_or_byte2
	    && within <= font->max_char_or_byte2
	    && row >= font->min_byte1
	    && row <= font->max_byte1))
	{
	  /* If char is out of range, try the font's default char instead.  */
	  c = font->default_char;
	  row = c >> (INTBITS - 8);
	  within = c & 0177;
	}
      if (!(within >= font->min_char_or_byte2
	    && within <= font->max_char_or_byte2
	    && row >= font->min_byte1
	    && row <= font->max_byte1))
	/* Still out of range means this char does not overlap.  */
	return 0;
      else
	/* We found the info for this char.  */
	s = (font->per_char + (within - font->min_char_or_byte2)
	     + row * rowlen);
    }
Jim Blandy's avatar
Jim Blandy committed
976

977 978
  return (s && s->lbearing < 0);
}
Jim Blandy's avatar
Jim Blandy committed
979

980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995
/* Return 1 if character C in font F extends past its right edge.  */

static int
font_char_overlap_right (font, c)
     XFontStruct *font;
     int c;
{
  XCharStruct *s;

  /* Find the bounding-box info for C.  */
  if (font->per_char == 0)
    s = &font->max_bounds;
  else
    {
      int rowlen = font->max_char_or_byte2 - font->min_char_or_byte2 + 1;
      int row, within;
996

997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
      /* Decode char into row number (byte 1) and code within row (byte 2).  */
      row = c >> 8;
      within = c & 0177;
      if (!(within >= font->min_char_or_byte2
	    && within <= font->max_char_or_byte2
	    && row >= font->min_byte1
	    && row <= font->max_byte1))
	{
	  /* If char is out of range, try the font's default char instead.  */
	  c = font->default_char;
	  row = c >> (INTBITS - 8);
	  within = c & 0177;
	}
      if (!(within >= font->min_char_or_byte2
	    && within <= font->max_char_or_byte2
	    && row >= font->min_byte1
	    && row <= font->max_byte1))
	/* Still out of range means this char does not overlap.  */
	return 0;
      else
	/* We found the info for this char.  */
	s = (font->per_char + (within - font->min_char_or_byte2)
	     + row * rowlen);
    }

  return (s && s->rbearing >= s->width);
Jim Blandy's avatar
Jim Blandy committed
1023
}
1024
#endif /* 0 */
Jim Blandy's avatar
Jim Blandy committed
1025

Jim Blandy's avatar
Jim Blandy committed
1026 1027 1028
/* 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
1029
   it's available.  If it isn't, we just won't do visual bells.  */
Jim Blandy's avatar
Jim Blandy committed
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
#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;
}
Jim Blandy's avatar
Jim Blandy committed
1063

Jim Blandy's avatar
Jim Blandy committed
1064 1065
XTflash (f)
     struct frame *f;
Jim Blandy's avatar
Jim Blandy committed
1066
{
Jim Blandy's avatar
Jim Blandy committed
1067
  BLOCK_INPUT;
Jim Blandy's avatar
Jim Blandy committed
1068

Jim Blandy's avatar
Jim Blandy committed
1069 1070
  {
    GC gc;
Jim Blandy's avatar
Jim Blandy committed
1071

Jim Blandy's avatar
Jim Blandy committed
1072 1073 1074 1075
    /* Create a GC that will use the GXxor function to flip foreground pixels
       into background pixels.  */
    {
      XGCValues values;
Jim Blandy's avatar
Jim Blandy committed
1076

Jim Blandy's avatar
Jim Blandy committed
1077 1078 1079
      values.function = GXxor;
      values.foreground = (f->display.x->foreground_pixel
			   ^ f->display.x->background_pixel);
1080

1081
      gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
Jim Blandy's avatar
Jim Blandy committed
1082 1083
		      GCFunction | GCForeground, &values);
    }
Jim Blandy's avatar
Jim Blandy committed
1084

Jim Blandy's avatar
Jim Blandy committed
1085 1086 1087
    {
      int width  = PIXEL_WIDTH  (f);
      int height = PIXEL_HEIGHT (f);
Jim Blandy's avatar
Jim Blandy committed
1088

1089
      XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
Jim Blandy's avatar
Jim Blandy committed
1090
		      width/4, height/4, width/2, height/2);
1091
      XFlush (FRAME_X_DISPLAY (f));
Jim Blandy's avatar
Jim Blandy committed
1092

Jim Blandy's avatar
Jim Blandy committed
1093 1094
      {
	struct timeval wakeup, now;
Jim Blandy's avatar
Jim Blandy committed
1095

1096
	EMACS_GET_TIME (wakeup);
Jim Blandy's avatar
Jim Blandy committed
1097

Jim Blandy's avatar
Jim Blandy committed
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
	/* 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.  */
	while (1)
	  {
	    struct timeval timeout;

1108
	    EMACS_GET_TIME (timeout);
Jim Blandy's avatar
Jim Blandy committed
1109 1110 1111 1112 1113 1114 1115

	    /* In effect, timeout = wakeup - timeout.
	       Break if result would be negative.  */
	    if (timeval_subtract (&timeout, wakeup, timeout))
	      break;

	    /* Try to wait that long--but we might wake up sooner.  */
1116
	    select (0, NULL, NULL, NULL, &timeout);
Jim Blandy's avatar
Jim Blandy committed
1117 1118
	  }
      }
1119

1120
      XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
Jim Blandy's avatar
Jim Blandy committed
1121
		      width/4, height/4, width/2, height/2);
1122 1123
      XFreeGC (FRAME_X_DISPLAY (f), gc);
      XFlush (FRAME_X_DISPLAY (f));