Merge branch 'obsd-master'
[tmux.git] / screen.c
blobbfa255ec01b52768265310c7958867bfc7446c1c
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->mode = MODE_CURSOR;
86 s->default_mode = 0;
87 s->ccolour = -1;
88 s->default_ccolour = -1;
89 s->tabs = NULL;
90 s->sel = NULL;
92 #ifdef ENABLE_SIXEL
93 TAILQ_INIT(&s->images);
94 #endif
96 s->write_list = NULL;
97 s->hyperlinks = NULL;
99 screen_reinit(s);
102 /* Reinitialise screen. */
103 void
104 screen_reinit(struct screen *s)
106 s->cx = 0;
107 s->cy = 0;
109 s->rupper = 0;
110 s->rlower = screen_size_y(s) - 1;
112 s->mode = MODE_CURSOR|MODE_WRAP|(s->mode & MODE_CRLF);
114 if (options_get_number(global_options, "extended-keys") == 2)
115 s->mode = (s->mode & ~EXTENDED_KEY_MODES)|MODE_KEYS_EXTENDED;
117 if (SCREEN_IS_ALTERNATE(s))
118 screen_alternate_off(s, NULL, 0);
119 s->saved_cx = UINT_MAX;
120 s->saved_cy = UINT_MAX;
122 screen_reset_tabs(s);
124 grid_clear_lines(s->grid, s->grid->hsize, s->grid->sy, 8);
126 screen_clear_selection(s);
127 screen_free_titles(s);
129 #ifdef ENABLE_SIXEL
130 image_free_all(s);
131 #endif
133 screen_reset_hyperlinks(s);
136 /* Reset hyperlinks of a screen. */
137 void
138 screen_reset_hyperlinks(struct screen *s)
140 if (s->hyperlinks == NULL)
141 s->hyperlinks = hyperlinks_init();
142 else
143 hyperlinks_reset(s->hyperlinks);
146 /* Destroy a screen. */
147 void
148 screen_free(struct screen *s)
150 free(s->sel);
151 free(s->tabs);
152 free(s->path);
153 free(s->title);
155 if (s->write_list != NULL)
156 screen_write_free_list(s);
158 if (SCREEN_IS_ALTERNATE(s))
159 grid_destroy(s->saved_grid);
160 grid_destroy(s->grid);
162 if (s->hyperlinks != NULL)
163 hyperlinks_free(s->hyperlinks);
164 screen_free_titles(s);
166 #ifdef ENABLE_SIXEL
167 image_free_all(s);
168 #endif
171 /* Reset tabs to default, eight spaces apart. */
172 void
173 screen_reset_tabs(struct screen *s)
175 u_int i;
177 free(s->tabs);
179 if ((s->tabs = bit_alloc(screen_size_x(s))) == NULL)
180 fatal("bit_alloc failed");
181 for (i = 8; i < screen_size_x(s); i += 8)
182 bit_set(s->tabs, i);
185 /* Set default cursor style and colour from options. */
186 void
187 screen_set_default_cursor(struct screen *s, struct options *oo)
189 int c;
191 c = options_get_number(oo, "cursor-colour");
192 s->default_ccolour = c;
194 c = options_get_number(oo, "cursor-style");
195 s->default_mode = 0;
196 screen_set_cursor_style(c, &s->default_cstyle, &s->default_mode);
199 /* Set screen cursor style and mode. */
200 void
201 screen_set_cursor_style(u_int style, enum screen_cursor_style *cstyle,
202 int *mode)
204 switch (style) {
205 case 0:
206 *cstyle = SCREEN_CURSOR_DEFAULT;
207 break;
208 case 1:
209 *cstyle = SCREEN_CURSOR_BLOCK;
210 *mode |= MODE_CURSOR_BLINKING;
211 break;
212 case 2:
213 *cstyle = SCREEN_CURSOR_BLOCK;
214 *mode &= ~MODE_CURSOR_BLINKING;
215 break;
216 case 3:
217 *cstyle = SCREEN_CURSOR_UNDERLINE;
218 *mode |= MODE_CURSOR_BLINKING;
219 break;
220 case 4:
221 *cstyle = SCREEN_CURSOR_UNDERLINE;
222 *mode &= ~MODE_CURSOR_BLINKING;
223 break;
224 case 5:
225 *cstyle = SCREEN_CURSOR_BAR;
226 *mode |= MODE_CURSOR_BLINKING;
227 break;
228 case 6:
229 *cstyle = SCREEN_CURSOR_BAR;
230 *mode &= ~MODE_CURSOR_BLINKING;
231 break;
235 /* Set screen cursor colour. */
236 void
237 screen_set_cursor_colour(struct screen *s, int colour)
239 s->ccolour = colour;
242 /* Set screen title. */
244 screen_set_title(struct screen *s, const char *title)
246 if (!utf8_isvalid(title))
247 return (0);
248 free(s->title);
249 s->title = xstrdup(title);
250 return (1);
253 /* Set screen path. */
254 void
255 screen_set_path(struct screen *s, const char *path)
257 free(s->path);
258 utf8_stravis(&s->path, path, VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL);
261 /* Push the current title onto the stack. */
262 void
263 screen_push_title(struct screen *s)
265 struct screen_title_entry *title_entry;
267 if (s->titles == NULL) {
268 s->titles = xmalloc(sizeof *s->titles);
269 TAILQ_INIT(s->titles);
271 title_entry = xmalloc(sizeof *title_entry);
272 title_entry->text = xstrdup(s->title);
273 TAILQ_INSERT_HEAD(s->titles, title_entry, entry);
277 * Pop a title from the stack and set it as the screen title. If the stack is
278 * empty, do nothing.
280 void
281 screen_pop_title(struct screen *s)
283 struct screen_title_entry *title_entry;
285 if (s->titles == NULL)
286 return;
288 title_entry = TAILQ_FIRST(s->titles);
289 if (title_entry != NULL) {
290 screen_set_title(s, title_entry->text);
292 TAILQ_REMOVE(s->titles, title_entry, entry);
293 free(title_entry->text);
294 free(title_entry);
298 /* Resize screen with options. */
299 void
300 screen_resize_cursor(struct screen *s, u_int sx, u_int sy, int reflow,
301 int eat_empty, int cursor)
303 u_int cx = s->cx, cy = s->grid->hsize + s->cy;
305 if (s->write_list != NULL)
306 screen_write_free_list(s);
308 log_debug("%s: new size %ux%u, now %ux%u (cursor %u,%u = %u,%u)",
309 __func__, sx, sy, screen_size_x(s), screen_size_y(s), s->cx, s->cy,
310 cx, cy);
312 if (sx < 1)
313 sx = 1;
314 if (sy < 1)
315 sy = 1;
317 if (sx != screen_size_x(s)) {
318 s->grid->sx = sx;
319 screen_reset_tabs(s);
320 } else
321 reflow = 0;
323 if (sy != screen_size_y(s))
324 screen_resize_y(s, sy, eat_empty, &cy);
326 #ifdef ENABLE_SIXEL
327 image_free_all(s);
328 #endif
330 if (reflow)
331 screen_reflow(s, sx, &cx, &cy, cursor);
333 if (cy >= s->grid->hsize) {
334 s->cx = cx;
335 s->cy = cy - s->grid->hsize;
336 } else {
337 s->cx = 0;
338 s->cy = 0;
341 log_debug("%s: cursor finished at %u,%u = %u,%u", __func__, s->cx,
342 s->cy, cx, cy);
344 if (s->write_list != NULL)
345 screen_write_make_list(s);
348 /* Resize screen. */
349 void
350 screen_resize(struct screen *s, u_int sx, u_int sy, int reflow)
352 screen_resize_cursor(s, sx, sy, reflow, 1, 1);
355 static void
356 screen_resize_y(struct screen *s, u_int sy, int eat_empty, u_int *cy)
358 struct grid *gd = s->grid;
359 u_int needed, available, oldy, i;
361 if (sy == 0)
362 fatalx("zero size");
363 oldy = screen_size_y(s);
366 * When resizing:
368 * If the height is decreasing, delete lines from the bottom until
369 * hitting the cursor, then push lines from the top into the history.
371 * When increasing, pull as many lines as possible from scrolled
372 * history (not explicitly cleared from view) to the top, then fill the
373 * remaining with blanks at the bottom.
376 /* Size decreasing. */
377 if (sy < oldy) {
378 needed = oldy - sy;
380 /* Delete as many lines as possible from the bottom. */
381 if (eat_empty) {
382 available = oldy - 1 - s->cy;
383 if (available > 0) {
384 if (available > needed)
385 available = needed;
386 grid_view_delete_lines(gd, oldy - available,
387 available, 8);
389 needed -= available;
393 * Now just increase the history size, if possible, to take
394 * over the lines which are left. If history is off, delete
395 * lines from the top.
397 available = s->cy;
398 if (gd->flags & GRID_HISTORY) {
399 gd->hscrolled += needed;
400 gd->hsize += needed;
401 } else if (needed > 0 && available > 0) {
402 if (available > needed)
403 available = needed;
404 grid_view_delete_lines(gd, 0, available, 8);
405 (*cy) -= available;
409 /* Resize line array. */
410 grid_adjust_lines(gd, gd->hsize + sy);
412 /* Size increasing. */
413 if (sy > oldy) {
414 needed = sy - oldy;
417 * Try to pull as much as possible out of scrolled history, if
418 * it is enabled.
420 available = gd->hscrolled;
421 if (gd->flags & GRID_HISTORY && available > 0) {
422 if (available > needed)
423 available = needed;
424 gd->hscrolled -= available;
425 gd->hsize -= available;
426 } else
427 available = 0;
428 needed -= available;
430 /* Then fill the rest in with blanks. */
431 for (i = gd->hsize + sy - needed; i < gd->hsize + sy; i++)
432 grid_empty_line(gd, i, 8);
435 /* Set the new size, and reset the scroll region. */
436 gd->sy = sy;
437 s->rupper = 0;
438 s->rlower = screen_size_y(s) - 1;
441 /* Set selection. */
442 void
443 screen_set_selection(struct screen *s, u_int sx, u_int sy,
444 u_int ex, u_int ey, u_int rectangle, int modekeys, struct grid_cell *gc)
446 if (s->sel == NULL)
447 s->sel = xcalloc(1, sizeof *s->sel);
449 memcpy(&s->sel->cell, gc, sizeof s->sel->cell);
450 s->sel->hidden = 0;
451 s->sel->rectangle = rectangle;
452 s->sel->modekeys = modekeys;
454 s->sel->sx = sx;
455 s->sel->sy = sy;
456 s->sel->ex = ex;
457 s->sel->ey = ey;
460 /* Clear selection. */
461 void
462 screen_clear_selection(struct screen *s)
464 free(s->sel);
465 s->sel = NULL;
468 /* Hide selection. */
469 void
470 screen_hide_selection(struct screen *s)
472 if (s->sel != NULL)
473 s->sel->hidden = 1;
476 /* Check if cell in selection. */
478 screen_check_selection(struct screen *s, u_int px, u_int py)
480 struct screen_sel *sel = s->sel;
481 u_int xx;
483 if (sel == NULL || sel->hidden)
484 return (0);
486 if (sel->rectangle) {
487 if (sel->sy < sel->ey) {
488 /* start line < end line -- downward selection. */
489 if (py < sel->sy || py > sel->ey)
490 return (0);
491 } else if (sel->sy > sel->ey) {
492 /* start line > end line -- upward selection. */
493 if (py > sel->sy || py < sel->ey)
494 return (0);
495 } else {
496 /* starting line == ending line. */
497 if (py != sel->sy)
498 return (0);
502 * Need to include the selection start row, but not the cursor
503 * row, which means the selection changes depending on which
504 * one is on the left.
506 if (sel->ex < sel->sx) {
507 /* Cursor (ex) is on the left. */
508 if (px < sel->ex)
509 return (0);
511 if (px > sel->sx)
512 return (0);
513 } else {
514 /* Selection start (sx) is on the left. */
515 if (px < sel->sx)
516 return (0);
518 if (px > sel->ex)
519 return (0);
521 } else {
523 * Like emacs, keep the top-left-most character, and drop the
524 * bottom-right-most, regardless of copy direction.
526 if (sel->sy < sel->ey) {
527 /* starting line < ending line -- downward selection. */
528 if (py < sel->sy || py > sel->ey)
529 return (0);
531 if (py == sel->sy && px < sel->sx)
532 return (0);
534 if (sel->modekeys == MODEKEY_EMACS)
535 xx = (sel->ex == 0 ? 0 : sel->ex - 1);
536 else
537 xx = sel->ex;
538 if (py == sel->ey && px > xx)
539 return (0);
540 } else if (sel->sy > sel->ey) {
541 /* starting line > ending line -- upward selection. */
542 if (py > sel->sy || py < sel->ey)
543 return (0);
545 if (py == sel->ey && px < sel->ex)
546 return (0);
548 if (sel->modekeys == MODEKEY_EMACS)
549 xx = sel->sx - 1;
550 else
551 xx = sel->sx;
552 if (py == sel->sy && (sel->sx == 0 || px > xx))
553 return (0);
554 } else {
555 /* starting line == ending line. */
556 if (py != sel->sy)
557 return (0);
559 if (sel->ex < sel->sx) {
560 /* cursor (ex) is on the left */
561 if (sel->modekeys == MODEKEY_EMACS)
562 xx = sel->sx - 1;
563 else
564 xx = sel->sx;
565 if (px > xx || px < sel->ex)
566 return (0);
567 } else {
568 /* selection start (sx) is on the left */
569 if (sel->modekeys == MODEKEY_EMACS)
570 xx = (sel->ex == 0 ? 0 : sel->ex - 1);
571 else
572 xx = sel->ex;
573 if (px < sel->sx || px > xx)
574 return (0);
579 return (1);
582 /* Get selected grid cell. */
583 void
584 screen_select_cell(struct screen *s, struct grid_cell *dst,
585 const struct grid_cell *src)
587 if (s->sel == NULL || s->sel->hidden)
588 return;
590 memcpy(dst, &s->sel->cell, sizeof *dst);
592 utf8_copy(&dst->data, &src->data);
593 dst->attr = dst->attr & ~GRID_ATTR_CHARSET;
594 dst->attr |= src->attr & GRID_ATTR_CHARSET;
595 dst->flags = src->flags;
598 /* Reflow wrapped lines. */
599 static void
600 screen_reflow(struct screen *s, u_int new_x, u_int *cx, u_int *cy, int cursor)
602 u_int wx, wy;
604 if (cursor) {
605 grid_wrap_position(s->grid, *cx, *cy, &wx, &wy);
606 log_debug("%s: cursor %u,%u is %u,%u", __func__, *cx, *cy, wx,
607 wy);
610 grid_reflow(s->grid, new_x);
612 if (cursor) {
613 grid_unwrap_position(s->grid, cx, cy, wx, wy);
614 log_debug("%s: new cursor is %u,%u", __func__, *cx, *cy);
616 else {
617 *cx = 0;
618 *cy = s->grid->hsize;
623 * Enter alternative screen mode. A copy of the visible screen is saved and the
624 * history is not updated.
626 void
627 screen_alternate_on(struct screen *s, struct grid_cell *gc, int cursor)
629 u_int sx, sy;
631 if (SCREEN_IS_ALTERNATE(s))
632 return;
633 sx = screen_size_x(s);
634 sy = screen_size_y(s);
636 s->saved_grid = grid_create(sx, sy, 0);
637 grid_duplicate_lines(s->saved_grid, 0, s->grid, screen_hsize(s), sy);
638 if (cursor) {
639 s->saved_cx = s->cx;
640 s->saved_cy = s->cy;
642 memcpy(&s->saved_cell, gc, sizeof s->saved_cell);
644 grid_view_clear(s->grid, 0, 0, sx, sy, 8);
646 s->saved_flags = s->grid->flags;
647 s->grid->flags &= ~GRID_HISTORY;
650 /* Exit alternate screen mode and restore the copied grid. */
651 void
652 screen_alternate_off(struct screen *s, struct grid_cell *gc, int cursor)
654 u_int sx = screen_size_x(s), sy = screen_size_y(s);
657 * If the current size is different, temporarily resize to the old size
658 * before copying back.
660 if (SCREEN_IS_ALTERNATE(s))
661 screen_resize(s, s->saved_grid->sx, s->saved_grid->sy, 0);
664 * Restore the cursor position and cell. This happens even if not
665 * currently in the alternate screen.
667 if (cursor && s->saved_cx != UINT_MAX && s->saved_cy != UINT_MAX) {
668 s->cx = s->saved_cx;
669 s->cy = s->saved_cy;
670 if (gc != NULL)
671 memcpy(gc, &s->saved_cell, sizeof *gc);
674 /* If not in the alternate screen, do nothing more. */
675 if (!SCREEN_IS_ALTERNATE(s)) {
676 if (s->cx > screen_size_x(s) - 1)
677 s->cx = screen_size_x(s) - 1;
678 if (s->cy > screen_size_y(s) - 1)
679 s->cy = screen_size_y(s) - 1;
680 return;
683 /* Restore the saved grid. */
684 grid_duplicate_lines(s->grid, screen_hsize(s), s->saved_grid, 0,
685 s->saved_grid->sy);
688 * Turn history back on (so resize can use it) and then resize back to
689 * the current size.
691 if (s->saved_flags & GRID_HISTORY)
692 s->grid->flags |= GRID_HISTORY;
693 screen_resize(s, sx, sy, 1);
695 grid_destroy(s->saved_grid);
696 s->saved_grid = NULL;
698 if (s->cx > screen_size_x(s) - 1)
699 s->cx = screen_size_x(s) - 1;
700 if (s->cy > screen_size_y(s) - 1)
701 s->cy = screen_size_y(s) - 1;
704 /* Get mode as a string. */
705 const char *
706 screen_mode_to_string(int mode)
708 static char tmp[1024];
710 if (mode == 0)
711 return ("NONE");
712 if (mode == ALL_MODES)
713 return ("ALL");
715 *tmp = '\0';
716 if (mode & MODE_CURSOR)
717 strlcat(tmp, "CURSOR,", sizeof tmp);
718 if (mode & MODE_INSERT)
719 strlcat(tmp, "INSERT,", sizeof tmp);
720 if (mode & MODE_KCURSOR)
721 strlcat(tmp, "KCURSOR,", sizeof tmp);
722 if (mode & MODE_KKEYPAD)
723 strlcat(tmp, "KKEYPAD,", sizeof tmp);
724 if (mode & MODE_WRAP)
725 strlcat(tmp, "WRAP,", sizeof tmp);
726 if (mode & MODE_MOUSE_STANDARD)
727 strlcat(tmp, "MOUSE_STANDARD,", sizeof tmp);
728 if (mode & MODE_MOUSE_BUTTON)
729 strlcat(tmp, "MOUSE_BUTTON,", sizeof tmp);
730 if (mode & MODE_CURSOR_BLINKING)
731 strlcat(tmp, "CURSOR_BLINKING,", sizeof tmp);
732 if (mode & MODE_CURSOR_VERY_VISIBLE)
733 strlcat(tmp, "CURSOR_VERY_VISIBLE,", sizeof tmp);
734 if (mode & MODE_MOUSE_UTF8)
735 strlcat(tmp, "MOUSE_UTF8,", sizeof tmp);
736 if (mode & MODE_MOUSE_SGR)
737 strlcat(tmp, "MOUSE_SGR,", sizeof tmp);
738 if (mode & MODE_BRACKETPASTE)
739 strlcat(tmp, "BRACKETPASTE,", sizeof tmp);
740 if (mode & MODE_FOCUSON)
741 strlcat(tmp, "FOCUSON,", sizeof tmp);
742 if (mode & MODE_MOUSE_ALL)
743 strlcat(tmp, "MOUSE_ALL,", sizeof tmp);
744 if (mode & MODE_ORIGIN)
745 strlcat(tmp, "ORIGIN,", sizeof tmp);
746 if (mode & MODE_CRLF)
747 strlcat(tmp, "CRLF,", sizeof tmp);
748 if (mode & MODE_KEYS_EXTENDED)
749 strlcat(tmp, "KEYS_EXTENDED,", sizeof tmp);
750 if (mode & MODE_KEYS_EXTENDED_2)
751 strlcat(tmp, "KEYS_EXTENDED_2,", sizeof tmp);
752 tmp[strlen(tmp) - 1] = '\0';
753 return (tmp);