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