Merge branch 'obsd-master'
[tmux.git] / screen.c
blob55eca09d030da90572d09e340ec6f3f9acc7c50a
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
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.
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.
19 #include <sys/types.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
25 #include "tmux.h"
27 /* Selected area in screen. */
28 struct screen_sel {
29 int hidden;
30 int rectangle;
31 int modekeys;
33 u_int sx;
34 u_int sy;
36 u_int ex;
37 u_int ey;
39 struct grid_cell cell;
42 /* Entry on title stack. */
43 struct screen_title_entry {
44 char *text;
46 TAILQ_ENTRY(screen_title_entry) entry;
48 TAILQ_HEAD(screen_titles, screen_title_entry);
50 static void screen_resize_y(struct screen *, u_int, int, u_int *);
51 static void screen_reflow(struct screen *, u_int, u_int *, u_int *, int);
53 /* Free titles stack. */
54 static void
55 screen_free_titles(struct screen *s)
57 struct screen_title_entry *title_entry;
59 if (s->titles == NULL)
60 return;
62 while ((title_entry = TAILQ_FIRST(s->titles)) != NULL) {
63 TAILQ_REMOVE(s->titles, title_entry, entry);
64 free(title_entry->text);
65 free(title_entry);
68 free(s->titles);
69 s->titles = NULL;
72 /* Create a new screen. */
73 void
74 screen_init(struct screen *s, u_int sx, u_int sy, u_int hlimit)
76 s->grid = grid_create(sx, sy, hlimit);
77 s->saved_grid = NULL;
79 s->title = xstrdup("");
80 s->titles = NULL;
81 s->path = NULL;
83 s->cstyle = SCREEN_CURSOR_DEFAULT;
84 s->default_cstyle = SCREEN_CURSOR_DEFAULT;
85 s->default_mode = 0;
86 s->ccolour = -1;
87 s->default_ccolour = -1;
88 s->tabs = NULL;
89 s->sel = NULL;
91 #ifdef ENABLE_SIXEL
92 TAILQ_INIT(&s->images);
93 #endif
95 s->write_list = NULL;
96 s->hyperlinks = NULL;
98 screen_reinit(s);
101 /* Reinitialise screen. */
102 void
103 screen_reinit(struct screen *s)
105 s->cx = 0;
106 s->cy = 0;
108 s->rupper = 0;
109 s->rlower = screen_size_y(s) - 1;
111 s->mode = MODE_CURSOR|MODE_WRAP|(s->mode & MODE_CRLF);
112 if (options_get_number(global_options, "extended-keys") == 2)
113 s->mode |= MODE_KEXTENDED;
115 if (s->saved_grid != NULL)
116 screen_alternate_off(s, NULL, 0);
117 s->saved_cx = UINT_MAX;
118 s->saved_cy = UINT_MAX;
120 screen_reset_tabs(s);
122 grid_clear_lines(s->grid, s->grid->hsize, s->grid->sy, 8);
124 screen_clear_selection(s);
125 screen_free_titles(s);
127 #ifdef ENABLE_SIXEL
128 image_free_all(s);
129 #endif
131 screen_reset_hyperlinks(s);
134 /* Reset hyperlinks of a screen. */
135 void
136 screen_reset_hyperlinks(struct screen *s)
138 if (s->hyperlinks == NULL)
139 s->hyperlinks = hyperlinks_init();
140 else
141 hyperlinks_reset(s->hyperlinks);
144 /* Destroy a screen. */
145 void
146 screen_free(struct screen *s)
148 free(s->sel);
149 free(s->tabs);
150 free(s->path);
151 free(s->title);
153 if (s->write_list != NULL)
154 screen_write_free_list(s);
156 if (s->saved_grid != NULL)
157 grid_destroy(s->saved_grid);
158 grid_destroy(s->grid);
160 if (s->hyperlinks != NULL)
161 hyperlinks_free(s->hyperlinks);
162 screen_free_titles(s);
164 #ifdef ENABLE_SIXEL
165 image_free_all(s);
166 #endif
169 /* Reset tabs to default, eight spaces apart. */
170 void
171 screen_reset_tabs(struct screen *s)
173 u_int i;
175 free(s->tabs);
177 if ((s->tabs = bit_alloc(screen_size_x(s))) == NULL)
178 fatal("bit_alloc failed");
179 for (i = 8; i < screen_size_x(s); i += 8)
180 bit_set(s->tabs, i);
183 /* Set screen cursor style and mode. */
184 void
185 screen_set_cursor_style(u_int style, enum screen_cursor_style *cstyle,
186 int *mode)
188 switch (style) {
189 case 0:
190 *cstyle = SCREEN_CURSOR_DEFAULT;
191 break;
192 case 1:
193 *cstyle = SCREEN_CURSOR_BLOCK;
194 *mode |= MODE_CURSOR_BLINKING;
195 break;
196 case 2:
197 *cstyle = SCREEN_CURSOR_BLOCK;
198 *mode &= ~MODE_CURSOR_BLINKING;
199 break;
200 case 3:
201 *cstyle = SCREEN_CURSOR_UNDERLINE;
202 *mode |= MODE_CURSOR_BLINKING;
203 break;
204 case 4:
205 *cstyle = SCREEN_CURSOR_UNDERLINE;
206 *mode &= ~MODE_CURSOR_BLINKING;
207 break;
208 case 5:
209 *cstyle = SCREEN_CURSOR_BAR;
210 *mode |= MODE_CURSOR_BLINKING;
211 break;
212 case 6:
213 *cstyle = SCREEN_CURSOR_BAR;
214 *mode &= ~MODE_CURSOR_BLINKING;
215 break;
219 /* Set screen cursor colour. */
220 void
221 screen_set_cursor_colour(struct screen *s, int colour)
223 s->ccolour = colour;
226 /* Set screen title. */
228 screen_set_title(struct screen *s, const char *title)
230 if (!utf8_isvalid(title))
231 return (0);
232 free(s->title);
233 s->title = xstrdup(title);
234 return (1);
237 /* Set screen path. */
238 void
239 screen_set_path(struct screen *s, const char *path)
241 free(s->path);
242 utf8_stravis(&s->path, path, VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL);
245 /* Push the current title onto the stack. */
246 void
247 screen_push_title(struct screen *s)
249 struct screen_title_entry *title_entry;
251 if (s->titles == NULL) {
252 s->titles = xmalloc(sizeof *s->titles);
253 TAILQ_INIT(s->titles);
255 title_entry = xmalloc(sizeof *title_entry);
256 title_entry->text = xstrdup(s->title);
257 TAILQ_INSERT_HEAD(s->titles, title_entry, entry);
261 * Pop a title from the stack and set it as the screen title. If the stack is
262 * empty, do nothing.
264 void
265 screen_pop_title(struct screen *s)
267 struct screen_title_entry *title_entry;
269 if (s->titles == NULL)
270 return;
272 title_entry = TAILQ_FIRST(s->titles);
273 if (title_entry != NULL) {
274 screen_set_title(s, title_entry->text);
276 TAILQ_REMOVE(s->titles, title_entry, entry);
277 free(title_entry->text);
278 free(title_entry);
282 /* Resize screen with options. */
283 void
284 screen_resize_cursor(struct screen *s, u_int sx, u_int sy, int reflow,
285 int eat_empty, int cursor)
287 u_int cx = s->cx, cy = s->grid->hsize + s->cy;
289 if (s->write_list != NULL)
290 screen_write_free_list(s);
292 log_debug("%s: new size %ux%u, now %ux%u (cursor %u,%u = %u,%u)",
293 __func__, sx, sy, screen_size_x(s), screen_size_y(s), s->cx, s->cy,
294 cx, cy);
296 if (sx < 1)
297 sx = 1;
298 if (sy < 1)
299 sy = 1;
301 if (sx != screen_size_x(s)) {
302 s->grid->sx = sx;
303 screen_reset_tabs(s);
304 } else
305 reflow = 0;
307 if (sy != screen_size_y(s))
308 screen_resize_y(s, sy, eat_empty, &cy);
310 if (reflow) {
311 #ifdef ENABLE_SIXEL
312 image_free_all(s);
313 #endif
314 screen_reflow(s, sx, &cx, &cy, cursor);
317 if (cy >= s->grid->hsize) {
318 s->cx = cx;
319 s->cy = cy - s->grid->hsize;
320 } else {
321 s->cx = 0;
322 s->cy = 0;
325 log_debug("%s: cursor finished at %u,%u = %u,%u", __func__, s->cx,
326 s->cy, cx, cy);
328 if (s->write_list != NULL)
329 screen_write_make_list(s);
332 /* Resize screen. */
333 void
334 screen_resize(struct screen *s, u_int sx, u_int sy, int reflow)
336 screen_resize_cursor(s, sx, sy, reflow, 1, 1);
339 static void
340 screen_resize_y(struct screen *s, u_int sy, int eat_empty, u_int *cy)
342 struct grid *gd = s->grid;
343 u_int needed, available, oldy, i;
345 if (sy == 0)
346 fatalx("zero size");
347 oldy = screen_size_y(s);
350 * When resizing:
352 * If the height is decreasing, delete lines from the bottom until
353 * hitting the cursor, then push lines from the top into the history.
355 * When increasing, pull as many lines as possible from scrolled
356 * history (not explicitly cleared from view) to the top, then fill the
357 * remaining with blanks at the bottom.
360 /* Size decreasing. */
361 if (sy < oldy) {
362 needed = oldy - sy;
364 /* Delete as many lines as possible from the bottom. */
365 if (eat_empty) {
366 available = oldy - 1 - s->cy;
367 if (available > 0) {
368 if (available > needed)
369 available = needed;
370 grid_view_delete_lines(gd, oldy - available,
371 available, 8);
373 needed -= available;
377 * Now just increase the history size, if possible, to take
378 * over the lines which are left. If history is off, delete
379 * lines from the top.
381 available = s->cy;
382 if (gd->flags & GRID_HISTORY) {
383 gd->hscrolled += needed;
384 gd->hsize += needed;
385 } else if (needed > 0 && available > 0) {
386 if (available > needed)
387 available = needed;
388 grid_view_delete_lines(gd, 0, available, 8);
389 (*cy) -= available;
393 /* Resize line array. */
394 grid_adjust_lines(gd, gd->hsize + sy);
396 /* Size increasing. */
397 if (sy > oldy) {
398 needed = sy - oldy;
401 * Try to pull as much as possible out of scrolled history, if
402 * is is enabled.
404 available = gd->hscrolled;
405 if (gd->flags & GRID_HISTORY && available > 0) {
406 if (available > needed)
407 available = needed;
408 gd->hscrolled -= available;
409 gd->hsize -= available;
410 } else
411 available = 0;
412 needed -= available;
414 /* Then fill the rest in with blanks. */
415 for (i = gd->hsize + sy - needed; i < gd->hsize + sy; i++)
416 grid_empty_line(gd, i, 8);
419 /* Set the new size, and reset the scroll region. */
420 gd->sy = sy;
421 s->rupper = 0;
422 s->rlower = screen_size_y(s) - 1;
425 /* Set selection. */
426 void
427 screen_set_selection(struct screen *s, u_int sx, u_int sy,
428 u_int ex, u_int ey, u_int rectangle, int modekeys, struct grid_cell *gc)
430 if (s->sel == NULL)
431 s->sel = xcalloc(1, sizeof *s->sel);
433 memcpy(&s->sel->cell, gc, sizeof s->sel->cell);
434 s->sel->hidden = 0;
435 s->sel->rectangle = rectangle;
436 s->sel->modekeys = modekeys;
438 s->sel->sx = sx;
439 s->sel->sy = sy;
440 s->sel->ex = ex;
441 s->sel->ey = ey;
444 /* Clear selection. */
445 void
446 screen_clear_selection(struct screen *s)
448 free(s->sel);
449 s->sel = NULL;
452 /* Hide selection. */
453 void
454 screen_hide_selection(struct screen *s)
456 if (s->sel != NULL)
457 s->sel->hidden = 1;
460 /* Check if cell in selection. */
462 screen_check_selection(struct screen *s, u_int px, u_int py)
464 struct screen_sel *sel = s->sel;
465 u_int xx;
467 if (sel == NULL || sel->hidden)
468 return (0);
470 if (sel->rectangle) {
471 if (sel->sy < sel->ey) {
472 /* start line < end line -- downward selection. */
473 if (py < sel->sy || py > sel->ey)
474 return (0);
475 } else if (sel->sy > sel->ey) {
476 /* start line > end line -- upward selection. */
477 if (py > sel->sy || py < sel->ey)
478 return (0);
479 } else {
480 /* starting line == ending line. */
481 if (py != sel->sy)
482 return (0);
486 * Need to include the selection start row, but not the cursor
487 * row, which means the selection changes depending on which
488 * one is on the left.
490 if (sel->ex < sel->sx) {
491 /* Cursor (ex) is on the left. */
492 if (px < sel->ex)
493 return (0);
495 if (px > sel->sx)
496 return (0);
497 } else {
498 /* Selection start (sx) is on the left. */
499 if (px < sel->sx)
500 return (0);
502 if (px > sel->ex)
503 return (0);
505 } else {
507 * Like emacs, keep the top-left-most character, and drop the
508 * bottom-right-most, regardless of copy direction.
510 if (sel->sy < sel->ey) {
511 /* starting line < ending line -- downward selection. */
512 if (py < sel->sy || py > sel->ey)
513 return (0);
515 if (py == sel->sy && px < sel->sx)
516 return (0);
518 if (sel->modekeys == MODEKEY_EMACS)
519 xx = (sel->ex == 0 ? 0 : sel->ex - 1);
520 else
521 xx = sel->ex;
522 if (py == sel->ey && px > xx)
523 return (0);
524 } else if (sel->sy > sel->ey) {
525 /* starting line > ending line -- upward selection. */
526 if (py > sel->sy || py < sel->ey)
527 return (0);
529 if (py == sel->ey && px < sel->ex)
530 return (0);
532 if (sel->modekeys == MODEKEY_EMACS)
533 xx = sel->sx - 1;
534 else
535 xx = sel->sx;
536 if (py == sel->sy && (sel->sx == 0 || px > xx))
537 return (0);
538 } else {
539 /* starting line == ending line. */
540 if (py != sel->sy)
541 return (0);
543 if (sel->ex < sel->sx) {
544 /* cursor (ex) is on the left */
545 if (sel->modekeys == MODEKEY_EMACS)
546 xx = sel->sx - 1;
547 else
548 xx = sel->sx;
549 if (px > xx || px < sel->ex)
550 return (0);
551 } else {
552 /* selection start (sx) is on the left */
553 if (sel->modekeys == MODEKEY_EMACS)
554 xx = (sel->ex == 0 ? 0 : sel->ex - 1);
555 else
556 xx = sel->ex;
557 if (px < sel->sx || px > xx)
558 return (0);
563 return (1);
566 /* Get selected grid cell. */
567 void
568 screen_select_cell(struct screen *s, struct grid_cell *dst,
569 const struct grid_cell *src)
571 if (s->sel == NULL || s->sel->hidden)
572 return;
574 memcpy(dst, &s->sel->cell, sizeof *dst);
576 utf8_copy(&dst->data, &src->data);
577 dst->attr = dst->attr & ~GRID_ATTR_CHARSET;
578 dst->attr |= src->attr & GRID_ATTR_CHARSET;
579 dst->flags = src->flags;
582 /* Reflow wrapped lines. */
583 static void
584 screen_reflow(struct screen *s, u_int new_x, u_int *cx, u_int *cy, int cursor)
586 u_int wx, wy;
588 if (cursor) {
589 grid_wrap_position(s->grid, *cx, *cy, &wx, &wy);
590 log_debug("%s: cursor %u,%u is %u,%u", __func__, *cx, *cy, wx,
591 wy);
594 grid_reflow(s->grid, new_x);
596 if (cursor) {
597 grid_unwrap_position(s->grid, cx, cy, wx, wy);
598 log_debug("%s: new cursor is %u,%u", __func__, *cx, *cy);
600 else {
601 *cx = 0;
602 *cy = s->grid->hsize;
607 * Enter alternative screen mode. A copy of the visible screen is saved and the
608 * history is not updated.
610 void
611 screen_alternate_on(struct screen *s, struct grid_cell *gc, int cursor)
613 u_int sx, sy;
615 if (s->saved_grid != NULL)
616 return;
617 sx = screen_size_x(s);
618 sy = screen_size_y(s);
620 s->saved_grid = grid_create(sx, sy, 0);
621 grid_duplicate_lines(s->saved_grid, 0, s->grid, screen_hsize(s), sy);
622 if (cursor) {
623 s->saved_cx = s->cx;
624 s->saved_cy = s->cy;
626 memcpy(&s->saved_cell, gc, sizeof s->saved_cell);
628 grid_view_clear(s->grid, 0, 0, sx, sy, 8);
630 s->saved_flags = s->grid->flags;
631 s->grid->flags &= ~GRID_HISTORY;
634 /* Exit alternate screen mode and restore the copied grid. */
635 void
636 screen_alternate_off(struct screen *s, struct grid_cell *gc, int cursor)
638 u_int sx = screen_size_x(s), sy = screen_size_y(s);
641 * If the current size is different, temporarily resize to the old size
642 * before copying back.
644 if (s->saved_grid != NULL)
645 screen_resize(s, s->saved_grid->sx, s->saved_grid->sy, 0);
648 * Restore the cursor position and cell. This happens even if not
649 * currently in the alternate screen.
651 if (cursor && s->saved_cx != UINT_MAX && s->saved_cy != UINT_MAX) {
652 s->cx = s->saved_cx;
653 s->cy = s->saved_cy;
654 if (gc != NULL)
655 memcpy(gc, &s->saved_cell, sizeof *gc);
658 /* If not in the alternate screen, do nothing more. */
659 if (s->saved_grid == NULL) {
660 if (s->cx > screen_size_x(s) - 1)
661 s->cx = screen_size_x(s) - 1;
662 if (s->cy > screen_size_y(s) - 1)
663 s->cy = screen_size_y(s) - 1;
664 return;
667 /* Restore the saved grid. */
668 grid_duplicate_lines(s->grid, screen_hsize(s), s->saved_grid, 0,
669 s->saved_grid->sy);
672 * Turn history back on (so resize can use it) and then resize back to
673 * the current size.
675 if (s->saved_flags & GRID_HISTORY)
676 s->grid->flags |= GRID_HISTORY;
677 screen_resize(s, sx, sy, 1);
679 grid_destroy(s->saved_grid);
680 s->saved_grid = NULL;
682 if (s->cx > screen_size_x(s) - 1)
683 s->cx = screen_size_x(s) - 1;
684 if (s->cy > screen_size_y(s) - 1)
685 s->cy = screen_size_y(s) - 1;
688 /* Get mode as a string. */
689 const char *
690 screen_mode_to_string(int mode)
692 static char tmp[1024];
694 if (mode == 0)
695 return ("NONE");
696 if (mode == ALL_MODES)
697 return ("ALL");
699 *tmp = '\0';
700 if (mode & MODE_CURSOR)
701 strlcat(tmp, "CURSOR,", sizeof tmp);
702 if (mode & MODE_INSERT)
703 strlcat(tmp, "INSERT,", sizeof tmp);
704 if (mode & MODE_KCURSOR)
705 strlcat(tmp, "KCURSOR,", sizeof tmp);
706 if (mode & MODE_KKEYPAD)
707 strlcat(tmp, "KKEYPAD,", sizeof tmp);
708 if (mode & MODE_WRAP)
709 strlcat(tmp, "WRAP,", sizeof tmp);
710 if (mode & MODE_MOUSE_STANDARD)
711 strlcat(tmp, "MOUSE_STANDARD,", sizeof tmp);
712 if (mode & MODE_MOUSE_BUTTON)
713 strlcat(tmp, "MOUSE_BUTTON,", sizeof tmp);
714 if (mode & MODE_CURSOR_BLINKING)
715 strlcat(tmp, "CURSOR_BLINKING,", sizeof tmp);
716 if (mode & MODE_CURSOR_VERY_VISIBLE)
717 strlcat(tmp, "CURSOR_VERY_VISIBLE,", sizeof tmp);
718 if (mode & MODE_MOUSE_UTF8)
719 strlcat(tmp, "MOUSE_UTF8,", sizeof tmp);
720 if (mode & MODE_MOUSE_SGR)
721 strlcat(tmp, "MOUSE_SGR,", sizeof tmp);
722 if (mode & MODE_BRACKETPASTE)
723 strlcat(tmp, "BRACKETPASTE,", sizeof tmp);
724 if (mode & MODE_FOCUSON)
725 strlcat(tmp, "FOCUSON,", sizeof tmp);
726 if (mode & MODE_MOUSE_ALL)
727 strlcat(tmp, "MOUSE_ALL,", sizeof tmp);
728 if (mode & MODE_ORIGIN)
729 strlcat(tmp, "ORIGIN,", sizeof tmp);
730 if (mode & MODE_CRLF)
731 strlcat(tmp, "CRLF,", sizeof tmp);
732 if (mode & MODE_KEXTENDED)
733 strlcat(tmp, "KEXTENDED,", sizeof tmp);
734 tmp[strlen(tmp) - 1] = '\0';
735 return (tmp);