/* $OpenBSD$ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
*
* 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 <stdlib.h>
#include <string.h>
#include "tmux.h"
static void screen_write_initctx(struct screen_write_ctx *,
struct tty_ctx *);
static void screen_write_save_last(struct screen_write_ctx *,
struct tty_ctx *);
static void screen_write_flush(struct screen_write_ctx *);
static int screen_write_overwrite(struct screen_write_ctx *,
struct grid_cell *, u_int);
static int screen_write_combine(struct screen_write_ctx *,
const struct utf8_data *);
static const struct grid_cell screen_write_pad_cell = {
GRID_FLAG_PADDING, 0, 8, 8, { { 0 }, 0, 0, 0 }
};
#define screen_dirty_bit(s, x, y) (((y) * screen_size_x(s)) + (x))
#define screen_dirty_clear(s, sx, sy, ex, ey) \
do { \
if (s->dirty != NULL) { \
bit_nclear(s->dirty, \
screen_dirty_bit(s, sx, sy), \
screen_dirty_bit(s, ex, ey)); \
} \
} while (0)
/* Initialize writing with a window. */
void
screen_write_start(struct screen_write_ctx *ctx, struct window_pane *wp,
struct screen *s)
{
u_int size;
char tmp[16];
const char *cp = tmp;
ctx->wp = wp;
if (wp != NULL && s == NULL)
ctx->s = wp->screen;
else
ctx->s = s;
size = screen_size_x(ctx->s) * screen_size_y(ctx->s);
if (ctx->s->dirtysize != size) {
free(ctx->s->dirty);
ctx->s->dirty = NULL;
ctx->s->dirtysize = size;
}
ctx->dirty = 0;
ctx->cells = ctx->written = ctx->skipped = 0;
if (wp == NULL)
cp = "no pane";
else
snprintf(tmp, sizeof tmp, "pane %%%u", wp->id);
log_debug("%s: size %ux%u, %s", __func__, screen_size_x(ctx->s),
screen_size_y(ctx->s), cp);
}
/* Finish writing. */
void
screen_write_stop(struct screen_write_ctx *ctx)
{
screen_write_flush(ctx);
log_debug("%s: %u of %u written (dirty %u, skipped %u)", __func__,
ctx->written, ctx->cells, ctx->cells - ctx->written, ctx->skipped);
}
/* Flush outstanding cell writes. */
static void
screen_write_flush(struct screen_write_ctx *ctx)
{
struct screen *s = ctx->s;
struct tty_ctx ttyctx;
u_int x, y, offset, cx, cy, dirty;
struct grid_cell gc;
if (ctx->dirty == 0)
return;
dirty = 0;
cx = s->cx;
cy = s->cy;
offset = 0;
for (y = 0; y < screen_size_y(s); y++) {
for (x = 0; x < screen_size_x(s); x++) {
offset++;
if (!bit_test(s->dirty, offset - 1))
continue;
bit_clear(s->dirty, offset - 1);
screen_write_cursormove(ctx, x, y);
grid_view_get_cell(s->grid, x, y, &gc);
screen_write_initctx(ctx, &ttyctx);
ttyctx.cell = &gc;
tty_write(tty_cmd_cell, &ttyctx);
ctx->written++;
if (++dirty == ctx->dirty)
break;
}
if (dirty == ctx->dirty)
break;
}
s->cx = cx;
s->cy = cy;
}
/* Reset screen state. */
void
screen_write_reset(struct screen_write_ctx *ctx)
{
struct screen *s = ctx->s;
screen_reset_tabs(s);
screen_write_scrollregion(ctx, 0, screen_size_y(s) - 1);
s->mode &= ~(MODE_INSERT|MODE_KCURSOR|MODE_KKEYPAD|MODE_FOCUSON);
s->mode &= ~(ALL_MOUSE_MODES|MODE_MOUSE_UTF8|MODE_MOUSE_SGR);
screen_write_clearscreen(ctx);
screen_write_cursormove(ctx, 0, 0);
}
/* Write character. */
void
screen_write_putc(struct screen_write_ctx *ctx, const struct grid_cell *gcp,
u_char ch)
{
struct grid_cell gc;
memcpy(&gc, gcp, sizeof gc);
utf8_set(&gc.data, ch);
screen_write_cell(ctx, &gc);
}
/* Calculate string length, with embedded formatting. */
size_t
|