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