1 /* $OpenBSD: screen.c,v 1.40 2016/09/02 20:57:20 nicm Exp $ */ 2 3 /* 4 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com> 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 <stdlib.h> 22 #include <string.h> 23 #include <unistd.h> 24 25 #include "tmux.h" 26 27 void screen_resize_x(struct screen *, u_int); 28 void screen_resize_y(struct screen *, u_int); 29 30 /* Create a new screen. */ 31 void 32 screen_init(struct screen *s, u_int sx, u_int sy, u_int hlimit) 33 { 34 s->grid = grid_create(sx, sy, hlimit); 35 s->title = xstrdup(""); 36 37 s->cstyle = 0; 38 s->ccolour = xstrdup(""); 39 s->tabs = NULL; 40 41 s->dirty = NULL; 42 s->dirtysize = 0; 43 44 screen_reinit(s); 45 } 46 47 /* Reinitialise screen. */ 48 void 49 screen_reinit(struct screen *s) 50 { 51 s->cx = 0; 52 s->cy = 0; 53 54 s->rupper = 0; 55 s->rlower = screen_size_y(s) - 1; 56 57 s->mode = MODE_CURSOR | MODE_WRAP; 58 59 screen_reset_tabs(s); 60 61 grid_clear_lines(s->grid, s->grid->hsize, s->grid->sy); 62 63 screen_clear_selection(s); 64 } 65 66 /* Destroy a screen. */ 67 void 68 screen_free(struct screen *s) 69 { 70 free(s->dirty); 71 free(s->tabs); 72 free(s->title); 73 free(s->ccolour); 74 grid_destroy(s->grid); 75 } 76 77 /* Reset tabs to default, eight spaces apart. */ 78 void 79 screen_reset_tabs(struct screen *s) 80 { 81 u_int i; 82 83 free(s->tabs); 84 85 if ((s->tabs = bit_alloc(screen_size_x(s))) == NULL) 86 fatal("bit_alloc failed"); 87 for (i = 8; i < screen_size_x(s); i += 8) 88 bit_set(s->tabs, i); 89 } 90 91 /* Set screen cursor style. */ 92 void 93 screen_set_cursor_style(struct screen *s, u_int style) 94 { 95 if (style <= 6) 96 s->cstyle = style; 97 } 98 99 /* Set screen cursor colour. */ 100 void 101 screen_set_cursor_colour(struct screen *s, const char *colour) 102 { 103 free(s->ccolour); 104 s->ccolour = xstrdup(colour); 105 } 106 107 /* Set screen title. */ 108 void 109 screen_set_title(struct screen *s, const char *title) 110 { 111 free(s->title); 112 s->title = xstrdup(title); 113 } 114 115 /* Resize screen. */ 116 void 117 screen_resize(struct screen *s, u_int sx, u_int sy, int reflow) 118 { 119 if (sx < 1) 120 sx = 1; 121 if (sy < 1) 122 sy = 1; 123 124 if (sx != screen_size_x(s)) { 125 screen_resize_x(s, sx); 126 127 /* 128 * It is unclear what should happen to tabs on resize. xterm 129 * seems to try and maintain them, rxvt resets them. Resetting 130 * is simpler and more reliable so let's do that. 131 */ 132 screen_reset_tabs(s); 133 } 134 135 if (sy != screen_size_y(s)) 136 screen_resize_y(s, sy); 137 138 if (reflow) 139 screen_reflow(s, sx); 140 } 141 142 void 143 screen_resize_x(struct screen *s, u_int sx) 144 { 145 struct grid *gd = s->grid; 146 147 if (sx == 0) 148 fatalx("zero size"); 149 150 /* 151 * Treat resizing horizontally simply: just ensure the cursor is 152 * on-screen and change the size. Don't bother to truncate any lines - 153 * then the data should be accessible if the size is then increased. 154 * 155 * The only potential wrinkle is if UTF-8 double-width characters are 156 * left in the last column, but UTF-8 terminals should deal with this 157 * sanely. 158 */ 159 if (s->cx >= sx) 160 s->cx = sx - 1; 161 gd->sx = sx; 162 } 163 164 void 165 screen_resize_y(struct screen *s, u_int sy) 166 { 167 struct grid *gd = s->grid; 168 u_int needed, available, oldy, i; 169 170 if (sy == 0) 171 fatalx("zero size"); 172 oldy = screen_size_y(s); 173 174 /* 175 * When resizing: 176 * 177 * If the height is decreasing, delete lines from the bottom until 178 * hitting the cursor, then push lines from the top into the history. 179 * 180 * When increasing, pull as many lines as possible from scrolled 181 * history (not explicitly cleared from view) to the top, then fill the 182 * remaining with blanks at the bottom. 183 */ 184 185 /* Size decreasing. */ 186 if (sy < oldy) { 187 needed = oldy - sy; 188 189 /* Delete as many lines as possible from the bottom. */ 190 available = oldy - 1 - s->cy; 191 if (available > 0) { 192 if (available > needed) 193 available = needed; 194 grid_view_delete_lines(gd, oldy - available, available); 195 } 196 needed -= available; 197 198 /* 199 * Now just increase the history size, if possible, to take 200 * over the lines which are left. If history is off, delete 201 * lines from the top. 202 */ 203 available = s->cy; 204 if (gd->flags & GRID_HISTORY) { 205 gd->hscrolled += needed; 206 gd->hsize += needed; 207 } else if (needed > 0 && available > 0) { 208 if (available > needed) 209 available = needed; 210 grid_view_delete_lines(gd, 0, available); 211 } 212 s->cy -= needed; 213 } 214 215 /* Resize line arrays. */ 216 gd->linedata = xreallocarray(gd->linedata, gd->hsize + sy, 217 sizeof *gd->linedata); 218 219 /* Size increasing. */ 220 if (sy > oldy) { 221 needed = sy - oldy; 222 223 /* 224 * Try to pull as much as possible out of scrolled history, if is 225 * is enabled. 226 */ 227 available = gd->hscrolled; 228 if (gd->flags & GRID_HISTORY && available > 0) { 229 if (available > needed) 230 available = needed; 231 gd->hscrolled -= available; 232 gd->hsize -= available; 233 s->cy += available; 234 } else 235 available = 0; 236 needed -= available; 237 238 /* Then fill the rest in with blanks. */ 239 for (i = gd->hsize + sy - needed; i < gd->hsize + sy; i++) 240 memset(&gd->linedata[i], 0, sizeof gd->linedata[i]); 241 } 242 243 /* Set the new size, and reset the scroll region. */ 244 gd->sy = sy; 245 s->rupper = 0; 246 s->rlower = screen_size_y(s) - 1; 247 } 248 249 /* Set selection. */ 250 void 251 screen_set_selection(struct screen *s, u_int sx, u_int sy, 252 u_int ex, u_int ey, u_int rectflag, struct grid_cell *gc) 253 { 254 struct screen_sel *sel = &s->sel; 255 256 memcpy(&sel->cell, gc, sizeof sel->cell); 257 sel->flag = 1; 258 sel->rectflag = rectflag; 259 260 sel->sx = sx; sel->sy = sy; 261 sel->ex = ex; sel->ey = ey; 262 } 263 264 /* Clear selection. */ 265 void 266 screen_clear_selection(struct screen *s) 267 { 268 struct screen_sel *sel = &s->sel; 269 270 sel->flag = 0; 271 sel->lineflag = LINE_SEL_NONE; 272 } 273 274 /* Check if cell in selection. */ 275 int 276 screen_check_selection(struct screen *s, u_int px, u_int py) 277 { 278 struct screen_sel *sel = &s->sel; 279 u_int xx; 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 return (0); 331 332 if (py == sel->ey && px > sel->ex) 333 return (0); 334 } else if (sel->sy > sel->ey) { 335 /* starting line > ending line -- upward selection. */ 336 if (py > sel->sy || py < sel->ey) 337 return (0); 338 339 if (py == sel->ey && px < sel->ex) 340 return (0); 341 342 if (sel->modekeys == MODEKEY_EMACS) 343 xx = sel->sx - 1; 344 else 345 xx = sel->sx; 346 if (py == sel->sy && px > xx) 347 return (0); 348 } else { 349 /* starting line == ending line. */ 350 if (py != sel->sy) 351 return (0); 352 353 if (sel->ex < sel->sx) { 354 /* cursor (ex) is on the left */ 355 if (sel->modekeys == MODEKEY_EMACS) 356 xx = sel->sx - 1; 357 else 358 xx = sel->sx; 359 if (px > xx || px < sel->ex) 360 return (0); 361 } else { 362 /* selection start (sx) is on the left */ 363 if (px < sel->sx || px > sel->ex) 364 return (0); 365 } 366 } 367 } 368 369 return (1); 370 } 371 372 /* Reflow wrapped lines. */ 373 void 374 screen_reflow(struct screen *s, u_int new_x) 375 { 376 struct grid *old = s->grid; 377 u_int change; 378 379 s->grid = grid_create(old->sx, old->sy, old->hlimit); 380 381 change = grid_reflow(s->grid, old, new_x); 382 if (change < s->cy) 383 s->cy -= change; 384 else 385 s->cy = 0; 386 } 387