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