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
;
87 s
->default_ccolour
= -1;
92 TAILQ_INIT(&s
->images
);
101 /* Reinitialise screen. */
103 screen_reinit(struct screen
*s
)
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
);
131 screen_reset_hyperlinks(s
);
134 /* Reset hyperlinks of a screen. */
136 screen_reset_hyperlinks(struct screen
*s
)
138 if (s
->hyperlinks
== NULL
)
139 s
->hyperlinks
= hyperlinks_init();
141 hyperlinks_reset(s
->hyperlinks
);
144 /* Destroy a screen. */
146 screen_free(struct screen
*s
)
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
);
169 /* Reset tabs to default, eight spaces apart. */
171 screen_reset_tabs(struct screen
*s
)
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)
183 /* Set screen cursor style and mode. */
185 screen_set_cursor_style(u_int style
, enum screen_cursor_style
*cstyle
,
190 *cstyle
= SCREEN_CURSOR_DEFAULT
;
193 *cstyle
= SCREEN_CURSOR_BLOCK
;
194 *mode
|= MODE_CURSOR_BLINKING
;
197 *cstyle
= SCREEN_CURSOR_BLOCK
;
198 *mode
&= ~MODE_CURSOR_BLINKING
;
201 *cstyle
= SCREEN_CURSOR_UNDERLINE
;
202 *mode
|= MODE_CURSOR_BLINKING
;
205 *cstyle
= SCREEN_CURSOR_UNDERLINE
;
206 *mode
&= ~MODE_CURSOR_BLINKING
;
209 *cstyle
= SCREEN_CURSOR_BAR
;
210 *mode
|= MODE_CURSOR_BLINKING
;
213 *cstyle
= SCREEN_CURSOR_BAR
;
214 *mode
&= ~MODE_CURSOR_BLINKING
;
219 /* Set screen cursor colour. */
221 screen_set_cursor_colour(struct screen
*s
, int colour
)
226 /* Set screen title. */
228 screen_set_title(struct screen
*s
, const char *title
)
230 if (!utf8_isvalid(title
))
233 s
->title
= xstrdup(title
);
237 /* Set screen path. */
239 screen_set_path(struct screen
*s
, const char *path
)
242 utf8_stravis(&s
->path
, path
, VIS_OCTAL
|VIS_CSTYLE
|VIS_TAB
|VIS_NL
);
245 /* Push the current title onto the stack. */
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
265 screen_pop_title(struct screen
*s
)
267 struct screen_title_entry
*title_entry
;
269 if (s
->titles
== NULL
)
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
);
282 /* Resize screen with options. */
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
,
301 if (sx
!= screen_size_x(s
)) {
303 screen_reset_tabs(s
);
307 if (sy
!= screen_size_y(s
))
308 screen_resize_y(s
, sy
, eat_empty
, &cy
);
314 screen_reflow(s
, sx
, &cx
, &cy
, cursor
);
317 if (cy
>= s
->grid
->hsize
) {
319 s
->cy
= cy
- s
->grid
->hsize
;
325 log_debug("%s: cursor finished at %u,%u = %u,%u", __func__
, s
->cx
,
328 if (s
->write_list
!= NULL
)
329 screen_write_make_list(s
);
334 screen_resize(struct screen
*s
, u_int sx
, u_int sy
, int reflow
)
336 screen_resize_cursor(s
, sx
, sy
, reflow
, 1, 1);
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
;
347 oldy
= screen_size_y(s
);
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. */
364 /* Delete as many lines as possible from the bottom. */
366 available
= oldy
- 1 - s
->cy
;
368 if (available
> needed
)
370 grid_view_delete_lines(gd
, oldy
- 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.
382 if (gd
->flags
& GRID_HISTORY
) {
383 gd
->hscrolled
+= needed
;
385 } else if (needed
> 0 && available
> 0) {
386 if (available
> needed
)
388 grid_view_delete_lines(gd
, 0, available
, 8);
393 /* Resize line array. */
394 grid_adjust_lines(gd
, gd
->hsize
+ sy
);
396 /* Size increasing. */
401 * Try to pull as much as possible out of scrolled history, if
404 available
= gd
->hscrolled
;
405 if (gd
->flags
& GRID_HISTORY
&& available
> 0) {
406 if (available
> needed
)
408 gd
->hscrolled
-= available
;
409 gd
->hsize
-= 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. */
422 s
->rlower
= screen_size_y(s
) - 1;
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
)
431 s
->sel
= xcalloc(1, sizeof *s
->sel
);
433 memcpy(&s
->sel
->cell
, gc
, sizeof s
->sel
->cell
);
435 s
->sel
->rectangle
= rectangle
;
436 s
->sel
->modekeys
= modekeys
;
444 /* Clear selection. */
446 screen_clear_selection(struct screen
*s
)
452 /* Hide selection. */
454 screen_hide_selection(struct screen
*s
)
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
;
467 if (sel
== NULL
|| sel
->hidden
)
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
)
475 } else if (sel
->sy
> sel
->ey
) {
476 /* start line > end line -- upward selection. */
477 if (py
> sel
->sy
|| py
< sel
->ey
)
480 /* starting line == ending line. */
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. */
498 /* Selection start (sx) is on the left. */
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
)
515 if (py
== sel
->sy
&& px
< sel
->sx
)
518 if (sel
->modekeys
== MODEKEY_EMACS
)
519 xx
= (sel
->ex
== 0 ? 0 : sel
->ex
- 1);
522 if (py
== sel
->ey
&& px
> xx
)
524 } else if (sel
->sy
> sel
->ey
) {
525 /* starting line > ending line -- upward selection. */
526 if (py
> sel
->sy
|| py
< sel
->ey
)
529 if (py
== sel
->ey
&& px
< sel
->ex
)
532 if (sel
->modekeys
== MODEKEY_EMACS
)
536 if (py
== sel
->sy
&& (sel
->sx
== 0 || px
> xx
))
539 /* starting line == ending line. */
543 if (sel
->ex
< sel
->sx
) {
544 /* cursor (ex) is on the left */
545 if (sel
->modekeys
== MODEKEY_EMACS
)
549 if (px
> xx
|| px
< sel
->ex
)
552 /* selection start (sx) is on the left */
553 if (sel
->modekeys
== MODEKEY_EMACS
)
554 xx
= (sel
->ex
== 0 ? 0 : sel
->ex
- 1);
557 if (px
< sel
->sx
|| px
> xx
)
566 /* Get selected grid cell. */
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
)
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. */
584 screen_reflow(struct screen
*s
, u_int new_x
, u_int
*cx
, u_int
*cy
, int 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
,
594 grid_reflow(s
->grid
, new_x
);
597 grid_unwrap_position(s
->grid
, cx
, cy
, wx
, wy
);
598 log_debug("%s: new cursor is %u,%u", __func__
, *cx
, *cy
);
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.
611 screen_alternate_on(struct screen
*s
, struct grid_cell
*gc
, int cursor
)
615 if (s
->saved_grid
!= NULL
)
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
);
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. */
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
) {
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;
667 /* Restore the saved grid. */
668 grid_duplicate_lines(s
->grid
, screen_hsize(s
), s
->saved_grid
, 0,
672 * Turn history back on (so resize can use it) and then resize back to
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. */
690 screen_mode_to_string(int mode
)
692 static char tmp
[1024];
696 if (mode
== ALL_MODES
)
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';