Annotation of src/usr.bin/tmux/grid.c, Revision 1.55
1.55 ! nicm 1: /* $OpenBSD: grid.c,v 1.54 2016/07/15 00:49:08 nicm Exp $ */
1.1 nicm 2:
3: /*
1.50 nicm 4: * Copyright (c) 2008 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1 nicm 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:
1.20 nicm 21: #include <stdlib.h>
1.1 nicm 22: #include <string.h>
23:
24: #include "tmux.h"
25:
26: /*
27: * Grid data. This is the basic data structure that represents what is shown on
28: * screen.
29: *
30: * A grid is a grid of cells (struct grid_cell). Lines are not allocated until
31: * cells in that line are written to. The grid is split into history and
32: * viewable data with the history starting at row (line) 0 and extending to
33: * (hsize - 1); from hsize to hsize + (sy - 1) is the viewable data. All
34: * functions in this file work on absolute coordinates, grid-view.c has
35: * functions which work on the screen data.
36: */
37:
38: /* Default grid cell data. */
1.48 nicm 39: const struct grid_cell grid_default_cell = {
1.53 nicm 40: 0, 0, 8, 8, { { ' ' }, 0, 1, 1 }
1.48 nicm 41: };
42: const struct grid_cell_entry grid_default_entry = {
43: 0, { .data = { 0, 8, 8, ' ' } }
44: };
1.1 nicm 45:
1.48 nicm 46: void grid_reflow_copy(struct grid_line *, u_int, struct grid_line *l,
47: u_int, u_int);
1.43 nicm 48: void grid_reflow_join(struct grid *, u_int *, struct grid_line *, u_int);
49: void grid_reflow_split(struct grid *, u_int *, struct grid_line *, u_int,
50: u_int);
51: void grid_reflow_move(struct grid *, u_int *, struct grid_line *);
52: size_t grid_string_cells_fg(const struct grid_cell *, int *);
53: size_t grid_string_cells_bg(const struct grid_cell *, int *);
54: void grid_string_cells_code(const struct grid_cell *,
55: const struct grid_cell *, char *, size_t, int);
56:
1.48 nicm 57: /* Copy default into a cell. */
58: static void
59: grid_clear_cell(struct grid *gd, u_int px, u_int py)
60: {
61: gd->linedata[py].celldata[px] = grid_default_entry;
62: }
63:
1.43 nicm 64: /* Check grid y position. */
1.54 nicm 65: static int
1.1 nicm 66: grid_check_y(struct grid *gd, u_int py)
67: {
68: if ((py) >= (gd)->hsize + (gd)->sy) {
69: log_debug("y out of range: %u", py);
70: return (-1);
71: }
72: return (0);
73: }
1.23 nicm 74:
1.54 nicm 75: /* Compare grid cells. Return 1 if equal, 0 if not. */
76: int
77: grid_cells_equal(const struct grid_cell *gca, const struct grid_cell *gcb)
78: {
79: if (gca->fg != gcb->fg || gca->bg != gcb->bg)
80: return (0);
81: if (gca->attr != gcb->attr || gca->flags != gcb->flags)
82: return (0);
83: if (gca->data.width != gcb->data.width)
84: return (0);
85: if (gca->data.size != gcb->data.size)
86: return (0);
87: return (memcmp(gca->data.data, gcb->data.data, gca->data.size) == 0);
88: }
89:
1.1 nicm 90: /* Create a new grid. */
91: struct grid *
92: grid_create(u_int sx, u_int sy, u_int hlimit)
93: {
94: struct grid *gd;
95:
96: gd = xmalloc(sizeof *gd);
97: gd->sx = sx;
98: gd->sy = sy;
99:
1.7 nicm 100: gd->flags = GRID_HISTORY;
101:
1.55 ! nicm 102: gd->hscrolled = 0;
1.1 nicm 103: gd->hsize = 0;
104: gd->hlimit = hlimit;
105:
1.10 nicm 106: gd->linedata = xcalloc(gd->sy, sizeof *gd->linedata);
1.1 nicm 107:
108: return (gd);
109: }
110:
111: /* Destroy grid. */
112: void
113: grid_destroy(struct grid *gd)
114: {
1.10 nicm 115: struct grid_line *gl;
116: u_int yy;
1.1 nicm 117:
118: for (yy = 0; yy < gd->hsize + gd->sy; yy++) {
1.10 nicm 119: gl = &gd->linedata[yy];
1.20 nicm 120: free(gl->celldata);
1.48 nicm 121: free(gl->extddata);
1.1 nicm 122: }
123:
1.20 nicm 124: free(gd->linedata);
1.1 nicm 125:
1.20 nicm 126: free(gd);
1.1 nicm 127: }
128:
129: /* Compare grids. */
130: int
131: grid_compare(struct grid *ga, struct grid *gb)
132: {
1.10 nicm 133: struct grid_line *gla, *glb;
1.48 nicm 134: struct grid_cell gca, gcb;
1.1 nicm 135: u_int xx, yy;
136:
1.33 nicm 137: if (ga->sx != gb->sx || ga->sy != gb->sy)
1.1 nicm 138: return (1);
139:
140: for (yy = 0; yy < ga->sy; yy++) {
1.10 nicm 141: gla = &ga->linedata[yy];
142: glb = &gb->linedata[yy];
143: if (gla->cellsize != glb->cellsize)
1.1 nicm 144: return (1);
1.48 nicm 145: for (xx = 0; xx < gla->cellsize; xx++) {
146: grid_get_cell(ga, xx, yy, &gca);
147: grid_get_cell(gb, xx, yy, &gcb);
1.54 nicm 148: if (!grid_cells_equal(&gca, &gcb))
1.1 nicm 149: return (1);
150: }
151: }
152:
153: return (0);
154: }
155:
1.15 nicm 156: /*
157: * Collect lines from the history if at the limit. Free the top (oldest) 10%
158: * and shift up.
159: */
1.1 nicm 160: void
1.15 nicm 161: grid_collect_history(struct grid *gd)
1.1 nicm 162: {
163: u_int yy;
164:
1.15 nicm 165: if (gd->hsize < gd->hlimit)
166: return;
167:
168: yy = gd->hlimit / 10;
169: if (yy < 1)
170: yy = 1;
171:
172: grid_move_lines(gd, 0, yy, gd->hsize + gd->sy - yy);
173: gd->hsize -= yy;
1.55 ! nicm 174: if (gd->hscrolled > gd->hsize)
! 175: gd->hscrolled = gd->hsize;
1.15 nicm 176: }
177:
1.17 nicm 178: /*
1.15 nicm 179: * Scroll the entire visible screen, moving one line into the history. Just
180: * allocate a new line at the bottom and move the history size indicator.
181: */
182: void
183: grid_scroll_history(struct grid *gd)
184: {
185: u_int yy;
1.1 nicm 186:
187: yy = gd->hsize + gd->sy;
1.41 nicm 188: gd->linedata = xreallocarray(gd->linedata, yy + 1,
189: sizeof *gd->linedata);
1.15 nicm 190: memset(&gd->linedata[yy], 0, sizeof gd->linedata[yy]);
1.17 nicm 191:
1.55 ! nicm 192: gd->hscrolled++;
1.15 nicm 193: gd->hsize++;
194: }
1.1 nicm 195:
1.46 nicm 196: /* Clear the history. */
197: void
198: grid_clear_history(struct grid *gd)
199: {
200: grid_clear_lines(gd, 0, gd->hsize);
201: grid_move_lines(gd, 0, gd->hsize, gd->sy);
202:
1.55 ! nicm 203: gd->hscrolled = 0;
1.46 nicm 204: gd->hsize = 0;
1.55 ! nicm 205:
1.46 nicm 206: gd->linedata = xreallocarray(gd->linedata, gd->sy,
207: sizeof *gd->linedata);
208: }
209:
1.15 nicm 210: /* Scroll a region up, moving the top line into the history. */
211: void
212: grid_scroll_history_region(struct grid *gd, u_int upper, u_int lower)
213: {
214: struct grid_line *gl_history, *gl_upper, *gl_lower;
215: u_int yy;
216:
217: /* Create a space for a new line. */
218: yy = gd->hsize + gd->sy;
1.41 nicm 219: gd->linedata = xreallocarray(gd->linedata, yy + 1,
220: sizeof *gd->linedata);
1.1 nicm 221:
1.15 nicm 222: /* Move the entire screen down to free a space for this line. */
223: gl_history = &gd->linedata[gd->hsize];
224: memmove(gl_history + 1, gl_history, gd->sy * sizeof *gl_history);
225:
226: /* Adjust the region and find its start and end. */
227: upper++;
228: gl_upper = &gd->linedata[upper];
229: lower++;
230: gl_lower = &gd->linedata[lower];
231:
232: /* Move the line into the history. */
233: memcpy(gl_history, gl_upper, sizeof *gl_history);
234:
235: /* Then move the region up and clear the bottom line. */
236: memmove(gl_upper, gl_upper + 1, (lower - upper) * sizeof *gl_upper);
237: memset(gl_lower, 0, sizeof *gl_lower);
238:
239: /* Move the history offset down over the line. */
1.55 ! nicm 240: gd->hscrolled++;
1.1 nicm 241: gd->hsize++;
242: }
243:
244: /* Expand line to fit to cell. */
245: void
1.14 nicm 246: grid_expand_line(struct grid *gd, u_int py, u_int sx)
1.1 nicm 247: {
1.10 nicm 248: struct grid_line *gl;
1.14 nicm 249: u_int xx;
1.1 nicm 250:
1.10 nicm 251: gl = &gd->linedata[py];
1.14 nicm 252: if (sx <= gl->cellsize)
1.1 nicm 253: return;
254:
1.41 nicm 255: gl->celldata = xreallocarray(gl->celldata, sx, sizeof *gl->celldata);
1.10 nicm 256: for (xx = gl->cellsize; xx < sx; xx++)
1.48 nicm 257: grid_clear_cell(gd, xx, py);
1.10 nicm 258: gl->cellsize = sx;
1.1 nicm 259: }
260:
1.26 nicm 261: /* Peek at grid line. */
262: const struct grid_line *
263: grid_peek_line(struct grid *gd, u_int py)
264: {
265: if (grid_check_y(gd, py) != 0)
266: return (NULL);
267: return (&gd->linedata[py]);
268: }
269:
1.1 nicm 270: /* Get cell for reading. */
1.48 nicm 271: void
272: grid_get_cell(struct grid *gd, u_int px, u_int py, struct grid_cell *gc)
1.1 nicm 273: {
1.48 nicm 274: struct grid_line *gl;
275: struct grid_cell_entry *gce;
276:
277: if (grid_check_y(gd, py) != 0 || px >= gd->linedata[py].cellsize) {
278: memcpy(gc, &grid_default_cell, sizeof *gc);
279: return;
280: }
1.1 nicm 281:
1.48 nicm 282: gl = &gd->linedata[py];
283: gce = &gl->celldata[px];
1.1 nicm 284:
1.48 nicm 285: if (gce->flags & GRID_FLAG_EXTENDED) {
286: if (gce->offset >= gl->extdsize)
287: memcpy(gc, &grid_default_cell, sizeof *gc);
288: else
289: memcpy(gc, &gl->extddata[gce->offset], sizeof *gc);
290: return;
291: }
1.1 nicm 292:
1.53 nicm 293: gc->flags = gce->flags & ~(GRID_FLAG_FG256|GRID_FLAG_BG256);
1.48 nicm 294: gc->attr = gce->data.attr;
295: gc->fg = gce->data.fg;
1.53 nicm 296: if (gce->flags & GRID_FLAG_FG256)
297: gc->fg |= COLOUR_FLAG_256;
1.48 nicm 298: gc->bg = gce->data.bg;
1.53 nicm 299: if (gce->flags & GRID_FLAG_BG256)
300: gc->bg |= COLOUR_FLAG_256;
1.48 nicm 301: utf8_set(&gc->data, gce->data.data);
1.1 nicm 302: }
303:
304: /* Set cell at relative position. */
305: void
1.31 nicm 306: grid_set_cell(struct grid *gd, u_int px, u_int py, const struct grid_cell *gc)
1.1 nicm 307: {
1.48 nicm 308: struct grid_line *gl;
309: struct grid_cell_entry *gce;
310: struct grid_cell *gcp;
1.51 nicm 311: int extended;
1.48 nicm 312:
1.1 nicm 313: if (grid_check_y(gd, py) != 0)
314: return;
315:
316: grid_expand_line(gd, py, px + 1);
1.48 nicm 317:
318: gl = &gd->linedata[py];
319: gce = &gl->celldata[px];
320:
1.51 nicm 321: extended = (gce->flags & GRID_FLAG_EXTENDED);
322: if (!extended && (gc->data.size != 1 || gc->data.width != 1))
323: extended = 1;
1.53 nicm 324: if (!extended && ((gc->fg & COLOUR_FLAG_RGB) ||
325: (gc->bg & COLOUR_FLAG_RGB)))
1.51 nicm 326: extended = 1;
327: if (extended) {
1.54 nicm 328: gl->flags |= GRID_LINE_EXTENDED;
329:
1.48 nicm 330: if (~gce->flags & GRID_FLAG_EXTENDED) {
331: gl->extddata = xreallocarray(gl->extddata,
332: gl->extdsize + 1, sizeof *gl->extddata);
333: gce->offset = gl->extdsize++;
334: gce->flags = gc->flags | GRID_FLAG_EXTENDED;
335: }
336:
337: if (gce->offset >= gl->extdsize)
338: fatalx("offset too big");
339: gcp = &gl->extddata[gce->offset];
340: memcpy(gcp, gc, sizeof *gcp);
341: return;
342: }
343:
1.53 nicm 344: gce->flags = gc->flags;
1.48 nicm 345: gce->data.attr = gc->attr;
1.53 nicm 346: gce->data.fg = gc->fg & 0xff;
347: if (gc->fg & COLOUR_FLAG_256)
348: gce->flags |= GRID_FLAG_FG256;
349: gce->data.bg = gc->bg & 0xff;
350: if (gc->bg & COLOUR_FLAG_256)
351: gce->flags |= GRID_FLAG_BG256;
1.48 nicm 352: gce->data.data = gc->data.data[0];
1.1 nicm 353: }
354:
1.14 nicm 355: /* Clear area. */
1.1 nicm 356: void
357: grid_clear(struct grid *gd, u_int px, u_int py, u_int nx, u_int ny)
358: {
359: u_int xx, yy;
360:
361: if (nx == 0 || ny == 0)
362: return;
363:
364: if (px == 0 && nx == gd->sx) {
365: grid_clear_lines(gd, py, ny);
366: return;
367: }
368:
369: if (grid_check_y(gd, py) != 0)
370: return;
371: if (grid_check_y(gd, py + ny - 1) != 0)
372: return;
373:
374: for (yy = py; yy < py + ny; yy++) {
1.14 nicm 375: if (px >= gd->linedata[yy].cellsize)
376: continue;
377: if (px + nx >= gd->linedata[yy].cellsize) {
378: gd->linedata[yy].cellsize = px;
379: continue;
380: }
1.1 nicm 381: for (xx = px; xx < px + nx; xx++) {
1.10 nicm 382: if (xx >= gd->linedata[yy].cellsize)
1.1 nicm 383: break;
1.48 nicm 384: grid_clear_cell(gd, xx, yy);
1.1 nicm 385: }
386: }
387: }
388:
389: /* Clear lines. This just frees and truncates the lines. */
390: void
391: grid_clear_lines(struct grid *gd, u_int py, u_int ny)
392: {
1.10 nicm 393: struct grid_line *gl;
394: u_int yy;
1.1 nicm 395:
396: if (ny == 0)
397: return;
398:
399: if (grid_check_y(gd, py) != 0)
400: return;
401: if (grid_check_y(gd, py + ny - 1) != 0)
402: return;
403:
404: for (yy = py; yy < py + ny; yy++) {
1.10 nicm 405: gl = &gd->linedata[yy];
1.20 nicm 406: free(gl->celldata);
1.49 nicm 407: free(gl->extddata);
1.10 nicm 408: memset(gl, 0, sizeof *gl);
1.1 nicm 409: }
410: }
411:
412: /* Move a group of lines. */
413: void
414: grid_move_lines(struct grid *gd, u_int dy, u_int py, u_int ny)
415: {
416: u_int yy;
417:
418: if (ny == 0 || py == dy)
419: return;
420:
421: if (grid_check_y(gd, py) != 0)
422: return;
423: if (grid_check_y(gd, py + ny - 1) != 0)
424: return;
425: if (grid_check_y(gd, dy) != 0)
426: return;
427: if (grid_check_y(gd, dy + ny - 1) != 0)
428: return;
429:
430: /* Free any lines which are being replaced. */
431: for (yy = dy; yy < dy + ny; yy++) {
432: if (yy >= py && yy < py + ny)
433: continue;
434: grid_clear_lines(gd, yy, 1);
435: }
436:
1.46 nicm 437: memmove(&gd->linedata[dy], &gd->linedata[py],
438: ny * (sizeof *gd->linedata));
1.1 nicm 439:
440: /* Wipe any lines that have been moved (without freeing them). */
441: for (yy = py; yy < py + ny; yy++) {
442: if (yy >= dy && yy < dy + ny)
443: continue;
1.10 nicm 444: memset(&gd->linedata[yy], 0, sizeof gd->linedata[yy]);
1.1 nicm 445: }
446: }
447:
448: /* Move a group of cells. */
449: void
450: grid_move_cells(struct grid *gd, u_int dx, u_int px, u_int py, u_int nx)
451: {
1.10 nicm 452: struct grid_line *gl;
453: u_int xx;
1.1 nicm 454:
455: if (nx == 0 || px == dx)
456: return;
457:
458: if (grid_check_y(gd, py) != 0)
459: return;
1.10 nicm 460: gl = &gd->linedata[py];
1.1 nicm 461:
462: grid_expand_line(gd, py, px + nx);
463: grid_expand_line(gd, py, dx + nx);
1.46 nicm 464: memmove(&gl->celldata[dx], &gl->celldata[px],
465: nx * sizeof *gl->celldata);
1.1 nicm 466:
467: /* Wipe any cells that have been moved. */
468: for (xx = px; xx < px + nx; xx++) {
469: if (xx >= dx && xx < dx + nx)
470: continue;
1.48 nicm 471: grid_clear_cell(gd, xx, py);
1.1 nicm 472: }
1.3 nicm 473: }
474:
1.24 nicm 475: /* Get ANSI foreground sequence. */
476: size_t
477: grid_string_cells_fg(const struct grid_cell *gc, int *values)
478: {
479: size_t n;
1.53 nicm 480: u_char r, g, b;
1.24 nicm 481:
482: n = 0;
1.53 nicm 483: if (gc->fg & COLOUR_FLAG_256) {
1.24 nicm 484: values[n++] = 38;
485: values[n++] = 5;
1.53 nicm 486: values[n++] = gc->fg & 0xff;
487: } else if (gc->fg & COLOUR_FLAG_RGB) {
1.52 nicm 488: values[n++] = 38;
489: values[n++] = 2;
1.53 nicm 490: colour_split_rgb(gc->fg, &r, &g, &b);
491: values[n++] = r;
492: values[n++] = g;
493: values[n++] = b;
1.24 nicm 494: } else {
495: switch (gc->fg) {
1.45 nicm 496: case 0:
497: case 1:
498: case 2:
499: case 3:
500: case 4:
501: case 5:
502: case 6:
503: case 7:
504: values[n++] = gc->fg + 30;
505: break;
506: case 8:
507: values[n++] = 39;
508: break;
509: case 90:
510: case 91:
511: case 92:
512: case 93:
513: case 94:
514: case 95:
515: case 96:
516: case 97:
517: values[n++] = gc->fg;
518: break;
1.24 nicm 519: }
520: }
521: return (n);
522: }
523:
524: /* Get ANSI background sequence. */
525: size_t
526: grid_string_cells_bg(const struct grid_cell *gc, int *values)
527: {
528: size_t n;
1.53 nicm 529: u_char r, g, b;
1.24 nicm 530:
531: n = 0;
1.53 nicm 532: if (gc->bg & COLOUR_FLAG_256) {
1.24 nicm 533: values[n++] = 48;
534: values[n++] = 5;
1.53 nicm 535: values[n++] = gc->bg & 0xff;
536: } else if (gc->bg & COLOUR_FLAG_RGB) {
1.52 nicm 537: values[n++] = 48;
538: values[n++] = 2;
1.53 nicm 539: colour_split_rgb(gc->bg, &r, &g, &b);
540: values[n++] = r;
541: values[n++] = g;
542: values[n++] = b;
1.24 nicm 543: } else {
544: switch (gc->bg) {
545: case 0:
546: case 1:
547: case 2:
548: case 3:
549: case 4:
550: case 5:
551: case 6:
552: case 7:
553: values[n++] = gc->bg + 40;
554: break;
555: case 8:
556: values[n++] = 49;
557: break;
558: case 100:
559: case 101:
560: case 102:
561: case 103:
562: case 104:
1.53 nicm 563: case 105:
1.24 nicm 564: case 106:
565: case 107:
566: values[n++] = gc->bg - 10;
567: break;
568: }
569: }
570: return (n);
571: }
572:
573: /*
574: * Returns ANSI code to set particular attributes (colour, bold and so on)
575: * given a current state. The output buffer must be able to hold at least 57
576: * bytes.
577: */
578: void
579: grid_string_cells_code(const struct grid_cell *lastgc,
1.26 nicm 580: const struct grid_cell *gc, char *buf, size_t len, int escape_c0)
1.24 nicm 581: {
1.52 nicm 582: int oldc[64], newc[64], s[128];
1.24 nicm 583: size_t noldc, nnewc, n, i;
584: u_int attr = gc->attr;
585: u_int lastattr = lastgc->attr;
586: char tmp[64];
587:
588: struct {
589: u_int mask;
590: u_int code;
591: } attrs[] = {
592: { GRID_ATTR_BRIGHT, 1 },
593: { GRID_ATTR_DIM, 2 },
594: { GRID_ATTR_ITALICS, 3 },
595: { GRID_ATTR_UNDERSCORE, 4 },
596: { GRID_ATTR_BLINK, 5 },
597: { GRID_ATTR_REVERSE, 7 },
598: { GRID_ATTR_HIDDEN, 8 }
599: };
600: n = 0;
601:
602: /* If any attribute is removed, begin with 0. */
603: for (i = 0; i < nitems(attrs); i++) {
604: if (!(attr & attrs[i].mask) && (lastattr & attrs[i].mask)) {
605: s[n++] = 0;
1.25 nicm 606: lastattr &= GRID_ATTR_CHARSET;
1.24 nicm 607: break;
608: }
609: }
610: /* For each attribute that is newly set, add its code. */
611: for (i = 0; i < nitems(attrs); i++) {
612: if ((attr & attrs[i].mask) && !(lastattr & attrs[i].mask))
613: s[n++] = attrs[i].code;
614: }
615:
1.39 nicm 616: /* If the foreground colour changed, append its parameters. */
1.24 nicm 617: nnewc = grid_string_cells_fg(gc, newc);
618: noldc = grid_string_cells_fg(lastgc, oldc);
1.39 nicm 619: if (nnewc != noldc || memcmp(newc, oldc, nnewc * sizeof newc[0]) != 0) {
1.24 nicm 620: for (i = 0; i < nnewc; i++)
621: s[n++] = newc[i];
622: }
623:
1.39 nicm 624: /* If the background colour changed, append its parameters. */
1.24 nicm 625: nnewc = grid_string_cells_bg(gc, newc);
626: noldc = grid_string_cells_bg(lastgc, oldc);
1.39 nicm 627: if (nnewc != noldc || memcmp(newc, oldc, nnewc * sizeof newc[0]) != 0) {
1.24 nicm 628: for (i = 0; i < nnewc; i++)
629: s[n++] = newc[i];
630: }
631:
632: /* If there are any parameters, append an SGR code. */
633: *buf = '\0';
634: if (n > 0) {
1.26 nicm 635: if (escape_c0)
636: strlcat(buf, "\\033[", len);
637: else
638: strlcat(buf, "\033[", len);
1.24 nicm 639: for (i = 0; i < n; i++) {
640: if (i + 1 < n)
641: xsnprintf(tmp, sizeof tmp, "%d;", s[i]);
642: else
643: xsnprintf(tmp, sizeof tmp, "%d", s[i]);
644: strlcat(buf, tmp, len);
645: }
646: strlcat(buf, "m", len);
647: }
648:
649: /* Append shift in/shift out if needed. */
1.26 nicm 650: if ((attr & GRID_ATTR_CHARSET) && !(lastattr & GRID_ATTR_CHARSET)) {
651: if (escape_c0)
652: strlcat(buf, "\\016", len); /* SO */
653: else
654: strlcat(buf, "\016", len); /* SO */
655: }
656: if (!(attr & GRID_ATTR_CHARSET) && (lastattr & GRID_ATTR_CHARSET)) {
657: if (escape_c0)
658: strlcat(buf, "\\017", len); /* SI */
659: else
660: strlcat(buf, "\017", len); /* SI */
661: }
1.24 nicm 662: }
663:
1.3 nicm 664: /* Convert cells into a string. */
665: char *
1.24 nicm 666: grid_string_cells(struct grid *gd, u_int px, u_int py, u_int nx,
1.28 nicm 667: struct grid_cell **lastgc, int with_codes, int escape_c0, int trim)
1.3 nicm 668: {
1.48 nicm 669: struct grid_cell gc;
1.24 nicm 670: static struct grid_cell lastgc1;
1.38 nicm 671: const char *data;
1.24 nicm 672: char *buf, code[128];
1.26 nicm 673: size_t len, off, size, codelen;
1.16 nicm 674: u_int xx;
1.30 nicm 675: const struct grid_line *gl;
1.3 nicm 676:
1.29 nicm 677: if (lastgc != NULL && *lastgc == NULL) {
1.24 nicm 678: memcpy(&lastgc1, &grid_default_cell, sizeof lastgc1);
679: *lastgc = &lastgc1;
680: }
681:
1.3 nicm 682: len = 128;
683: buf = xmalloc(len);
684: off = 0;
685:
1.30 nicm 686: gl = grid_peek_line(gd, py);
1.3 nicm 687: for (xx = px; xx < px + nx; xx++) {
1.30 nicm 688: if (gl == NULL || xx >= gl->cellsize)
689: break;
1.48 nicm 690: grid_get_cell(gd, xx, py, &gc);
691: if (gc.flags & GRID_FLAG_PADDING)
1.3 nicm 692: continue;
693:
1.24 nicm 694: if (with_codes) {
1.48 nicm 695: grid_string_cells_code(*lastgc, &gc, code, sizeof code,
1.26 nicm 696: escape_c0);
1.24 nicm 697: codelen = strlen(code);
1.48 nicm 698: memcpy(*lastgc, &gc, sizeof **lastgc);
1.24 nicm 699: } else
700: codelen = 0;
701:
1.48 nicm 702: data = gc.data.data;
703: size = gc.data.size;
1.26 nicm 704: if (escape_c0 && size == 1 && *data == '\\') {
1.27 nicm 705: data = "\\\\";
1.26 nicm 706: size = 2;
707: }
708:
709: while (len < off + size + codelen + 1) {
1.41 nicm 710: buf = xreallocarray(buf, 2, len);
1.21 nicm 711: len *= 2;
712: }
1.3 nicm 713:
1.24 nicm 714: if (codelen != 0) {
715: memcpy(buf + off, code, codelen);
716: off += codelen;
717: }
1.26 nicm 718: memcpy(buf + off, data, size);
719: off += size;
1.3 nicm 720: }
1.17 nicm 721:
1.37 nicm 722: if (trim) {
1.28 nicm 723: while (off > 0 && buf[off - 1] == ' ')
724: off--;
1.32 nicm 725: }
1.3 nicm 726: buf[off] = '\0';
1.26 nicm 727:
1.3 nicm 728: return (buf);
1.7 nicm 729: }
730:
1.17 nicm 731: /*
1.7 nicm 732: * Duplicate a set of lines between two grids. If there aren't enough lines in
733: * either source or destination, the number of lines is limited to the number
734: * available.
735: */
736: void
1.31 nicm 737: grid_duplicate_lines(struct grid *dst, u_int dy, struct grid *src, u_int sy,
738: u_int ny)
1.7 nicm 739: {
1.10 nicm 740: struct grid_line *dstl, *srcl;
741: u_int yy;
1.7 nicm 742:
743: if (dy + ny > dst->hsize + dst->sy)
744: ny = dst->hsize + dst->sy - dy;
745: if (sy + ny > src->hsize + src->sy)
746: ny = src->hsize + src->sy - sy;
747: grid_clear_lines(dst, dy, ny);
748:
749: for (yy = 0; yy < ny; yy++) {
1.11 nicm 750: srcl = &src->linedata[sy];
751: dstl = &dst->linedata[dy];
1.10 nicm 752:
753: memcpy(dstl, srcl, sizeof *dstl);
754: if (srcl->cellsize != 0) {
1.42 deraadt 755: dstl->celldata = xreallocarray(NULL,
1.10 nicm 756: srcl->cellsize, sizeof *dstl->celldata);
757: memcpy(dstl->celldata, srcl->celldata,
758: srcl->cellsize * sizeof *dstl->celldata);
1.44 nicm 759: } else
760: dstl->celldata = NULL;
1.7 nicm 761:
1.48 nicm 762: if (srcl->extdsize != 0) {
763: dstl->extdsize = srcl->extdsize;
764: dstl->extddata = xreallocarray(NULL, dstl->extdsize,
765: sizeof *dstl->extddata);
766: memcpy(dstl->extddata, srcl->extddata, dstl->extdsize *
767: sizeof *dstl->extddata);
768: }
769:
1.10 nicm 770: sy++;
771: dy++;
1.7 nicm 772: }
1.22 nicm 773: }
774:
1.48 nicm 775: /* Copy a section of a line. */
776: void
777: grid_reflow_copy(struct grid_line *dst_gl, u_int to, struct grid_line *src_gl,
778: u_int from, u_int to_copy)
779: {
780: struct grid_cell_entry *gce;
781: u_int i, was;
782:
783: memcpy(&dst_gl->celldata[to], &src_gl->celldata[from],
784: to_copy * sizeof *dst_gl->celldata);
785:
786: for (i = to; i < to + to_copy; i++) {
787: gce = &dst_gl->celldata[i];
788: if (~gce->flags & GRID_FLAG_EXTENDED)
789: continue;
790: was = gce->offset;
791:
792: dst_gl->extddata = xreallocarray(dst_gl->extddata,
793: dst_gl->extdsize + 1, sizeof *dst_gl->extddata);
794: gce->offset = dst_gl->extdsize++;
795: memcpy(&dst_gl->extddata[gce->offset], &src_gl->extddata[was],
796: sizeof *dst_gl->extddata);
797: }
798: }
799:
1.23 nicm 800: /* Join line data. */
801: void
802: grid_reflow_join(struct grid *dst, u_int *py, struct grid_line *src_gl,
803: u_int new_x)
804: {
805: struct grid_line *dst_gl = &dst->linedata[(*py) - 1];
806: u_int left, to_copy, ox, nx;
807:
808: /* How much is left on the old line? */
809: left = new_x - dst_gl->cellsize;
810:
811: /* Work out how much to append. */
812: to_copy = src_gl->cellsize;
813: if (to_copy > left)
814: to_copy = left;
815: ox = dst_gl->cellsize;
816: nx = ox + to_copy;
817:
818: /* Resize the destination line. */
1.41 nicm 819: dst_gl->celldata = xreallocarray(dst_gl->celldata, nx,
1.23 nicm 820: sizeof *dst_gl->celldata);
821: dst_gl->cellsize = nx;
822:
823: /* Append as much as possible. */
1.48 nicm 824: grid_reflow_copy(dst_gl, ox, src_gl, 0, to_copy);
1.23 nicm 825:
826: /* If there is any left in the source, split it. */
827: if (src_gl->cellsize > to_copy) {
828: dst_gl->flags |= GRID_LINE_WRAPPED;
829:
830: src_gl->cellsize -= to_copy;
831: grid_reflow_split(dst, py, src_gl, new_x, to_copy);
832: }
833: }
834:
835: /* Split line data. */
836: void
837: grid_reflow_split(struct grid *dst, u_int *py, struct grid_line *src_gl,
838: u_int new_x, u_int offset)
839: {
840: struct grid_line *dst_gl = NULL;
841: u_int to_copy;
842:
843: /* Loop and copy sections of the source line. */
844: while (src_gl->cellsize > 0) {
845: /* Create new line. */
846: if (*py >= dst->hsize + dst->sy)
847: grid_scroll_history(dst);
848: dst_gl = &dst->linedata[*py];
849: (*py)++;
850:
851: /* How much should we copy? */
852: to_copy = new_x;
853: if (to_copy > src_gl->cellsize)
854: to_copy = src_gl->cellsize;
855:
856: /* Expand destination line. */
1.41 nicm 857: dst_gl->celldata = xreallocarray(NULL, to_copy,
1.40 nicm 858: sizeof *dst_gl->celldata);
1.23 nicm 859: dst_gl->cellsize = to_copy;
860: dst_gl->flags |= GRID_LINE_WRAPPED;
861:
862: /* Copy the data. */
1.48 nicm 863: grid_reflow_copy(dst_gl, 0, src_gl, offset, to_copy);
1.23 nicm 864:
865: /* Move offset and reduce old line size. */
866: offset += to_copy;
867: src_gl->cellsize -= to_copy;
868: }
869:
870: /* Last line is not wrapped. */
871: if (dst_gl != NULL)
872: dst_gl->flags &= ~GRID_LINE_WRAPPED;
873: }
874:
875: /* Move line data. */
876: void
877: grid_reflow_move(struct grid *dst, u_int *py, struct grid_line *src_gl)
878: {
879: struct grid_line *dst_gl;
880:
881: /* Create new line. */
882: if (*py >= dst->hsize + dst->sy)
883: grid_scroll_history(dst);
884: dst_gl = &dst->linedata[*py];
885: (*py)++;
886:
887: /* Copy the old line. */
888: memcpy(dst_gl, src_gl, sizeof *dst_gl);
889: dst_gl->flags &= ~GRID_LINE_WRAPPED;
890:
891: /* Clear old line. */
892: src_gl->celldata = NULL;
1.48 nicm 893: src_gl->extddata = NULL;
1.23 nicm 894: }
895:
1.22 nicm 896: /*
1.23 nicm 897: * Reflow lines from src grid into dst grid of width new_x. Returns number of
898: * lines fewer in the visible area. The source grid is destroyed.
1.22 nicm 899: */
900: u_int
1.23 nicm 901: grid_reflow(struct grid *dst, struct grid *src, u_int new_x)
1.22 nicm 902: {
1.23 nicm 903: u_int py, sy, line;
1.22 nicm 904: int previous_wrapped;
1.23 nicm 905: struct grid_line *src_gl;
906:
907: py = 0;
908: sy = src->sy;
1.22 nicm 909:
1.23 nicm 910: previous_wrapped = 0;
911: for (line = 0; line < sy + src->hsize; line++) {
912: src_gl = src->linedata + line;
1.22 nicm 913: if (!previous_wrapped) {
1.23 nicm 914: /* Wasn't wrapped. If smaller, move to destination. */
915: if (src_gl->cellsize <= new_x)
916: grid_reflow_move(dst, &py, src_gl);
917: else
918: grid_reflow_split(dst, &py, src_gl, new_x, 0);
919: } else {
920: /* Previous was wrapped. Try to join. */
921: grid_reflow_join(dst, &py, src_gl, new_x);
1.22 nicm 922: }
1.48 nicm 923: previous_wrapped = (src_gl->flags & GRID_LINE_WRAPPED);
1.55 ! nicm 924:
! 925: /* This is where we started scrolling. */
! 926: if (line == sy + src->hsize - src->hscrolled - 1)
! 927: dst->hscrolled = 0;
1.22 nicm 928: }
929:
1.23 nicm 930: grid_destroy(src);
931:
932: if (py > sy)
1.22 nicm 933: return (0);
1.23 nicm 934: return (sy - py);
1.1 nicm 935: }