1 /* $OpenBSD: server-client.c,v 1.407 2024/08/27 07:25:27 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 #include <vis.h> 33 34 #include "tmux.h" 35 36 static void server_client_free(int, short, void *); 37 static void server_client_check_pane_resize(struct window_pane *); 38 static void server_client_check_pane_buffer(struct window_pane *); 39 static void server_client_check_window_resize(struct window *); 40 static key_code server_client_check_mouse(struct client *, struct key_event *); 41 static void server_client_repeat_timer(int, short, void *); 42 static void server_client_click_timer(int, short, void *); 43 static void server_client_check_exit(struct client *); 44 static void server_client_check_redraw(struct client *); 45 static void server_client_check_modes(struct client *); 46 static void server_client_set_title(struct client *); 47 static void server_client_set_path(struct client *); 48 static void server_client_reset_state(struct client *); 49 static int server_client_is_bracket_pasting(struct client *, key_code); 50 static int server_client_assume_paste(struct session *); 51 static void server_client_update_latest(struct client *); 52 53 static void server_client_dispatch(struct imsg *, void *); 54 static void server_client_dispatch_command(struct client *, struct imsg *); 55 static void server_client_dispatch_identify(struct client *, struct imsg *); 56 static void server_client_dispatch_shell(struct client *); 57 58 /* Compare client windows. */ 59 static int 60 server_client_window_cmp(struct client_window *cw1, 61 struct client_window *cw2) 62 { 63 if (cw1->window < cw2->window) 64 return (-1); 65 if (cw1->window > cw2->window) 66 return (1); 67 return (0); 68 } 69 RB_GENERATE(client_windows, client_window, entry, server_client_window_cmp); 70 71 /* Number of attached clients. */ 72 u_int 73 server_client_how_many(void) 74 { 75 struct client *c; 76 u_int n; 77 78 n = 0; 79 TAILQ_FOREACH(c, &clients, entry) { 80 if (c->session != NULL && (~c->flags & CLIENT_UNATTACHEDFLAGS)) 81 n++; 82 } 83 return (n); 84 } 85 86 /* Overlay timer callback. */ 87 static void 88 server_client_overlay_timer(__unused int fd, __unused short events, void *data) 89 { 90 server_client_clear_overlay(data); 91 } 92 93 /* Set an overlay on client. */ 94 void 95 server_client_set_overlay(struct client *c, u_int delay, 96 overlay_check_cb checkcb, overlay_mode_cb modecb, 97 overlay_draw_cb drawcb, overlay_key_cb keycb, overlay_free_cb freecb, 98 overlay_resize_cb resizecb, void *data) 99 { 100 struct timeval tv; 101 102 if (c->overlay_draw != NULL) 103 server_client_clear_overlay(c); 104 105 tv.tv_sec = delay / 1000; 106 tv.tv_usec = (delay % 1000) * 1000L; 107 108 if (event_initialized(&c->overlay_timer)) 109 evtimer_del(&c->overlay_timer); 110 evtimer_set(&c->overlay_timer, server_client_overlay_timer, c); 111 if (delay != 0) 112 evtimer_add(&c->overlay_timer, &tv); 113 114 c->overlay_check = checkcb; 115 c->overlay_mode = modecb; 116 c->overlay_draw = drawcb; 117 c->overlay_key = keycb; 118 c->overlay_free = freecb; 119 c->overlay_resize = resizecb; 120 c->overlay_data = data; 121 122 if (c->overlay_check == NULL) 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, c->overlay_data); 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 /* 154 * Given overlay position and dimensions, return parts of the input range which 155 * are visible. 156 */ 157 void 158 server_client_overlay_range(u_int x, u_int y, u_int sx, u_int sy, u_int px, 159 u_int py, u_int nx, struct overlay_ranges *r) 160 { 161 u_int ox, onx; 162 163 /* Return up to 2 ranges. */ 164 r->px[2] = 0; 165 r->nx[2] = 0; 166 167 /* Trivial case of no overlap in the y direction. */ 168 if (py < y || py > y + sy - 1) { 169 r->px[0] = px; 170 r->nx[0] = nx; 171 r->px[1] = 0; 172 r->nx[1] = 0; 173 return; 174 } 175 176 /* Visible bit to the left of the popup. */ 177 if (px < x) { 178 r->px[0] = px; 179 r->nx[0] = x - px; 180 if (r->nx[0] > nx) 181 r->nx[0] = nx; 182 } else { 183 r->px[0] = 0; 184 r->nx[0] = 0; 185 } 186 187 /* Visible bit to the right of the popup. */ 188 ox = x + sx; 189 if (px > ox) 190 ox = px; 191 onx = px + nx; 192 if (onx > ox) { 193 r->px[1] = ox; 194 r->nx[1] = onx - ox; 195 } else { 196 r->px[1] = 0; 197 r->nx[1] = 0; 198 } 199 } 200 201 /* Check if this client is inside this server. */ 202 int 203 server_client_check_nested(struct client *c) 204 { 205 struct environ_entry *envent; 206 struct window_pane *wp; 207 208 envent = environ_find(c->environ, "TMUX"); 209 if (envent == NULL || *envent->value == '\0') 210 return (0); 211 212 RB_FOREACH(wp, window_pane_tree, &all_window_panes) { 213 if (strcmp(wp->tty, c->ttyname) == 0) 214 return (1); 215 } 216 return (0); 217 } 218 219 /* Set client key table. */ 220 void 221 server_client_set_key_table(struct client *c, const char *name) 222 { 223 if (name == NULL) 224 name = server_client_get_key_table(c); 225 226 key_bindings_unref_table(c->keytable); 227 c->keytable = key_bindings_get_table(name, 1); 228 c->keytable->references++; 229 } 230 231 /* Get default key table. */ 232 const char * 233 server_client_get_key_table(struct client *c) 234 { 235 struct session *s = c->session; 236 const char *name; 237 238 if (s == NULL) 239 return ("root"); 240 241 name = options_get_string(s->options, "key-table"); 242 if (*name == '\0') 243 return ("root"); 244 return (name); 245 } 246 247 /* Is this table the default key table? */ 248 static int 249 server_client_is_default_key_table(struct client *c, struct key_table *table) 250 { 251 return (strcmp(table->name, server_client_get_key_table(c)) == 0); 252 } 253 254 /* Create a new client. */ 255 struct client * 256 server_client_create(int fd) 257 { 258 struct client *c; 259 260 setblocking(fd, 0); 261 262 c = xcalloc(1, sizeof *c); 263 c->references = 1; 264 c->peer = proc_add_peer(server_proc, fd, server_client_dispatch, c); 265 266 if (gettimeofday(&c->creation_time, NULL) != 0) 267 fatal("gettimeofday failed"); 268 memcpy(&c->activity_time, &c->creation_time, sizeof c->activity_time); 269 270 c->environ = environ_create(); 271 272 c->fd = -1; 273 c->out_fd = -1; 274 275 c->queue = cmdq_new(); 276 RB_INIT(&c->windows); 277 RB_INIT(&c->files); 278 279 c->tty.sx = 80; 280 c->tty.sy = 24; 281 282 status_init(c); 283 c->flags |= CLIENT_FOCUSED; 284 285 c->keytable = key_bindings_get_table("root", 1); 286 c->keytable->references++; 287 288 evtimer_set(&c->repeat_timer, server_client_repeat_timer, c); 289 evtimer_set(&c->click_timer, server_client_click_timer, c); 290 291 TAILQ_INSERT_TAIL(&clients, c, entry); 292 log_debug("new client %p", c); 293 return (c); 294 } 295 296 /* Open client terminal if needed. */ 297 int 298 server_client_open(struct client *c, char **cause) 299 { 300 const char *ttynam = _PATH_TTY; 301 302 if (c->flags & CLIENT_CONTROL) 303 return (0); 304 305 if (strcmp(c->ttyname, ttynam) == 0|| 306 ((isatty(STDIN_FILENO) && 307 (ttynam = ttyname(STDIN_FILENO)) != NULL && 308 strcmp(c->ttyname, ttynam) == 0) || 309 (isatty(STDOUT_FILENO) && 310 (ttynam = ttyname(STDOUT_FILENO)) != NULL && 311 strcmp(c->ttyname, ttynam) == 0) || 312 (isatty(STDERR_FILENO) && 313 (ttynam = ttyname(STDERR_FILENO)) != NULL && 314 strcmp(c->ttyname, ttynam) == 0))) { 315 xasprintf(cause, "can't use %s", c->ttyname); 316 return (-1); 317 } 318 319 if (!(c->flags & CLIENT_TERMINAL)) { 320 *cause = xstrdup("not a terminal"); 321 return (-1); 322 } 323 324 if (tty_open(&c->tty, cause) != 0) 325 return (-1); 326 327 return (0); 328 } 329 330 /* Lost an attached client. */ 331 static void 332 server_client_attached_lost(struct client *c) 333 { 334 struct session *s; 335 struct window *w; 336 struct client *loop; 337 struct client *found; 338 339 log_debug("lost attached client %p", c); 340 341 /* 342 * By this point the session in the client has been cleared so walk all 343 * windows to find any with this client as the latest. 344 */ 345 RB_FOREACH(w, windows, &windows) { 346 if (w->latest != c) 347 continue; 348 349 found = NULL; 350 TAILQ_FOREACH(loop, &clients, entry) { 351 s = loop->session; 352 if (loop == c || s == NULL || s->curw->window != w) 353 continue; 354 if (found == NULL || timercmp(&loop->activity_time, 355 &found->activity_time, >)) 356 found = loop; 357 } 358 if (found != NULL) 359 server_client_update_latest(found); 360 } 361 } 362 363 /* Set client session. */ 364 void 365 server_client_set_session(struct client *c, struct session *s) 366 { 367 struct session *old = c->session; 368 369 if (s != NULL && c->session != NULL && c->session != s) 370 c->last_session = c->session; 371 else if (s == NULL) 372 c->last_session = NULL; 373 c->session = s; 374 c->flags |= CLIENT_FOCUSED; 375 376 if (old != NULL && old->curw != NULL) 377 window_update_focus(old->curw->window); 378 if (s != NULL) { 379 recalculate_sizes(); 380 window_update_focus(s->curw->window); 381 session_update_activity(s, NULL); 382 gettimeofday(&s->last_attached_time, NULL); 383 s->curw->flags &= ~WINLINK_ALERTFLAGS; 384 s->curw->window->latest = c; 385 alerts_check_session(s); 386 tty_update_client_offset(c); 387 status_timer_start(c); 388 notify_client("client-session-changed", c); 389 server_redraw_client(c); 390 } 391 392 server_check_unattached(); 393 server_update_socket(); 394 } 395 396 /* Lost a client. */ 397 void 398 server_client_lost(struct client *c) 399 { 400 struct client_file *cf, *cf1; 401 struct client_window *cw, *cw1; 402 403 c->flags |= CLIENT_DEAD; 404 405 server_client_clear_overlay(c); 406 status_prompt_clear(c); 407 status_message_clear(c); 408 409 RB_FOREACH_SAFE(cf, client_files, &c->files, cf1) { 410 cf->error = EINTR; 411 file_fire_done(cf); 412 } 413 RB_FOREACH_SAFE(cw, client_windows, &c->windows, cw1) { 414 RB_REMOVE(client_windows, &c->windows, cw); 415 free(cw); 416 } 417 418 TAILQ_REMOVE(&clients, c, entry); 419 log_debug("lost client %p", c); 420 421 if (c->flags & CLIENT_ATTACHED) { 422 server_client_attached_lost(c); 423 notify_client("client-detached", c); 424 } 425 426 if (c->flags & CLIENT_CONTROL) 427 control_stop(c); 428 if (c->flags & CLIENT_TERMINAL) 429 tty_free(&c->tty); 430 free(c->ttyname); 431 free(c->clipboard_panes); 432 433 free(c->term_name); 434 free(c->term_type); 435 tty_term_free_list(c->term_caps, c->term_ncaps); 436 437 status_free(c); 438 439 free(c->title); 440 free((void *)c->cwd); 441 442 evtimer_del(&c->repeat_timer); 443 evtimer_del(&c->click_timer); 444 445 key_bindings_unref_table(c->keytable); 446 447 free(c->message_string); 448 if (event_initialized(&c->message_timer)) 449 evtimer_del(&c->message_timer); 450 451 free(c->prompt_saved); 452 free(c->prompt_string); 453 free(c->prompt_buffer); 454 455 format_lost_client(c); 456 environ_free(c->environ); 457 458 proc_remove_peer(c->peer); 459 c->peer = NULL; 460 461 if (c->out_fd != -1) 462 close(c->out_fd); 463 if (c->fd != -1) { 464 close(c->fd); 465 c->fd = -1; 466 } 467 server_client_unref(c); 468 469 server_add_accept(0); /* may be more file descriptors now */ 470 471 recalculate_sizes(); 472 server_check_unattached(); 473 server_update_socket(); 474 } 475 476 /* Remove reference from a client. */ 477 void 478 server_client_unref(struct client *c) 479 { 480 log_debug("unref client %p (%d references)", c, c->references); 481 482 c->references--; 483 if (c->references == 0) 484 event_once(-1, EV_TIMEOUT, server_client_free, c, NULL); 485 } 486 487 /* Free dead client. */ 488 static void 489 server_client_free(__unused int fd, __unused short events, void *arg) 490 { 491 struct client *c = arg; 492 493 log_debug("free client %p (%d references)", c, c->references); 494 495 cmdq_free(c->queue); 496 497 if (c->references == 0) { 498 free((void *)c->name); 499 free(c); 500 } 501 } 502 503 /* Suspend a client. */ 504 void 505 server_client_suspend(struct client *c) 506 { 507 struct session *s = c->session; 508 509 if (s == NULL || (c->flags & CLIENT_UNATTACHEDFLAGS)) 510 return; 511 512 tty_stop_tty(&c->tty); 513 c->flags |= CLIENT_SUSPENDED; 514 proc_send(c->peer, MSG_SUSPEND, -1, NULL, 0); 515 } 516 517 /* Detach a client. */ 518 void 519 server_client_detach(struct client *c, enum msgtype msgtype) 520 { 521 struct session *s = c->session; 522 523 if (s == NULL || (c->flags & CLIENT_NODETACHFLAGS)) 524 return; 525 526 c->flags |= CLIENT_EXIT; 527 528 c->exit_type = CLIENT_EXIT_DETACH; 529 c->exit_msgtype = msgtype; 530 c->exit_session = xstrdup(s->name); 531 } 532 533 /* Execute command to replace a client. */ 534 void 535 server_client_exec(struct client *c, const char *cmd) 536 { 537 struct session *s = c->session; 538 char *msg; 539 const char *shell; 540 size_t cmdsize, shellsize; 541 542 if (*cmd == '\0') 543 return; 544 cmdsize = strlen(cmd) + 1; 545 546 if (s != NULL) 547 shell = options_get_string(s->options, "default-shell"); 548 else 549 shell = options_get_string(global_s_options, "default-shell"); 550 if (!checkshell(shell)) 551 shell = _PATH_BSHELL; 552 shellsize = strlen(shell) + 1; 553 554 msg = xmalloc(cmdsize + shellsize); 555 memcpy(msg, cmd, cmdsize); 556 memcpy(msg + cmdsize, shell, shellsize); 557 558 proc_send(c->peer, MSG_EXEC, -1, msg, cmdsize + shellsize); 559 free(msg); 560 } 561 562 /* Check for mouse keys. */ 563 static key_code 564 server_client_check_mouse(struct client *c, struct key_event *event) 565 { 566 struct mouse_event *m = &event->m; 567 struct session *s = c->session, *fs; 568 struct winlink *fwl; 569 struct window_pane *wp, *fwp; 570 u_int x, y, b, sx, sy, px, py; 571 int ignore = 0; 572 key_code key; 573 struct timeval tv; 574 struct style_range *sr; 575 enum { NOTYPE, 576 MOVE, 577 DOWN, 578 UP, 579 DRAG, 580 WHEEL, 581 SECOND, 582 DOUBLE, 583 TRIPLE } type = NOTYPE; 584 enum { NOWHERE, 585 PANE, 586 STATUS, 587 STATUS_LEFT, 588 STATUS_RIGHT, 589 STATUS_DEFAULT, 590 BORDER } where = NOWHERE; 591 592 log_debug("%s mouse %02x at %u,%u (last %u,%u) (%d)", c->name, m->b, 593 m->x, m->y, m->lx, m->ly, c->tty.mouse_drag_flag); 594 595 /* What type of event is this? */ 596 if (event->key == KEYC_DOUBLECLICK) { 597 type = DOUBLE; 598 x = m->x, y = m->y, b = m->b; 599 ignore = 1; 600 log_debug("double-click at %u,%u", x, y); 601 } else if ((m->sgr_type != ' ' && 602 MOUSE_DRAG(m->sgr_b) && 603 MOUSE_RELEASE(m->sgr_b)) || 604 (m->sgr_type == ' ' && 605 MOUSE_DRAG(m->b) && 606 MOUSE_RELEASE(m->b) && 607 MOUSE_RELEASE(m->lb))) { 608 type = MOVE; 609 x = m->x, y = m->y, b = 0; 610 log_debug("move at %u,%u", x, y); 611 } else if (MOUSE_DRAG(m->b)) { 612 type = DRAG; 613 if (c->tty.mouse_drag_flag) { 614 x = m->x, y = m->y, b = m->b; 615 if (x == m->lx && y == m->ly) 616 return (KEYC_UNKNOWN); 617 log_debug("drag update at %u,%u", x, y); 618 } else { 619 x = m->lx, y = m->ly, b = m->lb; 620 log_debug("drag start at %u,%u", x, y); 621 } 622 } else if (MOUSE_WHEEL(m->b)) { 623 type = WHEEL; 624 x = m->x, y = m->y, b = m->b; 625 log_debug("wheel at %u,%u", x, y); 626 } else if (MOUSE_RELEASE(m->b)) { 627 type = UP; 628 x = m->x, y = m->y, b = m->lb; 629 if (m->sgr_type == 'm') 630 b = m->sgr_b; 631 log_debug("up at %u,%u", x, y); 632 } else { 633 if (c->flags & CLIENT_DOUBLECLICK) { 634 evtimer_del(&c->click_timer); 635 c->flags &= ~CLIENT_DOUBLECLICK; 636 if (m->b == c->click_button) { 637 type = SECOND; 638 x = m->x, y = m->y, b = m->b; 639 log_debug("second-click at %u,%u", x, y); 640 c->flags |= CLIENT_TRIPLECLICK; 641 } 642 } else if (c->flags & CLIENT_TRIPLECLICK) { 643 evtimer_del(&c->click_timer); 644 c->flags &= ~CLIENT_TRIPLECLICK; 645 if (m->b == c->click_button) { 646 type = TRIPLE; 647 x = m->x, y = m->y, b = m->b; 648 log_debug("triple-click at %u,%u", x, y); 649 goto have_event; 650 } 651 } 652 653 /* DOWN is the only remaining event type. */ 654 if (type == NOTYPE) { 655 type = DOWN; 656 x = m->x, y = m->y, b = m->b; 657 log_debug("down at %u,%u", x, y); 658 c->flags |= CLIENT_DOUBLECLICK; 659 } 660 661 if (KEYC_CLICK_TIMEOUT != 0) { 662 memcpy(&c->click_event, m, sizeof c->click_event); 663 c->click_button = m->b; 664 665 log_debug("click timer started"); 666 tv.tv_sec = KEYC_CLICK_TIMEOUT / 1000; 667 tv.tv_usec = (KEYC_CLICK_TIMEOUT % 1000) * 1000L; 668 evtimer_del(&c->click_timer); 669 evtimer_add(&c->click_timer, &tv); 670 } 671 } 672 673 have_event: 674 if (type == NOTYPE) 675 return (KEYC_UNKNOWN); 676 677 /* Save the session. */ 678 m->s = s->id; 679 m->w = -1; 680 m->wp = -1; 681 m->ignore = ignore; 682 683 /* Is this on the status line? */ 684 m->statusat = status_at_line(c); 685 m->statuslines = status_line_size(c); 686 if (m->statusat != -1 && 687 y >= (u_int)m->statusat && 688 y < m->statusat + m->statuslines) { 689 sr = status_get_range(c, x, y - m->statusat); 690 if (sr == NULL) { 691 where = STATUS_DEFAULT; 692 } else { 693 switch (sr->type) { 694 case STYLE_RANGE_NONE: 695 return (KEYC_UNKNOWN); 696 case STYLE_RANGE_LEFT: 697 log_debug("mouse range: left"); 698 where = STATUS_LEFT; 699 break; 700 case STYLE_RANGE_RIGHT: 701 log_debug("mouse range: right"); 702 where = STATUS_RIGHT; 703 break; 704 case STYLE_RANGE_PANE: 705 fwp = window_pane_find_by_id(sr->argument); 706 if (fwp == NULL) 707 return (KEYC_UNKNOWN); 708 m->wp = sr->argument; 709 710 log_debug("mouse range: pane %%%u", m->wp); 711 where = STATUS; 712 break; 713 case STYLE_RANGE_WINDOW: 714 fwl = winlink_find_by_index(&s->windows, 715 sr->argument); 716 if (fwl == NULL) 717 return (KEYC_UNKNOWN); 718 m->w = fwl->window->id; 719 720 log_debug("mouse range: window @%u", m->w); 721 where = STATUS; 722 break; 723 case STYLE_RANGE_SESSION: 724 fs = session_find_by_id(sr->argument); 725 if (fs == NULL) 726 return (KEYC_UNKNOWN); 727 m->s = sr->argument; 728 729 log_debug("mouse range: session $%u", m->s); 730 where = STATUS; 731 break; 732 case STYLE_RANGE_USER: 733 where = STATUS; 734 break; 735 } 736 } 737 } 738 739 /* Not on status line. Adjust position and check for border or pane. */ 740 if (where == NOWHERE) { 741 px = x; 742 if (m->statusat == 0 && y >= m->statuslines) 743 py = y - m->statuslines; 744 else if (m->statusat > 0 && y >= (u_int)m->statusat) 745 py = m->statusat - 1; 746 else 747 py = y; 748 749 tty_window_offset(&c->tty, &m->ox, &m->oy, &sx, &sy); 750 log_debug("mouse window @%u at %u,%u (%ux%u)", 751 s->curw->window->id, m->ox, m->oy, sx, sy); 752 if (px > sx || py > sy) 753 return (KEYC_UNKNOWN); 754 px = px + m->ox; 755 py = py + m->oy; 756 757 /* Try the pane borders if not zoomed. */ 758 if (~s->curw->window->flags & WINDOW_ZOOMED) { 759 TAILQ_FOREACH(wp, &s->curw->window->panes, entry) { 760 if ((wp->xoff + wp->sx == px && 761 wp->yoff <= 1 + py && 762 wp->yoff + wp->sy >= py) || 763 (wp->yoff + wp->sy == py && 764 wp->xoff <= 1 + px && 765 wp->xoff + wp->sx >= px)) 766 break; 767 } 768 if (wp != NULL) 769 where = BORDER; 770 } 771 772 /* Otherwise try inside the pane. */ 773 if (where == NOWHERE) { 774 wp = window_get_active_at(s->curw->window, px, py); 775 if (wp != NULL) 776 where = PANE; 777 else 778 return (KEYC_UNKNOWN); 779 } 780 if (where == PANE) 781 log_debug("mouse %u,%u on pane %%%u", x, y, wp->id); 782 else if (where == BORDER) 783 log_debug("mouse on pane %%%u border", wp->id); 784 m->wp = wp->id; 785 m->w = wp->window->id; 786 } 787 788 /* Stop dragging if needed. */ 789 if (type != DRAG && type != WHEEL && c->tty.mouse_drag_flag != 0) { 790 if (c->tty.mouse_drag_release != NULL) 791 c->tty.mouse_drag_release(c, m); 792 793 c->tty.mouse_drag_update = NULL; 794 c->tty.mouse_drag_release = NULL; 795 796 /* 797 * End a mouse drag by passing a MouseDragEnd key corresponding 798 * to the button that started the drag. 799 */ 800 switch (c->tty.mouse_drag_flag - 1) { 801 case MOUSE_BUTTON_1: 802 if (where == PANE) 803 key = KEYC_MOUSEDRAGEND1_PANE; 804 if (where == STATUS) 805 key = KEYC_MOUSEDRAGEND1_STATUS; 806 if (where == STATUS_LEFT) 807 key = KEYC_MOUSEDRAGEND1_STATUS_LEFT; 808 if (where == STATUS_RIGHT) 809 key = KEYC_MOUSEDRAGEND1_STATUS_RIGHT; 810 if (where == STATUS_DEFAULT) 811 key = KEYC_MOUSEDRAGEND1_STATUS_DEFAULT; 812 if (where == BORDER) 813 key = KEYC_MOUSEDRAGEND1_BORDER; 814 break; 815 case MOUSE_BUTTON_2: 816 if (where == PANE) 817 key = KEYC_MOUSEDRAGEND2_PANE; 818 if (where == STATUS) 819 key = KEYC_MOUSEDRAGEND2_STATUS; 820 if (where == STATUS_LEFT) 821 key = KEYC_MOUSEDRAGEND2_STATUS_LEFT; 822 if (where == STATUS_RIGHT) 823 key = KEYC_MOUSEDRAGEND2_STATUS_RIGHT; 824 if (where == STATUS_DEFAULT) 825 key = KEYC_MOUSEDRAGEND2_STATUS_DEFAULT; 826 if (where == BORDER) 827 key = KEYC_MOUSEDRAGEND2_BORDER; 828 break; 829 case MOUSE_BUTTON_3: 830 if (where == PANE) 831 key = KEYC_MOUSEDRAGEND3_PANE; 832 if (where == STATUS) 833 key = KEYC_MOUSEDRAGEND3_STATUS; 834 if (where == STATUS_LEFT) 835 key = KEYC_MOUSEDRAGEND3_STATUS_LEFT; 836 if (where == STATUS_RIGHT) 837 key = KEYC_MOUSEDRAGEND3_STATUS_RIGHT; 838 if (where == STATUS_DEFAULT) 839 key = KEYC_MOUSEDRAGEND3_STATUS_DEFAULT; 840 if (where == BORDER) 841 key = KEYC_MOUSEDRAGEND3_BORDER; 842 break; 843 case MOUSE_BUTTON_6: 844 if (where == PANE) 845 key = KEYC_MOUSEDRAGEND6_PANE; 846 if (where == STATUS) 847 key = KEYC_MOUSEDRAGEND6_STATUS; 848 if (where == STATUS_LEFT) 849 key = KEYC_MOUSEDRAGEND6_STATUS_LEFT; 850 if (where == STATUS_RIGHT) 851 key = KEYC_MOUSEDRAGEND6_STATUS_RIGHT; 852 if (where == STATUS_DEFAULT) 853 key = KEYC_MOUSEDRAGEND6_STATUS_DEFAULT; 854 if (where == BORDER) 855 key = KEYC_MOUSEDRAGEND6_BORDER; 856 break; 857 case MOUSE_BUTTON_7: 858 if (where == PANE) 859 key = KEYC_MOUSEDRAGEND7_PANE; 860 if (where == STATUS) 861 key = KEYC_MOUSEDRAGEND7_STATUS; 862 if (where == STATUS_LEFT) 863 key = KEYC_MOUSEDRAGEND7_STATUS_LEFT; 864 if (where == STATUS_RIGHT) 865 key = KEYC_MOUSEDRAGEND7_STATUS_RIGHT; 866 if (where == STATUS_DEFAULT) 867 key = KEYC_MOUSEDRAGEND7_STATUS_DEFAULT; 868 if (where == BORDER) 869 key = KEYC_MOUSEDRAGEND7_BORDER; 870 break; 871 case MOUSE_BUTTON_8: 872 if (where == PANE) 873 key = KEYC_MOUSEDRAGEND8_PANE; 874 if (where == STATUS) 875 key = KEYC_MOUSEDRAGEND8_STATUS; 876 if (where == STATUS_LEFT) 877 key = KEYC_MOUSEDRAGEND8_STATUS_LEFT; 878 if (where == STATUS_RIGHT) 879 key = KEYC_MOUSEDRAGEND8_STATUS_RIGHT; 880 if (where == STATUS_DEFAULT) 881 key = KEYC_MOUSEDRAGEND8_STATUS_DEFAULT; 882 if (where == BORDER) 883 key = KEYC_MOUSEDRAGEND8_BORDER; 884 break; 885 case MOUSE_BUTTON_9: 886 if (where == PANE) 887 key = KEYC_MOUSEDRAGEND9_PANE; 888 if (where == STATUS) 889 key = KEYC_MOUSEDRAGEND9_STATUS; 890 if (where == STATUS_LEFT) 891 key = KEYC_MOUSEDRAGEND9_STATUS_LEFT; 892 if (where == STATUS_RIGHT) 893 key = KEYC_MOUSEDRAGEND9_STATUS_RIGHT; 894 if (where == STATUS_DEFAULT) 895 key = KEYC_MOUSEDRAGEND9_STATUS_DEFAULT; 896 if (where == BORDER) 897 key = KEYC_MOUSEDRAGEND9_BORDER; 898 break; 899 case MOUSE_BUTTON_10: 900 if (where == PANE) 901 key = KEYC_MOUSEDRAGEND10_PANE; 902 if (where == STATUS) 903 key = KEYC_MOUSEDRAGEND10_STATUS; 904 if (where == STATUS_LEFT) 905 key = KEYC_MOUSEDRAGEND10_STATUS_LEFT; 906 if (where == STATUS_RIGHT) 907 key = KEYC_MOUSEDRAGEND10_STATUS_RIGHT; 908 if (where == STATUS_DEFAULT) 909 key = KEYC_MOUSEDRAGEND10_STATUS_DEFAULT; 910 if (where == BORDER) 911 key = KEYC_MOUSEDRAGEND10_BORDER; 912 break; 913 case MOUSE_BUTTON_11: 914 if (where == PANE) 915 key = KEYC_MOUSEDRAGEND11_PANE; 916 if (where == STATUS) 917 key = KEYC_MOUSEDRAGEND11_STATUS; 918 if (where == STATUS_LEFT) 919 key = KEYC_MOUSEDRAGEND11_STATUS_LEFT; 920 if (where == STATUS_RIGHT) 921 key = KEYC_MOUSEDRAGEND11_STATUS_RIGHT; 922 if (where == STATUS_DEFAULT) 923 key = KEYC_MOUSEDRAGEND11_STATUS_DEFAULT; 924 if (where == BORDER) 925 key = KEYC_MOUSEDRAGEND11_BORDER; 926 break; 927 default: 928 key = KEYC_MOUSE; 929 break; 930 } 931 c->tty.mouse_drag_flag = 0; 932 goto out; 933 } 934 935 /* Convert to a key binding. */ 936 key = KEYC_UNKNOWN; 937 switch (type) { 938 case NOTYPE: 939 break; 940 case MOVE: 941 if (where == PANE) 942 key = KEYC_MOUSEMOVE_PANE; 943 if (where == STATUS) 944 key = KEYC_MOUSEMOVE_STATUS; 945 if (where == STATUS_LEFT) 946 key = KEYC_MOUSEMOVE_STATUS_LEFT; 947 if (where == STATUS_RIGHT) 948 key = KEYC_MOUSEMOVE_STATUS_RIGHT; 949 if (where == STATUS_DEFAULT) 950 key = KEYC_MOUSEMOVE_STATUS_DEFAULT; 951 if (where == BORDER) 952 key = KEYC_MOUSEMOVE_BORDER; 953 break; 954 case DRAG: 955 if (c->tty.mouse_drag_update != NULL) 956 key = KEYC_DRAGGING; 957 else { 958 switch (MOUSE_BUTTONS(b)) { 959 case MOUSE_BUTTON_1: 960 if (where == PANE) 961 key = KEYC_MOUSEDRAG1_PANE; 962 if (where == STATUS) 963 key = KEYC_MOUSEDRAG1_STATUS; 964 if (where == STATUS_LEFT) 965 key = KEYC_MOUSEDRAG1_STATUS_LEFT; 966 if (where == STATUS_RIGHT) 967 key = KEYC_MOUSEDRAG1_STATUS_RIGHT; 968 if (where == STATUS_DEFAULT) 969 key = KEYC_MOUSEDRAG1_STATUS_DEFAULT; 970 if (where == BORDER) 971 key = KEYC_MOUSEDRAG1_BORDER; 972 break; 973 case MOUSE_BUTTON_2: 974 if (where == PANE) 975 key = KEYC_MOUSEDRAG2_PANE; 976 if (where == STATUS) 977 key = KEYC_MOUSEDRAG2_STATUS; 978 if (where == STATUS_LEFT) 979 key = KEYC_MOUSEDRAG2_STATUS_LEFT; 980 if (where == STATUS_RIGHT) 981 key = KEYC_MOUSEDRAG2_STATUS_RIGHT; 982 if (where == STATUS_DEFAULT) 983 key = KEYC_MOUSEDRAG2_STATUS_DEFAULT; 984 if (where == BORDER) 985 key = KEYC_MOUSEDRAG2_BORDER; 986 break; 987 case MOUSE_BUTTON_3: 988 if (where == PANE) 989 key = KEYC_MOUSEDRAG3_PANE; 990 if (where == STATUS) 991 key = KEYC_MOUSEDRAG3_STATUS; 992 if (where == STATUS_LEFT) 993 key = KEYC_MOUSEDRAG3_STATUS_LEFT; 994 if (where == STATUS_RIGHT) 995 key = KEYC_MOUSEDRAG3_STATUS_RIGHT; 996 if (where == STATUS_DEFAULT) 997 key = KEYC_MOUSEDRAG3_STATUS_DEFAULT; 998 if (where == BORDER) 999 key = KEYC_MOUSEDRAG3_BORDER; 1000 break; 1001 case MOUSE_BUTTON_6: 1002 if (where == PANE) 1003 key = KEYC_MOUSEDRAG6_PANE; 1004 if (where == STATUS) 1005 key = KEYC_MOUSEDRAG6_STATUS; 1006 if (where == STATUS_LEFT) 1007 key = KEYC_MOUSEDRAG6_STATUS_LEFT; 1008 if (where == STATUS_RIGHT) 1009 key = KEYC_MOUSEDRAG6_STATUS_RIGHT; 1010 if (where == STATUS_DEFAULT) 1011 key = KEYC_MOUSEDRAG6_STATUS_DEFAULT; 1012 if (where == BORDER) 1013 key = KEYC_MOUSEDRAG6_BORDER; 1014 break; 1015 case MOUSE_BUTTON_7: 1016 if (where == PANE) 1017 key = KEYC_MOUSEDRAG7_PANE; 1018 if (where == STATUS) 1019 key = KEYC_MOUSEDRAG7_STATUS; 1020 if (where == STATUS_LEFT) 1021 key = KEYC_MOUSEDRAG7_STATUS_LEFT; 1022 if (where == STATUS_RIGHT) 1023 key = KEYC_MOUSEDRAG7_STATUS_RIGHT; 1024 if (where == STATUS_DEFAULT) 1025 key = KEYC_MOUSEDRAG7_STATUS_DEFAULT; 1026 if (where == BORDER) 1027 key = KEYC_MOUSEDRAG7_BORDER; 1028 break; 1029 case MOUSE_BUTTON_8: 1030 if (where == PANE) 1031 key = KEYC_MOUSEDRAG8_PANE; 1032 if (where == STATUS) 1033 key = KEYC_MOUSEDRAG8_STATUS; 1034 if (where == STATUS_LEFT) 1035 key = KEYC_MOUSEDRAG8_STATUS_LEFT; 1036 if (where == STATUS_RIGHT) 1037 key = KEYC_MOUSEDRAG8_STATUS_RIGHT; 1038 if (where == STATUS_DEFAULT) 1039 key = KEYC_MOUSEDRAG8_STATUS_DEFAULT; 1040 if (where == BORDER) 1041 key = KEYC_MOUSEDRAG8_BORDER; 1042 break; 1043 case MOUSE_BUTTON_9: 1044 if (where == PANE) 1045 key = KEYC_MOUSEDRAG9_PANE; 1046 if (where == STATUS) 1047 key = KEYC_MOUSEDRAG9_STATUS; 1048 if (where == STATUS_LEFT) 1049 key = KEYC_MOUSEDRAG9_STATUS_LEFT; 1050 if (where == STATUS_RIGHT) 1051 key = KEYC_MOUSEDRAG9_STATUS_RIGHT; 1052 if (where == STATUS_DEFAULT) 1053 key = KEYC_MOUSEDRAG9_STATUS_DEFAULT; 1054 if (where == BORDER) 1055 key = KEYC_MOUSEDRAG9_BORDER; 1056 break; 1057 case MOUSE_BUTTON_10: 1058 if (where == PANE) 1059 key = KEYC_MOUSEDRAG10_PANE; 1060 if (where == STATUS) 1061 key = KEYC_MOUSEDRAG10_STATUS; 1062 if (where == STATUS_LEFT) 1063 key = KEYC_MOUSEDRAG10_STATUS_LEFT; 1064 if (where == STATUS_RIGHT) 1065 key = KEYC_MOUSEDRAG10_STATUS_RIGHT; 1066 if (where == STATUS_DEFAULT) 1067 key = KEYC_MOUSEDRAG10_STATUS_DEFAULT; 1068 if (where == BORDER) 1069 key = KEYC_MOUSEDRAG10_BORDER; 1070 break; 1071 case MOUSE_BUTTON_11: 1072 if (where == PANE) 1073 key = KEYC_MOUSEDRAG11_PANE; 1074 if (where == STATUS) 1075 key = KEYC_MOUSEDRAG11_STATUS; 1076 if (where == STATUS_LEFT) 1077 key = KEYC_MOUSEDRAG11_STATUS_LEFT; 1078 if (where == STATUS_RIGHT) 1079 key = KEYC_MOUSEDRAG11_STATUS_RIGHT; 1080 if (where == STATUS_DEFAULT) 1081 key = KEYC_MOUSEDRAG11_STATUS_DEFAULT; 1082 if (where == BORDER) 1083 key = KEYC_MOUSEDRAG11_BORDER; 1084 break; 1085 } 1086 } 1087 1088 /* 1089 * Begin a drag by setting the flag to a non-zero value that 1090 * corresponds to the mouse button in use. 1091 */ 1092 c->tty.mouse_drag_flag = MOUSE_BUTTONS(b) + 1; 1093 break; 1094 case WHEEL: 1095 if (MOUSE_BUTTONS(b) == MOUSE_WHEEL_UP) { 1096 if (where == PANE) 1097 key = KEYC_WHEELUP_PANE; 1098 if (where == STATUS) 1099 key = KEYC_WHEELUP_STATUS; 1100 if (where == STATUS_LEFT) 1101 key = KEYC_WHEELUP_STATUS_LEFT; 1102 if (where == STATUS_RIGHT) 1103 key = KEYC_WHEELUP_STATUS_RIGHT; 1104 if (where == STATUS_DEFAULT) 1105 key = KEYC_WHEELUP_STATUS_DEFAULT; 1106 if (where == BORDER) 1107 key = KEYC_WHEELUP_BORDER; 1108 } else { 1109 if (where == PANE) 1110 key = KEYC_WHEELDOWN_PANE; 1111 if (where == STATUS) 1112 key = KEYC_WHEELDOWN_STATUS; 1113 if (where == STATUS_LEFT) 1114 key = KEYC_WHEELDOWN_STATUS_LEFT; 1115 if (where == STATUS_RIGHT) 1116 key = KEYC_WHEELDOWN_STATUS_RIGHT; 1117 if (where == STATUS_DEFAULT) 1118 key = KEYC_WHEELDOWN_STATUS_DEFAULT; 1119 if (where == BORDER) 1120 key = KEYC_WHEELDOWN_BORDER; 1121 } 1122 break; 1123 case UP: 1124 switch (MOUSE_BUTTONS(b)) { 1125 case MOUSE_BUTTON_1: 1126 if (where == PANE) 1127 key = KEYC_MOUSEUP1_PANE; 1128 if (where == STATUS) 1129 key = KEYC_MOUSEUP1_STATUS; 1130 if (where == STATUS_LEFT) 1131 key = KEYC_MOUSEUP1_STATUS_LEFT; 1132 if (where == STATUS_RIGHT) 1133 key = KEYC_MOUSEUP1_STATUS_RIGHT; 1134 if (where == STATUS_DEFAULT) 1135 key = KEYC_MOUSEUP1_STATUS_DEFAULT; 1136 if (where == BORDER) 1137 key = KEYC_MOUSEUP1_BORDER; 1138 break; 1139 case MOUSE_BUTTON_2: 1140 if (where == PANE) 1141 key = KEYC_MOUSEUP2_PANE; 1142 if (where == STATUS) 1143 key = KEYC_MOUSEUP2_STATUS; 1144 if (where == STATUS_LEFT) 1145 key = KEYC_MOUSEUP2_STATUS_LEFT; 1146 if (where == STATUS_RIGHT) 1147 key = KEYC_MOUSEUP2_STATUS_RIGHT; 1148 if (where == STATUS_DEFAULT) 1149 key = KEYC_MOUSEUP2_STATUS_DEFAULT; 1150 if (where == BORDER) 1151 key = KEYC_MOUSEUP2_BORDER; 1152 break; 1153 case MOUSE_BUTTON_3: 1154 if (where == PANE) 1155 key = KEYC_MOUSEUP3_PANE; 1156 if (where == STATUS) 1157 key = KEYC_MOUSEUP3_STATUS; 1158 if (where == STATUS_LEFT) 1159 key = KEYC_MOUSEUP3_STATUS_LEFT; 1160 if (where == STATUS_RIGHT) 1161 key = KEYC_MOUSEUP3_STATUS_RIGHT; 1162 if (where == STATUS_DEFAULT) 1163 key = KEYC_MOUSEUP3_STATUS_DEFAULT; 1164 if (where == BORDER) 1165 key = KEYC_MOUSEUP3_BORDER; 1166 break; 1167 case MOUSE_BUTTON_6: 1168 if (where == PANE) 1169 key = KEYC_MOUSEUP6_PANE; 1170 if (where == STATUS) 1171 key = KEYC_MOUSEUP6_STATUS; 1172 if (where == STATUS_LEFT) 1173 key = KEYC_MOUSEUP6_STATUS_LEFT; 1174 if (where == STATUS_RIGHT) 1175 key = KEYC_MOUSEUP6_STATUS_RIGHT; 1176 if (where == STATUS_DEFAULT) 1177 key = KEYC_MOUSEUP6_STATUS_DEFAULT; 1178 if (where == BORDER) 1179 key = KEYC_MOUSEUP6_BORDER; 1180 break; 1181 case MOUSE_BUTTON_7: 1182 if (where == PANE) 1183 key = KEYC_MOUSEUP7_PANE; 1184 if (where == STATUS) 1185 key = KEYC_MOUSEUP7_STATUS; 1186 if (where == STATUS_LEFT) 1187 key = KEYC_MOUSEUP7_STATUS_LEFT; 1188 if (where == STATUS_RIGHT) 1189 key = KEYC_MOUSEUP7_STATUS_RIGHT; 1190 if (where == STATUS_DEFAULT) 1191 key = KEYC_MOUSEUP7_STATUS_DEFAULT; 1192 if (where == BORDER) 1193 key = KEYC_MOUSEUP7_BORDER; 1194 break; 1195 case MOUSE_BUTTON_8: 1196 if (where == PANE) 1197 key = KEYC_MOUSEUP8_PANE; 1198 if (where == STATUS) 1199 key = KEYC_MOUSEUP8_STATUS; 1200 if (where == STATUS_LEFT) 1201 key = KEYC_MOUSEUP8_STATUS_LEFT; 1202 if (where == STATUS_RIGHT) 1203 key = KEYC_MOUSEUP8_STATUS_RIGHT; 1204 if (where == STATUS_DEFAULT) 1205 key = KEYC_MOUSEUP8_STATUS_DEFAULT; 1206 if (where == BORDER) 1207 key = KEYC_MOUSEUP8_BORDER; 1208 break; 1209 case MOUSE_BUTTON_9: 1210 if (where == PANE) 1211 key = KEYC_MOUSEUP9_PANE; 1212 if (where == STATUS) 1213 key = KEYC_MOUSEUP9_STATUS; 1214 if (where == STATUS_LEFT) 1215 key = KEYC_MOUSEUP9_STATUS_LEFT; 1216 if (where == STATUS_RIGHT) 1217 key = KEYC_MOUSEUP9_STATUS_RIGHT; 1218 if (where == STATUS_DEFAULT) 1219 key = KEYC_MOUSEUP9_STATUS_DEFAULT; 1220 if (where == BORDER) 1221 key = KEYC_MOUSEUP9_BORDER; 1222 break; 1223 case MOUSE_BUTTON_10: 1224 if (where == PANE) 1225 key = KEYC_MOUSEUP1_PANE; 1226 if (where == STATUS) 1227 key = KEYC_MOUSEUP1_STATUS; 1228 if (where == STATUS_LEFT) 1229 key = KEYC_MOUSEUP1_STATUS_LEFT; 1230 if (where == STATUS_RIGHT) 1231 key = KEYC_MOUSEUP1_STATUS_RIGHT; 1232 if (where == STATUS_DEFAULT) 1233 key = KEYC_MOUSEUP1_STATUS_DEFAULT; 1234 if (where == BORDER) 1235 key = KEYC_MOUSEUP1_BORDER; 1236 break; 1237 case MOUSE_BUTTON_11: 1238 if (where == PANE) 1239 key = KEYC_MOUSEUP11_PANE; 1240 if (where == STATUS) 1241 key = KEYC_MOUSEUP11_STATUS; 1242 if (where == STATUS_LEFT) 1243 key = KEYC_MOUSEUP11_STATUS_LEFT; 1244 if (where == STATUS_RIGHT) 1245 key = KEYC_MOUSEUP11_STATUS_RIGHT; 1246 if (where == STATUS_DEFAULT) 1247 key = KEYC_MOUSEUP11_STATUS_DEFAULT; 1248 if (where == BORDER) 1249 key = KEYC_MOUSEUP11_BORDER; 1250 break; 1251 } 1252 break; 1253 case DOWN: 1254 switch (MOUSE_BUTTONS(b)) { 1255 case MOUSE_BUTTON_1: 1256 if (where == PANE) 1257 key = KEYC_MOUSEDOWN1_PANE; 1258 if (where == STATUS) 1259 key = KEYC_MOUSEDOWN1_STATUS; 1260 if (where == STATUS_LEFT) 1261 key = KEYC_MOUSEDOWN1_STATUS_LEFT; 1262 if (where == STATUS_RIGHT) 1263 key = KEYC_MOUSEDOWN1_STATUS_RIGHT; 1264 if (where == STATUS_DEFAULT) 1265 key = KEYC_MOUSEDOWN1_STATUS_DEFAULT; 1266 if (where == BORDER) 1267 key = KEYC_MOUSEDOWN1_BORDER; 1268 break; 1269 case MOUSE_BUTTON_2: 1270 if (where == PANE) 1271 key = KEYC_MOUSEDOWN2_PANE; 1272 if (where == STATUS) 1273 key = KEYC_MOUSEDOWN2_STATUS; 1274 if (where == STATUS_LEFT) 1275 key = KEYC_MOUSEDOWN2_STATUS_LEFT; 1276 if (where == STATUS_RIGHT) 1277 key = KEYC_MOUSEDOWN2_STATUS_RIGHT; 1278 if (where == STATUS_DEFAULT) 1279 key = KEYC_MOUSEDOWN2_STATUS_DEFAULT; 1280 if (where == BORDER) 1281 key = KEYC_MOUSEDOWN2_BORDER; 1282 break; 1283 case MOUSE_BUTTON_3: 1284 if (where == PANE) 1285 key = KEYC_MOUSEDOWN3_PANE; 1286 if (where == STATUS) 1287 key = KEYC_MOUSEDOWN3_STATUS; 1288 if (where == STATUS_LEFT) 1289 key = KEYC_MOUSEDOWN3_STATUS_LEFT; 1290 if (where == STATUS_RIGHT) 1291 key = KEYC_MOUSEDOWN3_STATUS_RIGHT; 1292 if (where == STATUS_DEFAULT) 1293 key = KEYC_MOUSEDOWN3_STATUS_DEFAULT; 1294 if (where == BORDER) 1295 key = KEYC_MOUSEDOWN3_BORDER; 1296 break; 1297 case MOUSE_BUTTON_6: 1298 if (where == PANE) 1299 key = KEYC_MOUSEDOWN6_PANE; 1300 if (where == STATUS) 1301 key = KEYC_MOUSEDOWN6_STATUS; 1302 if (where == STATUS_LEFT) 1303 key = KEYC_MOUSEDOWN6_STATUS_LEFT; 1304 if (where == STATUS_RIGHT) 1305 key = KEYC_MOUSEDOWN6_STATUS_RIGHT; 1306 if (where == STATUS_DEFAULT) 1307 key = KEYC_MOUSEDOWN6_STATUS_DEFAULT; 1308 if (where == BORDER) 1309 key = KEYC_MOUSEDOWN6_BORDER; 1310 break; 1311 case MOUSE_BUTTON_7: 1312 if (where == PANE) 1313 key = KEYC_MOUSEDOWN7_PANE; 1314 if (where == STATUS) 1315 key = KEYC_MOUSEDOWN7_STATUS; 1316 if (where == STATUS_LEFT) 1317 key = KEYC_MOUSEDOWN7_STATUS_LEFT; 1318 if (where == STATUS_RIGHT) 1319 key = KEYC_MOUSEDOWN7_STATUS_RIGHT; 1320 if (where == STATUS_DEFAULT) 1321 key = KEYC_MOUSEDOWN7_STATUS_DEFAULT; 1322 if (where == BORDER) 1323 key = KEYC_MOUSEDOWN7_BORDER; 1324 break; 1325 case MOUSE_BUTTON_8: 1326 if (where == PANE) 1327 key = KEYC_MOUSEDOWN8_PANE; 1328 if (where == STATUS) 1329 key = KEYC_MOUSEDOWN8_STATUS; 1330 if (where == STATUS_LEFT) 1331 key = KEYC_MOUSEDOWN8_STATUS_LEFT; 1332 if (where == STATUS_RIGHT) 1333 key = KEYC_MOUSEDOWN8_STATUS_RIGHT; 1334 if (where == STATUS_DEFAULT) 1335 key = KEYC_MOUSEDOWN8_STATUS_DEFAULT; 1336 if (where == BORDER) 1337 key = KEYC_MOUSEDOWN8_BORDER; 1338 break; 1339 case MOUSE_BUTTON_9: 1340 if (where == PANE) 1341 key = KEYC_MOUSEDOWN9_PANE; 1342 if (where == STATUS) 1343 key = KEYC_MOUSEDOWN9_STATUS; 1344 if (where == STATUS_LEFT) 1345 key = KEYC_MOUSEDOWN9_STATUS_LEFT; 1346 if (where == STATUS_RIGHT) 1347 key = KEYC_MOUSEDOWN9_STATUS_RIGHT; 1348 if (where == STATUS_DEFAULT) 1349 key = KEYC_MOUSEDOWN9_STATUS_DEFAULT; 1350 if (where == BORDER) 1351 key = KEYC_MOUSEDOWN9_BORDER; 1352 break; 1353 case MOUSE_BUTTON_10: 1354 if (where == PANE) 1355 key = KEYC_MOUSEDOWN10_PANE; 1356 if (where == STATUS) 1357 key = KEYC_MOUSEDOWN10_STATUS; 1358 if (where == STATUS_LEFT) 1359 key = KEYC_MOUSEDOWN10_STATUS_LEFT; 1360 if (where == STATUS_RIGHT) 1361 key = KEYC_MOUSEDOWN10_STATUS_RIGHT; 1362 if (where == STATUS_DEFAULT) 1363 key = KEYC_MOUSEDOWN10_STATUS_DEFAULT; 1364 if (where == BORDER) 1365 key = KEYC_MOUSEDOWN10_BORDER; 1366 break; 1367 case MOUSE_BUTTON_11: 1368 if (where == PANE) 1369 key = KEYC_MOUSEDOWN11_PANE; 1370 if (where == STATUS) 1371 key = KEYC_MOUSEDOWN11_STATUS; 1372 if (where == STATUS_LEFT) 1373 key = KEYC_MOUSEDOWN11_STATUS_LEFT; 1374 if (where == STATUS_RIGHT) 1375 key = KEYC_MOUSEDOWN11_STATUS_RIGHT; 1376 if (where == STATUS_DEFAULT) 1377 key = KEYC_MOUSEDOWN11_STATUS_DEFAULT; 1378 if (where == BORDER) 1379 key = KEYC_MOUSEDOWN11_BORDER; 1380 break; 1381 } 1382 break; 1383 case SECOND: 1384 switch (MOUSE_BUTTONS(b)) { 1385 case MOUSE_BUTTON_1: 1386 if (where == PANE) 1387 key = KEYC_SECONDCLICK1_PANE; 1388 if (where == STATUS) 1389 key = KEYC_SECONDCLICK1_STATUS; 1390 if (where == STATUS_LEFT) 1391 key = KEYC_SECONDCLICK1_STATUS_LEFT; 1392 if (where == STATUS_RIGHT) 1393 key = KEYC_SECONDCLICK1_STATUS_RIGHT; 1394 if (where == STATUS_DEFAULT) 1395 key = KEYC_SECONDCLICK1_STATUS_DEFAULT; 1396 if (where == BORDER) 1397 key = KEYC_SECONDCLICK1_BORDER; 1398 break; 1399 case MOUSE_BUTTON_2: 1400 if (where == PANE) 1401 key = KEYC_SECONDCLICK2_PANE; 1402 if (where == STATUS) 1403 key = KEYC_SECONDCLICK2_STATUS; 1404 if (where == STATUS_LEFT) 1405 key = KEYC_SECONDCLICK2_STATUS_LEFT; 1406 if (where == STATUS_RIGHT) 1407 key = KEYC_SECONDCLICK2_STATUS_RIGHT; 1408 if (where == STATUS_DEFAULT) 1409 key = KEYC_SECONDCLICK2_STATUS_DEFAULT; 1410 if (where == BORDER) 1411 key = KEYC_SECONDCLICK2_BORDER; 1412 break; 1413 case MOUSE_BUTTON_3: 1414 if (where == PANE) 1415 key = KEYC_SECONDCLICK3_PANE; 1416 if (where == STATUS) 1417 key = KEYC_SECONDCLICK3_STATUS; 1418 if (where == STATUS_LEFT) 1419 key = KEYC_SECONDCLICK3_STATUS_LEFT; 1420 if (where == STATUS_RIGHT) 1421 key = KEYC_SECONDCLICK3_STATUS_RIGHT; 1422 if (where == STATUS_DEFAULT) 1423 key = KEYC_SECONDCLICK3_STATUS_DEFAULT; 1424 if (where == BORDER) 1425 key = KEYC_SECONDCLICK3_BORDER; 1426 break; 1427 case MOUSE_BUTTON_6: 1428 if (where == PANE) 1429 key = KEYC_SECONDCLICK6_PANE; 1430 if (where == STATUS) 1431 key = KEYC_SECONDCLICK6_STATUS; 1432 if (where == STATUS_LEFT) 1433 key = KEYC_SECONDCLICK6_STATUS_LEFT; 1434 if (where == STATUS_RIGHT) 1435 key = KEYC_SECONDCLICK6_STATUS_RIGHT; 1436 if (where == STATUS_DEFAULT) 1437 key = KEYC_SECONDCLICK6_STATUS_DEFAULT; 1438 if (where == BORDER) 1439 key = KEYC_SECONDCLICK6_BORDER; 1440 break; 1441 case MOUSE_BUTTON_7: 1442 if (where == PANE) 1443 key = KEYC_SECONDCLICK7_PANE; 1444 if (where == STATUS) 1445 key = KEYC_SECONDCLICK7_STATUS; 1446 if (where == STATUS_LEFT) 1447 key = KEYC_SECONDCLICK7_STATUS_LEFT; 1448 if (where == STATUS_RIGHT) 1449 key = KEYC_SECONDCLICK7_STATUS_RIGHT; 1450 if (where == STATUS_DEFAULT) 1451 key = KEYC_SECONDCLICK7_STATUS_DEFAULT; 1452 if (where == BORDER) 1453 key = KEYC_SECONDCLICK7_BORDER; 1454 break; 1455 case MOUSE_BUTTON_8: 1456 if (where == PANE) 1457 key = KEYC_SECONDCLICK8_PANE; 1458 if (where == STATUS) 1459 key = KEYC_SECONDCLICK8_STATUS; 1460 if (where == STATUS_LEFT) 1461 key = KEYC_SECONDCLICK8_STATUS_LEFT; 1462 if (where == STATUS_RIGHT) 1463 key = KEYC_SECONDCLICK8_STATUS_RIGHT; 1464 if (where == STATUS_DEFAULT) 1465 key = KEYC_SECONDCLICK8_STATUS_DEFAULT; 1466 if (where == BORDER) 1467 key = KEYC_SECONDCLICK8_BORDER; 1468 break; 1469 case MOUSE_BUTTON_9: 1470 if (where == PANE) 1471 key = KEYC_SECONDCLICK9_PANE; 1472 if (where == STATUS) 1473 key = KEYC_SECONDCLICK9_STATUS; 1474 if (where == STATUS_LEFT) 1475 key = KEYC_SECONDCLICK9_STATUS_LEFT; 1476 if (where == STATUS_RIGHT) 1477 key = KEYC_SECONDCLICK9_STATUS_RIGHT; 1478 if (where == STATUS_DEFAULT) 1479 key = KEYC_SECONDCLICK9_STATUS_DEFAULT; 1480 if (where == BORDER) 1481 key = KEYC_SECONDCLICK9_BORDER; 1482 break; 1483 case MOUSE_BUTTON_10: 1484 if (where == PANE) 1485 key = KEYC_SECONDCLICK10_PANE; 1486 if (where == STATUS) 1487 key = KEYC_SECONDCLICK10_STATUS; 1488 if (where == STATUS_LEFT) 1489 key = KEYC_SECONDCLICK10_STATUS_LEFT; 1490 if (where == STATUS_RIGHT) 1491 key = KEYC_SECONDCLICK10_STATUS_RIGHT; 1492 if (where == STATUS_DEFAULT) 1493 key = KEYC_SECONDCLICK10_STATUS_DEFAULT; 1494 if (where == BORDER) 1495 key = KEYC_SECONDCLICK10_BORDER; 1496 break; 1497 case MOUSE_BUTTON_11: 1498 if (where == PANE) 1499 key = KEYC_SECONDCLICK11_PANE; 1500 if (where == STATUS) 1501 key = KEYC_SECONDCLICK11_STATUS; 1502 if (where == STATUS_LEFT) 1503 key = KEYC_SECONDCLICK11_STATUS_LEFT; 1504 if (where == STATUS_RIGHT) 1505 key = KEYC_SECONDCLICK11_STATUS_RIGHT; 1506 if (where == STATUS_DEFAULT) 1507 key = KEYC_SECONDCLICK11_STATUS_DEFAULT; 1508 if (where == BORDER) 1509 key = KEYC_SECONDCLICK11_BORDER; 1510 break; 1511 } 1512 break; 1513 case DOUBLE: 1514 switch (MOUSE_BUTTONS(b)) { 1515 case MOUSE_BUTTON_1: 1516 if (where == PANE) 1517 key = KEYC_DOUBLECLICK1_PANE; 1518 if (where == STATUS) 1519 key = KEYC_DOUBLECLICK1_STATUS; 1520 if (where == STATUS_LEFT) 1521 key = KEYC_DOUBLECLICK1_STATUS_LEFT; 1522 if (where == STATUS_RIGHT) 1523 key = KEYC_DOUBLECLICK1_STATUS_RIGHT; 1524 if (where == STATUS_DEFAULT) 1525 key = KEYC_DOUBLECLICK1_STATUS_DEFAULT; 1526 if (where == BORDER) 1527 key = KEYC_DOUBLECLICK1_BORDER; 1528 break; 1529 case MOUSE_BUTTON_2: 1530 if (where == PANE) 1531 key = KEYC_DOUBLECLICK2_PANE; 1532 if (where == STATUS) 1533 key = KEYC_DOUBLECLICK2_STATUS; 1534 if (where == STATUS_LEFT) 1535 key = KEYC_DOUBLECLICK2_STATUS_LEFT; 1536 if (where == STATUS_RIGHT) 1537 key = KEYC_DOUBLECLICK2_STATUS_RIGHT; 1538 if (where == STATUS_DEFAULT) 1539 key = KEYC_DOUBLECLICK2_STATUS_DEFAULT; 1540 if (where == BORDER) 1541 key = KEYC_DOUBLECLICK2_BORDER; 1542 break; 1543 case MOUSE_BUTTON_3: 1544 if (where == PANE) 1545 key = KEYC_DOUBLECLICK3_PANE; 1546 if (where == STATUS) 1547 key = KEYC_DOUBLECLICK3_STATUS; 1548 if (where == STATUS_LEFT) 1549 key = KEYC_DOUBLECLICK3_STATUS_LEFT; 1550 if (where == STATUS_RIGHT) 1551 key = KEYC_DOUBLECLICK3_STATUS_RIGHT; 1552 if (where == STATUS_DEFAULT) 1553 key = KEYC_DOUBLECLICK3_STATUS_DEFAULT; 1554 if (where == BORDER) 1555 key = KEYC_DOUBLECLICK3_BORDER; 1556 break; 1557 case MOUSE_BUTTON_6: 1558 if (where == PANE) 1559 key = KEYC_DOUBLECLICK6_PANE; 1560 if (where == STATUS) 1561 key = KEYC_DOUBLECLICK6_STATUS; 1562 if (where == STATUS_LEFT) 1563 key = KEYC_DOUBLECLICK6_STATUS_LEFT; 1564 if (where == STATUS_RIGHT) 1565 key = KEYC_DOUBLECLICK6_STATUS_RIGHT; 1566 if (where == STATUS_DEFAULT) 1567 key = KEYC_DOUBLECLICK6_STATUS_DEFAULT; 1568 if (where == BORDER) 1569 key = KEYC_DOUBLECLICK6_BORDER; 1570 break; 1571 case MOUSE_BUTTON_7: 1572 if (where == PANE) 1573 key = KEYC_DOUBLECLICK7_PANE; 1574 if (where == STATUS) 1575 key = KEYC_DOUBLECLICK7_STATUS; 1576 if (where == STATUS_LEFT) 1577 key = KEYC_DOUBLECLICK7_STATUS_LEFT; 1578 if (where == STATUS_RIGHT) 1579 key = KEYC_DOUBLECLICK7_STATUS_RIGHT; 1580 if (where == STATUS_DEFAULT) 1581 key = KEYC_DOUBLECLICK7_STATUS_DEFAULT; 1582 if (where == BORDER) 1583 key = KEYC_DOUBLECLICK7_BORDER; 1584 break; 1585 case MOUSE_BUTTON_8: 1586 if (where == PANE) 1587 key = KEYC_DOUBLECLICK8_PANE; 1588 if (where == STATUS) 1589 key = KEYC_DOUBLECLICK8_STATUS; 1590 if (where == STATUS_LEFT) 1591 key = KEYC_DOUBLECLICK8_STATUS_LEFT; 1592 if (where == STATUS_RIGHT) 1593 key = KEYC_DOUBLECLICK8_STATUS_RIGHT; 1594 if (where == STATUS_DEFAULT) 1595 key = KEYC_DOUBLECLICK8_STATUS_DEFAULT; 1596 if (where == BORDER) 1597 key = KEYC_DOUBLECLICK8_BORDER; 1598 break; 1599 case MOUSE_BUTTON_9: 1600 if (where == PANE) 1601 key = KEYC_DOUBLECLICK9_PANE; 1602 if (where == STATUS) 1603 key = KEYC_DOUBLECLICK9_STATUS; 1604 if (where == STATUS_LEFT) 1605 key = KEYC_DOUBLECLICK9_STATUS_LEFT; 1606 if (where == STATUS_RIGHT) 1607 key = KEYC_DOUBLECLICK9_STATUS_RIGHT; 1608 if (where == STATUS_DEFAULT) 1609 key = KEYC_DOUBLECLICK9_STATUS_DEFAULT; 1610 if (where == BORDER) 1611 key = KEYC_DOUBLECLICK9_BORDER; 1612 break; 1613 case MOUSE_BUTTON_10: 1614 if (where == PANE) 1615 key = KEYC_DOUBLECLICK10_PANE; 1616 if (where == STATUS) 1617 key = KEYC_DOUBLECLICK10_STATUS; 1618 if (where == STATUS_LEFT) 1619 key = KEYC_DOUBLECLICK10_STATUS_LEFT; 1620 if (where == STATUS_RIGHT) 1621 key = KEYC_DOUBLECLICK10_STATUS_RIGHT; 1622 if (where == STATUS_DEFAULT) 1623 key = KEYC_DOUBLECLICK10_STATUS_DEFAULT; 1624 if (where == BORDER) 1625 key = KEYC_DOUBLECLICK10_BORDER; 1626 break; 1627 case MOUSE_BUTTON_11: 1628 if (where == PANE) 1629 key = KEYC_DOUBLECLICK11_PANE; 1630 if (where == STATUS) 1631 key = KEYC_DOUBLECLICK11_STATUS; 1632 if (where == STATUS_LEFT) 1633 key = KEYC_DOUBLECLICK11_STATUS_LEFT; 1634 if (where == STATUS_RIGHT) 1635 key = KEYC_DOUBLECLICK11_STATUS_RIGHT; 1636 if (where == STATUS_DEFAULT) 1637 key = KEYC_DOUBLECLICK11_STATUS_DEFAULT; 1638 if (where == BORDER) 1639 key = KEYC_DOUBLECLICK11_BORDER; 1640 break; 1641 } 1642 break; 1643 case TRIPLE: 1644 switch (MOUSE_BUTTONS(b)) { 1645 case MOUSE_BUTTON_1: 1646 if (where == PANE) 1647 key = KEYC_TRIPLECLICK1_PANE; 1648 if (where == STATUS) 1649 key = KEYC_TRIPLECLICK1_STATUS; 1650 if (where == STATUS_LEFT) 1651 key = KEYC_TRIPLECLICK1_STATUS_LEFT; 1652 if (where == STATUS_RIGHT) 1653 key = KEYC_TRIPLECLICK1_STATUS_RIGHT; 1654 if (where == STATUS_DEFAULT) 1655 key = KEYC_TRIPLECLICK1_STATUS_DEFAULT; 1656 if (where == BORDER) 1657 key = KEYC_TRIPLECLICK1_BORDER; 1658 break; 1659 case MOUSE_BUTTON_2: 1660 if (where == PANE) 1661 key = KEYC_TRIPLECLICK2_PANE; 1662 if (where == STATUS) 1663 key = KEYC_TRIPLECLICK2_STATUS; 1664 if (where == STATUS_LEFT) 1665 key = KEYC_TRIPLECLICK2_STATUS_LEFT; 1666 if (where == STATUS_RIGHT) 1667 key = KEYC_TRIPLECLICK2_STATUS_RIGHT; 1668 if (where == STATUS_DEFAULT) 1669 key = KEYC_TRIPLECLICK2_STATUS_DEFAULT; 1670 if (where == BORDER) 1671 key = KEYC_TRIPLECLICK2_BORDER; 1672 break; 1673 case MOUSE_BUTTON_3: 1674 if (where == PANE) 1675 key = KEYC_TRIPLECLICK3_PANE; 1676 if (where == STATUS) 1677 key = KEYC_TRIPLECLICK3_STATUS; 1678 if (where == STATUS_LEFT) 1679 key = KEYC_TRIPLECLICK3_STATUS_LEFT; 1680 if (where == STATUS_RIGHT) 1681 key = KEYC_TRIPLECLICK3_STATUS_RIGHT; 1682 if (where == STATUS_DEFAULT) 1683 key = KEYC_TRIPLECLICK3_STATUS_DEFAULT; 1684 if (where == BORDER) 1685 key = KEYC_TRIPLECLICK3_BORDER; 1686 break; 1687 case MOUSE_BUTTON_6: 1688 if (where == PANE) 1689 key = KEYC_TRIPLECLICK6_PANE; 1690 if (where == STATUS) 1691 key = KEYC_TRIPLECLICK6_STATUS; 1692 if (where == STATUS_LEFT) 1693 key = KEYC_TRIPLECLICK6_STATUS_LEFT; 1694 if (where == STATUS_RIGHT) 1695 key = KEYC_TRIPLECLICK6_STATUS_RIGHT; 1696 if (where == STATUS_DEFAULT) 1697 key = KEYC_TRIPLECLICK6_STATUS_DEFAULT; 1698 if (where == BORDER) 1699 key = KEYC_TRIPLECLICK6_BORDER; 1700 break; 1701 case MOUSE_BUTTON_7: 1702 if (where == PANE) 1703 key = KEYC_TRIPLECLICK7_PANE; 1704 if (where == STATUS) 1705 key = KEYC_TRIPLECLICK7_STATUS; 1706 if (where == STATUS_LEFT) 1707 key = KEYC_TRIPLECLICK7_STATUS_LEFT; 1708 if (where == STATUS_RIGHT) 1709 key = KEYC_TRIPLECLICK7_STATUS_RIGHT; 1710 if (where == STATUS_DEFAULT) 1711 key = KEYC_TRIPLECLICK7_STATUS_DEFAULT; 1712 if (where == BORDER) 1713 key = KEYC_TRIPLECLICK7_BORDER; 1714 break; 1715 case MOUSE_BUTTON_8: 1716 if (where == PANE) 1717 key = KEYC_TRIPLECLICK8_PANE; 1718 if (where == STATUS) 1719 key = KEYC_TRIPLECLICK8_STATUS; 1720 if (where == STATUS_LEFT) 1721 key = KEYC_TRIPLECLICK8_STATUS_LEFT; 1722 if (where == STATUS_RIGHT) 1723 key = KEYC_TRIPLECLICK8_STATUS_RIGHT; 1724 if (where == STATUS_DEFAULT) 1725 key = KEYC_TRIPLECLICK8_STATUS_DEFAULT; 1726 if (where == BORDER) 1727 key = KEYC_TRIPLECLICK8_BORDER; 1728 break; 1729 case MOUSE_BUTTON_9: 1730 if (where == PANE) 1731 key = KEYC_TRIPLECLICK9_PANE; 1732 if (where == STATUS) 1733 key = KEYC_TRIPLECLICK9_STATUS; 1734 if (where == STATUS_LEFT) 1735 key = KEYC_TRIPLECLICK9_STATUS_LEFT; 1736 if (where == STATUS_RIGHT) 1737 key = KEYC_TRIPLECLICK9_STATUS_RIGHT; 1738 if (where == STATUS_DEFAULT) 1739 key = KEYC_TRIPLECLICK9_STATUS_DEFAULT; 1740 if (where == BORDER) 1741 key = KEYC_TRIPLECLICK9_BORDER; 1742 break; 1743 case MOUSE_BUTTON_10: 1744 if (where == PANE) 1745 key = KEYC_TRIPLECLICK10_PANE; 1746 if (where == STATUS) 1747 key = KEYC_TRIPLECLICK10_STATUS; 1748 if (where == STATUS_LEFT) 1749 key = KEYC_TRIPLECLICK10_STATUS_LEFT; 1750 if (where == STATUS_RIGHT) 1751 key = KEYC_TRIPLECLICK10_STATUS_RIGHT; 1752 if (where == STATUS_DEFAULT) 1753 key = KEYC_TRIPLECLICK10_STATUS_DEFAULT; 1754 if (where == BORDER) 1755 key = KEYC_TRIPLECLICK10_BORDER; 1756 break; 1757 case MOUSE_BUTTON_11: 1758 if (where == PANE) 1759 key = KEYC_TRIPLECLICK11_PANE; 1760 if (where == STATUS) 1761 key = KEYC_TRIPLECLICK11_STATUS; 1762 if (where == STATUS_LEFT) 1763 key = KEYC_TRIPLECLICK11_STATUS_LEFT; 1764 if (where == STATUS_RIGHT) 1765 key = KEYC_TRIPLECLICK11_STATUS_RIGHT; 1766 if (where == STATUS_DEFAULT) 1767 key = KEYC_TRIPLECLICK11_STATUS_DEFAULT; 1768 if (where == BORDER) 1769 key = KEYC_TRIPLECLICK11_BORDER; 1770 break; 1771 } 1772 break; 1773 } 1774 if (key == KEYC_UNKNOWN) 1775 return (KEYC_UNKNOWN); 1776 1777 out: 1778 /* Apply modifiers if any. */ 1779 if (b & MOUSE_MASK_META) 1780 key |= KEYC_META; 1781 if (b & MOUSE_MASK_CTRL) 1782 key |= KEYC_CTRL; 1783 if (b & MOUSE_MASK_SHIFT) 1784 key |= KEYC_SHIFT; 1785 1786 if (log_get_level() != 0) 1787 log_debug("mouse key is %s", key_string_lookup_key (key, 1)); 1788 return (key); 1789 } 1790 1791 /* Is this a bracket paste key? */ 1792 static int 1793 server_client_is_bracket_pasting(struct client *c, key_code key) 1794 { 1795 if (key == KEYC_PASTE_START) { 1796 c->flags |= CLIENT_BRACKETPASTING; 1797 log_debug("%s: bracket paste on", c->name); 1798 return (1); 1799 } 1800 1801 if (key == KEYC_PASTE_END) { 1802 c->flags &= ~CLIENT_BRACKETPASTING; 1803 log_debug("%s: bracket paste off", c->name); 1804 return (1); 1805 } 1806 1807 return !!(c->flags & CLIENT_BRACKETPASTING); 1808 } 1809 1810 /* Is this fast enough to probably be a paste? */ 1811 static int 1812 server_client_assume_paste(struct session *s) 1813 { 1814 struct timeval tv; 1815 int t; 1816 1817 if ((t = options_get_number(s->options, "assume-paste-time")) == 0) 1818 return (0); 1819 1820 timersub(&s->activity_time, &s->last_activity_time, &tv); 1821 if (tv.tv_sec == 0 && tv.tv_usec < t * 1000) { 1822 log_debug("session %s pasting (flag %d)", s->name, 1823 !!(s->flags & SESSION_PASTING)); 1824 if (s->flags & SESSION_PASTING) 1825 return (1); 1826 s->flags |= SESSION_PASTING; 1827 return (0); 1828 } 1829 log_debug("session %s not pasting", s->name); 1830 s->flags &= ~SESSION_PASTING; 1831 return (0); 1832 } 1833 1834 /* Has the latest client changed? */ 1835 static void 1836 server_client_update_latest(struct client *c) 1837 { 1838 struct window *w; 1839 1840 if (c->session == NULL) 1841 return; 1842 w = c->session->curw->window; 1843 1844 if (w->latest == c) 1845 return; 1846 w->latest = c; 1847 1848 if (options_get_number(w->options, "window-size") == WINDOW_SIZE_LATEST) 1849 recalculate_size(w, 0); 1850 1851 notify_client("client-active", c); 1852 } 1853 1854 /* 1855 * Handle data key input from client. This owns and can modify the key event it 1856 * is given and is responsible for freeing it. 1857 */ 1858 static enum cmd_retval 1859 server_client_key_callback(struct cmdq_item *item, void *data) 1860 { 1861 struct client *c = cmdq_get_client(item); 1862 struct key_event *event = data; 1863 key_code key = event->key; 1864 struct mouse_event *m = &event->m; 1865 struct session *s = c->session; 1866 struct winlink *wl; 1867 struct window_pane *wp; 1868 struct window_mode_entry *wme; 1869 struct timeval tv; 1870 struct key_table *table, *first; 1871 struct key_binding *bd; 1872 int xtimeout; 1873 uint64_t flags; 1874 struct cmd_find_state fs; 1875 key_code key0, prefix, prefix2; 1876 1877 /* Check the client is good to accept input. */ 1878 if (s == NULL || (c->flags & CLIENT_UNATTACHEDFLAGS)) 1879 goto out; 1880 wl = s->curw; 1881 1882 /* Update the activity timer. */ 1883 if (gettimeofday(&c->activity_time, NULL) != 0) 1884 fatal("gettimeofday failed"); 1885 session_update_activity(s, &c->activity_time); 1886 1887 /* Check for mouse keys. */ 1888 m->valid = 0; 1889 if (key == KEYC_MOUSE || key == KEYC_DOUBLECLICK) { 1890 if (c->flags & CLIENT_READONLY) 1891 goto out; 1892 key = server_client_check_mouse(c, event); 1893 if (key == KEYC_UNKNOWN) 1894 goto out; 1895 1896 m->valid = 1; 1897 m->key = key; 1898 1899 /* 1900 * Mouse drag is in progress, so fire the callback (now that 1901 * the mouse event is valid). 1902 */ 1903 if ((key & KEYC_MASK_KEY) == KEYC_DRAGGING) { 1904 c->tty.mouse_drag_update(c, m); 1905 goto out; 1906 } 1907 event->key = key; 1908 } 1909 1910 /* Find affected pane. */ 1911 if (!KEYC_IS_MOUSE(key) || cmd_find_from_mouse(&fs, m, 0) != 0) 1912 cmd_find_from_client(&fs, c, 0); 1913 wp = fs.wp; 1914 1915 /* Forward mouse keys if disabled. */ 1916 if (KEYC_IS_MOUSE(key) && !options_get_number(s->options, "mouse")) 1917 goto forward_key; 1918 1919 /* Forward if bracket pasting. */ 1920 if (server_client_is_bracket_pasting(c, key)) 1921 goto forward_key; 1922 1923 /* Treat everything as a regular key when pasting is detected. */ 1924 if (!KEYC_IS_MOUSE(key) && 1925 (~key & KEYC_SENT) && 1926 server_client_assume_paste(s)) 1927 goto forward_key; 1928 1929 /* 1930 * Work out the current key table. If the pane is in a mode, use 1931 * the mode table instead of the default key table. 1932 */ 1933 if (server_client_is_default_key_table(c, c->keytable) && 1934 wp != NULL && 1935 (wme = TAILQ_FIRST(&wp->modes)) != NULL && 1936 wme->mode->key_table != NULL) 1937 table = key_bindings_get_table(wme->mode->key_table(wme), 1); 1938 else 1939 table = c->keytable; 1940 first = table; 1941 1942 table_changed: 1943 /* 1944 * The prefix always takes precedence and forces a switch to the prefix 1945 * table, unless we are already there. 1946 */ 1947 prefix = (key_code)options_get_number(s->options, "prefix"); 1948 prefix2 = (key_code)options_get_number(s->options, "prefix2"); 1949 key0 = (key & (KEYC_MASK_KEY|KEYC_MASK_MODIFIERS)); 1950 if ((key0 == (prefix & (KEYC_MASK_KEY|KEYC_MASK_MODIFIERS)) || 1951 key0 == (prefix2 & (KEYC_MASK_KEY|KEYC_MASK_MODIFIERS))) && 1952 strcmp(table->name, "prefix") != 0) { 1953 server_client_set_key_table(c, "prefix"); 1954 server_status_client(c); 1955 goto out; 1956 } 1957 flags = c->flags; 1958 1959 try_again: 1960 /* Log key table. */ 1961 if (wp == NULL) 1962 log_debug("key table %s (no pane)", table->name); 1963 else 1964 log_debug("key table %s (pane %%%u)", table->name, wp->id); 1965 if (c->flags & CLIENT_REPEAT) 1966 log_debug("currently repeating"); 1967 1968 /* Try to see if there is a key binding in the current table. */ 1969 bd = key_bindings_get(table, key0); 1970 if (bd != NULL) { 1971 /* 1972 * Key was matched in this table. If currently repeating but a 1973 * non-repeating binding was found, stop repeating and try 1974 * again in the root table. 1975 */ 1976 if ((c->flags & CLIENT_REPEAT) && 1977 (~bd->flags & KEY_BINDING_REPEAT)) { 1978 log_debug("found in key table %s (not repeating)", 1979 table->name); 1980 server_client_set_key_table(c, NULL); 1981 first = table = c->keytable; 1982 c->flags &= ~CLIENT_REPEAT; 1983 server_status_client(c); 1984 goto table_changed; 1985 } 1986 log_debug("found in key table %s", table->name); 1987 1988 /* 1989 * Take a reference to this table to make sure the key binding 1990 * doesn't disappear. 1991 */ 1992 table->references++; 1993 1994 /* 1995 * If this is a repeating key, start the timer. Otherwise reset 1996 * the client back to the root table. 1997 */ 1998 xtimeout = options_get_number(s->options, "repeat-time"); 1999 if (xtimeout != 0 && (bd->flags & KEY_BINDING_REPEAT)) { 2000 c->flags |= CLIENT_REPEAT; 2001 2002 tv.tv_sec = xtimeout / 1000; 2003 tv.tv_usec = (xtimeout % 1000) * 1000L; 2004 evtimer_del(&c->repeat_timer); 2005 evtimer_add(&c->repeat_timer, &tv); 2006 } else { 2007 c->flags &= ~CLIENT_REPEAT; 2008 server_client_set_key_table(c, NULL); 2009 } 2010 server_status_client(c); 2011 2012 /* Execute the key binding. */ 2013 key_bindings_dispatch(bd, item, c, event, &fs); 2014 key_bindings_unref_table(table); 2015 goto out; 2016 } 2017 2018 /* 2019 * No match, try the ANY key. 2020 */ 2021 if (key0 != KEYC_ANY) { 2022 key0 = KEYC_ANY; 2023 goto try_again; 2024 } 2025 2026 /* 2027 * No match in this table. If not in the root table or if repeating, 2028 * switch the client back to the root table and try again. 2029 */ 2030 log_debug("not found in key table %s", table->name); 2031 if (!server_client_is_default_key_table(c, table) || 2032 (c->flags & CLIENT_REPEAT)) { 2033 log_debug("trying in root table"); 2034 server_client_set_key_table(c, NULL); 2035 table = c->keytable; 2036 if (c->flags & CLIENT_REPEAT) 2037 first = table; 2038 c->flags &= ~CLIENT_REPEAT; 2039 server_status_client(c); 2040 goto table_changed; 2041 } 2042 2043 /* 2044 * No match in the root table either. If this wasn't the first table 2045 * tried, don't pass the key to the pane. 2046 */ 2047 if (first != table && (~flags & CLIENT_REPEAT)) { 2048 server_client_set_key_table(c, NULL); 2049 server_status_client(c); 2050 goto out; 2051 } 2052 2053 forward_key: 2054 if (c->flags & CLIENT_READONLY) 2055 goto out; 2056 if (wp != NULL) 2057 window_pane_key(wp, c, s, wl, key, m); 2058 2059 out: 2060 if (s != NULL && key != KEYC_FOCUS_OUT) 2061 server_client_update_latest(c); 2062 free(event); 2063 return (CMD_RETURN_NORMAL); 2064 } 2065 2066 /* Handle a key event. */ 2067 int 2068 server_client_handle_key(struct client *c, struct key_event *event) 2069 { 2070 struct session *s = c->session; 2071 struct cmdq_item *item; 2072 2073 /* Check the client is good to accept input. */ 2074 if (s == NULL || (c->flags & CLIENT_UNATTACHEDFLAGS)) 2075 return (0); 2076 2077 /* 2078 * Key presses in overlay mode and the command prompt are a special 2079 * case. The queue might be blocked so they need to be processed 2080 * immediately rather than queued. 2081 */ 2082 if (~c->flags & CLIENT_READONLY) { 2083 if (c->message_string != NULL) { 2084 if (c->message_ignore_keys) 2085 return (0); 2086 status_message_clear(c); 2087 } 2088 if (c->overlay_key != NULL) { 2089 switch (c->overlay_key(c, c->overlay_data, event)) { 2090 case 0: 2091 return (0); 2092 case 1: 2093 server_client_clear_overlay(c); 2094 return (0); 2095 } 2096 } 2097 server_client_clear_overlay(c); 2098 if (c->prompt_string != NULL) { 2099 if (status_prompt_key(c, event->key) == 0) 2100 return (0); 2101 } 2102 } 2103 2104 /* 2105 * Add the key to the queue so it happens after any commands queued by 2106 * previous keys. 2107 */ 2108 item = cmdq_get_callback(server_client_key_callback, event); 2109 cmdq_append(c, item); 2110 return (1); 2111 } 2112 2113 /* Client functions that need to happen every loop. */ 2114 void 2115 server_client_loop(void) 2116 { 2117 struct client *c; 2118 struct window *w; 2119 struct window_pane *wp; 2120 2121 /* Check for window resize. This is done before redrawing. */ 2122 RB_FOREACH(w, windows, &windows) 2123 server_client_check_window_resize(w); 2124 2125 /* Check clients. */ 2126 TAILQ_FOREACH(c, &clients, entry) { 2127 server_client_check_exit(c); 2128 if (c->session != NULL) { 2129 server_client_check_modes(c); 2130 server_client_check_redraw(c); 2131 server_client_reset_state(c); 2132 } 2133 } 2134 2135 /* 2136 * Any windows will have been redrawn as part of clients, so clear 2137 * their flags now. 2138 */ 2139 RB_FOREACH(w, windows, &windows) { 2140 TAILQ_FOREACH(wp, &w->panes, entry) { 2141 if (wp->fd != -1) { 2142 server_client_check_pane_resize(wp); 2143 server_client_check_pane_buffer(wp); 2144 } 2145 wp->flags &= ~PANE_REDRAW; 2146 } 2147 check_window_name(w); 2148 } 2149 } 2150 2151 /* Check if window needs to be resized. */ 2152 static void 2153 server_client_check_window_resize(struct window *w) 2154 { 2155 struct winlink *wl; 2156 2157 if (~w->flags & WINDOW_RESIZE) 2158 return; 2159 2160 TAILQ_FOREACH(wl, &w->winlinks, wentry) { 2161 if (wl->session->attached != 0 && wl->session->curw == wl) 2162 break; 2163 } 2164 if (wl == NULL) 2165 return; 2166 2167 log_debug("%s: resizing window @%u", __func__, w->id); 2168 resize_window(w, w->new_sx, w->new_sy, w->new_xpixel, w->new_ypixel); 2169 } 2170 2171 /* Resize timer event. */ 2172 static void 2173 server_client_resize_timer(__unused int fd, __unused short events, void *data) 2174 { 2175 struct window_pane *wp = data; 2176 2177 log_debug("%s: %%%u resize timer expired", __func__, wp->id); 2178 evtimer_del(&wp->resize_timer); 2179 } 2180 2181 /* Check if pane should be resized. */ 2182 static void 2183 server_client_check_pane_resize(struct window_pane *wp) 2184 { 2185 struct window_pane_resize *r; 2186 struct window_pane_resize *r1; 2187 struct window_pane_resize *first; 2188 struct window_pane_resize *last; 2189 struct timeval tv = { .tv_usec = 250000 }; 2190 2191 if (TAILQ_EMPTY(&wp->resize_queue)) 2192 return; 2193 2194 if (!event_initialized(&wp->resize_timer)) 2195 evtimer_set(&wp->resize_timer, server_client_resize_timer, wp); 2196 if (evtimer_pending(&wp->resize_timer, NULL)) 2197 return; 2198 2199 log_debug("%s: %%%u needs to be resized", __func__, wp->id); 2200 TAILQ_FOREACH(r, &wp->resize_queue, entry) { 2201 log_debug("queued resize: %ux%u -> %ux%u", r->osx, r->osy, 2202 r->sx, r->sy); 2203 } 2204 2205 /* 2206 * There are three cases that matter: 2207 * 2208 * - Only one resize. It can just be applied. 2209 * 2210 * - Multiple resizes and the ending size is different from the 2211 * starting size. We can discard all resizes except the most recent. 2212 * 2213 * - Multiple resizes and the ending size is the same as the starting 2214 * size. We must resize at least twice to force the application to 2215 * redraw. So apply the first and leave the last on the queue for 2216 * next time. 2217 */ 2218 first = TAILQ_FIRST(&wp->resize_queue); 2219 last = TAILQ_LAST(&wp->resize_queue, window_pane_resizes); 2220 if (first == last) { 2221 /* Only one resize. */ 2222 window_pane_send_resize(wp, first->sx, first->sy); 2223 TAILQ_REMOVE(&wp->resize_queue, first, entry); 2224 free(first); 2225 } else if (last->sx != first->osx || last->sy != first->osy) { 2226 /* Multiple resizes ending up with a different size. */ 2227 window_pane_send_resize(wp, last->sx, last->sy); 2228 TAILQ_FOREACH_SAFE(r, &wp->resize_queue, entry, r1) { 2229 TAILQ_REMOVE(&wp->resize_queue, r, entry); 2230 free(r); 2231 } 2232 } else { 2233 /* 2234 * Multiple resizes ending up with the same size. There will 2235 * not be more than one to the same size in succession so we 2236 * can just use the last-but-one on the list and leave the last 2237 * for later. We reduce the time until the next check to avoid 2238 * a long delay between the resizes. 2239 */ 2240 r = TAILQ_PREV(last, window_pane_resizes, entry); 2241 window_pane_send_resize(wp, r->sx, r->sy); 2242 TAILQ_FOREACH_SAFE(r, &wp->resize_queue, entry, r1) { 2243 if (r == last) 2244 break; 2245 TAILQ_REMOVE(&wp->resize_queue, r, entry); 2246 free(r); 2247 } 2248 tv.tv_usec = 10000; 2249 } 2250 evtimer_add(&wp->resize_timer, &tv); 2251 } 2252 2253 /* Check pane buffer size. */ 2254 static void 2255 server_client_check_pane_buffer(struct window_pane *wp) 2256 { 2257 struct evbuffer *evb = wp->event->input; 2258 size_t minimum; 2259 struct client *c; 2260 struct window_pane_offset *wpo; 2261 int off = 1, flag; 2262 u_int attached_clients = 0; 2263 size_t new_size; 2264 2265 /* 2266 * Work out the minimum used size. This is the most that can be removed 2267 * from the buffer. 2268 */ 2269 minimum = wp->offset.used; 2270 if (wp->pipe_fd != -1 && wp->pipe_offset.used < minimum) 2271 minimum = wp->pipe_offset.used; 2272 TAILQ_FOREACH(c, &clients, entry) { 2273 if (c->session == NULL) 2274 continue; 2275 attached_clients++; 2276 2277 if (~c->flags & CLIENT_CONTROL) { 2278 off = 0; 2279 continue; 2280 } 2281 wpo = control_pane_offset(c, wp, &flag); 2282 if (wpo == NULL) { 2283 if (!flag) 2284 off = 0; 2285 continue; 2286 } 2287 if (!flag) 2288 off = 0; 2289 2290 window_pane_get_new_data(wp, wpo, &new_size); 2291 log_debug("%s: %s has %zu bytes used and %zu left for %%%u", 2292 __func__, c->name, wpo->used - wp->base_offset, new_size, 2293 wp->id); 2294 if (wpo->used < minimum) 2295 minimum = wpo->used; 2296 } 2297 if (attached_clients == 0) 2298 off = 0; 2299 minimum -= wp->base_offset; 2300 if (minimum == 0) 2301 goto out; 2302 2303 /* Drain the buffer. */ 2304 log_debug("%s: %%%u has %zu minimum (of %zu) bytes used", __func__, 2305 wp->id, minimum, EVBUFFER_LENGTH(evb)); 2306 evbuffer_drain(evb, minimum); 2307 2308 /* 2309 * Adjust the base offset. If it would roll over, all the offsets into 2310 * the buffer need to be adjusted. 2311 */ 2312 if (wp->base_offset > SIZE_MAX - minimum) { 2313 log_debug("%s: %%%u base offset has wrapped", __func__, wp->id); 2314 wp->offset.used -= wp->base_offset; 2315 if (wp->pipe_fd != -1) 2316 wp->pipe_offset.used -= wp->base_offset; 2317 TAILQ_FOREACH(c, &clients, entry) { 2318 if (c->session == NULL || (~c->flags & CLIENT_CONTROL)) 2319 continue; 2320 wpo = control_pane_offset(c, wp, &flag); 2321 if (wpo != NULL && !flag) 2322 wpo->used -= wp->base_offset; 2323 } 2324 wp->base_offset = minimum; 2325 } else 2326 wp->base_offset += minimum; 2327 2328 out: 2329 /* 2330 * If there is data remaining, and there are no clients able to consume 2331 * it, do not read any more. This is true when there are attached 2332 * clients, all of which are control clients which are not able to 2333 * accept any more data. 2334 */ 2335 log_debug("%s: pane %%%u is %s", __func__, wp->id, off ? "off" : "on"); 2336 if (off) 2337 bufferevent_disable(wp->event, EV_READ); 2338 else 2339 bufferevent_enable(wp->event, EV_READ); 2340 } 2341 2342 /* 2343 * Update cursor position and mode settings. The scroll region and attributes 2344 * are cleared when idle (waiting for an event) as this is the most likely time 2345 * a user may interrupt tmux, for example with ~^Z in ssh(1). This is a 2346 * compromise between excessive resets and likelihood of an interrupt. 2347 * 2348 * tty_region/tty_reset/tty_update_mode already take care of not resetting 2349 * things that are already in their default state. 2350 */ 2351 static void 2352 server_client_reset_state(struct client *c) 2353 { 2354 struct tty *tty = &c->tty; 2355 struct window *w = c->session->curw->window; 2356 struct window_pane *wp = server_client_get_pane(c), *loop; 2357 struct screen *s = NULL; 2358 struct options *oo = c->session->options; 2359 int mode = 0, cursor, flags, n; 2360 u_int cx = 0, cy = 0, ox, oy, sx, sy; 2361 2362 if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED)) 2363 return; 2364 2365 /* Disable the block flag. */ 2366 flags = (tty->flags & TTY_BLOCK); 2367 tty->flags &= ~TTY_BLOCK; 2368 2369 /* Get mode from overlay if any, else from screen. */ 2370 if (c->overlay_draw != NULL) { 2371 if (c->overlay_mode != NULL) 2372 s = c->overlay_mode(c, c->overlay_data, &cx, &cy); 2373 } else 2374 s = wp->screen; 2375 if (s != NULL) 2376 mode = s->mode; 2377 if (log_get_level() != 0) { 2378 log_debug("%s: client %s mode %s", __func__, c->name, 2379 screen_mode_to_string(mode)); 2380 } 2381 2382 /* Reset region and margin. */ 2383 tty_region_off(tty); 2384 tty_margin_off(tty); 2385 2386 /* Move cursor to pane cursor and offset. */ 2387 if (c->prompt_string != NULL) { 2388 n = options_get_number(c->session->options, "status-position"); 2389 if (n == 0) 2390 cy = 0; 2391 else { 2392 n = status_line_size(c); 2393 if (n == 0) 2394 cy = tty->sy - 1; 2395 else 2396 cy = tty->sy - n; 2397 } 2398 cx = c->prompt_cursor; 2399 mode &= ~MODE_CURSOR; 2400 } else if (c->overlay_draw == NULL) { 2401 cursor = 0; 2402 tty_window_offset(tty, &ox, &oy, &sx, &sy); 2403 if (wp->xoff + s->cx >= ox && wp->xoff + s->cx <= ox + sx && 2404 wp->yoff + s->cy >= oy && wp->yoff + s->cy <= oy + sy) { 2405 cursor = 1; 2406 2407 cx = wp->xoff + s->cx - ox; 2408 cy = wp->yoff + s->cy - oy; 2409 2410 if (status_at_line(c) == 0) 2411 cy += status_line_size(c); 2412 } 2413 if (!cursor) 2414 mode &= ~MODE_CURSOR; 2415 } 2416 log_debug("%s: cursor to %u,%u", __func__, cx, cy); 2417 tty_cursor(tty, cx, cy); 2418 2419 /* 2420 * Set mouse mode if requested. To support dragging, always use button 2421 * mode. 2422 */ 2423 if (options_get_number(oo, "mouse")) { 2424 if (c->overlay_draw == NULL) { 2425 mode &= ~ALL_MOUSE_MODES; 2426 TAILQ_FOREACH(loop, &w->panes, entry) { 2427 if (loop->screen->mode & MODE_MOUSE_ALL) 2428 mode |= MODE_MOUSE_ALL; 2429 } 2430 } 2431 if (~mode & MODE_MOUSE_ALL) 2432 mode |= MODE_MOUSE_BUTTON; 2433 } 2434 2435 /* Clear bracketed paste mode if at the prompt. */ 2436 if (c->overlay_draw == NULL && c->prompt_string != NULL) 2437 mode &= ~MODE_BRACKETPASTE; 2438 2439 /* Set the terminal mode and reset attributes. */ 2440 tty_update_mode(tty, mode, s); 2441 tty_reset(tty); 2442 2443 /* All writing must be done, send a sync end (if it was started). */ 2444 tty_sync_end(tty); 2445 tty->flags |= flags; 2446 } 2447 2448 /* Repeat time callback. */ 2449 static void 2450 server_client_repeat_timer(__unused int fd, __unused short events, void *data) 2451 { 2452 struct client *c = data; 2453 2454 if (c->flags & CLIENT_REPEAT) { 2455 server_client_set_key_table(c, NULL); 2456 c->flags &= ~CLIENT_REPEAT; 2457 server_status_client(c); 2458 } 2459 } 2460 2461 /* Double-click callback. */ 2462 static void 2463 server_client_click_timer(__unused int fd, __unused short events, void *data) 2464 { 2465 struct client *c = data; 2466 struct key_event *event; 2467 2468 log_debug("click timer expired"); 2469 2470 if (c->flags & CLIENT_TRIPLECLICK) { 2471 /* 2472 * Waiting for a third click that hasn't happened, so this must 2473 * have been a double click. 2474 */ 2475 event = xmalloc(sizeof *event); 2476 event->key = KEYC_DOUBLECLICK; 2477 memcpy(&event->m, &c->click_event, sizeof event->m); 2478 if (!server_client_handle_key(c, event)) 2479 free(event); 2480 } 2481 c->flags &= ~(CLIENT_DOUBLECLICK|CLIENT_TRIPLECLICK); 2482 } 2483 2484 /* Check if client should be exited. */ 2485 static void 2486 server_client_check_exit(struct client *c) 2487 { 2488 struct client_file *cf; 2489 const char *name = c->exit_session; 2490 char *data; 2491 size_t size, msize; 2492 2493 if (c->flags & (CLIENT_DEAD|CLIENT_EXITED)) 2494 return; 2495 if (~c->flags & CLIENT_EXIT) 2496 return; 2497 2498 if (c->flags & CLIENT_CONTROL) { 2499 control_discard(c); 2500 if (!control_all_done(c)) 2501 return; 2502 } 2503 RB_FOREACH(cf, client_files, &c->files) { 2504 if (EVBUFFER_LENGTH(cf->buffer) != 0) 2505 return; 2506 } 2507 c->flags |= CLIENT_EXITED; 2508 2509 switch (c->exit_type) { 2510 case CLIENT_EXIT_RETURN: 2511 if (c->exit_message != NULL) 2512 msize = strlen(c->exit_message) + 1; 2513 else 2514 msize = 0; 2515 size = (sizeof c->retval) + msize; 2516 data = xmalloc(size); 2517 memcpy(data, &c->retval, sizeof c->retval); 2518 if (c->exit_message != NULL) 2519 memcpy(data + sizeof c->retval, c->exit_message, msize); 2520 proc_send(c->peer, MSG_EXIT, -1, data, size); 2521 free(data); 2522 break; 2523 case CLIENT_EXIT_SHUTDOWN: 2524 proc_send(c->peer, MSG_SHUTDOWN, -1, NULL, 0); 2525 break; 2526 case CLIENT_EXIT_DETACH: 2527 proc_send(c->peer, c->exit_msgtype, -1, name, strlen(name) + 1); 2528 break; 2529 } 2530 free(c->exit_session); 2531 free(c->exit_message); 2532 } 2533 2534 /* Redraw timer callback. */ 2535 static void 2536 server_client_redraw_timer(__unused int fd, __unused short events, 2537 __unused void *data) 2538 { 2539 log_debug("redraw timer fired"); 2540 } 2541 2542 /* 2543 * Check if modes need to be updated. Only modes in the current window are 2544 * updated and it is done when the status line is redrawn. 2545 */ 2546 static void 2547 server_client_check_modes(struct client *c) 2548 { 2549 struct window *w = c->session->curw->window; 2550 struct window_pane *wp; 2551 struct window_mode_entry *wme; 2552 2553 if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED)) 2554 return; 2555 if (~c->flags & CLIENT_REDRAWSTATUS) 2556 return; 2557 TAILQ_FOREACH(wp, &w->panes, entry) { 2558 wme = TAILQ_FIRST(&wp->modes); 2559 if (wme != NULL && wme->mode->update != NULL) 2560 wme->mode->update(wme); 2561 } 2562 } 2563 2564 /* Check for client redraws. */ 2565 static void 2566 server_client_check_redraw(struct client *c) 2567 { 2568 struct session *s = c->session; 2569 struct tty *tty = &c->tty; 2570 struct window *w = c->session->curw->window; 2571 struct window_pane *wp; 2572 int needed, tty_flags, mode = tty->mode; 2573 uint64_t client_flags = 0; 2574 int redraw; 2575 u_int bit = 0; 2576 struct timeval tv = { .tv_usec = 1000 }; 2577 static struct event ev; 2578 size_t left; 2579 2580 if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED)) 2581 return; 2582 if (c->flags & CLIENT_ALLREDRAWFLAGS) { 2583 log_debug("%s: redraw%s%s%s%s%s", c->name, 2584 (c->flags & CLIENT_REDRAWWINDOW) ? " window" : "", 2585 (c->flags & CLIENT_REDRAWSTATUS) ? " status" : "", 2586 (c->flags & CLIENT_REDRAWBORDERS) ? " borders" : "", 2587 (c->flags & CLIENT_REDRAWOVERLAY) ? " overlay" : "", 2588 (c->flags & CLIENT_REDRAWPANES) ? " panes" : ""); 2589 } 2590 2591 /* 2592 * If there is outstanding data, defer the redraw until it has been 2593 * consumed. We can just add a timer to get out of the event loop and 2594 * end up back here. 2595 */ 2596 needed = 0; 2597 if (c->flags & CLIENT_ALLREDRAWFLAGS) 2598 needed = 1; 2599 else { 2600 TAILQ_FOREACH(wp, &w->panes, entry) { 2601 if (wp->flags & PANE_REDRAW) { 2602 needed = 1; 2603 break; 2604 } 2605 } 2606 if (needed) 2607 client_flags |= CLIENT_REDRAWPANES; 2608 } 2609 if (needed && (left = EVBUFFER_LENGTH(tty->out)) != 0) { 2610 log_debug("%s: redraw deferred (%zu left)", c->name, left); 2611 if (!evtimer_initialized(&ev)) 2612 evtimer_set(&ev, server_client_redraw_timer, NULL); 2613 if (!evtimer_pending(&ev, NULL)) { 2614 log_debug("redraw timer started"); 2615 evtimer_add(&ev, &tv); 2616 } 2617 2618 if (~c->flags & CLIENT_REDRAWWINDOW) { 2619 TAILQ_FOREACH(wp, &w->panes, entry) { 2620 if (wp->flags & PANE_REDRAW) { 2621 log_debug("%s: pane %%%u needs redraw", 2622 c->name, wp->id); 2623 c->redraw_panes |= (1 << bit); 2624 } 2625 if (++bit == 64) { 2626 /* 2627 * If more that 64 panes, give up and 2628 * just redraw the window. 2629 */ 2630 client_flags &= CLIENT_REDRAWPANES; 2631 client_flags |= CLIENT_REDRAWWINDOW; 2632 break; 2633 } 2634 } 2635 if (c->redraw_panes != 0) 2636 c->flags |= CLIENT_REDRAWPANES; 2637 } 2638 c->flags |= client_flags; 2639 return; 2640 } else if (needed) 2641 log_debug("%s: redraw needed", c->name); 2642 2643 tty_flags = tty->flags & (TTY_BLOCK|TTY_FREEZE|TTY_NOCURSOR); 2644 tty->flags = (tty->flags & ~(TTY_BLOCK|TTY_FREEZE))|TTY_NOCURSOR; 2645 2646 if (~c->flags & CLIENT_REDRAWWINDOW) { 2647 /* 2648 * If not redrawing the entire window, check whether each pane 2649 * needs to be redrawn. 2650 */ 2651 TAILQ_FOREACH(wp, &w->panes, entry) { 2652 redraw = 0; 2653 if (wp->flags & PANE_REDRAW) 2654 redraw = 1; 2655 else if (c->flags & CLIENT_REDRAWPANES) 2656 redraw = !!(c->redraw_panes & (1 << bit)); 2657 bit++; 2658 if (!redraw) 2659 continue; 2660 log_debug("%s: redrawing pane %%%u", __func__, wp->id); 2661 screen_redraw_pane(c, wp); 2662 } 2663 c->redraw_panes = 0; 2664 c->flags &= ~CLIENT_REDRAWPANES; 2665 } 2666 2667 if (c->flags & CLIENT_ALLREDRAWFLAGS) { 2668 if (options_get_number(s->options, "set-titles")) { 2669 server_client_set_title(c); 2670 server_client_set_path(c); 2671 } 2672 screen_redraw_screen(c); 2673 } 2674 2675 tty->flags = (tty->flags & ~TTY_NOCURSOR)|(tty_flags & TTY_NOCURSOR); 2676 tty_update_mode(tty, mode, NULL); 2677 tty->flags = (tty->flags & ~(TTY_BLOCK|TTY_FREEZE|TTY_NOCURSOR))| 2678 tty_flags; 2679 2680 c->flags &= ~(CLIENT_ALLREDRAWFLAGS|CLIENT_STATUSFORCE); 2681 2682 if (needed) { 2683 /* 2684 * We would have deferred the redraw unless the output buffer 2685 * was empty, so we can record how many bytes the redraw 2686 * generated. 2687 */ 2688 c->redraw = EVBUFFER_LENGTH(tty->out); 2689 log_debug("%s: redraw added %zu bytes", c->name, c->redraw); 2690 } 2691 } 2692 2693 /* Set client title. */ 2694 static void 2695 server_client_set_title(struct client *c) 2696 { 2697 struct session *s = c->session; 2698 const char *template; 2699 char *title; 2700 struct format_tree *ft; 2701 2702 template = options_get_string(s->options, "set-titles-string"); 2703 2704 ft = format_create(c, NULL, FORMAT_NONE, 0); 2705 format_defaults(ft, c, NULL, NULL, NULL); 2706 2707 title = format_expand_time(ft, template); 2708 if (c->title == NULL || strcmp(title, c->title) != 0) { 2709 free(c->title); 2710 c->title = xstrdup(title); 2711 tty_set_title(&c->tty, c->title); 2712 } 2713 free(title); 2714 2715 format_free(ft); 2716 } 2717 2718 /* Set client path. */ 2719 static void 2720 server_client_set_path(struct client *c) 2721 { 2722 struct session *s = c->session; 2723 const char *path; 2724 2725 if (s->curw == NULL) 2726 return; 2727 if (s->curw->window->active->base.path == NULL) 2728 path = ""; 2729 else 2730 path = s->curw->window->active->base.path; 2731 if (c->path == NULL || strcmp(path, c->path) != 0) { 2732 free(c->path); 2733 c->path = xstrdup(path); 2734 tty_set_path(&c->tty, c->path); 2735 } 2736 } 2737 2738 /* Dispatch message from client. */ 2739 static void 2740 server_client_dispatch(struct imsg *imsg, void *arg) 2741 { 2742 struct client *c = arg; 2743 ssize_t datalen; 2744 struct session *s; 2745 2746 if (c->flags & CLIENT_DEAD) 2747 return; 2748 2749 if (imsg == NULL) { 2750 server_client_lost(c); 2751 return; 2752 } 2753 2754 datalen = imsg->hdr.len - IMSG_HEADER_SIZE; 2755 2756 switch (imsg->hdr.type) { 2757 case MSG_IDENTIFY_CLIENTPID: 2758 case MSG_IDENTIFY_CWD: 2759 case MSG_IDENTIFY_ENVIRON: 2760 case MSG_IDENTIFY_FEATURES: 2761 case MSG_IDENTIFY_FLAGS: 2762 case MSG_IDENTIFY_LONGFLAGS: 2763 case MSG_IDENTIFY_STDIN: 2764 case MSG_IDENTIFY_STDOUT: 2765 case MSG_IDENTIFY_TERM: 2766 case MSG_IDENTIFY_TERMINFO: 2767 case MSG_IDENTIFY_TTYNAME: 2768 case MSG_IDENTIFY_DONE: 2769 server_client_dispatch_identify(c, imsg); 2770 break; 2771 case MSG_COMMAND: 2772 server_client_dispatch_command(c, imsg); 2773 break; 2774 case MSG_RESIZE: 2775 if (datalen != 0) 2776 fatalx("bad MSG_RESIZE size"); 2777 2778 if (c->flags & CLIENT_CONTROL) 2779 break; 2780 server_client_update_latest(c); 2781 tty_resize(&c->tty); 2782 tty_repeat_requests(&c->tty); 2783 recalculate_sizes(); 2784 if (c->overlay_resize == NULL) 2785 server_client_clear_overlay(c); 2786 else 2787 c->overlay_resize(c, c->overlay_data); 2788 server_redraw_client(c); 2789 if (c->session != NULL) 2790 notify_client("client-resized", c); 2791 break; 2792 case MSG_EXITING: 2793 if (datalen != 0) 2794 fatalx("bad MSG_EXITING size"); 2795 server_client_set_session(c, NULL); 2796 recalculate_sizes(); 2797 tty_close(&c->tty); 2798 proc_send(c->peer, MSG_EXITED, -1, NULL, 0); 2799 break; 2800 case MSG_WAKEUP: 2801 case MSG_UNLOCK: 2802 if (datalen != 0) 2803 fatalx("bad MSG_WAKEUP size"); 2804 2805 if (!(c->flags & CLIENT_SUSPENDED)) 2806 break; 2807 c->flags &= ~CLIENT_SUSPENDED; 2808 2809 if (c->fd == -1 || c->session == NULL) /* exited already */ 2810 break; 2811 s = c->session; 2812 2813 if (gettimeofday(&c->activity_time, NULL) != 0) 2814 fatal("gettimeofday failed"); 2815 2816 tty_start_tty(&c->tty); 2817 server_redraw_client(c); 2818 recalculate_sizes(); 2819 2820 if (s != NULL) 2821 session_update_activity(s, &c->activity_time); 2822 break; 2823 case MSG_SHELL: 2824 if (datalen != 0) 2825 fatalx("bad MSG_SHELL size"); 2826 2827 server_client_dispatch_shell(c); 2828 break; 2829 case MSG_WRITE_READY: 2830 file_write_ready(&c->files, imsg); 2831 break; 2832 case MSG_READ: 2833 file_read_data(&c->files, imsg); 2834 break; 2835 case MSG_READ_DONE: 2836 file_read_done(&c->files, imsg); 2837 break; 2838 } 2839 } 2840 2841 /* Callback when command is not allowed. */ 2842 static enum cmd_retval 2843 server_client_read_only(struct cmdq_item *item, __unused void *data) 2844 { 2845 cmdq_error(item, "client is read-only"); 2846 return (CMD_RETURN_ERROR); 2847 } 2848 2849 /* Callback when command is done. */ 2850 static enum cmd_retval 2851 server_client_command_done(struct cmdq_item *item, __unused void *data) 2852 { 2853 struct client *c = cmdq_get_client(item); 2854 2855 if (~c->flags & CLIENT_ATTACHED) 2856 c->flags |= CLIENT_EXIT; 2857 else if (~c->flags & CLIENT_EXIT) { 2858 if (c->flags & CLIENT_CONTROL) 2859 control_ready(c); 2860 tty_send_requests(&c->tty); 2861 } 2862 return (CMD_RETURN_NORMAL); 2863 } 2864 2865 /* Handle command message. */ 2866 static void 2867 server_client_dispatch_command(struct client *c, struct imsg *imsg) 2868 { 2869 struct msg_command data; 2870 char *buf; 2871 size_t len; 2872 int argc; 2873 char **argv, *cause; 2874 struct cmd_parse_result *pr; 2875 struct args_value *values; 2876 struct cmdq_item *new_item; 2877 2878 if (c->flags & CLIENT_EXIT) 2879 return; 2880 2881 if (imsg->hdr.len - IMSG_HEADER_SIZE < sizeof data) 2882 fatalx("bad MSG_COMMAND size"); 2883 memcpy(&data, imsg->data, sizeof data); 2884 2885 buf = (char *)imsg->data + sizeof data; 2886 len = imsg->hdr.len - IMSG_HEADER_SIZE - sizeof data; 2887 if (len > 0 && buf[len - 1] != '\0') 2888 fatalx("bad MSG_COMMAND string"); 2889 2890 argc = data.argc; 2891 if (cmd_unpack_argv(buf, len, argc, &argv) != 0) { 2892 cause = xstrdup("command too long"); 2893 goto error; 2894 } 2895 2896 if (argc == 0) { 2897 argc = 1; 2898 argv = xcalloc(1, sizeof *argv); 2899 *argv = xstrdup("new-session"); 2900 } 2901 2902 values = args_from_vector(argc, argv); 2903 pr = cmd_parse_from_arguments(values, argc, NULL); 2904 switch (pr->status) { 2905 case CMD_PARSE_ERROR: 2906 cause = pr->error; 2907 goto error; 2908 case CMD_PARSE_SUCCESS: 2909 break; 2910 } 2911 args_free_values(values, argc); 2912 free(values); 2913 cmd_free_argv(argc, argv); 2914 2915 if ((c->flags & CLIENT_READONLY) && 2916 !cmd_list_all_have(pr->cmdlist, CMD_READONLY)) 2917 new_item = cmdq_get_callback(server_client_read_only, NULL); 2918 else 2919 new_item = cmdq_get_command(pr->cmdlist, NULL); 2920 cmdq_append(c, new_item); 2921 cmdq_append(c, cmdq_get_callback(server_client_command_done, NULL)); 2922 2923 cmd_list_free(pr->cmdlist); 2924 return; 2925 2926 error: 2927 cmd_free_argv(argc, argv); 2928 2929 cmdq_append(c, cmdq_get_error(cause)); 2930 free(cause); 2931 2932 c->flags |= CLIENT_EXIT; 2933 } 2934 2935 /* Handle identify message. */ 2936 static void 2937 server_client_dispatch_identify(struct client *c, struct imsg *imsg) 2938 { 2939 const char *data, *home; 2940 size_t datalen; 2941 int flags, feat; 2942 uint64_t longflags; 2943 char *name; 2944 2945 if (c->flags & CLIENT_IDENTIFIED) 2946 fatalx("out-of-order identify message"); 2947 2948 data = imsg->data; 2949 datalen = imsg->hdr.len - IMSG_HEADER_SIZE; 2950 2951 switch (imsg->hdr.type) { 2952 case MSG_IDENTIFY_FEATURES: 2953 if (datalen != sizeof feat) 2954 fatalx("bad MSG_IDENTIFY_FEATURES size"); 2955 memcpy(&feat, data, sizeof feat); 2956 c->term_features |= feat; 2957 log_debug("client %p IDENTIFY_FEATURES %s", c, 2958 tty_get_features(feat)); 2959 break; 2960 case MSG_IDENTIFY_FLAGS: 2961 if (datalen != sizeof flags) 2962 fatalx("bad MSG_IDENTIFY_FLAGS size"); 2963 memcpy(&flags, data, sizeof flags); 2964 c->flags |= flags; 2965 log_debug("client %p IDENTIFY_FLAGS %#x", c, flags); 2966 break; 2967 case MSG_IDENTIFY_LONGFLAGS: 2968 if (datalen != sizeof longflags) 2969 fatalx("bad MSG_IDENTIFY_LONGFLAGS size"); 2970 memcpy(&longflags, data, sizeof longflags); 2971 c->flags |= longflags; 2972 log_debug("client %p IDENTIFY_LONGFLAGS %#llx", c, 2973 (unsigned long long)longflags); 2974 break; 2975 case MSG_IDENTIFY_TERM: 2976 if (datalen == 0 || data[datalen - 1] != '\0') 2977 fatalx("bad MSG_IDENTIFY_TERM string"); 2978 if (*data == '\0') 2979 c->term_name = xstrdup("unknown"); 2980 else 2981 c->term_name = xstrdup(data); 2982 log_debug("client %p IDENTIFY_TERM %s", c, data); 2983 break; 2984 case MSG_IDENTIFY_TERMINFO: 2985 if (datalen == 0 || data[datalen - 1] != '\0') 2986 fatalx("bad MSG_IDENTIFY_TERMINFO string"); 2987 c->term_caps = xreallocarray(c->term_caps, c->term_ncaps + 1, 2988 sizeof *c->term_caps); 2989 c->term_caps[c->term_ncaps++] = xstrdup(data); 2990 log_debug("client %p IDENTIFY_TERMINFO %s", c, data); 2991 break; 2992 case MSG_IDENTIFY_TTYNAME: 2993 if (datalen == 0 || data[datalen - 1] != '\0') 2994 fatalx("bad MSG_IDENTIFY_TTYNAME string"); 2995 c->ttyname = xstrdup(data); 2996 log_debug("client %p IDENTIFY_TTYNAME %s", c, data); 2997 break; 2998 case MSG_IDENTIFY_CWD: 2999 if (datalen == 0 || data[datalen - 1] != '\0') 3000 fatalx("bad MSG_IDENTIFY_CWD string"); 3001 if (access(data, X_OK) == 0) 3002 c->cwd = xstrdup(data); 3003 else if ((home = find_home()) != NULL) 3004 c->cwd = xstrdup(home); 3005 else 3006 c->cwd = xstrdup("/"); 3007 log_debug("client %p IDENTIFY_CWD %s", c, data); 3008 break; 3009 case MSG_IDENTIFY_STDIN: 3010 if (datalen != 0) 3011 fatalx("bad MSG_IDENTIFY_STDIN size"); 3012 c->fd = imsg_get_fd(imsg); 3013 log_debug("client %p IDENTIFY_STDIN %d", c, c->fd); 3014 break; 3015 case MSG_IDENTIFY_STDOUT: 3016 if (datalen != 0) 3017 fatalx("bad MSG_IDENTIFY_STDOUT size"); 3018 c->out_fd = imsg_get_fd(imsg); 3019 log_debug("client %p IDENTIFY_STDOUT %d", c, c->out_fd); 3020 break; 3021 case MSG_IDENTIFY_ENVIRON: 3022 if (datalen == 0 || data[datalen - 1] != '\0') 3023 fatalx("bad MSG_IDENTIFY_ENVIRON string"); 3024 if (strchr(data, '=') != NULL) 3025 environ_put(c->environ, data, 0); 3026 log_debug("client %p IDENTIFY_ENVIRON %s", c, data); 3027 break; 3028 case MSG_IDENTIFY_CLIENTPID: 3029 if (datalen != sizeof c->pid) 3030 fatalx("bad MSG_IDENTIFY_CLIENTPID size"); 3031 memcpy(&c->pid, data, sizeof c->pid); 3032 log_debug("client %p IDENTIFY_CLIENTPID %ld", c, (long)c->pid); 3033 break; 3034 default: 3035 break; 3036 } 3037 3038 if (imsg->hdr.type != MSG_IDENTIFY_DONE) 3039 return; 3040 c->flags |= CLIENT_IDENTIFIED; 3041 3042 if (*c->ttyname != '\0') 3043 name = xstrdup(c->ttyname); 3044 else 3045 xasprintf(&name, "client-%ld", (long)c->pid); 3046 c->name = name; 3047 log_debug("client %p name is %s", c, c->name); 3048 3049 if (c->flags & CLIENT_CONTROL) 3050 control_start(c); 3051 else if (c->fd != -1) { 3052 if (tty_init(&c->tty, c) != 0) { 3053 close(c->fd); 3054 c->fd = -1; 3055 } else { 3056 tty_resize(&c->tty); 3057 c->flags |= CLIENT_TERMINAL; 3058 } 3059 close(c->out_fd); 3060 c->out_fd = -1; 3061 } 3062 3063 /* 3064 * If this is the first client, load configuration files. Any later 3065 * clients are allowed to continue with their command even if the 3066 * config has not been loaded - they might have been run from inside it 3067 */ 3068 if ((~c->flags & CLIENT_EXIT) && 3069 !cfg_finished && 3070 c == TAILQ_FIRST(&clients)) 3071 start_cfg(); 3072 } 3073 3074 /* Handle shell message. */ 3075 static void 3076 server_client_dispatch_shell(struct client *c) 3077 { 3078 const char *shell; 3079 3080 shell = options_get_string(global_s_options, "default-shell"); 3081 if (!checkshell(shell)) 3082 shell = _PATH_BSHELL; 3083 proc_send(c->peer, MSG_SHELL, -1, shell, strlen(shell) + 1); 3084 3085 proc_kill_peer(c->peer); 3086 } 3087 3088 /* Get client working directory. */ 3089 const char * 3090 server_client_get_cwd(struct client *c, struct session *s) 3091 { 3092 const char *home; 3093 3094 if (!cfg_finished && cfg_client != NULL) 3095 return (cfg_client->cwd); 3096 if (c != NULL && c->session == NULL && c->cwd != NULL) 3097 return (c->cwd); 3098 if (s != NULL && s->cwd != NULL) 3099 return (s->cwd); 3100 if (c != NULL && (s = c->session) != NULL && s->cwd != NULL) 3101 return (s->cwd); 3102 if ((home = find_home()) != NULL) 3103 return (home); 3104 return ("/"); 3105 } 3106 3107 /* Get control client flags. */ 3108 static uint64_t 3109 server_client_control_flags(struct client *c, const char *next) 3110 { 3111 if (strcmp(next, "pause-after") == 0) { 3112 c->pause_age = 0; 3113 return (CLIENT_CONTROL_PAUSEAFTER); 3114 } 3115 if (sscanf(next, "pause-after=%u", &c->pause_age) == 1) { 3116 c->pause_age *= 1000; 3117 return (CLIENT_CONTROL_PAUSEAFTER); 3118 } 3119 if (strcmp(next, "no-output") == 0) 3120 return (CLIENT_CONTROL_NOOUTPUT); 3121 if (strcmp(next, "wait-exit") == 0) 3122 return (CLIENT_CONTROL_WAITEXIT); 3123 return (0); 3124 } 3125 3126 /* Set client flags. */ 3127 void 3128 server_client_set_flags(struct client *c, const char *flags) 3129 { 3130 char *s, *copy, *next; 3131 uint64_t flag; 3132 int not; 3133 3134 s = copy = xstrdup(flags); 3135 while ((next = strsep(&s, ",")) != NULL) { 3136 not = (*next == '!'); 3137 if (not) 3138 next++; 3139 3140 if (c->flags & CLIENT_CONTROL) 3141 flag = server_client_control_flags(c, next); 3142 else 3143 flag = 0; 3144 if (strcmp(next, "read-only") == 0) 3145 flag = CLIENT_READONLY; 3146 else if (strcmp(next, "ignore-size") == 0) 3147 flag = CLIENT_IGNORESIZE; 3148 else if (strcmp(next, "active-pane") == 0) 3149 flag = CLIENT_ACTIVEPANE; 3150 if (flag == 0) 3151 continue; 3152 3153 log_debug("client %s set flag %s", c->name, next); 3154 if (not) { 3155 if (c->flags & CLIENT_READONLY) 3156 flag &= ~CLIENT_READONLY; 3157 c->flags &= ~flag; 3158 } else 3159 c->flags |= flag; 3160 if (flag == CLIENT_CONTROL_NOOUTPUT) 3161 control_reset_offsets(c); 3162 } 3163 free(copy); 3164 proc_send(c->peer, MSG_FLAGS, -1, &c->flags, sizeof c->flags); 3165 } 3166 3167 /* Get client flags. This is only flags useful to show to users. */ 3168 const char * 3169 server_client_get_flags(struct client *c) 3170 { 3171 static char s[256]; 3172 char tmp[32]; 3173 3174 *s = '\0'; 3175 if (c->flags & CLIENT_ATTACHED) 3176 strlcat(s, "attached,", sizeof s); 3177 if (c->flags & CLIENT_FOCUSED) 3178 strlcat(s, "focused,", sizeof s); 3179 if (c->flags & CLIENT_CONTROL) 3180 strlcat(s, "control-mode,", sizeof s); 3181 if (c->flags & CLIENT_IGNORESIZE) 3182 strlcat(s, "ignore-size,", sizeof s); 3183 if (c->flags & CLIENT_CONTROL_NOOUTPUT) 3184 strlcat(s, "no-output,", sizeof s); 3185 if (c->flags & CLIENT_CONTROL_WAITEXIT) 3186 strlcat(s, "wait-exit,", sizeof s); 3187 if (c->flags & CLIENT_CONTROL_PAUSEAFTER) { 3188 xsnprintf(tmp, sizeof tmp, "pause-after=%u,", 3189 c->pause_age / 1000); 3190 strlcat(s, tmp, sizeof s); 3191 } 3192 if (c->flags & CLIENT_READONLY) 3193 strlcat(s, "read-only,", sizeof s); 3194 if (c->flags & CLIENT_ACTIVEPANE) 3195 strlcat(s, "active-pane,", sizeof s); 3196 if (c->flags & CLIENT_SUSPENDED) 3197 strlcat(s, "suspended,", sizeof s); 3198 if (c->flags & CLIENT_UTF8) 3199 strlcat(s, "UTF-8,", sizeof s); 3200 if (*s != '\0') 3201 s[strlen(s) - 1] = '\0'; 3202 return (s); 3203 } 3204 3205 /* Get client window. */ 3206 struct client_window * 3207 server_client_get_client_window(struct client *c, u_int id) 3208 { 3209 struct client_window cw = { .window = id }; 3210 3211 return (RB_FIND(client_windows, &c->windows, &cw)); 3212 } 3213 3214 /* Add client window. */ 3215 struct client_window * 3216 server_client_add_client_window(struct client *c, u_int id) 3217 { 3218 struct client_window *cw; 3219 3220 cw = server_client_get_client_window(c, id); 3221 if (cw == NULL) { 3222 cw = xcalloc(1, sizeof *cw); 3223 cw->window = id; 3224 RB_INSERT(client_windows, &c->windows, cw); 3225 } 3226 return (cw); 3227 } 3228 3229 /* Get client active pane. */ 3230 struct window_pane * 3231 server_client_get_pane(struct client *c) 3232 { 3233 struct session *s = c->session; 3234 struct client_window *cw; 3235 3236 if (s == NULL) 3237 return (NULL); 3238 3239 if (~c->flags & CLIENT_ACTIVEPANE) 3240 return (s->curw->window->active); 3241 cw = server_client_get_client_window(c, s->curw->window->id); 3242 if (cw == NULL) 3243 return (s->curw->window->active); 3244 return (cw->pane); 3245 } 3246 3247 /* Set client active pane. */ 3248 void 3249 server_client_set_pane(struct client *c, struct window_pane *wp) 3250 { 3251 struct session *s = c->session; 3252 struct client_window *cw; 3253 3254 if (s == NULL) 3255 return; 3256 3257 cw = server_client_add_client_window(c, s->curw->window->id); 3258 cw->pane = wp; 3259 log_debug("%s pane now %%%u", c->name, wp->id); 3260 } 3261 3262 /* Remove pane from client lists. */ 3263 void 3264 server_client_remove_pane(struct window_pane *wp) 3265 { 3266 struct client *c; 3267 struct window *w = wp->window; 3268 struct client_window *cw; 3269 3270 TAILQ_FOREACH(c, &clients, entry) { 3271 cw = server_client_get_client_window(c, w->id); 3272 if (cw != NULL && cw->pane == wp) { 3273 RB_REMOVE(client_windows, &c->windows, cw); 3274 free(cw); 3275 } 3276 } 3277 } 3278 3279 /* Print to a client. */ 3280 void 3281 server_client_print(struct client *c, int parse, struct evbuffer *evb) 3282 { 3283 void *data = EVBUFFER_DATA(evb); 3284 size_t size = EVBUFFER_LENGTH(evb); 3285 struct window_pane *wp; 3286 struct window_mode_entry *wme; 3287 char *sanitized, *msg, *line; 3288 3289 if (!parse) { 3290 utf8_stravisx(&msg, data, size, 3291 VIS_OCTAL|VIS_CSTYLE|VIS_NOSLASH); 3292 log_debug("%s: %s", __func__, msg); 3293 } else { 3294 msg = EVBUFFER_DATA(evb); 3295 if (msg[size - 1] != '\0') 3296 evbuffer_add(evb, "", 1); 3297 } 3298 3299 if (c == NULL) 3300 goto out; 3301 3302 if (c->session == NULL || (c->flags & CLIENT_CONTROL)) { 3303 if (~c->flags & CLIENT_UTF8) { 3304 sanitized = utf8_sanitize(msg); 3305 if (c->flags & CLIENT_CONTROL) 3306 control_write(c, "%s", sanitized); 3307 else 3308 file_print(c, "%s\n", sanitized); 3309 free(sanitized); 3310 } else { 3311 if (c->flags & CLIENT_CONTROL) 3312 control_write(c, "%s", msg); 3313 else 3314 file_print(c, "%s\n", msg); 3315 } 3316 goto out; 3317 } 3318 3319 wp = server_client_get_pane(c); 3320 wme = TAILQ_FIRST(&wp->modes); 3321 if (wme == NULL || wme->mode != &window_view_mode) 3322 window_pane_set_mode(wp, NULL, &window_view_mode, NULL, NULL); 3323 if (parse) { 3324 do { 3325 line = evbuffer_readln(evb, NULL, EVBUFFER_EOL_LF); 3326 if (line != NULL) { 3327 window_copy_add(wp, 1, "%s", line); 3328 free(line); 3329 } 3330 } while (line != NULL); 3331 3332 size = EVBUFFER_LENGTH(evb); 3333 if (size != 0) { 3334 line = EVBUFFER_DATA(evb); 3335 window_copy_add(wp, 1, "%.*s", (int)size, line); 3336 } 3337 } else 3338 window_copy_add(wp, 0, "%s", msg); 3339 3340 out: 3341 if (!parse) 3342 free(msg); 3343 } 3344