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