Home | History | Annotate | Line # | Download | only in dist
screen.c revision 1.1.1.3
      1 /* $Id: screen.c,v 1.1.1.3 2014/07/24 14:22:53 christos Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2007 Nicholas Marriott <nicm (at) users.sourceforge.net>
      5  *
      6  * Permission to use, copy, modify, and distribute this software for any
      7  * purpose with or without fee is hereby granted, provided that the above
      8  * copyright notice and this permission notice appear in all copies.
      9  *
     10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     14  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
     15  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
     16  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     17  */
     18 
     19 #include <sys/types.h>
     20 
     21 #include <netdb.h>
     22 #include <stdlib.h>
     23 #include <string.h>
     24 #include <unistd.h>
     25 
     26 #include "tmux.h"
     27 
     28 void	screen_resize_x(struct screen *, u_int);
     29 void	screen_resize_y(struct screen *, u_int);
     30 
     31 /* Create a new screen. */
     32 void
     33 screen_init(struct screen *s, u_int sx, u_int sy, u_int hlimit)
     34 {
     35 	char hn[MAXHOSTNAMELEN];
     36 
     37 	s->grid = grid_create(sx, sy, hlimit);
     38 
     39 	if (gethostname(hn, MAXHOSTNAMELEN) == 0)
     40 		s->title = xstrdup(hn);
     41 	else
     42 		s->title = xstrdup("");
     43 
     44 	s->cstyle = 0;
     45 	s->ccolour = xstrdup("");
     46 	s->tabs = NULL;
     47 
     48 	screen_reinit(s);
     49 }
     50 
     51 /* Reinitialise screen. */
     52 void
     53 screen_reinit(struct screen *s)
     54 {
     55 	s->cx = 0;
     56 	s->cy = 0;
     57 
     58 	s->rupper = 0;
     59 	s->rlower = screen_size_y(s) - 1;
     60 
     61 	s->mode = MODE_CURSOR | MODE_WRAP;
     62 
     63 	screen_reset_tabs(s);
     64 
     65 	grid_clear_lines(s->grid, s->grid->hsize, s->grid->sy);
     66 
     67 	screen_clear_selection(s);
     68 }
     69 
     70 /* Destroy a screen. */
     71 void
     72 screen_free(struct screen *s)
     73 {
     74 	free(s->tabs);
     75 	free(s->title);
     76 	free(s->ccolour);
     77 	grid_destroy(s->grid);
     78 }
     79 
     80 /* Reset tabs to default, eight spaces apart. */
     81 void
     82 screen_reset_tabs(struct screen *s)
     83 {
     84 	u_int	i;
     85 
     86 	free(s->tabs);
     87 
     88 	if ((s->tabs = bit_alloc(screen_size_x(s))) == NULL)
     89 		fatal("bit_alloc failed");
     90 	for (i = 8; i < screen_size_x(s); i += 8)
     91 		bit_set(s->tabs, i);
     92 }
     93 
     94 /* Set screen cursor style. */
     95 void
     96 screen_set_cursor_style(struct screen *s, u_int style)
     97 {
     98 	if (style <= 6)
     99 		s->cstyle = style;
    100 }
    101 
    102 /* Set screen cursor colour. */
    103 void
    104 screen_set_cursor_colour(struct screen *s, const char *colour_string)
    105 {
    106 	free(s->ccolour);
    107 	s->ccolour = xstrdup(colour_string);
    108 }
    109 
    110 /* Set screen title. */
    111 void
    112 screen_set_title(struct screen *s, const char *title)
    113 {
    114 	free(s->title);
    115 	s->title = xstrdup(title);
    116 }
    117 
    118 /* Resize screen. */
    119 void
    120 screen_resize(struct screen *s, u_int sx, u_int sy, int reflow)
    121 {
    122 	if (sx < 1)
    123 		sx = 1;
    124 	if (sy < 1)
    125 		sy = 1;
    126 
    127 	if (sx != screen_size_x(s)) {
    128 		screen_resize_x(s, sx);
    129 
    130 		/*
    131 		 * It is unclear what should happen to tabs on resize. xterm
    132 		 * seems to try and maintain them, rxvt resets them. Resetting
    133 		 * is simpler and more reliable so let's do that.
    134 		 */
    135 		screen_reset_tabs(s);
    136 	}
    137 
    138 	if (sy != screen_size_y(s))
    139 		screen_resize_y(s, sy);
    140 
    141 	if (reflow)
    142 		screen_reflow(s, sx);
    143 }
    144 
    145 void
    146 screen_resize_x(struct screen *s, u_int sx)
    147 {
    148 	struct grid		*gd = s->grid;
    149 
    150 	if (sx == 0)
    151 		fatalx("zero size");
    152 
    153 	/*
    154 	 * Treat resizing horizontally simply: just ensure the cursor is
    155 	 * on-screen and change the size. Don't bother to truncate any lines -
    156 	 * then the data should be accessible if the size is then incrased.
    157 	 *
    158 	 * The only potential wrinkle is if UTF-8 double-width characters are
    159 	 * left in the last column, but UTF-8 terminals should deal with this
    160 	 * sanely.
    161 	 */
    162 	if (s->cx >= sx)
    163 		s->cx = sx - 1;
    164 	gd->sx = sx;
    165 }
    166 
    167 void
    168 screen_resize_y(struct screen *s, u_int sy)
    169 {
    170 	struct grid	*gd = s->grid;
    171 	u_int		 needed, available, oldy, i;
    172 
    173 	if (sy == 0)
    174 		fatalx("zero size");
    175 	oldy = screen_size_y(s);
    176 
    177 	/*
    178 	 * When resizing:
    179 	 *
    180 	 * If the height is decreasing, delete lines from the bottom until
    181 	 * hitting the cursor, then push lines from the top into the history.
    182 	 *
    183 	 * When increasing, pull as many lines as possible from the history to
    184 	 * the top, then fill the remaining with blanks at the bottom.
    185 	 */
    186 
    187 	/* Size decreasing. */
    188 	if (sy < oldy) {
    189 		needed = oldy - sy;
    190 
    191 		/* Delete as many lines as possible from the bottom. */
    192 		available = oldy - 1 - s->cy;
    193 		if (available > 0) {
    194 			if (available > needed)
    195 				available = needed;
    196 			grid_view_delete_lines(gd, oldy - available, available);
    197 		}
    198 		needed -= available;
    199 
    200 		/*
    201 		 * Now just increase the history size, if possible, to take
    202 		 * over the lines which are left. If history is off, delete
    203 		 * lines from the top.
    204 		 *
    205 		 * XXX Should apply history limit?
    206 		 */
    207 		available = s->cy;
    208 		if (gd->flags & GRID_HISTORY)
    209 			gd->hsize += needed;
    210 		else if (needed > 0 && available > 0) {
    211 			if (available > needed)
    212 				available = needed;
    213 			grid_view_delete_lines(gd, 0, available);
    214 		}
    215 		s->cy -= needed;
    216 	}
    217 
    218 	/* Resize line arrays. */
    219 	gd->linedata = xrealloc(
    220 	    gd->linedata, gd->hsize + sy, sizeof *gd->linedata);
    221 
    222 	/* Size increasing. */
    223 	if (sy > oldy) {
    224 		needed = sy - oldy;
    225 
    226 		/*
    227 		 * Try to pull as much as possible out of the history, if is
    228 		 * is enabled.
    229 		 */
    230 		available = gd->hsize;
    231 		if (gd->flags & GRID_HISTORY && available > 0) {
    232 			if (available > needed)
    233 				available = needed;
    234 			gd->hsize -= available;
    235 			s->cy += available;
    236 		} else
    237 			available = 0;
    238 		needed -= available;
    239 
    240 		/* Then fill the rest in with blanks. */
    241 		for (i = gd->hsize + sy - needed; i < gd->hsize + sy; i++)
    242 			memset(&gd->linedata[i], 0, sizeof gd->linedata[i]);
    243 	}
    244 
    245 	/* Set the new size, and reset the scroll region. */
    246 	gd->sy = sy;
    247 	s->rupper = 0;
    248 	s->rlower = screen_size_y(s) - 1;
    249 }
    250 
    251 /* Set selection. */
    252 void
    253 screen_set_selection(struct screen *s, u_int sx, u_int sy,
    254     u_int ex, u_int ey, u_int rectflag, struct grid_cell *gc)
    255 {
    256 	struct screen_sel	*sel = &s->sel;
    257 
    258 	memcpy(&sel->cell, gc, sizeof sel->cell);
    259 	sel->flag = 1;
    260 	sel->rectflag = rectflag;
    261 
    262 	sel->sx = sx; sel->sy = sy;
    263 	sel->ex = ex; sel->ey = ey;
    264 }
    265 
    266 /* Clear selection. */
    267 void
    268 screen_clear_selection(struct screen *s)
    269 {
    270 	struct screen_sel	*sel = &s->sel;
    271 
    272 	sel->flag = 0;
    273 }
    274 
    275 /* Check if cell in selection. */
    276 int
    277 screen_check_selection(struct screen *s, u_int px, u_int py)
    278 {
    279 	struct screen_sel	*sel = &s->sel;
    280 
    281 	if (!sel->flag)
    282 		return (0);
    283 
    284 	if (sel->rectflag) {
    285 		if (sel->sy < sel->ey) {
    286 			/* start line < end line -- downward selection. */
    287 			if (py < sel->sy || py > sel->ey)
    288 				return (0);
    289 		} else if (sel->sy > sel->ey) {
    290 			/* start line > end line -- upward selection. */
    291 			if (py > sel->sy || py < sel->ey)
    292 				return (0);
    293 		} else {
    294 			/* starting line == ending line. */
    295 			if (py != sel->sy)
    296 				return (0);
    297 		}
    298 
    299 		/*
    300 		 * Need to include the selection start row, but not the cursor
    301 		 * row, which means the selection changes depending on which
    302 		 * one is on the left.
    303 		 */
    304 		if (sel->ex < sel->sx) {
    305 			/* Cursor (ex) is on the left. */
    306 			if (px < sel->ex)
    307 				return (0);
    308 
    309 			if (px > sel->sx)
    310 				return (0);
    311 		} else {
    312 			/* Selection start (sx) is on the left. */
    313 			if (px < sel->sx)
    314 				return (0);
    315 
    316 			if (px > sel->ex)
    317 				return (0);
    318 		}
    319 	} else {
    320 		/*
    321 		 * Like emacs, keep the top-left-most character, and drop the
    322 		 * bottom-right-most, regardless of copy direction.
    323 		 */
    324 		if (sel->sy < sel->ey) {
    325 			/* starting line < ending line -- downward selection. */
    326 			if (py < sel->sy || py > sel->ey)
    327 				return (0);
    328 
    329 			if ((py == sel->sy && px < sel->sx)
    330 			    || (py == sel->ey && px > sel->ex))
    331 				return (0);
    332 		} else if (sel->sy > sel->ey) {
    333 			/* starting line > ending line -- upward selection. */
    334 			if (py > sel->sy || py < sel->ey)
    335 				return (0);
    336 
    337 			if ((py == sel->sy && px >= sel->sx)
    338 			    || (py == sel->ey && px < sel->ex))
    339 				return (0);
    340 		} else {
    341 			/* starting line == ending line. */
    342 			if (py != sel->sy)
    343 				return (0);
    344 
    345 			if (sel->ex < sel->sx) {
    346 				/* cursor (ex) is on the left */
    347 				if (px > sel->sx || px < sel->ex)
    348 					return (0);
    349 			} else {
    350 				/* selection start (sx) is on the left */
    351 				if (px < sel->sx || px > sel->ex)
    352 					return (0);
    353 			}
    354 		}
    355 	}
    356 
    357 	return (1);
    358 }
    359 
    360 /* Reflow wrapped lines. */
    361 void
    362 screen_reflow(struct screen *s, u_int new_x)
    363 {
    364 	struct grid	*old = s->grid;
    365 	u_int		 change;
    366 
    367 	s->grid = grid_create(old->sx, old->sy, old->hlimit);
    368 
    369 	change = grid_reflow(s->grid, old, new_x);
    370 	if (change < s->cy)
    371 		s->cy -= change;
    372 	else
    373 		s->cy = 0;
    374 }
    375