1 /* $OpenBSD: server-client.c,v 1.358 2020/06/18 08:34:22 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", __func__, wp->id); 1478 window_pane_send_resize(wp, 0); 1479 wp->flags &= ~(PANE_RESIZE|PANE_RESIZEFORCE|PANE_RESIZENOW); 1480 } else if (!evtimer_pending(&wp->force_timer, NULL)) { 1481 log_debug("%s: forcing resize of %%%u", __func__, wp->id); 1482 window_pane_send_resize(wp, 1); 1483 server_client_start_force_timer(wp); 1484 } 1485 } 1486 } 1487 1488 /* Check pane buffer size. */ 1489 static void 1490 server_client_check_pane_buffer(struct window_pane *wp) 1491 { 1492 struct evbuffer *evb = wp->event->input; 1493 size_t minimum; 1494 struct client *c; 1495 struct window_pane_offset *wpo; 1496 int off = 1, flag; 1497 u_int attached_clients = 0; 1498 size_t new_size; 1499 1500 /* 1501 * Work out the minimum used size. This is the most that can be removed 1502 * from the buffer. 1503 */ 1504 minimum = wp->offset.used; 1505 if (wp->pipe_fd != -1 && wp->pipe_offset.used < minimum) 1506 minimum = wp->pipe_offset.used; 1507 TAILQ_FOREACH(c, &clients, entry) { 1508 if (c->session == NULL) 1509 continue; 1510 attached_clients++; 1511 1512 if (~c->flags & CLIENT_CONTROL) { 1513 off = 0; 1514 continue; 1515 } 1516 wpo = control_pane_offset(c, wp, &flag); 1517 if (wpo == NULL) { 1518 off = 0; 1519 continue; 1520 } 1521 if (!flag) 1522 off = 0; 1523 1524 window_pane_get_new_data(wp, wpo, &new_size); 1525 log_debug("%s: %s has %zu bytes used and %zu left for %%%u", 1526 __func__, c->name, wpo->used - wp->base_offset, new_size, 1527 wp->id); 1528 if (wpo->used < minimum) 1529 minimum = wpo->used; 1530 } 1531 if (attached_clients == 0) 1532 off = 0; 1533 minimum -= wp->base_offset; 1534 if (minimum == 0) 1535 goto out; 1536 1537 /* Drain the buffer. */ 1538 log_debug("%s: %%%u has %zu minimum (of %zu) bytes used", __func__, 1539 wp->id, minimum, EVBUFFER_LENGTH(evb)); 1540 evbuffer_drain(evb, minimum); 1541 1542 /* 1543 * Adjust the base offset. If it would roll over, all the offsets into 1544 * the buffer need to be adjusted. 1545 */ 1546 if (wp->base_offset > SIZE_MAX - minimum) { 1547 log_debug("%s: %%%u base offset has wrapped", __func__, wp->id); 1548 wp->offset.used -= wp->base_offset; 1549 if (wp->pipe_fd != -1) 1550 wp->pipe_offset.used -= wp->base_offset; 1551 TAILQ_FOREACH(c, &clients, entry) { 1552 if (c->session == NULL || (~c->flags & CLIENT_CONTROL)) 1553 continue; 1554 wpo = control_pane_offset(c, wp, &flag); 1555 if (wpo != NULL && !flag) 1556 wpo->used -= wp->base_offset; 1557 } 1558 wp->base_offset = minimum; 1559 } else 1560 wp->base_offset += minimum; 1561 1562 out: 1563 /* 1564 * If there is data remaining, and there are no clients able to consume 1565 * it, do not read any more. This is true when there are attached 1566 * clients, all of which are control clients which are not able to 1567 * accept any more data. 1568 */ 1569 log_debug("%s: pane %%%u is %s", __func__, wp->id, off ? "off" : "on"); 1570 if (off) 1571 bufferevent_disable(wp->event, EV_READ); 1572 else 1573 bufferevent_enable(wp->event, EV_READ); 1574 } 1575 1576 /* Check whether pane should be focused. */ 1577 static void 1578 server_client_check_pane_focus(struct window_pane *wp) 1579 { 1580 struct client *c; 1581 int push; 1582 1583 /* Do we need to push the focus state? */ 1584 push = wp->flags & PANE_FOCUSPUSH; 1585 wp->flags &= ~PANE_FOCUSPUSH; 1586 1587 /* If we're not the active pane in our window, we're not focused. */ 1588 if (wp->window->active != wp) 1589 goto not_focused; 1590 1591 /* If we're in a mode, we're not focused. */ 1592 if (wp->screen != &wp->base) 1593 goto not_focused; 1594 1595 /* 1596 * If our window is the current window in any focused clients with an 1597 * attached session, we're focused. 1598 */ 1599 TAILQ_FOREACH(c, &clients, entry) { 1600 if (c->session == NULL || !(c->flags & CLIENT_FOCUSED)) 1601 continue; 1602 if (c->session->attached == 0) 1603 continue; 1604 1605 if (c->session->curw->window == wp->window) 1606 goto focused; 1607 } 1608 1609 not_focused: 1610 if (push || (wp->flags & PANE_FOCUSED)) { 1611 if (wp->base.mode & MODE_FOCUSON) 1612 bufferevent_write(wp->event, "\033[O", 3); 1613 notify_pane("pane-focus-out", wp); 1614 } 1615 wp->flags &= ~PANE_FOCUSED; 1616 return; 1617 1618 focused: 1619 if (push || !(wp->flags & PANE_FOCUSED)) { 1620 if (wp->base.mode & MODE_FOCUSON) 1621 bufferevent_write(wp->event, "\033[I", 3); 1622 notify_pane("pane-focus-in", wp); 1623 session_update_activity(c->session, NULL); 1624 } 1625 wp->flags |= PANE_FOCUSED; 1626 } 1627 1628 /* 1629 * Update cursor position and mode settings. The scroll region and attributes 1630 * are cleared when idle (waiting for an event) as this is the most likely time 1631 * a user may interrupt tmux, for example with ~^Z in ssh(1). This is a 1632 * compromise between excessive resets and likelihood of an interrupt. 1633 * 1634 * tty_region/tty_reset/tty_update_mode already take care of not resetting 1635 * things that are already in their default state. 1636 */ 1637 static void 1638 server_client_reset_state(struct client *c) 1639 { 1640 struct tty *tty = &c->tty; 1641 struct window *w = c->session->curw->window; 1642 struct window_pane *wp = server_client_get_pane(c), *loop; 1643 struct screen *s = NULL; 1644 struct options *oo = c->session->options; 1645 int mode = 0, cursor, flags; 1646 u_int cx = 0, cy = 0, ox, oy, sx, sy; 1647 1648 if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED)) 1649 return; 1650 1651 /* Disable the block flag. */ 1652 flags = (tty->flags & TTY_BLOCK); 1653 tty->flags &= ~TTY_BLOCK; 1654 1655 /* Get mode from overlay if any, else from screen. */ 1656 if (c->overlay_draw != NULL) { 1657 if (c->overlay_mode != NULL) 1658 s = c->overlay_mode(c, &cx, &cy); 1659 } else 1660 s = wp->screen; 1661 if (s != NULL) 1662 mode = s->mode; 1663 if (c->prompt_string != NULL || c->message_string != NULL) 1664 mode &= ~MODE_CURSOR; 1665 log_debug("%s: client %s mode %x", __func__, c->name, mode); 1666 1667 /* Reset region and margin. */ 1668 tty_region_off(tty); 1669 tty_margin_off(tty); 1670 1671 /* Move cursor to pane cursor and offset. */ 1672 if (c->overlay_draw == NULL) { 1673 cursor = 0; 1674 tty_window_offset(tty, &ox, &oy, &sx, &sy); 1675 if (wp->xoff + s->cx >= ox && wp->xoff + s->cx <= ox + sx && 1676 wp->yoff + s->cy >= oy && wp->yoff + s->cy <= oy + sy) { 1677 cursor = 1; 1678 1679 cx = wp->xoff + s->cx - ox; 1680 cy = wp->yoff + s->cy - oy; 1681 1682 if (status_at_line(c) == 0) 1683 cy += status_line_size(c); 1684 } 1685 if (!cursor) 1686 mode &= ~MODE_CURSOR; 1687 } 1688 log_debug("%s: cursor to %u,%u", __func__, cx, cy); 1689 tty_cursor(tty, cx, cy); 1690 1691 /* 1692 * Set mouse mode if requested. To support dragging, always use button 1693 * mode. 1694 */ 1695 if (options_get_number(oo, "mouse")) { 1696 mode &= ~ALL_MOUSE_MODES; 1697 if (c->overlay_draw == NULL) { 1698 TAILQ_FOREACH(loop, &w->panes, entry) { 1699 if (loop->screen->mode & MODE_MOUSE_ALL) 1700 mode |= MODE_MOUSE_ALL; 1701 } 1702 } 1703 if (~mode & MODE_MOUSE_ALL) 1704 mode |= MODE_MOUSE_BUTTON; 1705 } 1706 1707 /* Clear bracketed paste mode if at the prompt. */ 1708 if (c->overlay_draw == NULL && c->prompt_string != NULL) 1709 mode &= ~MODE_BRACKETPASTE; 1710 1711 /* Set the terminal mode and reset attributes. */ 1712 tty_update_mode(tty, mode, s); 1713 tty_reset(tty); 1714 1715 /* All writing must be done, send a sync end (if it was started). */ 1716 tty_sync_end(tty); 1717 tty->flags |= flags; 1718 } 1719 1720 /* Repeat time callback. */ 1721 static void 1722 server_client_repeat_timer(__unused int fd, __unused short events, void *data) 1723 { 1724 struct client *c = data; 1725 1726 if (c->flags & CLIENT_REPEAT) { 1727 server_client_set_key_table(c, NULL); 1728 c->flags &= ~CLIENT_REPEAT; 1729 server_status_client(c); 1730 } 1731 } 1732 1733 /* Double-click callback. */ 1734 static void 1735 server_client_click_timer(__unused int fd, __unused short events, void *data) 1736 { 1737 struct client *c = data; 1738 struct key_event *event; 1739 1740 log_debug("click timer expired"); 1741 1742 if (c->flags & CLIENT_TRIPLECLICK) { 1743 /* 1744 * Waiting for a third click that hasn't happened, so this must 1745 * have been a double click. 1746 */ 1747 event = xmalloc(sizeof *event); 1748 event->key = KEYC_DOUBLECLICK; 1749 memcpy(&event->m, &c->click_event, sizeof event->m); 1750 if (!server_client_handle_key(c, event)) 1751 free(event); 1752 } 1753 c->flags &= ~(CLIENT_DOUBLECLICK|CLIENT_TRIPLECLICK); 1754 } 1755 1756 /* Check if client should be exited. */ 1757 static void 1758 server_client_check_exit(struct client *c) 1759 { 1760 struct client_file *cf; 1761 const char *name = c->exit_session; 1762 char *data; 1763 size_t size, msize; 1764 1765 if (c->flags & (CLIENT_DEAD|CLIENT_EXITED)) 1766 return; 1767 if (~c->flags & CLIENT_EXIT) 1768 return; 1769 1770 if (c->flags & CLIENT_CONTROL) { 1771 control_discard(c); 1772 if (!control_all_done(c)) 1773 return; 1774 } 1775 RB_FOREACH(cf, client_files, &c->files) { 1776 if (EVBUFFER_LENGTH(cf->buffer) != 0) 1777 return; 1778 } 1779 1780 if (c->flags & CLIENT_ATTACHED) 1781 notify_client("client-detached", c); 1782 c->flags |= CLIENT_EXITED; 1783 1784 switch (c->exit_type) { 1785 case CLIENT_EXIT_RETURN: 1786 if (c->exit_message != NULL) { 1787 msize = strlen(c->exit_message) + 1; 1788 size = (sizeof c->retval) + msize; 1789 } else 1790 size = (sizeof c->retval); 1791 data = xmalloc(size); 1792 memcpy(data, &c->retval, sizeof c->retval); 1793 if (c->exit_message != NULL) 1794 memcpy(data + sizeof c->retval, c->exit_message, msize); 1795 proc_send(c->peer, MSG_EXIT, -1, data, size); 1796 free(data); 1797 break; 1798 case CLIENT_EXIT_SHUTDOWN: 1799 proc_send(c->peer, MSG_SHUTDOWN, -1, NULL, 0); 1800 break; 1801 case CLIENT_EXIT_DETACH: 1802 proc_send(c->peer, c->exit_msgtype, -1, name, strlen(name) + 1); 1803 break; 1804 } 1805 free(c->exit_session); 1806 free(c->exit_message); 1807 } 1808 1809 /* Redraw timer callback. */ 1810 static void 1811 server_client_redraw_timer(__unused int fd, __unused short events, 1812 __unused void *data) 1813 { 1814 log_debug("redraw timer fired"); 1815 } 1816 1817 /* Check for client redraws. */ 1818 static void 1819 server_client_check_redraw(struct client *c) 1820 { 1821 struct session *s = c->session; 1822 struct tty *tty = &c->tty; 1823 struct window *w = c->session->curw->window; 1824 struct window_pane *wp; 1825 int needed, flags, mode = tty->mode, new_flags = 0; 1826 int redraw; 1827 u_int bit = 0; 1828 struct timeval tv = { .tv_usec = 1000 }; 1829 static struct event ev; 1830 size_t left; 1831 1832 if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED)) 1833 return; 1834 if (c->flags & CLIENT_ALLREDRAWFLAGS) { 1835 log_debug("%s: redraw%s%s%s%s%s", c->name, 1836 (c->flags & CLIENT_REDRAWWINDOW) ? " window" : "", 1837 (c->flags & CLIENT_REDRAWSTATUS) ? " status" : "", 1838 (c->flags & CLIENT_REDRAWBORDERS) ? " borders" : "", 1839 (c->flags & CLIENT_REDRAWOVERLAY) ? " overlay" : "", 1840 (c->flags & CLIENT_REDRAWPANES) ? " panes" : ""); 1841 } 1842 1843 /* 1844 * If there is outstanding data, defer the redraw until it has been 1845 * consumed. We can just add a timer to get out of the event loop and 1846 * end up back here. 1847 */ 1848 needed = 0; 1849 if (c->flags & CLIENT_ALLREDRAWFLAGS) 1850 needed = 1; 1851 else { 1852 TAILQ_FOREACH(wp, &w->panes, entry) { 1853 if (wp->flags & PANE_REDRAW) { 1854 needed = 1; 1855 break; 1856 } 1857 } 1858 if (needed) 1859 new_flags |= CLIENT_REDRAWPANES; 1860 } 1861 if (needed && (left = EVBUFFER_LENGTH(tty->out)) != 0) { 1862 log_debug("%s: redraw deferred (%zu left)", c->name, left); 1863 if (!evtimer_initialized(&ev)) 1864 evtimer_set(&ev, server_client_redraw_timer, NULL); 1865 if (!evtimer_pending(&ev, NULL)) { 1866 log_debug("redraw timer started"); 1867 evtimer_add(&ev, &tv); 1868 } 1869 1870 if (~c->flags & CLIENT_REDRAWWINDOW) { 1871 TAILQ_FOREACH(wp, &w->panes, entry) { 1872 if (wp->flags & PANE_REDRAW) { 1873 log_debug("%s: pane %%%u needs redraw", 1874 c->name, wp->id); 1875 c->redraw_panes |= (1 << bit); 1876 } 1877 if (++bit == 64) { 1878 /* 1879 * If more that 64 panes, give up and 1880 * just redraw the window. 1881 */ 1882 new_flags &= CLIENT_REDRAWPANES; 1883 new_flags |= CLIENT_REDRAWWINDOW; 1884 break; 1885 } 1886 } 1887 if (c->redraw_panes != 0) 1888 c->flags |= CLIENT_REDRAWPANES; 1889 } 1890 c->flags |= new_flags; 1891 return; 1892 } else if (needed) 1893 log_debug("%s: redraw needed", c->name); 1894 1895 flags = tty->flags & (TTY_BLOCK|TTY_FREEZE|TTY_NOCURSOR); 1896 tty->flags = (tty->flags & ~(TTY_BLOCK|TTY_FREEZE))|TTY_NOCURSOR; 1897 1898 if (~c->flags & CLIENT_REDRAWWINDOW) { 1899 /* 1900 * If not redrawing the entire window, check whether each pane 1901 * needs to be redrawn. 1902 */ 1903 TAILQ_FOREACH(wp, &w->panes, entry) { 1904 redraw = 0; 1905 if (wp->flags & PANE_REDRAW) 1906 redraw = 1; 1907 else if (c->flags & CLIENT_REDRAWPANES) 1908 redraw = !!(c->redraw_panes & (1 << bit)); 1909 bit++; 1910 if (!redraw) 1911 continue; 1912 log_debug("%s: redrawing pane %%%u", __func__, wp->id); 1913 screen_redraw_pane(c, wp); 1914 } 1915 c->redraw_panes = 0; 1916 c->flags &= ~CLIENT_REDRAWPANES; 1917 } 1918 1919 if (c->flags & CLIENT_ALLREDRAWFLAGS) { 1920 if (options_get_number(s->options, "set-titles")) 1921 server_client_set_title(c); 1922 screen_redraw_screen(c); 1923 } 1924 1925 tty->flags = (tty->flags & ~TTY_NOCURSOR)|(flags & TTY_NOCURSOR); 1926 tty_update_mode(tty, mode, NULL); 1927 tty->flags = (tty->flags & ~(TTY_BLOCK|TTY_FREEZE|TTY_NOCURSOR))|flags; 1928 1929 c->flags &= ~(CLIENT_ALLREDRAWFLAGS|CLIENT_STATUSFORCE); 1930 1931 if (needed) { 1932 /* 1933 * We would have deferred the redraw unless the output buffer 1934 * was empty, so we can record how many bytes the redraw 1935 * generated. 1936 */ 1937 c->redraw = EVBUFFER_LENGTH(tty->out); 1938 log_debug("%s: redraw added %zu bytes", c->name, c->redraw); 1939 } 1940 } 1941 1942 /* Set client title. */ 1943 static void 1944 server_client_set_title(struct client *c) 1945 { 1946 struct session *s = c->session; 1947 const char *template; 1948 char *title; 1949 struct format_tree *ft; 1950 1951 template = options_get_string(s->options, "set-titles-string"); 1952 1953 ft = format_create(c, NULL, FORMAT_NONE, 0); 1954 format_defaults(ft, c, NULL, NULL, NULL); 1955 1956 title = format_expand_time(ft, template); 1957 if (c->title == NULL || strcmp(title, c->title) != 0) { 1958 free(c->title); 1959 c->title = xstrdup(title); 1960 tty_set_title(&c->tty, c->title); 1961 } 1962 free(title); 1963 1964 format_free(ft); 1965 } 1966 1967 /* Dispatch message from client. */ 1968 static void 1969 server_client_dispatch(struct imsg *imsg, void *arg) 1970 { 1971 struct client *c = arg; 1972 ssize_t datalen; 1973 struct session *s; 1974 1975 if (c->flags & CLIENT_DEAD) 1976 return; 1977 1978 if (imsg == NULL) { 1979 server_client_lost(c); 1980 return; 1981 } 1982 1983 datalen = imsg->hdr.len - IMSG_HEADER_SIZE; 1984 1985 switch (imsg->hdr.type) { 1986 case MSG_IDENTIFY_FEATURES: 1987 case MSG_IDENTIFY_FLAGS: 1988 case MSG_IDENTIFY_TERM: 1989 case MSG_IDENTIFY_TTYNAME: 1990 case MSG_IDENTIFY_CWD: 1991 case MSG_IDENTIFY_STDIN: 1992 case MSG_IDENTIFY_STDOUT: 1993 case MSG_IDENTIFY_ENVIRON: 1994 case MSG_IDENTIFY_CLIENTPID: 1995 case MSG_IDENTIFY_DONE: 1996 server_client_dispatch_identify(c, imsg); 1997 break; 1998 case MSG_COMMAND: 1999 server_client_dispatch_command(c, imsg); 2000 break; 2001 case MSG_RESIZE: 2002 if (datalen != 0) 2003 fatalx("bad MSG_RESIZE size"); 2004 2005 if (c->flags & CLIENT_CONTROL) 2006 break; 2007 server_client_update_latest(c); 2008 server_client_clear_overlay(c); 2009 tty_resize(&c->tty); 2010 recalculate_sizes(); 2011 server_redraw_client(c); 2012 if (c->session != NULL) 2013 notify_client("client-resized", c); 2014 break; 2015 case MSG_EXITING: 2016 if (datalen != 0) 2017 fatalx("bad MSG_EXITING size"); 2018 c->session = NULL; 2019 tty_close(&c->tty); 2020 proc_send(c->peer, MSG_EXITED, -1, NULL, 0); 2021 break; 2022 case MSG_WAKEUP: 2023 case MSG_UNLOCK: 2024 if (datalen != 0) 2025 fatalx("bad MSG_WAKEUP size"); 2026 2027 if (!(c->flags & CLIENT_SUSPENDED)) 2028 break; 2029 c->flags &= ~CLIENT_SUSPENDED; 2030 2031 if (c->fd == -1) /* exited in the meantime */ 2032 break; 2033 s = c->session; 2034 2035 if (gettimeofday(&c->activity_time, NULL) != 0) 2036 fatal("gettimeofday failed"); 2037 2038 tty_start_tty(&c->tty); 2039 server_redraw_client(c); 2040 recalculate_sizes(); 2041 2042 if (s != NULL) 2043 session_update_activity(s, &c->activity_time); 2044 break; 2045 case MSG_SHELL: 2046 if (datalen != 0) 2047 fatalx("bad MSG_SHELL size"); 2048 2049 server_client_dispatch_shell(c); 2050 break; 2051 case MSG_WRITE_READY: 2052 server_client_dispatch_write_ready(c, imsg); 2053 break; 2054 case MSG_READ: 2055 server_client_dispatch_read_data(c, imsg); 2056 break; 2057 case MSG_READ_DONE: 2058 server_client_dispatch_read_done(c, imsg); 2059 break; 2060 } 2061 } 2062 2063 /* Callback when command is done. */ 2064 static enum cmd_retval 2065 server_client_command_done(struct cmdq_item *item, __unused void *data) 2066 { 2067 struct client *c = cmdq_get_client(item); 2068 2069 if (~c->flags & CLIENT_ATTACHED) 2070 c->flags |= CLIENT_EXIT; 2071 else if (~c->flags & CLIENT_EXIT) 2072 tty_send_requests(&c->tty); 2073 return (CMD_RETURN_NORMAL); 2074 } 2075 2076 /* Handle command message. */ 2077 static void 2078 server_client_dispatch_command(struct client *c, struct imsg *imsg) 2079 { 2080 struct msg_command data; 2081 char *buf; 2082 size_t len; 2083 int argc; 2084 char **argv, *cause; 2085 struct cmd_parse_result *pr; 2086 2087 if (c->flags & CLIENT_EXIT) 2088 return; 2089 2090 if (imsg->hdr.len - IMSG_HEADER_SIZE < sizeof data) 2091 fatalx("bad MSG_COMMAND size"); 2092 memcpy(&data, imsg->data, sizeof data); 2093 2094 buf = (char *)imsg->data + sizeof data; 2095 len = imsg->hdr.len - IMSG_HEADER_SIZE - sizeof data; 2096 if (len > 0 && buf[len - 1] != '\0') 2097 fatalx("bad MSG_COMMAND string"); 2098 2099 argc = data.argc; 2100 if (cmd_unpack_argv(buf, len, argc, &argv) != 0) { 2101 cause = xstrdup("command too long"); 2102 goto error; 2103 } 2104 2105 if (argc == 0) { 2106 argc = 1; 2107 argv = xcalloc(1, sizeof *argv); 2108 *argv = xstrdup("new-session"); 2109 } 2110 2111 pr = cmd_parse_from_arguments(argc, argv, NULL); 2112 switch (pr->status) { 2113 case CMD_PARSE_EMPTY: 2114 cause = xstrdup("empty command"); 2115 goto error; 2116 case CMD_PARSE_ERROR: 2117 cause = pr->error; 2118 goto error; 2119 case CMD_PARSE_SUCCESS: 2120 break; 2121 } 2122 cmd_free_argv(argc, argv); 2123 2124 cmdq_append(c, cmdq_get_command(pr->cmdlist, NULL)); 2125 cmdq_append(c, cmdq_get_callback(server_client_command_done, NULL)); 2126 2127 cmd_list_free(pr->cmdlist); 2128 return; 2129 2130 error: 2131 cmd_free_argv(argc, argv); 2132 2133 cmdq_append(c, cmdq_get_error(cause)); 2134 free(cause); 2135 2136 c->flags |= CLIENT_EXIT; 2137 } 2138 2139 /* Handle identify message. */ 2140 static void 2141 server_client_dispatch_identify(struct client *c, struct imsg *imsg) 2142 { 2143 const char *data, *home; 2144 size_t datalen; 2145 int flags, feat; 2146 char *name; 2147 2148 if (c->flags & CLIENT_IDENTIFIED) 2149 fatalx("out-of-order identify message"); 2150 2151 data = imsg->data; 2152 datalen = imsg->hdr.len - IMSG_HEADER_SIZE; 2153 2154 switch (imsg->hdr.type) { 2155 case MSG_IDENTIFY_FEATURES: 2156 if (datalen != sizeof feat) 2157 fatalx("bad MSG_IDENTIFY_FEATURES size"); 2158 memcpy(&feat, data, sizeof feat); 2159 c->term_features |= feat; 2160 log_debug("client %p IDENTIFY_FEATURES %s", c, 2161 tty_get_features(feat)); 2162 break; 2163 case MSG_IDENTIFY_FLAGS: 2164 if (datalen != sizeof flags) 2165 fatalx("bad MSG_IDENTIFY_FLAGS size"); 2166 memcpy(&flags, data, sizeof flags); 2167 c->flags |= flags; 2168 log_debug("client %p IDENTIFY_FLAGS %#x", c, flags); 2169 break; 2170 case MSG_IDENTIFY_TERM: 2171 if (datalen == 0 || data[datalen - 1] != '\0') 2172 fatalx("bad MSG_IDENTIFY_TERM string"); 2173 if (*data == '\0') 2174 c->term_name = xstrdup("unknown"); 2175 else 2176 c->term_name = xstrdup(data); 2177 log_debug("client %p IDENTIFY_TERM %s", c, data); 2178 break; 2179 case MSG_IDENTIFY_TTYNAME: 2180 if (datalen == 0 || data[datalen - 1] != '\0') 2181 fatalx("bad MSG_IDENTIFY_TTYNAME string"); 2182 c->ttyname = xstrdup(data); 2183 log_debug("client %p IDENTIFY_TTYNAME %s", c, data); 2184 break; 2185 case MSG_IDENTIFY_CWD: 2186 if (datalen == 0 || data[datalen - 1] != '\0') 2187 fatalx("bad MSG_IDENTIFY_CWD string"); 2188 if (access(data, X_OK) == 0) 2189 c->cwd = xstrdup(data); 2190 else if ((home = find_home()) != NULL) 2191 c->cwd = xstrdup(home); 2192 else 2193 c->cwd = xstrdup("/"); 2194 log_debug("client %p IDENTIFY_CWD %s", c, data); 2195 break; 2196 case MSG_IDENTIFY_STDIN: 2197 if (datalen != 0) 2198 fatalx("bad MSG_IDENTIFY_STDIN size"); 2199 c->fd = imsg->fd; 2200 log_debug("client %p IDENTIFY_STDIN %d", c, imsg->fd); 2201 break; 2202 case MSG_IDENTIFY_STDOUT: 2203 if (datalen != 0) 2204 fatalx("bad MSG_IDENTIFY_STDOUT size"); 2205 c->out_fd = imsg->fd; 2206 log_debug("client %p IDENTIFY_STDOUT %d", c, imsg->fd); 2207 break; 2208 case MSG_IDENTIFY_ENVIRON: 2209 if (datalen == 0 || data[datalen - 1] != '\0') 2210 fatalx("bad MSG_IDENTIFY_ENVIRON string"); 2211 if (strchr(data, '=') != NULL) 2212 environ_put(c->environ, data, 0); 2213 log_debug("client %p IDENTIFY_ENVIRON %s", c, data); 2214 break; 2215 case MSG_IDENTIFY_CLIENTPID: 2216 if (datalen != sizeof c->pid) 2217 fatalx("bad MSG_IDENTIFY_CLIENTPID size"); 2218 memcpy(&c->pid, data, sizeof c->pid); 2219 log_debug("client %p IDENTIFY_CLIENTPID %ld", c, (long)c->pid); 2220 break; 2221 default: 2222 break; 2223 } 2224 2225 if (imsg->hdr.type != MSG_IDENTIFY_DONE) 2226 return; 2227 c->flags |= CLIENT_IDENTIFIED; 2228 2229 if (*c->ttyname != '\0') 2230 name = xstrdup(c->ttyname); 2231 else 2232 xasprintf(&name, "client-%ld", (long)c->pid); 2233 c->name = name; 2234 log_debug("client %p name is %s", c, c->name); 2235 2236 if (c->flags & CLIENT_CONTROL) 2237 control_start(c); 2238 else if (c->fd != -1) { 2239 if (tty_init(&c->tty, c) != 0) { 2240 close(c->fd); 2241 c->fd = -1; 2242 } else { 2243 tty_resize(&c->tty); 2244 c->flags |= CLIENT_TERMINAL; 2245 } 2246 close(c->out_fd); 2247 c->out_fd = -1; 2248 } 2249 2250 /* 2251 * If this is the first client that has finished identifying, load 2252 * configuration files. 2253 */ 2254 if ((~c->flags & CLIENT_EXIT) && 2255 !cfg_finished && 2256 c == TAILQ_FIRST(&clients) && 2257 TAILQ_NEXT(c, entry) == NULL) 2258 start_cfg(); 2259 } 2260 2261 /* Handle shell message. */ 2262 static void 2263 server_client_dispatch_shell(struct client *c) 2264 { 2265 const char *shell; 2266 2267 shell = options_get_string(global_s_options, "default-shell"); 2268 if (!checkshell(shell)) 2269 shell = _PATH_BSHELL; 2270 proc_send(c->peer, MSG_SHELL, -1, shell, strlen(shell) + 1); 2271 2272 proc_kill_peer(c->peer); 2273 } 2274 2275 /* Handle write ready message. */ 2276 static void 2277 server_client_dispatch_write_ready(struct client *c, struct imsg *imsg) 2278 { 2279 struct msg_write_ready *msg = imsg->data; 2280 size_t msglen = imsg->hdr.len - IMSG_HEADER_SIZE; 2281 struct client_file find, *cf; 2282 2283 if (msglen != sizeof *msg) 2284 fatalx("bad MSG_WRITE_READY size"); 2285 find.stream = msg->stream; 2286 if ((cf = RB_FIND(client_files, &c->files, &find)) == NULL) 2287 return; 2288 if (msg->error != 0) { 2289 cf->error = msg->error; 2290 file_fire_done(cf); 2291 } else 2292 file_push(cf); 2293 } 2294 2295 /* Handle read data message. */ 2296 static void 2297 server_client_dispatch_read_data(struct client *c, struct imsg *imsg) 2298 { 2299 struct msg_read_data *msg = imsg->data; 2300 size_t msglen = imsg->hdr.len - IMSG_HEADER_SIZE; 2301 struct client_file find, *cf; 2302 void *bdata = msg + 1; 2303 size_t bsize = msglen - sizeof *msg; 2304 2305 if (msglen < sizeof *msg) 2306 fatalx("bad MSG_READ_DATA size"); 2307 find.stream = msg->stream; 2308 if ((cf = RB_FIND(client_files, &c->files, &find)) == NULL) 2309 return; 2310 2311 log_debug("%s: file %d read %zu bytes", c->name, cf->stream, bsize); 2312 if (cf->error == 0) { 2313 if (evbuffer_add(cf->buffer, bdata, bsize) != 0) { 2314 cf->error = ENOMEM; 2315 file_fire_done(cf); 2316 } else 2317 file_fire_read(cf); 2318 } 2319 } 2320 2321 /* Handle read done message. */ 2322 static void 2323 server_client_dispatch_read_done(struct client *c, struct imsg *imsg) 2324 { 2325 struct msg_read_done *msg = imsg->data; 2326 size_t msglen = imsg->hdr.len - IMSG_HEADER_SIZE; 2327 struct client_file find, *cf; 2328 2329 if (msglen != sizeof *msg) 2330 fatalx("bad MSG_READ_DONE size"); 2331 find.stream = msg->stream; 2332 if ((cf = RB_FIND(client_files, &c->files, &find)) == NULL) 2333 return; 2334 2335 log_debug("%s: file %d read done", c->name, cf->stream); 2336 cf->error = msg->error; 2337 file_fire_done(cf); 2338 } 2339 2340 /* Get client working directory. */ 2341 const char * 2342 server_client_get_cwd(struct client *c, struct session *s) 2343 { 2344 const char *home; 2345 2346 if (!cfg_finished && cfg_client != NULL) 2347 return (cfg_client->cwd); 2348 if (c != NULL && c->session == NULL && c->cwd != NULL) 2349 return (c->cwd); 2350 if (s != NULL && s->cwd != NULL) 2351 return (s->cwd); 2352 if (c != NULL && (s = c->session) != NULL && s->cwd != NULL) 2353 return (s->cwd); 2354 if ((home = find_home()) != NULL) 2355 return (home); 2356 return ("/"); 2357 } 2358 2359 /* Get control client flags. */ 2360 static uint64_t 2361 server_client_control_flags(struct client *c, const char *next) 2362 { 2363 if (strcmp(next, "pause-after") == 0) { 2364 c->pause_age = 0; 2365 return (CLIENT_CONTROL_PAUSEAFTER); 2366 } 2367 if (sscanf(next, "pause-after=%u", &c->pause_age) == 1) { 2368 c->pause_age *= 1000; 2369 return (CLIENT_CONTROL_PAUSEAFTER); 2370 } 2371 if (strcmp(next, "no-output") == 0) 2372 return (CLIENT_CONTROL_NOOUTPUT); 2373 if (strcmp(next, "wait-exit") == 0) 2374 return (CLIENT_CONTROL_WAITEXIT); 2375 return (0); 2376 } 2377 2378 /* Set client flags. */ 2379 void 2380 server_client_set_flags(struct client *c, const char *flags) 2381 { 2382 char *s, *copy, *next; 2383 uint64_t flag; 2384 int not; 2385 2386 s = copy = xstrdup (flags); 2387 while ((next = strsep(&s, ",")) != NULL) { 2388 not = (*next == '!'); 2389 if (not) 2390 next++; 2391 2392 if (c->flags & CLIENT_CONTROL) 2393 flag = server_client_control_flags(c, next); 2394 else 2395 flag = 0; 2396 if (strcmp(next, "read-only") == 0) 2397 flag = CLIENT_READONLY; 2398 else if (strcmp(next, "ignore-size") == 0) 2399 flag = CLIENT_IGNORESIZE; 2400 else if (strcmp(next, "active-pane") == 0) 2401 flag = CLIENT_ACTIVEPANE; 2402 if (flag == 0) 2403 continue; 2404 2405 log_debug("client %s set flag %s", c->name, next); 2406 if (not) 2407 c->flags &= ~flag; 2408 else 2409 c->flags |= flag; 2410 if (flag == CLIENT_CONTROL_NOOUTPUT) 2411 control_reset_offsets(c); 2412 } 2413 free(copy); 2414 proc_send(c->peer, MSG_FLAGS, -1, &c->flags, sizeof c->flags); 2415 } 2416 2417 /* Get client flags. This is only flags useful to show to users. */ 2418 const char * 2419 server_client_get_flags(struct client *c) 2420 { 2421 static char s[256]; 2422 char tmp[32]; 2423 2424 *s = '\0'; 2425 if (c->flags & CLIENT_ATTACHED) 2426 strlcat(s, "attached,", sizeof s); 2427 if (c->flags & CLIENT_CONTROL) 2428 strlcat(s, "control-mode,", sizeof s); 2429 if (c->flags & CLIENT_IGNORESIZE) 2430 strlcat(s, "ignore-size,", sizeof s); 2431 if (c->flags & CLIENT_CONTROL_NOOUTPUT) 2432 strlcat(s, "no-output,", sizeof s); 2433 if (c->flags & CLIENT_CONTROL_WAITEXIT) 2434 strlcat(s, "wait-exit,", sizeof s); 2435 if (c->flags & CLIENT_CONTROL_PAUSEAFTER) { 2436 xsnprintf(tmp, sizeof tmp, "pause-after=%u,", 2437 c->pause_age / 1000); 2438 strlcat(s, tmp, sizeof s); 2439 } 2440 if (c->flags & CLIENT_READONLY) 2441 strlcat(s, "read-only,", sizeof s); 2442 if (c->flags & CLIENT_ACTIVEPANE) 2443 strlcat(s, "active-pane,", sizeof s); 2444 if (c->flags & CLIENT_SUSPENDED) 2445 strlcat(s, "suspended,", sizeof s); 2446 if (c->flags & CLIENT_UTF8) 2447 strlcat(s, "UTF-8,", sizeof s); 2448 if (*s != '\0') 2449 s[strlen(s) - 1] = '\0'; 2450 return (s); 2451 } 2452 2453 /* Get client window. */ 2454 static struct client_window * 2455 server_client_get_client_window(struct client *c, u_int id) 2456 { 2457 struct client_window cw = { .window = id }; 2458 2459 return (RB_FIND(client_windows, &c->windows, &cw)); 2460 } 2461 2462 /* Get client active pane. */ 2463 struct window_pane * 2464 server_client_get_pane(struct client *c) 2465 { 2466 struct session *s = c->session; 2467 struct client_window *cw; 2468 2469 if (s == NULL) 2470 return (NULL); 2471 2472 if (~c->flags & CLIENT_ACTIVEPANE) 2473 return (s->curw->window->active); 2474 cw = server_client_get_client_window(c, s->curw->window->id); 2475 if (cw == NULL) 2476 return (s->curw->window->active); 2477 return (cw->pane); 2478 } 2479 2480 /* Set client active pane. */ 2481 void 2482 server_client_set_pane(struct client *c, struct window_pane *wp) 2483 { 2484 struct session *s = c->session; 2485 struct client_window *cw; 2486 2487 if (s == NULL) 2488 return; 2489 2490 cw = server_client_get_client_window(c, s->curw->window->id); 2491 if (cw == NULL) { 2492 cw = xcalloc(1, sizeof *cw); 2493 cw->window = s->curw->window->id; 2494 RB_INSERT(client_windows, &c->windows, cw); 2495 } 2496 cw->pane = wp; 2497 log_debug("%s pane now %%%u", c->name, wp->id); 2498 } 2499 2500 /* Remove pane from client lists. */ 2501 void 2502 server_client_remove_pane(struct window_pane *wp) 2503 { 2504 struct client *c; 2505 struct window *w = wp->window; 2506 struct client_window *cw; 2507 2508 TAILQ_FOREACH(c, &clients, entry) { 2509 cw = server_client_get_client_window(c, w->id); 2510 if (cw != NULL && cw->pane == wp) { 2511 RB_REMOVE(client_windows, &c->windows, cw); 2512 free(cw); 2513 } 2514 } 2515 } 2516