Annotation of src/usr.bin/tmux/grid.c, Revision 1.70
1.70 ! nicm 1: /* $OpenBSD: grid.c,v 1.69 2017/04/19 12:44:29 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
287: grid_scroll_history_region(struct grid *gd, u_int upper, u_int lower)
288: {
289: struct grid_line *gl_history, *gl_upper, *gl_lower;
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: gl_lower = &gd->linedata[lower];
306:
307: /* Move the line into the history. */
308: memcpy(gl_history, gl_upper, sizeof *gl_history);
309:
310: /* Then move the region up and clear the bottom line. */
311: memmove(gl_upper, gl_upper + 1, (lower - upper) * sizeof *gl_upper);
312: memset(gl_lower, 0, sizeof *gl_lower);
313:
314: /* Move the history offset down over the line. */
1.55 nicm 315: gd->hscrolled++;
1.1 nicm 316: gd->hsize++;
317: }
318:
319: /* Expand line to fit to cell. */
1.57 nicm 320: static void
1.58 nicm 321: grid_expand_line(struct grid *gd, u_int py, u_int sx, u_int bg)
1.1 nicm 322: {
1.10 nicm 323: struct grid_line *gl;
1.14 nicm 324: u_int xx;
1.1 nicm 325:
1.10 nicm 326: gl = &gd->linedata[py];
1.14 nicm 327: if (sx <= gl->cellsize)
1.1 nicm 328: return;
1.61 nicm 329:
1.62 nicm 330: if (sx < gd->sx / 4)
331: sx = gd->sx / 4;
332: else if (sx < gd->sx / 2)
333: sx = gd->sx / 2;
334: else
335: sx = gd->sx;
1.1 nicm 336:
1.41 nicm 337: gl->celldata = xreallocarray(gl->celldata, sx, sizeof *gl->celldata);
1.10 nicm 338: for (xx = gl->cellsize; xx < sx; xx++)
1.58 nicm 339: grid_clear_cell(gd, xx, py, bg);
1.10 nicm 340: gl->cellsize = sx;
1.1 nicm 341: }
342:
1.58 nicm 343: /* Empty a line and set background colour if needed. */
344: static void
345: grid_empty_line(struct grid *gd, u_int py, u_int bg)
346: {
347: memset(&gd->linedata[py], 0, sizeof gd->linedata[py]);
348: if (bg != 8)
349: grid_expand_line(gd, py, gd->sx, bg);
350: }
351:
1.26 nicm 352: /* Peek at grid line. */
353: const struct grid_line *
354: grid_peek_line(struct grid *gd, u_int py)
355: {
356: if (grid_check_y(gd, py) != 0)
357: return (NULL);
358: return (&gd->linedata[py]);
359: }
360:
1.1 nicm 361: /* Get cell for reading. */
1.48 nicm 362: void
363: grid_get_cell(struct grid *gd, u_int px, u_int py, struct grid_cell *gc)
1.1 nicm 364: {
1.48 nicm 365: struct grid_line *gl;
366: struct grid_cell_entry *gce;
367:
368: if (grid_check_y(gd, py) != 0 || px >= gd->linedata[py].cellsize) {
369: memcpy(gc, &grid_default_cell, sizeof *gc);
370: return;
371: }
1.1 nicm 372:
1.48 nicm 373: gl = &gd->linedata[py];
374: gce = &gl->celldata[px];
1.1 nicm 375:
1.48 nicm 376: if (gce->flags & GRID_FLAG_EXTENDED) {
377: if (gce->offset >= gl->extdsize)
378: memcpy(gc, &grid_default_cell, sizeof *gc);
379: else
380: memcpy(gc, &gl->extddata[gce->offset], sizeof *gc);
381: return;
382: }
1.1 nicm 383:
1.53 nicm 384: gc->flags = gce->flags & ~(GRID_FLAG_FG256|GRID_FLAG_BG256);
1.48 nicm 385: gc->attr = gce->data.attr;
386: gc->fg = gce->data.fg;
1.53 nicm 387: if (gce->flags & GRID_FLAG_FG256)
388: gc->fg |= COLOUR_FLAG_256;
1.48 nicm 389: gc->bg = gce->data.bg;
1.53 nicm 390: if (gce->flags & GRID_FLAG_BG256)
391: gc->bg |= COLOUR_FLAG_256;
1.48 nicm 392: utf8_set(&gc->data, gce->data.data);
1.1 nicm 393: }
394:
395: /* Set cell at relative position. */
396: void
1.31 nicm 397: grid_set_cell(struct grid *gd, u_int px, u_int py, const struct grid_cell *gc)
1.1 nicm 398: {
1.48 nicm 399: struct grid_line *gl;
400: struct grid_cell_entry *gce;
401:
1.1 nicm 402: if (grid_check_y(gd, py) != 0)
403: return;
404:
1.58 nicm 405: grid_expand_line(gd, py, px + 1, 8);
1.48 nicm 406:
407: gl = &gd->linedata[py];
1.58 nicm 408: if (px + 1 > gl->cellused)
409: gl->cellused = px + 1;
410:
1.63 nicm 411: gce = &gl->celldata[px];
1.65 nicm 412: if (grid_need_extended_cell(gce, gc))
1.59 nicm 413: grid_extended_cell(gl, gce, gc);
1.63 nicm 414: else
415: grid_store_cell(gce, gc, gc->data.data[0]);
1.64 nicm 416: }
417:
418: /* Set cells at relative position. */
419: void
420: grid_set_cells(struct grid *gd, u_int px, u_int py, const struct grid_cell *gc,
421: const char *s, size_t slen)
422: {
423: struct grid_line *gl;
424: struct grid_cell_entry *gce;
425: struct grid_cell *gcp;
426: u_int i;
427:
428: if (grid_check_y(gd, py) != 0)
429: return;
430:
431: grid_expand_line(gd, py, px + slen, 8);
432:
433: gl = &gd->linedata[py];
434: if (px + slen > gl->cellused)
435: gl->cellused = px + slen;
436:
437: for (i = 0; i < slen; i++) {
438: gce = &gl->celldata[px + i];
1.65 nicm 439: if (grid_need_extended_cell(gce, gc)) {
440: gcp = grid_extended_cell(gl, gce, gc);
1.64 nicm 441: utf8_set(&gcp->data, s[i]);
442: } else
443: grid_store_cell(gce, gc, s[i]);
444: }
1.1 nicm 445: }
446:
1.14 nicm 447: /* Clear area. */
1.1 nicm 448: void
1.58 nicm 449: grid_clear(struct grid *gd, u_int px, u_int py, u_int nx, u_int ny, u_int bg)
1.1 nicm 450: {
451: u_int xx, yy;
452:
453: if (nx == 0 || ny == 0)
454: return;
455:
456: if (px == 0 && nx == gd->sx) {
1.58 nicm 457: grid_clear_lines(gd, py, ny, bg);
1.1 nicm 458: return;
459: }
460:
461: if (grid_check_y(gd, py) != 0)
462: return;
463: if (grid_check_y(gd, py + ny - 1) != 0)
464: return;
465:
466: for (yy = py; yy < py + ny; yy++) {
1.58 nicm 467: if (px + nx >= gd->sx && px < gd->linedata[yy].cellused)
468: gd->linedata[yy].cellused = px;
469: if (px > gd->linedata[yy].cellsize && bg == 8)
1.14 nicm 470: continue;
1.58 nicm 471: if (px + nx >= gd->linedata[yy].cellsize && bg == 8) {
1.14 nicm 472: gd->linedata[yy].cellsize = px;
473: continue;
474: }
1.58 nicm 475: grid_expand_line(gd, yy, px + nx, bg);
476: for (xx = px; xx < px + nx; xx++)
477: grid_clear_cell(gd, xx, yy, bg);
1.1 nicm 478: }
479: }
480:
481: /* Clear lines. This just frees and truncates the lines. */
482: void
1.58 nicm 483: grid_clear_lines(struct grid *gd, u_int py, u_int ny, u_int bg)
1.1 nicm 484: {
1.10 nicm 485: struct grid_line *gl;
486: u_int yy;
1.1 nicm 487:
488: if (ny == 0)
489: return;
490:
491: if (grid_check_y(gd, py) != 0)
492: return;
493: if (grid_check_y(gd, py + ny - 1) != 0)
494: return;
495:
496: for (yy = py; yy < py + ny; yy++) {
1.10 nicm 497: gl = &gd->linedata[yy];
1.20 nicm 498: free(gl->celldata);
1.49 nicm 499: free(gl->extddata);
1.58 nicm 500: grid_empty_line(gd, yy, bg);
1.1 nicm 501: }
502: }
503:
504: /* Move a group of lines. */
505: void
1.58 nicm 506: grid_move_lines(struct grid *gd, u_int dy, u_int py, u_int ny, u_int bg)
1.1 nicm 507: {
508: u_int yy;
509:
510: if (ny == 0 || py == dy)
511: return;
512:
513: if (grid_check_y(gd, py) != 0)
514: return;
515: if (grid_check_y(gd, py + ny - 1) != 0)
516: return;
517: if (grid_check_y(gd, dy) != 0)
518: return;
519: if (grid_check_y(gd, dy + ny - 1) != 0)
520: return;
521:
522: /* Free any lines which are being replaced. */
523: for (yy = dy; yy < dy + ny; yy++) {
524: if (yy >= py && yy < py + ny)
525: continue;
1.58 nicm 526: grid_clear_lines(gd, yy, 1, bg);
1.1 nicm 527: }
528:
1.46 nicm 529: memmove(&gd->linedata[dy], &gd->linedata[py],
530: ny * (sizeof *gd->linedata));
1.1 nicm 531:
532: /* Wipe any lines that have been moved (without freeing them). */
533: for (yy = py; yy < py + ny; yy++) {
1.58 nicm 534: if (yy < dy || yy >= dy + ny)
535: grid_empty_line(gd, yy, bg);
1.1 nicm 536: }
537: }
538:
539: /* Move a group of cells. */
540: void
1.58 nicm 541: grid_move_cells(struct grid *gd, u_int dx, u_int px, u_int py, u_int nx,
542: u_int bg)
1.1 nicm 543: {
1.10 nicm 544: struct grid_line *gl;
545: u_int xx;
1.1 nicm 546:
547: if (nx == 0 || px == dx)
548: return;
549:
550: if (grid_check_y(gd, py) != 0)
551: return;
1.10 nicm 552: gl = &gd->linedata[py];
1.1 nicm 553:
1.58 nicm 554: grid_expand_line(gd, py, px + nx, 8);
555: grid_expand_line(gd, py, dx + nx, 8);
1.46 nicm 556: memmove(&gl->celldata[dx], &gl->celldata[px],
557: nx * sizeof *gl->celldata);
1.67 nicm 558: if (dx + nx > gl->cellused)
559: gl->cellused = dx + nx;
1.1 nicm 560:
561: /* Wipe any cells that have been moved. */
562: for (xx = px; xx < px + nx; xx++) {
563: if (xx >= dx && xx < dx + nx)
564: continue;
1.58 nicm 565: grid_clear_cell(gd, xx, py, bg);
1.1 nicm 566: }
1.3 nicm 567: }
568:
1.24 nicm 569: /* Get ANSI foreground sequence. */
1.56 nicm 570: static size_t
1.24 nicm 571: grid_string_cells_fg(const struct grid_cell *gc, int *values)
572: {
573: size_t n;
1.53 nicm 574: u_char r, g, b;
1.24 nicm 575:
576: n = 0;
1.53 nicm 577: if (gc->fg & COLOUR_FLAG_256) {
1.24 nicm 578: values[n++] = 38;
579: values[n++] = 5;
1.53 nicm 580: values[n++] = gc->fg & 0xff;
581: } else if (gc->fg & COLOUR_FLAG_RGB) {
1.52 nicm 582: values[n++] = 38;
583: values[n++] = 2;
1.53 nicm 584: colour_split_rgb(gc->fg, &r, &g, &b);
585: values[n++] = r;
586: values[n++] = g;
587: values[n++] = b;
1.24 nicm 588: } else {
589: switch (gc->fg) {
1.45 nicm 590: case 0:
591: case 1:
592: case 2:
593: case 3:
594: case 4:
595: case 5:
596: case 6:
597: case 7:
598: values[n++] = gc->fg + 30;
599: break;
600: case 8:
601: values[n++] = 39;
602: break;
603: case 90:
604: case 91:
605: case 92:
606: case 93:
607: case 94:
608: case 95:
609: case 96:
610: case 97:
611: values[n++] = gc->fg;
612: break;
1.24 nicm 613: }
614: }
615: return (n);
616: }
617:
618: /* Get ANSI background sequence. */
1.56 nicm 619: static size_t
1.24 nicm 620: grid_string_cells_bg(const struct grid_cell *gc, int *values)
621: {
622: size_t n;
1.53 nicm 623: u_char r, g, b;
1.24 nicm 624:
625: n = 0;
1.53 nicm 626: if (gc->bg & COLOUR_FLAG_256) {
1.24 nicm 627: values[n++] = 48;
628: values[n++] = 5;
1.53 nicm 629: values[n++] = gc->bg & 0xff;
630: } else if (gc->bg & COLOUR_FLAG_RGB) {
1.52 nicm 631: values[n++] = 48;
632: values[n++] = 2;
1.53 nicm 633: colour_split_rgb(gc->bg, &r, &g, &b);
634: values[n++] = r;
635: values[n++] = g;
636: values[n++] = b;
1.24 nicm 637: } else {
638: switch (gc->bg) {
639: case 0:
640: case 1:
641: case 2:
642: case 3:
643: case 4:
644: case 5:
645: case 6:
646: case 7:
647: values[n++] = gc->bg + 40;
648: break;
649: case 8:
650: values[n++] = 49;
651: break;
652: case 100:
653: case 101:
654: case 102:
655: case 103:
656: case 104:
1.53 nicm 657: case 105:
1.24 nicm 658: case 106:
659: case 107:
660: values[n++] = gc->bg - 10;
661: break;
662: }
663: }
664: return (n);
665: }
666:
667: /*
668: * Returns ANSI code to set particular attributes (colour, bold and so on)
669: * given a current state. The output buffer must be able to hold at least 57
670: * bytes.
671: */
1.56 nicm 672: static void
1.24 nicm 673: grid_string_cells_code(const struct grid_cell *lastgc,
1.26 nicm 674: const struct grid_cell *gc, char *buf, size_t len, int escape_c0)
1.24 nicm 675: {
1.52 nicm 676: int oldc[64], newc[64], s[128];
1.24 nicm 677: size_t noldc, nnewc, n, i;
1.70 ! nicm 678: u_int attr = gc->attr, lastattr = lastgc->attr;
1.24 nicm 679: char tmp[64];
680:
681: struct {
682: u_int mask;
683: u_int code;
684: } attrs[] = {
685: { GRID_ATTR_BRIGHT, 1 },
686: { GRID_ATTR_DIM, 2 },
687: { GRID_ATTR_ITALICS, 3 },
688: { GRID_ATTR_UNDERSCORE, 4 },
689: { GRID_ATTR_BLINK, 5 },
690: { GRID_ATTR_REVERSE, 7 },
1.68 nicm 691: { GRID_ATTR_HIDDEN, 8 },
692: { GRID_ATTR_STRIKETHROUGH, 9 }
1.24 nicm 693: };
694: n = 0;
695:
696: /* If any attribute is removed, begin with 0. */
697: for (i = 0; i < nitems(attrs); i++) {
698: if (!(attr & attrs[i].mask) && (lastattr & attrs[i].mask)) {
699: s[n++] = 0;
1.25 nicm 700: lastattr &= GRID_ATTR_CHARSET;
1.24 nicm 701: break;
702: }
703: }
704: /* For each attribute that is newly set, add its code. */
705: for (i = 0; i < nitems(attrs); i++) {
706: if ((attr & attrs[i].mask) && !(lastattr & attrs[i].mask))
707: s[n++] = attrs[i].code;
708: }
709:
1.70 ! nicm 710: /* Write the attributes. */
! 711: *buf = '\0';
! 712: if (n > 0) {
! 713: if (escape_c0)
! 714: strlcat(buf, "\\033[", len);
! 715: else
! 716: strlcat(buf, "\033[", len);
! 717: for (i = 0; i < n; i++) {
! 718: if (i + 1 < n)
! 719: xsnprintf(tmp, sizeof tmp, "%d;", s[i]);
! 720: else
! 721: xsnprintf(tmp, sizeof tmp, "%d", s[i]);
! 722: strlcat(buf, tmp, len);
! 723: }
! 724: strlcat(buf, "m", len);
! 725: }
! 726:
! 727: /* If the foreground colour changed, write its parameters. */
1.24 nicm 728: nnewc = grid_string_cells_fg(gc, newc);
729: noldc = grid_string_cells_fg(lastgc, oldc);
1.70 ! nicm 730: if (nnewc != noldc ||
! 731: memcmp(newc, oldc, nnewc * sizeof newc[0]) != 0 ||
! 732: (n != 0 && s[0] == 0)) {
! 733: if (escape_c0)
! 734: strlcat(buf, "\\033[", len);
! 735: else
! 736: strlcat(buf, "\033[", len);
! 737: for (i = 0; i < nnewc; i++) {
! 738: if (i + 1 < nnewc)
! 739: xsnprintf(tmp, sizeof tmp, "%d;", newc[i]);
! 740: else
! 741: xsnprintf(tmp, sizeof tmp, "%d", newc[i]);
! 742: strlcat(buf, tmp, len);
! 743: }
! 744: strlcat(buf, "m", len);
1.24 nicm 745: }
746:
1.39 nicm 747: /* If the background colour changed, append its parameters. */
1.24 nicm 748: nnewc = grid_string_cells_bg(gc, newc);
749: noldc = grid_string_cells_bg(lastgc, oldc);
1.70 ! nicm 750: if (nnewc != noldc ||
! 751: memcmp(newc, oldc, nnewc * sizeof newc[0]) != 0 ||
! 752: (n != 0 && s[0] == 0)) {
1.26 nicm 753: if (escape_c0)
754: strlcat(buf, "\\033[", len);
755: else
756: strlcat(buf, "\033[", len);
1.70 ! nicm 757: for (i = 0; i < nnewc; i++) {
! 758: if (i + 1 < nnewc)
! 759: xsnprintf(tmp, sizeof tmp, "%d;", newc[i]);
1.24 nicm 760: else
1.70 ! nicm 761: xsnprintf(tmp, sizeof tmp, "%d", newc[i]);
1.24 nicm 762: strlcat(buf, tmp, len);
763: }
764: strlcat(buf, "m", len);
765: }
766:
767: /* Append shift in/shift out if needed. */
1.26 nicm 768: if ((attr & GRID_ATTR_CHARSET) && !(lastattr & GRID_ATTR_CHARSET)) {
769: if (escape_c0)
1.70 ! nicm 770: strlcat(buf, "\\016", len); /* SO */
1.26 nicm 771: else
772: strlcat(buf, "\016", len); /* SO */
773: }
774: if (!(attr & GRID_ATTR_CHARSET) && (lastattr & GRID_ATTR_CHARSET)) {
775: if (escape_c0)
1.70 ! nicm 776: strlcat(buf, "\\017", len); /* SI */
1.26 nicm 777: else
778: strlcat(buf, "\017", len); /* SI */
779: }
1.24 nicm 780: }
781:
1.3 nicm 782: /* Convert cells into a string. */
783: char *
1.24 nicm 784: grid_string_cells(struct grid *gd, u_int px, u_int py, u_int nx,
1.28 nicm 785: struct grid_cell **lastgc, int with_codes, int escape_c0, int trim)
1.3 nicm 786: {
1.48 nicm 787: struct grid_cell gc;
1.24 nicm 788: static struct grid_cell lastgc1;
1.38 nicm 789: const char *data;
1.24 nicm 790: char *buf, code[128];
1.26 nicm 791: size_t len, off, size, codelen;
1.16 nicm 792: u_int xx;
1.30 nicm 793: const struct grid_line *gl;
1.3 nicm 794:
1.29 nicm 795: if (lastgc != NULL && *lastgc == NULL) {
1.24 nicm 796: memcpy(&lastgc1, &grid_default_cell, sizeof lastgc1);
797: *lastgc = &lastgc1;
798: }
799:
1.3 nicm 800: len = 128;
801: buf = xmalloc(len);
802: off = 0;
803:
1.30 nicm 804: gl = grid_peek_line(gd, py);
1.3 nicm 805: for (xx = px; xx < px + nx; xx++) {
1.30 nicm 806: if (gl == NULL || xx >= gl->cellsize)
807: break;
1.48 nicm 808: grid_get_cell(gd, xx, py, &gc);
809: if (gc.flags & GRID_FLAG_PADDING)
1.3 nicm 810: continue;
811:
1.24 nicm 812: if (with_codes) {
1.48 nicm 813: grid_string_cells_code(*lastgc, &gc, code, sizeof code,
1.26 nicm 814: escape_c0);
1.24 nicm 815: codelen = strlen(code);
1.48 nicm 816: memcpy(*lastgc, &gc, sizeof **lastgc);
1.24 nicm 817: } else
818: codelen = 0;
819:
1.48 nicm 820: data = gc.data.data;
821: size = gc.data.size;
1.26 nicm 822: if (escape_c0 && size == 1 && *data == '\\') {
1.27 nicm 823: data = "\\\\";
1.26 nicm 824: size = 2;
825: }
826:
827: while (len < off + size + codelen + 1) {
1.41 nicm 828: buf = xreallocarray(buf, 2, len);
1.21 nicm 829: len *= 2;
830: }
1.3 nicm 831:
1.24 nicm 832: if (codelen != 0) {
833: memcpy(buf + off, code, codelen);
834: off += codelen;
835: }
1.26 nicm 836: memcpy(buf + off, data, size);
837: off += size;
1.3 nicm 838: }
1.17 nicm 839:
1.37 nicm 840: if (trim) {
1.28 nicm 841: while (off > 0 && buf[off - 1] == ' ')
842: off--;
1.32 nicm 843: }
1.3 nicm 844: buf[off] = '\0';
1.26 nicm 845:
1.3 nicm 846: return (buf);
1.7 nicm 847: }
848:
1.17 nicm 849: /*
1.7 nicm 850: * Duplicate a set of lines between two grids. If there aren't enough lines in
851: * either source or destination, the number of lines is limited to the number
852: * available.
853: */
854: void
1.31 nicm 855: grid_duplicate_lines(struct grid *dst, u_int dy, struct grid *src, u_int sy,
856: u_int ny)
1.7 nicm 857: {
1.10 nicm 858: struct grid_line *dstl, *srcl;
859: u_int yy;
1.7 nicm 860:
861: if (dy + ny > dst->hsize + dst->sy)
862: ny = dst->hsize + dst->sy - dy;
863: if (sy + ny > src->hsize + src->sy)
864: ny = src->hsize + src->sy - sy;
1.58 nicm 865: grid_clear_lines(dst, dy, ny, 8);
1.7 nicm 866:
867: for (yy = 0; yy < ny; yy++) {
1.11 nicm 868: srcl = &src->linedata[sy];
869: dstl = &dst->linedata[dy];
1.10 nicm 870:
871: memcpy(dstl, srcl, sizeof *dstl);
872: if (srcl->cellsize != 0) {
1.42 deraadt 873: dstl->celldata = xreallocarray(NULL,
1.10 nicm 874: srcl->cellsize, sizeof *dstl->celldata);
875: memcpy(dstl->celldata, srcl->celldata,
876: srcl->cellsize * sizeof *dstl->celldata);
1.44 nicm 877: } else
878: dstl->celldata = NULL;
1.7 nicm 879:
1.48 nicm 880: if (srcl->extdsize != 0) {
881: dstl->extdsize = srcl->extdsize;
882: dstl->extddata = xreallocarray(NULL, dstl->extdsize,
883: sizeof *dstl->extddata);
884: memcpy(dstl->extddata, srcl->extddata, dstl->extdsize *
885: sizeof *dstl->extddata);
886: }
887:
1.10 nicm 888: sy++;
889: dy++;
1.7 nicm 890: }
1.22 nicm 891: }
892:
1.48 nicm 893: /* Copy a section of a line. */
1.56 nicm 894: static void
1.48 nicm 895: grid_reflow_copy(struct grid_line *dst_gl, u_int to, struct grid_line *src_gl,
896: u_int from, u_int to_copy)
897: {
898: struct grid_cell_entry *gce;
899: u_int i, was;
900:
901: memcpy(&dst_gl->celldata[to], &src_gl->celldata[from],
902: to_copy * sizeof *dst_gl->celldata);
903:
904: for (i = to; i < to + to_copy; i++) {
905: gce = &dst_gl->celldata[i];
906: if (~gce->flags & GRID_FLAG_EXTENDED)
907: continue;
908: was = gce->offset;
909:
910: dst_gl->extddata = xreallocarray(dst_gl->extddata,
911: dst_gl->extdsize + 1, sizeof *dst_gl->extddata);
912: gce->offset = dst_gl->extdsize++;
913: memcpy(&dst_gl->extddata[gce->offset], &src_gl->extddata[was],
914: sizeof *dst_gl->extddata);
915: }
916: }
917:
1.23 nicm 918: /* Join line data. */
1.56 nicm 919: static void
1.23 nicm 920: grid_reflow_join(struct grid *dst, u_int *py, struct grid_line *src_gl,
921: u_int new_x)
922: {
923: struct grid_line *dst_gl = &dst->linedata[(*py) - 1];
924: u_int left, to_copy, ox, nx;
925:
926: /* How much is left on the old line? */
1.58 nicm 927: left = new_x - dst_gl->cellused;
1.23 nicm 928:
929: /* Work out how much to append. */
1.58 nicm 930: to_copy = src_gl->cellused;
1.23 nicm 931: if (to_copy > left)
932: to_copy = left;
1.58 nicm 933: ox = dst_gl->cellused;
1.23 nicm 934: nx = ox + to_copy;
935:
936: /* Resize the destination line. */
1.41 nicm 937: dst_gl->celldata = xreallocarray(dst_gl->celldata, nx,
1.23 nicm 938: sizeof *dst_gl->celldata);
1.58 nicm 939: dst_gl->cellsize = dst_gl->cellused = nx;
1.23 nicm 940:
941: /* Append as much as possible. */
1.48 nicm 942: grid_reflow_copy(dst_gl, ox, src_gl, 0, to_copy);
1.23 nicm 943:
944: /* If there is any left in the source, split it. */
1.58 nicm 945: if (src_gl->cellused > to_copy) {
1.23 nicm 946: dst_gl->flags |= GRID_LINE_WRAPPED;
947:
1.58 nicm 948: src_gl->cellused -= to_copy;
1.23 nicm 949: grid_reflow_split(dst, py, src_gl, new_x, to_copy);
950: }
951: }
952:
953: /* Split line data. */
1.56 nicm 954: static void
1.23 nicm 955: grid_reflow_split(struct grid *dst, u_int *py, struct grid_line *src_gl,
956: u_int new_x, u_int offset)
957: {
958: struct grid_line *dst_gl = NULL;
959: u_int to_copy;
960:
961: /* Loop and copy sections of the source line. */
1.58 nicm 962: while (src_gl->cellused > 0) {
1.23 nicm 963: /* Create new line. */
964: if (*py >= dst->hsize + dst->sy)
1.58 nicm 965: grid_scroll_history(dst, 8);
1.23 nicm 966: dst_gl = &dst->linedata[*py];
967: (*py)++;
968:
969: /* How much should we copy? */
970: to_copy = new_x;
1.58 nicm 971: if (to_copy > src_gl->cellused)
972: to_copy = src_gl->cellused;
1.23 nicm 973:
974: /* Expand destination line. */
1.41 nicm 975: dst_gl->celldata = xreallocarray(NULL, to_copy,
1.40 nicm 976: sizeof *dst_gl->celldata);
1.58 nicm 977: dst_gl->cellsize = dst_gl->cellused = to_copy;
1.23 nicm 978: dst_gl->flags |= GRID_LINE_WRAPPED;
979:
980: /* Copy the data. */
1.48 nicm 981: grid_reflow_copy(dst_gl, 0, src_gl, offset, to_copy);
1.23 nicm 982:
983: /* Move offset and reduce old line size. */
984: offset += to_copy;
1.58 nicm 985: src_gl->cellused -= to_copy;
1.23 nicm 986: }
987:
988: /* Last line is not wrapped. */
989: if (dst_gl != NULL)
990: dst_gl->flags &= ~GRID_LINE_WRAPPED;
991: }
992:
993: /* Move line data. */
1.56 nicm 994: static void
1.23 nicm 995: grid_reflow_move(struct grid *dst, u_int *py, struct grid_line *src_gl)
996: {
997: struct grid_line *dst_gl;
998:
999: /* Create new line. */
1000: if (*py >= dst->hsize + dst->sy)
1.58 nicm 1001: grid_scroll_history(dst, 8);
1.23 nicm 1002: dst_gl = &dst->linedata[*py];
1003: (*py)++;
1004:
1005: /* Copy the old line. */
1006: memcpy(dst_gl, src_gl, sizeof *dst_gl);
1007: dst_gl->flags &= ~GRID_LINE_WRAPPED;
1008:
1009: /* Clear old line. */
1010: src_gl->celldata = NULL;
1.48 nicm 1011: src_gl->extddata = NULL;
1.23 nicm 1012: }
1013:
1.22 nicm 1014: /*
1.23 nicm 1015: * Reflow lines from src grid into dst grid of width new_x. Returns number of
1016: * lines fewer in the visible area. The source grid is destroyed.
1.22 nicm 1017: */
1018: u_int
1.23 nicm 1019: grid_reflow(struct grid *dst, struct grid *src, u_int new_x)
1.22 nicm 1020: {
1.23 nicm 1021: u_int py, sy, line;
1.22 nicm 1022: int previous_wrapped;
1.23 nicm 1023: struct grid_line *src_gl;
1024:
1025: py = 0;
1026: sy = src->sy;
1.22 nicm 1027:
1.23 nicm 1028: previous_wrapped = 0;
1029: for (line = 0; line < sy + src->hsize; line++) {
1030: src_gl = src->linedata + line;
1.22 nicm 1031: if (!previous_wrapped) {
1.23 nicm 1032: /* Wasn't wrapped. If smaller, move to destination. */
1.58 nicm 1033: if (src_gl->cellused <= new_x)
1.23 nicm 1034: grid_reflow_move(dst, &py, src_gl);
1035: else
1036: grid_reflow_split(dst, &py, src_gl, new_x, 0);
1037: } else {
1038: /* Previous was wrapped. Try to join. */
1039: grid_reflow_join(dst, &py, src_gl, new_x);
1.22 nicm 1040: }
1.48 nicm 1041: previous_wrapped = (src_gl->flags & GRID_LINE_WRAPPED);
1.55 nicm 1042:
1043: /* This is where we started scrolling. */
1044: if (line == sy + src->hsize - src->hscrolled - 1)
1045: dst->hscrolled = 0;
1.22 nicm 1046: }
1047:
1.23 nicm 1048: grid_destroy(src);
1049:
1050: if (py > sy)
1.22 nicm 1051: return (0);
1.23 nicm 1052: return (sy - py);
1.1 nicm 1053: }