Home | History | Annotate | Line # | Download | only in dist
screen.c revision 1.1.1.2
      1 /* $Id: screen.c,v 1.1.1.2 2011/08/17 18:40:05 jmmv 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 	if (s->tabs != NULL)
     75 		xfree(s->tabs);
     76 	xfree(s->title);
     77 	xfree(s->ccolour);
     78 	grid_destroy(s->grid);
     79 }
     80 
     81 /* Reset tabs to default, eight spaces apart. */
     82 void
     83 screen_reset_tabs(struct screen *s)
     84 {
     85 	u_int	i;
     86 
     87 	if (s->tabs != NULL)
     88 		xfree(s->tabs);
     89 
     90 	if ((s->tabs = bit_alloc(screen_size_x(s))) == NULL)
     91 		fatal("bit_alloc failed");
     92 	for (i = 8; i < screen_size_x(s); i += 8)
     93 		bit_set(s->tabs, i);
     94 }
     95 
     96 /* Set screen cursor style. */
     97 void
     98 screen_set_cursor_style(struct screen *s, u_int style)
     99 {
    100 	if (style <= 4)
    101 		s->cstyle = style;
    102 }
    103 
    104 /* Set screen cursor colour. */
    105 void
    106 screen_set_cursor_colour(struct screen *s, const char *colour_string)
    107 {
    108 	xfree(s->ccolour);
    109 	s->ccolour = xstrdup(colour_string);
    110 }
    111 
    112 /* Set screen title. */
    113 void
    114 screen_set_title(struct screen *s, const char *title)
    115 {
    116 	char	tmp[BUFSIZ];
    117 
    118 	strlcpy(tmp, title, sizeof tmp);
    119 
    120 	xfree(s->title);
    121 	s->title = xstrdup(tmp);
    122 }
    123 
    124 /* Resize screen. */
    125 void
    126 screen_resize(struct screen *s, u_int sx, u_int sy)
    127 {
    128 	if (sx < 1)
    129 		sx = 1;
    130 	if (sy < 1)
    131 		sy = 1;
    132 
    133 	if (sx != screen_size_x(s)) {
    134 		screen_resize_x(s, sx);
    135 
    136 		/*
    137 		 * It is unclear what should happen to tabs on resize. xterm
    138 		 * seems to try and maintain them, rxvt resets them. Resetting
    139 		 * is simpler and more reliable so let's do that.
    140 		 */
    141 		screen_reset_tabs(s);
    142 	}
    143 
    144 	if (sy != screen_size_y(s))
    145 		screen_resize_y(s, sy);
    146 }
    147 
    148 void
    149 screen_resize_x(struct screen *s, u_int sx)
    150 {
    151 	struct grid		*gd = s->grid;
    152 
    153 	if (sx == 0)
    154 		fatalx("zero size");
    155 
    156 	/*
    157 	 * Treat resizing horizontally simply: just ensure the cursor is
    158 	 * on-screen and change the size. Don't bother to truncate any lines -
    159 	 * then the data should be accessible if the size is then incrased.
    160 	 *
    161 	 * The only potential wrinkle is if UTF-8 double-width characters are
    162 	 * left in the last column, but UTF-8 terminals should deal with this
    163 	 * sanely.
    164 	 */
    165 	if (s->cx >= sx)
    166 		s->cx = sx - 1;
    167 	gd->sx = sx;
    168 }
    169 
    170 void
    171 screen_resize_y(struct screen *s, u_int sy)
    172 {
    173 	struct grid	*gd = s->grid;
    174 	u_int		 needed, available, oldy, i;
    175 
    176 	if (sy == 0)
    177 		fatalx("zero size");
    178 	oldy = screen_size_y(s);
    179 
    180 	/*
    181 	 * When resizing:
    182 	 *
    183 	 * If the height is decreasing, delete lines from the bottom until
    184 	 * hitting the cursor, then push lines from the top into the history.
    185 	 *
    186 	 * When increasing, pull as many lines as possible from the history to
    187 	 * the top, then fill the remaining with blanks at the bottom.
    188 	 */
    189 
    190 	/* Size decreasing. */
    191 	if (sy < oldy) {
    192 		needed = oldy - sy;
    193 
    194 		/* Delete as many lines as possible from the bottom. */
    195 		available = oldy - 1 - s->cy;
    196 		if (available > 0) {
    197 			if (available > needed)
    198 				available = needed;
    199 			grid_view_delete_lines(gd, oldy - available, available);
    200 		}
    201 		needed -= available;
    202 
    203 		/*
    204 		 * Now just increase the history size, if possible, to take
    205 		 * over the lines which are left. If history is off, delete
    206 		 * lines from the top.
    207 		 *
    208 		 * XXX Should apply history limit?
    209 		 */
    210 		available = s->cy;
    211 		if (gd->flags & GRID_HISTORY)
    212 			gd->hsize += needed;
    213 		else if (needed > 0 && available > 0) {
    214 			if (available > needed)
    215 				available = needed;
    216 			grid_view_delete_lines(gd, 0, available);
    217 		}
    218 		s->cy -= needed;
    219 	}
    220 
    221 	/* Resize line arrays. */
    222 	gd->linedata = xrealloc(
    223 	    gd->linedata, gd->hsize + sy, sizeof *gd->linedata);
    224 
    225 	/* Size increasing. */
    226 	if (sy > oldy) {
    227 		needed = sy - oldy;
    228 
    229 		/*
    230 		 * Try to pull as much as possible out of the history, if is
    231 		 * is enabled.
    232 		 */
    233 		available = gd->hsize;
    234 		if (gd->flags & GRID_HISTORY && available > 0) {
    235 			if (available > needed)
    236 				available = needed;
    237 			gd->hsize -= available;
    238 			s->cy += available;
    239 		} else
    240 			available = 0;
    241 		needed -= available;
    242 
    243 		/* Then fill the rest in with blanks. */
    244 		for (i = gd->hsize + sy - needed; i < gd->hsize + sy; i++)
    245 			memset(&gd->linedata[i], 0, sizeof gd->linedata[i]);
    246 	}
    247 
    248 	/* Set the new size, and reset the scroll region. */
    249 	gd->sy = sy;
    250 	s->rupper = 0;
    251 	s->rlower = screen_size_y(s) - 1;
    252 }
    253 
    254 /* Set selection. */
    255 void
    256 screen_set_selection(struct screen *s, u_int sx, u_int sy,
    257     u_int ex, u_int ey, u_int rectflag, struct grid_cell *gc)
    258 {
    259 	struct screen_sel	*sel = &s->sel;
    260 
    261 	memcpy(&sel->cell, gc, sizeof sel->cell);
    262 	sel->flag = 1;
    263 	sel->rectflag = rectflag;
    264 
    265 	sel->sx = sx; sel->sy = sy;
    266 	sel->ex = ex; sel->ey = ey;
    267 }
    268 
    269 /* Clear selection. */
    270 void
    271 screen_clear_selection(struct screen *s)
    272 {
    273 	struct screen_sel	*sel = &s->sel;
    274 
    275 	sel->flag = 0;
    276 }
    277 
    278 /* Check if cell in selection. */
    279 int
    280 screen_check_selection(struct screen *s, u_int px, u_int py)
    281 {
    282 	struct screen_sel	*sel = &s->sel;
    283 
    284 	if (!sel->flag)
    285 		return (0);
    286 
    287 	if (sel->rectflag) {
    288 		if (sel->sy < sel->ey) {
    289 			/* start line < end line -- downward selection. */
    290 			if (py < sel->sy || py > sel->ey)
    291 				return (0);
    292 		} else if (sel->sy > sel->ey) {
    293 			/* start line > end line -- upward selection. */
    294 			if (py > sel->sy || py < sel->ey)
    295 				return (0);
    296 		} else {
    297 			/* starting line == ending line. */
    298 			if (py != sel->sy)
    299 				return (0);
    300 		}
    301 
    302 		/*
    303 		 * Need to include the selection start row, but not the cursor
    304 		 * row, which means the selection changes depending on which
    305 		 * one is on the left.
    306 		 */
    307 		if (sel->ex < sel->sx) {
    308 			/* Cursor (ex) is on the left. */
    309 			if (px < sel->ex)
    310 				return (0);
    311 
    312 			if (px > sel->sx)
    313 				return (0);
    314 		} else {
    315 			/* Selection start (sx) is on the left. */
    316 			if (px < sel->sx)
    317 				return (0);
    318 
    319 			if (px > sel->ex)
    320 				return (0);
    321 		}
    322 	} else {
    323 		/*
    324 		 * Like emacs, keep the top-left-most character, and drop the
    325 		 * bottom-right-most, regardless of copy direction.
    326 		 */
    327 		if (sel->sy < sel->ey) {
    328 			/* starting line < ending line -- downward selection. */
    329 			if (py < sel->sy || py > sel->ey)
    330 				return (0);
    331 
    332 			if ((py == sel->sy && px < sel->sx)
    333 			    || (py == sel->ey && px > sel->ex))
    334 				return (0);
    335 		} else if (sel->sy > sel->ey) {
    336 			/* starting line > ending line -- upward selection. */
    337 			if (py > sel->sy || py < sel->ey)
    338 				return (0);
    339 
    340 			if ((py == sel->sy && px >= sel->sx)
    341 			    || (py == sel->ey && px < sel->ex))
    342 				return (0);
    343 		} else {
    344 			/* starting line == ending line. */
    345 			if (py != sel->sy)
    346 				return (0);
    347 
    348 			if (sel->ex < sel->sx) {
    349 				/* cursor (ex) is on the left */
    350 				if (px > sel->sx || px < sel->ex)
    351 					return (0);
    352 			} else {
    353 				/* selection start (sx) is on the left */
    354 				if (px < sel->sx || px > sel->ex)
    355 					return (0);
    356 			}
    357 		}
    358 	}
    359 
    360 	return (1);
    361 }
    362