2009-06-01 22:58:49 +00:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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>
|
2009-10-12 13:01:18 +00:00
|
|
|
#include <stdlib.h>
|
2009-06-01 22:58:49 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
|
|
|
void tty_fill_acs(struct tty *);
|
|
|
|
|
|
|
|
int tty_try_256(struct tty *, u_char, const char *);
|
|
|
|
int tty_try_88(struct tty *, u_char, const char *);
|
|
|
|
|
|
|
|
void tty_attributes_fg(struct tty *, const struct grid_cell *);
|
|
|
|
void tty_attributes_bg(struct tty *, const struct grid_cell *);
|
|
|
|
|
2009-08-12 09:41:59 +00:00
|
|
|
void tty_redraw_region(struct tty *, const struct tty_ctx *);
|
2009-07-22 18:02:23 +00:00
|
|
|
void tty_emulate_repeat(
|
|
|
|
struct tty *, enum tty_code_code, enum tty_code_code, u_int);
|
2009-07-22 20:53:38 +00:00
|
|
|
void tty_cell(struct tty *,
|
|
|
|
const struct grid_cell *, const struct grid_utf8 *);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
void
|
2009-09-23 06:05:02 +00:00
|
|
|
tty_init(struct tty *tty, int fd, char *term)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2009-09-23 06:05:02 +00:00
|
|
|
char *path;
|
|
|
|
|
|
|
|
memset(tty, 0, sizeof *tty);
|
2009-08-11 22:34:17 +00:00
|
|
|
tty->log_fd = -1;
|
2009-08-11 21:28:11 +00:00
|
|
|
|
2009-07-07 17:24:32 +00:00
|
|
|
if (term == NULL || *term == '\0')
|
2009-06-01 22:58:49 +00:00
|
|
|
tty->termname = xstrdup("unknown");
|
|
|
|
else
|
|
|
|
tty->termname = xstrdup(term);
|
2009-09-23 06:05:02 +00:00
|
|
|
|
|
|
|
if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
|
|
|
|
fatal("fcntl failed");
|
|
|
|
tty->fd = fd;
|
|
|
|
|
|
|
|
if ((path = ttyname(fd)) == NULL)
|
|
|
|
fatalx("ttyname failed");
|
|
|
|
tty->path = xstrdup(path);
|
|
|
|
|
2009-06-01 22:58:49 +00:00
|
|
|
tty->flags = 0;
|
|
|
|
tty->term_flags = 0;
|
|
|
|
}
|
|
|
|
|
2009-09-23 06:12:58 +00:00
|
|
|
void
|
|
|
|
tty_resize(struct tty *tty)
|
|
|
|
{
|
|
|
|
struct winsize ws;
|
|
|
|
|
|
|
|
if (ioctl(tty->fd, TIOCGWINSZ, &ws) != -1) {
|
|
|
|
tty->sx = ws.ws_col;
|
|
|
|
tty->sy = ws.ws_row;
|
|
|
|
}
|
|
|
|
if (tty->sx == 0)
|
|
|
|
tty->sx = 80;
|
|
|
|
if (tty->sy == 0)
|
|
|
|
tty->sy = 24;
|
|
|
|
|
|
|
|
tty->cx = UINT_MAX;
|
|
|
|
tty->cy = UINT_MAX;
|
|
|
|
|
|
|
|
tty->rupper = UINT_MAX;
|
|
|
|
tty->rlower = UINT_MAX;
|
|
|
|
}
|
|
|
|
|
2009-06-01 22:58:49 +00:00
|
|
|
int
|
2009-08-03 14:10:54 +00:00
|
|
|
tty_open(struct tty *tty, const char *overrides, char **cause)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2009-09-23 06:05:02 +00:00
|
|
|
int fd;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2009-09-23 06:05:02 +00:00
|
|
|
if (debug_level > 3) {
|
|
|
|
fd = open("tmux.out", O_WRONLY|O_CREAT|O_TRUNC, 0644);
|
|
|
|
if (fd != -1 && fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
|
|
|
|
fatal("fcntl failed");
|
|
|
|
tty->log_fd = fd;
|
|
|
|
}
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2009-08-03 14:10:54 +00:00
|
|
|
tty->term = tty_term_find(tty->termname, tty->fd, overrides, cause);
|
2009-08-11 20:29:04 +00:00
|
|
|
if (tty->term == NULL) {
|
|
|
|
tty_close(tty);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
tty->flags |= TTY_OPENED;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
tty->in = buffer_create(BUFSIZ);
|
|
|
|
tty->out = buffer_create(BUFSIZ);
|
|
|
|
|
2009-09-22 19:11:52 +00:00
|
|
|
tty->flags &= ~(TTY_NOCURSOR|TTY_FREEZE|TTY_ESCAPE);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
tty_start_tty(tty);
|
|
|
|
|
|
|
|
tty_keys_init(tty);
|
|
|
|
|
|
|
|
tty_fill_acs(tty);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tty_start_tty(struct tty *tty)
|
|
|
|
{
|
|
|
|
struct termios tio;
|
2009-09-23 12:03:30 +00:00
|
|
|
int what, mode;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2009-09-23 07:25:31 +00:00
|
|
|
if (tty->fd == -1)
|
|
|
|
return;
|
|
|
|
|
2009-09-23 12:03:30 +00:00
|
|
|
if ((mode = fcntl(tty->fd, F_GETFL)) == -1)
|
|
|
|
fatal("fcntl failed");
|
|
|
|
if (fcntl(tty->fd, F_SETFL, mode|O_NONBLOCK) == -1)
|
|
|
|
fatal("fcntl failed");
|
|
|
|
|
2009-06-01 22:58:49 +00:00
|
|
|
if (tcgetattr(tty->fd, &tty->tio) != 0)
|
|
|
|
fatal("tcgetattr failed");
|
|
|
|
memcpy(&tio, &tty->tio, sizeof tio);
|
|
|
|
tio.c_iflag &= ~(IXON|IXOFF|ICRNL|INLCR|IGNCR|IMAXBEL|ISTRIP);
|
|
|
|
tio.c_iflag |= IGNBRK;
|
|
|
|
tio.c_oflag &= ~(OPOST|ONLCR|OCRNL|ONLRET);
|
|
|
|
tio.c_lflag &= ~(IEXTEN|ICANON|ECHO|ECHOE|ECHONL|ECHOCTL|
|
|
|
|
ECHOPRT|ECHOKE|ECHOCTL|ISIG);
|
|
|
|
tio.c_cc[VMIN] = 1;
|
|
|
|
tio.c_cc[VTIME] = 0;
|
|
|
|
if (tcsetattr(tty->fd, TCSANOW, &tio) != 0)
|
|
|
|
fatal("tcsetattr failed");
|
|
|
|
|
|
|
|
what = 0;
|
|
|
|
if (ioctl(tty->fd, TIOCFLUSH, &what) != 0)
|
|
|
|
fatal("ioctl(TIOCFLUSH)");
|
|
|
|
|
|
|
|
tty_putcode(tty, TTYC_SMCUP);
|
2009-07-10 07:11:59 +00:00
|
|
|
|
2009-08-14 08:53:52 +00:00
|
|
|
tty_putcode(tty, TTYC_SGR0);
|
|
|
|
memcpy(&tty->cell, &grid_default_cell, sizeof tty->cell);
|
|
|
|
|
2009-06-01 22:58:49 +00:00
|
|
|
tty_putcode(tty, TTYC_SMKX);
|
|
|
|
tty_putcode(tty, TTYC_ENACS);
|
|
|
|
tty_putcode(tty, TTYC_CLEAR);
|
|
|
|
|
|
|
|
tty_putcode(tty, TTYC_CNORM);
|
|
|
|
if (tty_term_has(tty->term, TTYC_KMOUS))
|
|
|
|
tty_puts(tty, "\033[?1000l");
|
|
|
|
|
|
|
|
tty->cx = UINT_MAX;
|
|
|
|
tty->cy = UINT_MAX;
|
|
|
|
|
|
|
|
tty->rlower = UINT_MAX;
|
|
|
|
tty->rupper = UINT_MAX;
|
|
|
|
|
|
|
|
tty->mode = MODE_CURSOR;
|
2009-08-11 19:32:25 +00:00
|
|
|
|
|
|
|
tty->flags |= TTY_STARTED;
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tty_stop_tty(struct tty *tty)
|
|
|
|
{
|
|
|
|
struct winsize ws;
|
2009-09-23 12:03:30 +00:00
|
|
|
int mode;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2009-08-11 19:32:25 +00:00
|
|
|
if (!(tty->flags & TTY_STARTED))
|
|
|
|
return;
|
|
|
|
tty->flags &= ~TTY_STARTED;
|
|
|
|
|
2009-06-01 22:58:49 +00: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-09-23 12:03:30 +00:00
|
|
|
if ((mode = fcntl(tty->fd, F_GETFL)) == -1)
|
|
|
|
return;
|
|
|
|
if (fcntl(tty->fd, F_SETFL, mode & ~O_NONBLOCK) == -1)
|
|
|
|
return;
|
2009-06-01 22:58:49 +00: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_RMKX));
|
|
|
|
tty_raw(tty, tty_term_string(tty->term, TTYC_CLEAR));
|
|
|
|
|
|
|
|
tty_raw(tty, tty_term_string(tty->term, TTYC_CNORM));
|
|
|
|
if (tty_term_has(tty->term, TTYC_KMOUS))
|
|
|
|
tty_raw(tty, "\033[?1000l");
|
2009-07-10 07:11:59 +00:00
|
|
|
|
|
|
|
tty_raw(tty, tty_term_string(tty->term, TTYC_RMCUP));
|
2009-06-01 22:58:49 +00: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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-08-11 19:32:25 +00:00
|
|
|
tty_close(struct tty *tty)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
|
|
|
if (tty->log_fd != -1) {
|
|
|
|
close(tty->log_fd);
|
|
|
|
tty->log_fd = -1;
|
|
|
|
}
|
|
|
|
|
2009-08-11 19:32:25 +00:00
|
|
|
tty_stop_tty(tty);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2009-08-11 20:29:04 +00:00
|
|
|
if (tty->flags & TTY_OPENED) {
|
|
|
|
tty_term_free(tty->term);
|
|
|
|
tty_keys_free(tty);
|
|
|
|
|
|
|
|
buffer_destroy(tty->in);
|
|
|
|
buffer_destroy(tty->out);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2009-08-11 20:29:04 +00:00
|
|
|
tty->flags &= ~TTY_OPENED;
|
|
|
|
}
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2009-08-11 20:29:04 +00:00
|
|
|
if (tty->fd != -1) {
|
|
|
|
close(tty->fd);
|
|
|
|
tty->fd = -1;
|
|
|
|
}
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-08-11 19:32:25 +00:00
|
|
|
tty_free(struct tty *tty)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2009-08-11 19:32:25 +00:00
|
|
|
tty_close(tty);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
if (tty->path != NULL)
|
|
|
|
xfree(tty->path);
|
|
|
|
if (tty->termname != NULL)
|
|
|
|
xfree(tty->termname);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tty_raw(struct tty *tty, const char *s)
|
|
|
|
{
|
|
|
|
write(tty->fd, s, strlen(s));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tty_putcode(struct tty *tty, enum tty_code_code code)
|
|
|
|
{
|
|
|
|
tty_puts(tty, tty_term_string(tty->term, code));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tty_putcode1(struct tty *tty, enum tty_code_code code, int a)
|
|
|
|
{
|
|
|
|
if (a < 0)
|
|
|
|
return;
|
|
|
|
tty_puts(tty, tty_term_string1(tty->term, code, a));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tty_putcode2(struct tty *tty, enum tty_code_code code, int a, int b)
|
|
|
|
{
|
|
|
|
if (a < 0 || b < 0)
|
|
|
|
return;
|
|
|
|
tty_puts(tty, tty_term_string2(tty->term, code, a, b));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tty_puts(struct tty *tty, const char *s)
|
|
|
|
{
|
|
|
|
if (*s == '\0')
|
|
|
|
return;
|
|
|
|
buffer_write(tty->out, s, strlen(s));
|
|
|
|
|
|
|
|
if (tty->log_fd != -1)
|
|
|
|
write(tty->log_fd, s, strlen(s));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tty_putc(struct tty *tty, u_char ch)
|
|
|
|
{
|
|
|
|
u_int sx;
|
|
|
|
|
|
|
|
if (tty->cell.attr & GRID_ATTR_CHARSET)
|
|
|
|
ch = tty_get_acs(tty, ch);
|
|
|
|
buffer_write8(tty->out, ch);
|
|
|
|
|
|
|
|
if (ch >= 0x20 && ch != 0x7f) {
|
|
|
|
sx = tty->sx;
|
|
|
|
if (tty->term->flags & TERM_EARLYWRAP)
|
|
|
|
sx--;
|
|
|
|
|
|
|
|
if (tty->cx == sx) {
|
|
|
|
tty->cx = 0;
|
|
|
|
tty->cy++;
|
|
|
|
} else
|
|
|
|
tty->cx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tty->log_fd != -1)
|
|
|
|
write(tty->log_fd, &ch, 1);
|
|
|
|
}
|
|
|
|
|
2009-06-24 16:01:02 +00:00
|
|
|
void
|
|
|
|
tty_pututf8(struct tty *tty, const struct grid_utf8 *gu)
|
|
|
|
{
|
|
|
|
u_int i, width;
|
|
|
|
|
|
|
|
for (i = 0; i < UTF8_SIZE; i++) {
|
|
|
|
if (gu->data[i] == 0xff)
|
|
|
|
break;
|
|
|
|
buffer_write8(tty->out, gu->data[i]);
|
|
|
|
if (tty->log_fd != -1)
|
|
|
|
write(tty->log_fd, &gu->data[i], 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
width = utf8_width(gu->data);
|
|
|
|
tty->cx += width;
|
|
|
|
}
|
|
|
|
|
2009-06-01 22:58:49 +00:00
|
|
|
void
|
|
|
|
tty_set_title(struct tty *tty, const char *title)
|
|
|
|
{
|
|
|
|
if (strstr(tty->termname, "xterm") == NULL &&
|
|
|
|
strstr(tty->termname, "rxvt") == NULL &&
|
|
|
|
strcmp(tty->termname, "screen") != 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
tty_puts(tty, "\033]0;");
|
|
|
|
tty_puts(tty, title);
|
|
|
|
tty_putc(tty, '\007');
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Redraw scroll region using data from screen (already updated). Used when
|
|
|
|
* CSR not supported, or window is a pane that doesn't take up the full
|
|
|
|
* width of the terminal.
|
|
|
|
*/
|
|
|
|
void
|
2009-08-12 09:41:59 +00:00
|
|
|
tty_redraw_region(struct tty *tty, const struct tty_ctx *ctx)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2009-07-22 20:53:38 +00:00
|
|
|
struct window_pane *wp = ctx->wp;
|
|
|
|
struct screen *s = wp->screen;
|
|
|
|
u_int i;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If region is >= 50% of the screen, just schedule a window redraw. In
|
|
|
|
* most cases, this is likely to be followed by some more scrolling -
|
|
|
|
* without this, the entire pane ends up being redrawn many times which
|
|
|
|
* can be much more data.
|
|
|
|
*/
|
2009-07-22 20:53:38 +00:00
|
|
|
if (ctx->orupper - ctx->orlower >= screen_size_y(s) / 2) {
|
2009-06-01 22:58:49 +00:00
|
|
|
wp->flags |= PANE_REDRAW;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-07-22 20:53:38 +00:00
|
|
|
if (ctx->ocy < ctx->orupper || ctx->ocy > ctx->orlower) {
|
|
|
|
for (i = ctx->ocy; i < screen_size_y(s); i++)
|
2009-06-01 22:58:49 +00:00
|
|
|
tty_draw_line(tty, s, i, wp->xoff, wp->yoff);
|
|
|
|
} else {
|
2009-07-22 20:53:38 +00:00
|
|
|
for (i = ctx->orupper; i <= ctx->orlower; i++)
|
2009-06-01 22:58:49 +00:00
|
|
|
tty_draw_line(tty, s, i, wp->xoff, wp->yoff);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tty_draw_line(struct tty *tty, struct screen *s, u_int py, u_int ox, u_int oy)
|
|
|
|
{
|
|
|
|
const struct grid_cell *gc;
|
2009-07-27 11:33:21 +00:00
|
|
|
struct grid_cell tmpgc;
|
2009-06-01 22:58:49 +00:00
|
|
|
const struct grid_utf8 *gu;
|
|
|
|
u_int i, sx;
|
|
|
|
|
2009-10-09 07:23:28 +00:00
|
|
|
tty_update_mode(tty, tty->mode & ~MODE_CURSOR);
|
|
|
|
|
2009-06-01 22:58:49 +00:00
|
|
|
sx = screen_size_x(s);
|
2009-08-08 13:29:27 +00:00
|
|
|
if (sx > s->grid->linedata[s->grid->hsize + py].cellsize)
|
|
|
|
sx = s->grid->linedata[s->grid->hsize + py].cellsize;
|
2009-06-01 22:58:49 +00:00
|
|
|
if (sx > tty->sx)
|
|
|
|
sx = tty->sx;
|
|
|
|
|
2009-10-12 09:29:58 +00:00
|
|
|
tty_cursor(tty, ox, oy + py);
|
2009-06-01 22:58:49 +00:00
|
|
|
for (i = 0; i < sx; i++) {
|
|
|
|
gc = grid_view_peek_cell(s->grid, i, py);
|
|
|
|
|
|
|
|
gu = NULL;
|
|
|
|
if (gc->flags & GRID_FLAG_UTF8)
|
|
|
|
gu = grid_view_peek_utf8(s->grid, i, py);
|
|
|
|
|
|
|
|
if (screen_check_selection(s, i, py)) {
|
2009-07-27 11:33:21 +00:00
|
|
|
memcpy(&tmpgc, &s->sel.cell, sizeof tmpgc);
|
|
|
|
tmpgc.data = gc->data;
|
2009-10-11 22:35:10 +00:00
|
|
|
tmpgc.flags = gc->flags &
|
|
|
|
~(GRID_FLAG_FG256|GRID_FLAG_BG256);
|
|
|
|
tmpgc.flags |= s->sel.cell.flags &
|
|
|
|
(GRID_FLAG_FG256|GRID_FLAG_BG256);
|
2009-07-27 11:33:21 +00:00
|
|
|
tty_cell(tty, &tmpgc, gu);
|
2009-06-01 22:58:49 +00:00
|
|
|
} else
|
|
|
|
tty_cell(tty, gc, gu);
|
|
|
|
}
|
|
|
|
|
2009-10-09 07:23:28 +00:00
|
|
|
if (sx >= tty->sx) {
|
|
|
|
tty_update_mode(tty, tty->mode);
|
2009-06-01 22:58:49 +00:00
|
|
|
return;
|
2009-10-09 07:23:28 +00:00
|
|
|
}
|
2009-06-01 22:58:49 +00:00
|
|
|
tty_reset(tty);
|
|
|
|
|
2009-10-12 09:29:58 +00:00
|
|
|
tty_cursor(tty, ox + sx, oy + py);
|
2009-06-01 22:58:49 +00:00
|
|
|
if (screen_size_x(s) >= tty->sx && tty_term_has(tty->term, TTYC_EL))
|
|
|
|
tty_putcode(tty, TTYC_EL);
|
|
|
|
else {
|
|
|
|
for (i = sx; i < screen_size_x(s); i++)
|
|
|
|
tty_putc(tty, ' ');
|
|
|
|
}
|
2009-10-09 07:23:28 +00:00
|
|
|
tty_update_mode(tty, tty->mode);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2009-07-22 20:56:58 +00:00
|
|
|
void
|
2009-08-12 09:41:59 +00:00
|
|
|
tty_write(void (*cmdfn)(
|
|
|
|
struct tty *, const struct tty_ctx *), const struct tty_ctx *ctx)
|
2009-07-22 20:56:58 +00:00
|
|
|
{
|
|
|
|
struct window_pane *wp = ctx->wp;
|
|
|
|
struct client *c;
|
|
|
|
u_int i;
|
|
|
|
|
|
|
|
if (wp == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (wp->window->flags & WINDOW_REDRAW || wp->flags & PANE_REDRAW)
|
|
|
|
return;
|
|
|
|
if (wp->window->flags & WINDOW_HIDDEN || !window_pane_visible(wp))
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
|
|
|
|
c = ARRAY_ITEM(&clients, i);
|
|
|
|
if (c == NULL || c->session == NULL)
|
|
|
|
continue;
|
|
|
|
if (c->flags & CLIENT_SUSPENDED)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (c->session->curw->window == wp->window) {
|
|
|
|
if (c->tty.flags & TTY_FREEZE || c->tty.term == NULL)
|
|
|
|
continue;
|
|
|
|
cmdfn(&c->tty, ctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-01 22:58:49 +00:00
|
|
|
void
|
2009-08-12 09:41:59 +00:00
|
|
|
tty_cmd_insertcharacter(struct tty *tty, const struct tty_ctx *ctx)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2009-07-22 16:45:31 +00:00
|
|
|
struct window_pane *wp = ctx->wp;
|
|
|
|
struct screen *s = wp->screen;
|
2009-08-12 09:41:59 +00:00
|
|
|
u_int i;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
if (wp->xoff != 0 || screen_size_x(s) < tty->sx) {
|
2009-07-22 20:53:38 +00:00
|
|
|
tty_draw_line(tty, wp->screen, ctx->ocy, wp->xoff, wp->yoff);
|
2009-06-01 22:58:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tty_reset(tty);
|
|
|
|
|
2009-10-12 09:29:58 +00:00
|
|
|
tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
|
|
|
|
|
2009-06-01 22:58:49 +00:00
|
|
|
if (tty_term_has(tty->term, TTYC_ICH) ||
|
|
|
|
tty_term_has(tty->term, TTYC_ICH1))
|
2009-07-22 16:45:31 +00:00
|
|
|
tty_emulate_repeat(tty, TTYC_ICH, TTYC_ICH1, ctx->num);
|
2009-06-01 22:58:49 +00:00
|
|
|
else {
|
|
|
|
tty_putcode(tty, TTYC_SMIR);
|
2009-08-12 09:41:59 +00:00
|
|
|
for (i = 0; i < ctx->num; i++)
|
2009-06-01 22:58:49 +00:00
|
|
|
tty_putc(tty, ' ');
|
|
|
|
tty_putcode(tty, TTYC_RMIR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-08-12 09:41:59 +00:00
|
|
|
tty_cmd_deletecharacter(struct tty *tty, const struct tty_ctx *ctx)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2009-07-22 16:45:31 +00:00
|
|
|
struct window_pane *wp = ctx->wp;
|
|
|
|
struct screen *s = wp->screen;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2009-08-21 12:29:59 +00:00
|
|
|
if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
|
|
|
|
(!tty_term_has(tty->term, TTYC_DCH) &&
|
|
|
|
!tty_term_has(tty->term, TTYC_DCH1))) {
|
2009-07-22 20:53:38 +00:00
|
|
|
tty_draw_line(tty, wp->screen, ctx->ocy, wp->xoff, wp->yoff);
|
2009-06-01 22:58:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tty_reset(tty);
|
|
|
|
|
2009-10-12 09:29:58 +00:00
|
|
|
tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
|
|
|
|
|
2009-08-21 12:29:59 +00:00
|
|
|
if (tty_term_has(tty->term, TTYC_DCH) ||
|
|
|
|
tty_term_has(tty->term, TTYC_DCH1))
|
|
|
|
tty_emulate_repeat(tty, TTYC_DCH, TTYC_DCH1, ctx->num);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-08-12 09:41:59 +00:00
|
|
|
tty_cmd_insertline(struct tty *tty, const struct tty_ctx *ctx)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2009-07-22 16:45:31 +00:00
|
|
|
struct window_pane *wp = ctx->wp;
|
|
|
|
struct screen *s = wp->screen;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
|
|
|
|
!tty_term_has(tty->term, TTYC_CSR)) {
|
2009-07-22 20:53:38 +00:00
|
|
|
tty_redraw_region(tty, ctx);
|
2009-06-01 22:58:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tty_reset(tty);
|
|
|
|
|
2009-10-12 09:09:35 +00:00
|
|
|
tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
|
2009-10-12 09:29:58 +00:00
|
|
|
tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2009-07-22 16:45:31 +00:00
|
|
|
tty_emulate_repeat(tty, TTYC_IL, TTYC_IL1, ctx->num);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-08-12 09:41:59 +00:00
|
|
|
tty_cmd_deleteline(struct tty *tty, const struct tty_ctx *ctx)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2009-07-22 16:45:31 +00:00
|
|
|
struct window_pane *wp = ctx->wp;
|
|
|
|
struct screen *s = wp->screen;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
|
|
|
|
!tty_term_has(tty->term, TTYC_CSR)) {
|
2009-07-22 20:53:38 +00:00
|
|
|
tty_redraw_region(tty, ctx);
|
2009-06-01 22:58:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tty_reset(tty);
|
|
|
|
|
2009-10-12 09:09:35 +00:00
|
|
|
tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
|
2009-10-12 09:29:58 +00:00
|
|
|
tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2009-07-22 16:45:31 +00:00
|
|
|
tty_emulate_repeat(tty, TTYC_DL, TTYC_DL1, ctx->num);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-08-12 09:41:59 +00:00
|
|
|
tty_cmd_clearline(struct tty *tty, const struct tty_ctx *ctx)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2009-07-22 16:45:31 +00:00
|
|
|
struct window_pane *wp = ctx->wp;
|
|
|
|
struct screen *s = wp->screen;
|
|
|
|
u_int i;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
tty_reset(tty);
|
|
|
|
|
2009-10-12 09:29:58 +00:00
|
|
|
tty_cursor_pane(tty, ctx, 0, ctx->ocy);
|
|
|
|
|
2009-06-01 22:58:49 +00:00
|
|
|
if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
|
|
|
|
tty_term_has(tty->term, TTYC_EL)) {
|
|
|
|
tty_putcode(tty, TTYC_EL);
|
|
|
|
} else {
|
|
|
|
for (i = 0; i < screen_size_x(s); i++)
|
|
|
|
tty_putc(tty, ' ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-08-12 09:41:59 +00:00
|
|
|
tty_cmd_clearendofline(struct tty *tty, const struct tty_ctx *ctx)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2009-07-22 16:45:31 +00:00
|
|
|
struct window_pane *wp = ctx->wp;
|
|
|
|
struct screen *s = wp->screen;
|
|
|
|
u_int i;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
tty_reset(tty);
|
|
|
|
|
2009-10-12 09:29:58 +00:00
|
|
|
tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
|
|
|
|
|
2009-06-01 22:58:49 +00:00
|
|
|
if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
|
|
|
|
tty_term_has(tty->term, TTYC_EL))
|
|
|
|
tty_putcode(tty, TTYC_EL);
|
|
|
|
else {
|
2009-07-22 20:53:38 +00:00
|
|
|
for (i = ctx->ocx; i < screen_size_x(s); i++)
|
2009-06-01 22:58:49 +00:00
|
|
|
tty_putc(tty, ' ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-08-12 09:41:59 +00:00
|
|
|
tty_cmd_clearstartofline(struct tty *tty, const struct tty_ctx *ctx)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2009-07-22 16:45:31 +00:00
|
|
|
struct window_pane *wp = ctx->wp;
|
|
|
|
u_int i;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
tty_reset(tty);
|
|
|
|
|
|
|
|
if (wp->xoff == 0 && tty_term_has(tty->term, TTYC_EL1)) {
|
2009-10-12 09:29:58 +00:00
|
|
|
tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
|
2009-06-01 22:58:49 +00:00
|
|
|
tty_putcode(tty, TTYC_EL1);
|
|
|
|
} else {
|
2009-10-12 09:29:58 +00:00
|
|
|
tty_cursor_pane(tty, ctx, 0, ctx->ocy);
|
2009-07-22 20:53:38 +00:00
|
|
|
for (i = 0; i < ctx->ocx + 1; i++)
|
2009-06-01 22:58:49 +00:00
|
|
|
tty_putc(tty, ' ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-08-12 09:41:59 +00:00
|
|
|
tty_cmd_reverseindex(struct tty *tty, const struct tty_ctx *ctx)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2009-07-22 16:45:31 +00:00
|
|
|
struct window_pane *wp = ctx->wp;
|
|
|
|
struct screen *s = wp->screen;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
|
|
|
|
!tty_term_has(tty->term, TTYC_CSR)) {
|
2009-07-22 20:53:38 +00:00
|
|
|
tty_redraw_region(tty, ctx);
|
2009-06-01 22:58:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-07-22 20:53:38 +00:00
|
|
|
if (ctx->ocy == ctx->orupper) {
|
2009-10-11 22:35:10 +00:00
|
|
|
tty_reset(tty);
|
2009-10-12 09:09:35 +00:00
|
|
|
|
|
|
|
tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
|
2009-10-12 09:29:58 +00:00
|
|
|
tty_cursor_pane(tty, ctx, ctx->ocx, ctx->orupper);
|
2009-10-12 09:09:35 +00:00
|
|
|
|
2009-06-01 22:58:49 +00:00
|
|
|
tty_putcode(tty, TTYC_RI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-08-12 09:41:59 +00:00
|
|
|
tty_cmd_linefeed(struct tty *tty, const struct tty_ctx *ctx)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2009-07-22 16:45:31 +00:00
|
|
|
struct window_pane *wp = ctx->wp;
|
|
|
|
struct screen *s = wp->screen;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
|
|
|
|
!tty_term_has(tty->term, TTYC_CSR)) {
|
2009-07-22 20:53:38 +00:00
|
|
|
tty_redraw_region(tty, ctx);
|
2009-06-01 22:58:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-07-22 20:53:38 +00:00
|
|
|
if (ctx->ocy == ctx->orlower) {
|
2009-10-10 10:36:46 +00:00
|
|
|
tty_reset(tty);
|
2009-10-12 09:09:35 +00:00
|
|
|
|
|
|
|
tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
|
2009-10-12 09:29:58 +00:00
|
|
|
tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
|
2009-10-12 09:09:35 +00:00
|
|
|
|
2009-06-01 22:58:49 +00:00
|
|
|
tty_putc(tty, '\n');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-08-12 09:41:59 +00:00
|
|
|
tty_cmd_clearendofscreen(struct tty *tty, const struct tty_ctx *ctx)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2009-07-22 16:45:31 +00:00
|
|
|
struct window_pane *wp = ctx->wp;
|
|
|
|
struct screen *s = wp->screen;
|
|
|
|
u_int i, j;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
tty_reset(tty);
|
|
|
|
|
2009-10-12 09:09:35 +00:00
|
|
|
tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
|
2009-10-12 09:29:58 +00:00
|
|
|
tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
|
2009-10-12 09:09:35 +00:00
|
|
|
|
2009-06-01 22:58:49 +00:00
|
|
|
if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
|
|
|
|
tty_term_has(tty->term, TTYC_EL)) {
|
|
|
|
tty_putcode(tty, TTYC_EL);
|
2009-07-22 20:53:38 +00:00
|
|
|
if (ctx->ocy != screen_size_y(s) - 1) {
|
2009-10-12 09:29:58 +00:00
|
|
|
tty_cursor_pane(tty, ctx, 0, ctx->ocy + 1);
|
2009-07-22 20:53:38 +00:00
|
|
|
for (i = ctx->ocy + 1; i < screen_size_y(s); i++) {
|
2009-06-01 22:58:49 +00:00
|
|
|
tty_putcode(tty, TTYC_EL);
|
|
|
|
if (i == screen_size_y(s) - 1)
|
|
|
|
continue;
|
|
|
|
tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
|
|
|
|
tty->cy++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2009-07-22 20:53:38 +00:00
|
|
|
for (i = ctx->ocx; i < screen_size_x(s); i++)
|
2009-06-01 22:58:49 +00:00
|
|
|
tty_putc(tty, ' ');
|
2009-07-22 20:53:38 +00:00
|
|
|
for (j = ctx->ocy; j < screen_size_y(s); j++) {
|
2009-10-12 09:29:58 +00:00
|
|
|
tty_cursor_pane(tty, ctx, 0, j);
|
2009-06-01 22:58:49 +00:00
|
|
|
for (i = 0; i < screen_size_x(s); i++)
|
|
|
|
tty_putc(tty, ' ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-08-12 09:41:59 +00:00
|
|
|
tty_cmd_clearstartofscreen(struct tty *tty, const struct tty_ctx *ctx)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2009-07-22 16:45:31 +00:00
|
|
|
struct window_pane *wp = ctx->wp;
|
|
|
|
struct screen *s = wp->screen;
|
|
|
|
u_int i, j;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
tty_reset(tty);
|
|
|
|
|
2009-10-12 09:09:35 +00:00
|
|
|
tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
|
2009-10-12 09:29:58 +00:00
|
|
|
tty_cursor_pane(tty, ctx, 0, 0);
|
2009-10-12 09:09:35 +00:00
|
|
|
|
2009-06-01 22:58:49 +00:00
|
|
|
if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
|
|
|
|
tty_term_has(tty->term, TTYC_EL)) {
|
2009-07-22 20:53:38 +00:00
|
|
|
for (i = 0; i < ctx->ocy; i++) {
|
2009-06-01 22:58:49 +00:00
|
|
|
tty_putcode(tty, TTYC_EL);
|
|
|
|
tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
|
|
|
|
tty->cy++;
|
|
|
|
}
|
|
|
|
} else {
|
2009-07-22 20:53:38 +00:00
|
|
|
for (j = 0; j < ctx->ocy; j++) {
|
2009-10-12 09:29:58 +00:00
|
|
|
tty_cursor_pane(tty, ctx, 0, j);
|
2009-06-01 22:58:49 +00:00
|
|
|
for (i = 0; i < screen_size_x(s); i++)
|
|
|
|
tty_putc(tty, ' ');
|
|
|
|
}
|
|
|
|
}
|
2009-07-22 20:53:38 +00:00
|
|
|
for (i = 0; i <= ctx->ocx; i++)
|
2009-06-01 22:58:49 +00:00
|
|
|
tty_putc(tty, ' ');
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-08-12 09:41:59 +00:00
|
|
|
tty_cmd_clearscreen(struct tty *tty, const struct tty_ctx *ctx)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2009-07-22 16:45:31 +00:00
|
|
|
struct window_pane *wp = ctx->wp;
|
|
|
|
struct screen *s = wp->screen;
|
|
|
|
u_int i, j;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
tty_reset(tty);
|
|
|
|
|
2009-10-12 09:09:35 +00:00
|
|
|
tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
|
2009-10-12 09:29:58 +00:00
|
|
|
tty_cursor_pane(tty, ctx, 0, 0);
|
2009-10-12 09:09:35 +00:00
|
|
|
|
2009-06-01 22:58:49 +00:00
|
|
|
if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
|
|
|
|
tty_term_has(tty->term, TTYC_EL)) {
|
|
|
|
for (i = 0; i < screen_size_y(s); i++) {
|
|
|
|
tty_putcode(tty, TTYC_EL);
|
|
|
|
if (i != screen_size_y(s) - 1) {
|
|
|
|
tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
|
|
|
|
tty->cy++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (j = 0; j < screen_size_y(s); j++) {
|
2009-10-12 09:29:58 +00:00
|
|
|
tty_cursor_pane(tty, ctx, 0, j);
|
2009-06-01 22:58:49 +00:00
|
|
|
for (i = 0; i < screen_size_x(s); i++)
|
|
|
|
tty_putc(tty, ' ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-03 23:30:40 +00:00
|
|
|
void
|
2009-08-12 09:41:59 +00:00
|
|
|
tty_cmd_alignmenttest(struct tty *tty, const struct tty_ctx *ctx)
|
2009-06-03 23:30:40 +00:00
|
|
|
{
|
2009-07-22 16:45:31 +00:00
|
|
|
struct window_pane *wp = ctx->wp;
|
|
|
|
struct screen *s = wp->screen;
|
|
|
|
u_int i, j;
|
2009-06-03 23:30:40 +00:00
|
|
|
|
|
|
|
tty_reset(tty);
|
|
|
|
|
2009-10-12 09:09:35 +00:00
|
|
|
tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
|
2009-06-03 23:30:40 +00:00
|
|
|
|
|
|
|
for (j = 0; j < screen_size_y(s); j++) {
|
2009-10-12 09:29:58 +00:00
|
|
|
tty_cursor_pane(tty, ctx, 0, j);
|
2009-06-03 23:30:40 +00:00
|
|
|
for (i = 0; i < screen_size_x(s); i++)
|
|
|
|
tty_putc(tty, 'E');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-01 22:58:49 +00:00
|
|
|
void
|
2009-08-12 09:41:59 +00:00
|
|
|
tty_cmd_cell(struct tty *tty, const struct tty_ctx *ctx)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2009-10-12 09:29:58 +00:00
|
|
|
tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2009-07-22 16:45:31 +00:00
|
|
|
tty_cell(tty, ctx->cell, ctx->utf8);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-08-12 09:41:59 +00:00
|
|
|
tty_cmd_utf8character(struct tty *tty, const struct tty_ctx *ctx)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2009-07-22 16:45:31 +00:00
|
|
|
u_char *ptr = ctx->ptr;
|
2009-07-22 15:55:32 +00:00
|
|
|
size_t i;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2009-07-22 15:55:32 +00:00
|
|
|
for (i = 0; i < UTF8_SIZE; i++) {
|
2009-07-22 16:45:31 +00:00
|
|
|
if (ptr[i] == 0xff)
|
2009-07-22 15:55:32 +00:00
|
|
|
break;
|
2009-07-22 16:45:31 +00:00
|
|
|
tty_putc(tty, ptr[i]);
|
2009-07-22 15:55:32 +00:00
|
|
|
}
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tty_cell(
|
|
|
|
struct tty *tty, const struct grid_cell *gc, const struct grid_utf8 *gu)
|
|
|
|
{
|
|
|
|
u_int i;
|
|
|
|
|
|
|
|
/* Skip last character if terminal is stupid. */
|
|
|
|
if (tty->term->flags & TERM_EARLYWRAP &&
|
|
|
|
tty->cy == tty->sy - 1 && tty->cx == tty->sx - 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* If this is a padding character, do nothing. */
|
|
|
|
if (gc->flags & GRID_FLAG_PADDING)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Set the attributes. */
|
|
|
|
tty_attributes(tty, gc);
|
|
|
|
|
|
|
|
/* If not UTF-8, write directly. */
|
|
|
|
if (!(gc->flags & GRID_FLAG_UTF8)) {
|
|
|
|
if (gc->data < 0x20 || gc->data == 0x7f)
|
|
|
|
return;
|
|
|
|
tty_putc(tty, gc->data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the terminal doesn't support UTF-8, write underscores. */
|
|
|
|
if (!(tty->flags & TTY_UTF8)) {
|
|
|
|
for (i = 0; i < gu->width; i++)
|
|
|
|
tty_putc(tty, '_');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, write UTF-8. */
|
2009-06-24 16:01:02 +00:00
|
|
|
tty_pututf8(tty, gu);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tty_reset(struct tty *tty)
|
|
|
|
{
|
|
|
|
struct grid_cell *gc = &tty->cell;
|
|
|
|
|
|
|
|
if (memcmp(gc, &grid_default_cell, sizeof *gc) == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (tty_term_has(tty->term, TTYC_RMACS) && gc->attr & GRID_ATTR_CHARSET)
|
|
|
|
tty_putcode(tty, TTYC_RMACS);
|
|
|
|
tty_putcode(tty, TTYC_SGR0);
|
|
|
|
memcpy(gc, &grid_default_cell, sizeof *gc);
|
|
|
|
}
|
|
|
|
|
2009-10-12 09:16:59 +00:00
|
|
|
/* Set region inside pane. */
|
2009-06-01 22:58:49 +00:00
|
|
|
void
|
2009-10-12 09:09:35 +00:00
|
|
|
tty_region_pane(
|
|
|
|
struct tty *tty, const struct tty_ctx *ctx, u_int rupper, u_int rlower)
|
|
|
|
{
|
|
|
|
struct window_pane *wp = ctx->wp;
|
|
|
|
|
2009-10-12 09:16:59 +00:00
|
|
|
tty_region(tty, wp->yoff + rupper, wp->yoff + rlower);
|
2009-10-12 09:09:35 +00:00
|
|
|
}
|
|
|
|
|
2009-10-12 09:16:59 +00:00
|
|
|
/* Set region at absolute position. */
|
2009-10-12 09:09:35 +00:00
|
|
|
void
|
2009-10-12 09:16:59 +00:00
|
|
|
tty_region(struct tty *tty, u_int rupper, u_int rlower)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2009-10-12 09:09:35 +00:00
|
|
|
if (tty->rlower == rlower && tty->rupper == rupper)
|
|
|
|
return;
|
2009-06-01 22:58:49 +00:00
|
|
|
if (!tty_term_has(tty->term, TTYC_CSR))
|
|
|
|
return;
|
2009-10-12 09:09:35 +00:00
|
|
|
|
|
|
|
tty->rupper = rupper;
|
|
|
|
tty->rlower = rlower;
|
2009-10-12 13:01:18 +00:00
|
|
|
|
2009-10-12 09:09:35 +00:00
|
|
|
tty->cx = 0;
|
|
|
|
tty->cy = 0;
|
|
|
|
|
|
|
|
tty_putcode2(tty, TTYC_CSR, tty->rupper, tty->rlower);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2009-10-12 13:01:18 +00:00
|
|
|
/* Move cursor inside pane. */
|
2009-06-01 22:58:49 +00:00
|
|
|
void
|
2009-10-12 09:29:58 +00:00
|
|
|
tty_cursor_pane(struct tty *tty, const struct tty_ctx *ctx, u_int cx, u_int cy)
|
|
|
|
{
|
|
|
|
struct window_pane *wp = ctx->wp;
|
|
|
|
|
|
|
|
tty_cursor(tty, wp->xoff + cx, wp->yoff + cy);
|
|
|
|
}
|
|
|
|
|
2009-10-12 13:01:18 +00:00
|
|
|
/* Move cursor to absolute position. */
|
2009-10-12 09:29:58 +00:00
|
|
|
void
|
|
|
|
tty_cursor(struct tty *tty, u_int cx, u_int cy)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2009-10-12 13:01:18 +00:00
|
|
|
struct tty_term *term = tty->term;
|
|
|
|
u_int thisx, thisy;
|
|
|
|
int change;
|
|
|
|
|
|
|
|
if (cx > tty->sx - 1)
|
|
|
|
cx = tty->sx - 1;
|
|
|
|
|
|
|
|
thisx = tty->cx;
|
|
|
|
thisy = tty->cy;
|
|
|
|
|
|
|
|
/* No change. */
|
|
|
|
if (cx == thisx && cy == thisy)
|
|
|
|
return;
|
|
|
|
|
2009-10-12 14:54:19 +00:00
|
|
|
/* Very end of the line, just use absolute movement. */
|
|
|
|
if (thisx > tty->sx - 1)
|
|
|
|
goto absolute;
|
|
|
|
|
2009-10-12 13:01:18 +00:00
|
|
|
/* Move to home position (0, 0). */
|
|
|
|
if (cx == 0 && cy == 0 && tty_term_has(term, TTYC_HOME)) {
|
|
|
|
tty_putcode(tty, TTYC_HOME);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Zero on the next line. */
|
|
|
|
if (cx == 0 && cy == thisy + 1) {
|
2009-06-01 22:58:49 +00:00
|
|
|
tty_putc(tty, '\r');
|
2009-10-12 13:01:18 +00:00
|
|
|
tty_putc(tty, '\n');
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Row staying the same. */
|
|
|
|
if (cy == thisy) {
|
|
|
|
/* To left edge. */
|
|
|
|
if (cx == 0) {
|
|
|
|
tty_putc(tty, '\r');
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* One to the left. */
|
|
|
|
if (cx == thisx - 1 && tty_term_has(term, TTYC_CUB1)) {
|
|
|
|
tty_putcode(tty, TTYC_CUB1);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* One to the right. */
|
|
|
|
if (cx == thisx + 1 && tty_term_has(term, TTYC_CUF1)) {
|
|
|
|
tty_putcode(tty, TTYC_CUF1);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculate difference. */
|
|
|
|
change = thisx - cx; /* +ve left, -ve right */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Use HPA if change is larger than absolute, otherwise move
|
|
|
|
* the cursor with CUB/CUF.
|
|
|
|
*/
|
|
|
|
if (abs(change) > cx && tty_term_has(term, TTYC_HPA)) {
|
|
|
|
tty_putcode1(tty, TTYC_HPA, cx);
|
|
|
|
goto out;
|
|
|
|
} else if (change > 0 && tty_term_has(term, TTYC_CUB)) {
|
|
|
|
tty_putcode1(tty, TTYC_CUB, change);
|
|
|
|
goto out;
|
|
|
|
} else if (change < 0 && tty_term_has(term, TTYC_CUF)) {
|
|
|
|
tty_putcode1(tty, TTYC_CUF, -change);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Column staying the same. */
|
|
|
|
if (cx == thisx ) {
|
|
|
|
/* One above. */
|
|
|
|
if (cy != tty->rupper &&
|
|
|
|
cy == thisy - 1 && tty_term_has(term, TTYC_CUU1)) {
|
|
|
|
tty_putcode(tty, TTYC_CUU1);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* One below. */
|
|
|
|
if (cy != tty->rlower &&
|
|
|
|
cy == thisy + 1 && tty_term_has(term, TTYC_CUD1)) {
|
|
|
|
tty_putcode(tty, TTYC_CUD1);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculate difference. */
|
|
|
|
change = thisy - cy; /* +ve up, -ve down */
|
|
|
|
|
|
|
|
/*
|
2009-10-12 16:37:43 +00:00
|
|
|
* Try to use VPA if change is larger than absolute or if this change
|
2009-10-12 13:01:18 +00:00
|
|
|
* would cross the scroll region, otherwise use CUU/CUD.
|
|
|
|
*/
|
2009-10-12 16:37:43 +00:00
|
|
|
if (abs(change) > cy ||
|
2009-10-12 13:01:18 +00:00
|
|
|
(change < 0 && cy - change > tty->rlower) ||
|
2009-10-12 16:37:43 +00:00
|
|
|
(change > 0 && cy - change < tty->rupper)) {
|
|
|
|
if (tty_term_has(term, TTYC_VPA)) {
|
|
|
|
tty_putcode1(tty, TTYC_VPA, cy);
|
|
|
|
goto out;
|
|
|
|
}
|
2009-10-12 13:01:18 +00:00
|
|
|
} else if (change > 0 && tty_term_has(term, TTYC_CUU)) {
|
|
|
|
tty_putcode1(tty, TTYC_CUU, change);
|
|
|
|
goto out;
|
|
|
|
} else if (change < 0 && tty_term_has(term, TTYC_CUD)) {
|
|
|
|
tty_putcode1(tty, TTYC_CUD, -change);
|
|
|
|
goto out;
|
|
|
|
}
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
2009-10-12 13:01:18 +00:00
|
|
|
|
2009-10-12 14:54:19 +00:00
|
|
|
absolute:
|
2009-10-12 13:01:18 +00:00
|
|
|
/* Absolute movement. */
|
|
|
|
tty_putcode2(tty, TTYC_CUP, cy, cx);
|
|
|
|
|
|
|
|
out:
|
|
|
|
tty->cx = cx;
|
|
|
|
tty->cy = cy;
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tty_attributes(struct tty *tty, const struct grid_cell *gc)
|
|
|
|
{
|
|
|
|
struct grid_cell *tc = &tty->cell;
|
|
|
|
u_char changed;
|
2009-08-05 16:26:38 +00:00
|
|
|
u_int fg, bg, attr;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If no setab, try to use the reverse attribute as a best-effort for a
|
|
|
|
* non-default background. This is a bit of a hack but it doesn't do
|
|
|
|
* any serious harm and makes a couple of applications happier.
|
|
|
|
*/
|
|
|
|
fg = gc->fg; bg = gc->bg; attr = gc->attr;
|
|
|
|
if (!tty_term_has(tty->term, TTYC_SETAB)) {
|
|
|
|
if (attr & GRID_ATTR_REVERSE) {
|
|
|
|
if (fg != 7 && fg != 8)
|
|
|
|
attr &= ~GRID_ATTR_REVERSE;
|
|
|
|
} else {
|
|
|
|
if (bg != 0 && bg != 8)
|
|
|
|
attr |= GRID_ATTR_REVERSE;
|
|
|
|
}
|
|
|
|
}
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
/* If any bits are being cleared, reset everything. */
|
2009-08-05 16:26:38 +00:00
|
|
|
if (tc->attr & ~attr)
|
2009-06-01 22:58:49 +00:00
|
|
|
tty_reset(tty);
|
|
|
|
|
|
|
|
/* Filter out attribute bits already set. */
|
2009-08-05 16:26:38 +00:00
|
|
|
changed = attr & ~tc->attr;
|
|
|
|
tc->attr = attr;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
/* Set the attributes. */
|
|
|
|
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);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
if (changed & GRID_ATTR_HIDDEN)
|
|
|
|
tty_putcode(tty, TTYC_INVIS);
|
|
|
|
if (changed & GRID_ATTR_CHARSET)
|
|
|
|
tty_putcode(tty, TTYC_SMACS);
|
|
|
|
|
|
|
|
/* Set foreground colour. */
|
|
|
|
if (fg != tc->fg ||
|
|
|
|
(gc->flags & GRID_FLAG_FG256) != (tc->flags & GRID_FLAG_FG256)) {
|
|
|
|
tty_attributes_fg(tty, gc);
|
|
|
|
tc->fg = fg;
|
2009-06-27 12:57:14 +00:00
|
|
|
tc->flags &= ~GRID_FLAG_FG256;
|
|
|
|
tc->flags |= gc->flags & GRID_FLAG_FG256;
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set background colour. */
|
|
|
|
if (bg != tc->bg ||
|
|
|
|
(gc->flags & GRID_FLAG_BG256) != (tc->flags & GRID_FLAG_BG256)) {
|
|
|
|
tty_attributes_bg(tty, gc);
|
|
|
|
tc->bg = bg;
|
2009-06-27 12:57:14 +00:00
|
|
|
tc->flags &= ~GRID_FLAG_BG256;
|
|
|
|
tc->flags |= gc->flags & GRID_FLAG_BG256;
|
2009-06-01 22:58:49 +00: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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tty_attributes_fg(struct tty *tty, const struct grid_cell *gc)
|
|
|
|
{
|
|
|
|
u_char fg;
|
|
|
|
|
|
|
|
fg = gc->fg;
|
|
|
|
if (gc->flags & GRID_FLAG_FG256) {
|
|
|
|
if (tty_try_256(tty, fg, "38") == 0)
|
|
|
|
return;
|
|
|
|
if (tty_try_88(tty, fg, "38") == 0)
|
|
|
|
return;
|
|
|
|
fg = colour_256to16(fg);
|
|
|
|
if (fg & 8) {
|
|
|
|
fg &= 7;
|
|
|
|
tty_putcode(tty, TTYC_BOLD);
|
|
|
|
tty->cell.attr |= GRID_ATTR_BRIGHT;
|
|
|
|
} else if (tty->cell.attr & GRID_ATTR_BRIGHT)
|
|
|
|
tty_reset(tty);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fg == 8 &&
|
|
|
|
!(tty->term->flags & TERM_HASDEFAULTS) &&
|
|
|
|
!(tty->term_flags & TERM_HASDEFAULTS))
|
|
|
|
fg = 7;
|
|
|
|
if (fg == 8)
|
|
|
|
tty_puts(tty, "\033[39m");
|
|
|
|
else
|
|
|
|
tty_putcode1(tty, TTYC_SETAF, fg);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tty_attributes_bg(struct tty *tty, const struct grid_cell *gc)
|
|
|
|
{
|
|
|
|
u_char bg;
|
|
|
|
|
|
|
|
bg = gc->bg;
|
|
|
|
if (gc->flags & GRID_FLAG_BG256) {
|
|
|
|
if (tty_try_256(tty, bg, "48") == 0)
|
|
|
|
return;
|
|
|
|
if (tty_try_88(tty, bg, "48") == 0)
|
|
|
|
return;
|
|
|
|
bg = colour_256to16(bg);
|
|
|
|
if (bg & 8)
|
|
|
|
bg &= 7;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bg == 8 &&
|
|
|
|
!(tty->term->flags & TERM_HASDEFAULTS) &&
|
|
|
|
!(tty->term_flags & TERM_HASDEFAULTS))
|
|
|
|
bg = 0;
|
|
|
|
if (bg == 8)
|
|
|
|
tty_puts(tty, "\033[49m");
|
|
|
|
else
|
|
|
|
tty_putcode1(tty, TTYC_SETAB, bg);
|
|
|
|
}
|