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>
27 /* Selected area in screen. */
39 struct grid_cell cell
;
42 /* Entry on title stack. */
43 struct screen_title_entry
{
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. */
55 screen_free_titles(struct screen
*s
)
57 struct screen_title_entry
*title_entry
;
59 if (s
->titles
== NULL
)
62 while ((title_entry
= TAILQ_FIRST(s
->titles
)) != NULL
) {
63 TAILQ_REMOVE(s
->titles
, title_entry
, entry
);
64 free(title_entry
->text
);
72 /* Create a new screen. */
74 screen_init(struct screen
*s
, u_int sx
, u_int sy
, u_int hlimit
)
76 s
->grid
= grid_create(sx
, sy
, hlimit
);
79 s
->title
= xstrdup("");
83 s
->cstyle
= SCREEN_CURSOR_DEFAULT
;
84 s
->default_cstyle
= SCREEN_CURSOR_DEFAULT
;
85 s
->mode
= MODE_CURSOR
;
88 s
->default_ccolour
= -1;
93 TAILQ_INIT(&s
->images
);
102 /* Reinitialise screen. */
104 screen_reinit(struct screen
*s
)
110 s
->rlower
= screen_size_y(s
) - 1;
112 s
->mode
= MODE_CURSOR
|MODE_WRAP
|(s
->mode
& MODE_CRLF
);
113 if (options_get_number(global_options
, "extended-keys") == 2)
114 s
->mode
|= MODE_KEXTENDED
;
116 if (s
->saved_grid
!= NULL
)
117 screen_alternate_off(s
, NULL
, 0);
118 s
->saved_cx
= UINT_MAX
;
119 s
->saved_cy
= UINT_MAX
;
121 screen_reset_tabs(s
);
123 grid_clear_lines(s
->grid
, s
->grid
->hsize
, s
->grid
->sy
, 8);
125 screen_clear_selection(s
);
126 screen_free_titles(s
);
132 screen_reset_hyperlinks(s
);
135 /* Reset hyperlinks of a screen. */
137 screen_reset_hyperlinks(struct screen
*s
)
139 if (s
->hyperlinks
== NULL
)
140 s
->hyperlinks
= hyperlinks_init();
142 hyperlinks_reset(s
->hyperlinks
);
145 /* Destroy a screen. */
147 screen_free(struct screen
*s
)
154 if (s
->write_list
!= NULL
)
155 screen_write_free_list(s
);
157 if (s
->saved_grid
!= NULL
)
158 grid_destroy(s
->saved_grid
);
159 grid_destroy(s
->grid
);
161 if (s
->hyperlinks
!= NULL
)
162 hyperlinks_free(s
->hyperlinks
);
163 screen_free_titles(s
);
170 /* Reset tabs to default, eight spaces apart. */
172 screen_reset_tabs(struct screen
*s
)
178 if ((s
->tabs
= bit_alloc(screen_size_x(s
))) == NULL
)
179 fatal("bit_alloc failed");
180 for (i
= 8; i
< screen_size_x(s
); i
+= 8)
184 /* Set screen cursor style and mode. */
186 screen_set_cursor_style(u_int style
, enum screen_cursor_style
*cstyle
,
191 *cstyle
= SCREEN_CURSOR_DEFAULT
;
194 *cstyle
= SCREEN_CURSOR_BLOCK
;
195 *mode
|= MODE_CURSOR_BLINKING
;
198 *cstyle
= SCREEN_CURSOR_BLOCK
;
199 *mode
&= ~MODE_CURSOR_BLINKING
;
202 *cstyle
= SCREEN_CURSOR_UNDERLINE
;
203 *mode
|= MODE_CURSOR_BLINKING
;
206 *cstyle
= SCREEN_CURSOR_UNDERLINE
;
207 *mode
&= ~MODE_CURSOR_BLINKING
;
210 *cstyle
= SCREEN_CURSOR_BAR
;
211 *mode
|= MODE_CURSOR_BLINKING
;
214 *cstyle
= SCREEN_CURSOR_BAR
;
215 *mode
&= ~MODE_CURSOR_BLINKING
;
220 /* Set screen cursor colour. */
222 screen_set_cursor_colour(struct screen
*s
, int colour
)
227 /* Set screen title. */
229 screen_set_title(struct screen
*s
, const char *title
)
231 if (!utf8_isvalid(title
))
234 s
->title
= xstrdup(title
);
238 /* Set screen path. */
240 screen_set_path(struct screen
*s
, const char *path
)
243 utf8_stravis(&s
->path
, path
, VIS_OCTAL
|VIS_CSTYLE
|VIS_TAB
|VIS_NL
);
246 /* Push the current title onto the stack. */
248 screen_push_title(struct screen
*s
)
250 struct screen_title_entry
*title_entry
;
252 if (s
->titles
== NULL
) {
253 s
->titles
= xmalloc(sizeof *s
->titles
);
254 TAILQ_INIT(s
->titles
);
256 title_entry
= xmalloc(sizeof *title_entry
);
257 title_entry
->text
= xstrdup(s
->title
);
258 TAILQ_INSERT_HEAD(s
->titles
, title_entry
, entry
);
262 * Pop a title from the stack and set it as the screen title. If the stack is
266 screen_pop_title(struct screen
*s
)
268 struct screen_title_entry
*title_entry
;
270 if (s
->titles
== NULL
)
273 title_entry
= TAILQ_FIRST(s
->titles
);
274 if (title_entry
!= NULL
) {
275 screen_set_title(s
, title_entry
->text
);
277 TAILQ_REMOVE(s
->titles
, title_entry
, entry
);
278 free(title_entry
->text
);
283 /* Resize screen with options. */
285 screen_resize_cursor(struct screen
*s
, u_int sx
, u_int sy
, int reflow
,
286 int eat_empty
, int cursor
)
288 u_int cx
= s
->cx
, cy
= s
->grid
->hsize
+ s
->cy
;
290 if (s
->write_list
!= NULL
)
291 screen_write_free_list(s
);
293 log_debug("%s: new size %ux%u, now %ux%u (cursor %u,%u = %u,%u)",
294 __func__
, sx
, sy
, screen_size_x(s
), screen_size_y(s
), s
->cx
, s
->cy
,
302 if (sx
!= screen_size_x(s
)) {
304 screen_reset_tabs(s
);
308 if (sy
!= screen_size_y(s
))
309 screen_resize_y(s
, sy
, eat_empty
, &cy
);
316 screen_reflow(s
, sx
, &cx
, &cy
, cursor
);
318 if (cy
>= s
->grid
->hsize
) {
320 s
->cy
= cy
- s
->grid
->hsize
;
326 log_debug("%s: cursor finished at %u,%u = %u,%u", __func__
, s
->cx
,
329 if (s
->write_list
!= NULL
)
330 screen_write_make_list(s
);
335 screen_resize(struct screen
*s
, u_int sx
, u_int sy
, int reflow
)
337 screen_resize_cursor(s
, sx
, sy
, reflow
, 1, 1);
341 screen_resize_y(struct screen
*s
, u_int sy
, int eat_empty
, u_int
*cy
)
343 struct grid
*gd
= s
->grid
;
344 u_int needed
, available
, oldy
, i
;
348 oldy
= screen_size_y(s
);
353 * If the height is decreasing, delete lines from the bottom until
354 * hitting the cursor, then push lines from the top into the history.
356 * When increasing, pull as many lines as possible from scrolled
357 * history (not explicitly cleared from view) to the top, then fill the
358 * remaining with blanks at the bottom.
361 /* Size decreasing. */
365 /* Delete as many lines as possible from the bottom. */
367 available
= oldy
- 1 - s
->cy
;
369 if (available
> needed
)
371 grid_view_delete_lines(gd
, oldy
- available
,
378 * Now just increase the history size, if possible, to take
379 * over the lines which are left. If history is off, delete
380 * lines from the top.
383 if (gd
->flags
& GRID_HISTORY
) {
384 gd
->hscrolled
+= needed
;
386 } else if (needed
> 0 && available
> 0) {
387 if (available
> needed
)
389 grid_view_delete_lines(gd
, 0, available
, 8);
394 /* Resize line array. */
395 grid_adjust_lines(gd
, gd
->hsize
+ sy
);
397 /* Size increasing. */
402 * Try to pull as much as possible out of scrolled history, if
405 available
= gd
->hscrolled
;
406 if (gd
->flags
& GRID_HISTORY
&& available
> 0) {
407 if (available
> needed
)
409 gd
->hscrolled
-= available
;
410 gd
->hsize
-= available
;
415 /* Then fill the rest in with blanks. */
416 for (i
= gd
->hsize
+ sy
- needed
; i
< gd
->hsize
+ sy
; i
++)
417 grid_empty_line(gd
, i
, 8);
420 /* Set the new size, and reset the scroll region. */
423 s
->rlower
= screen_size_y(s
) - 1;
428 screen_set_selection(struct screen
*s
, u_int sx
, u_int sy
,
429 u_int ex
, u_int ey
, u_int rectangle
, int modekeys
, struct grid_cell
*gc
)
432 s
->sel
= xcalloc(1, sizeof *s
->sel
);
434 memcpy(&s
->sel
->cell
, gc
, sizeof s
->sel
->cell
);
436 s
->sel
->rectangle
= rectangle
;
437 s
->sel
->modekeys
= modekeys
;
445 /* Clear selection. */
447 screen_clear_selection(struct screen
*s
)
453 /* Hide selection. */
455 screen_hide_selection(struct screen
*s
)
461 /* Check if cell in selection. */
463 screen_check_selection(struct screen
*s
, u_int px
, u_int py
)
465 struct screen_sel
*sel
= s
->sel
;
468 if (sel
== NULL
|| sel
->hidden
)
471 if (sel
->rectangle
) {
472 if (sel
->sy
< sel
->ey
) {
473 /* start line < end line -- downward selection. */
474 if (py
< sel
->sy
|| py
> sel
->ey
)
476 } else if (sel
->sy
> sel
->ey
) {
477 /* start line > end line -- upward selection. */
478 if (py
> sel
->sy
|| py
< sel
->ey
)
481 /* starting line == ending line. */
487 * Need to include the selection start row, but not the cursor
488 * row, which means the selection changes depending on which
489 * one is on the left.
491 if (sel
->ex
< sel
->sx
) {
492 /* Cursor (ex) is on the left. */
499 /* Selection start (sx) is on the left. */
508 * Like emacs, keep the top-left-most character, and drop the
509 * bottom-right-most, regardless of copy direction.
511 if (sel
->sy
< sel
->ey
) {
512 /* starting line < ending line -- downward selection. */
513 if (py
< sel
->sy
|| py
> sel
->ey
)
516 if (py
== sel
->sy
&& px
< sel
->sx
)
519 if (sel
->modekeys
== MODEKEY_EMACS
)
520 xx
= (sel
->ex
== 0 ? 0 : sel
->ex
- 1);
523 if (py
== sel
->ey
&& px
> xx
)
525 } else if (sel
->sy
> sel
->ey
) {
526 /* starting line > ending line -- upward selection. */
527 if (py
> sel
->sy
|| py
< sel
->ey
)
530 if (py
== sel
->ey
&& px
< sel
->ex
)
533 if (sel
->modekeys
== MODEKEY_EMACS
)
537 if (py
== sel
->sy
&& (sel
->sx
== 0 || px
> xx
))
540 /* starting line == ending line. */
544 if (sel
->ex
< sel
->sx
) {
545 /* cursor (ex) is on the left */
546 if (sel
->modekeys
== MODEKEY_EMACS
)
550 if (px
> xx
|| px
< sel
->ex
)
553 /* selection start (sx) is on the left */
554 if (sel
->modekeys
== MODEKEY_EMACS
)
555 xx
= (sel
->ex
== 0 ? 0 : sel
->ex
- 1);
558 if (px
< sel
->sx
|| px
> xx
)
567 /* Get selected grid cell. */
569 screen_select_cell(struct screen
*s
, struct grid_cell
*dst
,
570 const struct grid_cell
*src
)
572 if (s
->sel
== NULL
|| s
->sel
->hidden
)
575 memcpy(dst
, &s
->sel
->cell
, sizeof *dst
);
577 utf8_copy(&dst
->data
, &src
->data
);
578 dst
->attr
= dst
->attr
& ~GRID_ATTR_CHARSET
;
579 dst
->attr
|= src
->attr
& GRID_ATTR_CHARSET
;
580 dst
->flags
= src
->flags
;
583 /* Reflow wrapped lines. */
585 screen_reflow(struct screen
*s
, u_int new_x
, u_int
*cx
, u_int
*cy
, int cursor
)
590 grid_wrap_position(s
->grid
, *cx
, *cy
, &wx
, &wy
);
591 log_debug("%s: cursor %u,%u is %u,%u", __func__
, *cx
, *cy
, wx
,
595 grid_reflow(s
->grid
, new_x
);
598 grid_unwrap_position(s
->grid
, cx
, cy
, wx
, wy
);
599 log_debug("%s: new cursor is %u,%u", __func__
, *cx
, *cy
);
603 *cy
= s
->grid
->hsize
;
608 * Enter alternative screen mode. A copy of the visible screen is saved and the
609 * history is not updated.
612 screen_alternate_on(struct screen
*s
, struct grid_cell
*gc
, int cursor
)
616 if (s
->saved_grid
!= NULL
)
618 sx
= screen_size_x(s
);
619 sy
= screen_size_y(s
);
621 s
->saved_grid
= grid_create(sx
, sy
, 0);
622 grid_duplicate_lines(s
->saved_grid
, 0, s
->grid
, screen_hsize(s
), sy
);
627 memcpy(&s
->saved_cell
, gc
, sizeof s
->saved_cell
);
629 grid_view_clear(s
->grid
, 0, 0, sx
, sy
, 8);
631 s
->saved_flags
= s
->grid
->flags
;
632 s
->grid
->flags
&= ~GRID_HISTORY
;
635 /* Exit alternate screen mode and restore the copied grid. */
637 screen_alternate_off(struct screen
*s
, struct grid_cell
*gc
, int cursor
)
639 u_int sx
= screen_size_x(s
), sy
= screen_size_y(s
);
642 * If the current size is different, temporarily resize to the old size
643 * before copying back.
645 if (s
->saved_grid
!= NULL
)
646 screen_resize(s
, s
->saved_grid
->sx
, s
->saved_grid
->sy
, 0);
649 * Restore the cursor position and cell. This happens even if not
650 * currently in the alternate screen.
652 if (cursor
&& s
->saved_cx
!= UINT_MAX
&& s
->saved_cy
!= UINT_MAX
) {
656 memcpy(gc
, &s
->saved_cell
, sizeof *gc
);
659 /* If not in the alternate screen, do nothing more. */
660 if (s
->saved_grid
== NULL
) {
661 if (s
->cx
> screen_size_x(s
) - 1)
662 s
->cx
= screen_size_x(s
) - 1;
663 if (s
->cy
> screen_size_y(s
) - 1)
664 s
->cy
= screen_size_y(s
) - 1;
668 /* Restore the saved grid. */
669 grid_duplicate_lines(s
->grid
, screen_hsize(s
), s
->saved_grid
, 0,
673 * Turn history back on (so resize can use it) and then resize back to
676 if (s
->saved_flags
& GRID_HISTORY
)
677 s
->grid
->flags
|= GRID_HISTORY
;
678 screen_resize(s
, sx
, sy
, 1);
680 grid_destroy(s
->saved_grid
);
681 s
->saved_grid
= NULL
;
683 if (s
->cx
> screen_size_x(s
) - 1)
684 s
->cx
= screen_size_x(s
) - 1;
685 if (s
->cy
> screen_size_y(s
) - 1)
686 s
->cy
= screen_size_y(s
) - 1;
689 /* Get mode as a string. */
691 screen_mode_to_string(int mode
)
693 static char tmp
[1024];
697 if (mode
== ALL_MODES
)
701 if (mode
& MODE_CURSOR
)
702 strlcat(tmp
, "CURSOR,", sizeof tmp
);
703 if (mode
& MODE_INSERT
)
704 strlcat(tmp
, "INSERT,", sizeof tmp
);
705 if (mode
& MODE_KCURSOR
)
706 strlcat(tmp
, "KCURSOR,", sizeof tmp
);
707 if (mode
& MODE_KKEYPAD
)
708 strlcat(tmp
, "KKEYPAD,", sizeof tmp
);
709 if (mode
& MODE_WRAP
)
710 strlcat(tmp
, "WRAP,", sizeof tmp
);
711 if (mode
& MODE_MOUSE_STANDARD
)
712 strlcat(tmp
, "MOUSE_STANDARD,", sizeof tmp
);
713 if (mode
& MODE_MOUSE_BUTTON
)
714 strlcat(tmp
, "MOUSE_BUTTON,", sizeof tmp
);
715 if (mode
& MODE_CURSOR_BLINKING
)
716 strlcat(tmp
, "CURSOR_BLINKING,", sizeof tmp
);
717 if (mode
& MODE_CURSOR_VERY_VISIBLE
)
718 strlcat(tmp
, "CURSOR_VERY_VISIBLE,", sizeof tmp
);
719 if (mode
& MODE_MOUSE_UTF8
)
720 strlcat(tmp
, "MOUSE_UTF8,", sizeof tmp
);
721 if (mode
& MODE_MOUSE_SGR
)
722 strlcat(tmp
, "MOUSE_SGR,", sizeof tmp
);
723 if (mode
& MODE_BRACKETPASTE
)
724 strlcat(tmp
, "BRACKETPASTE,", sizeof tmp
);
725 if (mode
& MODE_FOCUSON
)
726 strlcat(tmp
, "FOCUSON,", sizeof tmp
);
727 if (mode
& MODE_MOUSE_ALL
)
728 strlcat(tmp
, "MOUSE_ALL,", sizeof tmp
);
729 if (mode
& MODE_ORIGIN
)
730 strlcat(tmp
, "ORIGIN,", sizeof tmp
);
731 if (mode
& MODE_CRLF
)
732 strlcat(tmp
, "CRLF,", sizeof tmp
);
733 if (mode
& MODE_KEXTENDED
)
734 strlcat(tmp
, "KEXTENDED,", sizeof tmp
);
735 tmp
[strlen(tmp
) - 1] = '\0';