mirror of
				https://github.com/tmux/tmux.git
				synced 2025-11-04 09:26:05 +00:00 
			
		
		
		
	Another helper function to write to terminal and log.
This commit is contained in:
		
							
								
								
									
										5
									
								
								tmux.h
									
									
									
									
									
								
							
							
						
						
									
										5
									
								
								tmux.h
									
									
									
									
									
								
							@@ -483,6 +483,7 @@ struct msg_stderr_data {
 | 
			
		||||
#define MODE_FOCUSON 0x800
 | 
			
		||||
#define MODE_MOUSE_ALL 0x1000
 | 
			
		||||
 | 
			
		||||
#define ALL_MODES 0xffffff
 | 
			
		||||
#define ALL_MOUSE_MODES (MODE_MOUSE_STANDARD|MODE_MOUSE_BUTTON|MODE_MOUSE_ALL)
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
@@ -1085,9 +1086,9 @@ struct tty {
 | 
			
		||||
 | 
			
		||||
/* TTY command context. */
 | 
			
		||||
struct tty_ctx {
 | 
			
		||||
	struct window_pane *wp;
 | 
			
		||||
	struct window_pane	*wp;
 | 
			
		||||
 | 
			
		||||
	const struct grid_cell *cell;
 | 
			
		||||
	const struct grid_cell	*cell;
 | 
			
		||||
 | 
			
		||||
	u_int		 num;
 | 
			
		||||
	void		*ptr;
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										120
									
								
								tty.c
									
									
									
									
									
								
							
							
						
						
									
										120
									
								
								tty.c
									
									
									
									
									
								
							@@ -46,7 +46,7 @@ static int	tty_try_colour(struct tty *, int, const char *);
 | 
			
		||||
static void	tty_force_cursor_colour(struct tty *, const char *);
 | 
			
		||||
static void	tty_cursor_pane(struct tty *, const struct tty_ctx *, u_int,
 | 
			
		||||
		    u_int);
 | 
			
		||||
 | 
			
		||||
static void	tty_invalidate(struct tty *);
 | 
			
		||||
static void	tty_colours(struct tty *, const struct grid_cell *);
 | 
			
		||||
static void	tty_check_fg(struct tty *, const struct window_pane *,
 | 
			
		||||
		    struct grid_cell *);
 | 
			
		||||
@@ -145,23 +145,7 @@ tty_resize(struct tty *tty)
 | 
			
		||||
	log_debug("%s: %s now %ux%u", __func__, tty->path, sx, sy);
 | 
			
		||||
	if (!tty_set_size(tty, sx, sy))
 | 
			
		||||
		return (0);
 | 
			
		||||
 | 
			
		||||
	tty->cx = UINT_MAX;
 | 
			
		||||
	tty->cy = UINT_MAX;
 | 
			
		||||
 | 
			
		||||
	tty->rupper = tty->rleft = UINT_MAX;
 | 
			
		||||
	tty->rlower = tty->rright = UINT_MAX;
 | 
			
		||||
 | 
			
		||||
	/*
 | 
			
		||||
	 * 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_off(tty);
 | 
			
		||||
		tty_margin_off(tty);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	tty_invalidate(tty);
 | 
			
		||||
	return (1);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -244,12 +228,6 @@ tty_start_tty(struct tty *tty)
 | 
			
		||||
 | 
			
		||||
	tty_putcode(tty, TTYC_SMCUP);
 | 
			
		||||
 | 
			
		||||
	tty_putcode(tty, TTYC_SGR0);
 | 
			
		||||
	memcpy(&tty->cell, &grid_default_cell, sizeof tty->cell);
 | 
			
		||||
 | 
			
		||||
	memcpy(&tty->last_cell, &grid_default_cell, sizeof tty->last_cell);
 | 
			
		||||
	tty->last_wp = -1;
 | 
			
		||||
 | 
			
		||||
	tty_putcode(tty, TTYC_RMKX);
 | 
			
		||||
	if (tty_use_acs(tty))
 | 
			
		||||
		tty_putcode(tty, TTYC_ENACS);
 | 
			
		||||
@@ -267,15 +245,8 @@ tty_start_tty(struct tty *tty)
 | 
			
		||||
		tty_puts(tty, "\033[c");
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	tty->cx = UINT_MAX;
 | 
			
		||||
	tty->cy = UINT_MAX;
 | 
			
		||||
 | 
			
		||||
	tty->rupper = tty->rleft = UINT_MAX;
 | 
			
		||||
	tty->rlower = tty->rright = UINT_MAX;
 | 
			
		||||
 | 
			
		||||
	tty->mode = MODE_CURSOR;
 | 
			
		||||
 | 
			
		||||
	tty->flags |= TTY_STARTED;
 | 
			
		||||
	tty_invalidate(tty);
 | 
			
		||||
 | 
			
		||||
	tty_force_cursor_colour(tty, "");
 | 
			
		||||
 | 
			
		||||
@@ -437,40 +408,36 @@ tty_putcode_ptr2(struct tty *tty, enum tty_code_code code, const void *a,
 | 
			
		||||
		tty_puts(tty, tty_term_ptr2(tty->term, code, a, b));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
tty_add(struct tty *tty, const char *buf, size_t len)
 | 
			
		||||
{
 | 
			
		||||
	bufferevent_write(tty->event, buf, len);
 | 
			
		||||
	log_debug("%s: %.*s", tty->path, (int)len, buf);
 | 
			
		||||
 | 
			
		||||
	if (tty_log_fd != -1)
 | 
			
		||||
		write(tty_log_fd, buf, len);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
tty_puts(struct tty *tty, const char *s)
 | 
			
		||||
{
 | 
			
		||||
	size_t	size = EVBUFFER_LENGTH(tty->event->output);
 | 
			
		||||
 | 
			
		||||
	if (*s == '\0')
 | 
			
		||||
		return;
 | 
			
		||||
 | 
			
		||||
	bufferevent_write(tty->event, s, strlen(s));
 | 
			
		||||
	log_debug("%s (%zu): %s", tty->path, size, s);
 | 
			
		||||
 | 
			
		||||
	if (tty_log_fd != -1)
 | 
			
		||||
		write(tty_log_fd, s, strlen(s));
 | 
			
		||||
	if (*s != '\0')
 | 
			
		||||
		tty_add(tty, s, strlen(s));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
tty_putc(struct tty *tty, u_char ch)
 | 
			
		||||
{
 | 
			
		||||
	size_t		 size = EVBUFFER_LENGTH(tty->event->output);
 | 
			
		||||
	const char	*acs;
 | 
			
		||||
 | 
			
		||||
	if (tty->cell.attr & GRID_ATTR_CHARSET) {
 | 
			
		||||
		acs = tty_acs_get(tty, ch);
 | 
			
		||||
		if (acs != NULL) {
 | 
			
		||||
			bufferevent_write(tty->event, acs, strlen(acs));
 | 
			
		||||
			log_debug("%s (%zu): %s", tty->path, size, acs);
 | 
			
		||||
		} else {
 | 
			
		||||
			bufferevent_write(tty->event, &ch, 1);
 | 
			
		||||
			log_debug("%s (%zu): %c", tty->path, size, ch);
 | 
			
		||||
		}
 | 
			
		||||
	} else {
 | 
			
		||||
		bufferevent_write(tty->event, &ch, 1);
 | 
			
		||||
		log_debug("%s (%zu): %c", tty->path, size, ch);
 | 
			
		||||
	}
 | 
			
		||||
		if (acs != NULL)
 | 
			
		||||
			tty_add(tty, acs, strlen(acs));
 | 
			
		||||
		else
 | 
			
		||||
			tty_add(tty, &ch, 1);
 | 
			
		||||
	} else
 | 
			
		||||
		tty_add(tty, &ch, 1);
 | 
			
		||||
 | 
			
		||||
	if (ch >= 0x20 && ch != 0x7f) {
 | 
			
		||||
		if (tty->cx >= tty->sx) {
 | 
			
		||||
@@ -488,22 +455,12 @@ tty_putc(struct tty *tty, u_char ch)
 | 
			
		||||
		} else
 | 
			
		||||
			tty->cx++;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if (tty_log_fd != -1)
 | 
			
		||||
		write(tty_log_fd, &ch, 1);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
tty_putn(struct tty *tty, const void *buf, size_t len, u_int width)
 | 
			
		||||
{
 | 
			
		||||
	size_t	size = EVBUFFER_LENGTH(tty->event->output);
 | 
			
		||||
 | 
			
		||||
	bufferevent_write(tty->event, buf, len);
 | 
			
		||||
	log_debug("%s (%zu): %.*s", tty->path, size, (int)len, (char *)buf);
 | 
			
		||||
 | 
			
		||||
	if (tty_log_fd != -1)
 | 
			
		||||
		write(tty_log_fd, buf, len);
 | 
			
		||||
 | 
			
		||||
	tty_add(tty, buf, len);
 | 
			
		||||
	tty->cx += width;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -1230,12 +1187,7 @@ tty_cmd_rawstring(struct tty *tty, const struct tty_ctx *ctx)
 | 
			
		||||
 | 
			
		||||
	for (i = 0; i < ctx->num; i++)
 | 
			
		||||
		tty_putc(tty, str[i]);
 | 
			
		||||
 | 
			
		||||
	tty->cx = tty->cy = UINT_MAX;
 | 
			
		||||
	tty->rupper = tty->rlower = UINT_MAX;
 | 
			
		||||
 | 
			
		||||
	tty_attributes(tty, &grid_default_cell, ctx->wp);
 | 
			
		||||
	tty_cursor(tty, 0, 0);
 | 
			
		||||
	tty_invalidate(tty);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
@@ -1292,6 +1244,32 @@ tty_reset(struct tty *tty)
 | 
			
		||||
	tty->last_wp = -1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
tty_invalidate(struct tty *tty)
 | 
			
		||||
{
 | 
			
		||||
	memcpy(&tty->cell, &grid_default_cell, sizeof tty->cell);
 | 
			
		||||
 | 
			
		||||
	memcpy(&tty->last_cell, &grid_default_cell, sizeof tty->last_cell);
 | 
			
		||||
	tty->last_wp = -1;
 | 
			
		||||
 | 
			
		||||
	tty->cx = tty->cy = UINT_MAX;
 | 
			
		||||
 | 
			
		||||
	tty->rupper = tty->rleft = UINT_MAX;
 | 
			
		||||
	tty->rlower = tty->rright = UINT_MAX;
 | 
			
		||||
 | 
			
		||||
	if (tty->flags & TTY_STARTED) {
 | 
			
		||||
		tty_putcode(tty, TTYC_SGR0);
 | 
			
		||||
 | 
			
		||||
		tty->mode = ALL_MODES;
 | 
			
		||||
		tty_update_mode(tty, MODE_CURSOR, NULL);
 | 
			
		||||
 | 
			
		||||
		tty_cursor(tty, 0, 0);
 | 
			
		||||
		tty_region_off(tty);
 | 
			
		||||
		tty_margin_off(tty);
 | 
			
		||||
	} else
 | 
			
		||||
		tty->mode = MODE_CURSOR;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Turn off margin. */
 | 
			
		||||
void
 | 
			
		||||
tty_region_off(struct tty *tty)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user