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
;
88 s
->default_ccolour
= -1;
97 /* Reinitialise screen. */
99 screen_reinit(struct screen
*s
)
105 s
->rlower
= screen_size_y(s
) - 1;
107 s
->mode
= MODE_CURSOR
|MODE_WRAP
;
108 if (options_get_number(global_options
, "extended-keys") == 2)
109 s
->mode
|= MODE_KEXTENDED
;
111 if (s
->saved_grid
!= NULL
)
112 screen_alternate_off(s
, NULL
, 0);
113 s
->saved_cx
= UINT_MAX
;
114 s
->saved_cy
= UINT_MAX
;
116 screen_reset_tabs(s
);
118 grid_clear_lines(s
->grid
, s
->grid
->hsize
, s
->grid
->sy
, 8);
120 screen_clear_selection(s
);
121 screen_free_titles(s
);
124 /* Destroy a screen. */
126 screen_free(struct screen
*s
)
133 if (s
->write_list
!= NULL
)
134 screen_write_free_list(s
);
136 if (s
->saved_grid
!= NULL
)
137 grid_destroy(s
->saved_grid
);
138 grid_destroy(s
->grid
);
140 screen_free_titles(s
);
143 /* Reset tabs to default, eight spaces apart. */
145 screen_reset_tabs(struct screen
*s
)
151 if ((s
->tabs
= bit_alloc(screen_size_x(s
))) == NULL
)
152 fatal("bit_alloc failed");
153 for (i
= 8; i
< screen_size_x(s
); i
+= 8)
157 /* Set screen cursor style and mode. */
159 screen_set_cursor_style(u_int style
, enum screen_cursor_style
*cstyle
,
164 *cstyle
= SCREEN_CURSOR_DEFAULT
;
167 *cstyle
= SCREEN_CURSOR_BLOCK
;
168 *mode
|= MODE_CURSOR_BLINKING
;
171 *cstyle
= SCREEN_CURSOR_BLOCK
;
172 *mode
&= ~MODE_CURSOR_BLINKING
;
175 *cstyle
= SCREEN_CURSOR_UNDERLINE
;
176 *mode
|= MODE_CURSOR_BLINKING
;
179 *cstyle
= SCREEN_CURSOR_UNDERLINE
;
180 *mode
&= ~MODE_CURSOR_BLINKING
;
183 *cstyle
= SCREEN_CURSOR_BAR
;
184 *mode
|= MODE_CURSOR_BLINKING
;
187 *cstyle
= SCREEN_CURSOR_BAR
;
188 *mode
&= ~MODE_CURSOR_BLINKING
;
193 /* Set screen cursor colour. */
195 screen_set_cursor_colour(struct screen
*s
, int colour
)
200 /* Set screen title. */
202 screen_set_title(struct screen
*s
, const char *title
)
204 if (!utf8_isvalid(title
))
207 s
->title
= xstrdup(title
);
211 /* Set screen path. */
213 screen_set_path(struct screen
*s
, const char *path
)
216 utf8_stravis(&s
->path
, path
, VIS_OCTAL
|VIS_CSTYLE
|VIS_TAB
|VIS_NL
);
219 /* Push the current title onto the stack. */
221 screen_push_title(struct screen
*s
)
223 struct screen_title_entry
*title_entry
;
225 if (s
->titles
== NULL
) {
226 s
->titles
= xmalloc(sizeof *s
->titles
);
227 TAILQ_INIT(s
->titles
);
229 title_entry
= xmalloc(sizeof *title_entry
);
230 title_entry
->text
= xstrdup(s
->title
);
231 TAILQ_INSERT_HEAD(s
->titles
, title_entry
, entry
);
235 * Pop a title from the stack and set it as the screen title. If the stack is
239 screen_pop_title(struct screen
*s
)
241 struct screen_title_entry
*title_entry
;
243 if (s
->titles
== NULL
)
246 title_entry
= TAILQ_FIRST(s
->titles
);
247 if (title_entry
!= NULL
) {
248 screen_set_title(s
, title_entry
->text
);
250 TAILQ_REMOVE(s
->titles
, title_entry
, entry
);
251 free(title_entry
->text
);
256 /* Resize screen with options. */
258 screen_resize_cursor(struct screen
*s
, u_int sx
, u_int sy
, int reflow
,
259 int eat_empty
, int cursor
)
261 u_int cx
= s
->cx
, cy
= s
->grid
->hsize
+ s
->cy
;
263 if (s
->write_list
!= NULL
)
264 screen_write_free_list(s
);
266 log_debug("%s: new size %ux%u, now %ux%u (cursor %u,%u = %u,%u)",
267 __func__
, sx
, sy
, screen_size_x(s
), screen_size_y(s
), s
->cx
, s
->cy
,
275 if (sx
!= screen_size_x(s
)) {
277 screen_reset_tabs(s
);
281 if (sy
!= screen_size_y(s
))
282 screen_resize_y(s
, sy
, eat_empty
, &cy
);
285 screen_reflow(s
, sx
, &cx
, &cy
, cursor
);
287 if (cy
>= s
->grid
->hsize
) {
289 s
->cy
= cy
- s
->grid
->hsize
;
295 log_debug("%s: cursor finished at %u,%u = %u,%u", __func__
, s
->cx
,
298 if (s
->write_list
!= NULL
)
299 screen_write_make_list(s
);
304 screen_resize(struct screen
*s
, u_int sx
, u_int sy
, int reflow
)
306 screen_resize_cursor(s
, sx
, sy
, reflow
, 1, 1);
310 screen_resize_y(struct screen
*s
, u_int sy
, int eat_empty
, u_int
*cy
)
312 struct grid
*gd
= s
->grid
;
313 u_int needed
, available
, oldy
, i
;
317 oldy
= screen_size_y(s
);
322 * If the height is decreasing, delete lines from the bottom until
323 * hitting the cursor, then push lines from the top into the history.
325 * When increasing, pull as many lines as possible from scrolled
326 * history (not explicitly cleared from view) to the top, then fill the
327 * remaining with blanks at the bottom.
330 /* Size decreasing. */
334 /* Delete as many lines as possible from the bottom. */
336 available
= oldy
- 1 - s
->cy
;
338 if (available
> needed
)
340 grid_view_delete_lines(gd
, oldy
- available
,
347 * Now just increase the history size, if possible, to take
348 * over the lines which are left. If history is off, delete
349 * lines from the top.
352 if (gd
->flags
& GRID_HISTORY
) {
353 gd
->hscrolled
+= needed
;
355 } else if (needed
> 0 && available
> 0) {
356 if (available
> needed
)
358 grid_view_delete_lines(gd
, 0, available
, 8);
363 /* Resize line array. */
364 grid_adjust_lines(gd
, gd
->hsize
+ sy
);
366 /* Size increasing. */
371 * Try to pull as much as possible out of scrolled history, if
374 available
= gd
->hscrolled
;
375 if (gd
->flags
& GRID_HISTORY
&& available
> 0) {
376 if (available
> needed
)
378 gd
->hscrolled
-= available
;
379 gd
->hsize
-= available
;
384 /* Then fill the rest in with blanks. */
385 for (i
= gd
->hsize
+ sy
- needed
; i
< gd
->hsize
+ sy
; i
++)
386 grid_empty_line(gd
, i
, 8);
389 /* Set the new size, and reset the scroll region. */
392 s
->rlower
= screen_size_y(s
) - 1;
397 screen_set_selection(struct screen
*s
, u_int sx
, u_int sy
,
398 u_int ex
, u_int ey
, u_int rectangle
, int modekeys
, struct grid_cell
*gc
)
401 s
->sel
= xcalloc(1, sizeof *s
->sel
);
403 memcpy(&s
->sel
->cell
, gc
, sizeof s
->sel
->cell
);
405 s
->sel
->rectangle
= rectangle
;
406 s
->sel
->modekeys
= modekeys
;
414 /* Clear selection. */
416 screen_clear_selection(struct screen
*s
)
422 /* Hide selection. */
424 screen_hide_selection(struct screen
*s
)
430 /* Check if cell in selection. */
432 screen_check_selection(struct screen
*s
, u_int px
, u_int py
)
434 struct screen_sel
*sel
= s
->sel
;
437 if (sel
== NULL
|| sel
->hidden
)
440 if (sel
->rectangle
) {
441 if (sel
->sy
< sel
->ey
) {
442 /* start line < end line -- downward selection. */
443 if (py
< sel
->sy
|| py
> sel
->ey
)
445 } else if (sel
->sy
> sel
->ey
) {
446 /* start line > end line -- upward selection. */
447 if (py
> sel
->sy
|| py
< sel
->ey
)
450 /* starting line == ending line. */
456 * Need to include the selection start row, but not the cursor
457 * row, which means the selection changes depending on which
458 * one is on the left.
460 if (sel
->ex
< sel
->sx
) {
461 /* Cursor (ex) is on the left. */
468 /* Selection start (sx) is on the left. */
477 * Like emacs, keep the top-left-most character, and drop the
478 * bottom-right-most, regardless of copy direction.
480 if (sel
->sy
< sel
->ey
) {
481 /* starting line < ending line -- downward selection. */
482 if (py
< sel
->sy
|| py
> sel
->ey
)
485 if (py
== sel
->sy
&& px
< sel
->sx
)
488 if (sel
->modekeys
== MODEKEY_EMACS
)
489 xx
= (sel
->ex
== 0 ? 0 : sel
->ex
- 1);
492 if (py
== sel
->ey
&& px
> xx
)
494 } else if (sel
->sy
> sel
->ey
) {
495 /* starting line > ending line -- upward selection. */
496 if (py
> sel
->sy
|| py
< sel
->ey
)
499 if (py
== sel
->ey
&& px
< sel
->ex
)
502 if (sel
->modekeys
== MODEKEY_EMACS
)
506 if (py
== sel
->sy
&& (sel
->sx
== 0 || px
> xx
))
509 /* starting line == ending line. */
513 if (sel
->ex
< sel
->sx
) {
514 /* cursor (ex) is on the left */
515 if (sel
->modekeys
== MODEKEY_EMACS
)
519 if (px
> xx
|| px
< sel
->ex
)
522 /* selection start (sx) is on the left */
523 if (sel
->modekeys
== MODEKEY_EMACS
)
524 xx
= (sel
->ex
== 0 ? 0 : sel
->ex
- 1);
527 if (px
< sel
->sx
|| px
> xx
)
536 /* Get selected grid cell. */
538 screen_select_cell(struct screen
*s
, struct grid_cell
*dst
,
539 const struct grid_cell
*src
)
541 if (s
->sel
== NULL
|| s
->sel
->hidden
)
544 memcpy(dst
, &s
->sel
->cell
, sizeof *dst
);
546 utf8_copy(&dst
->data
, &src
->data
);
547 dst
->attr
= dst
->attr
& ~GRID_ATTR_CHARSET
;
548 dst
->attr
|= src
->attr
& GRID_ATTR_CHARSET
;
549 dst
->flags
= src
->flags
;
552 /* Reflow wrapped lines. */
554 screen_reflow(struct screen
*s
, u_int new_x
, u_int
*cx
, u_int
*cy
, int cursor
)
559 grid_wrap_position(s
->grid
, *cx
, *cy
, &wx
, &wy
);
560 log_debug("%s: cursor %u,%u is %u,%u", __func__
, *cx
, *cy
, wx
,
564 grid_reflow(s
->grid
, new_x
);
567 grid_unwrap_position(s
->grid
, cx
, cy
, wx
, wy
);
568 log_debug("%s: new cursor is %u,%u", __func__
, *cx
, *cy
);
572 *cy
= s
->grid
->hsize
;
577 * Enter alternative screen mode. A copy of the visible screen is saved and the
578 * history is not updated.
581 screen_alternate_on(struct screen
*s
, struct grid_cell
*gc
, int cursor
)
585 if (s
->saved_grid
!= NULL
)
587 sx
= screen_size_x(s
);
588 sy
= screen_size_y(s
);
590 s
->saved_grid
= grid_create(sx
, sy
, 0);
591 grid_duplicate_lines(s
->saved_grid
, 0, s
->grid
, screen_hsize(s
), sy
);
596 memcpy(&s
->saved_cell
, gc
, sizeof s
->saved_cell
);
598 grid_view_clear(s
->grid
, 0, 0, sx
, sy
, 8);
600 s
->saved_flags
= s
->grid
->flags
;
601 s
->grid
->flags
&= ~GRID_HISTORY
;
604 /* Exit alternate screen mode and restore the copied grid. */
606 screen_alternate_off(struct screen
*s
, struct grid_cell
*gc
, int cursor
)
608 u_int sx
= screen_size_x(s
), sy
= screen_size_y(s
);
611 * If the current size is different, temporarily resize to the old size
612 * before copying back.
614 if (s
->saved_grid
!= NULL
)
615 screen_resize(s
, s
->saved_grid
->sx
, s
->saved_grid
->sy
, 1);
618 * Restore the cursor position and cell. This happens even if not
619 * currently in the alternate screen.
621 if (cursor
&& s
->saved_cx
!= UINT_MAX
&& s
->saved_cy
!= UINT_MAX
) {
625 memcpy(gc
, &s
->saved_cell
, sizeof *gc
);
628 /* If not in the alternate screen, do nothing more. */
629 if (s
->saved_grid
== NULL
) {
630 if (s
->cx
> screen_size_x(s
) - 1)
631 s
->cx
= screen_size_x(s
) - 1;
632 if (s
->cy
> screen_size_y(s
) - 1)
633 s
->cy
= screen_size_y(s
) - 1;
637 /* Restore the saved grid. */
638 grid_duplicate_lines(s
->grid
, screen_hsize(s
), s
->saved_grid
, 0,
642 * Turn history back on (so resize can use it) and then resize back to
645 if (s
->saved_flags
& GRID_HISTORY
)
646 s
->grid
->flags
|= GRID_HISTORY
;
647 screen_resize(s
, sx
, sy
, 1);
649 grid_destroy(s
->saved_grid
);
650 s
->saved_grid
= NULL
;
652 if (s
->cx
> screen_size_x(s
) - 1)
653 s
->cx
= screen_size_x(s
) - 1;
654 if (s
->cy
> screen_size_y(s
) - 1)
655 s
->cy
= screen_size_y(s
) - 1;
658 /* Get mode as a string. */
660 screen_mode_to_string(int mode
)
662 static char tmp
[1024];
666 if (mode
== ALL_MODES
)
670 if (mode
& MODE_CURSOR
)
671 strlcat(tmp
, "CURSOR,", sizeof tmp
);
672 if (mode
& MODE_INSERT
)
673 strlcat(tmp
, "INSERT,", sizeof tmp
);
674 if (mode
& MODE_KCURSOR
)
675 strlcat(tmp
, "KCURSOR,", sizeof tmp
);
676 if (mode
& MODE_KKEYPAD
)
677 strlcat(tmp
, "KKEYPAD,", sizeof tmp
);
678 if (mode
& MODE_WRAP
)
679 strlcat(tmp
, "WRAP,", sizeof tmp
);
680 if (mode
& MODE_MOUSE_STANDARD
)
681 strlcat(tmp
, "MOUSE_STANDARD,", sizeof tmp
);
682 if (mode
& MODE_MOUSE_BUTTON
)
683 strlcat(tmp
, "MOUSE_BUTTON,", sizeof tmp
);
684 if (mode
& MODE_CURSOR_BLINKING
)
685 strlcat(tmp
, "CURSOR_BLINKING,", sizeof tmp
);
686 if (mode
& MODE_CURSOR_VERY_VISIBLE
)
687 strlcat(tmp
, "CURSOR_VERY_VISIBLE,", sizeof tmp
);
688 if (mode
& MODE_MOUSE_UTF8
)
689 strlcat(tmp
, "UTF8,", sizeof tmp
);
690 if (mode
& MODE_MOUSE_SGR
)
691 strlcat(tmp
, "SGR,", sizeof tmp
);
692 if (mode
& MODE_BRACKETPASTE
)
693 strlcat(tmp
, "BRACKETPASTE,", sizeof tmp
);
694 if (mode
& MODE_FOCUSON
)
695 strlcat(tmp
, "FOCUSON,", sizeof tmp
);
696 if (mode
& MODE_MOUSE_ALL
)
697 strlcat(tmp
, "MOUSE_ALL,", sizeof tmp
);
698 if (mode
& MODE_ORIGIN
)
699 strlcat(tmp
, "ORIGIN,", sizeof tmp
);
700 if (mode
& MODE_CRLF
)
701 strlcat(tmp
, "CRLF,", sizeof tmp
);
702 if (mode
& MODE_KEXTENDED
)
703 strlcat(tmp
, "KEXTENDED,", sizeof tmp
);
704 tmp
[strlen(tmp
) - 1] = '\0';