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