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