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>
28 /* Selected area in screen. */
40 struct grid_cell cell
;
43 /* Entry on title stack. */
44 struct screen_title_entry
{
47 TAILQ_ENTRY(screen_title_entry
) entry
;
49 TAILQ_HEAD(screen_titles
, screen_title_entry
);
51 static void screen_resize_y(struct screen
*, u_int
, int, u_int
*);
52 static void screen_reflow(struct screen
*, u_int
, u_int
*, u_int
*, int);
54 /* Free titles stack. */
56 screen_free_titles(struct screen
*s
)
58 struct screen_title_entry
*title_entry
;
60 if (s
->titles
== NULL
)
63 while ((title_entry
= TAILQ_FIRST(s
->titles
)) != NULL
) {
64 TAILQ_REMOVE(s
->titles
, title_entry
, entry
);
65 free(title_entry
->text
);
73 /* Create a new screen. */
75 screen_init(struct screen
*s
, u_int sx
, u_int sy
, u_int hlimit
)
77 s
->grid
= grid_create(sx
, sy
, hlimit
);
80 s
->title
= xstrdup("");
84 s
->cstyle
= SCREEN_CURSOR_DEFAULT
;
85 s
->default_cstyle
= SCREEN_CURSOR_DEFAULT
;
86 s
->mode
= MODE_CURSOR
;
89 s
->default_ccolour
= -1;
99 /* Reinitialise screen. */
101 screen_reinit(struct screen
*s
)
107 s
->rlower
= screen_size_y(s
) - 1;
109 s
->mode
= MODE_CURSOR
|MODE_WRAP
|(s
->mode
& MODE_CRLF
);
110 if (options_get_number(global_options
, "extended-keys") == 2)
111 s
->mode
|= MODE_KEXTENDED
;
113 if (s
->saved_grid
!= NULL
)
114 screen_alternate_off(s
, NULL
, 0);
115 s
->saved_cx
= UINT_MAX
;
116 s
->saved_cy
= UINT_MAX
;
118 screen_reset_tabs(s
);
120 grid_clear_lines(s
->grid
, s
->grid
->hsize
, s
->grid
->sy
, 8);
122 screen_clear_selection(s
);
123 screen_free_titles(s
);
124 screen_reset_hyperlinks(s
);
127 /* Reset hyperlinks of a screen. */
129 screen_reset_hyperlinks(struct screen
*s
)
131 if (s
->hyperlinks
== NULL
)
132 s
->hyperlinks
= hyperlinks_init();
134 hyperlinks_reset(s
->hyperlinks
);
137 /* Destroy a screen. */
139 screen_free(struct screen
*s
)
146 if (s
->write_list
!= NULL
)
147 screen_write_free_list(s
);
149 if (s
->saved_grid
!= NULL
)
150 grid_destroy(s
->saved_grid
);
151 grid_destroy(s
->grid
);
153 if (s
->hyperlinks
!= NULL
)
154 hyperlinks_free(s
->hyperlinks
);
155 screen_free_titles(s
);
158 /* Reset tabs to default, eight spaces apart. */
160 screen_reset_tabs(struct screen
*s
)
166 if ((s
->tabs
= bit_alloc(screen_size_x(s
))) == NULL
)
167 fatal("bit_alloc failed");
168 for (i
= 8; i
< screen_size_x(s
); i
+= 8)
172 /* Set screen cursor style and mode. */
174 screen_set_cursor_style(u_int style
, enum screen_cursor_style
*cstyle
,
179 *cstyle
= SCREEN_CURSOR_DEFAULT
;
182 *cstyle
= SCREEN_CURSOR_BLOCK
;
183 *mode
|= MODE_CURSOR_BLINKING
;
186 *cstyle
= SCREEN_CURSOR_BLOCK
;
187 *mode
&= ~MODE_CURSOR_BLINKING
;
190 *cstyle
= SCREEN_CURSOR_UNDERLINE
;
191 *mode
|= MODE_CURSOR_BLINKING
;
194 *cstyle
= SCREEN_CURSOR_UNDERLINE
;
195 *mode
&= ~MODE_CURSOR_BLINKING
;
198 *cstyle
= SCREEN_CURSOR_BAR
;
199 *mode
|= MODE_CURSOR_BLINKING
;
202 *cstyle
= SCREEN_CURSOR_BAR
;
203 *mode
&= ~MODE_CURSOR_BLINKING
;
208 /* Set screen cursor colour. */
210 screen_set_cursor_colour(struct screen
*s
, int colour
)
215 /* Set screen title. */
217 screen_set_title(struct screen
*s
, const char *title
)
219 if (!utf8_isvalid(title
))
222 s
->title
= xstrdup(title
);
226 /* Set screen path. */
228 screen_set_path(struct screen
*s
, const char *path
)
231 utf8_stravis(&s
->path
, path
, VIS_OCTAL
|VIS_CSTYLE
|VIS_TAB
|VIS_NL
);
234 /* Push the current title onto the stack. */
236 screen_push_title(struct screen
*s
)
238 struct screen_title_entry
*title_entry
;
240 if (s
->titles
== NULL
) {
241 s
->titles
= xmalloc(sizeof *s
->titles
);
242 TAILQ_INIT(s
->titles
);
244 title_entry
= xmalloc(sizeof *title_entry
);
245 title_entry
->text
= xstrdup(s
->title
);
246 TAILQ_INSERT_HEAD(s
->titles
, title_entry
, entry
);
250 * Pop a title from the stack and set it as the screen title. If the stack is
254 screen_pop_title(struct screen
*s
)
256 struct screen_title_entry
*title_entry
;
258 if (s
->titles
== NULL
)
261 title_entry
= TAILQ_FIRST(s
->titles
);
262 if (title_entry
!= NULL
) {
263 screen_set_title(s
, title_entry
->text
);
265 TAILQ_REMOVE(s
->titles
, title_entry
, entry
);
266 free(title_entry
->text
);
271 /* Resize screen with options. */
273 screen_resize_cursor(struct screen
*s
, u_int sx
, u_int sy
, int reflow
,
274 int eat_empty
, int cursor
)
276 u_int cx
= s
->cx
, cy
= s
->grid
->hsize
+ s
->cy
;
278 if (s
->write_list
!= NULL
)
279 screen_write_free_list(s
);
281 log_debug("%s: new size %ux%u, now %ux%u (cursor %u,%u = %u,%u)",
282 __func__
, sx
, sy
, screen_size_x(s
), screen_size_y(s
), s
->cx
, s
->cy
,
290 if (sx
!= screen_size_x(s
)) {
292 screen_reset_tabs(s
);
296 if (sy
!= screen_size_y(s
))
297 screen_resize_y(s
, sy
, eat_empty
, &cy
);
300 screen_reflow(s
, sx
, &cx
, &cy
, cursor
);
302 if (cy
>= s
->grid
->hsize
) {
304 s
->cy
= cy
- s
->grid
->hsize
;
310 log_debug("%s: cursor finished at %u,%u = %u,%u", __func__
, s
->cx
,
313 if (s
->write_list
!= NULL
)
314 screen_write_make_list(s
);
319 screen_resize(struct screen
*s
, u_int sx
, u_int sy
, int reflow
)
321 screen_resize_cursor(s
, sx
, sy
, reflow
, 1, 1);
325 screen_resize_y(struct screen
*s
, u_int sy
, int eat_empty
, u_int
*cy
)
327 struct grid
*gd
= s
->grid
;
328 u_int needed
, available
, oldy
, i
;
332 oldy
= screen_size_y(s
);
337 * If the height is decreasing, delete lines from the bottom until
338 * hitting the cursor, then push lines from the top into the history.
340 * When increasing, pull as many lines as possible from scrolled
341 * history (not explicitly cleared from view) to the top, then fill the
342 * remaining with blanks at the bottom.
345 /* Size decreasing. */
349 /* Delete as many lines as possible from the bottom. */
351 available
= oldy
- 1 - s
->cy
;
353 if (available
> needed
)
355 grid_view_delete_lines(gd
, oldy
- available
,
362 * Now just increase the history size, if possible, to take
363 * over the lines which are left. If history is off, delete
364 * lines from the top.
367 if (gd
->flags
& GRID_HISTORY
) {
368 gd
->hscrolled
+= needed
;
370 } else if (needed
> 0 && available
> 0) {
371 if (available
> needed
)
373 grid_view_delete_lines(gd
, 0, available
, 8);
378 /* Resize line array. */
379 grid_adjust_lines(gd
, gd
->hsize
+ sy
);
381 /* Size increasing. */
386 * Try to pull as much as possible out of scrolled history, if
389 available
= gd
->hscrolled
;
390 if (gd
->flags
& GRID_HISTORY
&& available
> 0) {
391 if (available
> needed
)
393 gd
->hscrolled
-= available
;
394 gd
->hsize
-= available
;
399 /* Then fill the rest in with blanks. */
400 for (i
= gd
->hsize
+ sy
- needed
; i
< gd
->hsize
+ sy
; i
++)
401 grid_empty_line(gd
, i
, 8);
404 /* Set the new size, and reset the scroll region. */
407 s
->rlower
= screen_size_y(s
) - 1;
412 screen_set_selection(struct screen
*s
, u_int sx
, u_int sy
,
413 u_int ex
, u_int ey
, u_int rectangle
, int modekeys
, struct grid_cell
*gc
)
416 s
->sel
= xcalloc(1, sizeof *s
->sel
);
418 memcpy(&s
->sel
->cell
, gc
, sizeof s
->sel
->cell
);
420 s
->sel
->rectangle
= rectangle
;
421 s
->sel
->modekeys
= modekeys
;
429 /* Clear selection. */
431 screen_clear_selection(struct screen
*s
)
437 /* Hide selection. */
439 screen_hide_selection(struct screen
*s
)
445 /* Check if cell in selection. */
447 screen_check_selection(struct screen
*s
, u_int px
, u_int py
)
449 struct screen_sel
*sel
= s
->sel
;
452 if (sel
== NULL
|| sel
->hidden
)
455 if (sel
->rectangle
) {
456 if (sel
->sy
< sel
->ey
) {
457 /* start line < end line -- downward selection. */
458 if (py
< sel
->sy
|| py
> sel
->ey
)
460 } else if (sel
->sy
> sel
->ey
) {
461 /* start line > end line -- upward selection. */
462 if (py
> sel
->sy
|| py
< sel
->ey
)
465 /* starting line == ending line. */
471 * Need to include the selection start row, but not the cursor
472 * row, which means the selection changes depending on which
473 * one is on the left.
475 if (sel
->ex
< sel
->sx
) {
476 /* Cursor (ex) is on the left. */
483 /* Selection start (sx) is on the left. */
492 * Like emacs, keep the top-left-most character, and drop the
493 * bottom-right-most, regardless of copy direction.
495 if (sel
->sy
< sel
->ey
) {
496 /* starting line < ending line -- downward selection. */
497 if (py
< sel
->sy
|| py
> sel
->ey
)
500 if (py
== sel
->sy
&& px
< sel
->sx
)
503 if (sel
->modekeys
== MODEKEY_EMACS
)
504 xx
= (sel
->ex
== 0 ? 0 : sel
->ex
- 1);
507 if (py
== sel
->ey
&& px
> xx
)
509 } else if (sel
->sy
> sel
->ey
) {
510 /* starting line > ending line -- upward selection. */
511 if (py
> sel
->sy
|| py
< sel
->ey
)
514 if (py
== sel
->ey
&& px
< sel
->ex
)
517 if (sel
->modekeys
== MODEKEY_EMACS
)
521 if (py
== sel
->sy
&& (sel
->sx
== 0 || px
> xx
))
524 /* starting line == ending line. */
528 if (sel
->ex
< sel
->sx
) {
529 /* cursor (ex) is on the left */
530 if (sel
->modekeys
== MODEKEY_EMACS
)
534 if (px
> xx
|| px
< sel
->ex
)
537 /* selection start (sx) is on the left */
538 if (sel
->modekeys
== MODEKEY_EMACS
)
539 xx
= (sel
->ex
== 0 ? 0 : sel
->ex
- 1);
542 if (px
< sel
->sx
|| px
> xx
)
551 /* Get selected grid cell. */
553 screen_select_cell(struct screen
*s
, struct grid_cell
*dst
,
554 const struct grid_cell
*src
)
556 if (s
->sel
== NULL
|| s
->sel
->hidden
)
559 memcpy(dst
, &s
->sel
->cell
, sizeof *dst
);
561 utf8_copy(&dst
->data
, &src
->data
);
562 dst
->attr
= dst
->attr
& ~GRID_ATTR_CHARSET
;
563 dst
->attr
|= src
->attr
& GRID_ATTR_CHARSET
;
564 dst
->flags
= src
->flags
;
567 /* Reflow wrapped lines. */
569 screen_reflow(struct screen
*s
, u_int new_x
, u_int
*cx
, u_int
*cy
, int cursor
)
574 grid_wrap_position(s
->grid
, *cx
, *cy
, &wx
, &wy
);
575 log_debug("%s: cursor %u,%u is %u,%u", __func__
, *cx
, *cy
, wx
,
579 grid_reflow(s
->grid
, new_x
);
582 grid_unwrap_position(s
->grid
, cx
, cy
, wx
, wy
);
583 log_debug("%s: new cursor is %u,%u", __func__
, *cx
, *cy
);
587 *cy
= s
->grid
->hsize
;
592 * Enter alternative screen mode. A copy of the visible screen is saved and the
593 * history is not updated.
596 screen_alternate_on(struct screen
*s
, struct grid_cell
*gc
, int cursor
)
600 if (s
->saved_grid
!= NULL
)
602 sx
= screen_size_x(s
);
603 sy
= screen_size_y(s
);
605 s
->saved_grid
= grid_create(sx
, sy
, 0);
606 grid_duplicate_lines(s
->saved_grid
, 0, s
->grid
, screen_hsize(s
), sy
);
611 memcpy(&s
->saved_cell
, gc
, sizeof s
->saved_cell
);
613 grid_view_clear(s
->grid
, 0, 0, sx
, sy
, 8);
615 s
->saved_flags
= s
->grid
->flags
;
616 s
->grid
->flags
&= ~GRID_HISTORY
;
619 /* Exit alternate screen mode and restore the copied grid. */
621 screen_alternate_off(struct screen
*s
, struct grid_cell
*gc
, int cursor
)
623 u_int sx
= screen_size_x(s
), sy
= screen_size_y(s
);
626 * If the current size is different, temporarily resize to the old size
627 * before copying back.
629 if (s
->saved_grid
!= NULL
)
630 screen_resize(s
, s
->saved_grid
->sx
, s
->saved_grid
->sy
, 0);
633 * Restore the cursor position and cell. This happens even if not
634 * currently in the alternate screen.
636 if (cursor
&& s
->saved_cx
!= UINT_MAX
&& s
->saved_cy
!= UINT_MAX
) {
640 memcpy(gc
, &s
->saved_cell
, sizeof *gc
);
643 /* If not in the alternate screen, do nothing more. */
644 if (s
->saved_grid
== NULL
) {
645 if (s
->cx
> screen_size_x(s
) - 1)
646 s
->cx
= screen_size_x(s
) - 1;
647 if (s
->cy
> screen_size_y(s
) - 1)
648 s
->cy
= screen_size_y(s
) - 1;
652 /* Restore the saved grid. */
653 grid_duplicate_lines(s
->grid
, screen_hsize(s
), s
->saved_grid
, 0,
657 * Turn history back on (so resize can use it) and then resize back to
660 if (s
->saved_flags
& GRID_HISTORY
)
661 s
->grid
->flags
|= GRID_HISTORY
;
662 screen_resize(s
, sx
, sy
, 1);
664 grid_destroy(s
->saved_grid
);
665 s
->saved_grid
= NULL
;
667 if (s
->cx
> screen_size_x(s
) - 1)
668 s
->cx
= screen_size_x(s
) - 1;
669 if (s
->cy
> screen_size_y(s
) - 1)
670 s
->cy
= screen_size_y(s
) - 1;
673 /* Get mode as a string. */
675 screen_mode_to_string(int mode
)
677 static char tmp
[1024];
681 if (mode
== ALL_MODES
)
685 if (mode
& MODE_CURSOR
)
686 strlcat(tmp
, "CURSOR,", sizeof tmp
);
687 if (mode
& MODE_INSERT
)
688 strlcat(tmp
, "INSERT,", sizeof tmp
);
689 if (mode
& MODE_KCURSOR
)
690 strlcat(tmp
, "KCURSOR,", sizeof tmp
);
691 if (mode
& MODE_KKEYPAD
)
692 strlcat(tmp
, "KKEYPAD,", sizeof tmp
);
693 if (mode
& MODE_WRAP
)
694 strlcat(tmp
, "WRAP,", sizeof tmp
);
695 if (mode
& MODE_MOUSE_STANDARD
)
696 strlcat(tmp
, "MOUSE_STANDARD,", sizeof tmp
);
697 if (mode
& MODE_MOUSE_BUTTON
)
698 strlcat(tmp
, "MOUSE_BUTTON,", sizeof tmp
);
699 if (mode
& MODE_CURSOR_BLINKING
)
700 strlcat(tmp
, "CURSOR_BLINKING,", sizeof tmp
);
701 if (mode
& MODE_CURSOR_VERY_VISIBLE
)
702 strlcat(tmp
, "CURSOR_VERY_VISIBLE,", sizeof tmp
);
703 if (mode
& MODE_MOUSE_UTF8
)
704 strlcat(tmp
, "MOUSE_UTF8,", sizeof tmp
);
705 if (mode
& MODE_MOUSE_SGR
)
706 strlcat(tmp
, "MOUSE_SGR,", sizeof tmp
);
707 if (mode
& MODE_BRACKETPASTE
)
708 strlcat(tmp
, "BRACKETPASTE,", sizeof tmp
);
709 if (mode
& MODE_FOCUSON
)
710 strlcat(tmp
, "FOCUSON,", sizeof tmp
);
711 if (mode
& MODE_MOUSE_ALL
)
712 strlcat(tmp
, "MOUSE_ALL,", sizeof tmp
);
713 if (mode
& MODE_ORIGIN
)
714 strlcat(tmp
, "ORIGIN,", sizeof tmp
);
715 if (mode
& MODE_CRLF
)
716 strlcat(tmp
, "CRLF,", sizeof tmp
);
717 if (mode
& MODE_KEXTENDED
)
718 strlcat(tmp
, "KEXTENDED,", sizeof tmp
);
719 tmp
[strlen(tmp
) - 1] = '\0';