1 /* $OpenBSD: screen.c,v 1.50 2017/11/15 19:21:24 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 #include <vis.h> 25 26 #include "tmux.h" 27 28 struct screen_title_entry { 29 char *text; 30 31 TAILQ_ENTRY(screen_title_entry) entry; 32 }; 33 TAILQ_HEAD(screen_titles, screen_title_entry); 34 35 static void screen_resize_x(struct screen *, u_int); 36 static void screen_resize_y(struct screen *, u_int); 37 38 static void screen_reflow(struct screen *, u_int); 39 40 /* Free titles stack. */ 41 static void 42 screen_free_titles(struct screen *s) 43 { 44 struct screen_title_entry *title_entry; 45 46 if (s->titles == NULL) 47 return; 48 49 while ((title_entry = TAILQ_FIRST(s->titles)) != NULL) { 50 TAILQ_REMOVE(s->titles, title_entry, entry); 51 free(title_entry->text); 52 free(title_entry); 53 } 54 55 free(s->titles); 56 s->titles = NULL; 57 } 58 59 /* Create a new screen. */ 60 void 61 screen_init(struct screen *s, u_int sx, u_int sy, u_int hlimit) 62 { 63 s->grid = grid_create(sx, sy, hlimit); 64 s->title = xstrdup(""); 65 s->titles = NULL; 66 67 s->cstyle = 0; 68 s->ccolour = xstrdup(""); 69 s->tabs = NULL; 70 71 screen_reinit(s); 72 } 73 74 /* Reinitialise screen. */ 75 void 76 screen_reinit(struct screen *s) 77 { 78 s->cx = 0; 79 s->cy = 0; 80 81 s->rupper = 0; 82 s->rlower = screen_size_y(s) - 1; 83 84 s->mode = MODE_CURSOR | MODE_WRAP; 85 86 screen_reset_tabs(s); 87 88 grid_clear_lines(s->grid, s->grid->hsize, s->grid->sy, 8); 89 90 screen_clear_selection(s); 91 screen_free_titles(s); 92 } 93 94 /* Destroy a screen. */ 95 void 96 screen_free(struct screen *s) 97 { 98 free(s->tabs); 99 free(s->title); 100 free(s->ccolour); 101 102 grid_destroy(s->grid); 103 104 screen_free_titles(s); 105 } 106 107 /* Reset tabs to default, eight spaces apart. */ 108 void 109 screen_reset_tabs(struct screen *s) 110 { 111 u_int i; 112 113 free(s->tabs); 114 115 if ((s->tabs = bit_alloc(screen_size_x(s))) == NULL) 116 fatal("bit_alloc failed"); 117 for (i = 8; i < screen_size_x(s); i += 8) 118 bit_set(s->tabs, i); 119 } 120 121 /* Set screen cursor style. */ 122 void 123 screen_set_cursor_style(struct screen *s, u_int style) 124 { 125 if (style <= 6) 126 s->cstyle = style; 127 } 128 129 /* Set screen cursor colour. */ 130 void 131 screen_set_cursor_colour(struct screen *s, const char *colour) 132 { 133 free(s->ccolour); 134 s->ccolour = xstrdup(colour); 135 } 136 137 /* Set screen title. */ 138 void 139 screen_set_title(struct screen *s, const char *title) 140 { 141 free(s->title); 142 utf8_stravis(&s->title, title, VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL); 143 } 144 145 /* Push the current title onto the stack. */ 146 void 147 screen_push_title(struct screen *s) 148 { 149 struct screen_title_entry *title_entry; 150 151 if (s->titles == NULL) { 152 s->titles = xmalloc(sizeof *s->titles); 153 TAILQ_INIT(s->titles); 154 } 155 title_entry = xmalloc(sizeof *title_entry); 156 title_entry->text = xstrdup(s->title); 157 TAILQ_INSERT_HEAD(s->titles, title_entry, entry); 158 } 159 160 /* 161 * Pop a title from the stack and set it as the screen title. If the stack is 162 * empty, do nothing. 163 */ 164 void 165 screen_pop_title(struct screen *s) 166 { 167 struct screen_title_entry *title_entry; 168 169 if (s->titles == NULL) 170 return; 171 172 title_entry = TAILQ_FIRST(s->titles); 173 if (title_entry != NULL) { 174 screen_set_title(s, title_entry->text); 175 176 TAILQ_REMOVE(s->titles, title_entry, entry); 177 free(title_entry->text); 178 free(title_entry); 179 } 180 } 181 182 /* Resize screen. */ 183 void 184 screen_resize(struct screen *s, u_int sx, u_int sy, int reflow) 185 { 186 if (sx < 1) 187 sx = 1; 188 if (sy < 1) 189 sy = 1; 190 191 if (sx != screen_size_x(s)) { 192 screen_resize_x(s, sx); 193 194 /* 195 * It is unclear what should happen to tabs on resize. xterm 196 * seems to try and maintain them, rxvt resets them. Resetting 197 * is simpler and more reliable so let's do that. 198 */ 199 screen_reset_tabs(s); 200 } else 201 reflow = 0; 202 203 if (sy != screen_size_y(s)) 204 screen_resize_y(s, sy); 205 206 if (reflow) 207 screen_reflow(s, sx); 208 } 209 210 static void 211 screen_resize_x(struct screen *s, u_int sx) 212 { 213 struct grid *gd = s->grid; 214 215 if (sx == 0) 216 fatalx("zero size"); 217 218 /* 219 * Treat resizing horizontally simply: just ensure the cursor is 220 * on-screen and change the size. Don't bother to truncate any lines - 221 * then the data should be accessible if the size is then increased. 222 * 223 * The only potential wrinkle is if UTF-8 double-width characters are 224 * left in the last column, but UTF-8 terminals should deal with this 225 * sanely. 226 */ 227 if (s->cx >= sx) 228 s->cx = sx - 1; 229 gd->sx = sx; 230 } 231 232 static void 233 screen_resize_y(struct screen *s, u_int sy) 234 { 235 struct grid *gd = s->grid; 236 u_int needed, available, oldy, i; 237 238 if (sy == 0) 239 fatalx("zero size"); 240 oldy = screen_size_y(s); 241 242 /* 243 * When resizing: 244 * 245 * If the height is decreasing, delete lines from the bottom until 246 * hitting the cursor, then push lines from the top into the history. 247 * 248 * When increasing, pull as many lines as possible from scrolled 249 * history (not explicitly cleared from view) to the top, then fill the 250 * remaining with blanks at the bottom. 251 */ 252 253 /* Size decreasing. */ 254 if (sy < oldy) { 255 needed = oldy - sy; 256 257 /* Delete as many lines as possible from the bottom. */ 258 available = oldy - 1 - s->cy; 259 if (available > 0) { 260 if (available > needed) 261 available = needed; 262 grid_view_delete_lines(gd, oldy - available, available, 263 8); 264 } 265 needed -= available; 266 267 /* 268 * Now just increase the history size, if possible, to take 269 * over the lines which are left. If history is off, delete 270 * lines from the top. 271 */ 272 available = s->cy; 273 if (gd->flags & GRID_HISTORY) { 274 gd->hscrolled += needed; 275 gd->hsize += needed; 276 } else if (needed > 0 && available > 0) { 277 if (available > needed) 278 available = needed; 279 grid_view_delete_lines(gd, 0, available, 8); 280 } 281 s->cy -= needed; 282 } 283 284 /* Resize line arrays. */ 285 gd->linedata = xreallocarray(gd->linedata, gd->hsize + sy, 286 sizeof *gd->linedata); 287 288 /* Size increasing. */ 289 if (sy > oldy) { 290 needed = sy - oldy; 291 292 /* 293 * Try to pull as much as possible out of scrolled history, if 294 * is is enabled. 295 */ 296 available = gd->hscrolled; 297 if (gd->flags & GRID_HISTORY && available > 0) { 298 if (available > needed) 299 available = needed; 300 gd->hscrolled -= available; 301 gd->hsize -= available; 302 s->cy += available; 303 } else 304 available = 0; 305 needed -= available; 306 307 /* Then fill the rest in with blanks. */ 308 for (i = gd->hsize + sy - needed; i < gd->hsize + sy; i++) 309 memset(&gd->linedata[i], 0, sizeof gd->linedata[i]); 310 } 311 312 /* Set the new size, and reset the scroll region. */ 313 gd->sy = sy; 314 s->rupper = 0; 315 s->rlower = screen_size_y(s) - 1; 316 } 317 318 /* Set selection. */ 319 void 320 screen_set_selection(struct screen *s, u_int sx, u_int sy, 321 u_int ex, u_int ey, u_int rectflag, struct grid_cell *gc) 322 { 323 struct screen_sel *sel = &s->sel; 324 325 memcpy(&sel->cell, gc, sizeof sel->cell); 326 sel->flag = 1; 327 sel->hidden = 0; 328 329 sel->rectflag = rectflag; 330 331 sel->sx = sx; sel->sy = sy; 332 sel->ex = ex; sel->ey = ey; 333 } 334 335 /* Clear selection. */ 336 void 337 screen_clear_selection(struct screen *s) 338 { 339 struct screen_sel *sel = &s->sel; 340 341 sel->flag = 0; 342 sel->hidden = 0; 343 sel->lineflag = LINE_SEL_NONE; 344 } 345 346 /* Hide selection. */ 347 void 348 screen_hide_selection(struct screen *s) 349 { 350 struct screen_sel *sel = &s->sel; 351 352 sel->hidden = 1; 353 } 354 355 /* Check if cell in selection. */ 356 int 357 screen_check_selection(struct screen *s, u_int px, u_int py) 358 { 359 struct screen_sel *sel = &s->sel; 360 u_int xx; 361 362 if (!sel->flag || sel->hidden) 363 return (0); 364 365 if (sel->rectflag) { 366 if (sel->sy < sel->ey) { 367 /* start line < end line -- downward selection. */ 368 if (py < sel->sy || py > sel->ey) 369 return (0); 370 } else if (sel->sy > sel->ey) { 371 /* start line > end line -- upward selection. */ 372 if (py > sel->sy || py < sel->ey) 373 return (0); 374 } else { 375 /* starting line == ending line. */ 376 if (py != sel->sy) 377 return (0); 378 } 379 380 /* 381 * Need to include the selection start row, but not the cursor 382 * row, which means the selection changes depending on which 383 * one is on the left. 384 */ 385 if (sel->ex < sel->sx) { 386 /* Cursor (ex) is on the left. */ 387 if (px < sel->ex) 388 return (0); 389 390 if (px > sel->sx) 391 return (0); 392 } else { 393 /* Selection start (sx) is on the left. */ 394 if (px < sel->sx) 395 return (0); 396 397 if (px > sel->ex) 398 return (0); 399 } 400 } else { 401 /* 402 * Like emacs, keep the top-left-most character, and drop the 403 * bottom-right-most, regardless of copy direction. 404 */ 405 if (sel->sy < sel->ey) { 406 /* starting line < ending line -- downward selection. */ 407 if (py < sel->sy || py > sel->ey) 408 return (0); 409 410 if (py == sel->sy && px < sel->sx) 411 return (0); 412 413 if (py == sel->ey && px > sel->ex) 414 return (0); 415 } else if (sel->sy > sel->ey) { 416 /* starting line > ending line -- upward selection. */ 417 if (py > sel->sy || py < sel->ey) 418 return (0); 419 420 if (py == sel->ey && px < sel->ex) 421 return (0); 422 423 if (sel->modekeys == MODEKEY_EMACS) 424 xx = sel->sx - 1; 425 else 426 xx = sel->sx; 427 if (py == sel->sy && (sel->sx == 0 || px > xx)) 428 return (0); 429 } else { 430 /* starting line == ending line. */ 431 if (py != sel->sy) 432 return (0); 433 434 if (sel->ex < sel->sx) { 435 /* cursor (ex) is on the left */ 436 if (sel->modekeys == MODEKEY_EMACS) 437 xx = sel->sx - 1; 438 else 439 xx = sel->sx; 440 if (px > xx || px < sel->ex) 441 return (0); 442 } else { 443 /* selection start (sx) is on the left */ 444 if (px < sel->sx || px > sel->ex) 445 return (0); 446 } 447 } 448 } 449 450 return (1); 451 } 452 453 /* Get selected grid cell. */ 454 void 455 screen_select_cell(struct screen *s, struct grid_cell *dst, 456 const struct grid_cell *src) 457 { 458 if (!s->sel.flag || s->sel.hidden) 459 return; 460 461 memcpy(dst, &s->sel.cell, sizeof *dst); 462 463 utf8_copy(&dst->data, &src->data); 464 dst->attr = dst->attr & ~GRID_ATTR_CHARSET; 465 dst->attr |= src->attr & GRID_ATTR_CHARSET; 466 dst->flags = src->flags; 467 } 468 469 /* Reflow wrapped lines. */ 470 static void 471 screen_reflow(struct screen *s, u_int new_x) 472 { 473 grid_reflow(s->grid, new_x, &s->cy); 474 } 475