Annotation of src/usr.bin/tmux/grid.c, Revision 1.23
1.23 ! nicm 1: /* $OpenBSD: grid.c,v 1.22 2013/02/05 11:08:59 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 *);
! 77:
1.1 nicm 78: /* Create a new grid. */
79: struct grid *
80: grid_create(u_int sx, u_int sy, u_int hlimit)
81: {
82: struct grid *gd;
83:
84: gd = xmalloc(sizeof *gd);
85: gd->sx = sx;
86: gd->sy = sy;
87:
1.7 nicm 88: gd->flags = GRID_HISTORY;
89:
1.1 nicm 90: gd->hsize = 0;
91: gd->hlimit = hlimit;
92:
1.10 nicm 93: gd->linedata = xcalloc(gd->sy, sizeof *gd->linedata);
1.1 nicm 94:
95: return (gd);
96: }
97:
98: /* Destroy grid. */
99: void
100: grid_destroy(struct grid *gd)
101: {
1.10 nicm 102: struct grid_line *gl;
103: u_int yy;
1.1 nicm 104:
105: for (yy = 0; yy < gd->hsize + gd->sy; yy++) {
1.10 nicm 106: gl = &gd->linedata[yy];
1.20 nicm 107: free(gl->celldata);
1.1 nicm 108: }
109:
1.20 nicm 110: free(gd->linedata);
1.1 nicm 111:
1.20 nicm 112: free(gd);
1.1 nicm 113: }
114:
115: /* Compare grids. */
116: int
117: grid_compare(struct grid *ga, struct grid *gb)
118: {
1.10 nicm 119: struct grid_line *gla, *glb;
1.1 nicm 120: struct grid_cell *gca, *gcb;
121: u_int xx, yy;
122:
123: if (ga->sx != gb->sx || ga->sy != ga->sy)
124: return (1);
125:
126: for (yy = 0; yy < ga->sy; yy++) {
1.10 nicm 127: gla = &ga->linedata[yy];
128: glb = &gb->linedata[yy];
129: if (gla->cellsize != glb->cellsize)
1.1 nicm 130: return (1);
131: for (xx = 0; xx < ga->sx; xx++) {
1.10 nicm 132: gca = &gla->celldata[xx];
133: gcb = &glb->celldata[xx];
1.1 nicm 134: if (memcmp(gca, gcb, sizeof (struct grid_cell)) != 0)
135: return (1);
136: }
137: }
138:
139: return (0);
140: }
141:
1.15 nicm 142: /*
143: * Collect lines from the history if at the limit. Free the top (oldest) 10%
144: * and shift up.
145: */
1.1 nicm 146: void
1.15 nicm 147: grid_collect_history(struct grid *gd)
1.1 nicm 148: {
149: u_int yy;
150:
1.17 nicm 151: GRID_DEBUG(gd, "");
1.1 nicm 152:
1.15 nicm 153: if (gd->hsize < gd->hlimit)
154: return;
155:
156: yy = gd->hlimit / 10;
157: if (yy < 1)
158: yy = 1;
159:
160: grid_move_lines(gd, 0, yy, gd->hsize + gd->sy - yy);
161: gd->hsize -= yy;
162: }
163:
1.17 nicm 164: /*
1.15 nicm 165: * Scroll the entire visible screen, moving one line into the history. Just
166: * allocate a new line at the bottom and move the history size indicator.
167: */
168: void
169: grid_scroll_history(struct grid *gd)
170: {
171: u_int yy;
1.1 nicm 172:
1.17 nicm 173: GRID_DEBUG(gd, "");
1.1 nicm 174:
175: yy = gd->hsize + gd->sy;
1.15 nicm 176: gd->linedata = xrealloc(gd->linedata, yy + 1, sizeof *gd->linedata);
177: memset(&gd->linedata[yy], 0, sizeof gd->linedata[yy]);
1.17 nicm 178:
1.15 nicm 179: gd->hsize++;
180: }
1.1 nicm 181:
1.15 nicm 182: /* Scroll a region up, moving the top line into the history. */
183: void
184: grid_scroll_history_region(struct grid *gd, u_int upper, u_int lower)
185: {
186: struct grid_line *gl_history, *gl_upper, *gl_lower;
187: u_int yy;
188:
1.17 nicm 189: GRID_DEBUG(gd, "upper=%u, lower=%u", upper, lower);
1.15 nicm 190:
191: /* Create a space for a new line. */
192: yy = gd->hsize + gd->sy;
1.10 nicm 193: gd->linedata = xrealloc(gd->linedata, yy + 1, sizeof *gd->linedata);
1.1 nicm 194:
1.15 nicm 195: /* Move the entire screen down to free a space for this line. */
196: gl_history = &gd->linedata[gd->hsize];
197: memmove(gl_history + 1, gl_history, gd->sy * sizeof *gl_history);
198:
199: /* Adjust the region and find its start and end. */
200: upper++;
201: gl_upper = &gd->linedata[upper];
202: lower++;
203: gl_lower = &gd->linedata[lower];
204:
205: /* Move the line into the history. */
206: memcpy(gl_history, gl_upper, sizeof *gl_history);
207:
208: /* Then move the region up and clear the bottom line. */
209: memmove(gl_upper, gl_upper + 1, (lower - upper) * sizeof *gl_upper);
210: memset(gl_lower, 0, sizeof *gl_lower);
211:
212: /* Move the history offset down over the line. */
1.1 nicm 213: gd->hsize++;
214: }
215:
216: /* Expand line to fit to cell. */
217: void
1.14 nicm 218: grid_expand_line(struct grid *gd, u_int py, u_int sx)
1.1 nicm 219: {
1.10 nicm 220: struct grid_line *gl;
1.14 nicm 221: u_int xx;
1.1 nicm 222:
1.10 nicm 223: gl = &gd->linedata[py];
1.14 nicm 224: if (sx <= gl->cellsize)
1.1 nicm 225: return;
226:
1.10 nicm 227: gl->celldata = xrealloc(gl->celldata, sx, sizeof *gl->celldata);
228: for (xx = gl->cellsize; xx < sx; xx++)
1.1 nicm 229: grid_put_cell(gd, xx, py, &grid_default_cell);
1.10 nicm 230: gl->cellsize = sx;
1.1 nicm 231: }
232:
233: /* Get cell for reading. */
234: const struct grid_cell *
235: grid_peek_cell(struct grid *gd, u_int px, u_int py)
236: {
237: if (grid_check_y(gd, py) != 0)
238: return (&grid_default_cell);
239:
1.10 nicm 240: if (px >= gd->linedata[py].cellsize)
1.1 nicm 241: return (&grid_default_cell);
1.10 nicm 242: return (&gd->linedata[py].celldata[px]);
1.1 nicm 243: }
244:
245: /* Get cell at relative position (for writing). */
246: struct grid_cell *
247: grid_get_cell(struct grid *gd, u_int px, u_int py)
248: {
249: if (grid_check_y(gd, py) != 0)
250: return (NULL);
251:
252: grid_expand_line(gd, py, px + 1);
1.10 nicm 253: return (&gd->linedata[py].celldata[px]);
1.1 nicm 254: }
255:
256: /* Set cell at relative position. */
257: void
258: grid_set_cell(
259: struct grid *gd, u_int px, u_int py, const struct grid_cell *gc)
260: {
261: if (grid_check_y(gd, py) != 0)
262: return;
263:
264: grid_expand_line(gd, py, px + 1);
265: grid_put_cell(gd, px, py, gc);
266: }
267:
1.14 nicm 268: /* Clear area. */
1.1 nicm 269: void
270: grid_clear(struct grid *gd, u_int px, u_int py, u_int nx, u_int ny)
271: {
272: u_int xx, yy;
273:
1.17 nicm 274: GRID_DEBUG(gd, "px=%u, py=%u, nx=%u, ny=%u", px, py, nx, ny);
1.1 nicm 275:
276: if (nx == 0 || ny == 0)
277: return;
278:
279: if (px == 0 && nx == gd->sx) {
280: grid_clear_lines(gd, py, ny);
281: return;
282: }
283:
284: if (grid_check_y(gd, py) != 0)
285: return;
286: if (grid_check_y(gd, py + ny - 1) != 0)
287: return;
288:
289: for (yy = py; yy < py + ny; yy++) {
1.14 nicm 290: if (px >= gd->linedata[yy].cellsize)
291: continue;
292: if (px + nx >= gd->linedata[yy].cellsize) {
293: gd->linedata[yy].cellsize = px;
294: continue;
295: }
1.1 nicm 296: for (xx = px; xx < px + nx; xx++) {
1.10 nicm 297: if (xx >= gd->linedata[yy].cellsize)
1.1 nicm 298: break;
299: grid_put_cell(gd, xx, yy, &grid_default_cell);
300: }
301: }
302: }
303:
304: /* Clear lines. This just frees and truncates the lines. */
305: void
306: grid_clear_lines(struct grid *gd, u_int py, u_int ny)
307: {
1.10 nicm 308: struct grid_line *gl;
309: u_int yy;
1.1 nicm 310:
1.17 nicm 311: GRID_DEBUG(gd, "py=%u, ny=%u", py, ny);
1.1 nicm 312:
313: if (ny == 0)
314: return;
315:
316: if (grid_check_y(gd, py) != 0)
317: return;
318: if (grid_check_y(gd, py + ny - 1) != 0)
319: return;
320:
321: for (yy = py; yy < py + ny; yy++) {
1.10 nicm 322: gl = &gd->linedata[yy];
1.20 nicm 323: free(gl->celldata);
1.10 nicm 324: memset(gl, 0, sizeof *gl);
1.1 nicm 325: }
326: }
327:
328: /* Move a group of lines. */
329: void
330: grid_move_lines(struct grid *gd, u_int dy, u_int py, u_int ny)
331: {
332: u_int yy;
333:
1.17 nicm 334: GRID_DEBUG(gd, "dy=%u, py=%u, ny=%u", dy, py, ny);
1.1 nicm 335:
336: if (ny == 0 || py == dy)
337: return;
338:
339: if (grid_check_y(gd, py) != 0)
340: return;
341: if (grid_check_y(gd, py + ny - 1) != 0)
342: return;
343: if (grid_check_y(gd, dy) != 0)
344: return;
345: if (grid_check_y(gd, dy + ny - 1) != 0)
346: return;
347:
348: /* Free any lines which are being replaced. */
349: for (yy = dy; yy < dy + ny; yy++) {
350: if (yy >= py && yy < py + ny)
351: continue;
352: grid_clear_lines(gd, yy, 1);
353: }
354:
1.10 nicm 355: memmove(
356: &gd->linedata[dy], &gd->linedata[py], ny * (sizeof *gd->linedata));
1.1 nicm 357:
358: /* Wipe any lines that have been moved (without freeing them). */
359: for (yy = py; yy < py + ny; yy++) {
360: if (yy >= dy && yy < dy + ny)
361: continue;
1.10 nicm 362: memset(&gd->linedata[yy], 0, sizeof gd->linedata[yy]);
1.1 nicm 363: }
364: }
365:
366: /* Move a group of cells. */
367: void
368: grid_move_cells(struct grid *gd, u_int dx, u_int px, u_int py, u_int nx)
369: {
1.10 nicm 370: struct grid_line *gl;
371: u_int xx;
1.1 nicm 372:
1.17 nicm 373: GRID_DEBUG(gd, "dx=%u, px=%u, py=%u, nx=%u", dx, px, py, nx);
1.1 nicm 374:
375: if (nx == 0 || px == dx)
376: return;
377:
378: if (grid_check_y(gd, py) != 0)
379: return;
1.10 nicm 380: gl = &gd->linedata[py];
1.1 nicm 381:
382: grid_expand_line(gd, py, px + nx);
383: grid_expand_line(gd, py, dx + nx);
1.10 nicm 384: memmove(
385: &gl->celldata[dx], &gl->celldata[px], nx * sizeof *gl->celldata);
1.1 nicm 386:
387: /* Wipe any cells that have been moved. */
388: for (xx = px; xx < px + nx; xx++) {
389: if (xx >= dx && xx < dx + nx)
390: continue;
391: grid_put_cell(gd, xx, py, &grid_default_cell);
392: }
1.3 nicm 393: }
394:
395: /* Convert cells into a string. */
396: char *
397: grid_string_cells(struct grid *gd, u_int px, u_int py, u_int nx)
398: {
1.17 nicm 399: const struct grid_cell *gc;
1.21 nicm 400: struct utf8_data ud;
1.3 nicm 401: char *buf;
1.21 nicm 402: size_t len, off;
1.16 nicm 403: u_int xx;
1.3 nicm 404:
405: GRID_DEBUG(gd, "px=%u, py=%u, nx=%u", px, py, nx);
406:
407: len = 128;
408: buf = xmalloc(len);
409: off = 0;
410:
411: for (xx = px; xx < px + nx; xx++) {
412: gc = grid_peek_cell(gd, xx, py);
413: if (gc->flags & GRID_FLAG_PADDING)
414: continue;
1.21 nicm 415: grid_cell_get(gc, &ud);
1.3 nicm 416:
1.21 nicm 417: while (len < off + ud.size + 1) {
418: buf = xrealloc(buf, 2, len);
419: len *= 2;
420: }
1.3 nicm 421:
1.21 nicm 422: memcpy(buf + off, ud.data, ud.size);
423: off += ud.size;
1.3 nicm 424: }
1.17 nicm 425:
1.4 nicm 426: while (off > 0 && buf[off - 1] == ' ')
427: off--;
1.3 nicm 428: buf[off] = '\0';
429: return (buf);
1.7 nicm 430: }
431:
1.17 nicm 432: /*
1.7 nicm 433: * Duplicate a set of lines between two grids. If there aren't enough lines in
434: * either source or destination, the number of lines is limited to the number
435: * available.
436: */
437: void
438: grid_duplicate_lines(
439: struct grid *dst, u_int dy, struct grid *src, u_int sy, u_int ny)
440: {
1.10 nicm 441: struct grid_line *dstl, *srcl;
442: u_int yy;
1.7 nicm 443:
444: GRID_DEBUG(src, "dy=%u, sy=%u, ny=%u", dy, sy, ny);
445:
446: if (dy + ny > dst->hsize + dst->sy)
447: ny = dst->hsize + dst->sy - dy;
448: if (sy + ny > src->hsize + src->sy)
449: ny = src->hsize + src->sy - sy;
450: grid_clear_lines(dst, dy, ny);
451:
452: for (yy = 0; yy < ny; yy++) {
1.11 nicm 453: srcl = &src->linedata[sy];
454: dstl = &dst->linedata[dy];
1.10 nicm 455:
456: memcpy(dstl, srcl, sizeof *dstl);
457: if (srcl->cellsize != 0) {
458: dstl->celldata = xcalloc(
459: srcl->cellsize, sizeof *dstl->celldata);
460: memcpy(dstl->celldata, srcl->celldata,
461: srcl->cellsize * sizeof *dstl->celldata);
1.7 nicm 462: }
463:
1.10 nicm 464: sy++;
465: dy++;
1.7 nicm 466: }
1.22 nicm 467: }
468:
1.23 ! nicm 469: /* Join line data. */
! 470: void
! 471: grid_reflow_join(struct grid *dst, u_int *py, struct grid_line *src_gl,
! 472: u_int new_x)
! 473: {
! 474: struct grid_line *dst_gl = &dst->linedata[(*py) - 1];
! 475: u_int left, to_copy, ox, nx;
! 476:
! 477: /* How much is left on the old line? */
! 478: left = new_x - dst_gl->cellsize;
! 479:
! 480: /* Work out how much to append. */
! 481: to_copy = src_gl->cellsize;
! 482: if (to_copy > left)
! 483: to_copy = left;
! 484: ox = dst_gl->cellsize;
! 485: nx = ox + to_copy;
! 486:
! 487: /* Resize the destination line. */
! 488: dst_gl->celldata = xrealloc(dst_gl->celldata, nx,
! 489: sizeof *dst_gl->celldata);
! 490: dst_gl->cellsize = nx;
! 491:
! 492: /* Append as much as possible. */
! 493: memcpy(&dst_gl->celldata[ox], &src_gl->celldata[0],
! 494: to_copy * sizeof src_gl->celldata[0]);
! 495:
! 496: /* If there is any left in the source, split it. */
! 497: if (src_gl->cellsize > to_copy) {
! 498: dst_gl->flags |= GRID_LINE_WRAPPED;
! 499:
! 500: src_gl->cellsize -= to_copy;
! 501: grid_reflow_split(dst, py, src_gl, new_x, to_copy);
! 502: }
! 503: }
! 504:
! 505: /* Split line data. */
! 506: void
! 507: grid_reflow_split(struct grid *dst, u_int *py, struct grid_line *src_gl,
! 508: u_int new_x, u_int offset)
! 509: {
! 510: struct grid_line *dst_gl = NULL;
! 511: u_int to_copy;
! 512:
! 513: /* Loop and copy sections of the source line. */
! 514: while (src_gl->cellsize > 0) {
! 515: /* Create new line. */
! 516: if (*py >= dst->hsize + dst->sy)
! 517: grid_scroll_history(dst);
! 518: dst_gl = &dst->linedata[*py];
! 519: (*py)++;
! 520:
! 521: /* How much should we copy? */
! 522: to_copy = new_x;
! 523: if (to_copy > src_gl->cellsize)
! 524: to_copy = src_gl->cellsize;
! 525:
! 526: /* Expand destination line. */
! 527: dst_gl->celldata = xmalloc(to_copy * sizeof *dst_gl->celldata);
! 528: dst_gl->cellsize = to_copy;
! 529: dst_gl->flags |= GRID_LINE_WRAPPED;
! 530:
! 531: /* Copy the data. */
! 532: memcpy (&dst_gl->celldata[0], &src_gl->celldata[offset],
! 533: to_copy * sizeof dst_gl->celldata[0]);
! 534:
! 535: /* Move offset and reduce old line size. */
! 536: offset += to_copy;
! 537: src_gl->cellsize -= to_copy;
! 538: }
! 539:
! 540: /* Last line is not wrapped. */
! 541: if (dst_gl != NULL)
! 542: dst_gl->flags &= ~GRID_LINE_WRAPPED;
! 543: }
! 544:
! 545: /* Move line data. */
! 546: void
! 547: grid_reflow_move(struct grid *dst, u_int *py, struct grid_line *src_gl)
! 548: {
! 549: struct grid_line *dst_gl;
! 550:
! 551: /* Create new line. */
! 552: if (*py >= dst->hsize + dst->sy)
! 553: grid_scroll_history(dst);
! 554: dst_gl = &dst->linedata[*py];
! 555: (*py)++;
! 556:
! 557: /* Copy the old line. */
! 558: memcpy(dst_gl, src_gl, sizeof *dst_gl);
! 559: dst_gl->flags &= ~GRID_LINE_WRAPPED;
! 560:
! 561: /* Clear old line. */
! 562: src_gl->celldata = NULL;
! 563: }
! 564:
1.22 nicm 565: /*
1.23 ! nicm 566: * Reflow lines from src grid into dst grid of width new_x. Returns number of
! 567: * lines fewer in the visible area. The source grid is destroyed.
1.22 nicm 568: */
569: u_int
1.23 ! nicm 570: grid_reflow(struct grid *dst, struct grid *src, u_int new_x)
1.22 nicm 571: {
1.23 ! nicm 572: u_int py, sy, line;
1.22 nicm 573: int previous_wrapped;
1.23 ! nicm 574: struct grid_line *src_gl;
! 575:
! 576: py = 0;
! 577: sy = src->sy;
1.22 nicm 578:
1.23 ! nicm 579: previous_wrapped = 0;
! 580: for (line = 0; line < sy + src->hsize; line++) {
! 581: src_gl = src->linedata + line;
1.22 nicm 582: if (!previous_wrapped) {
1.23 ! nicm 583: /* Wasn't wrapped. If smaller, move to destination. */
! 584: if (src_gl->cellsize <= new_x)
! 585: grid_reflow_move(dst, &py, src_gl);
! 586: else
! 587: grid_reflow_split(dst, &py, src_gl, new_x, 0);
! 588: } else {
! 589: /* Previous was wrapped. Try to join. */
! 590: grid_reflow_join(dst, &py, src_gl, new_x);
1.22 nicm 591: }
1.23 ! nicm 592: previous_wrapped = src_gl->flags & GRID_LINE_WRAPPED;
1.22 nicm 593: }
594:
1.23 ! nicm 595: grid_destroy(src);
! 596:
! 597: if (py > sy)
1.22 nicm 598: return (0);
1.23 ! nicm 599: return (sy - py);
1.1 nicm 600: }