Commit 9628b887 authored by Karoly Lorentey's avatar Karoly Lorentey
Browse files

lib-src/emacsclient.c: Implemented --here option (open a new Emacs tty). Needs more work.

(here): New variable.
(decode_options): Use it.
(ec_get_tty, ec_set_tty, init_tty, window_change, hang_up_signal): New functions.
(window_change_signal, init_signals, reset_tty, init_pty, copy_from_to): Ditto.
(pty_conversation): Ditto.
(main): Use them.
(master, pty_name, old_tty, tty, old_tty_valid, tty_erase_char): New variables.
(flow_control, meta_key, _sobuf, in_conversation, quit_conversation): Ditto.

lisp/server.el (server-process-filter): Added support for opening a new terminal frame.

dispextern.h (get_frame_size): Renamed to get_tty_size, added tty_output parameter.

dispnew.c (Fredraw_frame): fflush the current terminal instead of stdout.
(direct_output_for_insert, direct_output_forward_char, update_frame_1): Ditto.
(Fding, bitch_at_user): Ditto.
(update_frame_1): Count pending output for current terminal instead of stdout.
(window_change_signal): Resize all terminals.
(change_frame_size): Don't resize all terminals to the same size.

frame.c (Vterminal_frame): Removed.
(syms_of_frame): Removed declaration of Vterminal_frame.
(make_terminal_frame): Set the top frame of the terminal to the new frame.
(Fmake_terminal_frame): Get a new frame size from get_tty_size, don't copy it.
(do_switch_frame): Handle terminal frame visibility.
(next_frame, prev_frame): Skip over frames on different terminals.

frame.h (Vterminal_frame): Removed.

keyboard.c (input_fd): Removed.
(read_avail_input): Removed first argument from read_socket_hook.
Try to read from each available tty, until one succeeds.
(Fsuspend_emacs): Don't suspend if there are multiple terminals.

lisp.h (get_frame_size): Removed superflous declaration.

xterm.c (Xtread_socket): Removed first parameter.
macterm.h (XTread_socket): Ditto.
w32inevt.c (w32_console_read_socket): Ditto.
w32term.c (w32_read_socket): Ditto.

sysdep.c (input_fd): Removed.
(change_input_fd): Removed.
(discard_tty_input): Discard pending input on _all_ input descriptors.
(stuff_char, tabs_safe_p): Use current terminal instead of input_fd.
(init_baud_rate, request_sigio, unrequest_sigio): Ditto.
(init_sys_modes, reset_sys_modes): Ditto.
(narrow_foreground_group, widen_foreground_group): Use stdin.
(init_sys_modes, reset_sys_modes): otty parameter renamed to tty_out.
(get_frame_size): Renamed to get_tty_size, added tty_out parameter.

term.c (read_socket_hook): Removed first parameter.
(clear_end_of_line): Use updating_frame, if possible.
(write_glyphs, insert_glyphs, ins_del_lines): Ditto.
(term_init): Renamed get_frame_size to get_tty_size.

termchar.h (struct tty_output): New entries: top_frame,
previous_terminal_frame.

termhooks.h (read_socket_hook): Removed first parameter.

window.c (init_window_once): Removed reference to Vterminal_frame.

xdisp.c (previous_terminal_frame): Moved to struct tty_output.
(redisplay_internal): Updated to use previous_terminal_frame in tty_output.
Allow for simultaneous refresh of multiple ttys.


git-archimport-id: lorentey@elte.hu--2004/emacs--multi-tty--0--patch-5
parent 4f0359de
......@@ -25,19 +25,18 @@ STATUS
------
We can create frames on new tty devices, but there are problems with
refresh (only the (single) selected frame is refreshed), and input is
read only from the initial terminal. At the moment, the type of the
new terminals must be the same as the initial terminal.
redisplay. Input is read from all terminals (NOT via MULTIKBOARD!).
At the moment, the type of the new terminals must be the same as the
initial terminal. Emacsclient is extended to support opening a new
terminal frame.
To try it out, start up emacs, and evaluate the following:
To try it out, start up the emacs server (M-x server-start), and then
start emacsclient with
(make-terminal-frame '((tty . "/dev/pts/5") (tty-type . "xterm")))
emacsclient -h
(With your own values, of course.) If you switch to the new frame
with M-x other-frame, the new tty is refreshed with the frame
contents. The result of input from the original terminal appears on
the new. If you exit emacs, both terminals are restored to their
previous states.
If you exit emacs, both terminals are restored to their previous
states.
X, Mac, Windows and DOS support is broken.
......@@ -50,6 +49,8 @@ For the NEWS file:
specify a terminal device (`tty' parameter) and a terminal type
(`tty-type' parameter) to `make-terminal-frame'.
** Emacsclient has been extended to support opening a new terminal
frame.
CHANGELOG
---------
......@@ -76,15 +77,50 @@ THINGS THAT ARE DONE
(Done, new frame parameters: `tty' and `tty-type'.)
-- Implement support for reading from multiple terminals.
(Done, read_avail_input tries to read from each terminal, until one
succeeds.)
-- other-frame should cycle through the frames on the `current'
terminal.
(Done. A little fragile, but seems to work.)
-- Support different terminal sizes.
(Done, no problem.)
-- Make sure terminal resizes are handled gracefully. (Could be
problematic.)
(Done. We don't get SIGWINCH for additional ttys, though.)
-- Extend emacsclient to automatically open a new tty when it connects
to Emacs.
(Done. It's an ugly hack, needs more work.)
-- Redisplay must refresh the topmost frame on all terminals, not just
the initial terminal.
(Done, but introduced ugly redisplay problems. Ugh.)
THINGS TO DO
------------
** Fix redisplay problems.
** Make make-terminal-frame look up the tty and tty-type parameters
from the currently selected terminal before the global default.
** Move optimalization parameters (costs) from union output_data to
struct frame.
** Provide a way for emacsclient to tell Emacs that the tty has been
resized.
** Implement terminal deletion, i.e., closing the tty device and
restoring its previous state without exiting Emacs. This should be
exported to the Lisp interpreter.
......@@ -96,27 +132,14 @@ THINGS TO DO
Currently, they are still stored in global variables, so we don't
really support multiple terminal types.
** Support different terminal sizes. (Should be solved by the
previous entry.)
** Make sure terminal resizes are handled gracefully. (Could be
problematic.)
** Implement support for reading from multiple terminals.
** other-frame should cycle through the frames on the `current'
terminal. This means that Emacs must know from which terminal the
last keyboard event came from. (Multikeyboard support may help
with this.)
** Redisplay must refresh the topmost on all terminals, not just
the initial terminal.
** Each keypress should automatically select the frame corresponding
to the terminal that it was coming from. This means that Emacs
must know from which terminal the last keyboard event came from.
(Multikeyboard support may help with this.)
** Make struct tty_output available from Lisp.
** Extend emacsclient to automatically open a new tty when it connects
to Emacs.
** Implement support for starting an interactive Emacs session without
an initial frame. (The user would connect to it and open frames
later, with emacsclient.) Not necessary a good idea.
......
......@@ -41,6 +41,54 @@ Boston, MA 02111-1307, USA. */
# include <pwd.h>
#endif /* not VMS */
/****************************************/
#include <errno.h>
#include <signal.h>
#ifndef INCLUDED_FCNTL
#define INCLUDED_FCNTL
#include <fcntl.h>
#endif
#ifdef HAVE_TERMIOS
#ifndef NO_TERMIO
#include <termio.h>
#endif
#include <termios.h>
#endif /* not HAVE_TERMIOS */
#ifdef __GNU_LIBRARY__
#include <sys/ioctl.h>
#include <termios.h>
#endif
#if (defined (POSIX) || defined (NEED_UNISTD_H)) && defined (HAVE_UNISTD_H)
#include <unistd.h>
#endif
/* Try to establish the correct character to disable terminal functions
in a system-independent manner. Note that USG (at least) define
_POSIX_VDISABLE as 0! */
#ifdef _POSIX_VDISABLE
#define CDISABLE _POSIX_VDISABLE
#else /* not _POSIX_VDISABLE */
#ifdef CDEL
#undef CDISABLE
#define CDISABLE CDEL
#else /* not CDEL */
#define CDISABLE 255
#endif /* not CDEL */
#endif /* not _POSIX_VDISABLE */
/****************************************/
char *getenv (), *getwd ();
char *getcwd ();
......@@ -63,6 +111,9 @@ int eval = 0;
/* The display on which Emacs should work. --display. */
char *display = NULL;
/* Nonzero means open a new Emacs frame on the current terminal. */
int here = 0;
/* If non-NULL, the name of an editor to fallback to if the server
is not running. --alternate-editor. */
const char * alternate_editor = NULL;
......@@ -78,6 +129,7 @@ struct option longopts[] =
{ "eval", no_argument, NULL, 'e' },
{ "help", no_argument, NULL, 'H' },
{ "version", no_argument, NULL, 'V' },
{ "here", no_argument, NULL, 'h' },
{ "alternate-editor", required_argument, NULL, 'a' },
{ "socket-name", required_argument, NULL, 's' },
{ "display", required_argument, NULL, 'd' },
......@@ -95,7 +147,7 @@ decode_options (argc, argv)
while (1)
{
int opt = getopt_long (argc, argv,
"VHnea:s:d:", longopts, 0);
"VHnea:s:d:h", longopts, 0);
if (opt == EOF)
break;
......@@ -134,6 +186,10 @@ decode_options (argc, argv)
exit (0);
break;
case 'h':
here = 1;
break;
case 'H':
print_help_and_exit ();
break;
......@@ -144,6 +200,12 @@ decode_options (argc, argv)
break;
}
}
if (here) {
nowait = 0;
display = 0;
}
}
void
......@@ -157,6 +219,7 @@ Every FILE can be either just a FILENAME or [+LINE[:COLUMN]] FILENAME.\n\
The following OPTIONS are accepted:\n\
-V, --version Just print a version info and return\n\
-H, --help Print this usage information message\n\
-h, --here Open a new Emacs frame on the current terminal\n\
-n, --no-wait Don't wait for the server to return\n\
-e, --eval Evaluate the FILE arguments as ELisp expressions\n\
-d, --display=DISPLAY Visit the file in the given display\n\
......@@ -247,6 +310,455 @@ fail (argc, argv)
}
}
#ifdef HAVE_TERMIOS
/* Adapted from emacs_get_tty() in sysdep.c. */
int
ec_get_tty (int fd, struct termios *settings)
{
bzero (settings, sizeof (struct termios));
if (tcgetattr (fd, settings) < 0)
return -1;
return 0;
}
/* Adapted from emacs_set_tty() in sysdep.c. */
int
ec_set_tty (int fd, struct termios *settings, int flushp)
{
/* Set the primary parameters - baud rate, character size, etcetera. */
int i;
/* We have those nifty POSIX tcmumbleattr functions.
William J. Smith <wjs@wiis.wang.com> writes:
"POSIX 1003.1 defines tcsetattr to return success if it was
able to perform any of the requested actions, even if some
of the requested actions could not be performed.
We must read settings back to ensure tty setup properly.
AIX requires this to keep tty from hanging occasionally." */
/* This make sure that we don't loop indefinitely in here. */
for (i = 0 ; i < 10 ; i++)
if (tcsetattr (fd, flushp ? TCSAFLUSH : TCSADRAIN, settings) < 0)
{
if (errno == EINTR)
continue;
else
return -1;
}
else
{
struct termios new;
bzero (&new, sizeof (new));
/* Get the current settings, and see if they're what we asked for. */
tcgetattr (fd, &new);
/* We cannot use memcmp on the whole structure here because under
* aix386 the termios structure has some reserved field that may
* not be filled in.
*/
if ( new.c_iflag == settings->c_iflag
&& new.c_oflag == settings->c_oflag
&& new.c_cflag == settings->c_cflag
&& new.c_lflag == settings->c_lflag
&& memcmp (new.c_cc, settings->c_cc, NCCS) == 0)
break;
else
continue;
}
return 0;
}
int master;
char *pty_name;
struct termios old_tty;
struct termios tty;
int old_tty_valid;
int tty_erase_char;
int flow_control = 0;
int meta_key = 0;
char _sobuf[BUFSIZ];
/* Adapted from init_sys_modes() in sysdep.c. */
int
init_tty ()
{
if (! isatty (0))
{
fprintf (stderr, "%s: Input is not a terminal", "init_tty");
return 0;
}
ec_get_tty (0, &old_tty);
old_tty_valid = 1;
tty = old_tty;
tty_erase_char = old_tty.c_cc[VERASE];
tty.c_iflag |= (IGNBRK); /* Ignore break condition */
tty.c_iflag &= ~ICRNL; /* Disable map of CR to NL on input */
#ifdef INLCR
tty.c_iflag &= ~INLCR; /* Disable map of NL to CR on input */
#endif
#ifdef ISTRIP
tty.c_iflag &= ~ISTRIP; /* don't strip 8th bit on input */
#endif
tty.c_lflag &= ~ECHO; /* Disable echo */
tty.c_lflag &= ~ICANON; /* Disable erase/kill processing */
#ifdef IEXTEN
tty.c_lflag &= ~IEXTEN; /* Disable other editing characters. */
#endif
tty.c_lflag |= ISIG; /* Enable signals */
if (flow_control)
{
tty.c_iflag |= IXON; /* Enable start/stop output control */
#ifdef IXANY
tty.c_iflag &= ~IXANY;
#endif /* IXANY */
}
else
tty.c_iflag &= ~IXON; /* Disable start/stop output control */
tty.c_oflag &= ~ONLCR; /* Disable map of NL to CR-NL
on output */
tty.c_oflag &= ~TAB3; /* Disable tab expansion */
#ifdef CS8
if (meta_key)
{
tty.c_cflag |= CS8; /* allow 8th bit on input */
tty.c_cflag &= ~PARENB; /* Don't check parity */
}
#endif
tty.c_cc[VINTR] = CDISABLE;
tty.c_cc[VQUIT] = CDISABLE;
tty.c_cc[VMIN] = 1; /* Input should wait for at least 1 char */
tty.c_cc[VTIME] = 0; /* no matter how long that takes. */
#ifdef VSWTCH
tty.c_cc[VSWTCH] = CDISABLE; /* Turn off shell layering use of C-z */
#endif
#ifdef VSUSP
tty.c_cc[VSUSP] = CDISABLE; /* Turn off mips handling of C-z. */
#endif /* VSUSP */
#ifdef V_DSUSP
tty.c_cc[V_DSUSP] = CDISABLE; /* Turn off mips handling of C-y. */
#endif /* V_DSUSP */
#ifdef VDSUSP /* Some systems have VDSUSP, some have V_DSUSP. */
tty.c_cc[VDSUSP] = CDISABLE;
#endif /* VDSUSP */
#ifdef VLNEXT
tty.c_cc[VLNEXT] = CDISABLE;
#endif /* VLNEXT */
#ifdef VREPRINT
tty.c_cc[VREPRINT] = CDISABLE;
#endif /* VREPRINT */
#ifdef VWERASE
tty.c_cc[VWERASE] = CDISABLE;
#endif /* VWERASE */
#ifdef VDISCARD
tty.c_cc[VDISCARD] = CDISABLE;
#endif /* VDISCARD */
if (flow_control)
{
#ifdef VSTART
tty.c_cc[VSTART] = '\021';
#endif /* VSTART */
#ifdef VSTOP
tty.c_cc[VSTOP] = '\023';
#endif /* VSTOP */
}
else
{
#ifdef VSTART
tty.c_cc[VSTART] = CDISABLE;
#endif /* VSTART */
#ifdef VSTOP
tty.c_cc[VSTOP] = CDISABLE;
#endif /* VSTOP */
}
#ifdef SET_LINE_DISCIPLINE
/* Need to explicitly request TERMIODISC line discipline or
Ultrix's termios does not work correctly. */
tty.c_line = SET_LINE_DISCIPLINE;
#endif
#ifdef AIX
#ifndef IBMR2AIX
/* AIX enhanced edit loses NULs, so disable it. */
tty.c_line = 0;
tty.c_iflag &= ~ASCEDIT;
#else
tty.c_cc[VSTRT] = 255;
tty.c_cc[VSTOP] = 255;
tty.c_cc[VSUSP] = 255;
tty.c_cc[VDSUSP] = 255;
#endif /* IBMR2AIX */
if (flow_control)
{
#ifdef VSTART
tty.c_cc[VSTART] = '\021';
#endif /* VSTART */
#ifdef VSTOP
tty.c_cc[VSTOP] = '\023';
#endif /* VSTOP */
}
/* Also, PTY overloads NUL and BREAK.
don't ignore break, but don't signal either, so it looks like NUL.
This really serves a purpose only if running in an XTERM window
or via TELNET or the like, but does no harm elsewhere. */
tty.c_iflag &= ~IGNBRK;
tty.c_iflag &= ~BRKINT;
#endif /* AIX */
ec_set_tty (0, &tty, 0);
/* This code added to insure that, if flow-control is not to be used,
we have an unlocked terminal at the start. */
#ifdef TCXONC
if (!flow_control) ioctl (0, TCXONC, 1);
#endif
#ifndef APOLLO
#ifdef TIOCSTART
if (!flow_control) ioctl (0, TIOCSTART, 0);
#endif
#endif
#if defined (HAVE_TERMIOS) || defined (HPUX9)
#ifdef TCOON
if (!flow_control) tcflow (0, TCOON);
#endif
#endif
#ifdef _IOFBF
/* This symbol is defined on recent USG systems.
Someone says without this call USG won't really buffer the file
even with a call to setbuf. */
setvbuf (stdout, (char *) _sobuf, _IOFBF, sizeof _sobuf);
#else
setbuf (stdout, (char *) _sobuf);
#endif
return 1;
}
void
window_change ()
{
int width, height;
#ifdef TIOCGWINSZ
{
/* BSD-style. */
struct winsize size;
if (ioctl (0, TIOCGWINSZ, &size) == -1)
width = height = 0;
else
{
width = size.ws_col;
height = size.ws_row;
}
}
#else
#ifdef TIOCGSIZE
{
/* SunOS - style. */
struct ttysize size;
if (ioctl (0, TIOCGSIZE, &size) == -1)
width = height = 0;
else
{
width = size.ts_cols;
height = size.ts_lines;
}
}
#endif /* not SunOS-style */
#endif /* not BSD-style */
#ifdef TIOCSWINSZ
{
/* BSD-style. */
struct winsize size;
size.ws_row = height;
size.ws_col = width;
ioctl (master, TIOCSWINSZ, &size);
}
#else
#ifdef TIOCSSIZE
{
/* SunOS - style. */
struct ttysize size;
size.ts_lines = height;
size.ts_cols = width;
ioctl (master, TIOCGSIZE, &size);
}
#endif /* not SunOS-style */
#endif /* not BSD-style */
}
int in_conversation = 0;
int quit_conversation = 0;
SIGTYPE
hang_up_signal (int signalnum)
{
int old_errno = errno;
if (! in_conversation)
return;
quit_conversation = 1;
errno = old_errno;
}
SIGTYPE
window_change_signal (int signalnum)
{
int old_errno = errno;
if (! in_conversation)
goto end;
window_change();
end:
signal (SIGWINCH, window_change_signal);
errno = old_errno;
}
int
init_signals ()
{
/* Set up signal handlers. */
signal (SIGWINCH, window_change_signal);
signal (SIGHUP, hang_up_signal);
return 1;
}
/* Adapted from reset_sys_modes in sysdep.c. */
int
reset_tty ()
{
fflush (stdout);
#ifdef BSD_SYSTEM
#ifndef BSD4_1
/* Avoid possible loss of output when changing terminal modes. */
fsync (fileno (stdout));
#endif
#endif
#ifdef F_SETFL
#ifdef O_NDELAY
fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~O_NDELAY);
#endif
#endif /* F_SETFL */
if (old_tty_valid)
while (ec_set_tty (0, &old_tty, 0) < 0 && errno == EINTR)
;
return 1;
}
int
init_pty ()
{
master = getpt ();
if (master < 0)
return 0;
if (grantpt (master) < 0 || unlockpt (master) < 0)
goto close_master;
pty_name = strdup (ptsname (master));
if (! pty_name)
goto close_master;
/* Propagate window size. */
window_change ();
return 1;
close_master:
close (master);
return 0;
}
int
copy_from_to (int in, int out)
{
static char buf[BUFSIZ];
int nread = read (in, &buf, BUFSIZ);
if (nread == 0)
return 1; /* EOF */
else if (nread < 0 && errno != EAGAIN)
return 0; /* Error */
else if (nread > 0)
{
int r = 0;
int written = 0;
do {
r = write (