Annotation of src/usr.bin/tmux/grid.c, Revision 1.19
1.19 ! nicm 1: /* $OpenBSD: grid.c,v 1.18 2010/04/06 21:35:44 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:
21: #include <string.h>
22:
23: #include "tmux.h"
24:
25: /*
26: * Grid data. This is the basic data structure that represents what is shown on
27: * screen.
28: *
29: * A grid is a grid of cells (struct grid_cell). Lines are not allocated until
30: * cells in that line are written to. The grid is split into history and
31: * viewable data with the history starting at row (line) 0 and extending to
32: * (hsize - 1); from hsize to hsize + (sy - 1) is the viewable data. All
33: * functions in this file work on absolute coordinates, grid-view.c has
34: * functions which work on the screen data.
35: */
36:
37: /* Default grid cell data. */
38: const struct grid_cell grid_default_cell = { 0, 0, 8, 8, ' ' };
1.19 ! nicm 39: const struct grid_cell grid_marker_cell = { 0, 0, 8, 8, '_' };
1.1 nicm 40:
41: #define grid_put_cell(gd, px, py, gc) do { \
1.10 nicm 42: memcpy(&gd->linedata[py].celldata[px], \
43: gc, sizeof gd->linedata[py].celldata[px]); \
1.1 nicm 44: } while (0)
45: #define grid_put_utf8(gd, px, py, gc) do { \
1.10 nicm 46: memcpy(&gd->linedata[py].utf8data[px], \
47: gc, sizeof gd->linedata[py].utf8data[px]); \
1.1 nicm 48: } while (0)
49:
50: int grid_check_y(struct grid *, u_int);
51:
52: #ifdef DEBUG
53: int
54: grid_check_y(struct grid *gd, u_int py)
55: {
56: if ((py) >= (gd)->hsize + (gd)->sy)
57: log_fatalx("y out of range: %u", py);
58: return (0);
59: }
60: #else
61: int
62: grid_check_y(struct grid *gd, u_int py)
63: {
64: if ((py) >= (gd)->hsize + (gd)->sy) {
65: log_debug("y out of range: %u", py);
66: return (-1);
67: }
68: return (0);
69: }
70: #endif
71:
72: /* Create a new grid. */
73: struct grid *
74: grid_create(u_int sx, u_int sy, u_int hlimit)
75: {
76: struct grid *gd;
77:
78: gd = xmalloc(sizeof *gd);
79: gd->sx = sx;
80: gd->sy = sy;
81:
1.7 nicm 82: gd->flags = GRID_HISTORY;
83:
1.1 nicm 84: gd->hsize = 0;
85: gd->hlimit = hlimit;
86:
1.10 nicm 87: gd->linedata = xcalloc(gd->sy, sizeof *gd->linedata);
1.1 nicm 88:
89: return (gd);
90: }
91:
92: /* Destroy grid. */
93: void
94: grid_destroy(struct grid *gd)
95: {
1.10 nicm 96: struct grid_line *gl;
97: u_int yy;
1.1 nicm 98:
99: for (yy = 0; yy < gd->hsize + gd->sy; yy++) {
1.10 nicm 100: gl = &gd->linedata[yy];
101: if (gl->celldata != NULL)
102: xfree(gl->celldata);
103: if (gl->utf8data != NULL)
104: xfree(gl->utf8data);
1.1 nicm 105: }
106:
1.10 nicm 107: xfree(gd->linedata);
1.1 nicm 108:
109: xfree(gd);
110: }
111:
112: /* Compare grids. */
113: int
114: grid_compare(struct grid *ga, struct grid *gb)
115: {
1.10 nicm 116: struct grid_line *gla, *glb;
1.1 nicm 117: struct grid_cell *gca, *gcb;
118: struct grid_utf8 *gua, *gub;
119: u_int xx, yy;
120:
121: if (ga->sx != gb->sx || ga->sy != ga->sy)
122: return (1);
123:
124: for (yy = 0; yy < ga->sy; yy++) {
1.10 nicm 125: gla = &ga->linedata[yy];
126: glb = &gb->linedata[yy];
127: if (gla->cellsize != glb->cellsize)
1.1 nicm 128: return (1);
129: for (xx = 0; xx < ga->sx; xx++) {
1.10 nicm 130: gca = &gla->celldata[xx];
131: gcb = &glb->celldata[xx];
1.1 nicm 132: if (memcmp(gca, gcb, sizeof (struct grid_cell)) != 0)
133: return (1);
134: if (!(gca->flags & GRID_FLAG_UTF8))
135: continue;
1.10 nicm 136: gua = &gla->utf8data[xx];
137: gub = &glb->utf8data[xx];
1.1 nicm 138: if (memcmp(gua, gub, sizeof (struct grid_utf8)) != 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: /* Expand line to fit to cell for UTF-8. */
238: void
239: grid_expand_line_utf8(struct grid *gd, u_int py, u_int sx)
240: {
1.10 nicm 241: struct grid_line *gl;
242:
243: gl = &gd->linedata[py];
244: if (sx <= gl->utf8size)
1.1 nicm 245: return;
246:
1.10 nicm 247: gl->utf8data = xrealloc(gl->utf8data, sx, sizeof *gl->utf8data);
248: gl->utf8size = sx;
1.1 nicm 249: }
250:
251: /* Get cell for reading. */
252: const struct grid_cell *
253: grid_peek_cell(struct grid *gd, u_int px, u_int py)
254: {
255: if (grid_check_y(gd, py) != 0)
256: return (&grid_default_cell);
257:
1.10 nicm 258: if (px >= gd->linedata[py].cellsize)
1.1 nicm 259: return (&grid_default_cell);
1.10 nicm 260: return (&gd->linedata[py].celldata[px]);
1.1 nicm 261: }
262:
263: /* Get cell at relative position (for writing). */
264: struct grid_cell *
265: grid_get_cell(struct grid *gd, u_int px, u_int py)
266: {
267: if (grid_check_y(gd, py) != 0)
268: return (NULL);
269:
270: grid_expand_line(gd, py, px + 1);
1.10 nicm 271: return (&gd->linedata[py].celldata[px]);
1.1 nicm 272: }
273:
274: /* Set cell at relative position. */
275: void
276: grid_set_cell(
277: struct grid *gd, u_int px, u_int py, const struct grid_cell *gc)
278: {
279: if (grid_check_y(gd, py) != 0)
280: return;
281:
282: grid_expand_line(gd, py, px + 1);
283: grid_put_cell(gd, px, py, gc);
284: }
285:
286: /* Get UTF-8 for reading. */
287: const struct grid_utf8 *
288: grid_peek_utf8(struct grid *gd, u_int px, u_int py)
289: {
290: if (grid_check_y(gd, py) != 0)
291: return (NULL);
292:
1.10 nicm 293: if (px >= gd->linedata[py].utf8size)
1.1 nicm 294: return (NULL);
1.10 nicm 295: return (&gd->linedata[py].utf8data[px]);
1.1 nicm 296: }
297:
298: /* Get utf8 at relative position (for writing). */
299: struct grid_utf8 *
300: grid_get_utf8(struct grid *gd, u_int px, u_int py)
301: {
302: if (grid_check_y(gd, py) != 0)
303: return (NULL);
304:
305: grid_expand_line_utf8(gd, py, px + 1);
1.10 nicm 306: return (&gd->linedata[py].utf8data[px]);
1.1 nicm 307: }
308:
309: /* Set utf8 at relative position. */
310: void
311: grid_set_utf8(
312: struct grid *gd, u_int px, u_int py, const struct grid_utf8 *gc)
313: {
314: if (grid_check_y(gd, py) != 0)
315: return;
316:
317: grid_expand_line_utf8(gd, py, px + 1);
318: grid_put_utf8(gd, px, py, gc);
319: }
320:
1.14 nicm 321: /* Clear area. */
1.1 nicm 322: void
323: grid_clear(struct grid *gd, u_int px, u_int py, u_int nx, u_int ny)
324: {
325: u_int xx, yy;
326:
1.17 nicm 327: GRID_DEBUG(gd, "px=%u, py=%u, nx=%u, ny=%u", px, py, nx, ny);
1.1 nicm 328:
329: if (nx == 0 || ny == 0)
330: return;
331:
332: if (px == 0 && nx == gd->sx) {
333: grid_clear_lines(gd, py, ny);
334: return;
335: }
336:
337: if (grid_check_y(gd, py) != 0)
338: return;
339: if (grid_check_y(gd, py + ny - 1) != 0)
340: return;
341:
342: for (yy = py; yy < py + ny; yy++) {
1.14 nicm 343: if (px >= gd->linedata[yy].cellsize)
344: continue;
345: if (px + nx >= gd->linedata[yy].cellsize) {
346: gd->linedata[yy].cellsize = px;
347: continue;
348: }
1.1 nicm 349: for (xx = px; xx < px + nx; xx++) {
1.10 nicm 350: if (xx >= gd->linedata[yy].cellsize)
1.1 nicm 351: break;
352: grid_put_cell(gd, xx, yy, &grid_default_cell);
353: }
354: }
355: }
356:
357: /* Clear lines. This just frees and truncates the lines. */
358: void
359: grid_clear_lines(struct grid *gd, u_int py, u_int ny)
360: {
1.10 nicm 361: struct grid_line *gl;
362: u_int yy;
1.1 nicm 363:
1.17 nicm 364: GRID_DEBUG(gd, "py=%u, ny=%u", py, ny);
1.1 nicm 365:
366: if (ny == 0)
367: return;
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.10 nicm 375: gl = &gd->linedata[yy];
376: if (gl->celldata != NULL)
377: xfree(gl->celldata);
378: if (gl->utf8data != NULL)
379: xfree(gl->utf8data);
380: memset(gl, 0, sizeof *gl);
1.1 nicm 381: }
382: }
383:
384: /* Move a group of lines. */
385: void
386: grid_move_lines(struct grid *gd, u_int dy, u_int py, u_int ny)
387: {
388: u_int yy;
389:
1.17 nicm 390: GRID_DEBUG(gd, "dy=%u, py=%u, ny=%u", dy, py, ny);
1.1 nicm 391:
392: if (ny == 0 || py == dy)
393: return;
394:
395: if (grid_check_y(gd, py) != 0)
396: return;
397: if (grid_check_y(gd, py + ny - 1) != 0)
398: return;
399: if (grid_check_y(gd, dy) != 0)
400: return;
401: if (grid_check_y(gd, dy + ny - 1) != 0)
402: return;
403:
404: /* Free any lines which are being replaced. */
405: for (yy = dy; yy < dy + ny; yy++) {
406: if (yy >= py && yy < py + ny)
407: continue;
408: grid_clear_lines(gd, yy, 1);
409: }
410:
1.10 nicm 411: memmove(
412: &gd->linedata[dy], &gd->linedata[py], ny * (sizeof *gd->linedata));
1.1 nicm 413:
414: /* Wipe any lines that have been moved (without freeing them). */
415: for (yy = py; yy < py + ny; yy++) {
416: if (yy >= dy && yy < dy + ny)
417: continue;
1.10 nicm 418: memset(&gd->linedata[yy], 0, sizeof gd->linedata[yy]);
1.1 nicm 419: }
420: }
421:
422: /* Move a group of cells. */
423: void
424: grid_move_cells(struct grid *gd, u_int dx, u_int px, u_int py, u_int nx)
425: {
1.10 nicm 426: struct grid_line *gl;
427: u_int xx;
1.1 nicm 428:
1.17 nicm 429: GRID_DEBUG(gd, "dx=%u, px=%u, py=%u, nx=%u", dx, px, py, nx);
1.1 nicm 430:
431: if (nx == 0 || px == dx)
432: return;
433:
434: if (grid_check_y(gd, py) != 0)
435: return;
1.10 nicm 436: gl = &gd->linedata[py];
1.1 nicm 437:
438: grid_expand_line(gd, py, px + nx);
439: grid_expand_line(gd, py, dx + nx);
1.10 nicm 440: memmove(
441: &gl->celldata[dx], &gl->celldata[px], nx * sizeof *gl->celldata);
1.1 nicm 442:
1.10 nicm 443: if (gl->utf8data != NULL) {
1.1 nicm 444: grid_expand_line_utf8(gd, py, px + nx);
445: grid_expand_line_utf8(gd, py, dx + nx);
1.10 nicm 446: memmove(&gl->utf8data[dx],
447: &gl->utf8data[px], nx * sizeof *gl->utf8data);
1.1 nicm 448: }
449:
450: /* Wipe any cells that have been moved. */
451: for (xx = px; xx < px + nx; xx++) {
452: if (xx >= dx && xx < dx + nx)
453: continue;
454: grid_put_cell(gd, xx, py, &grid_default_cell);
455: }
1.3 nicm 456: }
457:
458: /* Convert cells into a string. */
459: char *
460: grid_string_cells(struct grid *gd, u_int px, u_int py, u_int nx)
461: {
1.17 nicm 462: const struct grid_cell *gc;
463: const struct grid_utf8 *gu;
1.3 nicm 464: char *buf;
1.16 nicm 465: size_t len, off, size;
466: u_int xx;
1.3 nicm 467:
468: GRID_DEBUG(gd, "px=%u, py=%u, nx=%u", px, py, nx);
469:
470: len = 128;
471: buf = xmalloc(len);
472: off = 0;
473:
474: for (xx = px; xx < px + nx; xx++) {
475: gc = grid_peek_cell(gd, xx, py);
476: if (gc->flags & GRID_FLAG_PADDING)
477: continue;
478:
479: if (gc->flags & GRID_FLAG_UTF8) {
1.16 nicm 480: gu = grid_peek_utf8(gd, xx, py);
481:
482: size = grid_utf8_size(gu);
483: while (len < off + size + 1) {
1.3 nicm 484: buf = xrealloc(buf, 2, len);
485: len *= 2;
486: }
487:
1.16 nicm 488: off += grid_utf8_copy(gu, buf + off, len - off);
1.3 nicm 489: } else {
490: while (len < off + 2) {
491: buf = xrealloc(buf, 2, len);
492: len *= 2;
493: }
494:
495: buf[off++] = gc->data;
496: }
497: }
1.17 nicm 498:
1.4 nicm 499: while (off > 0 && buf[off - 1] == ' ')
500: off--;
1.3 nicm 501: buf[off] = '\0';
502: return (buf);
1.7 nicm 503: }
504:
1.17 nicm 505: /*
1.7 nicm 506: * Duplicate a set of lines between two grids. If there aren't enough lines in
507: * either source or destination, the number of lines is limited to the number
508: * available.
509: */
510: void
511: grid_duplicate_lines(
512: struct grid *dst, u_int dy, struct grid *src, u_int sy, u_int ny)
513: {
1.10 nicm 514: struct grid_line *dstl, *srcl;
515: u_int yy;
1.7 nicm 516:
517: GRID_DEBUG(src, "dy=%u, sy=%u, ny=%u", dy, sy, ny);
518:
519: if (dy + ny > dst->hsize + dst->sy)
520: ny = dst->hsize + dst->sy - dy;
521: if (sy + ny > src->hsize + src->sy)
522: ny = src->hsize + src->sy - sy;
523: grid_clear_lines(dst, dy, ny);
524:
525: for (yy = 0; yy < ny; yy++) {
1.11 nicm 526: srcl = &src->linedata[sy];
527: dstl = &dst->linedata[dy];
1.10 nicm 528:
529: memcpy(dstl, srcl, sizeof *dstl);
530: if (srcl->cellsize != 0) {
531: dstl->celldata = xcalloc(
532: srcl->cellsize, sizeof *dstl->celldata);
533: memcpy(dstl->celldata, srcl->celldata,
534: srcl->cellsize * sizeof *dstl->celldata);
1.7 nicm 535: }
1.10 nicm 536: if (srcl->utf8size != 0) {
537: dstl->utf8data = xcalloc(
538: srcl->utf8size, sizeof *dstl->utf8data);
539: memcpy(dstl->utf8data, srcl->utf8data,
540: srcl->utf8size * sizeof *dstl->utf8data);
1.7 nicm 541: }
542:
1.10 nicm 543: sy++;
544: dy++;
1.7 nicm 545: }
1.1 nicm 546: }