GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/tmux/screen-write.c Lines: 0 825 0.0 %
Date: 2017-11-13 Branches: 0 464 0.0 %

Line Branch Exec Source
1
/* $OpenBSD: screen-write.c,v 1.134 2017/11/03 17:02:33 nicm Exp $ */
2
3
/*
4
 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
5
 *
6
 * Permission to use, copy, modify, and distribute this software for any
7
 * purpose with or without fee is hereby granted, provided that the above
8
 * copyright notice and this permission notice appear in all copies.
9
 *
10
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15
 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16
 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
 */
18
19
#include <sys/types.h>
20
21
#include <stdlib.h>
22
#include <string.h>
23
24
#include "tmux.h"
25
26
static void	screen_write_initctx(struct screen_write_ctx *,
27
		    struct tty_ctx *);
28
static void	screen_write_collect_clear(struct screen_write_ctx *, u_int,
29
		    u_int);
30
static void	screen_write_collect_scroll(struct screen_write_ctx *);
31
static void	screen_write_collect_flush(struct screen_write_ctx *, int);
32
33
static int	screen_write_overwrite(struct screen_write_ctx *,
34
		    struct grid_cell *, u_int);
35
static const struct grid_cell *screen_write_combine(struct screen_write_ctx *,
36
		    const struct utf8_data *, u_int *);
37
38
static const struct grid_cell screen_write_pad_cell = {
39
	GRID_FLAG_PADDING, 0, 8, 8, { { 0 }, 0, 0, 0 }
40
};
41
42
struct screen_write_collect_item {
43
	u_int			 x;
44
	int			 wrapped;
45
46
	u_int			 used;
47
	char			 data[256];
48
49
	struct grid_cell	 gc;
50
51
	TAILQ_ENTRY(screen_write_collect_item) entry;
52
};
53
struct screen_write_collect_line {
54
	TAILQ_HEAD(, screen_write_collect_item) items;
55
};
56
57
/* Initialize writing with a window. */
58
void
59
screen_write_start(struct screen_write_ctx *ctx, struct window_pane *wp,
60
    struct screen *s)
61
{
62
	char	tmp[16];
63
	u_int	y;
64
65
	memset(ctx, 0, sizeof *ctx);
66
67
	ctx->wp = wp;
68
	if (wp != NULL && s == NULL)
69
		ctx->s = wp->screen;
70
	else
71
		ctx->s = s;
72
73
	ctx->list = xcalloc(screen_size_y(ctx->s), sizeof *ctx->list);
74
	for (y = 0; y < screen_size_y(ctx->s); y++)
75
		TAILQ_INIT(&ctx->list[y].items);
76
	ctx->item = xcalloc(1, sizeof *ctx->item);
77
78
	ctx->scrolled = 0;
79
	ctx->bg = 8;
80
81
	if (wp != NULL)
82
		snprintf(tmp, sizeof tmp, "pane %%%u", wp->id);
83
	log_debug("%s: size %ux%u, %s", __func__, screen_size_x(ctx->s),
84
	    screen_size_y(ctx->s), wp == NULL ? "no pane" : tmp);
85
}
86
87
/* Finish writing. */
88
void
89
screen_write_stop(struct screen_write_ctx *ctx)
90
{
91
	screen_write_collect_end(ctx);
92
	screen_write_collect_flush(ctx, 0);
93
94
	log_debug("%s: %u cells (%u written, %u skipped)", __func__,
95
	    ctx->cells, ctx->written, ctx->skipped);
96
97
	free(ctx->item);
98
	free(ctx->list); /* flush will have emptied */
99
}
100
101
/* Reset screen state. */
102
void
103
screen_write_reset(struct screen_write_ctx *ctx)
104
{
105
	struct screen	*s = ctx->s;
106
107
	screen_reset_tabs(s);
108
	screen_write_scrollregion(ctx, 0, screen_size_y(s) - 1);
109
110
	s->mode &= ~(MODE_INSERT|MODE_KCURSOR|MODE_KKEYPAD|MODE_FOCUSON);
111
	s->mode &= ~(ALL_MOUSE_MODES|MODE_MOUSE_UTF8|MODE_MOUSE_SGR);
112
113
	screen_write_clearscreen(ctx, 8);
114
	screen_write_cursormove(ctx, 0, 0);
115
}
116
117
/* Write character. */
118
void
119
screen_write_putc(struct screen_write_ctx *ctx, const struct grid_cell *gcp,
120
    u_char ch)
121
{
122
	struct grid_cell	gc;
123
124
	memcpy(&gc, gcp, sizeof gc);
125
126
	utf8_set(&gc.data, ch);
127
	screen_write_cell(ctx, &gc);
128
}
129
130
/* Calculate string length, with embedded formatting. */
131
size_t
132
screen_write_cstrlen(const char *fmt, ...)
133
{
134
	va_list	ap;
135
	char   *msg, *msg2, *ptr, *ptr2;
136
	size_t	size;
137
138
	va_start(ap, fmt);
139
	xvasprintf(&msg, fmt, ap);
140
	va_end(ap);
141
	msg2 = xmalloc(strlen(msg) + 1);
142
143
	ptr = msg;
144
	ptr2 = msg2;
145
	while (*ptr != '\0') {
146
		if (ptr[0] == '#' && ptr[1] == '[') {
147
			while (*ptr != ']' && *ptr != '\0')
148
				ptr++;
149
			if (*ptr == ']')
150
				ptr++;
151
			continue;
152
		}
153
		*ptr2++ = *ptr++;
154
	}
155
	*ptr2 = '\0';
156
157
	size = screen_write_strlen("%s", msg2);
158
159
	free(msg);
160
	free(msg2);
161
162
	return (size);
163
}
164
165
/* Calculate string length. */
166
size_t
167
screen_write_strlen(const char *fmt, ...)
168
{
169
	va_list			ap;
170
	char   	       	       *msg;
171
	struct utf8_data	ud;
172
	u_char 	      	       *ptr;
173
	size_t			left, size = 0;
174
	enum utf8_state		more;
175
176
	va_start(ap, fmt);
177
	xvasprintf(&msg, fmt, ap);
178
	va_end(ap);
179
180
	ptr = msg;
181
	while (*ptr != '\0') {
182
		if (*ptr > 0x7f && utf8_open(&ud, *ptr) == UTF8_MORE) {
183
			ptr++;
184
185
			left = strlen(ptr);
186
			if (left < (size_t)ud.size - 1)
187
				break;
188
			while ((more = utf8_append(&ud, *ptr)) == UTF8_MORE)
189
				ptr++;
190
			ptr++;
191
192
			if (more == UTF8_DONE)
193
				size += ud.width;
194
		} else {
195
			if (*ptr > 0x1f && *ptr < 0x7f)
196
				size++;
197
			ptr++;
198
		}
199
	}
200
201
	free(msg);
202
	return (size);
203
}
204
205
/* Write simple string (no UTF-8 or maximum length). */
206
void
207
screen_write_puts(struct screen_write_ctx *ctx, const struct grid_cell *gcp,
208
    const char *fmt, ...)
209
{
210
	va_list	ap;
211
212
	va_start(ap, fmt);
213
	screen_write_vnputs(ctx, -1, gcp, fmt, ap);
214
	va_end(ap);
215
}
216
217
/* Write string with length limit (-1 for unlimited). */
218
void
219
screen_write_nputs(struct screen_write_ctx *ctx, ssize_t maxlen,
220
    const struct grid_cell *gcp, const char *fmt, ...)
221
{
222
	va_list	ap;
223
224
	va_start(ap, fmt);
225
	screen_write_vnputs(ctx, maxlen, gcp, fmt, ap);
226
	va_end(ap);
227
}
228
229
void
230
screen_write_vnputs(struct screen_write_ctx *ctx, ssize_t maxlen,
231
    const struct grid_cell *gcp, const char *fmt, va_list ap)
232
{
233
	struct grid_cell	gc;
234
	struct utf8_data       *ud = &gc.data;
235
	char   		       *msg;
236
	u_char 		       *ptr;
237
	size_t		 	left, size = 0;
238
	enum utf8_state		more;
239
240
	memcpy(&gc, gcp, sizeof gc);
241
	xvasprintf(&msg, fmt, ap);
242
243
	ptr = msg;
244
	while (*ptr != '\0') {
245
		if (*ptr > 0x7f && utf8_open(ud, *ptr) == UTF8_MORE) {
246
			ptr++;
247
248
			left = strlen(ptr);
249
			if (left < (size_t)ud->size - 1)
250
				break;
251
			while ((more = utf8_append(ud, *ptr)) == UTF8_MORE)
252
				ptr++;
253
			ptr++;
254
255
			if (more != UTF8_DONE)
256
				continue;
257
			if (maxlen > 0 && size + ud->width > (size_t)maxlen) {
258
				while (size < (size_t)maxlen) {
259
					screen_write_putc(ctx, &gc, ' ');
260
					size++;
261
				}
262
				break;
263
			}
264
			size += ud->width;
265
			screen_write_cell(ctx, &gc);
266
		} else {
267
			if (maxlen > 0 && size + 1 > (size_t)maxlen)
268
				break;
269
270
			if (*ptr == '\001')
271
				gc.attr ^= GRID_ATTR_CHARSET;
272
			else if (*ptr > 0x1f && *ptr < 0x7f) {
273
				size++;
274
				screen_write_putc(ctx, &gc, *ptr);
275
			}
276
			ptr++;
277
		}
278
	}
279
280
	free(msg);
281
}
282
283
/* Write string, similar to nputs, but with embedded formatting (#[]). */
284
void
285
screen_write_cnputs(struct screen_write_ctx *ctx, ssize_t maxlen,
286
    const struct grid_cell *gcp, const char *fmt, ...)
287
{
288
	struct grid_cell	 gc;
289
	struct utf8_data	*ud = &gc.data;
290
	va_list			 ap;
291
	char			*msg;
292
	u_char 			*ptr, *last;
293
	size_t			 left, size = 0;
294
	enum utf8_state		 more;
295
296
	memcpy(&gc, gcp, sizeof gc);
297
298
	va_start(ap, fmt);
299
	xvasprintf(&msg, fmt, ap);
300
	va_end(ap);
301
302
	ptr = msg;
303
	while (*ptr != '\0') {
304
		if (ptr[0] == '#' && ptr[1] == '[') {
305
			ptr += 2;
306
			last = ptr + strcspn(ptr, "]");
307
			if (*last == '\0') {
308
				/* No ]. Not much point in doing anything. */
309
				break;
310
			}
311
			*last = '\0';
312
313
			style_parse(gcp, &gc, ptr);
314
			ptr = last + 1;
315
			continue;
316
		}
317
318
		if (*ptr > 0x7f && utf8_open(ud, *ptr) == UTF8_MORE) {
319
			ptr++;
320
321
			left = strlen(ptr);
322
			if (left < (size_t)ud->size - 1)
323
				break;
324
			while ((more = utf8_append(ud, *ptr)) == UTF8_MORE)
325
				ptr++;
326
			ptr++;
327
328
			if (more != UTF8_DONE)
329
				continue;
330
			if (maxlen > 0 && size + ud->width > (size_t)maxlen) {
331
				while (size < (size_t)maxlen) {
332
					screen_write_putc(ctx, &gc, ' ');
333
					size++;
334
				}
335
				break;
336
			}
337
			size += ud->width;
338
			screen_write_cell(ctx, &gc);
339
		} else {
340
			if (maxlen > 0 && size + 1 > (size_t)maxlen)
341
				break;
342
343
			if (*ptr > 0x1f && *ptr < 0x7f) {
344
				size++;
345
				screen_write_putc(ctx, &gc, *ptr);
346
			}
347
			ptr++;
348
		}
349
	}
350
351
	free(msg);
352
}
353
354
/* Copy from another screen. Assumes target region is big enough. */
355
void
356
screen_write_copy(struct screen_write_ctx *ctx, struct screen *src, u_int px,
357
    u_int py, u_int nx, u_int ny, bitstr_t *mbs, const struct grid_cell *mgc)
358
{
359
	struct screen		*s = ctx->s;
360
	struct grid		*gd = src->grid;
361
	struct grid_cell	 gc;
362
	u_int		 	 xx, yy, cx, cy, b;
363
364
	if (nx == 0 || ny == 0)
365
		return;
366
367
	cx = s->cx;
368
	cy = s->cy;
369
370
	for (yy = py; yy < py + ny; yy++) {
371
		for (xx = px; xx < px + nx; xx++) {
372
			grid_get_cell(gd, xx, yy, &gc);
373
			if (mbs != NULL) {
374
				b = (yy * screen_size_x(src)) + xx;
375
				if (bit_test(mbs, b)) {
376
					gc.attr = mgc->attr;
377
					gc.fg = mgc->fg;
378
					gc.bg = mgc->bg;
379
				}
380
			}
381
			screen_write_cell(ctx, &gc);
382
		}
383
		cy++;
384
		screen_write_cursormove(ctx, cx, cy);
385
	}
386
}
387
388
/*
389
 * Copy from another screen but without the selection stuff. Also assumes the
390
 * target region is already big enough and already cleared.
391
 */
392
void
393
screen_write_fast_copy(struct screen_write_ctx *ctx, struct screen *src,
394
    u_int px, u_int py, u_int nx, u_int ny)
395
{
396
	struct screen		*s = ctx->s;
397
	struct grid		*gd = src->grid;
398
	struct grid_cell	 gc;
399
	u_int		 	 xx, yy, cx, cy;
400
401
	if (nx == 0 || ny == 0)
402
		return;
403
404
	cy = s->cy;
405
	for (yy = py; yy < py + ny; yy++) {
406
		if (yy >= gd->hsize + gd->sy)
407
			break;
408
		cx = s->cx;
409
		for (xx = px; xx < px + nx; xx++) {
410
			if (xx >= gd->linedata[yy].cellsize)
411
				break;
412
			grid_get_cell(gd, xx, yy, &gc);
413
			if (!grid_cells_equal(&gc, &grid_default_cell))
414
				grid_view_set_cell(ctx->s->grid, cx, cy, &gc);
415
			cx++;
416
		}
417
		cy++;
418
	}
419
}
420
421
/* Draw a horizontal line on screen. */
422
void
423
screen_write_hline(struct screen_write_ctx *ctx, u_int nx, int left, int right)
424
{
425
	struct screen		*s = ctx->s;
426
	struct grid_cell	 gc;
427
	u_int			 cx, cy, i;
428
429
	cx = s->cx;
430
	cy = s->cy;
431
432
	memcpy(&gc, &grid_default_cell, sizeof gc);
433
	gc.attr |= GRID_ATTR_CHARSET;
434
435
	screen_write_putc(ctx, &gc, left ? 't' : 'q');
436
	for (i = 1; i < nx - 1; i++)
437
		screen_write_putc(ctx, &gc, 'q');
438
	screen_write_putc(ctx, &gc, right ? 'u' : 'q');
439
440
	screen_write_cursormove(ctx, cx, cy);
441
}
442
443
/* Draw a horizontal line on screen. */
444
void
445
screen_write_vline(struct screen_write_ctx *ctx, u_int ny, int top, int bottom)
446
{
447
	struct screen		*s = ctx->s;
448
	struct grid_cell	 gc;
449
	u_int			 cx, cy, i;
450
451
	cx = s->cx;
452
	cy = s->cy;
453
454
	memcpy(&gc, &grid_default_cell, sizeof gc);
455
	gc.attr |= GRID_ATTR_CHARSET;
456
457
	screen_write_putc(ctx, &gc, top ? 'w' : 'x');
458
	for (i = 1; i < ny - 1; i++) {
459
		screen_write_cursormove(ctx, cx, cy + i);
460
		screen_write_putc(ctx, &gc, 'x');
461
	}
462
	screen_write_cursormove(ctx, cx, cy + ny - 1);
463
	screen_write_putc(ctx, &gc, bottom ? 'v' : 'x');
464
465
	screen_write_cursormove(ctx, cx, cy);
466
}
467
468
/* Draw a box on screen. */
469
void
470
screen_write_box(struct screen_write_ctx *ctx, u_int nx, u_int ny)
471
{
472
	struct screen		*s = ctx->s;
473
	struct grid_cell	 gc;
474
	u_int			 cx, cy, i;
475
476
	cx = s->cx;
477
	cy = s->cy;
478
479
	memcpy(&gc, &grid_default_cell, sizeof gc);
480
	gc.attr |= GRID_ATTR_CHARSET;
481
482
	screen_write_putc(ctx, &gc, 'l');
483
	for (i = 1; i < nx - 1; i++)
484
		screen_write_putc(ctx, &gc, 'q');
485
	screen_write_putc(ctx, &gc, 'k');
486
487
	screen_write_cursormove(ctx, cx, cy + ny - 1);
488
	screen_write_putc(ctx, &gc, 'm');
489
	for (i = 1; i < nx - 1; i++)
490
		screen_write_putc(ctx, &gc, 'q');
491
	screen_write_putc(ctx, &gc, 'j');
492
493
	for (i = 1; i < ny - 1; i++) {
494
		screen_write_cursormove(ctx, cx, cy + i);
495
		screen_write_putc(ctx, &gc, 'x');
496
	}
497
	for (i = 1; i < ny - 1; i++) {
498
		screen_write_cursormove(ctx, cx + nx - 1, cy + i);
499
		screen_write_putc(ctx, &gc, 'x');
500
	}
501
502
	screen_write_cursormove(ctx, cx, cy);
503
}
504
505
/*
506
 * Write a preview version of a window. Assumes target area is big enough and
507
 * already cleared.
508
 */
509
void
510
screen_write_preview(struct screen_write_ctx *ctx, struct screen *src, u_int nx,
511
    u_int ny)
512
{
513
	struct screen		*s = ctx->s;
514
	struct grid_cell	 gc;
515
	u_int			 cx, cy, px, py;
516
517
	cx = s->cx;
518
	cy = s->cy;
519
520
	/*
521
	 * If the cursor is on, pick the area around the cursor, otherwise use
522
	 * the top left.
523
	 */
524
	if (src->mode & MODE_CURSOR) {
525
		px = src->cx;
526
		if (px < nx / 3)
527
			px = 0;
528
		else
529
			px = px - nx / 3;
530
		if (px + nx > screen_size_x(src)) {
531
			if (nx > screen_size_x(src))
532
				px = 0;
533
			else
534
				px = screen_size_x(src) - nx;
535
		}
536
		py = src->cy;
537
		if (py < ny / 3)
538
			py = 0;
539
		else
540
			py = py - ny / 3;
541
		if (py + ny > screen_size_y(src)) {
542
			if (ny > screen_size_y(src))
543
				py = 0;
544
			else
545
				py = screen_size_y(src) - ny;
546
		}
547
	} else {
548
		px = 0;
549
		py = 0;
550
	}
551
552
	screen_write_fast_copy(ctx, src, px, src->grid->hsize + py, nx, ny);
553
554
	if (src->mode & MODE_CURSOR) {
555
		grid_view_get_cell(src->grid, src->cx, src->cy, &gc);
556
		gc.attr |= GRID_ATTR_REVERSE;
557
		screen_write_cursormove(ctx, cx + (src->cx - px),
558
		    cy + (src->cy - py));
559
		screen_write_cell(ctx, &gc);
560
	}
561
}
562
563
/* Set up context for TTY command. */
564
static void
565
screen_write_initctx(struct screen_write_ctx *ctx, struct tty_ctx *ttyctx)
566
{
567
	struct screen	*s = ctx->s;
568
569
	memset(ttyctx, 0, sizeof *ttyctx);
570
571
	ttyctx->wp = ctx->wp;
572
573
	ttyctx->ocx = s->cx;
574
	ttyctx->ocy = s->cy;
575
576
	ttyctx->orlower = s->rlower;
577
	ttyctx->orupper = s->rupper;
578
}
579
580
/* Set a mode. */
581
void
582
screen_write_mode_set(struct screen_write_ctx *ctx, int mode)
583
{
584
	struct screen	*s = ctx->s;
585
586
	s->mode |= mode;
587
}
588
589
/* Clear a mode. */
590
void
591
screen_write_mode_clear(struct screen_write_ctx *ctx, int mode)
592
{
593
	struct screen	*s = ctx->s;
594
595
	s->mode &= ~mode;
596
}
597
598
/* Cursor up by ny. */
599
void
600
screen_write_cursorup(struct screen_write_ctx *ctx, u_int ny)
601
{
602
	struct screen	*s = ctx->s;
603
604
	if (ny == 0)
605
		ny = 1;
606
607
	if (s->cy < s->rupper) {
608
		/* Above region. */
609
		if (ny > s->cy)
610
			ny = s->cy;
611
	} else {
612
		/* Below region. */
613
		if (ny > s->cy - s->rupper)
614
			ny = s->cy - s->rupper;
615
	}
616
	if (s->cx == screen_size_x(s))
617
	    s->cx--;
618
	if (ny == 0)
619
		return;
620
621
	s->cy -= ny;
622
}
623
624
/* Cursor down by ny. */
625
void
626
screen_write_cursordown(struct screen_write_ctx *ctx, u_int ny)
627
{
628
	struct screen	*s = ctx->s;
629
630
	if (ny == 0)
631
		ny = 1;
632
633
	if (s->cy > s->rlower) {
634
		/* Below region. */
635
		if (ny > screen_size_y(s) - 1 - s->cy)
636
			ny = screen_size_y(s) - 1 - s->cy;
637
	} else {
638
		/* Above region. */
639
		if (ny > s->rlower - s->cy)
640
			ny = s->rlower - s->cy;
641
	}
642
	if (s->cx == screen_size_x(s))
643
	    s->cx--;
644
	if (ny == 0)
645
		return;
646
647
	s->cy += ny;
648
}
649
650
/* Cursor right by nx. */
651
void
652
screen_write_cursorright(struct screen_write_ctx *ctx, u_int nx)
653
{
654
	struct screen	*s = ctx->s;
655
656
	if (nx == 0)
657
		nx = 1;
658
659
	if (nx > screen_size_x(s) - 1 - s->cx)
660
		nx = screen_size_x(s) - 1 - s->cx;
661
	if (nx == 0)
662
		return;
663
664
	s->cx += nx;
665
}
666
667
/* Cursor left by nx. */
668
void
669
screen_write_cursorleft(struct screen_write_ctx *ctx, u_int nx)
670
{
671
	struct screen	*s = ctx->s;
672
673
	if (nx == 0)
674
		nx = 1;
675
676
	if (nx > s->cx)
677
		nx = s->cx;
678
	if (nx == 0)
679
		return;
680
681
	s->cx -= nx;
682
}
683
684
/* Backspace; cursor left unless at start of wrapped line when can move up. */
685
void
686
screen_write_backspace(struct screen_write_ctx *ctx)
687
{
688
	struct screen		*s = ctx->s;
689
	struct grid_line	*gl;
690
691
	if (s->cx == 0) {
692
		if (s->cy == 0)
693
			return;
694
		gl = &s->grid->linedata[s->grid->hsize + s->cy - 1];
695
		if (gl->flags & GRID_LINE_WRAPPED) {
696
			s->cy--;
697
			s->cx = screen_size_x(s) - 1;
698
		}
699
	} else
700
		s->cx--;
701
}
702
703
/* VT100 alignment test. */
704
void
705
screen_write_alignmenttest(struct screen_write_ctx *ctx)
706
{
707
	struct screen		*s = ctx->s;
708
	struct tty_ctx	 	 ttyctx;
709
	struct grid_cell       	 gc;
710
	u_int			 xx, yy;
711
712
	screen_write_initctx(ctx, &ttyctx);
713
714
	memcpy(&gc, &grid_default_cell, sizeof gc);
715
	utf8_set(&gc.data, 'E');
716
717
	for (yy = 0; yy < screen_size_y(s); yy++) {
718
		for (xx = 0; xx < screen_size_x(s); xx++)
719
			grid_view_set_cell(s->grid, xx, yy, &gc);
720
	}
721
722
	s->cx = 0;
723
	s->cy = 0;
724
725
	s->rupper = 0;
726
	s->rlower = screen_size_y(s) - 1;
727
728
	screen_write_collect_clear(ctx, 0, screen_size_y(s) - 1);
729
	tty_write(tty_cmd_alignmenttest, &ttyctx);
730
}
731
732
/* Insert nx characters. */
733
void
734
screen_write_insertcharacter(struct screen_write_ctx *ctx, u_int nx, u_int bg)
735
{
736
	struct screen	*s = ctx->s;
737
	struct tty_ctx	 ttyctx;
738
739
	if (nx == 0)
740
		nx = 1;
741
742
	if (nx > screen_size_x(s) - s->cx)
743
		nx = screen_size_x(s) - s->cx;
744
	if (nx == 0)
745
		return;
746
747
	if (s->cx > screen_size_x(s) - 1)
748
		return;
749
750
	screen_write_initctx(ctx, &ttyctx);
751
	ttyctx.bg = bg;
752
753
	grid_view_insert_cells(s->grid, s->cx, s->cy, nx, bg);
754
755
	screen_write_collect_flush(ctx, 0);
756
	ttyctx.num = nx;
757
	tty_write(tty_cmd_insertcharacter, &ttyctx);
758
}
759
760
/* Delete nx characters. */
761
void
762
screen_write_deletecharacter(struct screen_write_ctx *ctx, u_int nx, u_int bg)
763
{
764
	struct screen	*s = ctx->s;
765
	struct tty_ctx	 ttyctx;
766
767
	if (nx == 0)
768
		nx = 1;
769
770
	if (nx > screen_size_x(s) - s->cx)
771
		nx = screen_size_x(s) - s->cx;
772
	if (nx == 0)
773
		return;
774
775
	if (s->cx > screen_size_x(s) - 1)
776
		return;
777
778
	screen_write_initctx(ctx, &ttyctx);
779
	ttyctx.bg = bg;
780
781
	grid_view_delete_cells(s->grid, s->cx, s->cy, nx, bg);
782
783
	screen_write_collect_flush(ctx, 0);
784
	ttyctx.num = nx;
785
	tty_write(tty_cmd_deletecharacter, &ttyctx);
786
}
787
788
/* Clear nx characters. */
789
void
790
screen_write_clearcharacter(struct screen_write_ctx *ctx, u_int nx, u_int bg)
791
{
792
	struct screen	*s = ctx->s;
793
	struct tty_ctx	 ttyctx;
794
795
	if (nx == 0)
796
		nx = 1;
797
798
	if (nx > screen_size_x(s) - s->cx)
799
		nx = screen_size_x(s) - s->cx;
800
	if (nx == 0)
801
		return;
802
803
	if (s->cx > screen_size_x(s) - 1)
804
		return;
805
806
	screen_write_initctx(ctx, &ttyctx);
807
	ttyctx.bg = bg;
808
809
	grid_view_clear(s->grid, s->cx, s->cy, nx, 1, bg);
810
811
	screen_write_collect_flush(ctx, 0);
812
	ttyctx.num = nx;
813
	tty_write(tty_cmd_clearcharacter, &ttyctx);
814
}
815
816
/* Insert ny lines. */
817
void
818
screen_write_insertline(struct screen_write_ctx *ctx, u_int ny, u_int bg)
819
{
820
	struct screen	*s = ctx->s;
821
	struct grid	*gd = s->grid;
822
	struct tty_ctx	 ttyctx;
823
824
	if (ny == 0)
825
		ny = 1;
826
827
	if (s->cy < s->rupper || s->cy > s->rlower) {
828
		if (ny > screen_size_y(s) - s->cy)
829
			ny = screen_size_y(s) - s->cy;
830
		if (ny == 0)
831
			return;
832
833
		screen_write_initctx(ctx, &ttyctx);
834
		ttyctx.bg = bg;
835
836
		grid_view_insert_lines(gd, s->cy, ny, bg);
837
838
		screen_write_collect_flush(ctx, 0);
839
		ttyctx.num = ny;
840
		tty_write(tty_cmd_insertline, &ttyctx);
841
		return;
842
	}
843
844
	if (ny > s->rlower + 1 - s->cy)
845
		ny = s->rlower + 1 - s->cy;
846
	if (ny == 0)
847
		return;
848
849
	screen_write_initctx(ctx, &ttyctx);
850
	ttyctx.bg = bg;
851
852
	if (s->cy < s->rupper || s->cy > s->rlower)
853
		grid_view_insert_lines(gd, s->cy, ny, bg);
854
	else
855
		grid_view_insert_lines_region(gd, s->rlower, s->cy, ny, bg);
856
857
	screen_write_collect_flush(ctx, 0);
858
	ttyctx.num = ny;
859
	tty_write(tty_cmd_insertline, &ttyctx);
860
}
861
862
/* Delete ny lines. */
863
void
864
screen_write_deleteline(struct screen_write_ctx *ctx, u_int ny, u_int bg)
865
{
866
	struct screen	*s = ctx->s;
867
	struct grid	*gd = s->grid;
868
	struct tty_ctx	 ttyctx;
869
870
	if (ny == 0)
871
		ny = 1;
872
873
	if (s->cy < s->rupper || s->cy > s->rlower) {
874
		if (ny > screen_size_y(s) - s->cy)
875
			ny = screen_size_y(s) - s->cy;
876
		if (ny == 0)
877
			return;
878
879
		screen_write_initctx(ctx, &ttyctx);
880
		ttyctx.bg = bg;
881
882
		grid_view_delete_lines(gd, s->cy, ny, bg);
883
884
		screen_write_collect_flush(ctx, 0);
885
		ttyctx.num = ny;
886
		tty_write(tty_cmd_deleteline, &ttyctx);
887
		return;
888
	}
889
890
	if (ny > s->rlower + 1 - s->cy)
891
		ny = s->rlower + 1 - s->cy;
892
	if (ny == 0)
893
		return;
894
895
	screen_write_initctx(ctx, &ttyctx);
896
	ttyctx.bg = bg;
897
898
	if (s->cy < s->rupper || s->cy > s->rlower)
899
		grid_view_delete_lines(gd, s->cy, ny, bg);
900
	else
901
		grid_view_delete_lines_region(gd, s->rlower, s->cy, ny, bg);
902
903
	screen_write_collect_flush(ctx, 0);
904
	ttyctx.num = ny;
905
	tty_write(tty_cmd_deleteline, &ttyctx);
906
}
907
908
/* Clear line at cursor. */
909
void
910
screen_write_clearline(struct screen_write_ctx *ctx, u_int bg)
911
{
912
	struct screen		*s = ctx->s;
913
	struct grid_line	*gl;
914
	struct tty_ctx		 ttyctx;
915
	u_int			 sx = screen_size_x(s);
916
917
	gl = &s->grid->linedata[s->grid->hsize + s->cy];
918
	if (gl->cellsize == 0 && bg == 8)
919
		return;
920
921
	screen_write_initctx(ctx, &ttyctx);
922
	ttyctx.bg = bg;
923
924
	grid_view_clear(s->grid, 0, s->cy, sx, 1, bg);
925
926
	screen_write_collect_clear(ctx, s->cy, 1);
927
	screen_write_collect_flush(ctx, 0);
928
	tty_write(tty_cmd_clearline, &ttyctx);
929
}
930
931
/* Clear to end of line from cursor. */
932
void
933
screen_write_clearendofline(struct screen_write_ctx *ctx, u_int bg)
934
{
935
	struct screen		*s = ctx->s;
936
	struct grid_line	*gl;
937
	struct tty_ctx		 ttyctx;
938
	u_int			 sx = screen_size_x(s);
939
940
	gl = &s->grid->linedata[s->grid->hsize + s->cy];
941
	if (s->cx > sx - 1 || (s->cx >= gl->cellsize && bg == 8))
942
		return;
943
944
	screen_write_initctx(ctx, &ttyctx);
945
	ttyctx.bg = bg;
946
947
	grid_view_clear(s->grid, s->cx, s->cy, sx - s->cx, 1, bg);
948
949
	if (s->cx == 0)
950
		screen_write_collect_clear(ctx, s->cy, 1);
951
	screen_write_collect_flush(ctx, 0);
952
	tty_write(tty_cmd_clearendofline, &ttyctx);
953
}
954
955
/* Clear to start of line from cursor. */
956
void
957
screen_write_clearstartofline(struct screen_write_ctx *ctx, u_int bg)
958
{
959
	struct screen	*s = ctx->s;
960
	struct tty_ctx	 ttyctx;
961
	u_int		 sx = screen_size_x(s);
962
963
	screen_write_initctx(ctx, &ttyctx);
964
	ttyctx.bg = bg;
965
966
	if (s->cx > sx - 1)
967
		grid_view_clear(s->grid, 0, s->cy, sx, 1, bg);
968
	else
969
		grid_view_clear(s->grid, 0, s->cy, s->cx + 1, 1, bg);
970
971
	if (s->cx > sx - 1)
972
		screen_write_collect_clear(ctx, s->cy, 1);
973
	screen_write_collect_flush(ctx, 0);
974
	tty_write(tty_cmd_clearstartofline, &ttyctx);
975
}
976
977
/* Move cursor to px,py. */
978
void
979
screen_write_cursormove(struct screen_write_ctx *ctx, u_int px, u_int py)
980
{
981
	struct screen	*s = ctx->s;
982
983
	if (px > screen_size_x(s) - 1)
984
		px = screen_size_x(s) - 1;
985
	if (py > screen_size_y(s) - 1)
986
		py = screen_size_y(s) - 1;
987
988
	s->cx = px;
989
	s->cy = py;
990
}
991
992
/* Reverse index (up with scroll). */
993
void
994
screen_write_reverseindex(struct screen_write_ctx *ctx, u_int bg)
995
{
996
	struct screen	*s = ctx->s;
997
	struct tty_ctx	 ttyctx;
998
999
	screen_write_initctx(ctx, &ttyctx);
1000
	ttyctx.bg = bg;
1001
1002
	if (s->cy == s->rupper)
1003
		grid_view_scroll_region_down(s->grid, s->rupper, s->rlower, bg);
1004
	else if (s->cy > 0)
1005
		s->cy--;
1006
1007
	screen_write_collect_flush(ctx, 0);
1008
	tty_write(tty_cmd_reverseindex, &ttyctx);
1009
}
1010
1011
/* Set scroll region. */
1012
void
1013
screen_write_scrollregion(struct screen_write_ctx *ctx, u_int rupper,
1014
    u_int rlower)
1015
{
1016
	struct screen	*s = ctx->s;
1017
1018
	if (rupper > screen_size_y(s) - 1)
1019
		rupper = screen_size_y(s) - 1;
1020
	if (rlower > screen_size_y(s) - 1)
1021
		rlower = screen_size_y(s) - 1;
1022
	if (rupper >= rlower)	/* cannot be one line */
1023
		return;
1024
1025
	screen_write_collect_flush(ctx, 0);
1026
1027
	/* Cursor moves to top-left. */
1028
	s->cx = 0;
1029
	s->cy = 0;
1030
1031
	s->rupper = rupper;
1032
	s->rlower = rlower;
1033
}
1034
1035
/* Line feed. */
1036
void
1037
screen_write_linefeed(struct screen_write_ctx *ctx, int wrapped, u_int bg)
1038
{
1039
	struct screen		*s = ctx->s;
1040
	struct grid		*gd = s->grid;
1041
	struct grid_line	*gl;
1042
1043
	gl = &gd->linedata[gd->hsize + s->cy];
1044
	if (wrapped)
1045
		gl->flags |= GRID_LINE_WRAPPED;
1046
	else
1047
		gl->flags &= ~GRID_LINE_WRAPPED;
1048
1049
	log_debug("%s: at %u,%u (region %u-%u)", __func__, s->cx, s->cy,
1050
	    s->rupper, s->rlower);
1051
1052
	if (bg != ctx->bg) {
1053
		screen_write_collect_flush(ctx, 1);
1054
		ctx->bg = bg;
1055
	}
1056
1057
	if (s->cy == s->rlower) {
1058
		grid_view_scroll_region_up(gd, s->rupper, s->rlower, bg);
1059
		screen_write_collect_scroll(ctx);
1060
		ctx->scrolled++;
1061
	} else if (s->cy < screen_size_y(s) - 1)
1062
		s->cy++;
1063
}
1064
1065
/* Scroll up. */
1066
void
1067
screen_write_scrollup(struct screen_write_ctx *ctx, u_int lines, u_int bg)
1068
{
1069
	struct screen	*s = ctx->s;
1070
	struct grid	*gd = s->grid;
1071
	u_int		 i;
1072
1073
	if (lines == 0)
1074
		lines = 1;
1075
	else if (lines > s->rlower - s->rupper + 1)
1076
		lines = s->rlower - s->rupper + 1;
1077
1078
	if (bg != ctx->bg) {
1079
		screen_write_collect_flush(ctx, 1);
1080
		ctx->bg = bg;
1081
	}
1082
1083
	for (i = 0; i < lines; i++) {
1084
		grid_view_scroll_region_up(gd, s->rupper, s->rlower, bg);
1085
		screen_write_collect_scroll(ctx);
1086
	}
1087
	ctx->scrolled += lines;
1088
}
1089
1090
/* Carriage return (cursor to start of line). */
1091
void
1092
screen_write_carriagereturn(struct screen_write_ctx *ctx)
1093
{
1094
	struct screen	*s = ctx->s;
1095
1096
	s->cx = 0;
1097
}
1098
1099
/* Clear to end of screen from cursor. */
1100
void
1101
screen_write_clearendofscreen(struct screen_write_ctx *ctx, u_int bg)
1102
{
1103
	struct screen	*s = ctx->s;
1104
	struct grid	*gd = s->grid;
1105
	struct tty_ctx	 ttyctx;
1106
	u_int		 sx = screen_size_x(s), sy = screen_size_y(s);
1107
1108
	screen_write_initctx(ctx, &ttyctx);
1109
	ttyctx.bg = bg;
1110
1111
	/* Scroll into history if it is enabled and clearing entire screen. */
1112
	if (s->cx == 0 && s->cy == 0 && (gd->flags & GRID_HISTORY))
1113
		grid_view_clear_history(gd, bg);
1114
	else {
1115
		if (s->cx <= sx - 1)
1116
			grid_view_clear(gd, s->cx, s->cy, sx - s->cx, 1, bg);
1117
		grid_view_clear(gd, 0, s->cy + 1, sx, sy - (s->cy + 1), bg);
1118
	}
1119
1120
	screen_write_collect_clear(ctx, s->cy + 1, sy - (s->cy + 1));
1121
	screen_write_collect_flush(ctx, 0);
1122
	tty_write(tty_cmd_clearendofscreen, &ttyctx);
1123
}
1124
1125
/* Clear to start of screen. */
1126
void
1127
screen_write_clearstartofscreen(struct screen_write_ctx *ctx, u_int bg)
1128
{
1129
	struct screen	*s = ctx->s;
1130
	struct tty_ctx	 ttyctx;
1131
	u_int		 sx = screen_size_x(s);
1132
1133
	screen_write_initctx(ctx, &ttyctx);
1134
	ttyctx.bg = bg;
1135
1136
	if (s->cy > 0)
1137
		grid_view_clear(s->grid, 0, 0, sx, s->cy, bg);
1138
	if (s->cx > sx - 1)
1139
		grid_view_clear(s->grid, 0, s->cy, sx, 1, bg);
1140
	else
1141
		grid_view_clear(s->grid, 0, s->cy, s->cx + 1, 1, bg);
1142
1143
	screen_write_collect_clear(ctx, 0, s->cy);
1144
	screen_write_collect_flush(ctx, 0);
1145
	tty_write(tty_cmd_clearstartofscreen, &ttyctx);
1146
}
1147
1148
/* Clear entire screen. */
1149
void
1150
screen_write_clearscreen(struct screen_write_ctx *ctx, u_int bg)
1151
{
1152
	struct screen	*s = ctx->s;
1153
	struct tty_ctx	 ttyctx;
1154
	u_int		 sx = screen_size_x(s), sy = screen_size_y(s);
1155
1156
	screen_write_initctx(ctx, &ttyctx);
1157
	ttyctx.bg = bg;
1158
1159
	/* Scroll into history if it is enabled. */
1160
	if (s->grid->flags & GRID_HISTORY)
1161
		grid_view_clear_history(s->grid, bg);
1162
	else
1163
		grid_view_clear(s->grid, 0, 0, sx, sy, bg);
1164
1165
	screen_write_collect_clear(ctx, 0, sy);
1166
	tty_write(tty_cmd_clearscreen, &ttyctx);
1167
}
1168
1169
/* Clear entire history. */
1170
void
1171
screen_write_clearhistory(struct screen_write_ctx *ctx)
1172
{
1173
	struct screen	*s = ctx->s;
1174
	struct grid	*gd = s->grid;
1175
1176
	grid_move_lines(gd, 0, gd->hsize, gd->sy, 8);
1177
	gd->hscrolled = gd->hsize = 0;
1178
}
1179
1180
/* Clear a collected line. */
1181
static void
1182
screen_write_collect_clear(struct screen_write_ctx *ctx, u_int y, u_int n)
1183
{
1184
	struct screen_write_collect_item	*ci, *tmp;
1185
	u_int					 i;
1186
	size_t					 size;
1187
1188
	for (i = y ; i < y + n; i++) {
1189
		if (TAILQ_EMPTY(&ctx->list[i].items))
1190
			continue;
1191
		size = 0;
1192
		TAILQ_FOREACH_SAFE(ci, &ctx->list[i].items, entry, tmp) {
1193
			size += ci->used;
1194
			TAILQ_REMOVE(&ctx->list[i].items, ci, entry);
1195
			free(ci);
1196
		}
1197
		ctx->skipped += size;
1198
		log_debug("%s: dropped %zu bytes (line %u)", __func__, size, i);
1199
	}
1200
}
1201
1202
/* Scroll collected lines up. */
1203
static void
1204
screen_write_collect_scroll(struct screen_write_ctx *ctx)
1205
{
1206
	struct screen				*s = ctx->s;
1207
	struct screen_write_collect_line	*cl;
1208
	u_int					 y;
1209
1210
	log_debug("%s: at %u,%u (region %u-%u)", __func__, s->cx, s->cy,
1211
	    s->rupper, s->rlower);
1212
1213
	screen_write_collect_clear(ctx, s->rupper, 1);
1214
	for (y = s->rupper; y < s->rlower; y++) {
1215
		cl = &ctx->list[y + 1];
1216
		TAILQ_CONCAT(&ctx->list[y].items, &cl->items, entry);
1217
		TAILQ_INIT(&cl->items);
1218
	}
1219
}
1220
1221
/* Flush collected lines. */
1222
static void
1223
screen_write_collect_flush(struct screen_write_ctx *ctx, int scroll_only)
1224
{
1225
	struct screen				*s = ctx->s;
1226
	struct screen_write_collect_item	*ci, *tmp;
1227
	u_int					 y, cx, cy, items = 0;
1228
	struct tty_ctx				 ttyctx;
1229
	size_t					 written = 0;
1230
1231
	if (ctx->scrolled != 0) {
1232
		log_debug("%s: scrolled %u (region %u-%u)", __func__,
1233
		    ctx->scrolled, s->rupper, s->rlower);
1234
		if (ctx->scrolled > s->rlower - s->rupper + 1)
1235
			ctx->scrolled = s->rlower - s->rupper + 1;
1236
1237
		screen_write_initctx(ctx, &ttyctx);
1238
		ttyctx.num = ctx->scrolled;
1239
		ttyctx.bg = ctx->bg;
1240
		tty_write(tty_cmd_scrollup, &ttyctx);
1241
	}
1242
	ctx->scrolled = 0;
1243
	ctx->bg = 8;
1244
1245
	if (scroll_only)
1246
		return;
1247
1248
	cx = s->cx; cy = s->cy;
1249
	for (y = 0; y < screen_size_y(s); y++) {
1250
		TAILQ_FOREACH_SAFE(ci, &ctx->list[y].items, entry, tmp) {
1251
			screen_write_cursormove(ctx, ci->x, y);
1252
			screen_write_initctx(ctx, &ttyctx);
1253
			ttyctx.cell = &ci->gc;
1254
			ttyctx.wrapped = ci->wrapped;
1255
			ttyctx.ptr = ci->data;
1256
			ttyctx.num = ci->used;
1257
			tty_write(tty_cmd_cells, &ttyctx);
1258
1259
			items++;
1260
			written += ci->used;
1261
1262
			TAILQ_REMOVE(&ctx->list[y].items, ci, entry);
1263
			free(ci);
1264
		}
1265
	}
1266
	s->cx = cx; s->cy = cy;
1267
1268
	log_debug("%s: flushed %u items (%zu bytes)", __func__, items, written);
1269
	ctx->written += written;
1270
}
1271
1272
/* Finish and store collected cells. */
1273
void
1274
screen_write_collect_end(struct screen_write_ctx *ctx)
1275
{
1276
	struct screen				*s = ctx->s;
1277
	struct screen_write_collect_item	*ci = ctx->item;
1278
	struct grid_cell			 gc;
1279
	u_int					 xx;
1280
1281
	if (ci->used == 0)
1282
		return;
1283
	ci->data[ci->used] = '\0';
1284
1285
	ci->x = s->cx;
1286
	TAILQ_INSERT_TAIL(&ctx->list[s->cy].items, ci, entry);
1287
	ctx->item = xcalloc(1, sizeof *ctx->item);
1288
1289
	log_debug("%s: %u %s (at %u,%u)", __func__, ci->used, ci->data, s->cx,
1290
	    s->cy);
1291
1292
	if (s->cx != 0) {
1293
		for (xx = s->cx; xx > 0; xx--) {
1294
			grid_view_get_cell(s->grid, xx, s->cy, &gc);
1295
			if (~gc.flags & GRID_FLAG_PADDING)
1296
				break;
1297
			grid_view_set_cell(s->grid, xx, s->cy, &grid_default_cell);
1298
		}
1299
		if (gc.data.width > 1)
1300
			grid_view_set_cell(s->grid, xx, s->cy, &grid_default_cell);
1301
	}
1302
1303
	memcpy(&gc, &ci->gc, sizeof gc);
1304
	grid_view_set_cells(s->grid, s->cx, s->cy, &gc, ci->data, ci->used);
1305
	s->cx += ci->used;
1306
1307
	for (xx = s->cx; xx < screen_size_x(s); xx++) {
1308
		grid_view_get_cell(s->grid, xx, s->cy, &gc);
1309
		if (~gc.flags & GRID_FLAG_PADDING)
1310
			break;
1311
		grid_view_set_cell(s->grid, xx, s->cy, &grid_default_cell);
1312
	}
1313
}
1314
1315
/* Write cell data, collecting if necessary. */
1316
void
1317
screen_write_collect_add(struct screen_write_ctx *ctx,
1318
    const struct grid_cell *gc)
1319
{
1320
	struct screen				*s = ctx->s;
1321
	struct screen_write_collect_item	*ci;
1322
	u_int					 sx = screen_size_x(s);
1323
	int					 collect;
1324
1325
	/*
1326
	 * Don't need to check that the attributes and whatnot are still the
1327
	 * same - input_parse will end the collection when anything that isn't
1328
	 * a plain character is encountered. Also nothing should make it here
1329
	 * that isn't a single ASCII character.
1330
	 */
1331
1332
	collect = 1;
1333
	if (gc->data.width != 1 || gc->data.size != 1)
1334
		collect = 0;
1335
	else if (gc->attr & GRID_ATTR_CHARSET)
1336
		collect = 0;
1337
	else if (~s->mode & MODE_WRAP)
1338
		collect = 0;
1339
	else if (s->mode & MODE_INSERT)
1340
		collect = 0;
1341
	else if (s->sel.flag)
1342
		collect = 0;
1343
	if (!collect) {
1344
		screen_write_collect_end(ctx);
1345
		screen_write_collect_flush(ctx, 0);
1346
		screen_write_cell(ctx, gc);
1347
		return;
1348
	}
1349
	ctx->cells++;
1350
1351
	if (s->cx > sx - 1 || ctx->item->used > sx - 1 - s->cx)
1352
		screen_write_collect_end(ctx);
1353
	ci = ctx->item; /* may have changed */
1354
1355
	if (s->cx > sx - 1) {
1356
		log_debug("%s: wrapped at %u,%u", __func__, s->cx, s->cy);
1357
		ci->wrapped = 1;
1358
		screen_write_linefeed(ctx, 1, 8);
1359
		s->cx = 0;
1360
	}
1361
1362
	if (ci->used == 0)
1363
		memcpy(&ci->gc, gc, sizeof ci->gc);
1364
	ci->data[ci->used++] = gc->data.data[0];
1365
	if (ci->used == (sizeof ci->data) - 1)
1366
		screen_write_collect_end(ctx);
1367
}
1368
1369
/* Write cell data. */
1370
void
1371
screen_write_cell(struct screen_write_ctx *ctx, const struct grid_cell *gc)
1372
{
1373
	struct screen		*s = ctx->s;
1374
	struct grid		*gd = s->grid;
1375
	struct grid_line	*gl;
1376
	struct grid_cell_entry	*gce;
1377
	struct grid_cell 	 tmp_gc, now_gc;
1378
	struct tty_ctx		 ttyctx;
1379
	u_int			 sx = screen_size_x(s), sy = screen_size_y(s);
1380
	u_int		 	 width = gc->data.width, xx, last, cx, cy;
1381
	int			 selected, skip = 1;
1382
1383
	/* Ignore padding cells. */
1384
	if (gc->flags & GRID_FLAG_PADDING)
1385
		return;
1386
	ctx->cells++;
1387
1388
	/* If the width is zero, combine onto the previous character. */
1389
	if (width == 0) {
1390
		screen_write_collect_flush(ctx, 0);
1391
		if ((gc = screen_write_combine(ctx, &gc->data, &xx)) != 0) {
1392
			cx = s->cx; cy = s->cy;
1393
			screen_write_cursormove(ctx, xx, s->cy);
1394
			screen_write_initctx(ctx, &ttyctx);
1395
			ttyctx.cell = gc;
1396
			tty_write(tty_cmd_cell, &ttyctx);
1397
			s->cx = cx; s->cy = cy;
1398
		}
1399
		return;
1400
	}
1401
1402
	/* Flush any existing scrolling. */
1403
	screen_write_collect_flush(ctx, 1);
1404
1405
	/* If this character doesn't fit, ignore it. */
1406
	if ((~s->mode & MODE_WRAP) &&
1407
	    width > 1 &&
1408
	    (width > sx || (s->cx != sx && s->cx > sx - width)))
1409
		return;
1410
1411
	/* If in insert mode, make space for the cells. */
1412
	if (s->mode & MODE_INSERT) {
1413
		grid_view_insert_cells(s->grid, s->cx, s->cy, width, 8);
1414
		skip = 0;
1415
	}
1416
1417
	/* Check this will fit on the current line and wrap if not. */
1418
	if ((s->mode & MODE_WRAP) && s->cx > sx - width) {
1419
		log_debug("%s: wrapped at %u,%u", __func__, s->cx, s->cy);
1420
		screen_write_linefeed(ctx, 1, 8);
1421
		s->cx = 0;
1422
		screen_write_collect_flush(ctx, 1);
1423
	}
1424
1425
	/* Sanity check cursor position. */
1426
	if (s->cx > sx - width || s->cy > sy - 1)
1427
		return;
1428
	screen_write_initctx(ctx, &ttyctx);
1429
1430
	/* Handle overwriting of UTF-8 characters. */
1431
	gl = &s->grid->linedata[s->grid->hsize + s->cy];
1432
	if (gl->flags & GRID_LINE_EXTENDED) {
1433
		grid_view_get_cell(gd, s->cx, s->cy, &now_gc);
1434
		if (screen_write_overwrite(ctx, &now_gc, width))
1435
			skip = 0;
1436
	}
1437
1438
	/*
1439
	 * If the new character is UTF-8 wide, fill in padding cells. Have
1440
	 * already ensured there is enough room.
1441
	 */
1442
	for (xx = s->cx + 1; xx < s->cx + width; xx++) {
1443
		log_debug("%s: new padding at %u,%u", __func__, xx, s->cy);
1444
		grid_view_set_cell(gd, xx, s->cy, &screen_write_pad_cell);
1445
		skip = 0;
1446
	}
1447
1448
	/* If no change, do not draw. */
1449
	if (skip) {
1450
		if (s->cx >= gl->cellsize)
1451
			skip = grid_cells_equal(gc, &grid_default_cell);
1452
		else {
1453
			gce = &gl->celldata[s->cx];
1454
			if (gce->flags & GRID_FLAG_EXTENDED)
1455
				skip = 0;
1456
			else if (gc->flags != gce->flags)
1457
				skip = 0;
1458
			else if (gc->attr != gce->data.attr)
1459
				skip = 0;
1460
			else if (gc->fg != gce->data.fg)
1461
				skip = 0;
1462
			else if (gc->bg != gce->data.bg)
1463
				skip = 0;
1464
			else if (gc->data.width != 1)
1465
				skip = 0;
1466
			else if (gc->data.size != 1)
1467
				skip = 0;
1468
			else if (gce->data.data != gc->data.data[0])
1469
				skip = 0;
1470
		}
1471
	}
1472
1473
	/* Update the selected flag and set the cell. */
1474
	selected = screen_check_selection(s, s->cx, s->cy);
1475
	if (selected && (~gc->flags & GRID_FLAG_SELECTED)) {
1476
		memcpy(&tmp_gc, gc, sizeof tmp_gc);
1477
		tmp_gc.flags |= GRID_FLAG_SELECTED;
1478
		grid_view_set_cell(gd, s->cx, s->cy, &tmp_gc);
1479
	} else if (!selected && (gc->flags & GRID_FLAG_SELECTED)) {
1480
		memcpy(&tmp_gc, gc, sizeof tmp_gc);
1481
		tmp_gc.flags &= ~GRID_FLAG_SELECTED;
1482
		grid_view_set_cell(gd, s->cx, s->cy, &tmp_gc);
1483
	} else if (!skip)
1484
		grid_view_set_cell(gd, s->cx, s->cy, gc);
1485
	if (selected)
1486
		skip = 0;
1487
1488
	/*
1489
	 * Move the cursor. If not wrapping, stick at the last character and
1490
	 * replace it.
1491
	 */
1492
	last = !(s->mode & MODE_WRAP);
1493
	if (s->cx <= sx - last - width)
1494
		s->cx += width;
1495
	else
1496
		s->cx = sx - last;
1497
1498
	/* Create space for character in insert mode. */
1499
	if (s->mode & MODE_INSERT) {
1500
		screen_write_collect_flush(ctx, 0);
1501
		ttyctx.num = width;
1502
		tty_write(tty_cmd_insertcharacter, &ttyctx);
1503
	}
1504
1505
	/* Write to the screen. */
1506
	if (!skip) {
1507
		if (selected) {
1508
			screen_select_cell(s, &tmp_gc, gc);
1509
			ttyctx.cell = &tmp_gc;
1510
		} else
1511
			ttyctx.cell = gc;
1512
		tty_write(tty_cmd_cell, &ttyctx);
1513
		ctx->written++;
1514
	} else
1515
		ctx->skipped++;
1516
}
1517
1518
/* Combine a UTF-8 zero-width character onto the previous. */
1519
static const struct grid_cell *
1520
screen_write_combine(struct screen_write_ctx *ctx, const struct utf8_data *ud,
1521
    u_int *xx)
1522
{
1523
	struct screen		*s = ctx->s;
1524
	struct grid		*gd = s->grid;
1525
	static struct grid_cell	 gc;
1526
	u_int			 n;
1527
1528
	/* Can't combine if at 0. */
1529
	if (s->cx == 0)
1530
		return (NULL);
1531
1532
	/* Empty data is out. */
1533
	if (ud->size == 0)
1534
		fatalx("UTF-8 data empty");
1535
1536
	/* Retrieve the previous cell. */
1537
	for (n = 1; n <= s->cx; n++) {
1538
		grid_view_get_cell(gd, s->cx - n, s->cy, &gc);
1539
		if (~gc.flags & GRID_FLAG_PADDING)
1540
			break;
1541
	}
1542
	if (n > s->cx)
1543
		return (NULL);
1544
	*xx = s->cx - n;
1545
1546
	/* Check there is enough space. */
1547
	if (gc.data.size + ud->size > sizeof gc.data.data)
1548
		return (NULL);
1549
1550
	log_debug("%s: %.*s onto %.*s at %u,%u", __func__, (int)ud->size,
1551
	    ud->data, (int)gc.data.size, gc.data.data, *xx, s->cy);
1552
1553
	/* Append the data. */
1554
	memcpy(gc.data.data + gc.data.size, ud->data, ud->size);
1555
	gc.data.size += ud->size;
1556
1557
	/* Set the new cell. */
1558
	grid_view_set_cell(gd, *xx, s->cy, &gc);
1559
1560
	return (&gc);
1561
}
1562
1563
/*
1564
 * UTF-8 wide characters are a bit of an annoyance. They take up more than one
1565
 * cell on the screen, so following cells must not be drawn by marking them as
1566
 * padding.
1567
 *
1568
 * So far, so good. The problem is, when overwriting a padding cell, or a UTF-8
1569
 * character, it is necessary to also overwrite any other cells which covered
1570
 * by the same character.
1571
 */
1572
static int
1573
screen_write_overwrite(struct screen_write_ctx *ctx, struct grid_cell *gc,
1574
    u_int width)
1575
{
1576
	struct screen		*s = ctx->s;
1577
	struct grid		*gd = s->grid;
1578
	struct grid_cell	 tmp_gc;
1579
	u_int			 xx;
1580
	int			 done = 0;
1581
1582
	if (gc->flags & GRID_FLAG_PADDING) {
1583
		/*
1584
		 * A padding cell, so clear any following and leading padding
1585
		 * cells back to the character. Don't overwrite the current
1586
		 * cell as that happens later anyway.
1587
		 */
1588
		xx = s->cx + 1;
1589
		while (--xx > 0) {
1590
			grid_view_get_cell(gd, xx, s->cy, &tmp_gc);
1591
			if (~tmp_gc.flags & GRID_FLAG_PADDING)
1592
				break;
1593
			log_debug("%s: padding at %u,%u", __func__, xx, s->cy);
1594
			grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
1595
		}
1596
1597
		/* Overwrite the character at the start of this padding. */
1598
		log_debug("%s: character at %u,%u", __func__, xx, s->cy);
1599
		grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
1600
		done = 1;
1601
	}
1602
1603
	/*
1604
	 * Overwrite any padding cells that belong to any UTF-8 characters
1605
	 * we'll be overwriting with the current character.
1606
	 */
1607
	if (width != 1 ||
1608
	    gc->data.width != 1 ||
1609
	    gc->flags & GRID_FLAG_PADDING) {
1610
		xx = s->cx + width - 1;
1611
		while (++xx < screen_size_x(s)) {
1612
			grid_view_get_cell(gd, xx, s->cy, &tmp_gc);
1613
			if (~tmp_gc.flags & GRID_FLAG_PADDING)
1614
				break;
1615
			log_debug("%s: overwrite at %u,%u", __func__, xx, s->cy);
1616
			grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
1617
			done = 1;
1618
		}
1619
	}
1620
1621
	return (done);
1622
}
1623
1624
/* Set external clipboard. */
1625
void
1626
screen_write_setselection(struct screen_write_ctx *ctx, u_char *str, u_int len)
1627
{
1628
	struct tty_ctx	ttyctx;
1629
1630
	screen_write_initctx(ctx, &ttyctx);
1631
	ttyctx.ptr = str;
1632
	ttyctx.num = len;
1633
1634
	tty_write(tty_cmd_setselection, &ttyctx);
1635
}
1636
1637
/* Write unmodified string. */
1638
void
1639
screen_write_rawstring(struct screen_write_ctx *ctx, u_char *str, u_int len)
1640
{
1641
	struct tty_ctx	ttyctx;
1642
1643
	screen_write_initctx(ctx, &ttyctx);
1644
	ttyctx.ptr = str;
1645
	ttyctx.num = len;
1646
1647
	tty_write(tty_cmd_rawstring, &ttyctx);
1648
}