view src/cm.c @ 4477:e34711681f30

Don't determine whether to call general device-type code at startup, rather decide in the device-specific code itself. lisp/ChangeLog addition: 2008-07-07 Aidan Kehoe <kehoea@parhasard.net> Patch to make it up to the device-specific code whether various Lisp functions should be called during device creation, not relying on the startup code to decide this. Also, rename initial-window-system to initial-device-type (which makes more sense in this scheme), always set it. * startup.el (command-line): Use initial-device-type, not initial-window-system; just call #'make-device, leave the special behaviour to be done the first time a console type is initialised to be decided on by the respective console code. * x-init.el (x-app-defaults-directory): Declare that it should be bound. (x-define-dead-key): Have the macro take a DEVICE argument. (x-initialize-compose): Have the function take a DEVICE argument, and use it when checking if various keysyms are available on the keyboard. (x-initialize-keyboard): Have the function take a DEVICE argument, allowing device-specific keyboard initialisation. (make-device-early-x-entry-point-called-p): New. (make-device-late-x-entry-point-called-p): New. Rename pre-x-win-initted, x-win-initted. (make-device-early-x-entry-point): Rename init-pre-x-win, take the call to make-x-device out (it should be called from the device-creation code, not vice-versa). (make-device-late-x-entry-point): Rename init-post-x-win, have it take a DEVICE argument, use that DEVICE argument when working out what device-specific things need doing. Don't use create-console-hook in core code. * x-win-xfree86.el (x-win-init-xfree86): Take a DEVICE argument; use it. * x-win-sun.el (x-win-init-sun): Take a DEVICE argument; use it. * mule/mule-x-init.el: Remove #'init-mule-x-win, an empty function. * tty-init.el (make-device-early-tty-entry-point-called-p): New. Rename pre-tty-win-initted. (make-device-early-tty-entry-point): New. Rename init-pre-tty-win. (make-frame-after-init-entry-point): New. Rename init-post-tty-win to better reflect when it's called. * gtk-init.el (gtk-early-lisp-options-file): New. Move this path to a documented variable. (gtk-command-switch-alist): Wrap the docstring to fewer than 79 columns. (make-device-early-gtk-entry-point-called-p): New. (make-device-late-gtk-entry-point-called-p): New. Renamed gtk-pre-win-initted, gtk-post-win-initted to these. (make-device-early-gtk-entry-point): New. (make-device-late-gtk-entry-point): New. Renamed init-pre-gtk-win, init-post-gtk-win to these. Have make-device-late-gtk-entry-point take a device argument, and use it; have make-device-early-gtk-entry-point load the GTK-specific startup code, instead of doing that in C. (init-gtk-win): Deleted, functionality moved to the GTK device creation code. (gtk-define-dead-key): Have it take a DEVICE argument; use this argument. (gtk-initialize-compose): Ditto. * coding.el (set-terminal-coding-system): Correct the docstring; the function isn't broken. src/ChangeLog addition: 2008-07-07 Aidan Kehoe <kehoea@parhasard.net> Patch to make it up to the device-specific code whether various Lisp functions should be called during device creation, not relying on the startup code to decide this. Also, rename initial-window-system to initial-device-type (which makes more sense in this scheme), always set it. * redisplay.c (Vinitial_device_type): New. (Vinitial_window_system): Removed. Rename initial-window-system to initial-device type, making it a stream if we're noninteractive. Update its docstring. * device-x.c (Qmake_device_early_x_entry_point, Qmake_device_late_x_entry_point): New. Rename Qinit_pre_x_win, Qinit_post_x_win. (x_init_device): Call #'make-device-early-x-entry-point earlier, now we rely on it to find the application class and the app-defaults directory. (x_finish_init_device): Call #'make-device-late-x-entry-point with the created device. (Vx_app_defaults_directory): Always make this available, to simplify code in x-init.el. * device-tty.c (Qmake_device_early_tty_entry_point): New. Rename Qinit_pre_tty_win, rename Qinit_post_tty_win and move to frame-tty.c as Qmake_frame_after_init_entry_point. (tty_init_device): Call #'make-device-early-tty-entry-point before doing anything. * frame-tty.c (Qmake_frame_after_init_entry_point): New. * frame-tty.c (tty_after_init_frame): Have it call the better-named #'make-frame-after-init-entry-point function instead of #'init-post-tty-win (since it's called after frame, not device, creation). * device-msw.c (Qmake_device_early_mswindows_entry_point, Qmake_device_late_mswindows_entry_point): New. Rename Qinit_pre_mswindows_win, Qinit_post_mswindows_win. (mswindows_init_device): Call #'make-device-early-mswindows-entry-point here, instead of having its predecessor call us. (mswindows_finish_init_device): Call #'make-device-early-mswindows-entry-point, for symmetry with the other device types (though it's an empty function). * device-gtk.c (Qmake_device_early_gtk_entry_point, Qmake_device_late_gtk_entry_point): New. Rename Qinit_pre_gtk_win, Qinit_post_gtk_win. (gtk_init_device): Call #'make-device-early-gtk-entry-point; don't load ~/.xemacs/gtk-options.el ourselves, leave that to lisp. (gtk_finish_init_device): Call #'make-device-late-gtk-entry-point with the created device as an argument.
author Aidan Kehoe <kehoea@parhasard.net>
date Wed, 09 Jul 2008 20:46:22 +0200
parents 3d8143fc88e1
children 6f2158fa75ed
line wrap: on
line source

/* Cursor motion subroutines for XEmacs.
   Copyright (C) 1985, 1994, 1995 Free Software Foundation, Inc.
    loosely based primarily on public domain code written by Chris Torek

This file is part of XEmacs.

XEmacs 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 2, or (at your option) any
later version.

XEmacs 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 XEmacs; see the file COPYING.  If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  */

/* Synched up with: FSF 19.30.  Substantially different from FSF. */

/* #### This file is extremely junky and needs major fixup. */

#include <config.h>
#include "lisp.h"

#include "device.h"
#include "frame.h"
#include "lstream.h"
#include "redisplay.h"

#include "console-tty-impl.h"

#define	EXPENSIVE 2000

EXTERN_C char *tgoto (const char *cm, int hpos, int vpos);
EXTERN_C int tputs (const char *, int, void (*)(int));

static void cmgoto_for_real (struct console *c, int row, int col);

static int cm_cost_counter;		/* sums up costs */

static void
evalcost (int UNUSED (c))
{
  cm_cost_counter++;
}

/* Ugh -- cmputc() can't take a console argument, so we pass it in a global */
struct console *cmputc_console;

void
send_string_to_tty_console (struct console *c, unsigned char *str, int len)
{
  /* #### Ben sez: don't some terminals need nulls outputted
     for proper timing? */
  Lstream *lstr = XLSTREAM (CONSOLE_TTY_DATA (c)->outstream);

  if (CONSOLE_TTY_REAL_CURSOR_X (c) != CONSOLE_TTY_CURSOR_X (c)
      || CONSOLE_TTY_REAL_CURSOR_Y (c) != CONSOLE_TTY_CURSOR_Y (c))
    {
      int row = CONSOLE_TTY_CURSOR_Y (c);
      int col = CONSOLE_TTY_CURSOR_X (c);
      cmgoto_for_real (c, row, col);
    }

  if (len == 1)
    Lstream_putc (lstr, *str);
  else if (len > 0)
    Lstream_write (lstr, str, len);
}

void
cmputc (int c)
{
  unsigned char ch = (unsigned char) c;

  if (termscript)
    fputc (c, termscript);

  send_string_to_tty_console (cmputc_console, &ch, 1);
}

#if 0

/*
 * Terminals with magicwrap (xn) don't all behave identically.
 * The VT100 leaves the cursor in the last column but will wrap before
 * printing the next character.  I hear that the Concept terminal does
 * the wrap immediately but ignores the next newline it sees.  And some
 * terminals just have buggy firmware, and think that the cursor is still
 * in limbo if we use direct cursor addressing from the phantom column.
 * The only guaranteed safe thing to do is to emit a CRLF immediately
 * after we reach the last column; this takes us to a known state.
 */
void
cmcheckmagic (void)
{
  if (curX == FrameCols)
    {
      if (!MagicWrap || curY >= FrameRows - 1)
	ABORT ();
      if (termscript)
	putc ('\r', termscript);
      putchar ('\r');
      if (termscript)
	putc ('\n', termscript);
      putchar ('\n');
      curX = 0;
      curY++;
    }
}

#endif /* 0 */

/*
 * (Re)Initialize the cost factors, given the output speed of the
 * terminal in DEVICE_TTY_DATA (dev)->ospeed.  (Note: this holds B300,
 * B9600, etc -- ie stuff out of <sgtty.h>.)
 */
void
cm_cost_init (struct console *c)
{
  char *tmp;

  cm_cost_counter = 0;
#define	COST(x,e) (x \
		   ? (cm_cost_counter = 0, tputs (x, 1, e), cm_cost_counter) \
		   : EXPENSIVE)
#define MINCOST(x,e) ((x == 0) \
		      ? EXPENSIVE \
		      : (tmp = tgoto(x, 0, 0), COST(tmp,e)))

  TTY_COST (c).cm_up = COST (TTY_CM (c).up, evalcost);
  TTY_COST (c).cm_down = COST (TTY_CM (c).down, evalcost);
  TTY_COST (c).cm_left = COST (TTY_CM (c).left, evalcost);
  TTY_COST (c).cm_right = COST (TTY_CM (c).right, evalcost);
  TTY_COST (c).cm_home = COST (TTY_CM (c).home, evalcost);
  TTY_COST (c).cm_low_left = COST (TTY_CM (c).low_left, evalcost);
  TTY_COST (c).cm_car_return = COST (TTY_CM (c).car_return, evalcost);

  /*
   * These last three are actually minimum costs.  When (if) they are
   * candidates for the least-cost motion, the real cost is computed.
   * (Note that "0" is the assumed to generate the minimum cost.
   * While this is not necessarily true, I have yet to see a terminal
   * for which is not; all the terminals that have variable-cost
   * cursor motion seem to take straight numeric values.  --ACT)
   */

  TTY_COST (c).cm_abs = MINCOST (TTY_CM (c).abs, evalcost);
  TTY_COST (c).cm_hor_abs = MINCOST (TTY_CM (c).hor_abs, evalcost);
  TTY_COST (c).cm_ver_abs = MINCOST (TTY_CM (c).ver_abs, evalcost);

#undef MINCOST
#undef COST
}

/*
 * Calculate the cost to move from (srcy, srcx) to (dsty, dstx) using
 * up and down, and left and right, and motions.  If doit is set
 * actually perform the motion.
 */

#ifdef NOT_YET
static int
calccost (struct frame *f, int srcy, int srcx, int dsty, int dstx, int doit)
{
  struct console *c = XCONSOLE (FRAME_CONSOLE (f));
  int totalcost = 0;
  int deltay, deltax;
  char *motion;
  int motion_cost;

#if 0
  int ntabs, n2tabs, tabx, tab2x, tabcost;
#endif

  cmputc_console = c;
#if 0
    /* If have just wrapped on a terminal with xn,
       don't believe the cursor position: give up here
       and force use of absolute positioning.  */
    if (curX == Wcm.cm_cols)
      goto fail;
#endif

  deltay = dsty - srcy;
  if (!deltay)
    goto calculate_x;

  if (deltay < 0)
    {
      motion = TTY_CM (c).up;
      motion_cost = TTY_COST (c).cm_up;
      deltay = -deltay;
    }
  else
    {
      motion = TTY_CM (c).down;
      motion_cost = TTY_COST (c).cm_down;
    }

  if (motion_cost == EXPENSIVE)
    {
/*      if (doit) */
	/* #### printing OOF is not acceptable */
      return motion_cost;
    }

  totalcost = motion_cost * deltay;

  if (doit)
    while (--deltay >= 0)
      tputs (motion, 1, cmputc);

calculate_x:

  deltax = dstx - srcx;
  if (!deltax)
    goto done;

  if (deltax < 0)
    {
      motion = TTY_CM (c).left;
      motion_cost = TTY_COST (c).cm_left;
      deltax = -deltax;
    }
  else
    {
      motion = TTY_CM (c).right;
      motion_cost = TTY_COST (c).cm_right;
    }

  if (motion_cost == EXPENSIVE)
    {
/*	if (doit) */
        /* #### printing OOF is not acceptable */
	return motion_cost;
    }

  totalcost += motion_cost * deltax;

  if (doit)
    while (--deltax >= 0)
      tputs (motion, 1, cmputc);

done:
    return totalcost;
}
#endif /* NOT_YET */

#define	USEREL	0
#define	USEHOME	1
#define	USELL	2
#define	USECR	3

#ifdef OLD_CURSOR_MOTION_SHIT
void
cmgoto (struct frame *f, int row, int col)
{
  struct console *c = XCONSOLE (FRAME_CONSOLE (f));
  char *motion;
#if 0
  int frame_x = FRAME_CURSOR_X(f);
  int frame_y = FRAME_CURSOR_Y(f);
  int relcost, directcost, llcost;
  int homecost;
  int use;
  char *dcm;
#endif

  cmputc_console = c;

  /* First the degenerate case */
#if 0
  if (row == frame_y && col == frame_x)
    return;
#endif

  /* #### something is fucked with the non-absolute cases */
  motion = tgoto (TTY_CM (c).abs, col, row);
  tputs (motion, 1, cmputc);
  CONSOLE_TTY_DATA (c)->cursor_x = col;
  CONSOLE_TTY_DATA (c)->cursor_y = row;
  return;

#if 0
  if (frame_y >= 0 && frame_x >= 0)
    {
      /*
       * Pick least-cost motions
       */

      relcost = calccost (f, frame_y, frame_x, row, col, 0);
      use = USEREL;

      homecost = TTY_COST (c).cm_home;
      if (homecost < EXPENSIVE)
	homecost += calccost (f, 0, 0, row, col, 0);

      if (homecost < relcost)
	{
	  relcost = homecost;
	  use = USEHOME;
	}

      llcost = TTY_COST (c).cm_low_left;
      if (llcost < EXPENSIVE)
	llcost += calccost (f, frame_y - 1, 0, row, col, 0);

      if (llcost < relcost)
	{
	  relcost = llcost;
	  use = USELL;
	}

#if 0
      if ((crcost = Wcm.cc_cr) < BIG) {
	  if (Wcm.cm_autolf)
	      if (curY + 1 >= Wcm.cm_rows)
		  crcost = BIG;
	      else
		  crcost += calccost (curY + 1, 0, row, col, 0);
	  else
	      crcost += calccost (curY, 0, row, col, 0);
      }
      if (crcost < relcost)
	  relcost = crcost, use = USECR;
#endif

      directcost = TTY_COST (c).cm_abs;
      dcm = TTY_CM (c).abs;

      if (row == frame_y && TTY_COST (c).cm_hor_abs < EXPENSIVE)
	{
	  directcost = TTY_COST (c).cm_hor_abs;
	  dcm = TTY_CM (c).hor_abs;
	}
      else if (col == frame_x && TTY_COST (c).cm_ver_abs < EXPENSIVE)
	{
	  directcost = TTY_COST (c).cm_ver_abs;
	  dcm = TTY_CM (c).ver_abs;
	}
    }
  else
    {
      directcost = 0;
      relcost = 100000;
      dcm = TTY_CM (c).abs;
    }

  /*
   * In the following comparison, the = in <= is because when the costs
   * are the same, it looks nicer (I think) to move directly there.
   */
  if (directcost <= relcost)
    {
      /* compute REAL direct cost */
      cm_cost_counter = 0;
      motion = (dcm == TTY_CM (c).hor_abs
		? tgoto (dcm, row, col)
		: tgoto (dcm, col, row));
      tputs (motion, 1, evalcost);
      if (cm_cost_counter <= relcost)
	{	/* really is cheaper */
	  tputs (motion, 1, cmputc);
	  FRAME_CURSOR_Y (f) = row;
	  FRAME_CURSOR_X (f) = col;
	  return;
	}
    }

  switch (use)
    {
    case USEHOME:
      tputs (TTY_CM (c).home, 1, cmputc);
      FRAME_CURSOR_X (f) = 0;
      FRAME_CURSOR_Y (f) = 0;
      break;

    case USELL:
      tputs (TTY_CM (c).low_left, 1, cmputc);
      FRAME_CURSOR_Y (f) = FRAME_HEIGHT (f) - 1;
      FRAME_CURSOR_X (f) = 0;
      break;

#if 0
    case USECR:
      tputs (Wcm.cm_cr, 1, cmputc);
      if (Wcm.cm_autolf)
	curY++;
      curX = 0;
      break;
#endif
    }

  calccost (f, FRAME_CURSOR_Y (f), FRAME_CURSOR_X (f), row, col, 1);
  FRAME_CURSOR_Y (f) = row;
  FRAME_CURSOR_X (f) = col;
#endif
}
#endif /* OLD_CURSOR_MOTION_SHIT */

/*****************************************************************************
 cmgoto

 This function is responsible for getting the cursor from its current
 location to the passed location in the most efficient manner
 possible.
 ****************************************************************************/
static void
cmgoto_for_real (struct console *c, int row, int col)
{
  char *motion;

  cmputc_console = c;

  /* First make sure that we actually have to do any work at all. */
  if (CONSOLE_TTY_REAL_CURSOR_X (c) == col
      && CONSOLE_TTY_REAL_CURSOR_Y (c) == row)
    return;

  CONSOLE_TTY_REAL_CURSOR_X (c) = col;
  CONSOLE_TTY_REAL_CURSOR_Y (c) = row;

  /* #### Need to reimplement cost analysis and potential relative
     movement. */

  /* If all else fails, use absolute movement. */
  motion = tgoto (TTY_CM (c).abs, col, row);
  tputs (motion, 1, cmputc);
  CONSOLE_TTY_CURSOR_X (c) = col;
  CONSOLE_TTY_CURSOR_Y (c) = row;
}

void
cmgoto (struct frame *f, int row, int col)
{
  /* We delay cursor motion until we do something other than cursor motion,
     to optimize the case where cmgoto() is called twice in a row. */
  struct console *c = XCONSOLE (FRAME_CONSOLE (f));
  CONSOLE_TTY_CURSOR_X (c) = col;
  CONSOLE_TTY_CURSOR_Y (c) = row;
}

#if 0
/* Clear out all terminal info.
   Used before copying into it the info on the actual terminal.
 */

void
Wcm_clear (void)
{
  xzero (Wcm);
  UP = 0;
  BC = 0;
}
#endif

#if 0
/*
 * Initialized stuff
 * Return 0 if can do CM.
 * Return -1 if cannot.
 * Return -2 if size not specified.
 */

int
Wcm_init (void)
{
#if 0
  if (Wcm.cm_abs && !Wcm.cm_ds)
    return 0;
#endif
  if (Wcm.cm_abs)
    return 0;
  /* Require up and left, and, if no absolute, down and right */
  if (!Wcm.cm_up || !Wcm.cm_left)
    return - 1;
  if (!Wcm.cm_abs && (!Wcm.cm_down || !Wcm.cm_right))
    return - 1;
  /* Check that we know the size of the frame.... */
  if (Wcm.cm_rows <= 0 || Wcm.cm_cols <= 0)
    return - 2;
  return 0;
}
#endif