2011-01-21 23:44:13 +00:00
|
|
|
/* $Id: tty.c,v 1.200 2011-01-21 23:44:13 tcunha Exp $ */
|
2007-11-27 19:23:34 +00: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>
|
2009-10-15 01:33:21 +00:00
|
|
|
#include <stdlib.h>
|
2007-11-27 19:23:34 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <termios.h>
|
2008-06-18 22:08:56 +00:00
|
|
|
#include <unistd.h>
|
2007-11-27 19:23:34 +00:00
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
2009-11-08 23:26:56 +00:00
|
|
|
void tty_read_callback(struct bufferevent *, void *);
|
2009-11-08 22:58:38 +00:00
|
|
|
void tty_error_callback(struct bufferevent *, short, void *);
|
|
|
|
|
2009-03-07 10:29:06 +00:00
|
|
|
int tty_try_256(struct tty *, u_char, const char *);
|
|
|
|
int tty_try_88(struct tty *, u_char, const char *);
|
|
|
|
|
2010-03-08 14:53:49 +00:00
|
|
|
void tty_colours(struct tty *, const struct grid_cell *);
|
|
|
|
void tty_check_fg(struct tty *, struct grid_cell *);
|
|
|
|
void tty_check_bg(struct tty *, struct grid_cell *);
|
|
|
|
void tty_colours_fg(struct tty *, const struct grid_cell *);
|
2009-11-28 14:50:37 +00:00
|
|
|
void tty_colours_bg(struct tty *, const struct grid_cell *);
|
2008-09-25 20:08:57 +00:00
|
|
|
|
2009-08-14 21:30:24 +00:00
|
|
|
void tty_redraw_region(struct tty *, const struct tty_ctx *);
|
2009-07-23 12:38:01 +00:00
|
|
|
void tty_emulate_repeat(
|
|
|
|
struct tty *, enum tty_code_code, enum tty_code_code, u_int);
|
2009-07-23 12:48:18 +00:00
|
|
|
void tty_cell(struct tty *,
|
2009-12-04 22:14:47 +00:00
|
|
|
const struct grid_cell *, const struct grid_utf8 *);
|
2007-11-27 19:23:34 +00:00
|
|
|
|
2010-09-18 15:43:53 +00:00
|
|
|
#define tty_use_acs(tty) \
|
2010-09-18 15:45:03 +00:00
|
|
|
(tty_term_has(tty->term, TTYC_ACSC) && !((tty)->flags & TTY_UTF8))
|
2010-09-18 15:43:53 +00:00
|
|
|
|
2007-11-27 19:23:34 +00:00
|
|
|
void
|
2009-09-23 14:39:30 +00:00
|
|
|
tty_init(struct tty *tty, int fd, char *term)
|
2007-11-27 19:23:34 +00:00
|
|
|
{
|
2009-09-23 14:39:30 +00:00
|
|
|
char *path;
|
|
|
|
|
|
|
|
memset(tty, 0, sizeof *tty);
|
2009-08-14 21:24:46 +00:00
|
|
|
tty->log_fd = -1;
|
2009-08-14 21:23:20 +00:00
|
|
|
|
2009-07-08 17:58:32 +00:00
|
|
|
if (term == NULL || *term == '\0')
|
2007-12-06 20:53:48 +00:00
|
|
|
tty->termname = xstrdup("unknown");
|
|
|
|
else
|
|
|
|
tty->termname = xstrdup(term);
|
2009-09-23 14:39:30 +00:00
|
|
|
tty->fd = fd;
|
|
|
|
|
|
|
|
if ((path = ttyname(fd)) == NULL)
|
|
|
|
fatalx("ttyname failed");
|
|
|
|
tty->path = xstrdup(path);
|
|
|
|
|
2008-09-09 22:16:37 +00:00
|
|
|
tty->flags = 0;
|
2008-09-25 20:08:57 +00:00
|
|
|
tty->term_flags = 0;
|
2007-11-27 19:23:34 +00:00
|
|
|
}
|
|
|
|
|
2010-06-06 00:30:34 +00:00
|
|
|
int
|
2009-09-23 14:44:02 +00:00
|
|
|
tty_resize(struct tty *tty)
|
|
|
|
{
|
|
|
|
struct winsize ws;
|
2010-06-06 00:30:34 +00:00
|
|
|
u_int sx, sy;
|
2009-09-23 14:44:02 +00:00
|
|
|
|
|
|
|
if (ioctl(tty->fd, TIOCGWINSZ, &ws) != -1) {
|
2010-06-06 00:30:34 +00:00
|
|
|
sx = ws.ws_col;
|
|
|
|
if (sx == 0)
|
|
|
|
sx = 80;
|
|
|
|
sy = ws.ws_row;
|
|
|
|
if (sy == 0)
|
|
|
|
sy = 24;
|
|
|
|
} else {
|
|
|
|
sx = 80;
|
|
|
|
sy = 24;
|
2009-09-23 14:44:02 +00:00
|
|
|
}
|
2010-06-06 00:30:34 +00:00
|
|
|
if (sx == tty->sx && sy == tty->sy)
|
|
|
|
return (0);
|
|
|
|
tty->sx = sx;
|
|
|
|
tty->sy = sy;
|
2009-09-23 14:44:02 +00:00
|
|
|
|
|
|
|
tty->cx = UINT_MAX;
|
|
|
|
tty->cy = UINT_MAX;
|
|
|
|
|
|
|
|
tty->rupper = UINT_MAX;
|
|
|
|
tty->rlower = UINT_MAX;
|
2010-06-06 00:30:34 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If the terminal has been started, reset the actual scroll region and
|
|
|
|
* cursor position, as this may not have happened.
|
|
|
|
*/
|
|
|
|
if (tty->flags & TTY_STARTED) {
|
|
|
|
tty_cursor(tty, 0, 0);
|
|
|
|
tty_region(tty, 0, tty->sy - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (1);
|
2009-09-23 14:44:02 +00:00
|
|
|
}
|
|
|
|
|
2007-11-27 19:23:34 +00:00
|
|
|
int
|
2009-08-09 15:26:24 +00:00
|
|
|
tty_open(struct tty *tty, const char *overrides, char **cause)
|
2007-11-27 19:23:34 +00:00
|
|
|
{
|
2010-06-06 00:30:34 +00:00
|
|
|
char out[64];
|
2009-09-23 14:39:30 +00:00
|
|
|
int fd;
|
2008-09-26 06:45:28 +00:00
|
|
|
|
2009-09-23 14:39:30 +00:00
|
|
|
if (debug_level > 3) {
|
2010-06-06 00:30:34 +00:00
|
|
|
xsnprintf(out, sizeof out, "tmux-out-%ld.log", (long) getpid());
|
|
|
|
fd = open(out, O_WRONLY|O_CREAT|O_TRUNC, 0644);
|
2009-09-23 14:39:30 +00:00
|
|
|
if (fd != -1 && fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
|
|
|
|
fatal("fcntl failed");
|
|
|
|
tty->log_fd = fd;
|
|
|
|
}
|
2008-09-09 22:16:37 +00:00
|
|
|
|
2009-08-09 15:26:24 +00:00
|
|
|
tty->term = tty_term_find(tty->termname, tty->fd, overrides, cause);
|
2009-08-14 21:20:01 +00:00
|
|
|
if (tty->term == NULL) {
|
|
|
|
tty_close(tty);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
tty->flags |= TTY_OPENED;
|
2007-12-06 18:28:55 +00:00
|
|
|
|
2009-09-23 14:33:13 +00:00
|
|
|
tty->flags &= ~(TTY_NOCURSOR|TTY_FREEZE|TTY_ESCAPE);
|
2009-01-18 12:09:42 +00:00
|
|
|
|
2009-11-08 22:58:38 +00:00
|
|
|
tty->event = bufferevent_new(
|
2009-11-08 23:26:56 +00:00
|
|
|
tty->fd, tty_read_callback, NULL, tty_error_callback, tty);
|
2009-11-08 22:58:38 +00:00
|
|
|
|
2009-01-18 12:09:42 +00:00
|
|
|
tty_start_tty(tty);
|
|
|
|
|
|
|
|
tty_keys_init(tty);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2009-11-28 14:50:37 +00:00
|
|
|
/* ARGSUSED */
|
2009-11-08 23:26:56 +00:00
|
|
|
void
|
|
|
|
tty_read_callback(unused struct bufferevent *bufev, void *data)
|
|
|
|
{
|
|
|
|
struct tty *tty = data;
|
|
|
|
|
|
|
|
while (tty_keys_next(tty))
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2009-11-28 14:50:37 +00:00
|
|
|
/* ARGSUSED */
|
2009-11-08 22:58:38 +00:00
|
|
|
void
|
|
|
|
tty_error_callback(
|
|
|
|
unused struct bufferevent *bufev, unused short what, unused void *data)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-01-18 12:09:42 +00:00
|
|
|
void
|
|
|
|
tty_start_tty(struct tty *tty)
|
|
|
|
{
|
|
|
|
struct termios tio;
|
2007-12-06 18:28:55 +00:00
|
|
|
|
2009-09-23 15:08:21 +00:00
|
|
|
if (tty->fd == -1)
|
|
|
|
return;
|
|
|
|
|
2011-01-21 23:44:13 +00:00
|
|
|
setblocking(tty->fd, 0);
|
2009-09-23 15:18:56 +00:00
|
|
|
|
2009-11-08 22:58:38 +00:00
|
|
|
bufferevent_enable(tty->event, EV_READ|EV_WRITE);
|
|
|
|
|
2007-12-06 18:28:55 +00:00
|
|
|
if (tcgetattr(tty->fd, &tty->tio) != 0)
|
|
|
|
fatal("tcgetattr failed");
|
2008-06-10 18:51:22 +00:00
|
|
|
memcpy(&tio, &tty->tio, sizeof tio);
|
2008-07-02 16:18:24 +00:00
|
|
|
tio.c_iflag &= ~(IXON|IXOFF|ICRNL|INLCR|IGNCR|IMAXBEL|ISTRIP);
|
2008-06-18 22:08:56 +00:00
|
|
|
tio.c_iflag |= IGNBRK;
|
2008-07-02 16:18:24 +00:00
|
|
|
tio.c_oflag &= ~(OPOST|ONLCR|OCRNL|ONLRET);
|
2008-09-08 17:40:51 +00:00
|
|
|
tio.c_lflag &= ~(IEXTEN|ICANON|ECHO|ECHOE|ECHONL|ECHOCTL|
|
|
|
|
ECHOPRT|ECHOKE|ECHOCTL|ISIG);
|
2008-06-18 22:21:51 +00:00
|
|
|
tio.c_cc[VMIN] = 1;
|
2009-10-28 23:12:38 +00:00
|
|
|
tio.c_cc[VTIME] = 0;
|
2007-12-06 18:28:55 +00:00
|
|
|
if (tcsetattr(tty->fd, TCSANOW, &tio) != 0)
|
|
|
|
fatal("tcsetattr failed");
|
2010-01-05 23:49:24 +00:00
|
|
|
tcflush(tty->fd, TCIOFLUSH);
|
2007-12-06 18:28:55 +00:00
|
|
|
|
2009-01-09 23:57:42 +00:00
|
|
|
tty_putcode(tty, TTYC_SMCUP);
|
2009-07-12 17:08:37 +00:00
|
|
|
|
2009-08-16 19:31:37 +00:00
|
|
|
tty_putcode(tty, TTYC_SGR0);
|
|
|
|
memcpy(&tty->cell, &grid_default_cell, sizeof tty->cell);
|
|
|
|
|
2009-12-04 22:05:52 +00:00
|
|
|
tty_putcode(tty, TTYC_RMKX);
|
2010-09-18 15:43:53 +00:00
|
|
|
if (tty_use_acs(tty))
|
|
|
|
tty_putcode(tty, TTYC_ENACS);
|
2009-01-09 23:57:42 +00:00
|
|
|
tty_putcode(tty, TTYC_CLEAR);
|
2009-05-04 17:58:27 +00:00
|
|
|
|
2009-01-27 21:39:15 +00:00
|
|
|
tty_putcode(tty, TTYC_CNORM);
|
2009-01-28 19:52:21 +00:00
|
|
|
if (tty_term_has(tty->term, TTYC_KMOUS))
|
|
|
|
tty_puts(tty, "\033[?1000l");
|
2008-06-21 12:41:05 +00:00
|
|
|
|
2009-01-18 12:09:42 +00:00
|
|
|
tty->cx = UINT_MAX;
|
|
|
|
tty->cy = UINT_MAX;
|
2007-12-06 18:28:55 +00:00
|
|
|
|
2009-01-18 12:09:42 +00:00
|
|
|
tty->rlower = UINT_MAX;
|
|
|
|
tty->rupper = UINT_MAX;
|
2009-01-27 21:39:15 +00:00
|
|
|
|
2009-01-28 19:52:21 +00:00
|
|
|
tty->mode = MODE_CURSOR;
|
2009-08-14 21:17:54 +00:00
|
|
|
|
|
|
|
tty->flags |= TTY_STARTED;
|
2009-01-18 12:09:42 +00:00
|
|
|
}
|
2007-12-06 18:28:55 +00:00
|
|
|
|
2009-01-18 12:09:42 +00:00
|
|
|
void
|
|
|
|
tty_stop_tty(struct tty *tty)
|
|
|
|
{
|
|
|
|
struct winsize ws;
|
2007-12-06 18:28:55 +00:00
|
|
|
|
2009-08-14 21:17:54 +00:00
|
|
|
if (!(tty->flags & TTY_STARTED))
|
|
|
|
return;
|
|
|
|
tty->flags &= ~TTY_STARTED;
|
|
|
|
|
2009-11-08 22:58:38 +00:00
|
|
|
bufferevent_disable(tty->event, EV_READ|EV_WRITE);
|
|
|
|
|
2009-01-18 12:09:42 +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-02-17 18:53:10 +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));
|
2010-09-18 15:43:53 +00:00
|
|
|
if (tty_use_acs(tty))
|
|
|
|
tty_raw(tty, tty_term_string(tty->term, TTYC_RMACS));
|
2009-02-17 18:53:10 +00:00
|
|
|
tty_raw(tty, tty_term_string(tty->term, TTYC_SGR0));
|
|
|
|
tty_raw(tty, tty_term_string(tty->term, TTYC_RMKX));
|
2009-05-19 16:08:35 +00:00
|
|
|
tty_raw(tty, tty_term_string(tty->term, TTYC_CLEAR));
|
2009-06-25 16:34:50 +00:00
|
|
|
|
2009-02-17 18:53:10 +00:00
|
|
|
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-12 17:08:37 +00:00
|
|
|
|
|
|
|
tty_raw(tty, tty_term_string(tty->term, TTYC_RMCUP));
|
2010-02-26 13:34:15 +00:00
|
|
|
|
2011-01-21 23:44:13 +00:00
|
|
|
setblocking(tty->fd, 1);
|
2007-12-06 18:28:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-08-14 21:17:54 +00:00
|
|
|
tty_close(struct tty *tty)
|
2007-12-06 18:28:55 +00:00
|
|
|
{
|
2008-09-09 22:16:37 +00:00
|
|
|
if (tty->log_fd != -1) {
|
|
|
|
close(tty->log_fd);
|
|
|
|
tty->log_fd = -1;
|
|
|
|
}
|
|
|
|
|
2009-11-08 23:26:56 +00:00
|
|
|
evtimer_del(&tty->key_timer);
|
2009-08-14 21:17:54 +00:00
|
|
|
tty_stop_tty(tty);
|
2009-05-04 17:58:27 +00:00
|
|
|
|
2009-08-14 21:20:01 +00:00
|
|
|
if (tty->flags & TTY_OPENED) {
|
2009-11-08 22:58:38 +00:00
|
|
|
bufferevent_free(tty->event);
|
|
|
|
|
2009-08-14 21:20:01 +00:00
|
|
|
tty_term_free(tty->term);
|
|
|
|
tty_keys_free(tty);
|
|
|
|
|
|
|
|
tty->flags &= ~TTY_OPENED;
|
|
|
|
}
|
2007-12-06 18:28:55 +00:00
|
|
|
|
2009-08-14 21:20:01 +00:00
|
|
|
if (tty->fd != -1) {
|
|
|
|
close(tty->fd);
|
|
|
|
tty->fd = -1;
|
|
|
|
}
|
2007-12-06 18:28:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-08-14 21:17:54 +00:00
|
|
|
tty_free(struct tty *tty)
|
2007-12-06 18:28:55 +00:00
|
|
|
{
|
2009-08-14 21:17:54 +00:00
|
|
|
tty_close(tty);
|
2007-12-06 18:28:55 +00:00
|
|
|
|
|
|
|
if (tty->path != NULL)
|
|
|
|
xfree(tty->path);
|
|
|
|
if (tty->termname != NULL)
|
|
|
|
xfree(tty->termname);
|
|
|
|
}
|
|
|
|
|
2009-01-09 23:57:42 +00:00
|
|
|
void
|
|
|
|
tty_raw(struct tty *tty, const char *s)
|
2007-12-06 18:28:55 +00:00
|
|
|
{
|
2009-01-09 23:57:42 +00:00
|
|
|
write(tty->fd, s, strlen(s));
|
2007-11-27 19:23:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-01-09 23:57:42 +00:00
|
|
|
tty_putcode(struct tty *tty, enum tty_code_code code)
|
2007-11-27 19:23:34 +00:00
|
|
|
{
|
2009-01-09 23:57:42 +00:00
|
|
|
tty_puts(tty, tty_term_string(tty->term, code));
|
2007-11-27 19:23:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-01-09 23:57:42 +00:00
|
|
|
tty_putcode1(struct tty *tty, enum tty_code_code code, int a)
|
2007-11-27 19:23:34 +00:00
|
|
|
{
|
2009-01-09 23:57:42 +00:00
|
|
|
if (a < 0)
|
2007-11-27 19:23:34 +00:00
|
|
|
return;
|
2009-01-09 23:57:42 +00:00
|
|
|
tty_puts(tty, tty_term_string1(tty->term, code, a));
|
2008-06-21 12:41:05 +00:00
|
|
|
}
|
|
|
|
|
2007-11-27 19:23:34 +00:00
|
|
|
void
|
2009-01-09 23:57:42 +00:00
|
|
|
tty_putcode2(struct tty *tty, enum tty_code_code code, int a, int b)
|
2007-11-27 19:23:34 +00:00
|
|
|
{
|
2009-01-09 23:57:42 +00:00
|
|
|
if (a < 0 || b < 0)
|
|
|
|
return;
|
|
|
|
tty_puts(tty, tty_term_string2(tty->term, code, a, b));
|
2007-11-27 19:23:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tty_puts(struct tty *tty, const char *s)
|
|
|
|
{
|
2009-01-09 23:57:42 +00:00
|
|
|
if (*s == '\0')
|
2008-09-23 17:54:35 +00:00
|
|
|
return;
|
2009-11-08 22:58:38 +00:00
|
|
|
bufferevent_write(tty->event, s, strlen(s));
|
2008-09-09 22:16:37 +00:00
|
|
|
|
|
|
|
if (tty->log_fd != -1)
|
2009-01-09 23:57:42 +00:00
|
|
|
write(tty->log_fd, s, strlen(s));
|
2007-11-27 19:23:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-04-29 17:50:52 +00:00
|
|
|
tty_putc(struct tty *tty, u_char ch)
|
2007-11-27 19:23:34 +00:00
|
|
|
{
|
2010-09-18 15:43:53 +00:00
|
|
|
const char *acs;
|
|
|
|
u_int sx;
|
2009-02-11 23:16:45 +00:00
|
|
|
|
2010-09-18 15:43:53 +00:00
|
|
|
if (tty->cell.attr & GRID_ATTR_CHARSET) {
|
|
|
|
acs = tty_acs_get(tty, ch);
|
|
|
|
if (acs != NULL)
|
|
|
|
bufferevent_write(tty->event, acs, strlen(acs));
|
|
|
|
else
|
|
|
|
bufferevent_write(tty->event, &ch, 1);
|
|
|
|
} else
|
|
|
|
bufferevent_write(tty->event, &ch, 1);
|
2008-09-09 22:16:37 +00:00
|
|
|
|
2009-04-29 17:06:45 +00:00
|
|
|
if (ch >= 0x20 && ch != 0x7f) {
|
2009-02-11 23:16:45 +00:00
|
|
|
sx = tty->sx;
|
|
|
|
if (tty->term->flags & TERM_EARLYWRAP)
|
|
|
|
sx--;
|
|
|
|
|
2009-10-15 01:41:14 +00:00
|
|
|
if (tty->cx >= sx) {
|
|
|
|
tty->cx = 1;
|
|
|
|
if (tty->cy != tty->rlower)
|
|
|
|
tty->cy++;
|
2009-02-11 23:16:45 +00:00
|
|
|
} else
|
|
|
|
tty->cx++;
|
|
|
|
}
|
2009-01-11 23:31:46 +00:00
|
|
|
|
2008-09-09 22:16:37 +00:00
|
|
|
if (tty->log_fd != -1)
|
|
|
|
write(tty->log_fd, &ch, 1);
|
2007-11-27 19:23:34 +00:00
|
|
|
}
|
|
|
|
|
2009-06-25 16:01:26 +00:00
|
|
|
void
|
|
|
|
tty_pututf8(struct tty *tty, const struct grid_utf8 *gu)
|
|
|
|
{
|
2009-11-19 22:23:27 +00:00
|
|
|
size_t size;
|
2009-06-25 16:01:26 +00:00
|
|
|
|
2009-11-19 22:23:27 +00:00
|
|
|
size = grid_utf8_size(gu);
|
|
|
|
bufferevent_write(tty->event, gu->data, size);
|
|
|
|
if (tty->log_fd != -1)
|
|
|
|
write(tty->log_fd, gu->data, size);
|
2009-10-23 17:16:25 +00:00
|
|
|
tty->cx += gu->width;
|
2009-06-25 16:01:26 +00:00
|
|
|
}
|
|
|
|
|
2008-06-18 18:52:44 +00:00
|
|
|
void
|
|
|
|
tty_set_title(struct tty *tty, const char *title)
|
|
|
|
{
|
2008-06-21 13:11:28 +00:00
|
|
|
if (strstr(tty->termname, "xterm") == NULL &&
|
|
|
|
strstr(tty->termname, "rxvt") == NULL &&
|
|
|
|
strcmp(tty->termname, "screen") != 0)
|
|
|
|
return;
|
|
|
|
|
2008-06-23 16:58:49 +00:00
|
|
|
tty_puts(tty, "\033]0;");
|
2008-06-18 18:52:44 +00:00
|
|
|
tty_puts(tty, title);
|
|
|
|
tty_putc(tty, '\007');
|
|
|
|
}
|
|
|
|
|
2009-01-28 19:52:21 +00: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);
|
|
|
|
}
|
2010-12-30 22:27:38 +00:00
|
|
|
if (changed & ALL_MOUSE_MODES) {
|
|
|
|
if (mode & ALL_MOUSE_MODES) {
|
2011-01-07 14:34:45 +00:00
|
|
|
if (mode & MODE_MOUSE_UTF8)
|
|
|
|
tty_puts(tty, "\033[?1005h");
|
2010-12-30 22:27:38 +00:00
|
|
|
if (mode & MODE_MOUSE_STANDARD)
|
2010-06-06 00:23:44 +00:00
|
|
|
tty_puts(tty, "\033[?1000h");
|
2010-12-30 22:27:38 +00:00
|
|
|
else if (mode & MODE_MOUSE_HIGHLIGHT)
|
|
|
|
tty_puts(tty, "\033[?1001h");
|
|
|
|
else if (mode & MODE_MOUSE_BUTTON)
|
|
|
|
tty_puts(tty, "\033[?1002h");
|
|
|
|
else if (mode & MODE_MOUSE_ANY)
|
|
|
|
tty_puts(tty, "\033[?1003h");
|
2010-06-06 00:23:44 +00:00
|
|
|
} else {
|
2010-12-30 22:27:38 +00:00
|
|
|
if (tty->mode & MODE_MOUSE_STANDARD)
|
2010-06-06 00:23:44 +00:00
|
|
|
tty_puts(tty, "\033[?1000l");
|
2010-12-30 22:27:38 +00:00
|
|
|
else if (tty->mode & MODE_MOUSE_HIGHLIGHT)
|
|
|
|
tty_puts(tty, "\033[?1001l");
|
|
|
|
else if (tty->mode & MODE_MOUSE_BUTTON)
|
|
|
|
tty_puts(tty, "\033[?1002l");
|
|
|
|
else if (tty->mode & MODE_MOUSE_ANY)
|
|
|
|
tty_puts(tty, "\033[?1003l");
|
2011-01-07 14:34:45 +00:00
|
|
|
if (tty->mode & MODE_MOUSE_UTF8)
|
|
|
|
tty_puts(tty, "\033[?1005l");
|
2010-06-06 00:23:44 +00:00
|
|
|
}
|
2009-01-28 19:52:21 +00:00
|
|
|
}
|
2009-12-04 22:05:52 +00:00
|
|
|
if (changed & MODE_KKEYPAD) {
|
|
|
|
if (mode & MODE_KKEYPAD)
|
|
|
|
tty_putcode(tty, TTYC_SMKX);
|
|
|
|
else
|
|
|
|
tty_putcode(tty, TTYC_RMKX);
|
|
|
|
}
|
2009-01-28 19:52:21 +00:00
|
|
|
tty->mode = mode;
|
|
|
|
}
|
|
|
|
|
2008-12-13 17:41:49 +00:00
|
|
|
void
|
2009-01-09 23:57:42 +00: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-04-02 20:30:23 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2009-02-11 19:06:58 +00:00
|
|
|
void
|
2009-08-14 21:30:24 +00:00
|
|
|
tty_redraw_region(struct tty *tty, const struct tty_ctx *ctx)
|
2009-04-01 18:21:42 +00:00
|
|
|
{
|
2009-07-23 12:48:18 +00:00
|
|
|
struct window_pane *wp = ctx->wp;
|
|
|
|
struct screen *s = wp->screen;
|
|
|
|
u_int i;
|
2009-04-02 20:30:23 +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-23 12:48:18 +00:00
|
|
|
if (ctx->orupper - ctx->orlower >= screen_size_y(s) / 2) {
|
2009-04-02 21:08:15 +00:00
|
|
|
wp->flags |= PANE_REDRAW;
|
2009-04-02 20:30:23 +00:00
|
|
|
return;
|
|
|
|
}
|
2009-04-01 18:21:42 +00:00
|
|
|
|
2009-07-23 12:48:18 +00:00
|
|
|
if (ctx->ocy < ctx->orupper || ctx->ocy > ctx->orlower) {
|
|
|
|
for (i = ctx->ocy; i < screen_size_y(s); i++)
|
2009-04-02 20:30:23 +00:00
|
|
|
tty_draw_line(tty, s, i, wp->xoff, wp->yoff);
|
2009-04-01 18:21:42 +00:00
|
|
|
} else {
|
2009-07-23 12:48:18 +00:00
|
|
|
for (i = ctx->orupper; i <= ctx->orlower; i++)
|
2009-04-02 20:30:23 +00:00
|
|
|
tty_draw_line(tty, s, i, wp->xoff, wp->yoff);
|
2009-04-01 18:21:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tty_draw_line(struct tty *tty, struct screen *s, u_int py, u_int ox, u_int oy)
|
2009-02-11 19:06:58 +00:00
|
|
|
{
|
2009-03-28 16:30:05 +00:00
|
|
|
const struct grid_cell *gc;
|
2009-10-15 01:41:14 +00:00
|
|
|
struct grid_line *gl;
|
2009-07-28 22:41:38 +00:00
|
|
|
struct grid_cell tmpgc;
|
2009-03-28 20:17:29 +00:00
|
|
|
const struct grid_utf8 *gu;
|
2009-03-28 10:15:01 +00:00
|
|
|
u_int i, sx;
|
2009-02-11 19:06:58 +00:00
|
|
|
|
2009-10-09 13:03:28 +00:00
|
|
|
tty_update_mode(tty, tty->mode & ~MODE_CURSOR);
|
|
|
|
|
2009-03-28 10:15:01 +00:00
|
|
|
sx = screen_size_x(s);
|
2009-08-09 17:28:24 +00:00
|
|
|
if (sx > s->grid->linedata[s->grid->hsize + py].cellsize)
|
|
|
|
sx = s->grid->linedata[s->grid->hsize + py].cellsize;
|
2009-03-28 10:15:01 +00:00
|
|
|
if (sx > tty->sx)
|
|
|
|
sx = tty->sx;
|
|
|
|
|
2009-10-15 01:41:14 +00:00
|
|
|
/*
|
|
|
|
* Don't move the cursor to the start permission if it will wrap there
|
2009-10-23 17:06:23 +00:00
|
|
|
* itself.
|
2009-10-15 01:41:14 +00:00
|
|
|
*/
|
|
|
|
gl = NULL;
|
|
|
|
if (py != 0)
|
|
|
|
gl = &s->grid->linedata[s->grid->hsize + py - 1];
|
2010-02-02 23:56:25 +00:00
|
|
|
if (oy + py == 0 || gl == NULL || !(gl->flags & GRID_LINE_WRAPPED) ||
|
2009-10-15 01:42:07 +00:00
|
|
|
tty->cx < tty->sx || ox != 0 ||
|
|
|
|
(oy + py != tty->cy + 1 && tty->cy != s->rlower + oy))
|
2009-10-15 01:41:14 +00:00
|
|
|
tty_cursor(tty, ox, oy + py);
|
|
|
|
|
2009-03-28 10:15:01 +00:00
|
|
|
for (i = 0; i < sx; i++) {
|
2009-02-11 19:06:58 +00:00
|
|
|
gc = grid_view_peek_cell(s->grid, i, py);
|
2009-03-28 20:17:29 +00:00
|
|
|
|
|
|
|
gu = NULL;
|
|
|
|
if (gc->flags & GRID_FLAG_UTF8)
|
|
|
|
gu = grid_view_peek_utf8(s->grid, i, py);
|
2009-02-11 19:06:58 +00:00
|
|
|
|
2009-03-28 20:17:29 +00:00
|
|
|
if (screen_check_selection(s, i, py)) {
|
2009-07-28 22:41:38 +00:00
|
|
|
memcpy(&tmpgc, &s->sel.cell, sizeof tmpgc);
|
|
|
|
tmpgc.data = gc->data;
|
2009-12-04 22:14:47 +00:00
|
|
|
tmpgc.flags = gc->flags &
|
2009-10-12 00:41:00 +00:00
|
|
|
~(GRID_FLAG_FG256|GRID_FLAG_BG256);
|
|
|
|
tmpgc.flags |= s->sel.cell.flags &
|
|
|
|
(GRID_FLAG_FG256|GRID_FLAG_BG256);
|
2009-07-28 22:41:38 +00:00
|
|
|
tty_cell(tty, &tmpgc, gu);
|
2009-03-28 20:17:29 +00:00
|
|
|
} else
|
|
|
|
tty_cell(tty, gc, gu);
|
2009-03-28 10:15:01 +00:00
|
|
|
}
|
|
|
|
|
2009-10-09 13:03:28 +00:00
|
|
|
if (sx >= tty->sx) {
|
|
|
|
tty_update_mode(tty, tty->mode);
|
2009-03-28 10:15:01 +00:00
|
|
|
return;
|
2009-10-09 13:03:28 +00:00
|
|
|
}
|
2009-03-28 10:15:01 +00:00
|
|
|
tty_reset(tty);
|
2009-05-04 17:58:27 +00:00
|
|
|
|
2009-10-15 01:30:00 +00:00
|
|
|
tty_cursor(tty, ox + sx, oy + py);
|
2009-03-31 18:39:45 +00:00
|
|
|
if (screen_size_x(s) >= tty->sx && tty_term_has(tty->term, TTYC_EL))
|
2009-03-28 10:15:01 +00:00
|
|
|
tty_putcode(tty, TTYC_EL);
|
|
|
|
else {
|
|
|
|
for (i = sx; i < screen_size_x(s); i++)
|
|
|
|
tty_putc(tty, ' ');
|
2009-02-11 19:06:58 +00:00
|
|
|
}
|
2009-10-09 13:03:28 +00:00
|
|
|
tty_update_mode(tty, tty->mode);
|
2009-02-11 19:06:58 +00:00
|
|
|
}
|
|
|
|
|
2009-07-23 12:57:45 +00:00
|
|
|
void
|
2009-08-14 21:30:24 +00:00
|
|
|
tty_write(void (*cmdfn)(
|
|
|
|
struct tty *, const struct tty_ctx *), const struct tty_ctx *ctx)
|
2009-07-23 12:57:45 +00:00
|
|
|
{
|
|
|
|
struct window_pane *wp = ctx->wp;
|
|
|
|
struct client *c;
|
|
|
|
u_int i;
|
|
|
|
|
2009-11-28 14:59:26 +00:00
|
|
|
/* wp can be NULL if updating the screen but not the terminal. */
|
2009-07-23 12:57:45 +00:00
|
|
|
if (wp == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (wp->window->flags & WINDOW_REDRAW || wp->flags & PANE_REDRAW)
|
|
|
|
return;
|
2010-12-06 21:57:56 +00:00
|
|
|
if (!window_pane_visible(wp))
|
2009-07-23 12:57:45 +00:00
|
|
|
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) {
|
2010-08-11 22:16:04 +00:00
|
|
|
if (c->tty.term == NULL)
|
|
|
|
continue;
|
|
|
|
if (c->tty.flags & (TTY_FREEZE|TTY_BACKOFF))
|
2009-07-23 12:57:45 +00:00
|
|
|
continue;
|
|
|
|
cmdfn(&c->tty, ctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-25 20:08:57 +00:00
|
|
|
void
|
2009-08-14 21:30:24 +00:00
|
|
|
tty_cmd_insertcharacter(struct tty *tty, const struct tty_ctx *ctx)
|
2008-09-25 20:08:57 +00:00
|
|
|
{
|
2009-12-04 22:14:47 +00:00
|
|
|
struct window_pane *wp = ctx->wp;
|
2009-07-22 18:08:56 +00:00
|
|
|
struct screen *s = wp->screen;
|
2009-08-14 21:30:24 +00:00
|
|
|
u_int i;
|
2008-09-25 20:08:57 +00:00
|
|
|
|
2009-04-01 18:21:42 +00:00
|
|
|
if (wp->xoff != 0 || screen_size_x(s) < tty->sx) {
|
2009-07-23 12:48:18 +00:00
|
|
|
tty_draw_line(tty, wp->screen, ctx->ocy, wp->xoff, wp->yoff);
|
2009-03-31 18:39:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-09-25 20:08:57 +00:00
|
|
|
tty_reset(tty);
|
|
|
|
|
2009-12-04 22:14:47 +00:00
|
|
|
tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
|
2009-10-15 01:30:00 +00:00
|
|
|
|
2009-05-04 17:58:27 +00:00
|
|
|
if (tty_term_has(tty->term, TTYC_ICH) ||
|
2009-01-10 22:28:40 +00:00
|
|
|
tty_term_has(tty->term, TTYC_ICH1))
|
2009-07-22 18:08:56 +00:00
|
|
|
tty_emulate_repeat(tty, TTYC_ICH, TTYC_ICH1, ctx->num);
|
2009-12-04 22:14:47 +00:00
|
|
|
else if (tty_term_has(tty->term, TTYC_SMIR) &&
|
2009-11-28 14:42:21 +00:00
|
|
|
tty_term_has(tty->term, TTYC_RMIR)) {
|
2009-01-10 22:28:40 +00:00
|
|
|
tty_putcode(tty, TTYC_SMIR);
|
2009-08-14 21:30:24 +00:00
|
|
|
for (i = 0; i < ctx->num; i++)
|
2009-01-10 22:28:40 +00:00
|
|
|
tty_putc(tty, ' ');
|
|
|
|
tty_putcode(tty, TTYC_RMIR);
|
2009-11-28 14:42:21 +00:00
|
|
|
} else
|
|
|
|
tty_draw_line(tty, wp->screen, ctx->ocy, wp->xoff, wp->yoff);
|
2008-09-25 20:08:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-08-14 21:30:24 +00:00
|
|
|
tty_cmd_deletecharacter(struct tty *tty, const struct tty_ctx *ctx)
|
2008-09-25 20:08:57 +00:00
|
|
|
{
|
2009-12-04 22:14:47 +00:00
|
|
|
struct window_pane *wp = ctx->wp;
|
2009-07-22 18:08:56 +00:00
|
|
|
struct screen *s = wp->screen;
|
2008-09-25 20:08:57 +00:00
|
|
|
|
2009-08-21 21:15:00 +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-23 12:48:18 +00:00
|
|
|
tty_draw_line(tty, wp->screen, ctx->ocy, wp->xoff, wp->yoff);
|
2009-03-31 18:39:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-09-25 20:08:57 +00:00
|
|
|
tty_reset(tty);
|
|
|
|
|
2009-12-04 22:14:47 +00:00
|
|
|
tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
|
2009-10-15 01:30:00 +00:00
|
|
|
|
2009-08-21 21:15:00 +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);
|
2008-09-25 20:08:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-08-14 21:30:24 +00:00
|
|
|
tty_cmd_insertline(struct tty *tty, const struct tty_ctx *ctx)
|
2008-09-25 20:08:57 +00:00
|
|
|
{
|
2009-12-04 22:14:47 +00:00
|
|
|
struct window_pane *wp = ctx->wp;
|
2009-07-22 18:08:56 +00:00
|
|
|
struct screen *s = wp->screen;
|
2009-02-11 19:06:58 +00:00
|
|
|
|
2009-12-04 22:14:47 +00:00
|
|
|
if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
|
|
|
|
!tty_term_has(tty->term, TTYC_CSR) ||
|
2009-11-28 14:42:21 +00:00
|
|
|
!tty_term_has(tty->term, TTYC_IL1)) {
|
2009-07-23 12:48:18 +00:00
|
|
|
tty_redraw_region(tty, ctx);
|
2009-02-11 19:06:58 +00:00
|
|
|
return;
|
|
|
|
}
|
2008-09-25 20:08:57 +00:00
|
|
|
|
|
|
|
tty_reset(tty);
|
2009-02-11 18:44:08 +00:00
|
|
|
|
2009-12-04 22:14:47 +00:00
|
|
|
tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
|
|
|
|
tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
|
2008-09-25 20:08:57 +00:00
|
|
|
|
2009-07-22 18:08:56 +00:00
|
|
|
tty_emulate_repeat(tty, TTYC_IL, TTYC_IL1, ctx->num);
|
2008-09-25 20:08:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-08-14 21:30:24 +00:00
|
|
|
tty_cmd_deleteline(struct tty *tty, const struct tty_ctx *ctx)
|
2008-09-25 20:08:57 +00:00
|
|
|
{
|
2009-12-04 22:14:47 +00:00
|
|
|
struct window_pane *wp = ctx->wp;
|
2009-07-22 18:08:56 +00:00
|
|
|
struct screen *s = wp->screen;
|
2009-02-11 19:06:58 +00:00
|
|
|
|
2009-12-04 22:14:47 +00:00
|
|
|
if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
|
2009-11-28 14:42:21 +00:00
|
|
|
!tty_term_has(tty->term, TTYC_CSR) ||
|
|
|
|
!tty_term_has(tty->term, TTYC_DL1)) {
|
2009-07-23 12:48:18 +00:00
|
|
|
tty_redraw_region(tty, ctx);
|
2009-02-11 19:06:58 +00:00
|
|
|
return;
|
|
|
|
}
|
2008-09-25 20:08:57 +00:00
|
|
|
|
|
|
|
tty_reset(tty);
|
2009-02-11 18:44:08 +00:00
|
|
|
|
2009-12-04 22:14:47 +00:00
|
|
|
tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
|
|
|
|
tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
|
2008-09-25 20:08:57 +00:00
|
|
|
|
2009-07-22 18:08:56 +00:00
|
|
|
tty_emulate_repeat(tty, TTYC_DL, TTYC_DL1, ctx->num);
|
2008-09-25 20:08:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-08-14 21:30:24 +00:00
|
|
|
tty_cmd_clearline(struct tty *tty, const struct tty_ctx *ctx)
|
2008-09-25 20:08:57 +00:00
|
|
|
{
|
2009-12-04 22:14:47 +00:00
|
|
|
struct window_pane *wp = ctx->wp;
|
2009-07-22 18:08:56 +00:00
|
|
|
struct screen *s = wp->screen;
|
|
|
|
u_int i;
|
2008-09-25 20:08:57 +00:00
|
|
|
|
|
|
|
tty_reset(tty);
|
|
|
|
|
2009-12-04 22:14:47 +00:00
|
|
|
tty_cursor_pane(tty, ctx, 0, ctx->ocy);
|
2009-10-15 01:30:00 +00:00
|
|
|
|
2009-04-01 18:21:42 +00:00
|
|
|
if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
|
|
|
|
tty_term_has(tty->term, TTYC_EL)) {
|
2009-01-09 23:57:42 +00:00
|
|
|
tty_putcode(tty, TTYC_EL);
|
2008-09-25 20:08:57 +00:00
|
|
|
} else {
|
|
|
|
for (i = 0; i < screen_size_x(s); i++)
|
|
|
|
tty_putc(tty, ' ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-08-14 21:30:24 +00:00
|
|
|
tty_cmd_clearendofline(struct tty *tty, const struct tty_ctx *ctx)
|
2008-09-25 20:08:57 +00:00
|
|
|
{
|
2009-12-04 22:14:47 +00:00
|
|
|
struct window_pane *wp = ctx->wp;
|
2009-07-22 18:08:56 +00:00
|
|
|
struct screen *s = wp->screen;
|
|
|
|
u_int i;
|
2008-09-26 06:45:28 +00:00
|
|
|
|
2008-09-25 20:08:57 +00:00
|
|
|
tty_reset(tty);
|
|
|
|
|
2009-10-15 01:30:00 +00:00
|
|
|
tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
|
|
|
|
|
2009-04-01 18:21:42 +00:00
|
|
|
if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
|
|
|
|
tty_term_has(tty->term, TTYC_EL))
|
2009-01-09 23:57:42 +00:00
|
|
|
tty_putcode(tty, TTYC_EL);
|
2009-01-10 01:51:22 +00:00
|
|
|
else {
|
2009-07-23 12:48:18 +00:00
|
|
|
for (i = ctx->ocx; i < screen_size_x(s); i++)
|
2008-09-25 20:08:57 +00:00
|
|
|
tty_putc(tty, ' ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-08-14 21:30:24 +00:00
|
|
|
tty_cmd_clearstartofline(struct tty *tty, const struct tty_ctx *ctx)
|
2008-09-25 20:08:57 +00:00
|
|
|
{
|
2009-12-04 22:14:47 +00:00
|
|
|
struct window_pane *wp = ctx->wp;
|
2009-07-22 18:08:56 +00:00
|
|
|
u_int i;
|
2008-09-25 20:08:57 +00:00
|
|
|
|
|
|
|
tty_reset(tty);
|
|
|
|
|
2009-04-01 18:21:42 +00:00
|
|
|
if (wp->xoff == 0 && tty_term_has(tty->term, TTYC_EL1)) {
|
2009-10-15 01:30:00 +00:00
|
|
|
tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
|
2009-01-09 23:57:42 +00:00
|
|
|
tty_putcode(tty, TTYC_EL1);
|
2009-02-11 18:44:08 +00:00
|
|
|
} else {
|
2009-10-15 01:30:00 +00:00
|
|
|
tty_cursor_pane(tty, ctx, 0, ctx->ocy);
|
2009-07-23 12:48:18 +00:00
|
|
|
for (i = 0; i < ctx->ocx + 1; i++)
|
2008-09-25 20:08:57 +00:00
|
|
|
tty_putc(tty, ' ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-08-14 21:30:24 +00:00
|
|
|
tty_cmd_reverseindex(struct tty *tty, const struct tty_ctx *ctx)
|
2008-09-25 20:08:57 +00:00
|
|
|
{
|
2009-12-04 22:14:47 +00:00
|
|
|
struct window_pane *wp = ctx->wp;
|
2009-07-22 18:08:56 +00:00
|
|
|
struct screen *s = wp->screen;
|
2009-02-11 19:06:58 +00:00
|
|
|
|
2009-10-23 17:23:52 +00:00
|
|
|
if (ctx->ocy != ctx->orupper)
|
|
|
|
return;
|
|
|
|
|
2009-12-04 22:14:47 +00:00
|
|
|
if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
|
2009-11-13 16:57:21 +00:00
|
|
|
!tty_term_has(tty->term, TTYC_CSR) ||
|
|
|
|
!tty_term_has(tty->term, TTYC_RI)) {
|
2009-07-23 12:48:18 +00:00
|
|
|
tty_redraw_region(tty, ctx);
|
2009-02-12 00:03:58 +00:00
|
|
|
return;
|
2009-02-11 19:06:58 +00:00
|
|
|
}
|
2009-02-11 07:02:34 +00:00
|
|
|
|
2009-10-23 17:23:52 +00:00
|
|
|
tty_reset(tty);
|
2009-12-04 22:14:47 +00:00
|
|
|
|
2009-10-23 17:23:52 +00:00
|
|
|
tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
|
|
|
|
tty_cursor_pane(tty, ctx, ctx->ocx, ctx->orupper);
|
2009-12-04 22:14:47 +00:00
|
|
|
|
2009-10-23 17:23:52 +00:00
|
|
|
tty_putcode(tty, TTYC_RI);
|
2008-09-25 20:08:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-08-14 21:30:24 +00:00
|
|
|
tty_cmd_linefeed(struct tty *tty, const struct tty_ctx *ctx)
|
2008-09-25 20:08:57 +00:00
|
|
|
{
|
2009-12-04 22:14:47 +00:00
|
|
|
struct window_pane *wp = ctx->wp;
|
2009-07-22 18:08:56 +00:00
|
|
|
struct screen *s = wp->screen;
|
2009-02-11 19:06:58 +00:00
|
|
|
|
2009-10-23 17:23:52 +00:00
|
|
|
if (ctx->ocy != ctx->orlower)
|
|
|
|
return;
|
|
|
|
|
2009-12-04 22:14:47 +00:00
|
|
|
if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
|
2009-04-01 18:21:42 +00:00
|
|
|
!tty_term_has(tty->term, TTYC_CSR)) {
|
2009-07-23 12:48:18 +00:00
|
|
|
tty_redraw_region(tty, ctx);
|
2009-04-01 18:21:42 +00:00
|
|
|
return;
|
2009-02-11 19:06:58 +00:00
|
|
|
}
|
2009-02-11 07:02:34 +00:00
|
|
|
|
2009-10-23 17:11:26 +00:00
|
|
|
/*
|
|
|
|
* If this line wrapped naturally (ctx->num is nonzero), don't do
|
|
|
|
* anything - the cursor can just be moved to the last cell and wrap
|
|
|
|
* naturally.
|
|
|
|
*/
|
|
|
|
if (ctx->num && !(tty->term->flags & TERM_EARLYWRAP))
|
|
|
|
return;
|
|
|
|
|
2009-10-23 17:23:52 +00:00
|
|
|
tty_reset(tty);
|
2009-12-04 22:14:47 +00:00
|
|
|
|
2009-10-23 17:23:52 +00:00
|
|
|
tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
|
|
|
|
tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
|
2009-12-04 22:14:47 +00:00
|
|
|
|
2009-10-23 17:23:52 +00:00
|
|
|
tty_putc(tty, '\n');
|
2008-09-25 20:08:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-08-14 21:30:24 +00:00
|
|
|
tty_cmd_clearendofscreen(struct tty *tty, const struct tty_ctx *ctx)
|
2008-09-25 20:08:57 +00:00
|
|
|
{
|
2009-12-04 22:14:47 +00:00
|
|
|
struct window_pane *wp = ctx->wp;
|
2009-07-22 18:08:56 +00:00
|
|
|
struct screen *s = wp->screen;
|
|
|
|
u_int i, j;
|
2008-09-25 20:08:57 +00:00
|
|
|
|
|
|
|
tty_reset(tty);
|
|
|
|
|
2009-10-15 01:26:50 +00:00
|
|
|
tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
|
2009-10-15 01:30:00 +00:00
|
|
|
tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
|
2009-10-15 01:26:50 +00:00
|
|
|
|
2009-04-01 18:21:42 +00:00
|
|
|
if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
|
2009-05-04 17:52:14 +00:00
|
|
|
tty_term_has(tty->term, TTYC_EL)) {
|
2009-02-11 18:44:08 +00:00
|
|
|
tty_putcode(tty, TTYC_EL);
|
2009-07-23 12:48:18 +00:00
|
|
|
if (ctx->ocy != screen_size_y(s) - 1) {
|
2009-10-15 01:30:00 +00:00
|
|
|
tty_cursor_pane(tty, ctx, 0, ctx->ocy + 1);
|
2009-07-23 12:48:18 +00:00
|
|
|
for (i = ctx->ocy + 1; i < screen_size_y(s); i++) {
|
2009-02-13 16:53:14 +00:00
|
|
|
tty_putcode(tty, TTYC_EL);
|
|
|
|
if (i == screen_size_y(s) - 1)
|
|
|
|
continue;
|
2009-01-09 23:57:42 +00:00
|
|
|
tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
|
2009-02-11 18:44:08 +00:00
|
|
|
tty->cy++;
|
|
|
|
}
|
2007-12-06 09:46:23 +00:00
|
|
|
}
|
2008-09-25 20:08:57 +00:00
|
|
|
} else {
|
2009-07-23 12:48:18 +00:00
|
|
|
for (i = ctx->ocx; i < screen_size_x(s); i++)
|
2008-09-25 20:08:57 +00:00
|
|
|
tty_putc(tty, ' ');
|
2009-11-08 23:34:47 +00:00
|
|
|
for (j = ctx->ocy + 1; j < screen_size_y(s); j++) {
|
2009-10-15 01:30:00 +00:00
|
|
|
tty_cursor_pane(tty, ctx, 0, j);
|
2008-09-25 20:08:57 +00:00
|
|
|
for (i = 0; i < screen_size_x(s); i++)
|
2007-12-06 09:46:23 +00:00
|
|
|
tty_putc(tty, ' ');
|
|
|
|
}
|
2008-09-25 20:08:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-08-14 21:30:24 +00:00
|
|
|
tty_cmd_clearstartofscreen(struct tty *tty, const struct tty_ctx *ctx)
|
2008-09-25 20:08:57 +00:00
|
|
|
{
|
2009-12-04 22:14:47 +00:00
|
|
|
struct window_pane *wp = ctx->wp;
|
2009-07-22 18:08:56 +00:00
|
|
|
struct screen *s = wp->screen;
|
|
|
|
u_int i, j;
|
2008-09-25 20:08:57 +00:00
|
|
|
|
|
|
|
tty_reset(tty);
|
|
|
|
|
2009-10-15 01:26:50 +00:00
|
|
|
tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
|
2009-10-15 01:30:00 +00:00
|
|
|
tty_cursor_pane(tty, ctx, 0, 0);
|
2009-10-15 01:26:50 +00:00
|
|
|
|
2009-04-01 18:21:42 +00:00
|
|
|
if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
|
|
|
|
tty_term_has(tty->term, TTYC_EL)) {
|
2009-07-23 12:48:18 +00:00
|
|
|
for (i = 0; i < ctx->ocy; i++) {
|
2009-01-09 23:57:42 +00:00
|
|
|
tty_putcode(tty, TTYC_EL);
|
|
|
|
tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
|
2009-02-11 18:44:08 +00:00
|
|
|
tty->cy++;
|
2009-01-09 23:57:42 +00:00
|
|
|
}
|
2008-09-25 20:08:57 +00:00
|
|
|
} else {
|
2009-07-23 12:48:18 +00:00
|
|
|
for (j = 0; j < ctx->ocy; j++) {
|
2009-10-15 01:30:00 +00:00
|
|
|
tty_cursor_pane(tty, ctx, 0, j);
|
2007-12-06 09:46:23 +00:00
|
|
|
for (i = 0; i < screen_size_x(s); i++)
|
|
|
|
tty_putc(tty, ' ');
|
2008-09-26 06:45:28 +00:00
|
|
|
}
|
2008-09-25 20:08:57 +00:00
|
|
|
}
|
2009-07-23 12:48:18 +00:00
|
|
|
for (i = 0; i <= ctx->ocx; i++)
|
2008-09-25 20:08:57 +00:00
|
|
|
tty_putc(tty, ' ');
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-08-14 21:30:24 +00:00
|
|
|
tty_cmd_clearscreen(struct tty *tty, const struct tty_ctx *ctx)
|
2008-09-25 20:08:57 +00:00
|
|
|
{
|
2009-12-04 22:14:47 +00:00
|
|
|
struct window_pane *wp = ctx->wp;
|
2009-07-22 18:08:56 +00:00
|
|
|
struct screen *s = wp->screen;
|
|
|
|
u_int i, j;
|
2008-09-25 20:08:57 +00:00
|
|
|
|
|
|
|
tty_reset(tty);
|
|
|
|
|
2009-10-15 01:26:50 +00:00
|
|
|
tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
|
2009-10-15 01:30:00 +00:00
|
|
|
tty_cursor_pane(tty, ctx, 0, 0);
|
2009-10-15 01:26:50 +00:00
|
|
|
|
2009-04-01 18:21:42 +00:00
|
|
|
if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
|
|
|
|
tty_term_has(tty->term, TTYC_EL)) {
|
2008-09-25 20:08:57 +00:00
|
|
|
for (i = 0; i < screen_size_y(s); i++) {
|
2009-01-09 23:57:42 +00:00
|
|
|
tty_putcode(tty, TTYC_EL);
|
2009-02-11 18:44:08 +00:00
|
|
|
if (i != screen_size_y(s) - 1) {
|
2009-01-09 23:57:42 +00:00
|
|
|
tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
|
2009-02-11 18:44:08 +00:00
|
|
|
tty->cy++;
|
|
|
|
}
|
2007-12-03 10:47:27 +00:00
|
|
|
}
|
2008-09-25 20:08:57 +00:00
|
|
|
} else {
|
|
|
|
for (j = 0; j < screen_size_y(s); j++) {
|
2009-10-15 01:30:00 +00:00
|
|
|
tty_cursor_pane(tty, ctx, 0, j);
|
2008-09-25 20:08:57 +00:00
|
|
|
for (i = 0; i < screen_size_x(s); i++)
|
2007-12-03 10:47:27 +00:00
|
|
|
tty_putc(tty, ' ');
|
|
|
|
}
|
2007-11-27 19:23:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-25 15:51:54 +00:00
|
|
|
void
|
2009-08-14 21:30:24 +00:00
|
|
|
tty_cmd_alignmenttest(struct tty *tty, const struct tty_ctx *ctx)
|
2009-06-25 15:51:54 +00:00
|
|
|
{
|
2009-07-22 18:08:56 +00:00
|
|
|
struct window_pane *wp = ctx->wp;
|
|
|
|
struct screen *s = wp->screen;
|
|
|
|
u_int i, j;
|
2009-06-25 15:51:54 +00:00
|
|
|
|
|
|
|
tty_reset(tty);
|
|
|
|
|
2009-10-15 01:26:50 +00:00
|
|
|
tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
|
2009-06-25 15:51:54 +00:00
|
|
|
|
|
|
|
for (j = 0; j < screen_size_y(s); j++) {
|
2009-10-15 01:30:00 +00:00
|
|
|
tty_cursor_pane(tty, ctx, 0, j);
|
2009-06-25 15:51:54 +00:00
|
|
|
for (i = 0; i < screen_size_x(s); i++)
|
|
|
|
tty_putc(tty, 'E');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-27 19:23:34 +00:00
|
|
|
void
|
2009-08-14 21:30:24 +00:00
|
|
|
tty_cmd_cell(struct tty *tty, const struct tty_ctx *ctx)
|
2009-02-11 06:50:16 +00:00
|
|
|
{
|
2009-10-15 01:41:14 +00:00
|
|
|
struct window_pane *wp = ctx->wp;
|
|
|
|
struct screen *s = wp->screen;
|
2009-10-23 17:28:29 +00:00
|
|
|
u_int cx;
|
2009-10-15 01:41:14 +00:00
|
|
|
|
|
|
|
tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
|
|
|
|
|
2009-10-23 17:25:51 +00:00
|
|
|
/* Is the cursor in the very last position? */
|
|
|
|
if (ctx->ocx > wp->sx - ctx->last_width) {
|
|
|
|
if (wp->xoff != 0 || wp->sx != tty->sx) {
|
|
|
|
/*
|
|
|
|
* The pane doesn't fill the entire line, the linefeed
|
|
|
|
* will already have happened, so just move the cursor.
|
|
|
|
*/
|
|
|
|
tty_cursor_pane(tty, ctx, 0, ctx->ocy + 1);
|
|
|
|
} else if (tty->cx < tty->sx) {
|
|
|
|
/*
|
|
|
|
* The cursor isn't in the last position already, so
|
|
|
|
* move as far left as possinble and redraw the last
|
|
|
|
* cell to move into the last position.
|
|
|
|
*/
|
2009-10-23 17:06:23 +00:00
|
|
|
cx = screen_size_x(s) - ctx->last_width;
|
|
|
|
tty_cursor_pane(tty, ctx, cx, ctx->ocy);
|
|
|
|
tty_cell(tty, &ctx->last_cell, &ctx->last_utf8);
|
|
|
|
}
|
|
|
|
} else
|
2009-10-15 01:41:14 +00:00
|
|
|
tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
|
2009-02-11 17:04:39 +00:00
|
|
|
|
2009-07-22 18:08:56 +00:00
|
|
|
tty_cell(tty, ctx->cell, ctx->utf8);
|
2009-03-28 20:17:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-08-14 21:30:24 +00:00
|
|
|
tty_cmd_utf8character(struct tty *tty, const struct tty_ctx *ctx)
|
2009-03-28 20:17:29 +00:00
|
|
|
{
|
2009-10-23 17:13:10 +00:00
|
|
|
struct window_pane *wp = ctx->wp;
|
2009-03-28 20:17:29 +00:00
|
|
|
|
2009-10-23 17:13:10 +00:00
|
|
|
/*
|
|
|
|
* Cannot rely on not being a partial character, so just redraw the
|
|
|
|
* whole line.
|
|
|
|
*/
|
2009-12-04 22:14:47 +00:00
|
|
|
tty_draw_line(tty, wp->screen, ctx->ocy, wp->xoff, wp->yoff);
|
2009-02-11 06:50:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-03-28 20:17:29 +00:00
|
|
|
tty_cell(
|
|
|
|
struct tty *tty, const struct grid_cell *gc, const struct grid_utf8 *gu)
|
2007-11-27 19:23:34 +00:00
|
|
|
{
|
2009-03-28 20:17:29 +00:00
|
|
|
u_int i;
|
2008-09-25 20:08:57 +00:00
|
|
|
|
2009-02-12 00:18:05 +00:00
|
|
|
/* Skip last character if terminal is stupid. */
|
2009-05-04 17:58:27 +00:00
|
|
|
if (tty->term->flags & TERM_EARLYWRAP &&
|
2009-02-12 00:18:05 +00:00
|
|
|
tty->cy == tty->sy - 1 && tty->cx == tty->sx - 1)
|
|
|
|
return;
|
|
|
|
|
2008-09-25 20:08:57 +00:00
|
|
|
/* If this is a padding character, do nothing. */
|
|
|
|
if (gc->flags & GRID_FLAG_PADDING)
|
2007-11-27 19:23:34 +00:00
|
|
|
return;
|
|
|
|
|
2008-09-25 20:08:57 +00:00
|
|
|
/* Set the attributes. */
|
|
|
|
tty_attributes(tty, gc);
|
|
|
|
|
2009-01-18 21:46:30 +00:00
|
|
|
/* If not UTF-8, write directly. */
|
|
|
|
if (!(gc->flags & GRID_FLAG_UTF8)) {
|
2009-03-28 20:17:29 +00:00
|
|
|
if (gc->data < 0x20 || gc->data == 0x7f)
|
2009-01-18 21:46:30 +00:00
|
|
|
return;
|
2009-03-28 20:17:29 +00:00
|
|
|
tty_putc(tty, gc->data);
|
2008-09-25 20:08:57 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-01-18 21:46:30 +00:00
|
|
|
/* If the terminal doesn't support UTF-8, write underscores. */
|
2008-09-25 20:08:57 +00:00
|
|
|
if (!(tty->flags & TTY_UTF8)) {
|
2009-03-28 20:17:29 +00:00
|
|
|
for (i = 0; i < gu->width; i++)
|
2008-09-25 20:08:57 +00:00
|
|
|
tty_putc(tty, '_');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-03-29 11:18:28 +00:00
|
|
|
/* Otherwise, write UTF-8. */
|
2009-06-25 16:01:26 +00:00
|
|
|
tty_pututf8(tty, gu);
|
2008-09-25 20:08:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tty_reset(struct tty *tty)
|
|
|
|
{
|
2009-03-28 16:30:05 +00:00
|
|
|
struct grid_cell *gc = &tty->cell;
|
2008-09-25 20:08:57 +00:00
|
|
|
|
2009-03-28 16:30:05 +00:00
|
|
|
if (memcmp(gc, &grid_default_cell, sizeof *gc) == 0)
|
2008-09-25 20:08:57 +00:00
|
|
|
return;
|
2008-09-26 06:45:28 +00:00
|
|
|
|
2010-09-18 15:43:53 +00:00
|
|
|
if ((gc->attr & GRID_ATTR_CHARSET) && tty_use_acs(tty))
|
2009-01-09 23:57:42 +00:00
|
|
|
tty_putcode(tty, TTYC_RMACS);
|
|
|
|
tty_putcode(tty, TTYC_SGR0);
|
2009-03-28 16:30:05 +00:00
|
|
|
memcpy(gc, &grid_default_cell, sizeof *gc);
|
2008-09-25 20:08:57 +00:00
|
|
|
}
|
|
|
|
|
2009-10-15 01:28:14 +00:00
|
|
|
/* Set region inside pane. */
|
2009-01-11 23:31:46 +00:00
|
|
|
void
|
2009-10-15 01:26:50 +00:00
|
|
|
tty_region_pane(
|
|
|
|
struct tty *tty, const struct tty_ctx *ctx, u_int rupper, u_int rlower)
|
|
|
|
{
|
2009-12-04 22:14:47 +00:00
|
|
|
struct window_pane *wp = ctx->wp;
|
2009-10-15 01:26:50 +00:00
|
|
|
|
2009-10-15 01:28:14 +00:00
|
|
|
tty_region(tty, wp->yoff + rupper, wp->yoff + rlower);
|
2009-10-15 01:26:50 +00:00
|
|
|
}
|
|
|
|
|
2009-10-15 01:28:14 +00:00
|
|
|
/* Set region at absolute position. */
|
2009-10-15 01:26:50 +00:00
|
|
|
void
|
2009-10-15 01:28:14 +00:00
|
|
|
tty_region(struct tty *tty, u_int rupper, u_int rlower)
|
2009-01-11 23:31:46 +00:00
|
|
|
{
|
2009-10-15 01:26:50 +00:00
|
|
|
if (tty->rlower == rlower && tty->rupper == rupper)
|
|
|
|
return;
|
2009-02-11 19:06:58 +00:00
|
|
|
if (!tty_term_has(tty->term, TTYC_CSR))
|
|
|
|
return;
|
2009-10-15 01:26:50 +00:00
|
|
|
|
|
|
|
tty->rupper = rupper;
|
|
|
|
tty->rlower = rlower;
|
2009-10-15 01:33:21 +00:00
|
|
|
|
2009-10-23 17:22:39 +00:00
|
|
|
/*
|
|
|
|
* Some terminals (such as PuTTY) do not correctly reset the cursor to
|
|
|
|
* 0,0 if it is beyond the last column (they do not reset their wrap
|
|
|
|
* flag so further output causes a line feed). As a workaround, do an
|
|
|
|
* explicit move to 0 first.
|
|
|
|
*/
|
|
|
|
if (tty->cx >= tty->sx)
|
|
|
|
tty_cursor(tty, 0, tty->cy);
|
|
|
|
|
2009-10-15 01:26:50 +00:00
|
|
|
tty_putcode2(tty, TTYC_CSR, tty->rupper, tty->rlower);
|
2009-12-04 22:17:26 +00:00
|
|
|
tty_cursor(tty, 0, 0);
|
2009-01-11 23:31:46 +00:00
|
|
|
}
|
|
|
|
|
2009-10-15 01:33:21 +00:00
|
|
|
/* Move cursor inside pane. */
|
2009-01-11 23:31:46 +00:00
|
|
|
void
|
2009-10-15 01:30:00 +00:00
|
|
|
tty_cursor_pane(struct tty *tty, const struct tty_ctx *ctx, u_int cx, u_int cy)
|
|
|
|
{
|
2009-12-04 22:14:47 +00:00
|
|
|
struct window_pane *wp = ctx->wp;
|
2009-10-15 01:30:00 +00:00
|
|
|
|
|
|
|
tty_cursor(tty, wp->xoff + cx, wp->yoff + cy);
|
|
|
|
}
|
|
|
|
|
2009-10-15 01:33:21 +00:00
|
|
|
/* Move cursor to absolute position. */
|
2009-10-15 01:30:00 +00:00
|
|
|
void
|
|
|
|
tty_cursor(struct tty *tty, u_int cx, u_int cy)
|
2009-01-11 23:31:46 +00:00
|
|
|
{
|
2009-10-15 01:33:21 +00:00
|
|
|
struct tty_term *term = tty->term;
|
|
|
|
u_int thisx, thisy;
|
|
|
|
int change;
|
2009-12-04 22:14:47 +00:00
|
|
|
|
2009-10-15 01:33:21 +00:00
|
|
|
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-15 01:34:28 +00:00
|
|
|
/* Very end of the line, just use absolute movement. */
|
|
|
|
if (thisx > tty->sx - 1)
|
|
|
|
goto absolute;
|
|
|
|
|
2009-10-15 01:33:21 +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. */
|
2009-10-15 01:44:15 +00:00
|
|
|
if (cx == 0 && cy == thisy + 1 && thisy != tty->rlower) {
|
2009-01-18 21:35:09 +00:00
|
|
|
tty_putc(tty, '\r');
|
2009-10-15 01:33:21 +00:00
|
|
|
tty_putc(tty, '\n');
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2009-10-15 01:38:09 +00:00
|
|
|
/* Moving column or row. */
|
2009-10-15 01:33:21 +00:00
|
|
|
if (cy == thisy) {
|
2009-10-15 01:38:09 +00:00
|
|
|
/*
|
|
|
|
* Moving column only, row staying the same.
|
|
|
|
*/
|
|
|
|
|
2009-10-15 01:33:21 +00:00
|
|
|
/* 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.
|
|
|
|
*/
|
2010-06-06 00:27:08 +00:00
|
|
|
if ((u_int) abs(change) > cx && tty_term_has(term, TTYC_HPA)) {
|
2009-10-15 01:33:21 +00:00
|
|
|
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;
|
|
|
|
}
|
2009-10-15 01:38:09 +00:00
|
|
|
} else if (cx == thisx) {
|
|
|
|
/*
|
|
|
|
* Moving row only, column staying the same.
|
|
|
|
*/
|
2009-10-15 01:33:21 +00:00
|
|
|
|
|
|
|
/* One above. */
|
2009-12-04 22:14:47 +00:00
|
|
|
if (thisy != tty->rupper &&
|
2009-10-15 01:33:21 +00:00
|
|
|
cy == thisy - 1 && tty_term_has(term, TTYC_CUU1)) {
|
|
|
|
tty_putcode(tty, TTYC_CUU1);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* One below. */
|
2009-10-23 17:03:48 +00:00
|
|
|
if (thisy != tty->rlower &&
|
2009-10-15 01:33:21 +00:00
|
|
|
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-12-04 22:14:47 +00:00
|
|
|
* Try to use VPA if change is larger than absolute or if this
|
|
|
|
* change would cross the scroll region, otherwise use CUU/CUD.
|
2009-10-15 01:33:21 +00:00
|
|
|
*/
|
2010-06-06 00:27:08 +00:00
|
|
|
if ((u_int) abs(change) > cy ||
|
2009-10-15 01:33:21 +00:00
|
|
|
(change < 0 && cy - change > tty->rlower) ||
|
2009-10-15 01:36:53 +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-15 01:33:21 +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-01-11 23:31:46 +00:00
|
|
|
}
|
2009-10-15 01:33:21 +00:00
|
|
|
|
2009-10-15 01:34:28 +00:00
|
|
|
absolute:
|
2009-10-15 01:33:21 +00:00
|
|
|
/* Absolute movement. */
|
|
|
|
tty_putcode2(tty, TTYC_CUP, cy, cx);
|
|
|
|
|
|
|
|
out:
|
|
|
|
tty->cx = cx;
|
|
|
|
tty->cy = cy;
|
2009-01-11 23:31:46 +00:00
|
|
|
}
|
|
|
|
|
2008-09-25 20:08:57 +00:00
|
|
|
void
|
|
|
|
tty_attributes(struct tty *tty, const struct grid_cell *gc)
|
|
|
|
{
|
2009-10-28 23:17:28 +00:00
|
|
|
struct grid_cell *tc = &tty->cell, gc2;
|
2010-03-08 14:53:49 +00:00
|
|
|
u_char changed;
|
2009-10-28 23:15:32 +00:00
|
|
|
|
2010-03-08 14:53:49 +00:00
|
|
|
memcpy(&gc2, gc, sizeof gc2);
|
2009-10-28 23:17:28 +00:00
|
|
|
|
2009-08-09 16:50:57 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
if (!tty_term_has(tty->term, TTYC_SETAB)) {
|
2010-03-08 14:53:49 +00:00
|
|
|
if (gc2.attr & GRID_ATTR_REVERSE) {
|
|
|
|
if (gc2.fg != 7 && gc2.fg != 8)
|
2009-11-04 23:09:48 +00:00
|
|
|
gc2.attr &= ~GRID_ATTR_REVERSE;
|
2009-08-09 16:50:57 +00:00
|
|
|
} else {
|
2010-03-08 14:53:49 +00:00
|
|
|
if (gc2.bg != 0 && gc2.bg != 8)
|
2009-11-04 23:09:48 +00:00
|
|
|
gc2.attr |= GRID_ATTR_REVERSE;
|
2009-08-09 16:50:57 +00:00
|
|
|
}
|
|
|
|
}
|
2008-09-25 20:08:57 +00:00
|
|
|
|
2010-03-08 14:53:49 +00:00
|
|
|
/* Fix up the colours if necessary. */
|
|
|
|
tty_check_fg(tty, &gc2);
|
|
|
|
tty_check_bg(tty, &gc2);
|
|
|
|
|
2009-11-04 23:09:48 +00:00
|
|
|
/* If any bits are being cleared, reset everything. */
|
2010-03-08 14:53:49 +00:00
|
|
|
if (tc->attr & ~gc2.attr)
|
2009-11-04 23:09:48 +00:00
|
|
|
tty_reset(tty);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the colours. This may call tty_reset() (so it comes next) and
|
|
|
|
* may add to (NOT remove) the desired attributes by changing new_attr.
|
|
|
|
*/
|
2010-03-08 14:53:49 +00:00
|
|
|
tty_colours(tty, &gc2);
|
2009-11-04 23:09:48 +00:00
|
|
|
|
2008-09-08 22:03:56 +00:00
|
|
|
/* Filter out attribute bits already set. */
|
2010-03-08 14:53:49 +00:00
|
|
|
changed = gc2.attr & ~tc->attr;
|
|
|
|
tc->attr = gc2.attr;
|
2007-11-27 19:23:34 +00:00
|
|
|
|
2008-09-08 22:03:56 +00:00
|
|
|
/* Set the attributes. */
|
2009-01-09 23:57:42 +00: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 19:01:11 +00: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))
|
2009-05-04 17:58:27 +00:00
|
|
|
tty_putcode(tty, TTYC_SMSO);
|
2009-01-19 19:01:11 +00:00
|
|
|
}
|
2009-01-09 23:57:42 +00:00
|
|
|
if (changed & GRID_ATTR_HIDDEN)
|
|
|
|
tty_putcode(tty, TTYC_INVIS);
|
2010-09-18 15:43:53 +00:00
|
|
|
if ((changed & GRID_ATTR_CHARSET) && tty_use_acs(tty))
|
2009-01-09 23:57:42 +00:00
|
|
|
tty_putcode(tty, TTYC_SMACS);
|
2008-09-08 22:03:56 +00:00
|
|
|
}
|
|
|
|
|
2009-10-28 23:15:32 +00:00
|
|
|
void
|
2010-03-08 14:53:49 +00:00
|
|
|
tty_colours(struct tty *tty, const struct grid_cell *gc)
|
2009-03-07 10:29:06 +00:00
|
|
|
{
|
2009-10-28 23:15:32 +00:00
|
|
|
struct grid_cell *tc = &tty->cell;
|
2009-10-28 23:16:30 +00:00
|
|
|
u_char fg = gc->fg, bg = gc->bg, flags = gc->flags;
|
|
|
|
int have_ax, fg_default, bg_default;
|
2009-05-04 17:58:27 +00:00
|
|
|
|
2009-10-28 23:15:32 +00:00
|
|
|
/* No changes? Nothing is necessary. */
|
2009-10-28 23:16:30 +00:00
|
|
|
if (fg == tc->fg && bg == tc->bg &&
|
|
|
|
((flags ^ tc->flags) & (GRID_FLAG_FG256|GRID_FLAG_BG256)) == 0)
|
2009-10-28 23:17:28 +00:00
|
|
|
return;
|
2009-03-07 10:29:06 +00:00
|
|
|
|
2009-10-28 23:15:32 +00:00
|
|
|
/*
|
|
|
|
* Is either the default colour? This is handled specially because the
|
|
|
|
* best solution might be to reset both colours to default, in which
|
|
|
|
* case if only one is default need to fall onward to set the other
|
|
|
|
* colour.
|
|
|
|
*/
|
2009-10-28 23:16:30 +00:00
|
|
|
fg_default = (fg == 8 && !(flags & GRID_FLAG_FG256));
|
|
|
|
bg_default = (bg == 8 && !(flags & GRID_FLAG_BG256));
|
2009-10-28 23:15:32 +00:00
|
|
|
if (fg_default || bg_default) {
|
|
|
|
/*
|
|
|
|
* If don't have AX but do have op, send sgr0 (op can't
|
|
|
|
* actually be used because it is sometimes the same as sgr0
|
|
|
|
* and sometimes isn't). This resets both colours to default.
|
|
|
|
*
|
|
|
|
* Otherwise, try to set the default colour only as needed.
|
|
|
|
*/
|
|
|
|
have_ax = tty_term_has(tty->term, TTYC_AX);
|
|
|
|
if (!have_ax && tty_term_has(tty->term, TTYC_OP))
|
|
|
|
tty_reset(tty);
|
|
|
|
else {
|
|
|
|
if (fg_default &&
|
2009-12-26 23:50:15 +00:00
|
|
|
(tc->fg != 8 || tc->flags & GRID_FLAG_FG256)) {
|
2009-10-28 23:15:32 +00:00
|
|
|
if (have_ax)
|
|
|
|
tty_puts(tty, "\033[39m");
|
2009-12-26 23:50:15 +00:00
|
|
|
else if (tc->fg != 7 ||
|
|
|
|
tc->flags & GRID_FLAG_FG256)
|
2009-10-28 23:15:32 +00:00
|
|
|
tty_putcode1(tty, TTYC_SETAF, 7);
|
|
|
|
tc->fg = 8;
|
|
|
|
tc->flags &= ~GRID_FLAG_FG256;
|
|
|
|
}
|
|
|
|
if (bg_default &&
|
2009-12-26 23:50:15 +00:00
|
|
|
(tc->bg != 8 || tc->flags & GRID_FLAG_BG256)) {
|
2009-12-04 22:14:47 +00:00
|
|
|
if (have_ax)
|
2009-10-28 23:15:32 +00:00
|
|
|
tty_puts(tty, "\033[49m");
|
2009-12-26 23:50:15 +00:00
|
|
|
else if (tc->bg != 0 ||
|
|
|
|
tc->flags & GRID_FLAG_BG256)
|
2009-10-28 23:15:32 +00:00
|
|
|
tty_putcode1(tty, TTYC_SETAB, 0);
|
|
|
|
tc->bg = 8;
|
|
|
|
tc->flags &= ~GRID_FLAG_BG256;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-05-04 17:58:27 +00:00
|
|
|
|
2009-10-28 23:15:32 +00:00
|
|
|
/* Set the foreground colour. */
|
|
|
|
if (!fg_default && (fg != tc->fg ||
|
2009-10-28 23:16:30 +00:00
|
|
|
((flags & GRID_FLAG_FG256) != (tc->flags & GRID_FLAG_FG256))))
|
2010-03-08 14:53:49 +00:00
|
|
|
tty_colours_fg(tty, gc);
|
2009-03-07 10:29:06 +00:00
|
|
|
|
2009-10-28 23:15:32 +00:00
|
|
|
/*
|
|
|
|
* Set the background colour. This must come after the foreground as
|
|
|
|
* tty_colour_fg() can call tty_reset().
|
|
|
|
*/
|
|
|
|
if (!bg_default && (bg != tc->bg ||
|
2009-10-28 23:16:30 +00:00
|
|
|
((flags & GRID_FLAG_BG256) != (tc->flags & GRID_FLAG_BG256))))
|
2009-11-28 14:50:37 +00:00
|
|
|
tty_colours_bg(tty, gc);
|
2009-03-07 10:29:06 +00:00
|
|
|
}
|
|
|
|
|
2008-09-25 20:08:57 +00:00
|
|
|
void
|
2010-03-08 14:53:49 +00:00
|
|
|
tty_check_fg(struct tty *tty, struct grid_cell *gc)
|
|
|
|
{
|
|
|
|
u_int colours;
|
|
|
|
|
|
|
|
/* Is this a 256-colour colour? */
|
|
|
|
if (gc->flags & GRID_FLAG_FG256) {
|
|
|
|
/* And not a 256 colour mode? */
|
|
|
|
if (!(tty->term->flags & TERM_88COLOURS) &&
|
|
|
|
!(tty->term_flags & TERM_88COLOURS) &&
|
|
|
|
!(tty->term->flags & TERM_256COLOURS) &&
|
|
|
|
!(tty->term_flags & TERM_256COLOURS)) {
|
|
|
|
gc->fg = colour_256to16(gc->fg);
|
|
|
|
if (gc->fg & 8) {
|
|
|
|
gc->fg &= 7;
|
|
|
|
gc->attr |= GRID_ATTR_BRIGHT;
|
|
|
|
} else
|
|
|
|
gc->attr &= ~GRID_ATTR_BRIGHT;
|
|
|
|
gc->flags &= ~GRID_FLAG_FG256;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Is this an aixterm colour? */
|
|
|
|
colours = tty_term_number(tty->term, TTYC_COLORS);
|
|
|
|
if (gc->fg >= 90 && gc->fg <= 97 && colours < 16) {
|
|
|
|
gc->fg -= 90;
|
|
|
|
gc->attr |= GRID_ATTR_BRIGHT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tty_check_bg(struct tty *tty, struct grid_cell *gc)
|
|
|
|
{
|
|
|
|
u_int colours;
|
|
|
|
|
|
|
|
/* Is this a 256-colour colour? */
|
|
|
|
if (gc->flags & GRID_FLAG_BG256) {
|
|
|
|
/*
|
|
|
|
* And not a 256 colour mode? Translate to 16-colour
|
|
|
|
* palette. Bold background doesn't exist portably, so just
|
|
|
|
* discard the bold bit if set.
|
|
|
|
*/
|
|
|
|
if (!(tty->term->flags & TERM_88COLOURS) &&
|
|
|
|
!(tty->term_flags & TERM_88COLOURS) &&
|
|
|
|
!(tty->term->flags & TERM_256COLOURS) &&
|
|
|
|
!(tty->term_flags & TERM_256COLOURS)) {
|
|
|
|
gc->bg = colour_256to16(gc->bg);
|
|
|
|
if (gc->bg & 8)
|
|
|
|
gc->bg &= 7;
|
|
|
|
gc->attr &= ~GRID_ATTR_BRIGHT;
|
|
|
|
gc->flags &= ~GRID_FLAG_BG256;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Is this an aixterm colour? */
|
|
|
|
colours = tty_term_number(tty->term, TTYC_COLORS);
|
|
|
|
if (gc->bg >= 100 && gc->bg <= 107 && colours < 16) {
|
|
|
|
gc->bg -= 90;
|
|
|
|
gc->attr |= GRID_ATTR_BRIGHT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tty_colours_fg(struct tty *tty, const struct grid_cell *gc)
|
2008-09-08 22:03:56 +00:00
|
|
|
{
|
2009-10-28 23:15:32 +00:00
|
|
|
struct grid_cell *tc = &tty->cell;
|
|
|
|
u_char fg = gc->fg;
|
2009-12-16 01:13:09 +00:00
|
|
|
char s[32];
|
2008-09-08 22:03:56 +00:00
|
|
|
|
2009-10-28 23:15:32 +00:00
|
|
|
/* Is this a 256-colour colour? */
|
2008-09-25 20:08:57 +00:00
|
|
|
if (gc->flags & GRID_FLAG_FG256) {
|
2009-10-28 23:15:32 +00:00
|
|
|
/* Try as 256 colours or translating to 88. */
|
2009-03-07 10:29:06 +00:00
|
|
|
if (tty_try_256(tty, fg, "38") == 0)
|
2009-10-28 23:15:32 +00:00
|
|
|
goto save_fg;
|
2009-03-07 10:29:06 +00:00
|
|
|
if (tty_try_88(tty, fg, "38") == 0)
|
2009-10-28 23:15:32 +00:00
|
|
|
goto save_fg;
|
2010-03-08 14:53:49 +00:00
|
|
|
/* Else already handled by tty_check_fg. */
|
|
|
|
return;
|
2007-11-27 19:23:34 +00:00
|
|
|
}
|
2009-05-04 17:58:27 +00:00
|
|
|
|
2009-12-16 01:13:09 +00:00
|
|
|
/* Is this an aixterm bright colour? */
|
|
|
|
if (fg >= 90 && fg <= 97) {
|
2010-03-08 14:53:49 +00:00
|
|
|
xsnprintf(s, sizeof s, "\033[%dm", fg);
|
|
|
|
tty_puts(tty, s);
|
|
|
|
goto save_fg;
|
2009-12-16 01:13:09 +00:00
|
|
|
}
|
|
|
|
|
2009-10-28 23:15:32 +00:00
|
|
|
/* Otherwise set the foreground colour. */
|
|
|
|
tty_putcode1(tty, TTYC_SETAF, fg);
|
|
|
|
|
2009-12-04 22:14:47 +00:00
|
|
|
save_fg:
|
2009-10-28 23:15:32 +00:00
|
|
|
/* Save the new values in the terminal current cell. */
|
|
|
|
tc->fg = fg;
|
|
|
|
tc->flags &= ~GRID_FLAG_FG256;
|
|
|
|
tc->flags |= gc->flags & GRID_FLAG_FG256;
|
2008-09-08 22:03:56 +00:00
|
|
|
}
|
|
|
|
|
2008-09-25 20:08:57 +00:00
|
|
|
void
|
2009-11-28 14:50:37 +00:00
|
|
|
tty_colours_bg(struct tty *tty, const struct grid_cell *gc)
|
2008-09-08 22:03:56 +00:00
|
|
|
{
|
2009-10-28 23:15:32 +00:00
|
|
|
struct grid_cell *tc = &tty->cell;
|
|
|
|
u_char bg = gc->bg;
|
2009-12-16 01:13:09 +00:00
|
|
|
char s[32];
|
2007-12-06 09:46:23 +00:00
|
|
|
|
2009-10-28 23:15:32 +00:00
|
|
|
/* Is this a 256-colour colour? */
|
2008-09-25 20:08:57 +00:00
|
|
|
if (gc->flags & GRID_FLAG_BG256) {
|
2009-10-28 23:15:32 +00:00
|
|
|
/* Try as 256 colours or translating to 88. */
|
2009-03-07 10:29:06 +00:00
|
|
|
if (tty_try_256(tty, bg, "48") == 0)
|
2009-10-28 23:15:32 +00:00
|
|
|
goto save_bg;
|
2009-03-07 10:29:06 +00:00
|
|
|
if (tty_try_88(tty, bg, "48") == 0)
|
2009-10-28 23:15:32 +00:00
|
|
|
goto save_bg;
|
2010-03-08 14:53:49 +00:00
|
|
|
/* Else already handled by tty_check_bg. */
|
|
|
|
return;
|
2008-09-08 22:03:56 +00:00
|
|
|
}
|
2008-09-26 06:45:28 +00:00
|
|
|
|
2009-12-16 01:13:09 +00:00
|
|
|
/* Is this an aixterm bright colour? */
|
|
|
|
if (bg >= 100 && bg <= 107) {
|
|
|
|
/* 16 colour terminals or above only. */
|
|
|
|
if (tty_term_number(tty->term, TTYC_COLORS) >= 16) {
|
|
|
|
xsnprintf(s, sizeof s, "\033[%dm", bg);
|
|
|
|
tty_puts(tty, s);
|
|
|
|
goto save_bg;
|
|
|
|
}
|
|
|
|
bg -= 100;
|
|
|
|
/* no such thing as a bold background */
|
|
|
|
}
|
|
|
|
|
2009-10-28 23:15:32 +00:00
|
|
|
/* Otherwise set the background colour. */
|
|
|
|
tty_putcode1(tty, TTYC_SETAB, bg);
|
|
|
|
|
|
|
|
save_bg:
|
|
|
|
/* Save the new values in the terminal current cell. */
|
|
|
|
tc->bg = bg;
|
|
|
|
tc->flags &= ~GRID_FLAG_BG256;
|
|
|
|
tc->flags |= gc->flags & GRID_FLAG_BG256;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2007-11-27 19:23:34 +00:00
|
|
|
}
|