/* $Id: tty.c,v 1.82 2009-03-27 16:44:51 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include "tmux.h"
void tty_fill_acs(struct tty *);
u_char tty_get_acs(struct tty *, u_char);
void tty_emulate_repeat(
struct tty *, enum tty_code_code, enum tty_code_code, u_int);
void tty_draw_line(struct tty *, struct window_pane *, u_int);
void tty_raw(struct tty *, const char *);
int tty_try_256(struct tty *, u_char, const char *);
int tty_try_88(struct tty *, u_char, const char *);
void tty_attributes(struct tty *, const struct grid_cell *);
void tty_attributes_fg(struct tty *, const struct grid_cell *);
void tty_attributes_bg(struct tty *, const struct grid_cell *);
void tty_cmd_cell(struct tty *, struct window_pane *, va_list);
void tty_cmd_clearendofline(struct tty *, struct window_pane *, va_list);
void tty_cmd_clearendofscreen(struct tty *, struct window_pane *, va_list);
void tty_cmd_clearline(struct tty *, struct window_pane *, va_list);
void tty_cmd_clearscreen(struct tty *, struct window_pane *, va_list);
void tty_cmd_clearstartofline(struct tty *, struct window_pane *, va_list);
void tty_cmd_clearstartofscreen(struct tty *, struct window_pane *, va_list);
void tty_cmd_deletecharacter(struct tty *, struct window_pane *, va_list);
void tty_cmd_deleteline(struct tty *, struct window_pane *, va_list);
void tty_cmd_insertcharacter(struct tty *, struct window_pane *, va_list);
void tty_cmd_insertline(struct tty *, struct window_pane *, va_list);
void tty_cmd_linefeed(struct tty *, struct window_pane *, va_list);
void tty_cmd_reverseindex(struct tty *, struct window_pane *, va_list);
void (*tty_cmds[])(struct tty *, struct window_pane *, va_list) = {
tty_cmd_cell,
tty_cmd_clearendofline,
tty_cmd_clearendofscreen,
tty_cmd_clearline,
tty_cmd_clearscreen,
tty_cmd_clearstartofline,
tty_cmd_clearstartofscreen,
tty_cmd_deletecharacter,
tty_cmd_deleteline,
tty_cmd_insertcharacter,
tty_cmd_insertline,
tty_cmd_linefeed,
tty_cmd_reverseindex,
};
void
tty_init(struct tty *tty, char *path, char *term)
{
tty->path = xstrdup(path);
if (term == NULL)
tty->termname = xstrdup("unknown");
else
tty->termname = xstrdup(term);
tty->flags = 0;
tty->term_flags = 0;
}
int
tty_open(struct tty *tty, char **cause)
{
int mode;
tty->fd = open(tty->path, O_RDWR|O_NONBLOCK);
if (tty->fd == -1) {
xasprintf(cause, "%s: %s", tty->path, strerror(errno));
return (-1);
}
if ((mode = fcntl(tty->fd, F_GETFL)) == -1)
fatal("fcntl");
if (fcntl(tty->fd, F_SETFL, mode|O_NONBLOCK) == -1)
fatal("fcntl");
if (debug_level > 3)
tty->log_fd = open("tmux.out", O_WRONLY|O_CREAT|O_TRUNC, 0644);
else
tty->log_fd = -1;
if ((tty->term = tty_term_find(tty->termname, tty->fd, cause)) == NULL)
goto error;
tty->in = buffer_create(BUFSIZ);
tty->out = buffer_create(BUFSIZ);
tty->flags &= TTY_UTF8;
tty_start_tty(tty);
tty_keys_init(tty);
tty_fill_acs(tty);
return (0);
error: