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