Annotation of src/usr.bin/tmux/screen.c, Revision 1.55
1.55 ! nicm 1: /* $OpenBSD: screen.c,v 1.54 2019/03/20 19:19:11 nicm Exp $ */
1.1 nicm 2:
3: /*
1.37 nicm 4: * Copyright (c) 2007 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.3 nicm 21: #include <stdlib.h>
1.1 nicm 22: #include <string.h>
1.16 nicm 23: #include <unistd.h>
1.47 nicm 24: #include <vis.h>
1.1 nicm 25:
26: #include "tmux.h"
27:
1.52 nicm 28: /* Selected area in screen. */
29: struct screen_sel {
30: int hidden;
31: int rectangle;
32: int modekeys;
33:
34: u_int sx;
35: u_int sy;
36:
37: u_int ex;
38: u_int ey;
39:
40: struct grid_cell cell;
41: };
42:
43: /* Entry on title stack. */
1.48 nicm 44: struct screen_title_entry {
45: char *text;
46:
47: TAILQ_ENTRY(screen_title_entry) entry;
48: };
49: TAILQ_HEAD(screen_titles, screen_title_entry);
50:
1.41 nicm 51: static void screen_resize_y(struct screen *, u_int);
1.1 nicm 52:
1.42 nicm 53: static void screen_reflow(struct screen *, u_int);
54:
1.48 nicm 55: /* Free titles stack. */
56: static void
57: screen_free_titles(struct screen *s)
58: {
59: struct screen_title_entry *title_entry;
60:
61: if (s->titles == NULL)
62: return;
63:
64: while ((title_entry = TAILQ_FIRST(s->titles)) != NULL) {
65: TAILQ_REMOVE(s->titles, title_entry, entry);
66: free(title_entry->text);
67: free(title_entry);
68: }
69:
70: free(s->titles);
71: s->titles = NULL;
72: }
73:
1.1 nicm 74: /* Create a new screen. */
75: void
76: screen_init(struct screen *s, u_int sx, u_int sy, u_int hlimit)
77: {
78: s->grid = grid_create(sx, sy, hlimit);
1.34 nicm 79: s->title = xstrdup("");
1.48 nicm 80: s->titles = NULL;
1.1 nicm 81:
1.21 nicm 82: s->cstyle = 0;
1.20 nicm 83: s->ccolour = xstrdup("");
1.3 nicm 84: s->tabs = NULL;
1.52 nicm 85: s->sel = NULL;
1.3 nicm 86:
1.1 nicm 87: screen_reinit(s);
88: }
89:
90: /* Reinitialise screen. */
91: void
92: screen_reinit(struct screen *s)
93: {
94: s->cx = 0;
95: s->cy = 0;
96:
97: s->rupper = 0;
98: s->rlower = screen_size_y(s) - 1;
99:
1.16 nicm 100: s->mode = MODE_CURSOR | MODE_WRAP;
1.12 nicm 101:
1.3 nicm 102: screen_reset_tabs(s);
1.1 nicm 103:
1.44 nicm 104: grid_clear_lines(s->grid, s->grid->hsize, s->grid->sy, 8);
1.1 nicm 105:
106: screen_clear_selection(s);
1.48 nicm 107: screen_free_titles(s);
1.1 nicm 108: }
109:
110: /* Destroy a screen. */
111: void
112: screen_free(struct screen *s)
113: {
1.52 nicm 114: free(s->sel);
1.23 nicm 115: free(s->tabs);
116: free(s->title);
117: free(s->ccolour);
1.48 nicm 118:
1.1 nicm 119: grid_destroy(s->grid);
1.48 nicm 120:
121: screen_free_titles(s);
1.1 nicm 122: }
123:
1.3 nicm 124: /* Reset tabs to default, eight spaces apart. */
125: void
126: screen_reset_tabs(struct screen *s)
127: {
128: u_int i;
129:
1.23 nicm 130: free(s->tabs);
1.3 nicm 131:
132: if ((s->tabs = bit_alloc(screen_size_x(s))) == NULL)
133: fatal("bit_alloc failed");
134: for (i = 8; i < screen_size_x(s); i += 8)
135: bit_set(s->tabs, i);
1.21 nicm 136: }
137:
138: /* Set screen cursor style. */
139: void
140: screen_set_cursor_style(struct screen *s, u_int style)
141: {
1.22 nicm 142: if (style <= 6)
1.21 nicm 143: s->cstyle = style;
1.20 nicm 144: }
145:
146: /* Set screen cursor colour. */
147: void
1.36 nicm 148: screen_set_cursor_colour(struct screen *s, const char *colour)
1.20 nicm 149: {
1.23 nicm 150: free(s->ccolour);
1.36 nicm 151: s->ccolour = xstrdup(colour);
1.3 nicm 152: }
153:
1.1 nicm 154: /* Set screen title. */
155: void
156: screen_set_title(struct screen *s, const char *title)
157: {
1.23 nicm 158: free(s->title);
1.47 nicm 159: utf8_stravis(&s->title, title, VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL);
1.48 nicm 160: }
161:
162: /* Push the current title onto the stack. */
163: void
164: screen_push_title(struct screen *s)
165: {
166: struct screen_title_entry *title_entry;
167:
168: if (s->titles == NULL) {
169: s->titles = xmalloc(sizeof *s->titles);
170: TAILQ_INIT(s->titles);
171: }
172: title_entry = xmalloc(sizeof *title_entry);
173: title_entry->text = xstrdup(s->title);
174: TAILQ_INSERT_HEAD(s->titles, title_entry, entry);
175: }
176:
177: /*
178: * Pop a title from the stack and set it as the screen title. If the stack is
179: * empty, do nothing.
180: */
181: void
182: screen_pop_title(struct screen *s)
183: {
184: struct screen_title_entry *title_entry;
185:
186: if (s->titles == NULL)
187: return;
188:
189: title_entry = TAILQ_FIRST(s->titles);
190: if (title_entry != NULL) {
191: screen_set_title(s, title_entry->text);
192:
193: TAILQ_REMOVE(s->titles, title_entry, entry);
194: free(title_entry->text);
195: free(title_entry);
196: }
1.1 nicm 197: }
198:
199: /* Resize screen. */
200: void
1.24 nicm 201: screen_resize(struct screen *s, u_int sx, u_int sy, int reflow)
1.1 nicm 202: {
203: if (sx < 1)
204: sx = 1;
205: if (sy < 1)
206: sy = 1;
207:
1.3 nicm 208: if (sx != screen_size_x(s)) {
1.54 nicm 209: s->grid->sx = sx;
1.3 nicm 210: screen_reset_tabs(s);
1.49 nicm 211: } else
212: reflow = 0;
1.3 nicm 213:
1.1 nicm 214: if (sy != screen_size_y(s))
215: screen_resize_y(s, sy);
1.24 nicm 216:
217: if (reflow)
218: screen_reflow(s, sx);
1.1 nicm 219: }
220:
1.41 nicm 221: static void
1.1 nicm 222: screen_resize_y(struct screen *s, u_int sy)
223: {
224: struct grid *gd = s->grid;
1.4 nicm 225: u_int needed, available, oldy, i;
1.1 nicm 226:
227: if (sy == 0)
228: fatalx("zero size");
1.4 nicm 229: oldy = screen_size_y(s);
230:
1.12 nicm 231: /*
1.4 nicm 232: * When resizing:
233: *
234: * If the height is decreasing, delete lines from the bottom until
235: * hitting the cursor, then push lines from the top into the history.
1.12 nicm 236: *
1.40 nicm 237: * When increasing, pull as many lines as possible from scrolled
238: * history (not explicitly cleared from view) to the top, then fill the
239: * remaining with blanks at the bottom.
1.4 nicm 240: */
1.1 nicm 241:
242: /* Size decreasing. */
1.4 nicm 243: if (sy < oldy) {
244: needed = oldy - sy;
1.1 nicm 245:
1.4 nicm 246: /* Delete as many lines as possible from the bottom. */
247: available = oldy - 1 - s->cy;
248: if (available > 0) {
249: if (available > needed)
250: available = needed;
1.44 nicm 251: grid_view_delete_lines(gd, oldy - available, available,
252: 8);
1.1 nicm 253: }
1.4 nicm 254: needed -= available;
1.1 nicm 255:
1.4 nicm 256: /*
1.8 nicm 257: * Now just increase the history size, if possible, to take
258: * over the lines which are left. If history is off, delete
259: * lines from the top.
1.4 nicm 260: */
1.8 nicm 261: available = s->cy;
1.40 nicm 262: if (gd->flags & GRID_HISTORY) {
263: gd->hscrolled += needed;
1.8 nicm 264: gd->hsize += needed;
1.40 nicm 265: } else if (needed > 0 && available > 0) {
1.8 nicm 266: if (available > needed)
267: available = needed;
1.44 nicm 268: grid_view_delete_lines(gd, 0, available, 8);
1.8 nicm 269: }
1.4 nicm 270: s->cy -= needed;
1.12 nicm 271: }
1.1 nicm 272:
1.51 nicm 273: /* Resize line array. */
274: grid_adjust_lines(gd, gd->hsize + sy);
1.1 nicm 275:
276: /* Size increasing. */
1.4 nicm 277: if (sy > oldy) {
278: needed = sy - oldy;
279:
1.8 nicm 280: /*
1.41 nicm 281: * Try to pull as much as possible out of scrolled history, if
282: * is is enabled.
1.8 nicm 283: */
1.40 nicm 284: available = gd->hscrolled;
1.8 nicm 285: if (gd->flags & GRID_HISTORY && available > 0) {
1.4 nicm 286: if (available > needed)
287: available = needed;
1.40 nicm 288: gd->hscrolled -= available;
1.4 nicm 289: gd->hsize -= available;
290: s->cy += available;
1.8 nicm 291: } else
292: available = 0;
1.4 nicm 293: needed -= available;
294:
295: /* Then fill the rest in with blanks. */
1.11 nicm 296: for (i = gd->hsize + sy - needed; i < gd->hsize + sy; i++)
1.51 nicm 297: memset(grid_get_line(gd, i), 0, sizeof(struct grid_line));
1.1 nicm 298: }
299:
1.4 nicm 300: /* Set the new size, and reset the scroll region. */
1.1 nicm 301: gd->sy = sy;
302: s->rupper = 0;
303: s->rlower = screen_size_y(s) - 1;
304: }
305:
306: /* Set selection. */
307: void
1.14 nicm 308: screen_set_selection(struct screen *s, u_int sx, u_int sy,
1.52 nicm 309: u_int ex, u_int ey, u_int rectangle, int modekeys, struct grid_cell *gc)
1.1 nicm 310: {
1.52 nicm 311: if (s->sel == NULL)
312: s->sel = xcalloc(1, sizeof *s->sel);
1.1 nicm 313:
1.52 nicm 314: memcpy(&s->sel->cell, gc, sizeof s->sel->cell);
315: s->sel->hidden = 0;
316: s->sel->rectangle = rectangle;
317: s->sel->modekeys = modekeys;
318:
319: s->sel->sx = sx;
320: s->sel->sy = sy;
321: s->sel->ex = ex;
322: s->sel->ey = ey;
1.1 nicm 323: }
324:
325: /* Clear selection. */
326: void
327: screen_clear_selection(struct screen *s)
328: {
1.52 nicm 329: free(s->sel);
330: s->sel = NULL;
1.1 nicm 331: }
332:
1.45 nicm 333: /* Hide selection. */
334: void
335: screen_hide_selection(struct screen *s)
336: {
1.52 nicm 337: if (s->sel != NULL)
338: s->sel->hidden = 1;
1.45 nicm 339: }
340:
1.1 nicm 341: /* Check if cell in selection. */
342: int
343: screen_check_selection(struct screen *s, u_int px, u_int py)
344: {
1.52 nicm 345: struct screen_sel *sel = s->sel;
1.28 nicm 346: u_int xx;
1.1 nicm 347:
1.52 nicm 348: if (sel == NULL || sel->hidden)
1.1 nicm 349: return (0);
350:
1.52 nicm 351: if (sel->rectangle) {
1.14 nicm 352: if (sel->sy < sel->ey) {
353: /* start line < end line -- downward selection. */
354: if (py < sel->sy || py > sel->ey)
355: return (0);
356: } else if (sel->sy > sel->ey) {
357: /* start line > end line -- upward selection. */
358: if (py > sel->sy || py < sel->ey)
359: return (0);
360: } else {
361: /* starting line == ending line. */
362: if (py != sel->sy)
363: return (0);
364: }
365:
366: /*
367: * Need to include the selection start row, but not the cursor
368: * row, which means the selection changes depending on which
369: * one is on the left.
370: */
371: if (sel->ex < sel->sx) {
372: /* Cursor (ex) is on the left. */
1.17 nicm 373: if (px < sel->ex)
1.14 nicm 374: return (0);
375:
376: if (px > sel->sx)
377: return (0);
378: } else {
379: /* Selection start (sx) is on the left. */
380: if (px < sel->sx)
381: return (0);
382:
1.17 nicm 383: if (px > sel->ex)
1.14 nicm 384: return (0);
385: }
386: } else {
387: /*
388: * Like emacs, keep the top-left-most character, and drop the
389: * bottom-right-most, regardless of copy direction.
390: */
391: if (sel->sy < sel->ey) {
392: /* starting line < ending line -- downward selection. */
393: if (py < sel->sy || py > sel->ey)
394: return (0);
395:
1.28 nicm 396: if (py == sel->sy && px < sel->sx)
1.29 nicm 397: return (0);
1.28 nicm 398:
1.53 nicm 399: if (sel->modekeys == MODEKEY_EMACS)
400: xx = (sel->ex == 0 ? 0 : sel->ex - 1);
401: else
402: xx = sel->ex;
403: if (py == sel->ey && px > xx)
1.14 nicm 404: return (0);
405: } else if (sel->sy > sel->ey) {
406: /* starting line > ending line -- upward selection. */
407: if (py > sel->sy || py < sel->ey)
408: return (0);
409:
1.28 nicm 410: if (py == sel->ey && px < sel->ex)
411: return (0);
412:
413: if (sel->modekeys == MODEKEY_EMACS)
414: xx = sel->sx - 1;
415: else
416: xx = sel->sx;
1.46 nicm 417: if (py == sel->sy && (sel->sx == 0 || px > xx))
1.14 nicm 418: return (0);
419: } else {
420: /* starting line == ending line. */
421: if (py != sel->sy)
422: return (0);
423:
424: if (sel->ex < sel->sx) {
425: /* cursor (ex) is on the left */
1.28 nicm 426: if (sel->modekeys == MODEKEY_EMACS)
427: xx = sel->sx - 1;
428: else
429: xx = sel->sx;
430: if (px > xx || px < sel->ex)
1.14 nicm 431: return (0);
432: } else {
433: /* selection start (sx) is on the left */
1.53 nicm 434: if (sel->modekeys == MODEKEY_EMACS)
435: xx = (sel->ex == 0 ? 0 : sel->ex - 1);
436: else
437: xx = sel->ex;
438: if (px < sel->sx || px > xx)
1.14 nicm 439: return (0);
440: }
441: }
1.1 nicm 442: }
443:
444: return (1);
1.43 nicm 445: }
446:
447: /* Get selected grid cell. */
448: void
449: screen_select_cell(struct screen *s, struct grid_cell *dst,
450: const struct grid_cell *src)
451: {
1.52 nicm 452: if (s->sel == NULL || s->sel->hidden)
1.43 nicm 453: return;
454:
1.52 nicm 455: memcpy(dst, &s->sel->cell, sizeof *dst);
1.43 nicm 456:
457: utf8_copy(&dst->data, &src->data);
458: dst->attr = dst->attr & ~GRID_ATTR_CHARSET;
459: dst->attr |= src->attr & GRID_ATTR_CHARSET;
460: dst->flags = src->flags;
1.24 nicm 461: }
462:
463: /* Reflow wrapped lines. */
1.42 nicm 464: static void
1.25 nicm 465: screen_reflow(struct screen *s, u_int new_x)
1.24 nicm 466: {
1.55 ! nicm 467: u_int cx = s->cx, cy = s->grid->hsize + s->cy, wx, wy;
1.54 nicm 468: struct timeval start, tv;
469:
470: gettimeofday(&start, NULL);
471:
1.55 ! nicm 472: grid_wrap_position(s->grid, cx, cy, &wx, &wy);
! 473: log_debug("%s: cursor %u,%u is %u,%u", __func__, cx, cy, wx, wy);
1.54 nicm 474:
475: grid_reflow(s->grid, new_x);
476:
1.55 ! nicm 477: grid_unwrap_position(s->grid, &cx, &cy, wx, wy);
1.54 nicm 478: log_debug("%s: new cursor is %u,%u", __func__, cx, cy);
479:
480: if (cy >= s->grid->hsize) {
481: s->cx = cx;
482: s->cy = cy - s->grid->hsize;
483: } else {
484: s->cx = 0;
485: s->cy = 0;
486: }
487:
488: gettimeofday(&tv, NULL);
489: timersub(&tv, &start, &tv);
490:
491: log_debug("%s: reflow took %llu.%06u seconds", __func__,
492: (unsigned long long)tv.tv_sec, (u_int)tv.tv_usec);
1.1 nicm 493: }