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