1 /* $OpenBSD$ */ 2 3 /* 4 * Copyright (c) 2009 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 #include <sys/ioctl.h> 21 #include <sys/uio.h> 22 23 #include <errno.h> 24 #include <fcntl.h> 25 #include <stdlib.h> 26 #include <string.h> 27 #include <time.h> 28 #include <unistd.h> 29 30 #include "tmux.h" 31 32 static void server_client_free(int, short, void *); 33 static void server_client_check_pane_focus(struct window_pane *); 34 static void server_client_check_pane_resize(struct window_pane *); 35 static void server_client_check_pane_buffer(struct window_pane *); 36 static void server_client_check_window_resize(struct window *); 37 static key_code server_client_check_mouse(struct client *, struct key_event *); 38 static void server_client_repeat_timer(int, short, void *); 39 static void server_client_click_timer(int, short, void *); 40 static void server_client_check_exit(struct client *); 41 static void server_client_check_redraw(struct client *); 42 static void server_client_check_modes(struct client *); 43 static void server_client_set_title(struct client *); 44 static void server_client_reset_state(struct client *); 45 static int server_client_assume_paste(struct session *); 46 static void server_client_update_latest(struct client *); 47 48 static void server_client_dispatch(struct imsg *, void *); 49 static void server_client_dispatch_command(struct client *, struct imsg *); 50 static void server_client_dispatch_identify(struct client *, struct imsg *); 51 static void server_client_dispatch_shell(struct client *); 52 53 /* Compare client windows. */ 54 static int 55 server_client_window_cmp(struct client_window *cw1, 56 struct client_window *cw2) 57 { 58 if (cw1->window < cw2->window) 59 return (-1); 60 if (cw1->window > cw2->window) 61 return (1); 62 return (0); 63 } 64 RB_GENERATE(client_windows, client_window, entry, server_client_window_cmp); 65 66 /* Number of attached clients. */ 67 u_int 68 server_client_how_many(void) 69 { 70 struct client *c; 71 u_int n; 72 73 n = 0; 74 TAILQ_FOREACH(c, &clients, entry) { 75 if (c->session != NULL && (~c->flags & CLIENT_UNATTACHEDFLAGS)) 76 n++; 77 } 78 return (n); 79 } 80 81 /* Overlay timer callback. */ 82 static void 83 server_client_overlay_timer(__unused int fd, __unused short events, void *data) 84 { 85 server_client_clear_overlay(data); 86 } 87 88 /* Set an overlay on client. */ 89 void 90 server_client_set_overlay(struct client *c, u_int delay, 91 overlay_check_cb checkcb, overlay_mode_cb modecb, 92 overlay_draw_cb drawcb, overlay_key_cb keycb, overlay_free_cb freecb, 93 void *data) 94 { 95 struct timeval tv; 96 97 if (c->overlay_draw != NULL) 98 server_client_clear_overlay(c); 99 100 tv.tv_sec = delay / 1000; 101 tv.tv_usec = (delay % 1000) * 1000L; 102 103 if (event_initialized(&c->overlay_timer)) 104 evtimer_del(&c->overlay_timer); 105 evtimer_set(&c->overlay_timer, server_client_overlay_timer, c); 106 if (delay != 0) 107 evtimer_add(&c->overlay_timer, &tv); 108 109 c->overlay_check = checkcb; 110 c->overlay_mode = modecb; 111 c->overlay_draw = drawcb; 112 c->overlay_key = keycb; 113 c->overlay_free = freecb; 114 c->overlay_data = data; 115 116 c->tty.flags |= TTY_FREEZE; 117 if (c->overlay_mode == NULL) 118 c->tty.flags |= TTY_NOCURSOR; 119 server_redraw_client(c); 120 } 121 122 /* Clear overlay mode on client. */ 123 void 124 server_client_clear_overlay(struct client *c) 125 { 126 if (c->overlay_draw == NULL) 127 return; 128 129 if (event_initialized(&c->overlay_timer)) 130 evtimer_del(&c->overlay_timer); 131 132 if (c->overlay_free != NULL) 133 c->overlay_free(c); 134 135 c->overlay_check = NULL; 136 c->overlay_mode = NULL; 137 c->overlay_draw = NULL; 138 c->overlay_key = NULL; 139 c->overlay_free = NULL; 140 c->overlay_data = NULL; 141 142 c->tty.flags &= ~(TTY_FREEZE|TTY_NOCURSOR); 143 server_redraw_client(c); 144 } 145 146 /* Check if this client is inside this server. */ 147 int 148 server_client_check_nested(struct client *c) 149 { 150 struct environ_entry *envent; 151 struct window_pane *wp; 152 153 envent = environ_find(c->environ, "TMUX"); 154 if (envent == NULL || *envent->value == '\0') 155 return (0); 156 157 RB_FOREACH(wp, window_pane_tree, &all_window_panes) { 158 if (strcmp(wp->tty, c->ttyname) == 0) 159 return (1); 160 } 161 return (0); 162 } 163 164 /* Set client key table. */ 165 void 166 server_client_set_key_table(struct client *c, const char *name) 167 { 168 if (name == NULL) 169 name = server_client_get_key_table(c); 170 171 key_bindings_unref_table(c->keytable); 172 c->keytable = key_bindings_get_table(name, 1); 173 c->keytable->references++; 174 } 175 176 /* Get default key table. */ 177 const char * 178 server_client_get_key_table(struct client *c) 179 { 180 struct session *s = c->session; 181 const char *name; 182 183 if (s == NULL) 184 return ("root"); 185 186 name = options_get_string(s->options, "key-table"); 187 if (*name == '\0') 188 return ("root"); 189 return (name); 190 } 191 192 /* Is this table the default key table? */ 193 static int 194 server_client_is_default_key_table(struct client *c, struct key_table *table) 195 { 196 return (strcmp(table->name, server_client_get_key_table(c)) == 0); 197 } 198 199 /* Create a new client. */ 200 struct client * 201 server_client_create(int fd) 202 { 203 struct client *c; 204 205 setblocking(fd, 0); 206 207 c = xcalloc(1, sizeof *c); 208 c->references = 1; 209 c->peer = proc_add_peer(server_proc, fd, server_client_dispatch, c); 210 211 if (gettimeofday(&c->creation_time, NULL) != 0) 212 fatal("gettimeofday failed"); 213 memcpy(&c->activity_time, &c->creation_time, sizeof c->activity_time); 214 215 c->environ = environ_create(); 216 217 c->fd = -1; 218 c->out_fd = -1; 219 220 c->queue = cmdq_new(); 221 RB_INIT(&c->windows); 222 RB_INIT(&c->files); 223 224 c->tty.sx = 80; 225 c->tty.sy = 24; 226 227 status_init(c); 228 c->flags |= CLIENT_FOCUSED; 229 230 c->keytable = key_bindings_get_table("root", 1); 231 c->keytable->references++; 232 233 evtimer_set(&c->repeat_timer, server_client_repeat_timer, c); 234 evtimer_set(&c->click_timer, server_client_click_timer, c); 235 236 TAILQ_INSERT_TAIL(&clients, c, entry); 237 log_debug("new client %p", c); 238 return (c); 239 } 240 241 /* Open client terminal if needed. */ 242 int 243 server_client_open(struct client *c, char **cause) 244 { 245 const char *ttynam = _PATH_TTY; 246 247 if (c->flags & CLIENT_CONTROL) 248 return (0); 249 250 if (strcmp(c->ttyname, ttynam) == 0|| 251 ((isatty(STDIN_FILENO) && 252 (ttynam = ttyname(STDIN_FILENO)) != NULL && 253 strcmp(c->ttyname, ttynam) == 0) || 254 (isatty(STDOUT_FILENO) && 255 (ttynam = ttyname(STDOUT_FILENO)) != NULL && 256 strcmp(c->ttyname, ttynam) == 0) || 257 (isatty(STDERR_FILENO) && 258 (ttynam = ttyname(STDERR_FILENO)) != NULL && 259 strcmp(c->ttyname, ttynam) == 0))) { 260 xasprintf(cause, "can't use %s", c->ttyname); 261 return (-1); 262 } 263 264 if (!(c->flags & CLIENT_TERMINAL)) { 265 *cause = xstrdup("not a terminal"); 266 return (-1); 267 } 268 269 if (tty_open(&c->tty, cause) != 0) 270 return (-1); 271 272 return (0); 273 } 274 275 /* Lost an attached client. */ 276 static void 277 server_client_attached_lost(struct client *c) 278 { 279 struct session *s = c->session; 280 struct window *w; 281 struct client *loop; 282 struct client *found; 283 284 log_debug("lost attached client %p", c); 285 286 /* 287 * By this point the session in the client has been cleared so walk all 288 * windows to find any with this client as the latest. 289 */ 290 RB_FOREACH(w, windows, &windows) { 291 if (w->latest != c) 292 continue; 293 294 found = NULL; 295 TAILQ_FOREACH(loop, &clients, entry) { 296 s = loop->session; 297 if (loop == c || s == NULL || s->curw->window != w) 298 continue; 299 if (found == NULL || 300 timercmp(&loop->activity_time, &found->activity_time, 301 >)) 302 found = loop; 303 } 304 if (found != NULL) 305 server_client_update_latest(found); 306 } 307 } 308 309 /* Lost a client. */ 310 void 311 server_client_lost(struct client *c) 312 { 313 struct client_file *cf, *cf1; 314 struct client_window *cw, *cw1; 315 316 c->flags |= CLIENT_DEAD; 317 318 server_client_clear_overlay(c); 319 status_prompt_clear(c); 320 status_message_clear(c); 321 322 RB_FOREACH_SAFE(cf, client_files, &c->files, cf1) { 323 cf->error = EINTR; 324 file_fire_done(cf); 325 } 326 RB_FOREACH_SAFE(cw, client_windows, &c->windows, cw1) { 327 RB_REMOVE(client_windows, &c->windows, cw); 328 free(cw); 329 } 330 331 TAILQ_REMOVE(&clients, c, entry); 332 log_debug("lost client %p", c); 333 334 if (c->flags & CLIENT_ATTACHED) { 335 server_client_attached_lost(c); 336 notify_client("client-detached", c); 337 } 338 339 if (c->flags & CLIENT_CONTROL) 340 control_stop(c); 341 if (c->flags & CLIENT_TERMINAL) 342 tty_free(&c->tty); 343 free(c->ttyname); 344 345 free(c->term_name); 346 free(c->term_type); 347 tty_term_free_list(c->term_caps, c->term_ncaps); 348 349 status_free(c); 350 351 free(c->title); 352 free(__UNCONST(c->cwd)); 353 354 evtimer_del(&c->repeat_timer); 355 evtimer_del(&c->click_timer); 356 357 key_bindings_unref_table(c->keytable); 358 359 free(c->message_string); 360 if (event_initialized(&c->message_timer)) 361 evtimer_del(&c->message_timer); 362 363 free(c->prompt_saved); 364 free(c->prompt_string); 365 free(c->prompt_buffer); 366 367 format_lost_client(c); 368 environ_free(c->environ); 369 370 proc_remove_peer(c->peer); 371 c->peer = NULL; 372 373 if (c->out_fd != -1) 374 close(c->out_fd); 375 if (c->fd != -1) { 376 close(c->fd); 377 c->fd = -1; 378 } 379 server_client_unref(c); 380 381 server_add_accept(0); /* may be more file descriptors now */ 382 383 recalculate_sizes(); 384 server_check_unattached(); 385 server_update_socket(); 386 } 387 388 /* Remove reference from a client. */ 389 void 390 server_client_unref(struct client *c) 391 { 392 log_debug("unref client %p (%d references)", c, c->references); 393 394 c->references--; 395 if (c->references == 0) 396 event_once(-1, EV_TIMEOUT, server_client_free, c, NULL); 397 } 398 399 /* Free dead client. */ 400 static void 401 server_client_free(__unused int fd, __unused short events, void *arg) 402 { 403 struct client *c = arg; 404 405 log_debug("free client %p (%d references)", c, c->references); 406 407 cmdq_free(c->queue); 408 409 if (c->references == 0) { 410 free(__UNCONST(c->name)); 411 free(c); 412 } 413 } 414 415 /* Suspend a client. */ 416 void 417 server_client_suspend(struct client *c) 418 { 419 struct session *s = c->session; 420 421 if (s == NULL || (c->flags & CLIENT_UNATTACHEDFLAGS)) 422 return; 423 424 tty_stop_tty(&c->tty); 425 c->flags |= CLIENT_SUSPENDED; 426 proc_send(c->peer, MSG_SUSPEND, -1, NULL, 0); 427 } 428 429 /* Detach a client. */ 430 void 431 server_client_detach(struct client *c, enum msgtype msgtype) 432 { 433 struct session *s = c->session; 434 435 if (s == NULL || (c->flags & CLIENT_UNATTACHEDFLAGS)) 436 return; 437 438 c->flags |= CLIENT_EXIT; 439 440 c->exit_type = CLIENT_EXIT_DETACH; 441 c->exit_msgtype = msgtype; 442 c->exit_session = xstrdup(s->name); 443 } 444 445 /* Execute command to replace a client. */ 446 void 447 server_client_exec(struct client *c, const char *cmd) 448 { 449 struct session *s = c->session; 450 char *msg; 451 const char *shell; 452 size_t cmdsize, shellsize; 453 454 if (*cmd == '\0') 455 return; 456 cmdsize = strlen(cmd) + 1; 457 458 if (s != NULL) 459 shell = options_get_string(s->options, "default-shell"); 460 else 461 shell = options_get_string(global_s_options, "default-shell"); 462 if (!checkshell(shell)) 463 shell = _PATH_BSHELL; 464 shellsize = strlen(shell) + 1; 465 466 msg = xmalloc(cmdsize + shellsize); 467 memcpy(msg, cmd, cmdsize); 468 memcpy(msg + cmdsize, shell, shellsize); 469 470 proc_send(c->peer, MSG_EXEC, -1, msg, cmdsize + shellsize); 471 free(msg); 472 } 473 474 /* Check for mouse keys. */ 475 static key_code 476 server_client_check_mouse(struct client *c, struct key_event *event) 477 { 478 struct mouse_event *m = &event->m; 479 struct session *s = c->session; 480 struct winlink *wl; 481 struct window_pane *wp; 482 u_int x, y, b, sx, sy, px, py; 483 int ignore = 0; 484 key_code key; 485 struct timeval tv; 486 struct style_range *sr; 487 enum { NOTYPE, 488 MOVE, 489 DOWN, 490 UP, 491 DRAG, 492 WHEEL, 493 SECOND, 494 DOUBLE, 495 TRIPLE } type = NOTYPE; 496 enum { NOWHERE, 497 PANE, 498 STATUS, 499 STATUS_LEFT, 500 STATUS_RIGHT, 501 STATUS_DEFAULT, 502 BORDER } where = NOWHERE; 503 504 log_debug("%s mouse %02x at %u,%u (last %u,%u) (%d)", c->name, m->b, 505 m->x, m->y, m->lx, m->ly, c->tty.mouse_drag_flag); 506 507 /* What type of event is this? */ 508 if (event->key == KEYC_DOUBLECLICK) { 509 type = DOUBLE; 510 x = m->x, y = m->y, b = m->b; 511 ignore = 1; 512 log_debug("double-click at %u,%u", x, y); 513 } else if ((m->sgr_type != ' ' && 514 MOUSE_DRAG(m->sgr_b) && 515 MOUSE_BUTTONS(m->sgr_b) == 3) || 516 (m->sgr_type == ' ' && 517 MOUSE_DRAG(m->b) && 518 MOUSE_BUTTONS(m->b) == 3 && 519 MOUSE_BUTTONS(m->lb) == 3)) { 520 type = MOVE; 521 x = m->x, y = m->y, b = 0; 522 log_debug("move at %u,%u", x, y); 523 } else if (MOUSE_DRAG(m->b)) { 524 type = DRAG; 525 if (c->tty.mouse_drag_flag) { 526 x = m->x, y = m->y, b = m->b; 527 if (x == m->lx && y == m->ly) 528 return (KEYC_UNKNOWN); 529 log_debug("drag update at %u,%u", x, y); 530 } else { 531 x = m->lx, y = m->ly, b = m->lb; 532 log_debug("drag start at %u,%u", x, y); 533 } 534 } else if (MOUSE_WHEEL(m->b)) { 535 type = WHEEL; 536 x = m->x, y = m->y, b = m->b; 537 log_debug("wheel at %u,%u", x, y); 538 } else if (MOUSE_RELEASE(m->b)) { 539 type = UP; 540 x = m->x, y = m->y, b = m->lb; 541 log_debug("up at %u,%u", x, y); 542 } else { 543 if (c->flags & CLIENT_DOUBLECLICK) { 544 evtimer_del(&c->click_timer); 545 c->flags &= ~CLIENT_DOUBLECLICK; 546 if (m->b == c->click_button) { 547 type = SECOND; 548 x = m->x, y = m->y, b = m->b; 549 log_debug("second-click at %u,%u", x, y); 550 c->flags |= CLIENT_TRIPLECLICK; 551 } 552 } else if (c->flags & CLIENT_TRIPLECLICK) { 553 evtimer_del(&c->click_timer); 554 c->flags &= ~CLIENT_TRIPLECLICK; 555 if (m->b == c->click_button) { 556 type = TRIPLE; 557 x = m->x, y = m->y, b = m->b; 558 log_debug("triple-click at %u,%u", x, y); 559 goto have_event; 560 } 561 } else { 562 type = DOWN; 563 x = m->x, y = m->y, b = m->b; 564 log_debug("down at %u,%u", x, y); 565 c->flags |= CLIENT_DOUBLECLICK; 566 } 567 568 if (KEYC_CLICK_TIMEOUT != 0) { 569 memcpy(&c->click_event, m, sizeof c->click_event); 570 c->click_button = m->b; 571 572 log_debug("click timer started"); 573 tv.tv_sec = KEYC_CLICK_TIMEOUT / 1000; 574 tv.tv_usec = (KEYC_CLICK_TIMEOUT % 1000) * 1000L; 575 evtimer_del(&c->click_timer); 576 evtimer_add(&c->click_timer, &tv); 577 } 578 } 579 580 have_event: 581 if (type == NOTYPE) 582 return (KEYC_UNKNOWN); 583 584 /* Save the session. */ 585 m->s = s->id; 586 m->w = -1; 587 m->ignore = ignore; 588 589 /* Is this on the status line? */ 590 m->statusat = status_at_line(c); 591 m->statuslines = status_line_size(c); 592 if (m->statusat != -1 && 593 y >= (u_int)m->statusat && 594 y < m->statusat + m->statuslines) { 595 sr = status_get_range(c, x, y - m->statusat); 596 if (sr == NULL) { 597 where = STATUS_DEFAULT; 598 } else { 599 switch (sr->type) { 600 case STYLE_RANGE_NONE: 601 return (KEYC_UNKNOWN); 602 case STYLE_RANGE_LEFT: 603 where = STATUS_LEFT; 604 break; 605 case STYLE_RANGE_RIGHT: 606 where = STATUS_RIGHT; 607 break; 608 case STYLE_RANGE_WINDOW: 609 wl = winlink_find_by_index(&s->windows, 610 sr->argument); 611 if (wl == NULL) 612 return (KEYC_UNKNOWN); 613 m->w = wl->window->id; 614 615 where = STATUS; 616 break; 617 } 618 } 619 } 620 621 /* Not on status line. Adjust position and check for border or pane. */ 622 if (where == NOWHERE) { 623 px = x; 624 if (m->statusat == 0 && y >= m->statuslines) 625 py = y - m->statuslines; 626 else if (m->statusat > 0 && y >= (u_int)m->statusat) 627 py = m->statusat - 1; 628 else 629 py = y; 630 631 tty_window_offset(&c->tty, &m->ox, &m->oy, &sx, &sy); 632 log_debug("mouse window @%u at %u,%u (%ux%u)", 633 s->curw->window->id, m->ox, m->oy, sx, sy); 634 if (px > sx || py > sy) 635 return (KEYC_UNKNOWN); 636 px = px + m->ox; 637 py = py + m->oy; 638 639 /* Try the pane borders if not zoomed. */ 640 if (~s->curw->window->flags & WINDOW_ZOOMED) { 641 TAILQ_FOREACH(wp, &s->curw->window->panes, entry) { 642 if ((wp->xoff + wp->sx == px && 643 wp->yoff <= 1 + py && 644 wp->yoff + wp->sy >= py) || 645 (wp->yoff + wp->sy == py && 646 wp->xoff <= 1 + px && 647 wp->xoff + wp->sx >= px)) 648 break; 649 } 650 if (wp != NULL) 651 where = BORDER; 652 } 653 654 /* Otherwise try inside the pane. */ 655 if (where == NOWHERE) { 656 wp = window_get_active_at(s->curw->window, px, py); 657 if (wp != NULL) 658 where = PANE; 659 else 660 return (KEYC_UNKNOWN); 661 } 662 if (where == PANE) 663 log_debug("mouse %u,%u on pane %%%u", x, y, wp->id); 664 else if (where == BORDER) 665 log_debug("mouse on pane %%%u border", wp->id); 666 m->wp = wp->id; 667 m->w = wp->window->id; 668 } else 669 m->wp = -1; 670 671 /* Stop dragging if needed. */ 672 if (type != DRAG && type != WHEEL && c->tty.mouse_drag_flag) { 673 if (c->tty.mouse_drag_release != NULL) 674 c->tty.mouse_drag_release(c, m); 675 676 c->tty.mouse_drag_update = NULL; 677 c->tty.mouse_drag_release = NULL; 678 679 /* 680 * End a mouse drag by passing a MouseDragEnd key corresponding 681 * to the button that started the drag. 682 */ 683 switch (c->tty.mouse_drag_flag) { 684 case 1: 685 if (where == PANE) 686 key = KEYC_MOUSEDRAGEND1_PANE; 687 if (where == STATUS) 688 key = KEYC_MOUSEDRAGEND1_STATUS; 689 if (where == STATUS_LEFT) 690 key = KEYC_MOUSEDRAGEND1_STATUS_LEFT; 691 if (where == STATUS_RIGHT) 692 key = KEYC_MOUSEDRAGEND1_STATUS_RIGHT; 693 if (where == STATUS_DEFAULT) 694 key = KEYC_MOUSEDRAGEND1_STATUS_DEFAULT; 695 if (where == BORDER) 696 key = KEYC_MOUSEDRAGEND1_BORDER; 697 break; 698 case 2: 699 if (where == PANE) 700 key = KEYC_MOUSEDRAGEND2_PANE; 701 if (where == STATUS) 702 key = KEYC_MOUSEDRAGEND2_STATUS; 703 if (where == STATUS_LEFT) 704 key = KEYC_MOUSEDRAGEND2_STATUS_LEFT; 705 if (where == STATUS_RIGHT) 706 key = KEYC_MOUSEDRAGEND2_STATUS_RIGHT; 707 if (where == STATUS_DEFAULT) 708 key = KEYC_MOUSEDRAGEND2_STATUS_DEFAULT; 709 if (where == BORDER) 710 key = KEYC_MOUSEDRAGEND2_BORDER; 711 break; 712 case 3: 713 if (where == PANE) 714 key = KEYC_MOUSEDRAGEND3_PANE; 715 if (where == STATUS) 716 key = KEYC_MOUSEDRAGEND3_STATUS; 717 if (where == STATUS_LEFT) 718 key = KEYC_MOUSEDRAGEND3_STATUS_LEFT; 719 if (where == STATUS_RIGHT) 720 key = KEYC_MOUSEDRAGEND3_STATUS_RIGHT; 721 if (where == STATUS_DEFAULT) 722 key = KEYC_MOUSEDRAGEND3_STATUS_DEFAULT; 723 if (where == BORDER) 724 key = KEYC_MOUSEDRAGEND3_BORDER; 725 break; 726 default: 727 key = KEYC_MOUSE; 728 break; 729 } 730 c->tty.mouse_drag_flag = 0; 731 goto out; 732 } 733 734 /* Convert to a key binding. */ 735 key = KEYC_UNKNOWN; 736 switch (type) { 737 case NOTYPE: 738 break; 739 case MOVE: 740 if (where == PANE) 741 key = KEYC_MOUSEMOVE_PANE; 742 if (where == STATUS) 743 key = KEYC_MOUSEMOVE_STATUS; 744 if (where == STATUS_LEFT) 745 key = KEYC_MOUSEMOVE_STATUS_LEFT; 746 if (where == STATUS_RIGHT) 747 key = KEYC_MOUSEMOVE_STATUS_RIGHT; 748 if (where == STATUS_DEFAULT) 749 key = KEYC_MOUSEMOVE_STATUS_DEFAULT; 750 if (where == BORDER) 751 key = KEYC_MOUSEMOVE_BORDER; 752 break; 753 case DRAG: 754 if (c->tty.mouse_drag_update != NULL) 755 key = KEYC_DRAGGING; 756 else { 757 switch (MOUSE_BUTTONS(b)) { 758 case 0: 759 if (where == PANE) 760 key = KEYC_MOUSEDRAG1_PANE; 761 if (where == STATUS) 762 key = KEYC_MOUSEDRAG1_STATUS; 763 if (where == STATUS_LEFT) 764 key = KEYC_MOUSEDRAG1_STATUS_LEFT; 765 if (where == STATUS_RIGHT) 766 key = KEYC_MOUSEDRAG1_STATUS_RIGHT; 767 if (where == STATUS_DEFAULT) 768 key = KEYC_MOUSEDRAG1_STATUS_DEFAULT; 769 if (where == BORDER) 770 key = KEYC_MOUSEDRAG1_BORDER; 771 break; 772 case 1: 773 if (where == PANE) 774 key = KEYC_MOUSEDRAG2_PANE; 775 if (where == STATUS) 776 key = KEYC_MOUSEDRAG2_STATUS; 777 if (where == STATUS_LEFT) 778 key = KEYC_MOUSEDRAG2_STATUS_LEFT; 779 if (where == STATUS_RIGHT) 780 key = KEYC_MOUSEDRAG2_STATUS_RIGHT; 781 if (where == STATUS_DEFAULT) 782 key = KEYC_MOUSEDRAG2_STATUS_DEFAULT; 783 if (where == BORDER) 784 key = KEYC_MOUSEDRAG2_BORDER; 785 break; 786 case 2: 787 if (where == PANE) 788 key = KEYC_MOUSEDRAG3_PANE; 789 if (where == STATUS) 790 key = KEYC_MOUSEDRAG3_STATUS; 791 if (where == STATUS_LEFT) 792 key = KEYC_MOUSEDRAG3_STATUS_LEFT; 793 if (where == STATUS_RIGHT) 794 key = KEYC_MOUSEDRAG3_STATUS_RIGHT; 795 if (where == STATUS_DEFAULT) 796 key = KEYC_MOUSEDRAG3_STATUS_DEFAULT; 797 if (where == BORDER) 798 key = KEYC_MOUSEDRAG3_BORDER; 799 break; 800 } 801 } 802 803 /* 804 * Begin a drag by setting the flag to a non-zero value that 805 * corresponds to the mouse button in use. 806 */ 807 c->tty.mouse_drag_flag = MOUSE_BUTTONS(b) + 1; 808 break; 809 case WHEEL: 810 if (MOUSE_BUTTONS(b) == MOUSE_WHEEL_UP) { 811 if (where == PANE) 812 key = KEYC_WHEELUP_PANE; 813 if (where == STATUS) 814 key = KEYC_WHEELUP_STATUS; 815 if (where == STATUS_LEFT) 816 key = KEYC_WHEELUP_STATUS_LEFT; 817 if (where == STATUS_RIGHT) 818 key = KEYC_WHEELUP_STATUS_RIGHT; 819 if (where == STATUS_DEFAULT) 820 key = KEYC_WHEELUP_STATUS_DEFAULT; 821 if (where == BORDER) 822 key = KEYC_WHEELUP_BORDER; 823 } else { 824 if (where == PANE) 825 key = KEYC_WHEELDOWN_PANE; 826 if (where == STATUS) 827 key = KEYC_WHEELDOWN_STATUS; 828 if (where == STATUS_LEFT) 829 key = KEYC_WHEELDOWN_STATUS_LEFT; 830 if (where == STATUS_RIGHT) 831 key = KEYC_WHEELDOWN_STATUS_RIGHT; 832 if (where == STATUS_DEFAULT) 833 key = KEYC_WHEELDOWN_STATUS_DEFAULT; 834 if (where == BORDER) 835 key = KEYC_WHEELDOWN_BORDER; 836 } 837 break; 838 case UP: 839 switch (MOUSE_BUTTONS(b)) { 840 case 0: 841 if (where == PANE) 842 key = KEYC_MOUSEUP1_PANE; 843 if (where == STATUS) 844 key = KEYC_MOUSEUP1_STATUS; 845 if (where == STATUS_LEFT) 846 key = KEYC_MOUSEUP1_STATUS_LEFT; 847 if (where == STATUS_RIGHT) 848 key = KEYC_MOUSEUP1_STATUS_RIGHT; 849 if (where == STATUS_DEFAULT) 850 key = KEYC_MOUSEUP1_STATUS_DEFAULT; 851 if (where == BORDER) 852 key = KEYC_MOUSEUP1_BORDER; 853 break; 854 case 1: 855 if (where == PANE) 856 key = KEYC_MOUSEUP2_PANE; 857 if (where == STATUS) 858 key = KEYC_MOUSEUP2_STATUS; 859 if (where == STATUS_LEFT) 860 key = KEYC_MOUSEUP2_STATUS_LEFT; 861 if (where == STATUS_RIGHT) 862 key = KEYC_MOUSEUP2_STATUS_RIGHT; 863 if (where == STATUS_DEFAULT) 864 key = KEYC_MOUSEUP2_STATUS_DEFAULT; 865 if (where == BORDER) 866 key = KEYC_MOUSEUP2_BORDER; 867 break; 868 case 2: 869 if (where == PANE) 870 key = KEYC_MOUSEUP3_PANE; 871 if (where == STATUS) 872 key = KEYC_MOUSEUP3_STATUS; 873 if (where == STATUS_LEFT) 874 key = KEYC_MOUSEUP3_STATUS_LEFT; 875 if (where == STATUS_RIGHT) 876 key = KEYC_MOUSEUP3_STATUS_RIGHT; 877 if (where == STATUS_DEFAULT) 878 key = KEYC_MOUSEUP3_STATUS_DEFAULT; 879 if (where == BORDER) 880 key = KEYC_MOUSEUP3_BORDER; 881 break; 882 } 883 break; 884 case DOWN: 885 switch (MOUSE_BUTTONS(b)) { 886 case 0: 887 if (where == PANE) 888 key = KEYC_MOUSEDOWN1_PANE; 889 if (where == STATUS) 890 key = KEYC_MOUSEDOWN1_STATUS; 891 if (where == STATUS_LEFT) 892 key = KEYC_MOUSEDOWN1_STATUS_LEFT; 893 if (where == STATUS_RIGHT) 894 key = KEYC_MOUSEDOWN1_STATUS_RIGHT; 895 if (where == STATUS_DEFAULT) 896 key = KEYC_MOUSEDOWN1_STATUS_DEFAULT; 897 if (where == BORDER) 898 key = KEYC_MOUSEDOWN1_BORDER; 899 break; 900 case 1: 901 if (where == PANE) 902 key = KEYC_MOUSEDOWN2_PANE; 903 if (where == STATUS) 904 key = KEYC_MOUSEDOWN2_STATUS; 905 if (where == STATUS_LEFT) 906 key = KEYC_MOUSEDOWN2_STATUS_LEFT; 907 if (where == STATUS_RIGHT) 908 key = KEYC_MOUSEDOWN2_STATUS_RIGHT; 909 if (where == STATUS_DEFAULT) 910 key = KEYC_MOUSEDOWN2_STATUS_DEFAULT; 911 if (where == BORDER) 912 key = KEYC_MOUSEDOWN2_BORDER; 913 break; 914 case 2: 915 if (where == PANE) 916 key = KEYC_MOUSEDOWN3_PANE; 917 if (where == STATUS) 918 key = KEYC_MOUSEDOWN3_STATUS; 919 if (where == STATUS_LEFT) 920 key = KEYC_MOUSEDOWN3_STATUS_LEFT; 921 if (where == STATUS_RIGHT) 922 key = KEYC_MOUSEDOWN3_STATUS_RIGHT; 923 if (where == STATUS_DEFAULT) 924 key = KEYC_MOUSEDOWN3_STATUS_DEFAULT; 925 if (where == BORDER) 926 key = KEYC_MOUSEDOWN3_BORDER; 927 break; 928 } 929 break; 930 case SECOND: 931 switch (MOUSE_BUTTONS(b)) { 932 case 0: 933 if (where == PANE) 934 key = KEYC_SECONDCLICK1_PANE; 935 if (where == STATUS) 936 key = KEYC_SECONDCLICK1_STATUS; 937 if (where == STATUS_LEFT) 938 key = KEYC_SECONDCLICK1_STATUS_LEFT; 939 if (where == STATUS_RIGHT) 940 key = KEYC_SECONDCLICK1_STATUS_RIGHT; 941 if (where == STATUS_DEFAULT) 942 key = KEYC_SECONDCLICK1_STATUS_DEFAULT; 943 if (where == BORDER) 944 key = KEYC_SECONDCLICK1_BORDER; 945 break; 946 case 1: 947 if (where == PANE) 948 key = KEYC_SECONDCLICK2_PANE; 949 if (where == STATUS) 950 key = KEYC_SECONDCLICK2_STATUS; 951 if (where == STATUS_LEFT) 952 key = KEYC_SECONDCLICK2_STATUS_LEFT; 953 if (where == STATUS_RIGHT) 954 key = KEYC_SECONDCLICK2_STATUS_RIGHT; 955 if (where == STATUS_DEFAULT) 956 key = KEYC_SECONDCLICK2_STATUS_DEFAULT; 957 if (where == BORDER) 958 key = KEYC_SECONDCLICK2_BORDER; 959 break; 960 case 2: 961 if (where == PANE) 962 key = KEYC_SECONDCLICK3_PANE; 963 if (where == STATUS) 964 key = KEYC_SECONDCLICK3_STATUS; 965 if (where == STATUS_LEFT) 966 key = KEYC_SECONDCLICK3_STATUS_LEFT; 967 if (where == STATUS_RIGHT) 968 key = KEYC_SECONDCLICK3_STATUS_RIGHT; 969 if (where == STATUS_DEFAULT) 970 key = KEYC_SECONDCLICK3_STATUS_DEFAULT; 971 if (where == BORDER) 972 key = KEYC_SECONDCLICK3_BORDER; 973 break; 974 } 975 break; 976 case DOUBLE: 977 switch (MOUSE_BUTTONS(b)) { 978 case 0: 979 if (where == PANE) 980 key = KEYC_DOUBLECLICK1_PANE; 981 if (where == STATUS) 982 key = KEYC_DOUBLECLICK1_STATUS; 983 if (where == STATUS_LEFT) 984 key = KEYC_DOUBLECLICK1_STATUS_LEFT; 985 if (where == STATUS_RIGHT) 986 key = KEYC_DOUBLECLICK1_STATUS_RIGHT; 987 if (where == STATUS_DEFAULT) 988 key = KEYC_DOUBLECLICK1_STATUS_DEFAULT; 989 if (where == BORDER) 990 key = KEYC_DOUBLECLICK1_BORDER; 991 break; 992 case 1: 993 if (where == PANE) 994 key = KEYC_DOUBLECLICK2_PANE; 995 if (where == STATUS) 996 key = KEYC_DOUBLECLICK2_STATUS; 997 if (where == STATUS_LEFT) 998 key = KEYC_DOUBLECLICK2_STATUS_LEFT; 999 if (where == STATUS_RIGHT) 1000 key = KEYC_DOUBLECLICK2_STATUS_RIGHT; 1001 if (where == STATUS_DEFAULT) 1002 key = KEYC_DOUBLECLICK2_STATUS_DEFAULT; 1003 if (where == BORDER) 1004 key = KEYC_DOUBLECLICK2_BORDER; 1005 break; 1006 case 2: 1007 if (where == PANE) 1008 key = KEYC_DOUBLECLICK3_PANE; 1009 if (where == STATUS) 1010 key = KEYC_DOUBLECLICK3_STATUS; 1011 if (where == STATUS_LEFT) 1012 key = KEYC_DOUBLECLICK3_STATUS_LEFT; 1013 if (where == STATUS_RIGHT) 1014 key = KEYC_DOUBLECLICK3_STATUS_RIGHT; 1015 if (where == STATUS_DEFAULT) 1016 key = KEYC_DOUBLECLICK3_STATUS_DEFAULT; 1017 if (where == BORDER) 1018 key = KEYC_DOUBLECLICK3_BORDER; 1019 break; 1020 } 1021 break; 1022 case TRIPLE: 1023 switch (MOUSE_BUTTONS(b)) { 1024 case 0: 1025 if (where == PANE) 1026 key = KEYC_TRIPLECLICK1_PANE; 1027 if (where == STATUS) 1028 key = KEYC_TRIPLECLICK1_STATUS; 1029 if (where == STATUS_LEFT) 1030 key = KEYC_TRIPLECLICK1_STATUS_LEFT; 1031 if (where == STATUS_RIGHT) 1032 key = KEYC_TRIPLECLICK1_STATUS_RIGHT; 1033 if (where == STATUS_DEFAULT) 1034 key = KEYC_TRIPLECLICK1_STATUS_DEFAULT; 1035 if (where == BORDER) 1036 key = KEYC_TRIPLECLICK1_BORDER; 1037 break; 1038 case 1: 1039 if (where == PANE) 1040 key = KEYC_TRIPLECLICK2_PANE; 1041 if (where == STATUS) 1042 key = KEYC_TRIPLECLICK2_STATUS; 1043 if (where == STATUS_LEFT) 1044 key = KEYC_TRIPLECLICK2_STATUS_LEFT; 1045 if (where == STATUS_RIGHT) 1046 key = KEYC_TRIPLECLICK2_STATUS_RIGHT; 1047 if (where == STATUS_DEFAULT) 1048 key = KEYC_TRIPLECLICK2_STATUS_DEFAULT; 1049 if (where == BORDER) 1050 key = KEYC_TRIPLECLICK2_BORDER; 1051 break; 1052 case 2: 1053 if (where == PANE) 1054 key = KEYC_TRIPLECLICK3_PANE; 1055 if (where == STATUS) 1056 key = KEYC_TRIPLECLICK3_STATUS; 1057 if (where == STATUS_LEFT) 1058 key = KEYC_TRIPLECLICK3_STATUS_LEFT; 1059 if (where == STATUS_RIGHT) 1060 key = KEYC_TRIPLECLICK3_STATUS_RIGHT; 1061 if (where == STATUS_DEFAULT) 1062 key = KEYC_TRIPLECLICK3_STATUS_DEFAULT; 1063 if (where == BORDER) 1064 key = KEYC_TRIPLECLICK3_BORDER; 1065 break; 1066 } 1067 break; 1068 } 1069 if (key == KEYC_UNKNOWN) 1070 return (KEYC_UNKNOWN); 1071 1072 out: 1073 /* Apply modifiers if any. */ 1074 if (b & MOUSE_MASK_META) 1075 key |= KEYC_META; 1076 if (b & MOUSE_MASK_CTRL) 1077 key |= KEYC_CTRL; 1078 if (b & MOUSE_MASK_SHIFT) 1079 key |= KEYC_SHIFT; 1080 1081 if (log_get_level() != 0) 1082 log_debug("mouse key is %s", key_string_lookup_key (key, 1)); 1083 return (key); 1084 } 1085 1086 /* Is this fast enough to probably be a paste? */ 1087 static int 1088 server_client_assume_paste(struct session *s) 1089 { 1090 struct timeval tv; 1091 int t; 1092 1093 if ((t = options_get_number(s->options, "assume-paste-time")) == 0) 1094 return (0); 1095 1096 timersub(&s->activity_time, &s->last_activity_time, &tv); 1097 if (tv.tv_sec == 0 && tv.tv_usec < t * 1000) { 1098 log_debug("session %s pasting (flag %d)", s->name, 1099 !!(s->flags & SESSION_PASTING)); 1100 if (s->flags & SESSION_PASTING) 1101 return (1); 1102 s->flags |= SESSION_PASTING; 1103 return (0); 1104 } 1105 log_debug("session %s not pasting", s->name); 1106 s->flags &= ~SESSION_PASTING; 1107 return (0); 1108 } 1109 1110 /* Has the latest client changed? */ 1111 static void 1112 server_client_update_latest(struct client *c) 1113 { 1114 struct window *w; 1115 1116 if (c->session == NULL) 1117 return; 1118 w = c->session->curw->window; 1119 1120 if (w->latest == c) 1121 return; 1122 w->latest = c; 1123 1124 if (options_get_number(w->options, "window-size") == WINDOW_SIZE_LATEST) 1125 recalculate_size(w, 0); 1126 } 1127 1128 /* 1129 * Handle data key input from client. This owns and can modify the key event it 1130 * is given and is responsible for freeing it. 1131 */ 1132 static enum cmd_retval 1133 server_client_key_callback(struct cmdq_item *item, void *data) 1134 { 1135 struct client *c = cmdq_get_client(item); 1136 struct key_event *event = data; 1137 key_code key = event->key; 1138 struct mouse_event *m = &event->m; 1139 struct session *s = c->session; 1140 struct winlink *wl; 1141 struct window_pane *wp; 1142 struct window_mode_entry *wme; 1143 struct timeval tv; 1144 struct key_table *table, *first; 1145 struct key_binding *bd; 1146 int xtimeout, flags; 1147 struct cmd_find_state fs; 1148 key_code key0; 1149 1150 /* Check the client is good to accept input. */ 1151 if (s == NULL || (c->flags & CLIENT_UNATTACHEDFLAGS)) 1152 goto out; 1153 wl = s->curw; 1154 1155 /* Update the activity timer. */ 1156 if (gettimeofday(&c->activity_time, NULL) != 0) 1157 fatal("gettimeofday failed"); 1158 session_update_activity(s, &c->activity_time); 1159 1160 /* Check for mouse keys. */ 1161 m->valid = 0; 1162 if (key == KEYC_MOUSE || key == KEYC_DOUBLECLICK) { 1163 if (c->flags & CLIENT_READONLY) 1164 goto out; 1165 key = server_client_check_mouse(c, event); 1166 if (key == KEYC_UNKNOWN) 1167 goto out; 1168 1169 m->valid = 1; 1170 m->key = key; 1171 1172 /* 1173 * Mouse drag is in progress, so fire the callback (now that 1174 * the mouse event is valid). 1175 */ 1176 if ((key & KEYC_MASK_KEY) == KEYC_DRAGGING) { 1177 c->tty.mouse_drag_update(c, m); 1178 goto out; 1179 } 1180 event->key = key; 1181 } 1182 1183 /* Find affected pane. */ 1184 if (!KEYC_IS_MOUSE(key) || cmd_find_from_mouse(&fs, m, 0) != 0) 1185 cmd_find_from_client(&fs, c, 0); 1186 wp = fs.wp; 1187 1188 /* Forward mouse keys if disabled. */ 1189 if (KEYC_IS_MOUSE(key) && !options_get_number(s->options, "mouse")) 1190 goto forward_key; 1191 1192 /* Treat everything as a regular key when pasting is detected. */ 1193 if (!KEYC_IS_MOUSE(key) && server_client_assume_paste(s)) 1194 goto forward_key; 1195 1196 /* 1197 * Work out the current key table. If the pane is in a mode, use 1198 * the mode table instead of the default key table. 1199 */ 1200 if (server_client_is_default_key_table(c, c->keytable) && 1201 wp != NULL && 1202 (wme = TAILQ_FIRST(&wp->modes)) != NULL && 1203 wme->mode->key_table != NULL) 1204 table = key_bindings_get_table(wme->mode->key_table(wme), 1); 1205 else 1206 table = c->keytable; 1207 first = table; 1208 1209 table_changed: 1210 /* 1211 * The prefix always takes precedence and forces a switch to the prefix 1212 * table, unless we are already there. 1213 */ 1214 key0 = (key & (KEYC_MASK_KEY|KEYC_MASK_MODIFIERS)); 1215 if ((key0 == (key_code)options_get_number(s->options, "prefix") || 1216 key0 == (key_code)options_get_number(s->options, "prefix2")) && 1217 strcmp(table->name, "prefix") != 0) { 1218 server_client_set_key_table(c, "prefix"); 1219 server_status_client(c); 1220 goto out; 1221 } 1222 flags = c->flags; 1223 1224 try_again: 1225 /* Log key table. */ 1226 if (wp == NULL) 1227 log_debug("key table %s (no pane)", table->name); 1228 else 1229 log_debug("key table %s (pane %%%u)", table->name, wp->id); 1230 if (c->flags & CLIENT_REPEAT) 1231 log_debug("currently repeating"); 1232 1233 /* Try to see if there is a key binding in the current table. */ 1234 bd = key_bindings_get(table, key0); 1235 if (bd != NULL) { 1236 /* 1237 * Key was matched in this table. If currently repeating but a 1238 * non-repeating binding was found, stop repeating and try 1239 * again in the root table. 1240 */ 1241 if ((c->flags & CLIENT_REPEAT) && 1242 (~bd->flags & KEY_BINDING_REPEAT)) { 1243 log_debug("found in key table %s (not repeating)", 1244 table->name); 1245 server_client_set_key_table(c, NULL); 1246 first = table = c->keytable; 1247 c->flags &= ~CLIENT_REPEAT; 1248 server_status_client(c); 1249 goto table_changed; 1250 } 1251 log_debug("found in key table %s", table->name); 1252 1253 /* 1254 * Take a reference to this table to make sure the key binding 1255 * doesn't disappear. 1256 */ 1257 table->references++; 1258 1259 /* 1260 * If this is a repeating key, start the timer. Otherwise reset 1261 * the client back to the root table. 1262 */ 1263 xtimeout = options_get_number(s->options, "repeat-time"); 1264 if (xtimeout != 0 && (bd->flags & KEY_BINDING_REPEAT)) { 1265 c->flags |= CLIENT_REPEAT; 1266 1267 tv.tv_sec = xtimeout / 1000; 1268 tv.tv_usec = (xtimeout % 1000) * 1000L; 1269 evtimer_del(&c->repeat_timer); 1270 evtimer_add(&c->repeat_timer, &tv); 1271 } else { 1272 c->flags &= ~CLIENT_REPEAT; 1273 server_client_set_key_table(c, NULL); 1274 } 1275 server_status_client(c); 1276 1277 /* Execute the key binding. */ 1278 key_bindings_dispatch(bd, item, c, event, &fs); 1279 key_bindings_unref_table(table); 1280 goto out; 1281 } 1282 1283 /* 1284 * No match, try the ANY key. 1285 */ 1286 if (key0 != KEYC_ANY) { 1287 key0 = KEYC_ANY; 1288 goto try_again; 1289 } 1290 1291 /* 1292 * No match in this table. If not in the root table or if repeating, 1293 * switch the client back to the root table and try again. 1294 */ 1295 log_debug("not found in key table %s", table->name); 1296 if (!server_client_is_default_key_table(c, table) || 1297 (c->flags & CLIENT_REPEAT)) { 1298 log_debug("trying in root table"); 1299 server_client_set_key_table(c, NULL); 1300 table = c->keytable; 1301 if (c->flags & CLIENT_REPEAT) 1302 first = table; 1303 c->flags &= ~CLIENT_REPEAT; 1304 server_status_client(c); 1305 goto table_changed; 1306 } 1307 1308 /* 1309 * No match in the root table either. If this wasn't the first table 1310 * tried, don't pass the key to the pane. 1311 */ 1312 if (first != table && (~flags & CLIENT_REPEAT)) { 1313 server_client_set_key_table(c, NULL); 1314 server_status_client(c); 1315 goto out; 1316 } 1317 1318 forward_key: 1319 if (c->flags & CLIENT_READONLY) 1320 goto out; 1321 if (wp != NULL) 1322 window_pane_key(wp, c, s, wl, key, m); 1323 1324 out: 1325 if (s != NULL && key != KEYC_FOCUS_OUT) 1326 server_client_update_latest(c); 1327 free(event); 1328 return (CMD_RETURN_NORMAL); 1329 } 1330 1331 /* Handle a key event. */ 1332 int 1333 server_client_handle_key(struct client *c, struct key_event *event) 1334 { 1335 struct session *s = c->session; 1336 struct cmdq_item *item; 1337 1338 /* Check the client is good to accept input. */ 1339 if (s == NULL || (c->flags & CLIENT_UNATTACHEDFLAGS)) 1340 return (0); 1341 1342 /* 1343 * Key presses in overlay mode and the command prompt are a special 1344 * case. The queue might be blocked so they need to be processed 1345 * immediately rather than queued. 1346 */ 1347 if (~c->flags & CLIENT_READONLY) { 1348 if (c->message_string != NULL) { 1349 if (c->message_ignore_keys) 1350 return (0); 1351 status_message_clear(c); 1352 } 1353 if (c->overlay_key != NULL) { 1354 switch (c->overlay_key(c, event)) { 1355 case 0: 1356 return (0); 1357 case 1: 1358 server_client_clear_overlay(c); 1359 return (0); 1360 } 1361 } 1362 server_client_clear_overlay(c); 1363 if (c->prompt_string != NULL) { 1364 if (status_prompt_key(c, event->key) == 0) 1365 return (0); 1366 } 1367 } 1368 1369 /* 1370 * Add the key to the queue so it happens after any commands queued by 1371 * previous keys. 1372 */ 1373 item = cmdq_get_callback(server_client_key_callback, event); 1374 cmdq_append(c, item); 1375 return (1); 1376 } 1377 1378 /* Client functions that need to happen every loop. */ 1379 void 1380 server_client_loop(void) 1381 { 1382 struct client *c; 1383 struct window *w; 1384 struct window_pane *wp; 1385 int focus; 1386 1387 /* Check for window resize. This is done before redrawing. */ 1388 RB_FOREACH(w, windows, &windows) 1389 server_client_check_window_resize(w); 1390 1391 /* Check clients. */ 1392 TAILQ_FOREACH(c, &clients, entry) { 1393 server_client_check_exit(c); 1394 if (c->session != NULL) { 1395 server_client_check_modes(c); 1396 server_client_check_redraw(c); 1397 server_client_reset_state(c); 1398 } 1399 } 1400 1401 /* 1402 * Any windows will have been redrawn as part of clients, so clear 1403 * their flags now. Also check pane focus and resize. 1404 */ 1405 focus = options_get_number(global_options, "focus-events"); 1406 RB_FOREACH(w, windows, &windows) { 1407 TAILQ_FOREACH(wp, &w->panes, entry) { 1408 if (wp->fd != -1) { 1409 if (focus) 1410 server_client_check_pane_focus(wp); 1411 server_client_check_pane_resize(wp); 1412 server_client_check_pane_buffer(wp); 1413 } 1414 wp->flags &= ~PANE_REDRAW; 1415 } 1416 check_window_name(w); 1417 } 1418 } 1419 1420 /* Check if window needs to be resized. */ 1421 static void 1422 server_client_check_window_resize(struct window *w) 1423 { 1424 struct winlink *wl; 1425 1426 if (~w->flags & WINDOW_RESIZE) 1427 return; 1428 1429 TAILQ_FOREACH(wl, &w->winlinks, wentry) { 1430 if (wl->session->attached != 0 && wl->session->curw == wl) 1431 break; 1432 } 1433 if (wl == NULL) 1434 return; 1435 1436 log_debug("%s: resizing window @%u", __func__, w->id); 1437 resize_window(w, w->new_sx, w->new_sy, w->new_xpixel, w->new_ypixel); 1438 } 1439 1440 /* Resize timer event. */ 1441 static void 1442 server_client_resize_timer(__unused int fd, __unused short events, void *data) 1443 { 1444 struct window_pane *wp = data; 1445 1446 log_debug("%s: %%%u resize timer expired", __func__, wp->id); 1447 evtimer_del(&wp->resize_timer); 1448 } 1449 1450 /* Check if pane should be resized. */ 1451 static void 1452 server_client_check_pane_resize(struct window_pane *wp) 1453 { 1454 struct window_pane_resize *r; 1455 struct window_pane_resize *r1; 1456 struct window_pane_resize *first; 1457 struct window_pane_resize *last; 1458 struct timeval tv = { .tv_usec = 250000 }; 1459 1460 if (TAILQ_EMPTY(&wp->resize_queue)) 1461 return; 1462 1463 if (!event_initialized(&wp->resize_timer)) 1464 evtimer_set(&wp->resize_timer, server_client_resize_timer, wp); 1465 if (evtimer_pending(&wp->resize_timer, NULL)) 1466 return; 1467 1468 log_debug("%s: %%%u needs to be resized", __func__, wp->id); 1469 TAILQ_FOREACH(r, &wp->resize_queue, entry) { 1470 log_debug("queued resize: %ux%u -> %ux%u", r->osx, r->osy, 1471 r->sx, r->sy); 1472 } 1473 1474 /* 1475 * There are three cases that matter: 1476 * 1477 * - Only one resize. It can just be applied. 1478 * 1479 * - Multiple resizes and the ending size is different from the 1480 * starting size. We can discard all resizes except the most recent. 1481 * 1482 * - Multiple resizes and the ending size is the same as the starting 1483 * size. We must resize at least twice to force the application to 1484 * redraw. So apply the first and leave the last on the queue for 1485 * next time. 1486 */ 1487 first = TAILQ_FIRST(&wp->resize_queue); 1488 last = TAILQ_LAST(&wp->resize_queue, window_pane_resizes); 1489 if (first == last) { 1490 /* Only one resize. */ 1491 window_pane_send_resize(wp, first->sx, first->sy); 1492 TAILQ_REMOVE(&wp->resize_queue, first, entry); 1493 free(first); 1494 } else if (last->sx != first->osx || last->sy != first->osy) { 1495 /* Multiple resizes ending up with a different size. */ 1496 window_pane_send_resize(wp, last->sx, last->sy); 1497 TAILQ_FOREACH_SAFE(r, &wp->resize_queue, entry, r1) { 1498 TAILQ_REMOVE(&wp->resize_queue, r, entry); 1499 free(r); 1500 } 1501 } else { 1502 /* 1503 * Multiple resizes ending up with the same size. There will 1504 * not be more than one to the same size in succession so we 1505 * can just use the last-but-one on the list and leave the last 1506 * for later. We reduce the time until the next check to avoid 1507 * a long delay between the resizes. 1508 */ 1509 r = TAILQ_PREV(last, window_pane_resizes, entry); 1510 window_pane_send_resize(wp, r->sx, r->sy); 1511 TAILQ_FOREACH_SAFE(r, &wp->resize_queue, entry, r1) { 1512 if (r == last) 1513 break; 1514 TAILQ_REMOVE(&wp->resize_queue, r, entry); 1515 free(r); 1516 } 1517 tv.tv_usec = 10000; 1518 } 1519 evtimer_add(&wp->resize_timer, &tv); 1520 } 1521 1522 1523 /* Check pane buffer size. */ 1524 static void 1525 server_client_check_pane_buffer(struct window_pane *wp) 1526 { 1527 struct evbuffer *evb = wp->event->input; 1528 size_t minimum; 1529 struct client *c; 1530 struct window_pane_offset *wpo; 1531 int off = 1, flag; 1532 u_int attached_clients = 0; 1533 size_t new_size; 1534 1535 /* 1536 * Work out the minimum used size. This is the most that can be removed 1537 * from the buffer. 1538 */ 1539 minimum = wp->offset.used; 1540 if (wp->pipe_fd != -1 && wp->pipe_offset.used < minimum) 1541 minimum = wp->pipe_offset.used; 1542 TAILQ_FOREACH(c, &clients, entry) { 1543 if (c->session == NULL) 1544 continue; 1545 attached_clients++; 1546 1547 if (~c->flags & CLIENT_CONTROL) { 1548 off = 0; 1549 continue; 1550 } 1551 wpo = control_pane_offset(c, wp, &flag); 1552 if (wpo == NULL) { 1553 off = 0; 1554 continue; 1555 } 1556 if (!flag) 1557 off = 0; 1558 1559 window_pane_get_new_data(wp, wpo, &new_size); 1560 log_debug("%s: %s has %zu bytes used and %zu left for %%%u", 1561 __func__, c->name, wpo->used - wp->base_offset, new_size, 1562 wp->id); 1563 if (wpo->used < minimum) 1564 minimum = wpo->used; 1565 } 1566 if (attached_clients == 0) 1567 off = 0; 1568 minimum -= wp->base_offset; 1569 if (minimum == 0) 1570 goto out; 1571 1572 /* Drain the buffer. */ 1573 log_debug("%s: %%%u has %zu minimum (of %zu) bytes used", __func__, 1574 wp->id, minimum, EVBUFFER_LENGTH(evb)); 1575 evbuffer_drain(evb, minimum); 1576 1577 /* 1578 * Adjust the base offset. If it would roll over, all the offsets into 1579 * the buffer need to be adjusted. 1580 */ 1581 if (wp->base_offset > SIZE_MAX - minimum) { 1582 log_debug("%s: %%%u base offset has wrapped", __func__, wp->id); 1583 wp->offset.used -= wp->base_offset; 1584 if (wp->pipe_fd != -1) 1585 wp->pipe_offset.used -= wp->base_offset; 1586 TAILQ_FOREACH(c, &clients, entry) { 1587 if (c->session == NULL || (~c->flags & CLIENT_CONTROL)) 1588 continue; 1589 wpo = control_pane_offset(c, wp, &flag); 1590 if (wpo != NULL && !flag) 1591 wpo->used -= wp->base_offset; 1592 } 1593 wp->base_offset = minimum; 1594 } else 1595 wp->base_offset += minimum; 1596 1597 out: 1598 /* 1599 * If there is data remaining, and there are no clients able to consume 1600 * it, do not read any more. This is true when there are attached 1601 * clients, all of which are control clients which are not able to 1602 * accept any more data. 1603 */ 1604 log_debug("%s: pane %%%u is %s", __func__, wp->id, off ? "off" : "on"); 1605 if (off) 1606 bufferevent_disable(wp->event, EV_READ); 1607 else 1608 bufferevent_enable(wp->event, EV_READ); 1609 } 1610 1611 /* Check whether pane should be focused. */ 1612 static void 1613 server_client_check_pane_focus(struct window_pane *wp) 1614 { 1615 struct client *c; 1616 int push; 1617 1618 /* Do we need to push the focus state? */ 1619 push = wp->flags & PANE_FOCUSPUSH; 1620 wp->flags &= ~PANE_FOCUSPUSH; 1621 1622 /* If we're not the active pane in our window, we're not focused. */ 1623 if (wp->window->active != wp) 1624 goto not_focused; 1625 1626 /* 1627 * If our window is the current window in any focused clients with an 1628 * attached session, we're focused. 1629 */ 1630 TAILQ_FOREACH(c, &clients, entry) { 1631 if (c->session == NULL || !(c->flags & CLIENT_FOCUSED)) 1632 continue; 1633 if (c->session->attached == 0) 1634 continue; 1635 1636 if (c->session->curw->window == wp->window) 1637 goto focused; 1638 } 1639 1640 not_focused: 1641 if (push || (wp->flags & PANE_FOCUSED)) { 1642 if (wp->base.mode & MODE_FOCUSON) 1643 bufferevent_write(wp->event, "\033[O", 3); 1644 notify_pane("pane-focus-out", wp); 1645 } 1646 wp->flags &= ~PANE_FOCUSED; 1647 return; 1648 1649 focused: 1650 if (push || !(wp->flags & PANE_FOCUSED)) { 1651 if (wp->base.mode & MODE_FOCUSON) 1652 bufferevent_write(wp->event, "\033[I", 3); 1653 notify_pane("pane-focus-in", wp); 1654 session_update_activity(c->session, NULL); 1655 } 1656 wp->flags |= PANE_FOCUSED; 1657 } 1658 1659 /* 1660 * Update cursor position and mode settings. The scroll region and attributes 1661 * are cleared when idle (waiting for an event) as this is the most likely time 1662 * a user may interrupt tmux, for example with ~^Z in ssh(1). This is a 1663 * compromise between excessive resets and likelihood of an interrupt. 1664 * 1665 * tty_region/tty_reset/tty_update_mode already take care of not resetting 1666 * things that are already in their default state. 1667 */ 1668 static void 1669 server_client_reset_state(struct client *c) 1670 { 1671 struct tty *tty = &c->tty; 1672 struct window *w = c->session->curw->window; 1673 struct window_pane *wp = server_client_get_pane(c), *loop; 1674 struct screen *s = NULL; 1675 struct options *oo = c->session->options; 1676 int mode = 0, cursor, flags; 1677 u_int cx = 0, cy = 0, ox, oy, sx, sy; 1678 1679 if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED)) 1680 return; 1681 1682 /* Disable the block flag. */ 1683 flags = (tty->flags & TTY_BLOCK); 1684 tty->flags &= ~TTY_BLOCK; 1685 1686 /* Get mode from overlay if any, else from screen. */ 1687 if (c->overlay_draw != NULL) { 1688 if (c->overlay_mode != NULL) 1689 s = c->overlay_mode(c, &cx, &cy); 1690 } else 1691 s = wp->screen; 1692 if (s != NULL) 1693 mode = s->mode; 1694 log_debug("%s: client %s mode %x", __func__, c->name, mode); 1695 1696 /* Reset region and margin. */ 1697 tty_region_off(tty); 1698 tty_margin_off(tty); 1699 1700 /* Move cursor to pane cursor and offset. */ 1701 if (c->overlay_draw == NULL) { 1702 cursor = 0; 1703 tty_window_offset(tty, &ox, &oy, &sx, &sy); 1704 if (wp->xoff + s->cx >= ox && wp->xoff + s->cx <= ox + sx && 1705 wp->yoff + s->cy >= oy && wp->yoff + s->cy <= oy + sy) { 1706 cursor = 1; 1707 1708 cx = wp->xoff + s->cx - ox; 1709 cy = wp->yoff + s->cy - oy; 1710 1711 if (status_at_line(c) == 0) 1712 cy += status_line_size(c); 1713 } 1714 if (!cursor) 1715 mode &= ~MODE_CURSOR; 1716 } 1717 log_debug("%s: cursor to %u,%u", __func__, cx, cy); 1718 tty_cursor(tty, cx, cy); 1719 1720 /* 1721 * Set mouse mode if requested. To support dragging, always use button 1722 * mode. 1723 */ 1724 if (options_get_number(oo, "mouse")) { 1725 if (c->overlay_draw == NULL) { 1726 mode &= ~ALL_MOUSE_MODES; 1727 TAILQ_FOREACH(loop, &w->panes, entry) { 1728 if (loop->screen->mode & MODE_MOUSE_ALL) 1729 mode |= MODE_MOUSE_ALL; 1730 } 1731 } 1732 if (~mode & MODE_MOUSE_ALL) 1733 mode |= MODE_MOUSE_BUTTON; 1734 } 1735 1736 /* Clear bracketed paste mode if at the prompt. */ 1737 if (c->overlay_draw == NULL && c->prompt_string != NULL) 1738 mode &= ~MODE_BRACKETPASTE; 1739 1740 /* Set the terminal mode and reset attributes. */ 1741 tty_update_mode(tty, mode, s); 1742 tty_reset(tty); 1743 1744 /* All writing must be done, send a sync end (if it was started). */ 1745 tty_sync_end(tty); 1746 tty->flags |= flags; 1747 } 1748 1749 /* Repeat time callback. */ 1750 static void 1751 server_client_repeat_timer(__unused int fd, __unused short events, void *data) 1752 { 1753 struct client *c = data; 1754 1755 if (c->flags & CLIENT_REPEAT) { 1756 server_client_set_key_table(c, NULL); 1757 c->flags &= ~CLIENT_REPEAT; 1758 server_status_client(c); 1759 } 1760 } 1761 1762 /* Double-click callback. */ 1763 static void 1764 server_client_click_timer(__unused int fd, __unused short events, void *data) 1765 { 1766 struct client *c = data; 1767 struct key_event *event; 1768 1769 log_debug("click timer expired"); 1770 1771 if (c->flags & CLIENT_TRIPLECLICK) { 1772 /* 1773 * Waiting for a third click that hasn't happened, so this must 1774 * have been a double click. 1775 */ 1776 event = xmalloc(sizeof *event); 1777 event->key = KEYC_DOUBLECLICK; 1778 memcpy(&event->m, &c->click_event, sizeof event->m); 1779 if (!server_client_handle_key(c, event)) 1780 free(event); 1781 } 1782 c->flags &= ~(CLIENT_DOUBLECLICK|CLIENT_TRIPLECLICK); 1783 } 1784 1785 /* Check if client should be exited. */ 1786 static void 1787 server_client_check_exit(struct client *c) 1788 { 1789 struct client_file *cf; 1790 const char *name = c->exit_session; 1791 char *data; 1792 size_t size, msize; 1793 1794 if (c->flags & (CLIENT_DEAD|CLIENT_EXITED)) 1795 return; 1796 if (~c->flags & CLIENT_EXIT) 1797 return; 1798 1799 if (c->flags & CLIENT_CONTROL) { 1800 control_discard(c); 1801 if (!control_all_done(c)) 1802 return; 1803 } 1804 RB_FOREACH(cf, client_files, &c->files) { 1805 if (EVBUFFER_LENGTH(cf->buffer) != 0) 1806 return; 1807 } 1808 c->flags |= CLIENT_EXITED; 1809 1810 switch (c->exit_type) { 1811 case CLIENT_EXIT_RETURN: 1812 if (c->exit_message != NULL) 1813 msize = strlen(c->exit_message) + 1; 1814 else 1815 msize = 0; 1816 size = (sizeof c->retval) + msize; 1817 data = xmalloc(size); 1818 memcpy(data, &c->retval, sizeof c->retval); 1819 if (c->exit_message != NULL) 1820 memcpy(data + sizeof c->retval, c->exit_message, msize); 1821 proc_send(c->peer, MSG_EXIT, -1, data, size); 1822 free(data); 1823 break; 1824 case CLIENT_EXIT_SHUTDOWN: 1825 proc_send(c->peer, MSG_SHUTDOWN, -1, NULL, 0); 1826 break; 1827 case CLIENT_EXIT_DETACH: 1828 proc_send(c->peer, c->exit_msgtype, -1, name, strlen(name) + 1); 1829 break; 1830 } 1831 free(c->exit_session); 1832 free(c->exit_message); 1833 } 1834 1835 /* Redraw timer callback. */ 1836 static void 1837 server_client_redraw_timer(__unused int fd, __unused short events, 1838 __unused void *data) 1839 { 1840 log_debug("redraw timer fired"); 1841 } 1842 1843 /* 1844 * Check if modes need to be updated. Only modes in the current window are 1845 * updated and it is done when the status line is redrawn. 1846 */ 1847 static void 1848 server_client_check_modes(struct client *c) 1849 { 1850 struct window *w = c->session->curw->window; 1851 struct window_pane *wp; 1852 struct window_mode_entry *wme; 1853 1854 if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED)) 1855 return; 1856 if (~c->flags & CLIENT_REDRAWSTATUS) 1857 return; 1858 TAILQ_FOREACH(wp, &w->panes, entry) { 1859 wme = TAILQ_FIRST(&wp->modes); 1860 if (wme != NULL && wme->mode->update != NULL) 1861 wme->mode->update(wme); 1862 } 1863 } 1864 1865 /* Check for client redraws. */ 1866 static void 1867 server_client_check_redraw(struct client *c) 1868 { 1869 struct session *s = c->session; 1870 struct tty *tty = &c->tty; 1871 struct window *w = c->session->curw->window; 1872 struct window_pane *wp; 1873 int needed, flags, mode = tty->mode, new_flags = 0; 1874 int redraw; 1875 u_int bit = 0; 1876 struct timeval tv = { .tv_usec = 1000 }; 1877 static struct event ev; 1878 size_t left; 1879 1880 if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED)) 1881 return; 1882 if (c->flags & CLIENT_ALLREDRAWFLAGS) { 1883 log_debug("%s: redraw%s%s%s%s%s", c->name, 1884 (c->flags & CLIENT_REDRAWWINDOW) ? " window" : "", 1885 (c->flags & CLIENT_REDRAWSTATUS) ? " status" : "", 1886 (c->flags & CLIENT_REDRAWBORDERS) ? " borders" : "", 1887 (c->flags & CLIENT_REDRAWOVERLAY) ? " overlay" : "", 1888 (c->flags & CLIENT_REDRAWPANES) ? " panes" : ""); 1889 } 1890 1891 /* 1892 * If there is outstanding data, defer the redraw until it has been 1893 * consumed. We can just add a timer to get out of the event loop and 1894 * end up back here. 1895 */ 1896 needed = 0; 1897 if (c->flags & CLIENT_ALLREDRAWFLAGS) 1898 needed = 1; 1899 else { 1900 TAILQ_FOREACH(wp, &w->panes, entry) { 1901 if (wp->flags & PANE_REDRAW) { 1902 needed = 1; 1903 break; 1904 } 1905 } 1906 if (needed) 1907 new_flags |= CLIENT_REDRAWPANES; 1908 } 1909 if (needed && (left = EVBUFFER_LENGTH(tty->out)) != 0) { 1910 log_debug("%s: redraw deferred (%zu left)", c->name, left); 1911 if (!evtimer_initialized(&ev)) 1912 evtimer_set(&ev, server_client_redraw_timer, NULL); 1913 if (!evtimer_pending(&ev, NULL)) { 1914 log_debug("redraw timer started"); 1915 evtimer_add(&ev, &tv); 1916 } 1917 1918 if (~c->flags & CLIENT_REDRAWWINDOW) { 1919 TAILQ_FOREACH(wp, &w->panes, entry) { 1920 if (wp->flags & PANE_REDRAW) { 1921 log_debug("%s: pane %%%u needs redraw", 1922 c->name, wp->id); 1923 c->redraw_panes |= (1 << bit); 1924 } 1925 if (++bit == 64) { 1926 /* 1927 * If more that 64 panes, give up and 1928 * just redraw the window. 1929 */ 1930 new_flags &= CLIENT_REDRAWPANES; 1931 new_flags |= CLIENT_REDRAWWINDOW; 1932 break; 1933 } 1934 } 1935 if (c->redraw_panes != 0) 1936 c->flags |= CLIENT_REDRAWPANES; 1937 } 1938 c->flags |= new_flags; 1939 return; 1940 } else if (needed) 1941 log_debug("%s: redraw needed", c->name); 1942 1943 flags = tty->flags & (TTY_BLOCK|TTY_FREEZE|TTY_NOCURSOR); 1944 tty->flags = (tty->flags & ~(TTY_BLOCK|TTY_FREEZE))|TTY_NOCURSOR; 1945 1946 if (~c->flags & CLIENT_REDRAWWINDOW) { 1947 /* 1948 * If not redrawing the entire window, check whether each pane 1949 * needs to be redrawn. 1950 */ 1951 TAILQ_FOREACH(wp, &w->panes, entry) { 1952 redraw = 0; 1953 if (wp->flags & PANE_REDRAW) 1954 redraw = 1; 1955 else if (c->flags & CLIENT_REDRAWPANES) 1956 redraw = !!(c->redraw_panes & (1 << bit)); 1957 bit++; 1958 if (!redraw) 1959 continue; 1960 log_debug("%s: redrawing pane %%%u", __func__, wp->id); 1961 screen_redraw_pane(c, wp); 1962 } 1963 c->redraw_panes = 0; 1964 c->flags &= ~CLIENT_REDRAWPANES; 1965 } 1966 1967 if (c->flags & CLIENT_ALLREDRAWFLAGS) { 1968 if (options_get_number(s->options, "set-titles")) 1969 server_client_set_title(c); 1970 screen_redraw_screen(c); 1971 } 1972 1973 tty->flags = (tty->flags & ~TTY_NOCURSOR)|(flags & TTY_NOCURSOR); 1974 tty_update_mode(tty, mode, NULL); 1975 tty->flags = (tty->flags & ~(TTY_BLOCK|TTY_FREEZE|TTY_NOCURSOR))|flags; 1976 1977 c->flags &= ~(CLIENT_ALLREDRAWFLAGS|CLIENT_STATUSFORCE); 1978 1979 if (needed) { 1980 /* 1981 * We would have deferred the redraw unless the output buffer 1982 * was empty, so we can record how many bytes the redraw 1983 * generated. 1984 */ 1985 c->redraw = EVBUFFER_LENGTH(tty->out); 1986 log_debug("%s: redraw added %zu bytes", c->name, c->redraw); 1987 } 1988 } 1989 1990 /* Set client title. */ 1991 static void 1992 server_client_set_title(struct client *c) 1993 { 1994 struct session *s = c->session; 1995 const char *template; 1996 char *title; 1997 struct format_tree *ft; 1998 1999 template = options_get_string(s->options, "set-titles-string"); 2000 2001 ft = format_create(c, NULL, FORMAT_NONE, 0); 2002 format_defaults(ft, c, NULL, NULL, NULL); 2003 2004 title = format_expand_time(ft, template); 2005 if (c->title == NULL || strcmp(title, c->title) != 0) { 2006 free(c->title); 2007 c->title = xstrdup(title); 2008 tty_set_title(&c->tty, c->title); 2009 } 2010 free(title); 2011 2012 format_free(ft); 2013 } 2014 2015 /* Dispatch message from client. */ 2016 static void 2017 server_client_dispatch(struct imsg *imsg, void *arg) 2018 { 2019 struct client *c = arg; 2020 ssize_t datalen; 2021 struct session *s; 2022 2023 if (c->flags & CLIENT_DEAD) 2024 return; 2025 2026 if (imsg == NULL) { 2027 server_client_lost(c); 2028 return; 2029 } 2030 2031 datalen = imsg->hdr.len - IMSG_HEADER_SIZE; 2032 2033 switch (imsg->hdr.type) { 2034 case MSG_IDENTIFY_CLIENTPID: 2035 case MSG_IDENTIFY_CWD: 2036 case MSG_IDENTIFY_ENVIRON: 2037 case MSG_IDENTIFY_FEATURES: 2038 case MSG_IDENTIFY_FLAGS: 2039 case MSG_IDENTIFY_LONGFLAGS: 2040 case MSG_IDENTIFY_STDIN: 2041 case MSG_IDENTIFY_STDOUT: 2042 case MSG_IDENTIFY_TERM: 2043 case MSG_IDENTIFY_TERMINFO: 2044 case MSG_IDENTIFY_TTYNAME: 2045 case MSG_IDENTIFY_DONE: 2046 server_client_dispatch_identify(c, imsg); 2047 break; 2048 case MSG_COMMAND: 2049 server_client_dispatch_command(c, imsg); 2050 break; 2051 case MSG_RESIZE: 2052 if (datalen != 0) 2053 fatalx("bad MSG_RESIZE size"); 2054 2055 if (c->flags & CLIENT_CONTROL) 2056 break; 2057 server_client_update_latest(c); 2058 server_client_clear_overlay(c); 2059 tty_resize(&c->tty); 2060 recalculate_sizes(); 2061 server_redraw_client(c); 2062 if (c->session != NULL) 2063 notify_client("client-resized", c); 2064 break; 2065 case MSG_EXITING: 2066 if (datalen != 0) 2067 fatalx("bad MSG_EXITING size"); 2068 c->session = NULL; 2069 tty_close(&c->tty); 2070 proc_send(c->peer, MSG_EXITED, -1, NULL, 0); 2071 break; 2072 case MSG_WAKEUP: 2073 case MSG_UNLOCK: 2074 if (datalen != 0) 2075 fatalx("bad MSG_WAKEUP size"); 2076 2077 if (!(c->flags & CLIENT_SUSPENDED)) 2078 break; 2079 c->flags &= ~CLIENT_SUSPENDED; 2080 2081 if (c->fd == -1 || c->session == NULL) /* exited already */ 2082 break; 2083 s = c->session; 2084 2085 if (gettimeofday(&c->activity_time, NULL) != 0) 2086 fatal("gettimeofday failed"); 2087 2088 tty_start_tty(&c->tty); 2089 server_redraw_client(c); 2090 recalculate_sizes(); 2091 2092 if (s != NULL) 2093 session_update_activity(s, &c->activity_time); 2094 break; 2095 case MSG_SHELL: 2096 if (datalen != 0) 2097 fatalx("bad MSG_SHELL size"); 2098 2099 server_client_dispatch_shell(c); 2100 break; 2101 case MSG_WRITE_READY: 2102 file_write_ready(&c->files, imsg); 2103 break; 2104 case MSG_READ: 2105 file_read_data(&c->files, imsg); 2106 break; 2107 case MSG_READ_DONE: 2108 file_read_done(&c->files, imsg); 2109 break; 2110 } 2111 } 2112 2113 /* Callback when command is done. */ 2114 static enum cmd_retval 2115 server_client_command_done(struct cmdq_item *item, __unused void *data) 2116 { 2117 struct client *c = cmdq_get_client(item); 2118 2119 if (~c->flags & CLIENT_ATTACHED) 2120 c->flags |= CLIENT_EXIT; 2121 else if (~c->flags & CLIENT_EXIT) 2122 tty_send_requests(&c->tty); 2123 return (CMD_RETURN_NORMAL); 2124 } 2125 2126 /* Handle command message. */ 2127 static void 2128 server_client_dispatch_command(struct client *c, struct imsg *imsg) 2129 { 2130 struct msg_command data; 2131 char *buf; 2132 size_t len; 2133 int argc; 2134 char **argv, *cause; 2135 struct cmd_parse_result *pr; 2136 2137 if (c->flags & CLIENT_EXIT) 2138 return; 2139 2140 if (imsg->hdr.len - IMSG_HEADER_SIZE < sizeof data) 2141 fatalx("bad MSG_COMMAND size"); 2142 memcpy(&data, imsg->data, sizeof data); 2143 2144 buf = (char *)imsg->data + sizeof data; 2145 len = imsg->hdr.len - IMSG_HEADER_SIZE - sizeof data; 2146 if (len > 0 && buf[len - 1] != '\0') 2147 fatalx("bad MSG_COMMAND string"); 2148 2149 argc = data.argc; 2150 if (cmd_unpack_argv(buf, len, argc, &argv) != 0) { 2151 cause = xstrdup("command too long"); 2152 goto error; 2153 } 2154 2155 if (argc == 0) { 2156 argc = 1; 2157 argv = xcalloc(1, sizeof *argv); 2158 *argv = xstrdup("new-session"); 2159 } 2160 2161 pr = cmd_parse_from_arguments(argc, argv, NULL); 2162 switch (pr->status) { 2163 case CMD_PARSE_EMPTY: 2164 cause = xstrdup("empty command"); 2165 goto error; 2166 case CMD_PARSE_ERROR: 2167 cause = pr->error; 2168 goto error; 2169 case CMD_PARSE_SUCCESS: 2170 break; 2171 } 2172 cmd_free_argv(argc, argv); 2173 2174 cmdq_append(c, cmdq_get_command(pr->cmdlist, NULL)); 2175 cmdq_append(c, cmdq_get_callback(server_client_command_done, NULL)); 2176 2177 cmd_list_free(pr->cmdlist); 2178 return; 2179 2180 error: 2181 cmd_free_argv(argc, argv); 2182 2183 cmdq_append(c, cmdq_get_error(cause)); 2184 free(cause); 2185 2186 c->flags |= CLIENT_EXIT; 2187 } 2188 2189 /* Handle identify message. */ 2190 static void 2191 server_client_dispatch_identify(struct client *c, struct imsg *imsg) 2192 { 2193 const char *data, *home; 2194 size_t datalen; 2195 int flags, feat; 2196 uint64_t longflags; 2197 char *name; 2198 2199 if (c->flags & CLIENT_IDENTIFIED) 2200 fatalx("out-of-order identify message"); 2201 2202 data = imsg->data; 2203 datalen = imsg->hdr.len - IMSG_HEADER_SIZE; 2204 2205 switch (imsg->hdr.type) { 2206 case MSG_IDENTIFY_FEATURES: 2207 if (datalen != sizeof feat) 2208 fatalx("bad MSG_IDENTIFY_FEATURES size"); 2209 memcpy(&feat, data, sizeof feat); 2210 c->term_features |= feat; 2211 log_debug("client %p IDENTIFY_FEATURES %s", c, 2212 tty_get_features(feat)); 2213 break; 2214 case MSG_IDENTIFY_FLAGS: 2215 if (datalen != sizeof flags) 2216 fatalx("bad MSG_IDENTIFY_FLAGS size"); 2217 memcpy(&flags, data, sizeof flags); 2218 c->flags |= flags; 2219 log_debug("client %p IDENTIFY_FLAGS %#x", c, flags); 2220 break; 2221 case MSG_IDENTIFY_LONGFLAGS: 2222 if (datalen != sizeof longflags) 2223 fatalx("bad MSG_IDENTIFY_LONGFLAGS size"); 2224 memcpy(&longflags, data, sizeof longflags); 2225 c->flags |= longflags; 2226 log_debug("client %p IDENTIFY_LONGFLAGS %#llx", c, 2227 (unsigned long long)longflags); 2228 break; 2229 case MSG_IDENTIFY_TERM: 2230 if (datalen == 0 || data[datalen - 1] != '\0') 2231 fatalx("bad MSG_IDENTIFY_TERM string"); 2232 if (*data == '\0') 2233 c->term_name = xstrdup("unknown"); 2234 else 2235 c->term_name = xstrdup(data); 2236 log_debug("client %p IDENTIFY_TERM %s", c, data); 2237 break; 2238 case MSG_IDENTIFY_TERMINFO: 2239 if (datalen == 0 || data[datalen - 1] != '\0') 2240 fatalx("bad MSG_IDENTIFY_TERMINFO string"); 2241 c->term_caps = xreallocarray(c->term_caps, c->term_ncaps + 1, 2242 sizeof *c->term_caps); 2243 c->term_caps[c->term_ncaps++] = xstrdup(data); 2244 log_debug("client %p IDENTIFY_TERMINFO %s", c, data); 2245 break; 2246 case MSG_IDENTIFY_TTYNAME: 2247 if (datalen == 0 || data[datalen - 1] != '\0') 2248 fatalx("bad MSG_IDENTIFY_TTYNAME string"); 2249 c->ttyname = xstrdup(data); 2250 log_debug("client %p IDENTIFY_TTYNAME %s", c, data); 2251 break; 2252 case MSG_IDENTIFY_CWD: 2253 if (datalen == 0 || data[datalen - 1] != '\0') 2254 fatalx("bad MSG_IDENTIFY_CWD string"); 2255 if (access(data, X_OK) == 0) 2256 c->cwd = xstrdup(data); 2257 else if ((home = find_home()) != NULL) 2258 c->cwd = xstrdup(home); 2259 else 2260 c->cwd = xstrdup("/"); 2261 log_debug("client %p IDENTIFY_CWD %s", c, data); 2262 break; 2263 case MSG_IDENTIFY_STDIN: 2264 if (datalen != 0) 2265 fatalx("bad MSG_IDENTIFY_STDIN size"); 2266 c->fd = imsg->fd; 2267 log_debug("client %p IDENTIFY_STDIN %d", c, imsg->fd); 2268 break; 2269 case MSG_IDENTIFY_STDOUT: 2270 if (datalen != 0) 2271 fatalx("bad MSG_IDENTIFY_STDOUT size"); 2272 c->out_fd = imsg->fd; 2273 log_debug("client %p IDENTIFY_STDOUT %d", c, imsg->fd); 2274 break; 2275 case MSG_IDENTIFY_ENVIRON: 2276 if (datalen == 0 || data[datalen - 1] != '\0') 2277 fatalx("bad MSG_IDENTIFY_ENVIRON string"); 2278 if (strchr(data, '=') != NULL) 2279 environ_put(c->environ, data, 0); 2280 log_debug("client %p IDENTIFY_ENVIRON %s", c, data); 2281 break; 2282 case MSG_IDENTIFY_CLIENTPID: 2283 if (datalen != sizeof c->pid) 2284 fatalx("bad MSG_IDENTIFY_CLIENTPID size"); 2285 memcpy(&c->pid, data, sizeof c->pid); 2286 log_debug("client %p IDENTIFY_CLIENTPID %ld", c, (long)c->pid); 2287 break; 2288 default: 2289 break; 2290 } 2291 2292 if (imsg->hdr.type != MSG_IDENTIFY_DONE) 2293 return; 2294 c->flags |= CLIENT_IDENTIFIED; 2295 2296 if (*c->ttyname != '\0') 2297 name = xstrdup(c->ttyname); 2298 else 2299 xasprintf(&name, "client-%ld", (long)c->pid); 2300 c->name = name; 2301 log_debug("client %p name is %s", c, c->name); 2302 2303 #ifdef __CYGWIN__ 2304 c->fd = open(c->ttyname, O_RDWR|O_NOCTTY); 2305 #endif 2306 2307 if (c->flags & CLIENT_CONTROL) 2308 control_start(c); 2309 else if (c->fd != -1) { 2310 if (tty_init(&c->tty, c) != 0) { 2311 close(c->fd); 2312 c->fd = -1; 2313 } else { 2314 tty_resize(&c->tty); 2315 c->flags |= CLIENT_TERMINAL; 2316 } 2317 close(c->out_fd); 2318 c->out_fd = -1; 2319 } 2320 2321 /* 2322 * If this is the first client, load configuration files. Any later 2323 * clients are allowed to continue with their command even if the 2324 * config has not been loaded - they might have been run from inside it 2325 */ 2326 if ((~c->flags & CLIENT_EXIT) && 2327 !cfg_finished && 2328 c == TAILQ_FIRST(&clients)) 2329 start_cfg(); 2330 } 2331 2332 /* Handle shell message. */ 2333 static void 2334 server_client_dispatch_shell(struct client *c) 2335 { 2336 const char *shell; 2337 2338 shell = options_get_string(global_s_options, "default-shell"); 2339 if (!checkshell(shell)) 2340 shell = _PATH_BSHELL; 2341 proc_send(c->peer, MSG_SHELL, -1, shell, strlen(shell) + 1); 2342 2343 proc_kill_peer(c->peer); 2344 } 2345 2346 /* Get client working directory. */ 2347 const char * 2348 server_client_get_cwd(struct client *c, struct session *s) 2349 { 2350 const char *home; 2351 2352 if (!cfg_finished && cfg_client != NULL) 2353 return (cfg_client->cwd); 2354 if (c != NULL && c->session == NULL && c->cwd != NULL) 2355 return (c->cwd); 2356 if (s != NULL && s->cwd != NULL) 2357 return (s->cwd); 2358 if (c != NULL && (s = c->session) != NULL && s->cwd != NULL) 2359 return (s->cwd); 2360 if ((home = find_home()) != NULL) 2361 return (home); 2362 return ("/"); 2363 } 2364 2365 /* Get control client flags. */ 2366 static uint64_t 2367 server_client_control_flags(struct client *c, const char *next) 2368 { 2369 if (strcmp(next, "pause-after") == 0) { 2370 c->pause_age = 0; 2371 return (CLIENT_CONTROL_PAUSEAFTER); 2372 } 2373 if (sscanf(next, "pause-after=%u", &c->pause_age) == 1) { 2374 c->pause_age *= 1000; 2375 return (CLIENT_CONTROL_PAUSEAFTER); 2376 } 2377 if (strcmp(next, "no-output") == 0) 2378 return (CLIENT_CONTROL_NOOUTPUT); 2379 if (strcmp(next, "wait-exit") == 0) 2380 return (CLIENT_CONTROL_WAITEXIT); 2381 return (0); 2382 } 2383 2384 /* Set client flags. */ 2385 void 2386 server_client_set_flags(struct client *c, const char *flags) 2387 { 2388 char *s, *copy, *next; 2389 uint64_t flag; 2390 int not; 2391 2392 s = copy = xstrdup (flags); 2393 while ((next = strsep(&s, ",")) != NULL) { 2394 not = (*next == '!'); 2395 if (not) 2396 next++; 2397 2398 if (c->flags & CLIENT_CONTROL) 2399 flag = server_client_control_flags(c, next); 2400 else 2401 flag = 0; 2402 if (strcmp(next, "read-only") == 0) 2403 flag = CLIENT_READONLY; 2404 else if (strcmp(next, "ignore-size") == 0) 2405 flag = CLIENT_IGNORESIZE; 2406 else if (strcmp(next, "active-pane") == 0) 2407 flag = CLIENT_ACTIVEPANE; 2408 if (flag == 0) 2409 continue; 2410 2411 log_debug("client %s set flag %s", c->name, next); 2412 if (not) 2413 c->flags &= ~flag; 2414 else 2415 c->flags |= flag; 2416 if (flag == CLIENT_CONTROL_NOOUTPUT) 2417 control_reset_offsets(c); 2418 } 2419 free(copy); 2420 proc_send(c->peer, MSG_FLAGS, -1, &c->flags, sizeof c->flags); 2421 } 2422 2423 /* Get client flags. This is only flags useful to show to users. */ 2424 const char * 2425 server_client_get_flags(struct client *c) 2426 { 2427 static char s[256]; 2428 char tmp[32]; 2429 2430 *s = '\0'; 2431 if (c->flags & CLIENT_ATTACHED) 2432 strlcat(s, "attached,", sizeof s); 2433 if (c->flags & CLIENT_FOCUSED) 2434 strlcat(s, "focused,", sizeof s); 2435 if (c->flags & CLIENT_CONTROL) 2436 strlcat(s, "control-mode,", sizeof s); 2437 if (c->flags & CLIENT_IGNORESIZE) 2438 strlcat(s, "ignore-size,", sizeof s); 2439 if (c->flags & CLIENT_CONTROL_NOOUTPUT) 2440 strlcat(s, "no-output,", sizeof s); 2441 if (c->flags & CLIENT_CONTROL_WAITEXIT) 2442 strlcat(s, "wait-exit,", sizeof s); 2443 if (c->flags & CLIENT_CONTROL_PAUSEAFTER) { 2444 xsnprintf(tmp, sizeof tmp, "pause-after=%u,", 2445 c->pause_age / 1000); 2446 strlcat(s, tmp, sizeof s); 2447 } 2448 if (c->flags & CLIENT_READONLY) 2449 strlcat(s, "read-only,", sizeof s); 2450 if (c->flags & CLIENT_ACTIVEPANE) 2451 strlcat(s, "active-pane,", sizeof s); 2452 if (c->flags & CLIENT_SUSPENDED) 2453 strlcat(s, "suspended,", sizeof s); 2454 if (c->flags & CLIENT_UTF8) 2455 strlcat(s, "UTF-8,", sizeof s); 2456 if (*s != '\0') 2457 s[strlen(s) - 1] = '\0'; 2458 return (s); 2459 } 2460 2461 /* Get client window. */ 2462 static struct client_window * 2463 server_client_get_client_window(struct client *c, u_int id) 2464 { 2465 struct client_window cw = { .window = id }; 2466 2467 return (RB_FIND(client_windows, &c->windows, &cw)); 2468 } 2469 2470 /* Get client active pane. */ 2471 struct window_pane * 2472 server_client_get_pane(struct client *c) 2473 { 2474 struct session *s = c->session; 2475 struct client_window *cw; 2476 2477 if (s == NULL) 2478 return (NULL); 2479 2480 if (~c->flags & CLIENT_ACTIVEPANE) 2481 return (s->curw->window->active); 2482 cw = server_client_get_client_window(c, s->curw->window->id); 2483 if (cw == NULL) 2484 return (s->curw->window->active); 2485 return (cw->pane); 2486 } 2487 2488 /* Set client active pane. */ 2489 void 2490 server_client_set_pane(struct client *c, struct window_pane *wp) 2491 { 2492 struct session *s = c->session; 2493 struct client_window *cw; 2494 2495 if (s == NULL) 2496 return; 2497 2498 cw = server_client_get_client_window(c, s->curw->window->id); 2499 if (cw == NULL) { 2500 cw = xcalloc(1, sizeof *cw); 2501 cw->window = s->curw->window->id; 2502 RB_INSERT(client_windows, &c->windows, cw); 2503 } 2504 cw->pane = wp; 2505 log_debug("%s pane now %%%u", c->name, wp->id); 2506 } 2507 2508 /* Remove pane from client lists. */ 2509 void 2510 server_client_remove_pane(struct window_pane *wp) 2511 { 2512 struct client *c; 2513 struct window *w = wp->window; 2514 struct client_window *cw; 2515 2516 TAILQ_FOREACH(c, &clients, entry) { 2517 cw = server_client_get_client_window(c, w->id); 2518 if (cw != NULL && cw->pane == wp) { 2519 RB_REMOVE(client_windows, &c->windows, cw); 2520 free(cw); 2521 } 2522 } 2523 } 2524