Annotation of src/usr.bin/tmux/screen.c, Revision 1.57
1.57 ! nicm 1: /* $OpenBSD: screen.c,v 1.56 2019/11/15 11:16:53 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. */
1.57 ! nicm 155: int
1.1 nicm 156: screen_set_title(struct screen *s, const char *title)
157: {
1.57 ! nicm 158: char *cp;
! 159:
! 160: if (!utf8_isvalid(title))
! 161: return (0);
1.23 nicm 162: free(s->title);
1.57 ! nicm 163: s->title = xstrdup(title);
! 164: return (1);
1.56 nicm 165: }
166:
167: /* Set screen path. */
168: void
169: screen_set_path(struct screen *s, const char *path)
170: {
171: free(s->path);
172: utf8_stravis(&s->path, path, VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL);
1.48 nicm 173: }
174:
175: /* Push the current title onto the stack. */
176: void
177: screen_push_title(struct screen *s)
178: {
179: struct screen_title_entry *title_entry;
180:
181: if (s->titles == NULL) {
182: s->titles = xmalloc(sizeof *s->titles);
183: TAILQ_INIT(s->titles);
184: }
185: title_entry = xmalloc(sizeof *title_entry);
186: title_entry->text = xstrdup(s->title);
187: TAILQ_INSERT_HEAD(s->titles, title_entry, entry);
188: }
189:
190: /*
191: * Pop a title from the stack and set it as the screen title. If the stack is
192: * empty, do nothing.
193: */
194: void
195: screen_pop_title(struct screen *s)
196: {
197: struct screen_title_entry *title_entry;
198:
199: if (s->titles == NULL)
200: return;
201:
202: title_entry = TAILQ_FIRST(s->titles);
203: if (title_entry != NULL) {
204: screen_set_title(s, title_entry->text);
205:
206: TAILQ_REMOVE(s->titles, title_entry, entry);
207: free(title_entry->text);
208: free(title_entry);
209: }
1.1 nicm 210: }
211:
212: /* Resize screen. */
213: void
1.24 nicm 214: screen_resize(struct screen *s, u_int sx, u_int sy, int reflow)
1.1 nicm 215: {
216: if (sx < 1)
217: sx = 1;
218: if (sy < 1)
219: sy = 1;
220:
1.3 nicm 221: if (sx != screen_size_x(s)) {
1.54 nicm 222: s->grid->sx = sx;
1.3 nicm 223: screen_reset_tabs(s);
1.49 nicm 224: } else
225: reflow = 0;
1.3 nicm 226:
1.1 nicm 227: if (sy != screen_size_y(s))
228: screen_resize_y(s, sy);
1.24 nicm 229:
230: if (reflow)
231: screen_reflow(s, sx);
1.1 nicm 232: }
233:
1.41 nicm 234: static void
1.1 nicm 235: screen_resize_y(struct screen *s, u_int sy)
236: {
237: struct grid *gd = s->grid;
1.4 nicm 238: u_int needed, available, oldy, i;
1.1 nicm 239:
240: if (sy == 0)
241: fatalx("zero size");
1.4 nicm 242: oldy = screen_size_y(s);
243:
1.12 nicm 244: /*
1.4 nicm 245: * When resizing:
246: *
247: * If the height is decreasing, delete lines from the bottom until
248: * hitting the cursor, then push lines from the top into the history.
1.12 nicm 249: *
1.40 nicm 250: * When increasing, pull as many lines as possible from scrolled
251: * history (not explicitly cleared from view) to the top, then fill the
252: * remaining with blanks at the bottom.
1.4 nicm 253: */
1.1 nicm 254:
255: /* Size decreasing. */
1.4 nicm 256: if (sy < oldy) {
257: needed = oldy - sy;
1.1 nicm 258:
1.4 nicm 259: /* Delete as many lines as possible from the bottom. */
260: available = oldy - 1 - s->cy;
261: if (available > 0) {
262: if (available > needed)
263: available = needed;
1.44 nicm 264: grid_view_delete_lines(gd, oldy - available, available,
265: 8);
1.1 nicm 266: }
1.4 nicm 267: needed -= available;
1.1 nicm 268:
1.4 nicm 269: /*
1.8 nicm 270: * Now just increase the history size, if possible, to take
271: * over the lines which are left. If history is off, delete
272: * lines from the top.
1.4 nicm 273: */
1.8 nicm 274: available = s->cy;
1.40 nicm 275: if (gd->flags & GRID_HISTORY) {
276: gd->hscrolled += needed;
1.8 nicm 277: gd->hsize += needed;
1.40 nicm 278: } else if (needed > 0 && available > 0) {
1.8 nicm 279: if (available > needed)
280: available = needed;
1.44 nicm 281: grid_view_delete_lines(gd, 0, available, 8);
1.8 nicm 282: }
1.4 nicm 283: s->cy -= needed;
1.12 nicm 284: }
1.1 nicm 285:
1.51 nicm 286: /* Resize line array. */
287: grid_adjust_lines(gd, gd->hsize + sy);
1.1 nicm 288:
289: /* Size increasing. */
1.4 nicm 290: if (sy > oldy) {
291: needed = sy - oldy;
292:
1.8 nicm 293: /*
1.41 nicm 294: * Try to pull as much as possible out of scrolled history, if
295: * is is enabled.
1.8 nicm 296: */
1.40 nicm 297: available = gd->hscrolled;
1.8 nicm 298: if (gd->flags & GRID_HISTORY && available > 0) {
1.4 nicm 299: if (available > needed)
300: available = needed;
1.40 nicm 301: gd->hscrolled -= available;
1.4 nicm 302: gd->hsize -= available;
303: s->cy += available;
1.8 nicm 304: } else
305: available = 0;
1.4 nicm 306: needed -= available;
307:
308: /* Then fill the rest in with blanks. */
1.11 nicm 309: for (i = gd->hsize + sy - needed; i < gd->hsize + sy; i++)
1.51 nicm 310: memset(grid_get_line(gd, i), 0, sizeof(struct grid_line));
1.1 nicm 311: }
312:
1.4 nicm 313: /* Set the new size, and reset the scroll region. */
1.1 nicm 314: gd->sy = sy;
315: s->rupper = 0;
316: s->rlower = screen_size_y(s) - 1;
317: }
318:
319: /* Set selection. */
320: void
1.14 nicm 321: screen_set_selection(struct screen *s, u_int sx, u_int sy,
1.52 nicm 322: u_int ex, u_int ey, u_int rectangle, int modekeys, struct grid_cell *gc)
1.1 nicm 323: {
1.52 nicm 324: if (s->sel == NULL)
325: s->sel = xcalloc(1, sizeof *s->sel);
1.1 nicm 326:
1.52 nicm 327: memcpy(&s->sel->cell, gc, sizeof s->sel->cell);
328: s->sel->hidden = 0;
329: s->sel->rectangle = rectangle;
330: s->sel->modekeys = modekeys;
331:
332: s->sel->sx = sx;
333: s->sel->sy = sy;
334: s->sel->ex = ex;
335: s->sel->ey = ey;
1.1 nicm 336: }
337:
338: /* Clear selection. */
339: void
340: screen_clear_selection(struct screen *s)
341: {
1.52 nicm 342: free(s->sel);
343: s->sel = NULL;
1.1 nicm 344: }
345:
1.45 nicm 346: /* Hide selection. */
347: void
348: screen_hide_selection(struct screen *s)
349: {
1.52 nicm 350: if (s->sel != NULL)
351: s->sel->hidden = 1;
1.45 nicm 352: }
353:
1.1 nicm 354: /* Check if cell in selection. */
355: int
356: screen_check_selection(struct screen *s, u_int px, u_int py)
357: {
1.52 nicm 358: struct screen_sel *sel = s->sel;
1.28 nicm 359: u_int xx;
1.1 nicm 360:
1.52 nicm 361: if (sel == NULL || sel->hidden)
1.1 nicm 362: return (0);
363:
1.52 nicm 364: if (sel->rectangle) {
1.14 nicm 365: if (sel->sy < sel->ey) {
366: /* start line < end line -- downward selection. */
367: if (py < sel->sy || py > sel->ey)
368: return (0);
369: } else if (sel->sy > sel->ey) {
370: /* start line > end line -- upward selection. */
371: if (py > sel->sy || py < sel->ey)
372: return (0);
373: } else {
374: /* starting line == ending line. */
375: if (py != sel->sy)
376: return (0);
377: }
378:
379: /*
380: * Need to include the selection start row, but not the cursor
381: * row, which means the selection changes depending on which
382: * one is on the left.
383: */
384: if (sel->ex < sel->sx) {
385: /* Cursor (ex) is on the left. */
1.17 nicm 386: if (px < sel->ex)
1.14 nicm 387: return (0);
388:
389: if (px > sel->sx)
390: return (0);
391: } else {
392: /* Selection start (sx) is on the left. */
393: if (px < sel->sx)
394: return (0);
395:
1.17 nicm 396: if (px > sel->ex)
1.14 nicm 397: return (0);
398: }
399: } else {
400: /*
401: * Like emacs, keep the top-left-most character, and drop the
402: * bottom-right-most, regardless of copy direction.
403: */
404: if (sel->sy < sel->ey) {
405: /* starting line < ending line -- downward selection. */
406: if (py < sel->sy || py > sel->ey)
407: return (0);
408:
1.28 nicm 409: if (py == sel->sy && px < sel->sx)
1.29 nicm 410: return (0);
1.28 nicm 411:
1.53 nicm 412: if (sel->modekeys == MODEKEY_EMACS)
413: xx = (sel->ex == 0 ? 0 : sel->ex - 1);
414: else
415: xx = sel->ex;
416: if (py == sel->ey && px > xx)
1.14 nicm 417: return (0);
418: } else if (sel->sy > sel->ey) {
419: /* starting line > ending line -- upward selection. */
420: if (py > sel->sy || py < sel->ey)
421: return (0);
422:
1.28 nicm 423: if (py == sel->ey && px < sel->ex)
424: return (0);
425:
426: if (sel->modekeys == MODEKEY_EMACS)
427: xx = sel->sx - 1;
428: else
429: xx = sel->sx;
1.46 nicm 430: if (py == sel->sy && (sel->sx == 0 || px > xx))
1.14 nicm 431: return (0);
432: } else {
433: /* starting line == ending line. */
434: if (py != sel->sy)
435: return (0);
436:
437: if (sel->ex < sel->sx) {
438: /* cursor (ex) is on the left */
1.28 nicm 439: if (sel->modekeys == MODEKEY_EMACS)
440: xx = sel->sx - 1;
441: else
442: xx = sel->sx;
443: if (px > xx || px < sel->ex)
1.14 nicm 444: return (0);
445: } else {
446: /* selection start (sx) is on the left */
1.53 nicm 447: if (sel->modekeys == MODEKEY_EMACS)
448: xx = (sel->ex == 0 ? 0 : sel->ex - 1);
449: else
450: xx = sel->ex;
451: if (px < sel->sx || px > xx)
1.14 nicm 452: return (0);
453: }
454: }
1.1 nicm 455: }
456:
457: return (1);
1.43 nicm 458: }
459:
460: /* Get selected grid cell. */
461: void
462: screen_select_cell(struct screen *s, struct grid_cell *dst,
463: const struct grid_cell *src)
464: {
1.52 nicm 465: if (s->sel == NULL || s->sel->hidden)
1.43 nicm 466: return;
467:
1.52 nicm 468: memcpy(dst, &s->sel->cell, sizeof *dst);
1.43 nicm 469:
470: utf8_copy(&dst->data, &src->data);
471: dst->attr = dst->attr & ~GRID_ATTR_CHARSET;
472: dst->attr |= src->attr & GRID_ATTR_CHARSET;
473: dst->flags = src->flags;
1.24 nicm 474: }
475:
476: /* Reflow wrapped lines. */
1.42 nicm 477: static void
1.25 nicm 478: screen_reflow(struct screen *s, u_int new_x)
1.24 nicm 479: {
1.55 nicm 480: u_int cx = s->cx, cy = s->grid->hsize + s->cy, wx, wy;
1.54 nicm 481: struct timeval start, tv;
482:
483: gettimeofday(&start, NULL);
484:
1.55 nicm 485: grid_wrap_position(s->grid, cx, cy, &wx, &wy);
486: log_debug("%s: cursor %u,%u is %u,%u", __func__, cx, cy, wx, wy);
1.54 nicm 487:
488: grid_reflow(s->grid, new_x);
489:
1.55 nicm 490: grid_unwrap_position(s->grid, &cx, &cy, wx, wy);
1.54 nicm 491: log_debug("%s: new cursor is %u,%u", __func__, cx, cy);
492:
493: if (cy >= s->grid->hsize) {
494: s->cx = cx;
495: s->cy = cy - s->grid->hsize;
496: } else {
497: s->cx = 0;
498: s->cy = 0;
499: }
500:
501: gettimeofday(&tv, NULL);
502: timersub(&tv, &start, &tv);
503:
504: log_debug("%s: reflow took %llu.%06u seconds", __func__,
505: (unsigned long long)tv.tv_sec, (u_int)tv.tv_usec);
1.1 nicm 506: }