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