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