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