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