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