Home | History | Annotate | Line # | Download | only in dist
screen.c revision 1.1.1.16
      1   1.1.1.5  christos /* $OpenBSD$ */
      2       1.1      jmmv 
      3       1.1      jmmv /*
      4   1.1.1.6  christos  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott (at) gmail.com>
      5       1.1      jmmv  *
      6       1.1      jmmv  * Permission to use, copy, modify, and distribute this software for any
      7       1.1      jmmv  * purpose with or without fee is hereby granted, provided that the above
      8       1.1      jmmv  * copyright notice and this permission notice appear in all copies.
      9       1.1      jmmv  *
     10       1.1      jmmv  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     11       1.1      jmmv  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     12       1.1      jmmv  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     13       1.1      jmmv  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     14       1.1      jmmv  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
     15       1.1      jmmv  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
     16       1.1      jmmv  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     17       1.1      jmmv  */
     18       1.1      jmmv 
     19       1.1      jmmv #include <sys/types.h>
     20       1.1      jmmv 
     21       1.1      jmmv #include <stdlib.h>
     22       1.1      jmmv #include <string.h>
     23       1.1      jmmv #include <unistd.h>
     24       1.1      jmmv 
     25       1.1      jmmv #include "tmux.h"
     26       1.1      jmmv 
     27   1.1.1.9  christos /* Selected area in screen. */
     28   1.1.1.9  christos struct screen_sel {
     29   1.1.1.9  christos 	int		 hidden;
     30   1.1.1.9  christos 	int		 rectangle;
     31   1.1.1.9  christos 	int		 modekeys;
     32   1.1.1.9  christos 
     33   1.1.1.9  christos 	u_int		 sx;
     34   1.1.1.9  christos 	u_int		 sy;
     35   1.1.1.9  christos 
     36   1.1.1.9  christos 	u_int		 ex;
     37   1.1.1.9  christos 	u_int		 ey;
     38   1.1.1.9  christos 
     39   1.1.1.9  christos 	struct grid_cell cell;
     40   1.1.1.9  christos };
     41   1.1.1.9  christos 
     42   1.1.1.9  christos /* Entry on title stack. */
     43   1.1.1.9  christos struct screen_title_entry {
     44   1.1.1.9  christos 	char				*text;
     45   1.1.1.9  christos 
     46   1.1.1.9  christos 	TAILQ_ENTRY(screen_title_entry)	 entry;
     47   1.1.1.9  christos };
     48   1.1.1.9  christos TAILQ_HEAD(screen_titles, screen_title_entry);
     49   1.1.1.9  christos 
     50  1.1.1.12  christos static void	screen_resize_y(struct screen *, u_int, int, u_int *);
     51  1.1.1.12  christos static void	screen_reflow(struct screen *, u_int, u_int *, u_int *, int);
     52       1.1      jmmv 
     53   1.1.1.9  christos /* Free titles stack. */
     54   1.1.1.9  christos static void
     55   1.1.1.9  christos screen_free_titles(struct screen *s)
     56   1.1.1.9  christos {
     57   1.1.1.9  christos 	struct screen_title_entry	*title_entry;
     58   1.1.1.9  christos 
     59   1.1.1.9  christos 	if (s->titles == NULL)
     60   1.1.1.9  christos 		return;
     61   1.1.1.9  christos 
     62   1.1.1.9  christos 	while ((title_entry = TAILQ_FIRST(s->titles)) != NULL) {
     63   1.1.1.9  christos 		TAILQ_REMOVE(s->titles, title_entry, entry);
     64   1.1.1.9  christos 		free(title_entry->text);
     65   1.1.1.9  christos 		free(title_entry);
     66   1.1.1.9  christos 	}
     67   1.1.1.9  christos 
     68   1.1.1.9  christos 	free(s->titles);
     69   1.1.1.9  christos 	s->titles = NULL;
     70   1.1.1.9  christos }
     71   1.1.1.9  christos 
     72       1.1      jmmv /* Create a new screen. */
     73       1.1      jmmv void
     74       1.1      jmmv screen_init(struct screen *s, u_int sx, u_int sy, u_int hlimit)
     75       1.1      jmmv {
     76       1.1      jmmv 	s->grid = grid_create(sx, sy, hlimit);
     77  1.1.1.12  christos 	s->saved_grid = NULL;
     78  1.1.1.12  christos 
     79   1.1.1.5  christos 	s->title = xstrdup("");
     80   1.1.1.9  christos 	s->titles = NULL;
     81  1.1.1.12  christos 	s->path = NULL;
     82       1.1      jmmv 
     83  1.1.1.14       wiz 	s->cstyle = SCREEN_CURSOR_DEFAULT;
     84  1.1.1.14       wiz 	s->default_cstyle = SCREEN_CURSOR_DEFAULT;
     85  1.1.1.15       wiz 	s->mode = MODE_CURSOR;
     86  1.1.1.14       wiz 	s->default_mode = 0;
     87  1.1.1.14       wiz 	s->ccolour = -1;
     88  1.1.1.14       wiz 	s->default_ccolour = -1;
     89       1.1      jmmv 	s->tabs = NULL;
     90   1.1.1.9  christos 	s->sel = NULL;
     91       1.1      jmmv 
     92  1.1.1.15       wiz #ifdef ENABLE_SIXEL
     93  1.1.1.15       wiz 	TAILQ_INIT(&s->images);
     94  1.1.1.15       wiz #endif
     95  1.1.1.15       wiz 
     96  1.1.1.12  christos 	s->write_list = NULL;
     97  1.1.1.15       wiz 	s->hyperlinks = NULL;
     98  1.1.1.12  christos 
     99       1.1      jmmv 	screen_reinit(s);
    100       1.1      jmmv }
    101       1.1      jmmv 
    102       1.1      jmmv /* Reinitialise screen. */
    103       1.1      jmmv void
    104       1.1      jmmv screen_reinit(struct screen *s)
    105       1.1      jmmv {
    106       1.1      jmmv 	s->cx = 0;
    107       1.1      jmmv 	s->cy = 0;
    108       1.1      jmmv 
    109       1.1      jmmv 	s->rupper = 0;
    110       1.1      jmmv 	s->rlower = screen_size_y(s) - 1;
    111       1.1      jmmv 
    112  1.1.1.14       wiz 	s->mode = MODE_CURSOR|MODE_WRAP|(s->mode & MODE_CRLF);
    113  1.1.1.16       wiz 
    114  1.1.1.13  christos 	if (options_get_number(global_options, "extended-keys") == 2)
    115  1.1.1.16       wiz 		s->mode = (s->mode & ~EXTENDED_KEY_MODES)|MODE_KEYS_EXTENDED;
    116       1.1      jmmv 
    117  1.1.1.12  christos 	if (s->saved_grid != NULL)
    118  1.1.1.12  christos 		screen_alternate_off(s, NULL, 0);
    119  1.1.1.12  christos 	s->saved_cx = UINT_MAX;
    120  1.1.1.12  christos 	s->saved_cy = UINT_MAX;
    121  1.1.1.12  christos 
    122       1.1      jmmv 	screen_reset_tabs(s);
    123       1.1      jmmv 
    124   1.1.1.7  christos 	grid_clear_lines(s->grid, s->grid->hsize, s->grid->sy, 8);
    125       1.1      jmmv 
    126       1.1      jmmv 	screen_clear_selection(s);
    127   1.1.1.9  christos 	screen_free_titles(s);
    128  1.1.1.15       wiz 
    129  1.1.1.15       wiz #ifdef ENABLE_SIXEL
    130  1.1.1.15       wiz 	image_free_all(s);
    131  1.1.1.15       wiz #endif
    132  1.1.1.15       wiz 
    133  1.1.1.15       wiz 	screen_reset_hyperlinks(s);
    134  1.1.1.15       wiz }
    135  1.1.1.15       wiz 
    136  1.1.1.15       wiz /* Reset hyperlinks of a screen. */
    137  1.1.1.15       wiz void
    138  1.1.1.15       wiz screen_reset_hyperlinks(struct screen *s)
    139  1.1.1.15       wiz {
    140  1.1.1.15       wiz 	if (s->hyperlinks == NULL)
    141  1.1.1.15       wiz 		s->hyperlinks = hyperlinks_init();
    142  1.1.1.15       wiz 	else
    143  1.1.1.15       wiz 		hyperlinks_reset(s->hyperlinks);
    144       1.1      jmmv }
    145       1.1      jmmv 
    146       1.1      jmmv /* Destroy a screen. */
    147       1.1      jmmv void
    148       1.1      jmmv screen_free(struct screen *s)
    149       1.1      jmmv {
    150   1.1.1.9  christos 	free(s->sel);
    151   1.1.1.3  christos 	free(s->tabs);
    152  1.1.1.12  christos 	free(s->path);
    153   1.1.1.3  christos 	free(s->title);
    154   1.1.1.9  christos 
    155  1.1.1.12  christos 	if (s->write_list != NULL)
    156  1.1.1.12  christos 		screen_write_free_list(s);
    157  1.1.1.12  christos 
    158  1.1.1.12  christos 	if (s->saved_grid != NULL)
    159  1.1.1.12  christos 		grid_destroy(s->saved_grid);
    160       1.1      jmmv 	grid_destroy(s->grid);
    161   1.1.1.9  christos 
    162  1.1.1.15       wiz 	if (s->hyperlinks != NULL)
    163  1.1.1.15       wiz 		hyperlinks_free(s->hyperlinks);
    164   1.1.1.9  christos 	screen_free_titles(s);
    165  1.1.1.15       wiz 
    166  1.1.1.15       wiz #ifdef ENABLE_SIXEL
    167  1.1.1.15       wiz 	image_free_all(s);
    168  1.1.1.15       wiz #endif
    169       1.1      jmmv }
    170       1.1      jmmv 
    171       1.1      jmmv /* Reset tabs to default, eight spaces apart. */
    172       1.1      jmmv void
    173       1.1      jmmv screen_reset_tabs(struct screen *s)
    174       1.1      jmmv {
    175       1.1      jmmv 	u_int	i;
    176       1.1      jmmv 
    177   1.1.1.3  christos 	free(s->tabs);
    178       1.1      jmmv 
    179       1.1      jmmv 	if ((s->tabs = bit_alloc(screen_size_x(s))) == NULL)
    180       1.1      jmmv 		fatal("bit_alloc failed");
    181       1.1      jmmv 	for (i = 8; i < screen_size_x(s); i += 8)
    182       1.1      jmmv 		bit_set(s->tabs, i);
    183       1.1      jmmv }
    184       1.1      jmmv 
    185  1.1.1.14       wiz /* Set screen cursor style and mode. */
    186   1.1.1.2      jmmv void
    187  1.1.1.14       wiz screen_set_cursor_style(u_int style, enum screen_cursor_style *cstyle,
    188  1.1.1.14       wiz     int *mode)
    189   1.1.1.2      jmmv {
    190  1.1.1.14       wiz 	switch (style) {
    191  1.1.1.14       wiz 	case 0:
    192  1.1.1.14       wiz 		*cstyle = SCREEN_CURSOR_DEFAULT;
    193  1.1.1.14       wiz 		break;
    194  1.1.1.14       wiz 	case 1:
    195  1.1.1.14       wiz 		*cstyle = SCREEN_CURSOR_BLOCK;
    196  1.1.1.14       wiz 		*mode |= MODE_CURSOR_BLINKING;
    197  1.1.1.14       wiz 		break;
    198  1.1.1.14       wiz 	case 2:
    199  1.1.1.14       wiz 		*cstyle = SCREEN_CURSOR_BLOCK;
    200  1.1.1.14       wiz 		*mode &= ~MODE_CURSOR_BLINKING;
    201  1.1.1.14       wiz 		break;
    202  1.1.1.14       wiz 	case 3:
    203  1.1.1.14       wiz 		*cstyle = SCREEN_CURSOR_UNDERLINE;
    204  1.1.1.14       wiz 		*mode |= MODE_CURSOR_BLINKING;
    205  1.1.1.14       wiz 		break;
    206  1.1.1.14       wiz 	case 4:
    207  1.1.1.14       wiz 		*cstyle = SCREEN_CURSOR_UNDERLINE;
    208  1.1.1.14       wiz 		*mode &= ~MODE_CURSOR_BLINKING;
    209  1.1.1.14       wiz 		break;
    210  1.1.1.14       wiz 	case 5:
    211  1.1.1.14       wiz 		*cstyle = SCREEN_CURSOR_BAR;
    212  1.1.1.14       wiz 		*mode |= MODE_CURSOR_BLINKING;
    213  1.1.1.14       wiz 		break;
    214  1.1.1.14       wiz 	case 6:
    215  1.1.1.14       wiz 		*cstyle = SCREEN_CURSOR_BAR;
    216  1.1.1.14       wiz 		*mode &= ~MODE_CURSOR_BLINKING;
    217  1.1.1.14       wiz 		break;
    218  1.1.1.12  christos 	}
    219   1.1.1.2      jmmv }
    220   1.1.1.2      jmmv 
    221   1.1.1.2      jmmv /* Set screen cursor colour. */
    222   1.1.1.2      jmmv void
    223  1.1.1.14       wiz screen_set_cursor_colour(struct screen *s, int colour)
    224   1.1.1.2      jmmv {
    225  1.1.1.14       wiz 	s->ccolour = colour;
    226   1.1.1.2      jmmv }
    227   1.1.1.2      jmmv 
    228       1.1      jmmv /* Set screen title. */
    229  1.1.1.11  christos int
    230       1.1      jmmv screen_set_title(struct screen *s, const char *title)
    231       1.1      jmmv {
    232  1.1.1.11  christos 	if (!utf8_isvalid(title))
    233  1.1.1.11  christos 		return (0);
    234   1.1.1.3  christos 	free(s->title);
    235  1.1.1.11  christos 	s->title = xstrdup(title);
    236  1.1.1.11  christos 	return (1);
    237  1.1.1.11  christos }
    238  1.1.1.11  christos 
    239  1.1.1.11  christos /* Set screen path. */
    240  1.1.1.11  christos void
    241  1.1.1.11  christos screen_set_path(struct screen *s, const char *path)
    242  1.1.1.11  christos {
    243  1.1.1.11  christos 	free(s->path);
    244  1.1.1.11  christos 	utf8_stravis(&s->path, path, VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL);
    245       1.1      jmmv }
    246       1.1      jmmv 
    247   1.1.1.9  christos /* Push the current title onto the stack. */
    248   1.1.1.9  christos void
    249   1.1.1.9  christos screen_push_title(struct screen *s)
    250   1.1.1.9  christos {
    251   1.1.1.9  christos 	struct screen_title_entry *title_entry;
    252   1.1.1.9  christos 
    253   1.1.1.9  christos 	if (s->titles == NULL) {
    254   1.1.1.9  christos 		s->titles = xmalloc(sizeof *s->titles);
    255   1.1.1.9  christos 		TAILQ_INIT(s->titles);
    256   1.1.1.9  christos 	}
    257   1.1.1.9  christos 	title_entry = xmalloc(sizeof *title_entry);
    258   1.1.1.9  christos 	title_entry->text = xstrdup(s->title);
    259   1.1.1.9  christos 	TAILQ_INSERT_HEAD(s->titles, title_entry, entry);
    260   1.1.1.9  christos }
    261   1.1.1.9  christos 
    262   1.1.1.9  christos /*
    263   1.1.1.9  christos  * Pop a title from the stack and set it as the screen title. If the stack is
    264   1.1.1.9  christos  * empty, do nothing.
    265   1.1.1.9  christos  */
    266   1.1.1.9  christos void
    267   1.1.1.9  christos screen_pop_title(struct screen *s)
    268   1.1.1.9  christos {
    269   1.1.1.9  christos 	struct screen_title_entry *title_entry;
    270   1.1.1.9  christos 
    271   1.1.1.9  christos 	if (s->titles == NULL)
    272   1.1.1.9  christos 		return;
    273   1.1.1.9  christos 
    274   1.1.1.9  christos 	title_entry = TAILQ_FIRST(s->titles);
    275   1.1.1.9  christos 	if (title_entry != NULL) {
    276   1.1.1.9  christos 		screen_set_title(s, title_entry->text);
    277   1.1.1.9  christos 
    278   1.1.1.9  christos 		TAILQ_REMOVE(s->titles, title_entry, entry);
    279   1.1.1.9  christos 		free(title_entry->text);
    280   1.1.1.9  christos 		free(title_entry);
    281   1.1.1.9  christos 	}
    282   1.1.1.9  christos }
    283   1.1.1.9  christos 
    284  1.1.1.12  christos /* Resize screen with options. */
    285       1.1      jmmv void
    286  1.1.1.12  christos screen_resize_cursor(struct screen *s, u_int sx, u_int sy, int reflow,
    287  1.1.1.12  christos     int eat_empty, int cursor)
    288       1.1      jmmv {
    289  1.1.1.12  christos 	u_int	cx = s->cx, cy = s->grid->hsize + s->cy;
    290  1.1.1.12  christos 
    291  1.1.1.12  christos 	if (s->write_list != NULL)
    292  1.1.1.12  christos 		screen_write_free_list(s);
    293  1.1.1.12  christos 
    294  1.1.1.12  christos 	log_debug("%s: new size %ux%u, now %ux%u (cursor %u,%u = %u,%u)",
    295  1.1.1.12  christos 	    __func__, sx, sy, screen_size_x(s), screen_size_y(s), s->cx, s->cy,
    296  1.1.1.12  christos 	    cx, cy);
    297  1.1.1.12  christos 
    298       1.1      jmmv 	if (sx < 1)
    299       1.1      jmmv 		sx = 1;
    300       1.1      jmmv 	if (sy < 1)
    301       1.1      jmmv 		sy = 1;
    302       1.1      jmmv 
    303       1.1      jmmv 	if (sx != screen_size_x(s)) {
    304  1.1.1.10  christos 		s->grid->sx = sx;
    305       1.1      jmmv 		screen_reset_tabs(s);
    306   1.1.1.9  christos 	} else
    307   1.1.1.9  christos 		reflow = 0;
    308       1.1      jmmv 
    309       1.1      jmmv 	if (sy != screen_size_y(s))
    310  1.1.1.12  christos 		screen_resize_y(s, sy, eat_empty, &cy);
    311   1.1.1.3  christos 
    312  1.1.1.15       wiz #ifdef ENABLE_SIXEL
    313  1.1.1.16       wiz 	image_free_all(s);
    314  1.1.1.15       wiz #endif
    315  1.1.1.16       wiz 
    316  1.1.1.16       wiz 	if (reflow)
    317  1.1.1.12  christos 		screen_reflow(s, sx, &cx, &cy, cursor);
    318  1.1.1.12  christos 
    319  1.1.1.12  christos 	if (cy >= s->grid->hsize) {
    320  1.1.1.12  christos 		s->cx = cx;
    321  1.1.1.12  christos 		s->cy = cy - s->grid->hsize;
    322  1.1.1.12  christos 	} else {
    323  1.1.1.12  christos 		s->cx = 0;
    324  1.1.1.12  christos 		s->cy = 0;
    325  1.1.1.12  christos 	}
    326  1.1.1.12  christos 
    327  1.1.1.12  christos 	log_debug("%s: cursor finished at %u,%u = %u,%u", __func__, s->cx,
    328  1.1.1.12  christos 	    s->cy, cx, cy);
    329  1.1.1.12  christos 
    330  1.1.1.12  christos 	if (s->write_list != NULL)
    331  1.1.1.12  christos 		screen_write_make_list(s);
    332  1.1.1.12  christos }
    333  1.1.1.12  christos 
    334  1.1.1.12  christos /* Resize screen. */
    335  1.1.1.12  christos void
    336  1.1.1.12  christos screen_resize(struct screen *s, u_int sx, u_int sy, int reflow)
    337  1.1.1.12  christos {
    338  1.1.1.12  christos 	screen_resize_cursor(s, sx, sy, reflow, 1, 1);
    339       1.1      jmmv }
    340       1.1      jmmv 
    341   1.1.1.7  christos static void
    342  1.1.1.12  christos screen_resize_y(struct screen *s, u_int sy, int eat_empty, u_int *cy)
    343       1.1      jmmv {
    344       1.1      jmmv 	struct grid	*gd = s->grid;
    345       1.1      jmmv 	u_int		 needed, available, oldy, i;
    346       1.1      jmmv 
    347       1.1      jmmv 	if (sy == 0)
    348       1.1      jmmv 		fatalx("zero size");
    349       1.1      jmmv 	oldy = screen_size_y(s);
    350       1.1      jmmv 
    351       1.1      jmmv 	/*
    352       1.1      jmmv 	 * When resizing:
    353       1.1      jmmv 	 *
    354       1.1      jmmv 	 * If the height is decreasing, delete lines from the bottom until
    355       1.1      jmmv 	 * hitting the cursor, then push lines from the top into the history.
    356       1.1      jmmv 	 *
    357   1.1.1.7  christos 	 * When increasing, pull as many lines as possible from scrolled
    358   1.1.1.7  christos 	 * history (not explicitly cleared from view) to the top, then fill the
    359   1.1.1.7  christos 	 * remaining with blanks at the bottom.
    360       1.1      jmmv 	 */
    361       1.1      jmmv 
    362       1.1      jmmv 	/* Size decreasing. */
    363       1.1      jmmv 	if (sy < oldy) {
    364       1.1      jmmv 		needed = oldy - sy;
    365       1.1      jmmv 
    366       1.1      jmmv 		/* Delete as many lines as possible from the bottom. */
    367  1.1.1.12  christos 		if (eat_empty) {
    368  1.1.1.12  christos 			available = oldy - 1 - s->cy;
    369  1.1.1.12  christos 			if (available > 0) {
    370  1.1.1.12  christos 				if (available > needed)
    371  1.1.1.12  christos 					available = needed;
    372  1.1.1.12  christos 				grid_view_delete_lines(gd, oldy - available,
    373  1.1.1.12  christos 				    available, 8);
    374  1.1.1.12  christos 			}
    375  1.1.1.12  christos 			needed -= available;
    376       1.1      jmmv 		}
    377       1.1      jmmv 
    378       1.1      jmmv 		/*
    379       1.1      jmmv 		 * Now just increase the history size, if possible, to take
    380       1.1      jmmv 		 * over the lines which are left. If history is off, delete
    381       1.1      jmmv 		 * lines from the top.
    382       1.1      jmmv 		 */
    383       1.1      jmmv 		available = s->cy;
    384   1.1.1.7  christos 		if (gd->flags & GRID_HISTORY) {
    385   1.1.1.7  christos 			gd->hscrolled += needed;
    386       1.1      jmmv 			gd->hsize += needed;
    387   1.1.1.7  christos 		} else if (needed > 0 && available > 0) {
    388       1.1      jmmv 			if (available > needed)
    389       1.1      jmmv 				available = needed;
    390   1.1.1.7  christos 			grid_view_delete_lines(gd, 0, available, 8);
    391  1.1.1.12  christos 			(*cy) -= available;
    392       1.1      jmmv 		}
    393       1.1      jmmv 	}
    394       1.1      jmmv 
    395   1.1.1.9  christos 	/* Resize line array. */
    396   1.1.1.9  christos 	grid_adjust_lines(gd, gd->hsize + sy);
    397       1.1      jmmv 
    398       1.1      jmmv 	/* Size increasing. */
    399       1.1      jmmv 	if (sy > oldy) {
    400       1.1      jmmv 		needed = sy - oldy;
    401       1.1      jmmv 
    402       1.1      jmmv 		/*
    403   1.1.1.7  christos 		 * Try to pull as much as possible out of scrolled history, if
    404  1.1.1.16       wiz 		 * it is enabled.
    405       1.1      jmmv 		 */
    406   1.1.1.7  christos 		available = gd->hscrolled;
    407       1.1      jmmv 		if (gd->flags & GRID_HISTORY && available > 0) {
    408       1.1      jmmv 			if (available > needed)
    409       1.1      jmmv 				available = needed;
    410   1.1.1.7  christos 			gd->hscrolled -= available;
    411       1.1      jmmv 			gd->hsize -= available;
    412       1.1      jmmv 		} else
    413       1.1      jmmv 			available = 0;
    414       1.1      jmmv 		needed -= available;
    415       1.1      jmmv 
    416       1.1      jmmv 		/* Then fill the rest in with blanks. */
    417       1.1      jmmv 		for (i = gd->hsize + sy - needed; i < gd->hsize + sy; i++)
    418  1.1.1.12  christos 			grid_empty_line(gd, i, 8);
    419       1.1      jmmv 	}
    420       1.1      jmmv 
    421       1.1      jmmv 	/* Set the new size, and reset the scroll region. */
    422       1.1      jmmv 	gd->sy = sy;
    423       1.1      jmmv 	s->rupper = 0;
    424       1.1      jmmv 	s->rlower = screen_size_y(s) - 1;
    425       1.1      jmmv }
    426       1.1      jmmv 
    427       1.1      jmmv /* Set selection. */
    428       1.1      jmmv void
    429       1.1      jmmv screen_set_selection(struct screen *s, u_int sx, u_int sy,
    430   1.1.1.9  christos     u_int ex, u_int ey, u_int rectangle, int modekeys, struct grid_cell *gc)
    431       1.1      jmmv {
    432   1.1.1.9  christos 	if (s->sel == NULL)
    433   1.1.1.9  christos 		s->sel = xcalloc(1, sizeof *s->sel);
    434       1.1      jmmv 
    435   1.1.1.9  christos 	memcpy(&s->sel->cell, gc, sizeof s->sel->cell);
    436   1.1.1.9  christos 	s->sel->hidden = 0;
    437   1.1.1.9  christos 	s->sel->rectangle = rectangle;
    438   1.1.1.9  christos 	s->sel->modekeys = modekeys;
    439   1.1.1.9  christos 
    440   1.1.1.9  christos 	s->sel->sx = sx;
    441   1.1.1.9  christos 	s->sel->sy = sy;
    442   1.1.1.9  christos 	s->sel->ex = ex;
    443   1.1.1.9  christos 	s->sel->ey = ey;
    444       1.1      jmmv }
    445       1.1      jmmv 
    446       1.1      jmmv /* Clear selection. */
    447       1.1      jmmv void
    448       1.1      jmmv screen_clear_selection(struct screen *s)
    449       1.1      jmmv {
    450   1.1.1.9  christos 	free(s->sel);
    451   1.1.1.9  christos 	s->sel = NULL;
    452       1.1      jmmv }
    453       1.1      jmmv 
    454   1.1.1.7  christos /* Hide selection. */
    455   1.1.1.7  christos void
    456   1.1.1.7  christos screen_hide_selection(struct screen *s)
    457   1.1.1.7  christos {
    458   1.1.1.9  christos 	if (s->sel != NULL)
    459   1.1.1.9  christos 		s->sel->hidden = 1;
    460   1.1.1.7  christos }
    461   1.1.1.7  christos 
    462       1.1      jmmv /* Check if cell in selection. */
    463       1.1      jmmv int
    464       1.1      jmmv screen_check_selection(struct screen *s, u_int px, u_int py)
    465       1.1      jmmv {
    466   1.1.1.9  christos 	struct screen_sel	*sel = s->sel;
    467   1.1.1.5  christos 	u_int			 xx;
    468       1.1      jmmv 
    469   1.1.1.9  christos 	if (sel == NULL || sel->hidden)
    470       1.1      jmmv 		return (0);
    471       1.1      jmmv 
    472   1.1.1.9  christos 	if (sel->rectangle) {
    473       1.1      jmmv 		if (sel->sy < sel->ey) {
    474       1.1      jmmv 			/* start line < end line -- downward selection. */
    475       1.1      jmmv 			if (py < sel->sy || py > sel->ey)
    476       1.1      jmmv 				return (0);
    477       1.1      jmmv 		} else if (sel->sy > sel->ey) {
    478       1.1      jmmv 			/* start line > end line -- upward selection. */
    479       1.1      jmmv 			if (py > sel->sy || py < sel->ey)
    480       1.1      jmmv 				return (0);
    481       1.1      jmmv 		} else {
    482       1.1      jmmv 			/* starting line == ending line. */
    483       1.1      jmmv 			if (py != sel->sy)
    484       1.1      jmmv 				return (0);
    485       1.1      jmmv 		}
    486       1.1      jmmv 
    487       1.1      jmmv 		/*
    488       1.1      jmmv 		 * Need to include the selection start row, but not the cursor
    489       1.1      jmmv 		 * row, which means the selection changes depending on which
    490       1.1      jmmv 		 * one is on the left.
    491       1.1      jmmv 		 */
    492       1.1      jmmv 		if (sel->ex < sel->sx) {
    493       1.1      jmmv 			/* Cursor (ex) is on the left. */
    494       1.1      jmmv 			if (px < sel->ex)
    495       1.1      jmmv 				return (0);
    496       1.1      jmmv 
    497       1.1      jmmv 			if (px > sel->sx)
    498       1.1      jmmv 				return (0);
    499       1.1      jmmv 		} else {
    500       1.1      jmmv 			/* Selection start (sx) is on the left. */
    501       1.1      jmmv 			if (px < sel->sx)
    502       1.1      jmmv 				return (0);
    503       1.1      jmmv 
    504       1.1      jmmv 			if (px > sel->ex)
    505       1.1      jmmv 				return (0);
    506       1.1      jmmv 		}
    507       1.1      jmmv 	} else {
    508       1.1      jmmv 		/*
    509       1.1      jmmv 		 * Like emacs, keep the top-left-most character, and drop the
    510       1.1      jmmv 		 * bottom-right-most, regardless of copy direction.
    511       1.1      jmmv 		 */
    512       1.1      jmmv 		if (sel->sy < sel->ey) {
    513       1.1      jmmv 			/* starting line < ending line -- downward selection. */
    514       1.1      jmmv 			if (py < sel->sy || py > sel->ey)
    515       1.1      jmmv 				return (0);
    516       1.1      jmmv 
    517   1.1.1.5  christos 			if (py == sel->sy && px < sel->sx)
    518   1.1.1.5  christos 				return (0);
    519   1.1.1.5  christos 
    520  1.1.1.10  christos 			if (sel->modekeys == MODEKEY_EMACS)
    521  1.1.1.10  christos 				xx = (sel->ex == 0 ? 0 : sel->ex - 1);
    522  1.1.1.10  christos 			else
    523  1.1.1.10  christos 				xx = sel->ex;
    524  1.1.1.10  christos 			if (py == sel->ey && px > xx)
    525       1.1      jmmv 				return (0);
    526       1.1      jmmv 		} else if (sel->sy > sel->ey) {
    527       1.1      jmmv 			/* starting line > ending line -- upward selection. */
    528       1.1      jmmv 			if (py > sel->sy || py < sel->ey)
    529       1.1      jmmv 				return (0);
    530       1.1      jmmv 
    531   1.1.1.5  christos 			if (py == sel->ey && px < sel->ex)
    532   1.1.1.5  christos 				return (0);
    533   1.1.1.5  christos 
    534   1.1.1.5  christos 			if (sel->modekeys == MODEKEY_EMACS)
    535   1.1.1.5  christos 				xx = sel->sx - 1;
    536   1.1.1.5  christos 			else
    537   1.1.1.5  christos 				xx = sel->sx;
    538   1.1.1.7  christos 			if (py == sel->sy && (sel->sx == 0 || px > xx))
    539       1.1      jmmv 				return (0);
    540       1.1      jmmv 		} else {
    541       1.1      jmmv 			/* starting line == ending line. */
    542       1.1      jmmv 			if (py != sel->sy)
    543       1.1      jmmv 				return (0);
    544       1.1      jmmv 
    545       1.1      jmmv 			if (sel->ex < sel->sx) {
    546       1.1      jmmv 				/* cursor (ex) is on the left */
    547   1.1.1.5  christos 				if (sel->modekeys == MODEKEY_EMACS)
    548   1.1.1.5  christos 					xx = sel->sx - 1;
    549   1.1.1.5  christos 				else
    550   1.1.1.5  christos 					xx = sel->sx;
    551   1.1.1.5  christos 				if (px > xx || px < sel->ex)
    552       1.1      jmmv 					return (0);
    553       1.1      jmmv 			} else {
    554       1.1      jmmv 				/* selection start (sx) is on the left */
    555  1.1.1.10  christos 				if (sel->modekeys == MODEKEY_EMACS)
    556  1.1.1.10  christos 					xx = (sel->ex == 0 ? 0 : sel->ex - 1);
    557  1.1.1.10  christos 				else
    558  1.1.1.10  christos 					xx = sel->ex;
    559  1.1.1.10  christos 				if (px < sel->sx || px > xx)
    560       1.1      jmmv 					return (0);
    561       1.1      jmmv 			}
    562       1.1      jmmv 		}
    563       1.1      jmmv 	}
    564       1.1      jmmv 
    565       1.1      jmmv 	return (1);
    566       1.1      jmmv }
    567   1.1.1.3  christos 
    568   1.1.1.7  christos /* Get selected grid cell. */
    569   1.1.1.3  christos void
    570   1.1.1.7  christos screen_select_cell(struct screen *s, struct grid_cell *dst,
    571   1.1.1.7  christos     const struct grid_cell *src)
    572   1.1.1.7  christos {
    573   1.1.1.9  christos 	if (s->sel == NULL || s->sel->hidden)
    574   1.1.1.7  christos 		return;
    575   1.1.1.7  christos 
    576   1.1.1.9  christos 	memcpy(dst, &s->sel->cell, sizeof *dst);
    577   1.1.1.7  christos 
    578   1.1.1.7  christos 	utf8_copy(&dst->data, &src->data);
    579   1.1.1.7  christos 	dst->attr = dst->attr & ~GRID_ATTR_CHARSET;
    580   1.1.1.7  christos 	dst->attr |= src->attr & GRID_ATTR_CHARSET;
    581   1.1.1.7  christos 	dst->flags = src->flags;
    582   1.1.1.7  christos }
    583   1.1.1.7  christos 
    584   1.1.1.7  christos /* Reflow wrapped lines. */
    585   1.1.1.7  christos static void
    586  1.1.1.12  christos screen_reflow(struct screen *s, u_int new_x, u_int *cx, u_int *cy, int cursor)
    587   1.1.1.3  christos {
    588  1.1.1.12  christos 	u_int	wx, wy;
    589  1.1.1.10  christos 
    590  1.1.1.12  christos 	if (cursor) {
    591  1.1.1.12  christos 		grid_wrap_position(s->grid, *cx, *cy, &wx, &wy);
    592  1.1.1.12  christos 		log_debug("%s: cursor %u,%u is %u,%u", __func__, *cx, *cy, wx,
    593  1.1.1.12  christos 		    wy);
    594  1.1.1.12  christos 	}
    595  1.1.1.10  christos 
    596  1.1.1.10  christos 	grid_reflow(s->grid, new_x);
    597  1.1.1.10  christos 
    598  1.1.1.12  christos 	if (cursor) {
    599  1.1.1.12  christos 		grid_unwrap_position(s->grid, cx, cy, wx, wy);
    600  1.1.1.12  christos 		log_debug("%s: new cursor is %u,%u", __func__, *cx, *cy);
    601  1.1.1.12  christos 	}
    602  1.1.1.12  christos 	else {
    603  1.1.1.12  christos 		*cx = 0;
    604  1.1.1.12  christos 		*cy = s->grid->hsize;
    605  1.1.1.12  christos 	}
    606  1.1.1.12  christos }
    607  1.1.1.10  christos 
    608  1.1.1.12  christos /*
    609  1.1.1.12  christos  * Enter alternative screen mode. A copy of the visible screen is saved and the
    610  1.1.1.12  christos  * history is not updated.
    611  1.1.1.12  christos  */
    612  1.1.1.12  christos void
    613  1.1.1.12  christos screen_alternate_on(struct screen *s, struct grid_cell *gc, int cursor)
    614  1.1.1.12  christos {
    615  1.1.1.12  christos 	u_int	sx, sy;
    616  1.1.1.12  christos 
    617  1.1.1.12  christos 	if (s->saved_grid != NULL)
    618  1.1.1.12  christos 		return;
    619  1.1.1.12  christos 	sx = screen_size_x(s);
    620  1.1.1.12  christos 	sy = screen_size_y(s);
    621  1.1.1.12  christos 
    622  1.1.1.12  christos 	s->saved_grid = grid_create(sx, sy, 0);
    623  1.1.1.12  christos 	grid_duplicate_lines(s->saved_grid, 0, s->grid, screen_hsize(s), sy);
    624  1.1.1.12  christos 	if (cursor) {
    625  1.1.1.12  christos 		s->saved_cx = s->cx;
    626  1.1.1.12  christos 		s->saved_cy = s->cy;
    627  1.1.1.12  christos 	}
    628  1.1.1.12  christos 	memcpy(&s->saved_cell, gc, sizeof s->saved_cell);
    629  1.1.1.12  christos 
    630  1.1.1.12  christos 	grid_view_clear(s->grid, 0, 0, sx, sy, 8);
    631  1.1.1.12  christos 
    632  1.1.1.12  christos 	s->saved_flags = s->grid->flags;
    633  1.1.1.12  christos 	s->grid->flags &= ~GRID_HISTORY;
    634  1.1.1.12  christos }
    635  1.1.1.12  christos 
    636  1.1.1.12  christos /* Exit alternate screen mode and restore the copied grid. */
    637  1.1.1.12  christos void
    638  1.1.1.12  christos screen_alternate_off(struct screen *s, struct grid_cell *gc, int cursor)
    639  1.1.1.12  christos {
    640  1.1.1.12  christos 	u_int	sx = screen_size_x(s), sy = screen_size_y(s);
    641  1.1.1.12  christos 
    642  1.1.1.12  christos 	/*
    643  1.1.1.12  christos 	 * If the current size is different, temporarily resize to the old size
    644  1.1.1.12  christos 	 * before copying back.
    645  1.1.1.12  christos 	 */
    646  1.1.1.12  christos 	if (s->saved_grid != NULL)
    647  1.1.1.15       wiz 		screen_resize(s, s->saved_grid->sx, s->saved_grid->sy, 0);
    648  1.1.1.12  christos 
    649  1.1.1.12  christos 	/*
    650  1.1.1.12  christos 	 * Restore the cursor position and cell. This happens even if not
    651  1.1.1.12  christos 	 * currently in the alternate screen.
    652  1.1.1.12  christos 	 */
    653  1.1.1.12  christos 	if (cursor && s->saved_cx != UINT_MAX && s->saved_cy != UINT_MAX) {
    654  1.1.1.12  christos 		s->cx = s->saved_cx;
    655  1.1.1.12  christos 		s->cy = s->saved_cy;
    656  1.1.1.12  christos 		if (gc != NULL)
    657  1.1.1.12  christos 			memcpy(gc, &s->saved_cell, sizeof *gc);
    658  1.1.1.12  christos 	}
    659  1.1.1.12  christos 
    660  1.1.1.12  christos 	/* If not in the alternate screen, do nothing more. */
    661  1.1.1.12  christos 	if (s->saved_grid == NULL) {
    662  1.1.1.12  christos 		if (s->cx > screen_size_x(s) - 1)
    663  1.1.1.12  christos 			s->cx = screen_size_x(s) - 1;
    664  1.1.1.12  christos 		if (s->cy > screen_size_y(s) - 1)
    665  1.1.1.12  christos 			s->cy = screen_size_y(s) - 1;
    666  1.1.1.12  christos 		return;
    667  1.1.1.10  christos 	}
    668  1.1.1.10  christos 
    669  1.1.1.12  christos 	/* Restore the saved grid. */
    670  1.1.1.12  christos 	grid_duplicate_lines(s->grid, screen_hsize(s), s->saved_grid, 0,
    671  1.1.1.12  christos 	    s->saved_grid->sy);
    672  1.1.1.10  christos 
    673  1.1.1.12  christos 	/*
    674  1.1.1.12  christos 	 * Turn history back on (so resize can use it) and then resize back to
    675  1.1.1.12  christos 	 * the current size.
    676  1.1.1.12  christos 	 */
    677  1.1.1.12  christos 	if (s->saved_flags & GRID_HISTORY)
    678  1.1.1.12  christos 		s->grid->flags |= GRID_HISTORY;
    679  1.1.1.12  christos 	screen_resize(s, sx, sy, 1);
    680  1.1.1.12  christos 
    681  1.1.1.12  christos 	grid_destroy(s->saved_grid);
    682  1.1.1.12  christos 	s->saved_grid = NULL;
    683  1.1.1.12  christos 
    684  1.1.1.12  christos 	if (s->cx > screen_size_x(s) - 1)
    685  1.1.1.12  christos 		s->cx = screen_size_x(s) - 1;
    686  1.1.1.12  christos 	if (s->cy > screen_size_y(s) - 1)
    687  1.1.1.12  christos 		s->cy = screen_size_y(s) - 1;
    688   1.1.1.3  christos }
    689  1.1.1.14       wiz 
    690  1.1.1.14       wiz /* Get mode as a string. */
    691  1.1.1.14       wiz const char *
    692  1.1.1.14       wiz screen_mode_to_string(int mode)
    693  1.1.1.14       wiz {
    694  1.1.1.14       wiz 	static char	tmp[1024];
    695  1.1.1.14       wiz 
    696  1.1.1.14       wiz 	if (mode == 0)
    697  1.1.1.14       wiz 		return ("NONE");
    698  1.1.1.14       wiz 	if (mode == ALL_MODES)
    699  1.1.1.14       wiz 		return ("ALL");
    700  1.1.1.14       wiz 
    701  1.1.1.14       wiz 	*tmp = '\0';
    702  1.1.1.14       wiz 	if (mode & MODE_CURSOR)
    703  1.1.1.14       wiz 		strlcat(tmp, "CURSOR,", sizeof tmp);
    704  1.1.1.14       wiz 	if (mode & MODE_INSERT)
    705  1.1.1.14       wiz 		strlcat(tmp, "INSERT,", sizeof tmp);
    706  1.1.1.14       wiz 	if (mode & MODE_KCURSOR)
    707  1.1.1.14       wiz 		strlcat(tmp, "KCURSOR,", sizeof tmp);
    708  1.1.1.14       wiz 	if (mode & MODE_KKEYPAD)
    709  1.1.1.14       wiz 		strlcat(tmp, "KKEYPAD,", sizeof tmp);
    710  1.1.1.14       wiz 	if (mode & MODE_WRAP)
    711  1.1.1.14       wiz 		strlcat(tmp, "WRAP,", sizeof tmp);
    712  1.1.1.14       wiz 	if (mode & MODE_MOUSE_STANDARD)
    713  1.1.1.14       wiz 		strlcat(tmp, "MOUSE_STANDARD,", sizeof tmp);
    714  1.1.1.14       wiz 	if (mode & MODE_MOUSE_BUTTON)
    715  1.1.1.14       wiz 		strlcat(tmp, "MOUSE_BUTTON,", sizeof tmp);
    716  1.1.1.14       wiz 	if (mode & MODE_CURSOR_BLINKING)
    717  1.1.1.14       wiz 		strlcat(tmp, "CURSOR_BLINKING,", sizeof tmp);
    718  1.1.1.14       wiz 	if (mode & MODE_CURSOR_VERY_VISIBLE)
    719  1.1.1.14       wiz 		strlcat(tmp, "CURSOR_VERY_VISIBLE,", sizeof tmp);
    720  1.1.1.14       wiz 	if (mode & MODE_MOUSE_UTF8)
    721  1.1.1.15       wiz 		strlcat(tmp, "MOUSE_UTF8,", sizeof tmp);
    722  1.1.1.14       wiz 	if (mode & MODE_MOUSE_SGR)
    723  1.1.1.15       wiz 		strlcat(tmp, "MOUSE_SGR,", sizeof tmp);
    724  1.1.1.14       wiz 	if (mode & MODE_BRACKETPASTE)
    725  1.1.1.14       wiz 		strlcat(tmp, "BRACKETPASTE,", sizeof tmp);
    726  1.1.1.14       wiz 	if (mode & MODE_FOCUSON)
    727  1.1.1.14       wiz 		strlcat(tmp, "FOCUSON,", sizeof tmp);
    728  1.1.1.14       wiz 	if (mode & MODE_MOUSE_ALL)
    729  1.1.1.14       wiz 		strlcat(tmp, "MOUSE_ALL,", sizeof tmp);
    730  1.1.1.14       wiz 	if (mode & MODE_ORIGIN)
    731  1.1.1.14       wiz 		strlcat(tmp, "ORIGIN,", sizeof tmp);
    732  1.1.1.14       wiz 	if (mode & MODE_CRLF)
    733  1.1.1.14       wiz 		strlcat(tmp, "CRLF,", sizeof tmp);
    734  1.1.1.16       wiz 	if (mode & MODE_KEYS_EXTENDED)
    735  1.1.1.16       wiz 		strlcat(tmp, "KEYS_EXTENDED,", sizeof tmp);
    736  1.1.1.16       wiz 	if (mode & MODE_KEYS_EXTENDED_2)
    737  1.1.1.16       wiz 		strlcat(tmp, "KEYS_EXTENDED_2,", sizeof tmp);
    738  1.1.1.14       wiz 	tmp[strlen(tmp) - 1] = '\0';
    739  1.1.1.14       wiz 	return (tmp);
    740  1.1.1.14       wiz }
    741