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;
96 /* Reinitialise screen. */
98 screen_reinit(struct screen
*s
)
104 s
->rlower
= screen_size_y(s
) - 1;
106 s
->mode
= MODE_CURSOR
|MODE_WRAP
;
107 if (options_get_number(global_options
, "extended-keys") == 2)
108 s
->mode
|= MODE_KEXTENDED
;
110 if (s
->saved_grid
!= NULL
)
111 screen_alternate_off(s
, NULL
, 0);
112 s
->saved_cx
= UINT_MAX
;
113 s
->saved_cy
= UINT_MAX
;
115 screen_reset_tabs(s
);
117 grid_clear_lines(s
->grid
, s
->grid
->hsize
, s
->grid
->sy
, 8);
119 screen_clear_selection(s
);
120 screen_free_titles(s
);
123 /* Destroy a screen. */
125 screen_free(struct screen
*s
)
132 if (s
->write_list
!= NULL
)
133 screen_write_free_list(s
);
135 if (s
->saved_grid
!= NULL
)
136 grid_destroy(s
->saved_grid
);
137 grid_destroy(s
->grid
);
139 screen_free_titles(s
);
142 /* Reset tabs to default, eight spaces apart. */
144 screen_reset_tabs(struct screen
*s
)
150 if ((s
->tabs
= bit_alloc(screen_size_x(s
))) == NULL
)
151 fatal("bit_alloc failed");
152 for (i
= 8; i
< screen_size_x(s
); i
+= 8)
156 /* Set screen cursor style and mode. */
158 screen_set_cursor_style(u_int style
, enum screen_cursor_style
*cstyle
,
163 *cstyle
= SCREEN_CURSOR_DEFAULT
;
166 *cstyle
= SCREEN_CURSOR_BLOCK
;
167 *mode
|= MODE_CURSOR_BLINKING
;
170 *cstyle
= SCREEN_CURSOR_BLOCK
;
171 *mode
&= ~MODE_CURSOR_BLINKING
;
174 *cstyle
= SCREEN_CURSOR_UNDERLINE
;
175 *mode
|= MODE_CURSOR_BLINKING
;
178 *cstyle
= SCREEN_CURSOR_UNDERLINE
;
179 *mode
&= ~MODE_CURSOR_BLINKING
;
182 *cstyle
= SCREEN_CURSOR_BAR
;
183 *mode
|= MODE_CURSOR_BLINKING
;
186 *cstyle
= SCREEN_CURSOR_BAR
;
187 *mode
&= ~MODE_CURSOR_BLINKING
;
192 /* Set screen cursor colour. */
194 screen_set_cursor_colour(struct screen
*s
, int colour
)
199 /* Set screen title. */
201 screen_set_title(struct screen
*s
, const char *title
)
203 if (!utf8_isvalid(title
))
206 s
->title
= xstrdup(title
);
210 /* Set screen path. */
212 screen_set_path(struct screen
*s
, const char *path
)
215 utf8_stravis(&s
->path
, path
, VIS_OCTAL
|VIS_CSTYLE
|VIS_TAB
|VIS_NL
);
218 /* Push the current title onto the stack. */
220 screen_push_title(struct screen
*s
)
222 struct screen_title_entry
*title_entry
;
224 if (s
->titles
== NULL
) {
225 s
->titles
= xmalloc(sizeof *s
->titles
);
226 TAILQ_INIT(s
->titles
);
228 title_entry
= xmalloc(sizeof *title_entry
);
229 title_entry
->text
= xstrdup(s
->title
);
230 TAILQ_INSERT_HEAD(s
->titles
, title_entry
, entry
);
234 * Pop a title from the stack and set it as the screen title. If the stack is
238 screen_pop_title(struct screen
*s
)
240 struct screen_title_entry
*title_entry
;
242 if (s
->titles
== NULL
)
245 title_entry
= TAILQ_FIRST(s
->titles
);
246 if (title_entry
!= NULL
) {
247 screen_set_title(s
, title_entry
->text
);
249 TAILQ_REMOVE(s
->titles
, title_entry
, entry
);
250 free(title_entry
->text
);
255 /* Resize screen with options. */
257 screen_resize_cursor(struct screen
*s
, u_int sx
, u_int sy
, int reflow
,
258 int eat_empty
, int cursor
)
260 u_int cx
= s
->cx
, cy
= s
->grid
->hsize
+ s
->cy
;
262 if (s
->write_list
!= NULL
)
263 screen_write_free_list(s
);
265 log_debug("%s: new size %ux%u, now %ux%u (cursor %u,%u = %u,%u)",
266 __func__
, sx
, sy
, screen_size_x(s
), screen_size_y(s
), s
->cx
, s
->cy
,
274 if (sx
!= screen_size_x(s
)) {
276 screen_reset_tabs(s
);
280 if (sy
!= screen_size_y(s
))
281 screen_resize_y(s
, sy
, eat_empty
, &cy
);
284 screen_reflow(s
, sx
, &cx
, &cy
, cursor
);
286 if (cy
>= s
->grid
->hsize
) {
288 s
->cy
= cy
- s
->grid
->hsize
;
294 log_debug("%s: cursor finished at %u,%u = %u,%u", __func__
, s
->cx
,
297 if (s
->write_list
!= NULL
)
298 screen_write_make_list(s
);
303 screen_resize(struct screen
*s
, u_int sx
, u_int sy
, int reflow
)
305 screen_resize_cursor(s
, sx
, sy
, reflow
, 1, 1);
309 screen_resize_y(struct screen
*s
, u_int sy
, int eat_empty
, u_int
*cy
)
311 struct grid
*gd
= s
->grid
;
312 u_int needed
, available
, oldy
, i
;
316 oldy
= screen_size_y(s
);
321 * If the height is decreasing, delete lines from the bottom until
322 * hitting the cursor, then push lines from the top into the history.
324 * When increasing, pull as many lines as possible from scrolled
325 * history (not explicitly cleared from view) to the top, then fill the
326 * remaining with blanks at the bottom.
329 /* Size decreasing. */
333 /* Delete as many lines as possible from the bottom. */
335 available
= oldy
- 1 - s
->cy
;
337 if (available
> needed
)
339 grid_view_delete_lines(gd
, oldy
- available
,
346 * Now just increase the history size, if possible, to take
347 * over the lines which are left. If history is off, delete
348 * lines from the top.
351 if (gd
->flags
& GRID_HISTORY
) {
352 gd
->hscrolled
+= needed
;
354 } else if (needed
> 0 && available
> 0) {
355 if (available
> needed
)
357 grid_view_delete_lines(gd
, 0, available
, 8);
362 /* Resize line array. */
363 grid_adjust_lines(gd
, gd
->hsize
+ sy
);
365 /* Size increasing. */
370 * Try to pull as much as possible out of scrolled history, if
373 available
= gd
->hscrolled
;
374 if (gd
->flags
& GRID_HISTORY
&& available
> 0) {
375 if (available
> needed
)
377 gd
->hscrolled
-= available
;
378 gd
->hsize
-= available
;
383 /* Then fill the rest in with blanks. */
384 for (i
= gd
->hsize
+ sy
- needed
; i
< gd
->hsize
+ sy
; i
++)
385 grid_empty_line(gd
, i
, 8);
388 /* Set the new size, and reset the scroll region. */
391 s
->rlower
= screen_size_y(s
) - 1;
396 screen_set_selection(struct screen
*s
, u_int sx
, u_int sy
,
397 u_int ex
, u_int ey
, u_int rectangle
, int modekeys
, struct grid_cell
*gc
)
400 s
->sel
= xcalloc(1, sizeof *s
->sel
);
402 memcpy(&s
->sel
->cell
, gc
, sizeof s
->sel
->cell
);
404 s
->sel
->rectangle
= rectangle
;
405 s
->sel
->modekeys
= modekeys
;
413 /* Clear selection. */
415 screen_clear_selection(struct screen
*s
)
421 /* Hide selection. */
423 screen_hide_selection(struct screen
*s
)
429 /* Check if cell in selection. */
431 screen_check_selection(struct screen
*s
, u_int px
, u_int py
)
433 struct screen_sel
*sel
= s
->sel
;
436 if (sel
== NULL
|| sel
->hidden
)
439 if (sel
->rectangle
) {
440 if (sel
->sy
< sel
->ey
) {
441 /* start line < end line -- downward selection. */
442 if (py
< sel
->sy
|| py
> sel
->ey
)
444 } else if (sel
->sy
> sel
->ey
) {
445 /* start line > end line -- upward selection. */
446 if (py
> sel
->sy
|| py
< sel
->ey
)
449 /* starting line == ending line. */
455 * Need to include the selection start row, but not the cursor
456 * row, which means the selection changes depending on which
457 * one is on the left.
459 if (sel
->ex
< sel
->sx
) {
460 /* Cursor (ex) is on the left. */
467 /* Selection start (sx) is on the left. */
476 * Like emacs, keep the top-left-most character, and drop the
477 * bottom-right-most, regardless of copy direction.
479 if (sel
->sy
< sel
->ey
) {
480 /* starting line < ending line -- downward selection. */
481 if (py
< sel
->sy
|| py
> sel
->ey
)
484 if (py
== sel
->sy
&& px
< sel
->sx
)
487 if (sel
->modekeys
== MODEKEY_EMACS
)
488 xx
= (sel
->ex
== 0 ? 0 : sel
->ex
- 1);
491 if (py
== sel
->ey
&& px
> xx
)
493 } else if (sel
->sy
> sel
->ey
) {
494 /* starting line > ending line -- upward selection. */
495 if (py
> sel
->sy
|| py
< sel
->ey
)
498 if (py
== sel
->ey
&& px
< sel
->ex
)
501 if (sel
->modekeys
== MODEKEY_EMACS
)
505 if (py
== sel
->sy
&& (sel
->sx
== 0 || px
> xx
))
508 /* starting line == ending line. */
512 if (sel
->ex
< sel
->sx
) {
513 /* cursor (ex) is on the left */
514 if (sel
->modekeys
== MODEKEY_EMACS
)
518 if (px
> xx
|| px
< sel
->ex
)
521 /* selection start (sx) is on the left */
522 if (sel
->modekeys
== MODEKEY_EMACS
)
523 xx
= (sel
->ex
== 0 ? 0 : sel
->ex
- 1);
526 if (px
< sel
->sx
|| px
> xx
)
535 /* Get selected grid cell. */
537 screen_select_cell(struct screen
*s
, struct grid_cell
*dst
,
538 const struct grid_cell
*src
)
540 if (s
->sel
== NULL
|| s
->sel
->hidden
)
543 memcpy(dst
, &s
->sel
->cell
, sizeof *dst
);
545 utf8_copy(&dst
->data
, &src
->data
);
546 dst
->attr
= dst
->attr
& ~GRID_ATTR_CHARSET
;
547 dst
->attr
|= src
->attr
& GRID_ATTR_CHARSET
;
548 dst
->flags
= src
->flags
;
551 /* Reflow wrapped lines. */
553 screen_reflow(struct screen
*s
, u_int new_x
, u_int
*cx
, u_int
*cy
, int cursor
)
558 grid_wrap_position(s
->grid
, *cx
, *cy
, &wx
, &wy
);
559 log_debug("%s: cursor %u,%u is %u,%u", __func__
, *cx
, *cy
, wx
,
563 grid_reflow(s
->grid
, new_x
);
566 grid_unwrap_position(s
->grid
, cx
, cy
, wx
, wy
);
567 log_debug("%s: new cursor is %u,%u", __func__
, *cx
, *cy
);
571 *cy
= s
->grid
->hsize
;
576 * Enter alternative screen mode. A copy of the visible screen is saved and the
577 * history is not updated.
580 screen_alternate_on(struct screen
*s
, struct grid_cell
*gc
, int cursor
)
584 if (s
->saved_grid
!= NULL
)
586 sx
= screen_size_x(s
);
587 sy
= screen_size_y(s
);
589 s
->saved_grid
= grid_create(sx
, sy
, 0);
590 grid_duplicate_lines(s
->saved_grid
, 0, s
->grid
, screen_hsize(s
), sy
);
595 memcpy(&s
->saved_cell
, gc
, sizeof s
->saved_cell
);
597 grid_view_clear(s
->grid
, 0, 0, sx
, sy
, 8);
599 s
->saved_flags
= s
->grid
->flags
;
600 s
->grid
->flags
&= ~GRID_HISTORY
;
603 /* Exit alternate screen mode and restore the copied grid. */
605 screen_alternate_off(struct screen
*s
, struct grid_cell
*gc
, int cursor
)
607 u_int sx
= screen_size_x(s
), sy
= screen_size_y(s
);
610 * If the current size is different, temporarily resize to the old size
611 * before copying back.
613 if (s
->saved_grid
!= NULL
)
614 screen_resize(s
, s
->saved_grid
->sx
, s
->saved_grid
->sy
, 1);
617 * Restore the cursor position and cell. This happens even if not
618 * currently in the alternate screen.
620 if (cursor
&& s
->saved_cx
!= UINT_MAX
&& s
->saved_cy
!= UINT_MAX
) {
624 memcpy(gc
, &s
->saved_cell
, sizeof *gc
);
627 /* If not in the alternate screen, do nothing more. */
628 if (s
->saved_grid
== NULL
) {
629 if (s
->cx
> screen_size_x(s
) - 1)
630 s
->cx
= screen_size_x(s
) - 1;
631 if (s
->cy
> screen_size_y(s
) - 1)
632 s
->cy
= screen_size_y(s
) - 1;
636 /* Restore the saved grid. */
637 grid_duplicate_lines(s
->grid
, screen_hsize(s
), s
->saved_grid
, 0,
641 * Turn history back on (so resize can use it) and then resize back to
644 if (s
->saved_flags
& GRID_HISTORY
)
645 s
->grid
->flags
|= GRID_HISTORY
;
646 screen_resize(s
, sx
, sy
, 1);
648 grid_destroy(s
->saved_grid
);
649 s
->saved_grid
= NULL
;
651 if (s
->cx
> screen_size_x(s
) - 1)
652 s
->cx
= screen_size_x(s
) - 1;
653 if (s
->cy
> screen_size_y(s
) - 1)
654 s
->cy
= screen_size_y(s
) - 1;
657 /* Get mode as a string. */
659 screen_mode_to_string(int mode
)
661 static char tmp
[1024];
665 if (mode
== ALL_MODES
)
669 if (mode
& MODE_CURSOR
)
670 strlcat(tmp
, "CURSOR,", sizeof tmp
);
671 if (mode
& MODE_INSERT
)
672 strlcat(tmp
, "INSERT,", sizeof tmp
);
673 if (mode
& MODE_KCURSOR
)
674 strlcat(tmp
, "KCURSOR,", sizeof tmp
);
675 if (mode
& MODE_KKEYPAD
)
676 strlcat(tmp
, "KKEYPAD,", sizeof tmp
);
677 if (mode
& MODE_WRAP
)
678 strlcat(tmp
, "WRAP,", sizeof tmp
);
679 if (mode
& MODE_MOUSE_STANDARD
)
680 strlcat(tmp
, "MOUSE_STANDARD,", sizeof tmp
);
681 if (mode
& MODE_MOUSE_BUTTON
)
682 strlcat(tmp
, "MOUSE_BUTTON,", sizeof tmp
);
683 if (mode
& MODE_CURSOR_BLINKING
)
684 strlcat(tmp
, "CURSOR_BLINKING,", sizeof tmp
);
685 if (mode
& MODE_CURSOR_VERY_VISIBLE
)
686 strlcat(tmp
, "CURSOR_VERY_VISIBLE,", sizeof tmp
);
687 if (mode
& MODE_MOUSE_UTF8
)
688 strlcat(tmp
, "UTF8,", sizeof tmp
);
689 if (mode
& MODE_MOUSE_SGR
)
690 strlcat(tmp
, "SGR,", sizeof tmp
);
691 if (mode
& MODE_BRACKETPASTE
)
692 strlcat(tmp
, "BRACKETPASTE,", sizeof tmp
);
693 if (mode
& MODE_FOCUSON
)
694 strlcat(tmp
, "FOCUSON,", sizeof tmp
);
695 if (mode
& MODE_MOUSE_ALL
)
696 strlcat(tmp
, "MOUSE_ALL,", sizeof tmp
);
697 if (mode
& MODE_ORIGIN
)
698 strlcat(tmp
, "ORIGIN,", sizeof tmp
);
699 if (mode
& MODE_CRLF
)
700 strlcat(tmp
, "CRLF,", sizeof tmp
);
701 if (mode
& MODE_KEXTENDED
)
702 strlcat(tmp
, "KEXTENDED,", sizeof tmp
);
703 tmp
[strlen(tmp
) - 1] = '\0';