2009-03-07 11:29:06 +01:00
|
|
|
/* $Id: tty.c,v 1.80 2009-03-07 10:29:06 nicm Exp $ */
|
2007-11-27 20:23:34 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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>
|
2008-06-19 00:08:56 +02:00
|
|
|
#include <unistd.h>
|
2007-11-27 20:23:34 +01:00
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
|
|
|
void tty_fill_acs(struct tty *);
|
|
|
|
u_char tty_get_acs(struct tty *, u_char);
|
|
|
|
|
2009-01-10 00:57:42 +01:00
|
|
|
void tty_emulate_repeat(
|
|
|
|
struct tty *, enum tty_code_code, enum tty_code_code, u_int);
|
2009-02-11 20:06:58 +01:00
|
|
|
void tty_draw_line(struct tty *, struct window_pane *, u_int);
|
2008-12-13 18:41:49 +01:00
|
|
|
|
2007-11-27 20:23:34 +01:00
|
|
|
void tty_raw(struct tty *, const char *);
|
|
|
|
|
2009-03-07 11:29:06 +01:00
|
|
|
int tty_try_256(struct tty *, u_char, const char *);
|
|
|
|
int tty_try_88(struct tty *, u_char, const char *);
|
|
|
|
|
2008-09-25 22:08:57 +02:00
|
|
|
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 *);
|
|
|
|
|
2009-02-11 08:02:34 +01:00
|
|
|
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) = {
|
2009-01-10 00:57:42 +01:00
|
|
|
tty_cmd_cell,
|
|
|
|
tty_cmd_clearendofline,
|
|
|
|
tty_cmd_clearendofscreen,
|
|
|
|
tty_cmd_clearline,
|
|
|
|
tty_cmd_clearscreen,
|
|
|
|
tty_cmd_clearstartofline,
|
|
|
|
tty_cmd_clearstartofscreen,
|
2008-09-25 22:08:57 +02:00
|
|
|
tty_cmd_deletecharacter,
|
|
|
|
tty_cmd_deleteline,
|
2009-01-10 00:57:42 +01:00
|
|
|
tty_cmd_insertcharacter,
|
|
|
|
tty_cmd_insertline,
|
2008-09-25 22:08:57 +02:00
|
|
|
tty_cmd_linefeed,
|
2009-01-10 00:57:42 +01:00
|
|
|
tty_cmd_reverseindex,
|
2008-09-25 22:08:57 +02:00
|
|
|
};
|
2007-11-27 20:23:34 +01:00
|
|
|
|
|
|
|
void
|
|
|
|
tty_init(struct tty *tty, char *path, char *term)
|
|
|
|
{
|
|
|
|
tty->path = xstrdup(path);
|
2007-12-06 21:53:48 +01:00
|
|
|
if (term == NULL)
|
|
|
|
tty->termname = xstrdup("unknown");
|
|
|
|
else
|
|
|
|
tty->termname = xstrdup(term);
|
2008-09-10 00:16:37 +02:00
|
|
|
tty->flags = 0;
|
2008-09-25 22:08:57 +02:00
|
|
|
tty->term_flags = 0;
|
2007-11-27 20:23:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
tty_open(struct tty *tty, char **cause)
|
|
|
|
{
|
2008-12-06 10:30:25 +01:00
|
|
|
int mode;
|
2007-11-27 20:23:34 +01:00
|
|
|
|
|
|
|
tty->fd = open(tty->path, O_RDWR|O_NONBLOCK);
|
|
|
|
if (tty->fd == -1) {
|
|
|
|
xasprintf(cause, "%s: %s", tty->path, strerror(errno));
|
|
|
|
return (-1);
|
|
|
|
}
|
2008-09-26 08:45:28 +02:00
|
|
|
|
2008-06-19 00:08:56 +02:00
|
|
|
if ((mode = fcntl(tty->fd, F_GETFL)) == -1)
|
|
|
|
fatal("fcntl");
|
|
|
|
if (fcntl(tty->fd, F_SETFL, mode|O_NONBLOCK) == -1)
|
|
|
|
fatal("fcntl");
|
|
|
|
|
2008-09-25 22:08:57 +02:00
|
|
|
if (debug_level > 3)
|
2008-09-10 00:16:37 +02:00
|
|
|
tty->log_fd = open("tmux.out", O_WRONLY|O_CREAT|O_TRUNC, 0644);
|
2008-09-25 22:08:57 +02:00
|
|
|
else
|
2008-09-10 00:16:37 +02:00
|
|
|
tty->log_fd = -1;
|
|
|
|
|
2009-01-10 00:57:42 +01:00
|
|
|
if ((tty->term = tty_term_find(tty->termname, tty->fd, cause)) == NULL)
|
2007-12-06 19:28:55 +01:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
tty->in = buffer_create(BUFSIZ);
|
|
|
|
tty->out = buffer_create(BUFSIZ);
|
|
|
|
|
2008-09-10 00:16:37 +02:00
|
|
|
tty->flags &= TTY_UTF8;
|
2009-01-18 13:09:42 +01:00
|
|
|
|
|
|
|
tty_start_tty(tty);
|
|
|
|
|
|
|
|
tty_keys_init(tty);
|
|
|
|
|
|
|
|
tty_fill_acs(tty);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
error:
|
|
|
|
close(tty->fd);
|
|
|
|
tty->fd = -1;
|
|
|
|
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tty_start_tty(struct tty *tty)
|
|
|
|
{
|
|
|
|
struct termios tio;
|
|
|
|
#ifdef TIOCFLUSH
|
|
|
|
int what;
|
|
|
|
#endif
|
2007-12-06 19:28:55 +01:00
|
|
|
|
|
|
|
if (tcgetattr(tty->fd, &tty->tio) != 0)
|
|
|
|
fatal("tcgetattr failed");
|
2008-06-10 20:51:22 +02:00
|
|
|
memcpy(&tio, &tty->tio, sizeof tio);
|
2008-07-02 18:18:24 +02:00
|
|
|
tio.c_iflag &= ~(IXON|IXOFF|ICRNL|INLCR|IGNCR|IMAXBEL|ISTRIP);
|
2008-06-19 00:08:56 +02:00
|
|
|
tio.c_iflag |= IGNBRK;
|
2008-07-02 18:18:24 +02:00
|
|
|
tio.c_oflag &= ~(OPOST|ONLCR|OCRNL|ONLRET);
|
2008-09-08 19:40:51 +02:00
|
|
|
tio.c_lflag &= ~(IEXTEN|ICANON|ECHO|ECHOE|ECHONL|ECHOCTL|
|
|
|
|
ECHOPRT|ECHOKE|ECHOCTL|ISIG);
|
2008-06-19 00:21:51 +02:00
|
|
|
tio.c_cc[VMIN] = 1;
|
2008-06-19 00:08:56 +02:00
|
|
|
tio.c_cc[VTIME] = 0;
|
2007-12-06 19:28:55 +01:00
|
|
|
if (tcsetattr(tty->fd, TCSANOW, &tio) != 0)
|
|
|
|
fatal("tcsetattr failed");
|
|
|
|
|
2007-12-16 18:18:43 +01:00
|
|
|
#ifdef TIOCFLUSH
|
2007-12-06 19:28:55 +01:00
|
|
|
what = 0;
|
|
|
|
if (ioctl(tty->fd, TIOCFLUSH, &what) != 0)
|
|
|
|
fatal("ioctl(TIOCFLUSH)");
|
2007-12-16 18:18:43 +01:00
|
|
|
#endif
|
2007-12-06 19:28:55 +01:00
|
|
|
|
2009-01-10 00:57:42 +01:00
|
|
|
tty_putcode(tty, TTYC_IS1);
|
|
|
|
tty_putcode(tty, TTYC_IS2);
|
|
|
|
tty_putcode(tty, TTYC_IS3);
|
|
|
|
|
|
|
|
tty_putcode(tty, TTYC_SMCUP);
|
|
|
|
tty_putcode(tty, TTYC_SMKX);
|
|
|
|
tty_putcode(tty, TTYC_ENACS);
|
|
|
|
tty_putcode(tty, TTYC_CLEAR);
|
2009-01-27 22:39:15 +01:00
|
|
|
|
|
|
|
tty_putcode(tty, TTYC_CNORM);
|
2009-01-28 20:52:21 +01:00
|
|
|
if (tty_term_has(tty->term, TTYC_KMOUS))
|
|
|
|
tty_puts(tty, "\033[?1000l");
|
2008-06-21 14:41:05 +02:00
|
|
|
|
2009-01-18 13:09:42 +01:00
|
|
|
memcpy(&tty->cell, &grid_default_cell, sizeof tty->cell);
|
2008-06-21 14:41:05 +02:00
|
|
|
|
2009-01-18 13:09:42 +01:00
|
|
|
tty->cx = UINT_MAX;
|
|
|
|
tty->cy = UINT_MAX;
|
2007-12-06 19:28:55 +01:00
|
|
|
|
2009-01-18 13:09:42 +01:00
|
|
|
tty->rlower = UINT_MAX;
|
|
|
|
tty->rupper = UINT_MAX;
|
2009-01-27 22:39:15 +01:00
|
|
|
|
2009-01-28 20:52:21 +01:00
|
|
|
tty->mode = MODE_CURSOR;
|
2009-01-18 13:09:42 +01:00
|
|
|
}
|
2007-12-06 19:28:55 +01:00
|
|
|
|
2009-01-18 13:09:42 +01:00
|
|
|
void
|
|
|
|
tty_stop_tty(struct tty *tty)
|
|
|
|
{
|
|
|
|
struct winsize ws;
|
2007-12-06 19:28:55 +01:00
|
|
|
|
2009-01-18 13:09:42 +01:00
|
|
|
/*
|
|
|
|
* Be flexible about error handling and try not kill the server just
|
|
|
|
* because the fd is invalid. Things like ssh -t can easily leave us
|
|
|
|
* with a dead tty.
|
|
|
|
*/
|
2009-02-17 19:53:10 +01:00
|
|
|
if (ioctl(tty->fd, TIOCGWINSZ, &ws) == -1)
|
|
|
|
return;
|
|
|
|
if (tcsetattr(tty->fd, TCSANOW, &tty->tio) == -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
tty_raw(tty, tty_term_string2(tty->term, TTYC_CSR, 0, ws.ws_row - 1));
|
|
|
|
tty_raw(tty, tty_term_string(tty->term, TTYC_RMACS));
|
|
|
|
tty_raw(tty, tty_term_string(tty->term, TTYC_SGR0));
|
|
|
|
tty_raw(tty, tty_term_string(tty->term, TTYC_CLEAR));
|
|
|
|
tty_raw(tty, tty_term_string(tty->term, TTYC_RMKX));
|
|
|
|
tty_raw(tty, tty_term_string(tty->term, TTYC_RMCUP));
|
|
|
|
|
|
|
|
tty_raw(tty, tty_term_string(tty->term, TTYC_CNORM));
|
|
|
|
if (tty_term_has(tty->term, TTYC_KMOUS))
|
|
|
|
tty_raw(tty, "\033[?1000l");
|
2007-12-06 19:28:55 +01:00
|
|
|
}
|
|
|
|
|
2009-01-10 00:57:42 +01:00
|
|
|
void
|
|
|
|
tty_fill_acs(struct tty *tty)
|
|
|
|
{
|
|
|
|
const char *ptr;
|
|
|
|
|
|
|
|
memset(tty->acs, 0, sizeof tty->acs);
|
|
|
|
if (!tty_term_has(tty->term, TTYC_ACSC))
|
|
|
|
return;
|
|
|
|
|
|
|
|
ptr = tty_term_string(tty->term, TTYC_ACSC);
|
|
|
|
if (strlen(ptr) % 2 != 0)
|
|
|
|
return;
|
|
|
|
for (; *ptr != '\0'; ptr += 2)
|
|
|
|
tty->acs[(u_char) ptr[0]] = ptr[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
u_char
|
|
|
|
tty_get_acs(struct tty *tty, u_char ch)
|
|
|
|
{
|
|
|
|
if (tty->acs[ch] != '\0')
|
|
|
|
return (tty->acs[ch]);
|
|
|
|
return (ch);
|
|
|
|
}
|
|
|
|
|
2007-12-06 19:28:55 +01:00
|
|
|
void
|
2009-01-18 13:09:42 +01:00
|
|
|
tty_close(struct tty *tty, int no_stop)
|
2007-12-06 19:28:55 +01:00
|
|
|
{
|
2007-12-06 21:53:48 +01:00
|
|
|
if (tty->fd == -1)
|
|
|
|
return;
|
|
|
|
|
2008-09-10 00:16:37 +02:00
|
|
|
if (tty->log_fd != -1) {
|
|
|
|
close(tty->log_fd);
|
|
|
|
tty->log_fd = -1;
|
|
|
|
}
|
|
|
|
|
2009-01-18 13:09:42 +01:00
|
|
|
if (!no_stop)
|
|
|
|
tty_stop_tty(tty);
|
2009-02-16 20:01:16 +01:00
|
|
|
|
2009-01-10 00:57:42 +01:00
|
|
|
tty_term_free(tty->term);
|
2007-12-06 19:28:55 +01:00
|
|
|
tty_keys_free(tty);
|
|
|
|
|
|
|
|
close(tty->fd);
|
|
|
|
tty->fd = -1;
|
|
|
|
|
|
|
|
buffer_destroy(tty->in);
|
|
|
|
buffer_destroy(tty->out);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-01-18 13:09:42 +01:00
|
|
|
tty_free(struct tty *tty, int no_stop)
|
2007-12-06 19:28:55 +01:00
|
|
|
{
|
2009-01-18 13:09:42 +01:00
|
|
|
tty_close(tty, no_stop);
|
2007-12-06 19:28:55 +01:00
|
|
|
|
|
|
|
if (tty->path != NULL)
|
|
|
|
xfree(tty->path);
|
|
|
|
if (tty->termname != NULL)
|
|
|
|
xfree(tty->termname);
|
|
|
|
}
|
|
|
|
|
2009-01-10 00:57:42 +01:00
|
|
|
void
|
|
|
|
tty_raw(struct tty *tty, const char *s)
|
2007-12-06 19:28:55 +01:00
|
|
|
{
|
2009-01-10 00:57:42 +01:00
|
|
|
write(tty->fd, s, strlen(s));
|
2007-11-27 20:23:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-01-10 00:57:42 +01:00
|
|
|
tty_putcode(struct tty *tty, enum tty_code_code code)
|
2007-11-27 20:23:34 +01:00
|
|
|
{
|
2009-01-10 00:57:42 +01:00
|
|
|
tty_puts(tty, tty_term_string(tty->term, code));
|
2007-11-27 20:23:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-01-10 00:57:42 +01:00
|
|
|
tty_putcode1(struct tty *tty, enum tty_code_code code, int a)
|
2007-11-27 20:23:34 +01:00
|
|
|
{
|
2009-01-10 00:57:42 +01:00
|
|
|
if (a < 0)
|
2007-11-27 20:23:34 +01:00
|
|
|
return;
|
2009-01-10 00:57:42 +01:00
|
|
|
tty_puts(tty, tty_term_string1(tty->term, code, a));
|
2008-06-21 14:41:05 +02:00
|
|
|
}
|
|
|
|
|
2007-11-27 20:23:34 +01:00
|
|
|
void
|
2009-01-10 00:57:42 +01:00
|
|
|
tty_putcode2(struct tty *tty, enum tty_code_code code, int a, int b)
|
2007-11-27 20:23:34 +01:00
|
|
|
{
|
2009-01-10 00:57:42 +01:00
|
|
|
if (a < 0 || b < 0)
|
|
|
|
return;
|
|
|
|
tty_puts(tty, tty_term_string2(tty->term, code, a, b));
|
2007-11-27 20:23:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tty_puts(struct tty *tty, const char *s)
|
|
|
|
{
|
2009-01-10 00:57:42 +01:00
|
|
|
if (*s == '\0')
|
2008-09-23 19:54:35 +02:00
|
|
|
return;
|
2009-01-10 00:57:42 +01:00
|
|
|
buffer_write(tty->out, s, strlen(s));
|
2008-09-10 00:16:37 +02:00
|
|
|
|
|
|
|
if (tty->log_fd != -1)
|
2009-01-10 00:57:42 +01:00
|
|
|
write(tty->log_fd, s, strlen(s));
|
2007-11-27 20:23:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tty_putc(struct tty *tty, char ch)
|
|
|
|
{
|
2009-02-12 00:16:45 +01:00
|
|
|
u_int sx;
|
|
|
|
|
2008-09-25 22:08:57 +02:00
|
|
|
if (tty->cell.attr & GRID_ATTR_CHARSET)
|
2007-11-27 20:23:34 +01:00
|
|
|
ch = tty_get_acs(tty, ch);
|
|
|
|
buffer_write8(tty->out, ch);
|
2008-09-10 00:16:37 +02:00
|
|
|
|
2009-02-12 00:16:45 +01:00
|
|
|
if (ch >= 0x20) {
|
|
|
|
sx = tty->sx;
|
|
|
|
if (tty->term->flags & TERM_EARLYWRAP)
|
|
|
|
sx--;
|
|
|
|
|
|
|
|
if (tty->cx == sx) {
|
|
|
|
tty->cx = 0;
|
|
|
|
tty->cy++;
|
|
|
|
} else
|
|
|
|
tty->cx++;
|
|
|
|
}
|
2009-01-12 00:31:46 +01:00
|
|
|
|
2008-09-10 00:16:37 +02:00
|
|
|
if (tty->log_fd != -1)
|
|
|
|
write(tty->log_fd, &ch, 1);
|
2007-11-27 20:23:34 +01:00
|
|
|
}
|
|
|
|
|
2008-06-18 20:52:44 +02:00
|
|
|
void
|
|
|
|
tty_set_title(struct tty *tty, const char *title)
|
|
|
|
{
|
2008-06-21 15:11:28 +02:00
|
|
|
if (strstr(tty->termname, "xterm") == NULL &&
|
|
|
|
strstr(tty->termname, "rxvt") == NULL &&
|
|
|
|
strcmp(tty->termname, "screen") != 0)
|
|
|
|
return;
|
|
|
|
|
2008-06-23 18:58:49 +02:00
|
|
|
tty_puts(tty, "\033]0;");
|
2008-06-18 20:52:44 +02:00
|
|
|
tty_puts(tty, title);
|
|
|
|
tty_putc(tty, '\007');
|
|
|
|
}
|
|
|
|
|
2009-01-28 20:52:21 +01:00
|
|
|
void
|
|
|
|
tty_update_mode(struct tty *tty, int mode)
|
|
|
|
{
|
|
|
|
int changed;
|
|
|
|
|
|
|
|
if (tty->flags & TTY_NOCURSOR)
|
|
|
|
mode &= ~MODE_CURSOR;
|
|
|
|
|
|
|
|
changed = mode ^ tty->mode;
|
|
|
|
if (changed & MODE_CURSOR) {
|
|
|
|
if (mode & MODE_CURSOR)
|
|
|
|
tty_putcode(tty, TTYC_CNORM);
|
|
|
|
else
|
|
|
|
tty_putcode(tty, TTYC_CIVIS);
|
|
|
|
}
|
|
|
|
if (changed & MODE_MOUSE) {
|
|
|
|
if (mode & MODE_MOUSE)
|
|
|
|
tty_puts(tty, "\033[?1000h");
|
|
|
|
else
|
|
|
|
tty_puts(tty, "\033[?1000l");
|
|
|
|
}
|
|
|
|
tty->mode = mode;
|
|
|
|
}
|
|
|
|
|
2008-12-13 18:41:49 +01:00
|
|
|
void
|
2009-01-10 00:57:42 +01:00
|
|
|
tty_emulate_repeat(
|
|
|
|
struct tty *tty, enum tty_code_code code, enum tty_code_code code1, u_int n)
|
|
|
|
{
|
|
|
|
if (tty_term_has(tty->term, code))
|
|
|
|
tty_putcode1(tty, code, n);
|
|
|
|
else {
|
|
|
|
while (n-- > 0)
|
|
|
|
tty_putcode(tty, code1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-11 20:06:58 +01:00
|
|
|
void
|
|
|
|
tty_draw_line(struct tty *tty, struct window_pane *wp, u_int py)
|
|
|
|
{
|
|
|
|
struct screen *s = wp->screen;
|
|
|
|
const struct grid_cell *gc;
|
|
|
|
struct grid_cell tc;
|
|
|
|
u_int i;
|
|
|
|
|
|
|
|
for (i = 0; i < tty->sx; i++) {
|
|
|
|
gc = grid_view_peek_cell(s->grid, i, py);
|
|
|
|
|
|
|
|
tty_cursor(tty, i, py, wp->yoff);
|
|
|
|
if (screen_check_selection(s, i, py)) {
|
|
|
|
memcpy(&tc, &s->sel.cell, sizeof tc);
|
|
|
|
tc.data = gc->data;
|
|
|
|
tty_cell(tty, &tc);
|
|
|
|
} else
|
|
|
|
tty_cell(tty, gc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-10 00:57:42 +01:00
|
|
|
void
|
2009-02-11 08:02:34 +01:00
|
|
|
tty_write(struct tty *tty, struct window_pane *wp, enum tty_cmd cmd, ...)
|
2008-12-13 18:41:49 +01:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, cmd);
|
2009-02-11 08:02:34 +01:00
|
|
|
tty_vwrite(tty, wp, cmd, ap);
|
2008-12-13 18:41:49 +01:00
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2007-11-27 20:23:34 +01:00
|
|
|
void
|
2009-01-12 00:31:46 +01:00
|
|
|
tty_vwrite(
|
2009-02-11 08:02:34 +01:00
|
|
|
struct tty *tty, struct window_pane *wp, enum tty_cmd cmd, va_list ap)
|
2007-11-27 20:23:34 +01:00
|
|
|
{
|
2009-01-10 00:57:42 +01:00
|
|
|
if (tty->flags & TTY_FREEZE || tty->term == NULL)
|
2008-06-20 08:36:01 +02:00
|
|
|
return;
|
2008-09-25 22:08:57 +02:00
|
|
|
if (tty_cmds[cmd] != NULL)
|
2009-02-11 08:02:34 +01:00
|
|
|
tty_cmds[cmd](tty, wp, ap);
|
2008-09-25 22:08:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-02-11 08:02:34 +01:00
|
|
|
tty_cmd_insertcharacter(struct tty *tty, struct window_pane *wp, va_list ap)
|
2008-09-25 22:08:57 +02:00
|
|
|
{
|
2009-02-11 08:02:34 +01:00
|
|
|
struct screen *s = wp->screen;
|
|
|
|
u_int ua;
|
2008-09-25 22:08:57 +02:00
|
|
|
|
|
|
|
ua = va_arg(ap, u_int);
|
|
|
|
|
|
|
|
tty_reset(tty);
|
|
|
|
|
2009-02-11 19:44:08 +01:00
|
|
|
tty_cursor(tty, s->old_cx, s->old_cy, wp->yoff);
|
2009-01-10 23:28:40 +01:00
|
|
|
if (tty_term_has(tty->term, TTYC_ICH) ||
|
|
|
|
tty_term_has(tty->term, TTYC_ICH1))
|
2009-02-11 19:44:08 +01:00
|
|
|
tty_emulate_repeat(tty, TTYC_ICH, TTYC_ICH1, ua);
|
2009-01-10 23:28:40 +01:00
|
|
|
else {
|
|
|
|
tty_putcode(tty, TTYC_SMIR);
|
|
|
|
while (ua-- > 0)
|
|
|
|
tty_putc(tty, ' ');
|
|
|
|
tty_putcode(tty, TTYC_RMIR);
|
|
|
|
}
|
2008-09-25 22:08:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-02-11 08:02:34 +01:00
|
|
|
tty_cmd_deletecharacter(struct tty *tty, struct window_pane *wp, va_list ap)
|
2008-09-25 22:08:57 +02:00
|
|
|
{
|
2009-02-11 08:02:34 +01:00
|
|
|
struct screen *s = wp->screen;
|
|
|
|
u_int ua;
|
2008-09-25 22:08:57 +02:00
|
|
|
|
|
|
|
ua = va_arg(ap, u_int);
|
|
|
|
|
|
|
|
tty_reset(tty);
|
|
|
|
|
2009-02-11 19:44:08 +01:00
|
|
|
tty_cursor(tty, s->old_cx, s->old_cy, wp->yoff);
|
2009-01-10 00:57:42 +01:00
|
|
|
tty_emulate_repeat(tty, TTYC_DCH, TTYC_DCH1, ua);
|
2008-09-25 22:08:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-02-11 08:02:34 +01:00
|
|
|
tty_cmd_insertline(struct tty *tty, struct window_pane *wp, va_list ap)
|
2008-09-25 22:08:57 +02:00
|
|
|
{
|
2009-02-11 08:02:34 +01:00
|
|
|
struct screen *s = wp->screen;
|
2009-02-11 20:06:58 +01:00
|
|
|
u_int ua, i;
|
|
|
|
|
|
|
|
if (!tty_term_has(tty->term, TTYC_CSR)) {
|
|
|
|
/*
|
|
|
|
* Scroll region unsupported. Redraw using data from screen
|
|
|
|
* (already updated).
|
|
|
|
*/
|
|
|
|
if (s->old_cy < s->old_rupper || s->old_cy > s->old_rlower) {
|
|
|
|
for (i = s->old_cy; i < screen_size_y(s); i++)
|
|
|
|
tty_draw_line(tty, wp, i);
|
|
|
|
} else {
|
|
|
|
for (i = s->old_rupper; i <= s->old_rlower; i++)
|
|
|
|
tty_draw_line(tty, wp, i);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2008-09-25 22:08:57 +02:00
|
|
|
|
|
|
|
ua = va_arg(ap, u_int);
|
|
|
|
|
|
|
|
tty_reset(tty);
|
2009-02-11 19:44:08 +01:00
|
|
|
|
2009-02-11 18:04:39 +01:00
|
|
|
tty_region(tty, s->old_rupper, s->old_rlower, wp->yoff);
|
2008-09-25 22:08:57 +02:00
|
|
|
|
2009-02-11 19:44:08 +01:00
|
|
|
tty_cursor(tty, s->old_cx, s->old_cy, wp->yoff);
|
2009-01-10 00:57:42 +01:00
|
|
|
tty_emulate_repeat(tty, TTYC_IL, TTYC_IL1, ua);
|
2008-09-25 22:08:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-02-11 08:02:34 +01:00
|
|
|
tty_cmd_deleteline(struct tty *tty, struct window_pane *wp, va_list ap)
|
2008-09-25 22:08:57 +02:00
|
|
|
{
|
2009-02-11 08:02:34 +01:00
|
|
|
struct screen *s = wp->screen;
|
2009-02-11 20:06:58 +01:00
|
|
|
u_int ua, i;
|
|
|
|
|
|
|
|
if (!tty_term_has(tty->term, TTYC_CSR)) {
|
|
|
|
/*
|
|
|
|
* Scroll region unsupported. Redraw using data from screen
|
|
|
|
* (already updated).
|
|
|
|
*/
|
|
|
|
if (s->old_cy < s->old_rupper || s->old_cy > s->old_rlower) {
|
|
|
|
for (i = s->old_cy; i < screen_size_y(s); i++)
|
|
|
|
tty_draw_line(tty, wp, i);
|
|
|
|
} else {
|
|
|
|
for (i = s->old_rupper; i <= s->old_rlower; i++)
|
|
|
|
tty_draw_line(tty, wp, i);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2008-09-25 22:08:57 +02:00
|
|
|
|
|
|
|
ua = va_arg(ap, u_int);
|
|
|
|
|
|
|
|
tty_reset(tty);
|
2009-02-11 19:44:08 +01:00
|
|
|
|
2009-02-11 18:04:39 +01:00
|
|
|
tty_region(tty, s->old_rupper, s->old_rlower, wp->yoff);
|
2008-09-25 22:08:57 +02:00
|
|
|
|
2009-02-11 19:44:08 +01:00
|
|
|
tty_cursor(tty, s->old_cx, s->old_cy, wp->yoff);
|
2009-01-10 00:57:42 +01:00
|
|
|
tty_emulate_repeat(tty, TTYC_DL, TTYC_DL1, ua);
|
2008-09-25 22:08:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-02-11 08:02:34 +01:00
|
|
|
tty_cmd_clearline(struct tty *tty, struct window_pane *wp, unused va_list ap)
|
2008-09-25 22:08:57 +02:00
|
|
|
{
|
2009-02-11 08:02:34 +01:00
|
|
|
struct screen *s = wp->screen;
|
|
|
|
u_int i;
|
2008-09-25 22:08:57 +02:00
|
|
|
|
|
|
|
tty_reset(tty);
|
|
|
|
|
2009-02-11 19:44:08 +01:00
|
|
|
tty_cursor(tty, 0, s->old_cy, wp->yoff);
|
2009-01-10 00:57:42 +01:00
|
|
|
if (tty_term_has(tty->term, TTYC_EL)) {
|
|
|
|
tty_putcode(tty, TTYC_EL);
|
2008-09-25 22:08:57 +02:00
|
|
|
} else {
|
|
|
|
for (i = 0; i < screen_size_x(s); i++)
|
|
|
|
tty_putc(tty, ' ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-01-12 00:31:46 +01:00
|
|
|
tty_cmd_clearendofline(
|
2009-02-11 08:02:34 +01:00
|
|
|
struct tty *tty, struct window_pane *wp, unused va_list ap)
|
2008-09-25 22:08:57 +02:00
|
|
|
{
|
2009-02-11 08:02:34 +01:00
|
|
|
struct screen *s = wp->screen;
|
|
|
|
u_int i;
|
2008-09-26 08:45:28 +02:00
|
|
|
|
2008-09-25 22:08:57 +02:00
|
|
|
tty_reset(tty);
|
|
|
|
|
2009-02-11 19:44:08 +01:00
|
|
|
tty_cursor(tty, s->old_cx, s->old_cy, wp->yoff);
|
2009-01-10 00:57:42 +01:00
|
|
|
if (tty_term_has(tty->term, TTYC_EL))
|
|
|
|
tty_putcode(tty, TTYC_EL);
|
2009-01-10 02:51:22 +01:00
|
|
|
else {
|
2009-02-11 18:04:39 +01:00
|
|
|
for (i = s->old_cx; i < screen_size_x(s); i++)
|
2008-09-25 22:08:57 +02:00
|
|
|
tty_putc(tty, ' ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-01-12 00:31:46 +01:00
|
|
|
tty_cmd_clearstartofline(
|
2009-02-11 08:02:34 +01:00
|
|
|
struct tty *tty, struct window_pane *wp, unused va_list ap)
|
2008-09-25 22:08:57 +02:00
|
|
|
{
|
2009-02-11 08:02:34 +01:00
|
|
|
struct screen *s = wp->screen;
|
|
|
|
u_int i;
|
2008-09-25 22:08:57 +02:00
|
|
|
|
|
|
|
tty_reset(tty);
|
|
|
|
|
2009-02-11 19:44:08 +01:00
|
|
|
if (tty_term_has(tty->term, TTYC_EL1)) {
|
|
|
|
tty_cursor(tty, s->old_cx, s->old_cy, wp->yoff);
|
2009-01-10 00:57:42 +01:00
|
|
|
tty_putcode(tty, TTYC_EL1);
|
2009-02-11 19:44:08 +01:00
|
|
|
} else {
|
|
|
|
tty_cursor(tty, 0, s->old_cy, wp->yoff);
|
2009-02-11 18:04:39 +01:00
|
|
|
for (i = 0; i < s->old_cx + 1; i++)
|
2008-09-25 22:08:57 +02:00
|
|
|
tty_putc(tty, ' ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-02-11 08:02:34 +01:00
|
|
|
tty_cmd_reverseindex(struct tty *tty, struct window_pane *wp, unused va_list ap)
|
2008-09-25 22:08:57 +02:00
|
|
|
{
|
2009-02-11 08:02:34 +01:00
|
|
|
struct screen *s = wp->screen;
|
2009-02-11 20:06:58 +01:00
|
|
|
u_int i;
|
|
|
|
|
|
|
|
if (!tty_term_has(tty->term, TTYC_CSR)) {
|
|
|
|
/*
|
|
|
|
* Scroll region unsupported. If would have scrolled, redraw
|
|
|
|
* scroll region from already updated window screen.
|
|
|
|
*/
|
|
|
|
if (s->old_cy == s->old_rupper) {
|
|
|
|
for (i = s->old_rupper; i <= s->old_rlower; i++)
|
|
|
|
tty_draw_line(tty, wp, i);
|
|
|
|
}
|
2009-02-12 01:03:58 +01:00
|
|
|
return;
|
2009-02-11 20:06:58 +01:00
|
|
|
}
|
2009-02-11 08:02:34 +01:00
|
|
|
|
2008-09-25 22:08:57 +02:00
|
|
|
tty_reset(tty);
|
2009-02-11 19:44:08 +01:00
|
|
|
|
2009-02-11 18:04:39 +01:00
|
|
|
tty_region(tty, s->old_rupper, s->old_rlower, wp->yoff);
|
2009-01-10 00:57:42 +01:00
|
|
|
|
2009-02-12 01:03:58 +01:00
|
|
|
if (s->old_cy == s->old_rupper) {
|
2009-02-13 20:45:16 +01:00
|
|
|
tty_cursor(tty, s->old_cx, s->old_rupper, wp->yoff);
|
2009-02-12 01:03:58 +01:00
|
|
|
tty_putcode(tty, TTYC_RI);
|
|
|
|
}
|
2008-09-25 22:08:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-02-11 08:02:34 +01:00
|
|
|
tty_cmd_linefeed(struct tty *tty, struct window_pane *wp, unused va_list ap)
|
2008-09-25 22:08:57 +02:00
|
|
|
{
|
2009-02-11 08:02:34 +01:00
|
|
|
struct screen *s = wp->screen;
|
2009-02-11 20:06:58 +01:00
|
|
|
u_int i;
|
|
|
|
|
|
|
|
if (!tty_term_has(tty->term, TTYC_CSR)) {
|
|
|
|
/*
|
|
|
|
* Scroll region unsupported. If would have scrolled, redraw
|
|
|
|
* scroll region from already updated window screen.
|
|
|
|
*/
|
|
|
|
if (s->old_cy == s->old_rlower) {
|
|
|
|
for (i = s->old_rupper; i <= s->old_rlower; i++)
|
|
|
|
tty_draw_line(tty, wp, i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2009-02-11 08:02:34 +01:00
|
|
|
|
2008-09-25 22:08:57 +02:00
|
|
|
tty_reset(tty);
|
2009-02-11 19:44:08 +01:00
|
|
|
|
2009-02-11 18:04:39 +01:00
|
|
|
tty_region(tty, s->old_rupper, s->old_rlower, wp->yoff);
|
2008-09-25 22:08:57 +02:00
|
|
|
|
2009-02-11 19:44:08 +01:00
|
|
|
tty_cursor(tty, s->old_cx, s->old_cy, wp->yoff);
|
2008-09-25 22:08:57 +02:00
|
|
|
tty_putc(tty, '\n');
|
2009-02-11 19:44:08 +01:00
|
|
|
|
2009-02-07 01:05:23 +01:00
|
|
|
tty->cy++;
|
2008-09-25 22:08:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-01-12 00:31:46 +01:00
|
|
|
tty_cmd_clearendofscreen(
|
2009-02-11 08:02:34 +01:00
|
|
|
struct tty *tty, struct window_pane *wp, unused va_list ap)
|
2008-09-25 22:08:57 +02:00
|
|
|
{
|
2009-02-11 08:02:34 +01:00
|
|
|
struct screen *s = wp->screen;
|
|
|
|
u_int i, j, oy;
|
|
|
|
|
|
|
|
oy = wp->yoff;
|
2008-09-25 22:08:57 +02:00
|
|
|
|
|
|
|
tty_reset(tty);
|
|
|
|
|
2009-02-11 19:44:08 +01:00
|
|
|
tty_cursor(tty, s->old_cx, s->old_cy, wp->yoff);
|
2009-02-13 18:09:55 +01:00
|
|
|
tty_region(tty, 0, screen_size_y(s) - 1, wp->yoff);
|
2009-02-11 19:44:08 +01:00
|
|
|
if (tty_term_has(tty->term, TTYC_EL)) {
|
|
|
|
tty_putcode(tty, TTYC_EL);
|
|
|
|
if (s->old_cy != screen_size_y(s) - 1) {
|
2009-02-13 17:53:14 +01:00
|
|
|
tty_cursor(tty, 0, s->old_cy + 1, wp->yoff);
|
|
|
|
for (i = s->old_cy + 1; i < screen_size_y(s); i++) {
|
|
|
|
tty_putcode(tty, TTYC_EL);
|
|
|
|
if (i == screen_size_y(s) - 1)
|
|
|
|
continue;
|
2009-01-10 00:57:42 +01:00
|
|
|
tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
|
2009-02-11 19:44:08 +01:00
|
|
|
tty->cy++;
|
|
|
|
}
|
2007-12-06 10:46:23 +01:00
|
|
|
}
|
2008-09-25 22:08:57 +02:00
|
|
|
} else {
|
2009-02-13 17:53:14 +01:00
|
|
|
for (i = s->old_cx; i < screen_size_x(s); i++)
|
2008-09-25 22:08:57 +02:00
|
|
|
tty_putc(tty, ' ');
|
2009-02-11 19:44:08 +01:00
|
|
|
for (j = s->old_cy; j < screen_size_y(s); j++) {
|
2008-09-25 22:08:57 +02:00
|
|
|
for (i = 0; i < screen_size_x(s); i++)
|
2007-12-06 10:46:23 +01:00
|
|
|
tty_putc(tty, ' ');
|
|
|
|
}
|
2008-09-25 22:08:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-01-12 00:31:46 +01:00
|
|
|
tty_cmd_clearstartofscreen(
|
2009-02-11 08:02:34 +01:00
|
|
|
struct tty *tty, struct window_pane *wp, unused va_list ap)
|
2008-09-25 22:08:57 +02:00
|
|
|
{
|
2009-02-11 08:02:34 +01:00
|
|
|
struct screen *s = wp->screen;
|
2009-02-11 19:44:08 +01:00
|
|
|
u_int i, j;
|
2008-09-25 22:08:57 +02:00
|
|
|
|
|
|
|
tty_reset(tty);
|
|
|
|
|
2009-02-11 19:44:08 +01:00
|
|
|
tty_cursor(tty, 0, 0, wp->yoff);
|
2009-02-13 18:09:55 +01:00
|
|
|
tty_region(tty, 0, screen_size_y(s) - 1, wp->yoff);
|
2009-01-10 00:57:42 +01:00
|
|
|
if (tty_term_has(tty->term, TTYC_EL)) {
|
2009-02-11 19:44:08 +01:00
|
|
|
for (i = 0; i < s->old_cy; i++) {
|
2009-01-10 00:57:42 +01:00
|
|
|
tty_putcode(tty, TTYC_EL);
|
|
|
|
tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
|
2009-02-11 19:44:08 +01:00
|
|
|
tty->cy++;
|
2009-01-10 00:57:42 +01:00
|
|
|
}
|
2008-09-25 22:08:57 +02:00
|
|
|
} else {
|
2009-02-11 19:44:08 +01:00
|
|
|
for (j = 0; j < s->old_cy; j++) {
|
2007-12-06 10:46:23 +01:00
|
|
|
for (i = 0; i < screen_size_x(s); i++)
|
|
|
|
tty_putc(tty, ' ');
|
2008-09-26 08:45:28 +02:00
|
|
|
}
|
2008-09-25 22:08:57 +02:00
|
|
|
}
|
2009-02-11 18:04:39 +01:00
|
|
|
for (i = 0; i < s->old_cx; i++)
|
2008-09-25 22:08:57 +02:00
|
|
|
tty_putc(tty, ' ');
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-01-12 00:31:46 +01:00
|
|
|
tty_cmd_clearscreen(
|
2009-02-11 08:02:34 +01:00
|
|
|
struct tty *tty, struct window_pane *wp, unused va_list ap)
|
2008-09-25 22:08:57 +02:00
|
|
|
{
|
2009-02-11 08:02:34 +01:00
|
|
|
struct screen *s = wp->screen;
|
2009-02-11 19:44:08 +01:00
|
|
|
u_int i, j;
|
2008-09-25 22:08:57 +02:00
|
|
|
|
|
|
|
tty_reset(tty);
|
|
|
|
|
2009-02-11 19:44:08 +01:00
|
|
|
tty_cursor(tty, 0, 0, wp->yoff);
|
2009-02-13 18:09:55 +01:00
|
|
|
tty_region(tty, 0, screen_size_y(s) - 1, wp->yoff);
|
2009-01-10 00:57:42 +01:00
|
|
|
if (tty_term_has(tty->term, TTYC_EL)) {
|
2008-09-25 22:08:57 +02:00
|
|
|
for (i = 0; i < screen_size_y(s); i++) {
|
2009-01-10 00:57:42 +01:00
|
|
|
tty_putcode(tty, TTYC_EL);
|
2009-02-11 19:44:08 +01:00
|
|
|
if (i != screen_size_y(s) - 1) {
|
2009-01-10 00:57:42 +01:00
|
|
|
tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
|
2009-02-11 19:44:08 +01:00
|
|
|
tty->cy++;
|
|
|
|
}
|
2007-12-03 11:47:27 +01:00
|
|
|
}
|
2008-09-25 22:08:57 +02:00
|
|
|
} else {
|
|
|
|
for (j = 0; j < screen_size_y(s); j++) {
|
|
|
|
for (i = 0; i < screen_size_x(s); i++)
|
2007-12-03 11:47:27 +01:00
|
|
|
tty_putc(tty, ' ');
|
|
|
|
}
|
2007-11-27 20:23:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-02-11 08:02:34 +01:00
|
|
|
tty_cmd_cell(struct tty *tty, struct window_pane *wp, va_list ap)
|
2009-02-11 07:50:16 +01:00
|
|
|
{
|
2009-02-11 18:04:39 +01:00
|
|
|
struct screen *s = wp->screen;
|
2009-02-11 07:50:16 +01:00
|
|
|
struct grid_cell *gc;
|
|
|
|
|
|
|
|
gc = va_arg(ap, struct grid_cell *);
|
|
|
|
|
2009-02-11 18:04:39 +01:00
|
|
|
tty_cursor(tty, s->old_cx, s->old_cy, wp->yoff);
|
|
|
|
|
|
|
|
tty_cell(tty, gc);
|
2009-02-11 07:50:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-02-11 18:04:39 +01:00
|
|
|
tty_cell(struct tty *tty, const struct grid_cell *gc)
|
2007-11-27 20:23:34 +01:00
|
|
|
{
|
2009-02-11 08:02:34 +01:00
|
|
|
u_int i, width;
|
|
|
|
u_char out[4];
|
2008-09-25 22:08:57 +02:00
|
|
|
|
2009-02-12 01:18:05 +01:00
|
|
|
/* Skip last character if terminal is stupid. */
|
2009-02-12 01:19:31 +01:00
|
|
|
if (tty->term->flags & TERM_EARLYWRAP &&
|
2009-02-12 01:18:05 +01:00
|
|
|
tty->cy == tty->sy - 1 && tty->cx == tty->sx - 1)
|
|
|
|
return;
|
|
|
|
|
2008-09-25 22:08:57 +02:00
|
|
|
/* If this is a padding character, do nothing. */
|
|
|
|
if (gc->flags & GRID_FLAG_PADDING)
|
2007-11-27 20:23:34 +01:00
|
|
|
return;
|
|
|
|
|
2008-09-25 22:08:57 +02:00
|
|
|
/* Set the attributes. */
|
|
|
|
tty_attributes(tty, gc);
|
|
|
|
|
2009-01-18 22:46:30 +01:00
|
|
|
/* If not UTF-8, write directly. */
|
|
|
|
if (!(gc->flags & GRID_FLAG_UTF8)) {
|
|
|
|
if (gc->data > 0xff || gc->data < 0x20 || gc->data == 0x7f)
|
|
|
|
return;
|
2008-09-25 22:08:57 +02:00
|
|
|
tty_putc(tty, gc->data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-01-18 22:46:30 +01:00
|
|
|
/* If the terminal doesn't support UTF-8, write underscores. */
|
2008-09-25 22:08:57 +02:00
|
|
|
if (!(tty->flags & TTY_UTF8)) {
|
|
|
|
width = utf8_width(gc->data);
|
|
|
|
while (width-- > 0)
|
|
|
|
tty_putc(tty, '_');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-01-18 22:46:30 +01:00
|
|
|
/* Otherwise, unpack UTF-8 and write it. */
|
2008-09-25 22:08:57 +02:00
|
|
|
utf8_split(gc->data, out);
|
|
|
|
for (i = 0; i < 4; i++) {
|
|
|
|
if (out[i] == 0xff)
|
|
|
|
break;
|
|
|
|
tty_putc(tty, out[i]);
|
2007-11-27 20:23:34 +01:00
|
|
|
}
|
2008-09-25 22:08:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tty_reset(struct tty *tty)
|
|
|
|
{
|
|
|
|
struct grid_cell *tc = &tty->cell;
|
|
|
|
|
|
|
|
tc->data = grid_default_cell.data;
|
|
|
|
if (memcmp(tc, &grid_default_cell, sizeof *tc) == 0)
|
|
|
|
return;
|
2008-09-26 08:45:28 +02:00
|
|
|
|
2009-01-10 00:57:42 +01:00
|
|
|
if (tty_term_has(tty->term, TTYC_RMACS) && tc->attr & GRID_ATTR_CHARSET)
|
|
|
|
tty_putcode(tty, TTYC_RMACS);
|
|
|
|
tty_putcode(tty, TTYC_SGR0);
|
2008-09-25 22:08:57 +02:00
|
|
|
memcpy(tc, &grid_default_cell, sizeof *tc);
|
|
|
|
}
|
|
|
|
|
2009-01-12 00:31:46 +01:00
|
|
|
void
|
2009-02-11 18:04:39 +01:00
|
|
|
tty_region(struct tty *tty, u_int rupper, u_int rlower, u_int oy)
|
2009-01-12 00:31:46 +01:00
|
|
|
{
|
2009-02-11 20:06:58 +01:00
|
|
|
if (!tty_term_has(tty->term, TTYC_CSR))
|
|
|
|
return;
|
2009-02-11 18:04:39 +01:00
|
|
|
if (tty->rlower != oy + rlower || tty->rupper != oy + rupper) {
|
|
|
|
tty->rlower = oy + rlower;
|
|
|
|
tty->rupper = oy + rupper;
|
2009-01-12 00:31:46 +01:00
|
|
|
tty->cx = 0;
|
2009-02-11 20:06:58 +01:00
|
|
|
tty->cy = 0;
|
2009-01-12 00:31:46 +01:00
|
|
|
tty_putcode2(tty, TTYC_CSR, tty->rupper, tty->rlower);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tty_cursor(struct tty *tty, u_int cx, u_int cy, u_int oy)
|
|
|
|
{
|
2009-01-18 22:35:09 +01:00
|
|
|
if (cx == 0 && tty->cx != 0 && tty->cy == oy + cy) {
|
|
|
|
tty->cx = 0;
|
|
|
|
tty_putc(tty, '\r');
|
|
|
|
} else if (tty->cx != cx || tty->cy != oy + cy) {
|
2009-01-12 00:31:46 +01:00
|
|
|
tty->cx = cx;
|
|
|
|
tty->cy = oy + cy;
|
|
|
|
tty_putcode2(tty, TTYC_CUP, tty->cy, tty->cx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-25 22:08:57 +02:00
|
|
|
void
|
|
|
|
tty_attributes(struct tty *tty, const struct grid_cell *gc)
|
|
|
|
{
|
|
|
|
struct grid_cell *tc = &tty->cell;
|
|
|
|
u_char changed;
|
2009-01-19 20:01:11 +01:00
|
|
|
u_int fg, bg;
|
2008-09-25 22:08:57 +02:00
|
|
|
|
|
|
|
/* If any bits are being cleared, reset everything. */
|
|
|
|
if (tc->attr & ~gc->attr)
|
|
|
|
tty_reset(tty);
|
2007-11-27 20:23:34 +01:00
|
|
|
|
2008-09-09 00:03:56 +02:00
|
|
|
/* Filter out attribute bits already set. */
|
2008-09-25 22:08:57 +02:00
|
|
|
changed = gc->attr & ~tc->attr;
|
|
|
|
tc->attr = gc->attr;
|
2007-11-27 20:23:34 +01:00
|
|
|
|
2008-09-09 00:03:56 +02:00
|
|
|
/* Set the attributes. */
|
2009-01-19 20:01:11 +01:00
|
|
|
fg = gc->fg;
|
|
|
|
bg = gc->bg;
|
2009-01-10 00:57:42 +01:00
|
|
|
if (changed & GRID_ATTR_BRIGHT)
|
|
|
|
tty_putcode(tty, TTYC_BOLD);
|
|
|
|
if (changed & GRID_ATTR_DIM)
|
|
|
|
tty_putcode(tty, TTYC_DIM);
|
|
|
|
if (changed & GRID_ATTR_ITALICS)
|
|
|
|
tty_putcode(tty, TTYC_SMSO);
|
|
|
|
if (changed & GRID_ATTR_UNDERSCORE)
|
|
|
|
tty_putcode(tty, TTYC_SMUL);
|
|
|
|
if (changed & GRID_ATTR_BLINK)
|
|
|
|
tty_putcode(tty, TTYC_BLINK);
|
2009-01-19 20:01:11 +01:00
|
|
|
if (changed & GRID_ATTR_REVERSE) {
|
|
|
|
if (tty_term_has(tty->term, TTYC_REV))
|
|
|
|
tty_putcode(tty, TTYC_REV);
|
|
|
|
else if (tty_term_has(tty->term, TTYC_SMSO))
|
|
|
|
tty_putcode(tty, TTYC_SMSO);
|
|
|
|
}
|
2009-01-10 00:57:42 +01:00
|
|
|
if (changed & GRID_ATTR_HIDDEN)
|
|
|
|
tty_putcode(tty, TTYC_INVIS);
|
|
|
|
if (changed & GRID_ATTR_CHARSET)
|
|
|
|
tty_putcode(tty, TTYC_SMACS);
|
2007-11-27 20:23:34 +01:00
|
|
|
|
2008-09-09 00:03:56 +02:00
|
|
|
/* Set foreground colour. */
|
2009-01-19 20:01:11 +01:00
|
|
|
if (fg != tc->fg ||
|
2008-09-25 22:08:57 +02:00
|
|
|
(gc->flags & GRID_FLAG_FG256) != (tc->flags & GRID_FLAG_FG256)) {
|
|
|
|
tty_attributes_fg(tty, gc);
|
2009-01-19 20:01:11 +01:00
|
|
|
tc->fg = fg;
|
2008-09-25 22:08:57 +02:00
|
|
|
}
|
2008-09-26 08:45:28 +02:00
|
|
|
|
2008-09-09 00:03:56 +02:00
|
|
|
/* Set background colour. */
|
2009-01-19 20:01:11 +01:00
|
|
|
if (bg != tc->bg ||
|
2008-09-25 22:08:57 +02:00
|
|
|
(gc->flags & GRID_FLAG_BG256) != (tc->flags & GRID_FLAG_BG256)) {
|
|
|
|
tty_attributes_bg(tty, gc);
|
2009-01-19 20:01:11 +01:00
|
|
|
tc->bg = bg;
|
2008-09-25 22:08:57 +02:00
|
|
|
}
|
2008-09-09 00:03:56 +02:00
|
|
|
}
|
|
|
|
|
2009-03-07 11:29:06 +01:00
|
|
|
int
|
|
|
|
tty_try_256(struct tty *tty, u_char colour, const char *type)
|
|
|
|
{
|
|
|
|
char s[32];
|
|
|
|
|
|
|
|
if (!(tty->term->flags & TERM_256COLOURS) &&
|
|
|
|
!(tty->term_flags & TERM_256COLOURS))
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
xsnprintf(s, sizeof s, "\033[%s;5;%hhum", type, colour);
|
|
|
|
tty_puts(tty, s);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
tty_try_88(struct tty *tty, u_char colour, const char *type)
|
|
|
|
{
|
|
|
|
char s[32];
|
|
|
|
|
|
|
|
if (!(tty->term->flags & TERM_88COLOURS) &&
|
|
|
|
!(tty->term_flags & TERM_88COLOURS))
|
|
|
|
return (-1);
|
|
|
|
colour = colour_256to88(colour);
|
|
|
|
|
|
|
|
xsnprintf(s, sizeof s, "\033[%s;5;%hhum", type, colour);
|
|
|
|
tty_puts(tty, s);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2008-09-25 22:08:57 +02:00
|
|
|
void
|
|
|
|
tty_attributes_fg(struct tty *tty, const struct grid_cell *gc)
|
2008-09-09 00:03:56 +02:00
|
|
|
{
|
2009-03-07 11:29:06 +01:00
|
|
|
u_char fg;
|
2008-09-09 00:03:56 +02:00
|
|
|
|
2009-03-07 11:29:06 +01:00
|
|
|
fg = gc->fg;
|
2008-09-25 22:08:57 +02:00
|
|
|
if (gc->flags & GRID_FLAG_FG256) {
|
2009-03-07 11:29:06 +01:00
|
|
|
if (tty_try_256(tty, fg, "38") == 0)
|
2008-09-25 22:08:57 +02:00
|
|
|
return;
|
2009-03-07 11:29:06 +01:00
|
|
|
if (tty_try_88(tty, fg, "38") == 0)
|
|
|
|
return;
|
|
|
|
fg = colour_256to16(fg);
|
2008-10-10 00:00:33 +02:00
|
|
|
if (fg & 8) {
|
|
|
|
fg &= 7;
|
2009-01-10 00:57:42 +01:00
|
|
|
tty_putcode(tty, TTYC_BOLD);
|
2008-10-10 00:00:33 +02:00
|
|
|
tty->cell.attr |= GRID_ATTR_BRIGHT;
|
|
|
|
} else if (tty->cell.attr & GRID_ATTR_BRIGHT)
|
|
|
|
tty_reset(tty);
|
2007-11-27 20:23:34 +01:00
|
|
|
}
|
2009-03-07 11:29:06 +01:00
|
|
|
|
2008-09-25 22:08:57 +02:00
|
|
|
if (fg == 8 &&
|
|
|
|
!(tty->term->flags & TERM_HASDEFAULTS) &&
|
|
|
|
!(tty->term_flags & TERM_HASDEFAULTS))
|
2008-09-09 00:03:56 +02:00
|
|
|
fg = 7;
|
|
|
|
if (fg == 8)
|
|
|
|
tty_puts(tty, "\033[39m");
|
2009-01-10 00:57:42 +01:00
|
|
|
else
|
|
|
|
tty_putcode1(tty, TTYC_SETAF, fg);
|
2008-09-09 00:03:56 +02:00
|
|
|
}
|
|
|
|
|
2008-09-25 22:08:57 +02:00
|
|
|
void
|
|
|
|
tty_attributes_bg(struct tty *tty, const struct grid_cell *gc)
|
2008-09-09 00:03:56 +02:00
|
|
|
{
|
2009-03-07 11:29:06 +01:00
|
|
|
u_char bg;
|
2007-12-06 10:46:23 +01:00
|
|
|
|
2009-03-07 11:29:06 +01:00
|
|
|
bg = gc->bg;
|
2008-09-25 22:08:57 +02:00
|
|
|
if (gc->flags & GRID_FLAG_BG256) {
|
2009-03-07 11:29:06 +01:00
|
|
|
if (tty_try_256(tty, bg, "48") == 0)
|
2008-09-25 22:08:57 +02:00
|
|
|
return;
|
2009-03-07 11:29:06 +01:00
|
|
|
if (tty_try_88(tty, bg, "48") == 0)
|
2009-01-10 02:51:22 +01:00
|
|
|
return;
|
2009-03-07 11:29:06 +01:00
|
|
|
bg = colour_256to16(bg);
|
|
|
|
if (bg & 8)
|
2008-10-10 00:03:36 +02:00
|
|
|
bg &= 7;
|
2008-09-09 00:03:56 +02:00
|
|
|
}
|
2008-09-26 08:45:28 +02:00
|
|
|
|
2008-09-25 22:08:57 +02:00
|
|
|
if (bg == 8 &&
|
|
|
|
!(tty->term->flags & TERM_HASDEFAULTS) &&
|
|
|
|
!(tty->term_flags & TERM_HASDEFAULTS))
|
2008-09-09 00:03:56 +02:00
|
|
|
bg = 0;
|
|
|
|
if (bg == 8)
|
|
|
|
tty_puts(tty, "\033[49m");
|
2009-01-10 02:51:22 +01:00
|
|
|
else
|
2009-01-10 00:57:42 +01:00
|
|
|
tty_putcode1(tty, TTYC_SETAB, bg);
|
2007-11-27 20:23:34 +01:00
|
|
|
}
|