Merge branch 'obsd-master'
[tmux.git] / server-client.c
blob449a9bcccbfd992ba5231c2775650575d82d62cb
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 if (m->sgr_type == 'm')
626 b = m->sgr_b;
627 log_debug("up at %u,%u", x, y);
628 } else {
629 if (c->flags & CLIENT_DOUBLECLICK) {
630 evtimer_del(&c->click_timer);
631 c->flags &= ~CLIENT_DOUBLECLICK;
632 if (m->b == c->click_button) {
633 type = SECOND;
634 x = m->x, y = m->y, b = m->b;
635 log_debug("second-click at %u,%u", x, y);
636 c->flags |= CLIENT_TRIPLECLICK;
638 } else if (c->flags & CLIENT_TRIPLECLICK) {
639 evtimer_del(&c->click_timer);
640 c->flags &= ~CLIENT_TRIPLECLICK;
641 if (m->b == c->click_button) {
642 type = TRIPLE;
643 x = m->x, y = m->y, b = m->b;
644 log_debug("triple-click at %u,%u", x, y);
645 goto have_event;
649 /* DOWN is the only remaining event type. */
650 if (type == NOTYPE) {
651 type = DOWN;
652 x = m->x, y = m->y, b = m->b;
653 log_debug("down at %u,%u", x, y);
654 c->flags |= CLIENT_DOUBLECLICK;
657 if (KEYC_CLICK_TIMEOUT != 0) {
658 memcpy(&c->click_event, m, sizeof c->click_event);
659 c->click_button = m->b;
661 log_debug("click timer started");
662 tv.tv_sec = KEYC_CLICK_TIMEOUT / 1000;
663 tv.tv_usec = (KEYC_CLICK_TIMEOUT % 1000) * 1000L;
664 evtimer_del(&c->click_timer);
665 evtimer_add(&c->click_timer, &tv);
669 have_event:
670 if (type == NOTYPE)
671 return (KEYC_UNKNOWN);
673 /* Save the session. */
674 m->s = s->id;
675 m->w = -1;
676 m->wp = -1;
677 m->ignore = ignore;
679 /* Is this on the status line? */
680 m->statusat = status_at_line(c);
681 m->statuslines = status_line_size(c);
682 if (m->statusat != -1 &&
683 y >= (u_int)m->statusat &&
684 y < m->statusat + m->statuslines) {
685 sr = status_get_range(c, x, y - m->statusat);
686 if (sr == NULL) {
687 where = STATUS_DEFAULT;
688 } else {
689 switch (sr->type) {
690 case STYLE_RANGE_NONE:
691 return (KEYC_UNKNOWN);
692 case STYLE_RANGE_LEFT:
693 log_debug("mouse range: left");
694 where = STATUS_LEFT;
695 break;
696 case STYLE_RANGE_RIGHT:
697 log_debug("mouse range: right");
698 where = STATUS_RIGHT;
699 break;
700 case STYLE_RANGE_PANE:
701 fwp = window_pane_find_by_id(sr->argument);
702 if (fwp == NULL)
703 return (KEYC_UNKNOWN);
704 m->wp = sr->argument;
706 log_debug("mouse range: pane %%%u", m->wp);
707 where = STATUS;
708 break;
709 case STYLE_RANGE_WINDOW:
710 fwl = winlink_find_by_index(&s->windows,
711 sr->argument);
712 if (fwl == NULL)
713 return (KEYC_UNKNOWN);
714 m->w = fwl->window->id;
716 log_debug("mouse range: window @%u", m->w);
717 where = STATUS;
718 break;
719 case STYLE_RANGE_SESSION:
720 fs = session_find_by_id(sr->argument);
721 if (fs == NULL)
722 return (KEYC_UNKNOWN);
723 m->s = sr->argument;
725 log_debug("mouse range: session $%u", m->s);
726 where = STATUS;
727 break;
728 case STYLE_RANGE_USER:
729 where = STATUS;
730 break;
735 /* Not on status line. Adjust position and check for border or pane. */
736 if (where == NOWHERE) {
737 px = x;
738 if (m->statusat == 0 && y >= m->statuslines)
739 py = y - m->statuslines;
740 else if (m->statusat > 0 && y >= (u_int)m->statusat)
741 py = m->statusat - 1;
742 else
743 py = y;
745 tty_window_offset(&c->tty, &m->ox, &m->oy, &sx, &sy);
746 log_debug("mouse window @%u at %u,%u (%ux%u)",
747 s->curw->window->id, m->ox, m->oy, sx, sy);
748 if (px > sx || py > sy)
749 return (KEYC_UNKNOWN);
750 px = px + m->ox;
751 py = py + m->oy;
753 /* Try the pane borders if not zoomed. */
754 if (~s->curw->window->flags & WINDOW_ZOOMED) {
755 TAILQ_FOREACH(wp, &s->curw->window->panes, entry) {
756 if ((wp->xoff + wp->sx == px &&
757 wp->yoff <= 1 + py &&
758 wp->yoff + wp->sy >= py) ||
759 (wp->yoff + wp->sy == py &&
760 wp->xoff <= 1 + px &&
761 wp->xoff + wp->sx >= px))
762 break;
764 if (wp != NULL)
765 where = BORDER;
768 /* Otherwise try inside the pane. */
769 if (where == NOWHERE) {
770 wp = window_get_active_at(s->curw->window, px, py);
771 if (wp != NULL)
772 where = PANE;
773 else
774 return (KEYC_UNKNOWN);
776 if (where == PANE)
777 log_debug("mouse %u,%u on pane %%%u", x, y, wp->id);
778 else if (where == BORDER)
779 log_debug("mouse on pane %%%u border", wp->id);
780 m->wp = wp->id;
781 m->w = wp->window->id;
782 } else
783 m->wp = -1;
785 /* Stop dragging if needed. */
786 if (type != DRAG && type != WHEEL && c->tty.mouse_drag_flag != 0) {
787 if (c->tty.mouse_drag_release != NULL)
788 c->tty.mouse_drag_release(c, m);
790 c->tty.mouse_drag_update = NULL;
791 c->tty.mouse_drag_release = NULL;
794 * End a mouse drag by passing a MouseDragEnd key corresponding
795 * to the button that started the drag.
797 switch (c->tty.mouse_drag_flag - 1) {
798 case MOUSE_BUTTON_1:
799 if (where == PANE)
800 key = KEYC_MOUSEDRAGEND1_PANE;
801 if (where == STATUS)
802 key = KEYC_MOUSEDRAGEND1_STATUS;
803 if (where == STATUS_LEFT)
804 key = KEYC_MOUSEDRAGEND1_STATUS_LEFT;
805 if (where == STATUS_RIGHT)
806 key = KEYC_MOUSEDRAGEND1_STATUS_RIGHT;
807 if (where == STATUS_DEFAULT)
808 key = KEYC_MOUSEDRAGEND1_STATUS_DEFAULT;
809 if (where == BORDER)
810 key = KEYC_MOUSEDRAGEND1_BORDER;
811 break;
812 case MOUSE_BUTTON_2:
813 if (where == PANE)
814 key = KEYC_MOUSEDRAGEND2_PANE;
815 if (where == STATUS)
816 key = KEYC_MOUSEDRAGEND2_STATUS;
817 if (where == STATUS_LEFT)
818 key = KEYC_MOUSEDRAGEND2_STATUS_LEFT;
819 if (where == STATUS_RIGHT)
820 key = KEYC_MOUSEDRAGEND2_STATUS_RIGHT;
821 if (where == STATUS_DEFAULT)
822 key = KEYC_MOUSEDRAGEND2_STATUS_DEFAULT;
823 if (where == BORDER)
824 key = KEYC_MOUSEDRAGEND2_BORDER;
825 break;
826 case MOUSE_BUTTON_3:
827 if (where == PANE)
828 key = KEYC_MOUSEDRAGEND3_PANE;
829 if (where == STATUS)
830 key = KEYC_MOUSEDRAGEND3_STATUS;
831 if (where == STATUS_LEFT)
832 key = KEYC_MOUSEDRAGEND3_STATUS_LEFT;
833 if (where == STATUS_RIGHT)
834 key = KEYC_MOUSEDRAGEND3_STATUS_RIGHT;
835 if (where == STATUS_DEFAULT)
836 key = KEYC_MOUSEDRAGEND3_STATUS_DEFAULT;
837 if (where == BORDER)
838 key = KEYC_MOUSEDRAGEND3_BORDER;
839 break;
840 case MOUSE_BUTTON_6:
841 if (where == PANE)
842 key = KEYC_MOUSEDRAGEND6_PANE;
843 if (where == STATUS)
844 key = KEYC_MOUSEDRAGEND6_STATUS;
845 if (where == STATUS_LEFT)
846 key = KEYC_MOUSEDRAGEND6_STATUS_LEFT;
847 if (where == STATUS_RIGHT)
848 key = KEYC_MOUSEDRAGEND6_STATUS_RIGHT;
849 if (where == STATUS_DEFAULT)
850 key = KEYC_MOUSEDRAGEND6_STATUS_DEFAULT;
851 if (where == BORDER)
852 key = KEYC_MOUSEDRAGEND6_BORDER;
853 break;
854 case MOUSE_BUTTON_7:
855 if (where == PANE)
856 key = KEYC_MOUSEDRAGEND7_PANE;
857 if (where == STATUS)
858 key = KEYC_MOUSEDRAGEND7_STATUS;
859 if (where == STATUS_LEFT)
860 key = KEYC_MOUSEDRAGEND7_STATUS_LEFT;
861 if (where == STATUS_RIGHT)
862 key = KEYC_MOUSEDRAGEND7_STATUS_RIGHT;
863 if (where == STATUS_DEFAULT)
864 key = KEYC_MOUSEDRAGEND7_STATUS_DEFAULT;
865 if (where == BORDER)
866 key = KEYC_MOUSEDRAGEND7_BORDER;
867 break;
868 case MOUSE_BUTTON_8:
869 if (where == PANE)
870 key = KEYC_MOUSEDRAGEND8_PANE;
871 if (where == STATUS)
872 key = KEYC_MOUSEDRAGEND8_STATUS;
873 if (where == STATUS_LEFT)
874 key = KEYC_MOUSEDRAGEND8_STATUS_LEFT;
875 if (where == STATUS_RIGHT)
876 key = KEYC_MOUSEDRAGEND8_STATUS_RIGHT;
877 if (where == STATUS_DEFAULT)
878 key = KEYC_MOUSEDRAGEND8_STATUS_DEFAULT;
879 if (where == BORDER)
880 key = KEYC_MOUSEDRAGEND8_BORDER;
881 break;
882 case MOUSE_BUTTON_9:
883 if (where == PANE)
884 key = KEYC_MOUSEDRAGEND9_PANE;
885 if (where == STATUS)
886 key = KEYC_MOUSEDRAGEND9_STATUS;
887 if (where == STATUS_LEFT)
888 key = KEYC_MOUSEDRAGEND9_STATUS_LEFT;
889 if (where == STATUS_RIGHT)
890 key = KEYC_MOUSEDRAGEND9_STATUS_RIGHT;
891 if (where == STATUS_DEFAULT)
892 key = KEYC_MOUSEDRAGEND9_STATUS_DEFAULT;
893 if (where == BORDER)
894 key = KEYC_MOUSEDRAGEND9_BORDER;
895 break;
896 case MOUSE_BUTTON_10:
897 if (where == PANE)
898 key = KEYC_MOUSEDRAGEND10_PANE;
899 if (where == STATUS)
900 key = KEYC_MOUSEDRAGEND10_STATUS;
901 if (where == STATUS_LEFT)
902 key = KEYC_MOUSEDRAGEND10_STATUS_LEFT;
903 if (where == STATUS_RIGHT)
904 key = KEYC_MOUSEDRAGEND10_STATUS_RIGHT;
905 if (where == STATUS_DEFAULT)
906 key = KEYC_MOUSEDRAGEND10_STATUS_DEFAULT;
907 if (where == BORDER)
908 key = KEYC_MOUSEDRAGEND10_BORDER;
909 break;
910 case MOUSE_BUTTON_11:
911 if (where == PANE)
912 key = KEYC_MOUSEDRAGEND11_PANE;
913 if (where == STATUS)
914 key = KEYC_MOUSEDRAGEND11_STATUS;
915 if (where == STATUS_LEFT)
916 key = KEYC_MOUSEDRAGEND11_STATUS_LEFT;
917 if (where == STATUS_RIGHT)
918 key = KEYC_MOUSEDRAGEND11_STATUS_RIGHT;
919 if (where == STATUS_DEFAULT)
920 key = KEYC_MOUSEDRAGEND11_STATUS_DEFAULT;
921 if (where == BORDER)
922 key = KEYC_MOUSEDRAGEND11_BORDER;
923 break;
924 default:
925 key = KEYC_MOUSE;
926 break;
928 c->tty.mouse_drag_flag = 0;
929 goto out;
932 /* Convert to a key binding. */
933 key = KEYC_UNKNOWN;
934 switch (type) {
935 case NOTYPE:
936 break;
937 case MOVE:
938 if (where == PANE)
939 key = KEYC_MOUSEMOVE_PANE;
940 if (where == STATUS)
941 key = KEYC_MOUSEMOVE_STATUS;
942 if (where == STATUS_LEFT)
943 key = KEYC_MOUSEMOVE_STATUS_LEFT;
944 if (where == STATUS_RIGHT)
945 key = KEYC_MOUSEMOVE_STATUS_RIGHT;
946 if (where == STATUS_DEFAULT)
947 key = KEYC_MOUSEMOVE_STATUS_DEFAULT;
948 if (where == BORDER)
949 key = KEYC_MOUSEMOVE_BORDER;
950 break;
951 case DRAG:
952 if (c->tty.mouse_drag_update != NULL)
953 key = KEYC_DRAGGING;
954 else {
955 switch (MOUSE_BUTTONS(b)) {
956 case MOUSE_BUTTON_1:
957 if (where == PANE)
958 key = KEYC_MOUSEDRAG1_PANE;
959 if (where == STATUS)
960 key = KEYC_MOUSEDRAG1_STATUS;
961 if (where == STATUS_LEFT)
962 key = KEYC_MOUSEDRAG1_STATUS_LEFT;
963 if (where == STATUS_RIGHT)
964 key = KEYC_MOUSEDRAG1_STATUS_RIGHT;
965 if (where == STATUS_DEFAULT)
966 key = KEYC_MOUSEDRAG1_STATUS_DEFAULT;
967 if (where == BORDER)
968 key = KEYC_MOUSEDRAG1_BORDER;
969 break;
970 case MOUSE_BUTTON_2:
971 if (where == PANE)
972 key = KEYC_MOUSEDRAG2_PANE;
973 if (where == STATUS)
974 key = KEYC_MOUSEDRAG2_STATUS;
975 if (where == STATUS_LEFT)
976 key = KEYC_MOUSEDRAG2_STATUS_LEFT;
977 if (where == STATUS_RIGHT)
978 key = KEYC_MOUSEDRAG2_STATUS_RIGHT;
979 if (where == STATUS_DEFAULT)
980 key = KEYC_MOUSEDRAG2_STATUS_DEFAULT;
981 if (where == BORDER)
982 key = KEYC_MOUSEDRAG2_BORDER;
983 break;
984 case MOUSE_BUTTON_3:
985 if (where == PANE)
986 key = KEYC_MOUSEDRAG3_PANE;
987 if (where == STATUS)
988 key = KEYC_MOUSEDRAG3_STATUS;
989 if (where == STATUS_LEFT)
990 key = KEYC_MOUSEDRAG3_STATUS_LEFT;
991 if (where == STATUS_RIGHT)
992 key = KEYC_MOUSEDRAG3_STATUS_RIGHT;
993 if (where == STATUS_DEFAULT)
994 key = KEYC_MOUSEDRAG3_STATUS_DEFAULT;
995 if (where == BORDER)
996 key = KEYC_MOUSEDRAG3_BORDER;
997 break;
998 case MOUSE_BUTTON_6:
999 if (where == PANE)
1000 key = KEYC_MOUSEDRAG6_PANE;
1001 if (where == STATUS)
1002 key = KEYC_MOUSEDRAG6_STATUS;
1003 if (where == STATUS_LEFT)
1004 key = KEYC_MOUSEDRAG6_STATUS_LEFT;
1005 if (where == STATUS_RIGHT)
1006 key = KEYC_MOUSEDRAG6_STATUS_RIGHT;
1007 if (where == STATUS_DEFAULT)
1008 key = KEYC_MOUSEDRAG6_STATUS_DEFAULT;
1009 if (where == BORDER)
1010 key = KEYC_MOUSEDRAG6_BORDER;
1011 break;
1012 case MOUSE_BUTTON_7:
1013 if (where == PANE)
1014 key = KEYC_MOUSEDRAG7_PANE;
1015 if (where == STATUS)
1016 key = KEYC_MOUSEDRAG7_STATUS;
1017 if (where == STATUS_LEFT)
1018 key = KEYC_MOUSEDRAG7_STATUS_LEFT;
1019 if (where == STATUS_RIGHT)
1020 key = KEYC_MOUSEDRAG7_STATUS_RIGHT;
1021 if (where == STATUS_DEFAULT)
1022 key = KEYC_MOUSEDRAG7_STATUS_DEFAULT;
1023 if (where == BORDER)
1024 key = KEYC_MOUSEDRAG7_BORDER;
1025 break;
1026 case MOUSE_BUTTON_8:
1027 if (where == PANE)
1028 key = KEYC_MOUSEDRAG8_PANE;
1029 if (where == STATUS)
1030 key = KEYC_MOUSEDRAG8_STATUS;
1031 if (where == STATUS_LEFT)
1032 key = KEYC_MOUSEDRAG8_STATUS_LEFT;
1033 if (where == STATUS_RIGHT)
1034 key = KEYC_MOUSEDRAG8_STATUS_RIGHT;
1035 if (where == STATUS_DEFAULT)
1036 key = KEYC_MOUSEDRAG8_STATUS_DEFAULT;
1037 if (where == BORDER)
1038 key = KEYC_MOUSEDRAG8_BORDER;
1039 break;
1040 case MOUSE_BUTTON_9:
1041 if (where == PANE)
1042 key = KEYC_MOUSEDRAG9_PANE;
1043 if (where == STATUS)
1044 key = KEYC_MOUSEDRAG9_STATUS;
1045 if (where == STATUS_LEFT)
1046 key = KEYC_MOUSEDRAG9_STATUS_LEFT;
1047 if (where == STATUS_RIGHT)
1048 key = KEYC_MOUSEDRAG9_STATUS_RIGHT;
1049 if (where == STATUS_DEFAULT)
1050 key = KEYC_MOUSEDRAG9_STATUS_DEFAULT;
1051 if (where == BORDER)
1052 key = KEYC_MOUSEDRAG9_BORDER;
1053 break;
1054 case MOUSE_BUTTON_10:
1055 if (where == PANE)
1056 key = KEYC_MOUSEDRAG10_PANE;
1057 if (where == STATUS)
1058 key = KEYC_MOUSEDRAG10_STATUS;
1059 if (where == STATUS_LEFT)
1060 key = KEYC_MOUSEDRAG10_STATUS_LEFT;
1061 if (where == STATUS_RIGHT)
1062 key = KEYC_MOUSEDRAG10_STATUS_RIGHT;
1063 if (where == STATUS_DEFAULT)
1064 key = KEYC_MOUSEDRAG10_STATUS_DEFAULT;
1065 if (where == BORDER)
1066 key = KEYC_MOUSEDRAG10_BORDER;
1067 break;
1068 case MOUSE_BUTTON_11:
1069 if (where == PANE)
1070 key = KEYC_MOUSEDRAG11_PANE;
1071 if (where == STATUS)
1072 key = KEYC_MOUSEDRAG11_STATUS;
1073 if (where == STATUS_LEFT)
1074 key = KEYC_MOUSEDRAG11_STATUS_LEFT;
1075 if (where == STATUS_RIGHT)
1076 key = KEYC_MOUSEDRAG11_STATUS_RIGHT;
1077 if (where == STATUS_DEFAULT)
1078 key = KEYC_MOUSEDRAG11_STATUS_DEFAULT;
1079 if (where == BORDER)
1080 key = KEYC_MOUSEDRAG11_BORDER;
1081 break;
1086 * Begin a drag by setting the flag to a non-zero value that
1087 * corresponds to the mouse button in use.
1089 c->tty.mouse_drag_flag = MOUSE_BUTTONS(b) + 1;
1090 break;
1091 case WHEEL:
1092 if (MOUSE_BUTTONS(b) == MOUSE_WHEEL_UP) {
1093 if (where == PANE)
1094 key = KEYC_WHEELUP_PANE;
1095 if (where == STATUS)
1096 key = KEYC_WHEELUP_STATUS;
1097 if (where == STATUS_LEFT)
1098 key = KEYC_WHEELUP_STATUS_LEFT;
1099 if (where == STATUS_RIGHT)
1100 key = KEYC_WHEELUP_STATUS_RIGHT;
1101 if (where == STATUS_DEFAULT)
1102 key = KEYC_WHEELUP_STATUS_DEFAULT;
1103 if (where == BORDER)
1104 key = KEYC_WHEELUP_BORDER;
1105 } else {
1106 if (where == PANE)
1107 key = KEYC_WHEELDOWN_PANE;
1108 if (where == STATUS)
1109 key = KEYC_WHEELDOWN_STATUS;
1110 if (where == STATUS_LEFT)
1111 key = KEYC_WHEELDOWN_STATUS_LEFT;
1112 if (where == STATUS_RIGHT)
1113 key = KEYC_WHEELDOWN_STATUS_RIGHT;
1114 if (where == STATUS_DEFAULT)
1115 key = KEYC_WHEELDOWN_STATUS_DEFAULT;
1116 if (where == BORDER)
1117 key = KEYC_WHEELDOWN_BORDER;
1119 break;
1120 case UP:
1121 switch (MOUSE_BUTTONS(b)) {
1122 case MOUSE_BUTTON_1:
1123 if (where == PANE)
1124 key = KEYC_MOUSEUP1_PANE;
1125 if (where == STATUS)
1126 key = KEYC_MOUSEUP1_STATUS;
1127 if (where == STATUS_LEFT)
1128 key = KEYC_MOUSEUP1_STATUS_LEFT;
1129 if (where == STATUS_RIGHT)
1130 key = KEYC_MOUSEUP1_STATUS_RIGHT;
1131 if (where == STATUS_DEFAULT)
1132 key = KEYC_MOUSEUP1_STATUS_DEFAULT;
1133 if (where == BORDER)
1134 key = KEYC_MOUSEUP1_BORDER;
1135 break;
1136 case MOUSE_BUTTON_2:
1137 if (where == PANE)
1138 key = KEYC_MOUSEUP2_PANE;
1139 if (where == STATUS)
1140 key = KEYC_MOUSEUP2_STATUS;
1141 if (where == STATUS_LEFT)
1142 key = KEYC_MOUSEUP2_STATUS_LEFT;
1143 if (where == STATUS_RIGHT)
1144 key = KEYC_MOUSEUP2_STATUS_RIGHT;
1145 if (where == STATUS_DEFAULT)
1146 key = KEYC_MOUSEUP2_STATUS_DEFAULT;
1147 if (where == BORDER)
1148 key = KEYC_MOUSEUP2_BORDER;
1149 break;
1150 case MOUSE_BUTTON_3:
1151 if (where == PANE)
1152 key = KEYC_MOUSEUP3_PANE;
1153 if (where == STATUS)
1154 key = KEYC_MOUSEUP3_STATUS;
1155 if (where == STATUS_LEFT)
1156 key = KEYC_MOUSEUP3_STATUS_LEFT;
1157 if (where == STATUS_RIGHT)
1158 key = KEYC_MOUSEUP3_STATUS_RIGHT;
1159 if (where == STATUS_DEFAULT)
1160 key = KEYC_MOUSEUP3_STATUS_DEFAULT;
1161 if (where == BORDER)
1162 key = KEYC_MOUSEUP3_BORDER;
1163 break;
1164 case MOUSE_BUTTON_6:
1165 if (where == PANE)
1166 key = KEYC_MOUSEUP6_PANE;
1167 if (where == STATUS)
1168 key = KEYC_MOUSEUP6_STATUS;
1169 if (where == STATUS_LEFT)
1170 key = KEYC_MOUSEUP6_STATUS_LEFT;
1171 if (where == STATUS_RIGHT)
1172 key = KEYC_MOUSEUP6_STATUS_RIGHT;
1173 if (where == STATUS_DEFAULT)
1174 key = KEYC_MOUSEUP6_STATUS_DEFAULT;
1175 if (where == BORDER)
1176 key = KEYC_MOUSEUP6_BORDER;
1177 break;
1178 case MOUSE_BUTTON_7:
1179 if (where == PANE)
1180 key = KEYC_MOUSEUP7_PANE;
1181 if (where == STATUS)
1182 key = KEYC_MOUSEUP7_STATUS;
1183 if (where == STATUS_LEFT)
1184 key = KEYC_MOUSEUP7_STATUS_LEFT;
1185 if (where == STATUS_RIGHT)
1186 key = KEYC_MOUSEUP7_STATUS_RIGHT;
1187 if (where == STATUS_DEFAULT)
1188 key = KEYC_MOUSEUP7_STATUS_DEFAULT;
1189 if (where == BORDER)
1190 key = KEYC_MOUSEUP7_BORDER;
1191 break;
1192 case MOUSE_BUTTON_8:
1193 if (where == PANE)
1194 key = KEYC_MOUSEUP8_PANE;
1195 if (where == STATUS)
1196 key = KEYC_MOUSEUP8_STATUS;
1197 if (where == STATUS_LEFT)
1198 key = KEYC_MOUSEUP8_STATUS_LEFT;
1199 if (where == STATUS_RIGHT)
1200 key = KEYC_MOUSEUP8_STATUS_RIGHT;
1201 if (where == STATUS_DEFAULT)
1202 key = KEYC_MOUSEUP8_STATUS_DEFAULT;
1203 if (where == BORDER)
1204 key = KEYC_MOUSEUP8_BORDER;
1205 break;
1206 case MOUSE_BUTTON_9:
1207 if (where == PANE)
1208 key = KEYC_MOUSEUP9_PANE;
1209 if (where == STATUS)
1210 key = KEYC_MOUSEUP9_STATUS;
1211 if (where == STATUS_LEFT)
1212 key = KEYC_MOUSEUP9_STATUS_LEFT;
1213 if (where == STATUS_RIGHT)
1214 key = KEYC_MOUSEUP9_STATUS_RIGHT;
1215 if (where == STATUS_DEFAULT)
1216 key = KEYC_MOUSEUP9_STATUS_DEFAULT;
1217 if (where == BORDER)
1218 key = KEYC_MOUSEUP9_BORDER;
1219 break;
1220 case MOUSE_BUTTON_10:
1221 if (where == PANE)
1222 key = KEYC_MOUSEUP1_PANE;
1223 if (where == STATUS)
1224 key = KEYC_MOUSEUP1_STATUS;
1225 if (where == STATUS_LEFT)
1226 key = KEYC_MOUSEUP1_STATUS_LEFT;
1227 if (where == STATUS_RIGHT)
1228 key = KEYC_MOUSEUP1_STATUS_RIGHT;
1229 if (where == STATUS_DEFAULT)
1230 key = KEYC_MOUSEUP1_STATUS_DEFAULT;
1231 if (where == BORDER)
1232 key = KEYC_MOUSEUP1_BORDER;
1233 break;
1234 case MOUSE_BUTTON_11:
1235 if (where == PANE)
1236 key = KEYC_MOUSEUP11_PANE;
1237 if (where == STATUS)
1238 key = KEYC_MOUSEUP11_STATUS;
1239 if (where == STATUS_LEFT)
1240 key = KEYC_MOUSEUP11_STATUS_LEFT;
1241 if (where == STATUS_RIGHT)
1242 key = KEYC_MOUSEUP11_STATUS_RIGHT;
1243 if (where == STATUS_DEFAULT)
1244 key = KEYC_MOUSEUP11_STATUS_DEFAULT;
1245 if (where == BORDER)
1246 key = KEYC_MOUSEUP11_BORDER;
1247 break;
1249 break;
1250 case DOWN:
1251 switch (MOUSE_BUTTONS(b)) {
1252 case MOUSE_BUTTON_1:
1253 if (where == PANE)
1254 key = KEYC_MOUSEDOWN1_PANE;
1255 if (where == STATUS)
1256 key = KEYC_MOUSEDOWN1_STATUS;
1257 if (where == STATUS_LEFT)
1258 key = KEYC_MOUSEDOWN1_STATUS_LEFT;
1259 if (where == STATUS_RIGHT)
1260 key = KEYC_MOUSEDOWN1_STATUS_RIGHT;
1261 if (where == STATUS_DEFAULT)
1262 key = KEYC_MOUSEDOWN1_STATUS_DEFAULT;
1263 if (where == BORDER)
1264 key = KEYC_MOUSEDOWN1_BORDER;
1265 break;
1266 case MOUSE_BUTTON_2:
1267 if (where == PANE)
1268 key = KEYC_MOUSEDOWN2_PANE;
1269 if (where == STATUS)
1270 key = KEYC_MOUSEDOWN2_STATUS;
1271 if (where == STATUS_LEFT)
1272 key = KEYC_MOUSEDOWN2_STATUS_LEFT;
1273 if (where == STATUS_RIGHT)
1274 key = KEYC_MOUSEDOWN2_STATUS_RIGHT;
1275 if (where == STATUS_DEFAULT)
1276 key = KEYC_MOUSEDOWN2_STATUS_DEFAULT;
1277 if (where == BORDER)
1278 key = KEYC_MOUSEDOWN2_BORDER;
1279 break;
1280 case MOUSE_BUTTON_3:
1281 if (where == PANE)
1282 key = KEYC_MOUSEDOWN3_PANE;
1283 if (where == STATUS)
1284 key = KEYC_MOUSEDOWN3_STATUS;
1285 if (where == STATUS_LEFT)
1286 key = KEYC_MOUSEDOWN3_STATUS_LEFT;
1287 if (where == STATUS_RIGHT)
1288 key = KEYC_MOUSEDOWN3_STATUS_RIGHT;
1289 if (where == STATUS_DEFAULT)
1290 key = KEYC_MOUSEDOWN3_STATUS_DEFAULT;
1291 if (where == BORDER)
1292 key = KEYC_MOUSEDOWN3_BORDER;
1293 break;
1294 case MOUSE_BUTTON_6:
1295 if (where == PANE)
1296 key = KEYC_MOUSEDOWN6_PANE;
1297 if (where == STATUS)
1298 key = KEYC_MOUSEDOWN6_STATUS;
1299 if (where == STATUS_LEFT)
1300 key = KEYC_MOUSEDOWN6_STATUS_LEFT;
1301 if (where == STATUS_RIGHT)
1302 key = KEYC_MOUSEDOWN6_STATUS_RIGHT;
1303 if (where == STATUS_DEFAULT)
1304 key = KEYC_MOUSEDOWN6_STATUS_DEFAULT;
1305 if (where == BORDER)
1306 key = KEYC_MOUSEDOWN6_BORDER;
1307 break;
1308 case MOUSE_BUTTON_7:
1309 if (where == PANE)
1310 key = KEYC_MOUSEDOWN7_PANE;
1311 if (where == STATUS)
1312 key = KEYC_MOUSEDOWN7_STATUS;
1313 if (where == STATUS_LEFT)
1314 key = KEYC_MOUSEDOWN7_STATUS_LEFT;
1315 if (where == STATUS_RIGHT)
1316 key = KEYC_MOUSEDOWN7_STATUS_RIGHT;
1317 if (where == STATUS_DEFAULT)
1318 key = KEYC_MOUSEDOWN7_STATUS_DEFAULT;
1319 if (where == BORDER)
1320 key = KEYC_MOUSEDOWN7_BORDER;
1321 break;
1322 case MOUSE_BUTTON_8:
1323 if (where == PANE)
1324 key = KEYC_MOUSEDOWN8_PANE;
1325 if (where == STATUS)
1326 key = KEYC_MOUSEDOWN8_STATUS;
1327 if (where == STATUS_LEFT)
1328 key = KEYC_MOUSEDOWN8_STATUS_LEFT;
1329 if (where == STATUS_RIGHT)
1330 key = KEYC_MOUSEDOWN8_STATUS_RIGHT;
1331 if (where == STATUS_DEFAULT)
1332 key = KEYC_MOUSEDOWN8_STATUS_DEFAULT;
1333 if (where == BORDER)
1334 key = KEYC_MOUSEDOWN8_BORDER;
1335 break;
1336 case MOUSE_BUTTON_9:
1337 if (where == PANE)
1338 key = KEYC_MOUSEDOWN9_PANE;
1339 if (where == STATUS)
1340 key = KEYC_MOUSEDOWN9_STATUS;
1341 if (where == STATUS_LEFT)
1342 key = KEYC_MOUSEDOWN9_STATUS_LEFT;
1343 if (where == STATUS_RIGHT)
1344 key = KEYC_MOUSEDOWN9_STATUS_RIGHT;
1345 if (where == STATUS_DEFAULT)
1346 key = KEYC_MOUSEDOWN9_STATUS_DEFAULT;
1347 if (where == BORDER)
1348 key = KEYC_MOUSEDOWN9_BORDER;
1349 break;
1350 case MOUSE_BUTTON_10:
1351 if (where == PANE)
1352 key = KEYC_MOUSEDOWN10_PANE;
1353 if (where == STATUS)
1354 key = KEYC_MOUSEDOWN10_STATUS;
1355 if (where == STATUS_LEFT)
1356 key = KEYC_MOUSEDOWN10_STATUS_LEFT;
1357 if (where == STATUS_RIGHT)
1358 key = KEYC_MOUSEDOWN10_STATUS_RIGHT;
1359 if (where == STATUS_DEFAULT)
1360 key = KEYC_MOUSEDOWN10_STATUS_DEFAULT;
1361 if (where == BORDER)
1362 key = KEYC_MOUSEDOWN10_BORDER;
1363 break;
1364 case MOUSE_BUTTON_11:
1365 if (where == PANE)
1366 key = KEYC_MOUSEDOWN11_PANE;
1367 if (where == STATUS)
1368 key = KEYC_MOUSEDOWN11_STATUS;
1369 if (where == STATUS_LEFT)
1370 key = KEYC_MOUSEDOWN11_STATUS_LEFT;
1371 if (where == STATUS_RIGHT)
1372 key = KEYC_MOUSEDOWN11_STATUS_RIGHT;
1373 if (where == STATUS_DEFAULT)
1374 key = KEYC_MOUSEDOWN11_STATUS_DEFAULT;
1375 if (where == BORDER)
1376 key = KEYC_MOUSEDOWN11_BORDER;
1377 break;
1379 break;
1380 case SECOND:
1381 switch (MOUSE_BUTTONS(b)) {
1382 case MOUSE_BUTTON_1:
1383 if (where == PANE)
1384 key = KEYC_SECONDCLICK1_PANE;
1385 if (where == STATUS)
1386 key = KEYC_SECONDCLICK1_STATUS;
1387 if (where == STATUS_LEFT)
1388 key = KEYC_SECONDCLICK1_STATUS_LEFT;
1389 if (where == STATUS_RIGHT)
1390 key = KEYC_SECONDCLICK1_STATUS_RIGHT;
1391 if (where == STATUS_DEFAULT)
1392 key = KEYC_SECONDCLICK1_STATUS_DEFAULT;
1393 if (where == BORDER)
1394 key = KEYC_SECONDCLICK1_BORDER;
1395 break;
1396 case MOUSE_BUTTON_2:
1397 if (where == PANE)
1398 key = KEYC_SECONDCLICK2_PANE;
1399 if (where == STATUS)
1400 key = KEYC_SECONDCLICK2_STATUS;
1401 if (where == STATUS_LEFT)
1402 key = KEYC_SECONDCLICK2_STATUS_LEFT;
1403 if (where == STATUS_RIGHT)
1404 key = KEYC_SECONDCLICK2_STATUS_RIGHT;
1405 if (where == STATUS_DEFAULT)
1406 key = KEYC_SECONDCLICK2_STATUS_DEFAULT;
1407 if (where == BORDER)
1408 key = KEYC_SECONDCLICK2_BORDER;
1409 break;
1410 case MOUSE_BUTTON_3:
1411 if (where == PANE)
1412 key = KEYC_SECONDCLICK3_PANE;
1413 if (where == STATUS)
1414 key = KEYC_SECONDCLICK3_STATUS;
1415 if (where == STATUS_LEFT)
1416 key = KEYC_SECONDCLICK3_STATUS_LEFT;
1417 if (where == STATUS_RIGHT)
1418 key = KEYC_SECONDCLICK3_STATUS_RIGHT;
1419 if (where == STATUS_DEFAULT)
1420 key = KEYC_SECONDCLICK3_STATUS_DEFAULT;
1421 if (where == BORDER)
1422 key = KEYC_SECONDCLICK3_BORDER;
1423 break;
1424 case MOUSE_BUTTON_6:
1425 if (where == PANE)
1426 key = KEYC_SECONDCLICK6_PANE;
1427 if (where == STATUS)
1428 key = KEYC_SECONDCLICK6_STATUS;
1429 if (where == STATUS_LEFT)
1430 key = KEYC_SECONDCLICK6_STATUS_LEFT;
1431 if (where == STATUS_RIGHT)
1432 key = KEYC_SECONDCLICK6_STATUS_RIGHT;
1433 if (where == STATUS_DEFAULT)
1434 key = KEYC_SECONDCLICK6_STATUS_DEFAULT;
1435 if (where == BORDER)
1436 key = KEYC_SECONDCLICK6_BORDER;
1437 break;
1438 case MOUSE_BUTTON_7:
1439 if (where == PANE)
1440 key = KEYC_SECONDCLICK7_PANE;
1441 if (where == STATUS)
1442 key = KEYC_SECONDCLICK7_STATUS;
1443 if (where == STATUS_LEFT)
1444 key = KEYC_SECONDCLICK7_STATUS_LEFT;
1445 if (where == STATUS_RIGHT)
1446 key = KEYC_SECONDCLICK7_STATUS_RIGHT;
1447 if (where == STATUS_DEFAULT)
1448 key = KEYC_SECONDCLICK7_STATUS_DEFAULT;
1449 if (where == BORDER)
1450 key = KEYC_SECONDCLICK7_BORDER;
1451 break;
1452 case MOUSE_BUTTON_8:
1453 if (where == PANE)
1454 key = KEYC_SECONDCLICK8_PANE;
1455 if (where == STATUS)
1456 key = KEYC_SECONDCLICK8_STATUS;
1457 if (where == STATUS_LEFT)
1458 key = KEYC_SECONDCLICK8_STATUS_LEFT;
1459 if (where == STATUS_RIGHT)
1460 key = KEYC_SECONDCLICK8_STATUS_RIGHT;
1461 if (where == STATUS_DEFAULT)
1462 key = KEYC_SECONDCLICK8_STATUS_DEFAULT;
1463 if (where == BORDER)
1464 key = KEYC_SECONDCLICK8_BORDER;
1465 break;
1466 case MOUSE_BUTTON_9:
1467 if (where == PANE)
1468 key = KEYC_SECONDCLICK9_PANE;
1469 if (where == STATUS)
1470 key = KEYC_SECONDCLICK9_STATUS;
1471 if (where == STATUS_LEFT)
1472 key = KEYC_SECONDCLICK9_STATUS_LEFT;
1473 if (where == STATUS_RIGHT)
1474 key = KEYC_SECONDCLICK9_STATUS_RIGHT;
1475 if (where == STATUS_DEFAULT)
1476 key = KEYC_SECONDCLICK9_STATUS_DEFAULT;
1477 if (where == BORDER)
1478 key = KEYC_SECONDCLICK9_BORDER;
1479 break;
1480 case MOUSE_BUTTON_10:
1481 if (where == PANE)
1482 key = KEYC_SECONDCLICK10_PANE;
1483 if (where == STATUS)
1484 key = KEYC_SECONDCLICK10_STATUS;
1485 if (where == STATUS_LEFT)
1486 key = KEYC_SECONDCLICK10_STATUS_LEFT;
1487 if (where == STATUS_RIGHT)
1488 key = KEYC_SECONDCLICK10_STATUS_RIGHT;
1489 if (where == STATUS_DEFAULT)
1490 key = KEYC_SECONDCLICK10_STATUS_DEFAULT;
1491 if (where == BORDER)
1492 key = KEYC_SECONDCLICK10_BORDER;
1493 break;
1494 case MOUSE_BUTTON_11:
1495 if (where == PANE)
1496 key = KEYC_SECONDCLICK11_PANE;
1497 if (where == STATUS)
1498 key = KEYC_SECONDCLICK11_STATUS;
1499 if (where == STATUS_LEFT)
1500 key = KEYC_SECONDCLICK11_STATUS_LEFT;
1501 if (where == STATUS_RIGHT)
1502 key = KEYC_SECONDCLICK11_STATUS_RIGHT;
1503 if (where == STATUS_DEFAULT)
1504 key = KEYC_SECONDCLICK11_STATUS_DEFAULT;
1505 if (where == BORDER)
1506 key = KEYC_SECONDCLICK11_BORDER;
1507 break;
1509 break;
1510 case DOUBLE:
1511 switch (MOUSE_BUTTONS(b)) {
1512 case MOUSE_BUTTON_1:
1513 if (where == PANE)
1514 key = KEYC_DOUBLECLICK1_PANE;
1515 if (where == STATUS)
1516 key = KEYC_DOUBLECLICK1_STATUS;
1517 if (where == STATUS_LEFT)
1518 key = KEYC_DOUBLECLICK1_STATUS_LEFT;
1519 if (where == STATUS_RIGHT)
1520 key = KEYC_DOUBLECLICK1_STATUS_RIGHT;
1521 if (where == STATUS_DEFAULT)
1522 key = KEYC_DOUBLECLICK1_STATUS_DEFAULT;
1523 if (where == BORDER)
1524 key = KEYC_DOUBLECLICK1_BORDER;
1525 break;
1526 case MOUSE_BUTTON_2:
1527 if (where == PANE)
1528 key = KEYC_DOUBLECLICK2_PANE;
1529 if (where == STATUS)
1530 key = KEYC_DOUBLECLICK2_STATUS;
1531 if (where == STATUS_LEFT)
1532 key = KEYC_DOUBLECLICK2_STATUS_LEFT;
1533 if (where == STATUS_RIGHT)
1534 key = KEYC_DOUBLECLICK2_STATUS_RIGHT;
1535 if (where == STATUS_DEFAULT)
1536 key = KEYC_DOUBLECLICK2_STATUS_DEFAULT;
1537 if (where == BORDER)
1538 key = KEYC_DOUBLECLICK2_BORDER;
1539 break;
1540 case MOUSE_BUTTON_3:
1541 if (where == PANE)
1542 key = KEYC_DOUBLECLICK3_PANE;
1543 if (where == STATUS)
1544 key = KEYC_DOUBLECLICK3_STATUS;
1545 if (where == STATUS_LEFT)
1546 key = KEYC_DOUBLECLICK3_STATUS_LEFT;
1547 if (where == STATUS_RIGHT)
1548 key = KEYC_DOUBLECLICK3_STATUS_RIGHT;
1549 if (where == STATUS_DEFAULT)
1550 key = KEYC_DOUBLECLICK3_STATUS_DEFAULT;
1551 if (where == BORDER)
1552 key = KEYC_DOUBLECLICK3_BORDER;
1553 break;
1554 case MOUSE_BUTTON_6:
1555 if (where == PANE)
1556 key = KEYC_DOUBLECLICK6_PANE;
1557 if (where == STATUS)
1558 key = KEYC_DOUBLECLICK6_STATUS;
1559 if (where == STATUS_LEFT)
1560 key = KEYC_DOUBLECLICK6_STATUS_LEFT;
1561 if (where == STATUS_RIGHT)
1562 key = KEYC_DOUBLECLICK6_STATUS_RIGHT;
1563 if (where == STATUS_DEFAULT)
1564 key = KEYC_DOUBLECLICK6_STATUS_DEFAULT;
1565 if (where == BORDER)
1566 key = KEYC_DOUBLECLICK6_BORDER;
1567 break;
1568 case MOUSE_BUTTON_7:
1569 if (where == PANE)
1570 key = KEYC_DOUBLECLICK7_PANE;
1571 if (where == STATUS)
1572 key = KEYC_DOUBLECLICK7_STATUS;
1573 if (where == STATUS_LEFT)
1574 key = KEYC_DOUBLECLICK7_STATUS_LEFT;
1575 if (where == STATUS_RIGHT)
1576 key = KEYC_DOUBLECLICK7_STATUS_RIGHT;
1577 if (where == STATUS_DEFAULT)
1578 key = KEYC_DOUBLECLICK7_STATUS_DEFAULT;
1579 if (where == BORDER)
1580 key = KEYC_DOUBLECLICK7_BORDER;
1581 break;
1582 case MOUSE_BUTTON_8:
1583 if (where == PANE)
1584 key = KEYC_DOUBLECLICK8_PANE;
1585 if (where == STATUS)
1586 key = KEYC_DOUBLECLICK8_STATUS;
1587 if (where == STATUS_LEFT)
1588 key = KEYC_DOUBLECLICK8_STATUS_LEFT;
1589 if (where == STATUS_RIGHT)
1590 key = KEYC_DOUBLECLICK8_STATUS_RIGHT;
1591 if (where == STATUS_DEFAULT)
1592 key = KEYC_DOUBLECLICK8_STATUS_DEFAULT;
1593 if (where == BORDER)
1594 key = KEYC_DOUBLECLICK8_BORDER;
1595 break;
1596 case MOUSE_BUTTON_9:
1597 if (where == PANE)
1598 key = KEYC_DOUBLECLICK9_PANE;
1599 if (where == STATUS)
1600 key = KEYC_DOUBLECLICK9_STATUS;
1601 if (where == STATUS_LEFT)
1602 key = KEYC_DOUBLECLICK9_STATUS_LEFT;
1603 if (where == STATUS_RIGHT)
1604 key = KEYC_DOUBLECLICK9_STATUS_RIGHT;
1605 if (where == STATUS_DEFAULT)
1606 key = KEYC_DOUBLECLICK9_STATUS_DEFAULT;
1607 if (where == BORDER)
1608 key = KEYC_DOUBLECLICK9_BORDER;
1609 break;
1610 case MOUSE_BUTTON_10:
1611 if (where == PANE)
1612 key = KEYC_DOUBLECLICK10_PANE;
1613 if (where == STATUS)
1614 key = KEYC_DOUBLECLICK10_STATUS;
1615 if (where == STATUS_LEFT)
1616 key = KEYC_DOUBLECLICK10_STATUS_LEFT;
1617 if (where == STATUS_RIGHT)
1618 key = KEYC_DOUBLECLICK10_STATUS_RIGHT;
1619 if (where == STATUS_DEFAULT)
1620 key = KEYC_DOUBLECLICK10_STATUS_DEFAULT;
1621 if (where == BORDER)
1622 key = KEYC_DOUBLECLICK10_BORDER;
1623 break;
1624 case MOUSE_BUTTON_11:
1625 if (where == PANE)
1626 key = KEYC_DOUBLECLICK11_PANE;
1627 if (where == STATUS)
1628 key = KEYC_DOUBLECLICK11_STATUS;
1629 if (where == STATUS_LEFT)
1630 key = KEYC_DOUBLECLICK11_STATUS_LEFT;
1631 if (where == STATUS_RIGHT)
1632 key = KEYC_DOUBLECLICK11_STATUS_RIGHT;
1633 if (where == STATUS_DEFAULT)
1634 key = KEYC_DOUBLECLICK11_STATUS_DEFAULT;
1635 if (where == BORDER)
1636 key = KEYC_DOUBLECLICK11_BORDER;
1637 break;
1639 break;
1640 case TRIPLE:
1641 switch (MOUSE_BUTTONS(b)) {
1642 case MOUSE_BUTTON_1:
1643 if (where == PANE)
1644 key = KEYC_TRIPLECLICK1_PANE;
1645 if (where == STATUS)
1646 key = KEYC_TRIPLECLICK1_STATUS;
1647 if (where == STATUS_LEFT)
1648 key = KEYC_TRIPLECLICK1_STATUS_LEFT;
1649 if (where == STATUS_RIGHT)
1650 key = KEYC_TRIPLECLICK1_STATUS_RIGHT;
1651 if (where == STATUS_DEFAULT)
1652 key = KEYC_TRIPLECLICK1_STATUS_DEFAULT;
1653 if (where == BORDER)
1654 key = KEYC_TRIPLECLICK1_BORDER;
1655 break;
1656 case MOUSE_BUTTON_2:
1657 if (where == PANE)
1658 key = KEYC_TRIPLECLICK2_PANE;
1659 if (where == STATUS)
1660 key = KEYC_TRIPLECLICK2_STATUS;
1661 if (where == STATUS_LEFT)
1662 key = KEYC_TRIPLECLICK2_STATUS_LEFT;
1663 if (where == STATUS_RIGHT)
1664 key = KEYC_TRIPLECLICK2_STATUS_RIGHT;
1665 if (where == STATUS_DEFAULT)
1666 key = KEYC_TRIPLECLICK2_STATUS_DEFAULT;
1667 if (where == BORDER)
1668 key = KEYC_TRIPLECLICK2_BORDER;
1669 break;
1670 case MOUSE_BUTTON_3:
1671 if (where == PANE)
1672 key = KEYC_TRIPLECLICK3_PANE;
1673 if (where == STATUS)
1674 key = KEYC_TRIPLECLICK3_STATUS;
1675 if (where == STATUS_LEFT)
1676 key = KEYC_TRIPLECLICK3_STATUS_LEFT;
1677 if (where == STATUS_RIGHT)
1678 key = KEYC_TRIPLECLICK3_STATUS_RIGHT;
1679 if (where == STATUS_DEFAULT)
1680 key = KEYC_TRIPLECLICK3_STATUS_DEFAULT;
1681 if (where == BORDER)
1682 key = KEYC_TRIPLECLICK3_BORDER;
1683 break;
1684 case MOUSE_BUTTON_6:
1685 if (where == PANE)
1686 key = KEYC_TRIPLECLICK6_PANE;
1687 if (where == STATUS)
1688 key = KEYC_TRIPLECLICK6_STATUS;
1689 if (where == STATUS_LEFT)
1690 key = KEYC_TRIPLECLICK6_STATUS_LEFT;
1691 if (where == STATUS_RIGHT)
1692 key = KEYC_TRIPLECLICK6_STATUS_RIGHT;
1693 if (where == STATUS_DEFAULT)
1694 key = KEYC_TRIPLECLICK6_STATUS_DEFAULT;
1695 if (where == BORDER)
1696 key = KEYC_TRIPLECLICK6_BORDER;
1697 break;
1698 case MOUSE_BUTTON_7:
1699 if (where == PANE)
1700 key = KEYC_TRIPLECLICK7_PANE;
1701 if (where == STATUS)
1702 key = KEYC_TRIPLECLICK7_STATUS;
1703 if (where == STATUS_LEFT)
1704 key = KEYC_TRIPLECLICK7_STATUS_LEFT;
1705 if (where == STATUS_RIGHT)
1706 key = KEYC_TRIPLECLICK7_STATUS_RIGHT;
1707 if (where == STATUS_DEFAULT)
1708 key = KEYC_TRIPLECLICK7_STATUS_DEFAULT;
1709 if (where == BORDER)
1710 key = KEYC_TRIPLECLICK7_BORDER;
1711 break;
1712 case MOUSE_BUTTON_8:
1713 if (where == PANE)
1714 key = KEYC_TRIPLECLICK8_PANE;
1715 if (where == STATUS)
1716 key = KEYC_TRIPLECLICK8_STATUS;
1717 if (where == STATUS_LEFT)
1718 key = KEYC_TRIPLECLICK8_STATUS_LEFT;
1719 if (where == STATUS_RIGHT)
1720 key = KEYC_TRIPLECLICK8_STATUS_RIGHT;
1721 if (where == STATUS_DEFAULT)
1722 key = KEYC_TRIPLECLICK8_STATUS_DEFAULT;
1723 if (where == BORDER)
1724 key = KEYC_TRIPLECLICK8_BORDER;
1725 break;
1726 case MOUSE_BUTTON_9:
1727 if (where == PANE)
1728 key = KEYC_TRIPLECLICK9_PANE;
1729 if (where == STATUS)
1730 key = KEYC_TRIPLECLICK9_STATUS;
1731 if (where == STATUS_LEFT)
1732 key = KEYC_TRIPLECLICK9_STATUS_LEFT;
1733 if (where == STATUS_RIGHT)
1734 key = KEYC_TRIPLECLICK9_STATUS_RIGHT;
1735 if (where == STATUS_DEFAULT)
1736 key = KEYC_TRIPLECLICK9_STATUS_DEFAULT;
1737 if (where == BORDER)
1738 key = KEYC_TRIPLECLICK9_BORDER;
1739 break;
1740 case MOUSE_BUTTON_10:
1741 if (where == PANE)
1742 key = KEYC_TRIPLECLICK10_PANE;
1743 if (where == STATUS)
1744 key = KEYC_TRIPLECLICK10_STATUS;
1745 if (where == STATUS_LEFT)
1746 key = KEYC_TRIPLECLICK10_STATUS_LEFT;
1747 if (where == STATUS_RIGHT)
1748 key = KEYC_TRIPLECLICK10_STATUS_RIGHT;
1749 if (where == STATUS_DEFAULT)
1750 key = KEYC_TRIPLECLICK10_STATUS_DEFAULT;
1751 if (where == BORDER)
1752 key = KEYC_TRIPLECLICK10_BORDER;
1753 break;
1754 case MOUSE_BUTTON_11:
1755 if (where == PANE)
1756 key = KEYC_TRIPLECLICK11_PANE;
1757 if (where == STATUS)
1758 key = KEYC_TRIPLECLICK11_STATUS;
1759 if (where == STATUS_LEFT)
1760 key = KEYC_TRIPLECLICK11_STATUS_LEFT;
1761 if (where == STATUS_RIGHT)
1762 key = KEYC_TRIPLECLICK11_STATUS_RIGHT;
1763 if (where == STATUS_DEFAULT)
1764 key = KEYC_TRIPLECLICK11_STATUS_DEFAULT;
1765 if (where == BORDER)
1766 key = KEYC_TRIPLECLICK11_BORDER;
1767 break;
1769 break;
1771 if (key == KEYC_UNKNOWN)
1772 return (KEYC_UNKNOWN);
1774 out:
1775 /* Apply modifiers if any. */
1776 if (b & MOUSE_MASK_META)
1777 key |= KEYC_META;
1778 if (b & MOUSE_MASK_CTRL)
1779 key |= KEYC_CTRL;
1780 if (b & MOUSE_MASK_SHIFT)
1781 key |= KEYC_SHIFT;
1783 if (log_get_level() != 0)
1784 log_debug("mouse key is %s", key_string_lookup_key (key, 1));
1785 return (key);
1788 /* Is this a bracket paste key? */
1789 static int
1790 server_client_is_bracket_pasting(struct client *c, key_code key)
1792 if (key == KEYC_PASTE_START) {
1793 c->flags |= CLIENT_BRACKETPASTING;
1794 log_debug("%s: bracket paste on", c->name);
1795 return (1);
1798 if (key == KEYC_PASTE_END) {
1799 c->flags &= ~CLIENT_BRACKETPASTING;
1800 log_debug("%s: bracket paste off", c->name);
1801 return (1);
1804 return !!(c->flags & CLIENT_BRACKETPASTING);
1807 /* Is this fast enough to probably be a paste? */
1808 static int
1809 server_client_assume_paste(struct session *s)
1811 struct timeval tv;
1812 int t;
1814 if ((t = options_get_number(s->options, "assume-paste-time")) == 0)
1815 return (0);
1817 timersub(&s->activity_time, &s->last_activity_time, &tv);
1818 if (tv.tv_sec == 0 && tv.tv_usec < t * 1000) {
1819 log_debug("session %s pasting (flag %d)", s->name,
1820 !!(s->flags & SESSION_PASTING));
1821 if (s->flags & SESSION_PASTING)
1822 return (1);
1823 s->flags |= SESSION_PASTING;
1824 return (0);
1826 log_debug("session %s not pasting", s->name);
1827 s->flags &= ~SESSION_PASTING;
1828 return (0);
1831 /* Has the latest client changed? */
1832 static void
1833 server_client_update_latest(struct client *c)
1835 struct window *w;
1837 if (c->session == NULL)
1838 return;
1839 w = c->session->curw->window;
1841 if (w->latest == c)
1842 return;
1843 w->latest = c;
1845 if (options_get_number(w->options, "window-size") == WINDOW_SIZE_LATEST)
1846 recalculate_size(w, 0);
1848 notify_client("client-active", c);
1852 * Handle data key input from client. This owns and can modify the key event it
1853 * is given and is responsible for freeing it.
1855 static enum cmd_retval
1856 server_client_key_callback(struct cmdq_item *item, void *data)
1858 struct client *c = cmdq_get_client(item);
1859 struct key_event *event = data;
1860 key_code key = event->key;
1861 struct mouse_event *m = &event->m;
1862 struct session *s = c->session;
1863 struct winlink *wl;
1864 struct window_pane *wp;
1865 struct window_mode_entry *wme;
1866 struct timeval tv;
1867 struct key_table *table, *first;
1868 struct key_binding *bd;
1869 int xtimeout, flags;
1870 struct cmd_find_state fs;
1871 key_code key0, prefix, prefix2;
1873 /* Check the client is good to accept input. */
1874 if (s == NULL || (c->flags & CLIENT_UNATTACHEDFLAGS))
1875 goto out;
1876 wl = s->curw;
1878 /* Update the activity timer. */
1879 if (gettimeofday(&c->activity_time, NULL) != 0)
1880 fatal("gettimeofday failed");
1881 session_update_activity(s, &c->activity_time);
1883 /* Check for mouse keys. */
1884 m->valid = 0;
1885 if (key == KEYC_MOUSE || key == KEYC_DOUBLECLICK) {
1886 if (c->flags & CLIENT_READONLY)
1887 goto out;
1888 key = server_client_check_mouse(c, event);
1889 if (key == KEYC_UNKNOWN)
1890 goto out;
1892 m->valid = 1;
1893 m->key = key;
1896 * Mouse drag is in progress, so fire the callback (now that
1897 * the mouse event is valid).
1899 if ((key & KEYC_MASK_KEY) == KEYC_DRAGGING) {
1900 c->tty.mouse_drag_update(c, m);
1901 goto out;
1903 event->key = key;
1906 /* Find affected pane. */
1907 if (!KEYC_IS_MOUSE(key) || cmd_find_from_mouse(&fs, m, 0) != 0)
1908 cmd_find_from_client(&fs, c, 0);
1909 wp = fs.wp;
1911 /* Forward mouse keys if disabled. */
1912 if (KEYC_IS_MOUSE(key) && !options_get_number(s->options, "mouse"))
1913 goto forward_key;
1915 /* Forward if bracket pasting. */
1916 if (server_client_is_bracket_pasting(c, key))
1917 goto forward_key;
1919 /* Treat everything as a regular key when pasting is detected. */
1920 if (!KEYC_IS_MOUSE(key) &&
1921 (~key & KEYC_SENT) &&
1922 server_client_assume_paste(s))
1923 goto forward_key;
1926 * Work out the current key table. If the pane is in a mode, use
1927 * the mode table instead of the default key table.
1929 if (server_client_is_default_key_table(c, c->keytable) &&
1930 wp != NULL &&
1931 (wme = TAILQ_FIRST(&wp->modes)) != NULL &&
1932 wme->mode->key_table != NULL)
1933 table = key_bindings_get_table(wme->mode->key_table(wme), 1);
1934 else
1935 table = c->keytable;
1936 first = table;
1938 table_changed:
1940 * The prefix always takes precedence and forces a switch to the prefix
1941 * table, unless we are already there.
1943 prefix = (key_code)options_get_number(s->options, "prefix");
1944 prefix2 = (key_code)options_get_number(s->options, "prefix2");
1945 key0 = (key & (KEYC_MASK_KEY|KEYC_MASK_MODIFIERS));
1946 if ((key0 == (prefix & (KEYC_MASK_KEY|KEYC_MASK_MODIFIERS)) ||
1947 key0 == (prefix2 & (KEYC_MASK_KEY|KEYC_MASK_MODIFIERS))) &&
1948 strcmp(table->name, "prefix") != 0) {
1949 server_client_set_key_table(c, "prefix");
1950 server_status_client(c);
1951 goto out;
1953 flags = c->flags;
1955 try_again:
1956 /* Log key table. */
1957 if (wp == NULL)
1958 log_debug("key table %s (no pane)", table->name);
1959 else
1960 log_debug("key table %s (pane %%%u)", table->name, wp->id);
1961 if (c->flags & CLIENT_REPEAT)
1962 log_debug("currently repeating");
1964 /* Try to see if there is a key binding in the current table. */
1965 bd = key_bindings_get(table, key0);
1966 if (bd != NULL) {
1968 * Key was matched in this table. If currently repeating but a
1969 * non-repeating binding was found, stop repeating and try
1970 * again in the root table.
1972 if ((c->flags & CLIENT_REPEAT) &&
1973 (~bd->flags & KEY_BINDING_REPEAT)) {
1974 log_debug("found in key table %s (not repeating)",
1975 table->name);
1976 server_client_set_key_table(c, NULL);
1977 first = table = c->keytable;
1978 c->flags &= ~CLIENT_REPEAT;
1979 server_status_client(c);
1980 goto table_changed;
1982 log_debug("found in key table %s", table->name);
1985 * Take a reference to this table to make sure the key binding
1986 * doesn't disappear.
1988 table->references++;
1991 * If this is a repeating key, start the timer. Otherwise reset
1992 * the client back to the root table.
1994 xtimeout = options_get_number(s->options, "repeat-time");
1995 if (xtimeout != 0 && (bd->flags & KEY_BINDING_REPEAT)) {
1996 c->flags |= CLIENT_REPEAT;
1998 tv.tv_sec = xtimeout / 1000;
1999 tv.tv_usec = (xtimeout % 1000) * 1000L;
2000 evtimer_del(&c->repeat_timer);
2001 evtimer_add(&c->repeat_timer, &tv);
2002 } else {
2003 c->flags &= ~CLIENT_REPEAT;
2004 server_client_set_key_table(c, NULL);
2006 server_status_client(c);
2008 /* Execute the key binding. */
2009 key_bindings_dispatch(bd, item, c, event, &fs);
2010 key_bindings_unref_table(table);
2011 goto out;
2015 * No match, try the ANY key.
2017 if (key0 != KEYC_ANY) {
2018 key0 = KEYC_ANY;
2019 goto try_again;
2023 * No match in this table. If not in the root table or if repeating,
2024 * switch the client back to the root table and try again.
2026 log_debug("not found in key table %s", table->name);
2027 if (!server_client_is_default_key_table(c, table) ||
2028 (c->flags & CLIENT_REPEAT)) {
2029 log_debug("trying in root table");
2030 server_client_set_key_table(c, NULL);
2031 table = c->keytable;
2032 if (c->flags & CLIENT_REPEAT)
2033 first = table;
2034 c->flags &= ~CLIENT_REPEAT;
2035 server_status_client(c);
2036 goto table_changed;
2040 * No match in the root table either. If this wasn't the first table
2041 * tried, don't pass the key to the pane.
2043 if (first != table && (~flags & CLIENT_REPEAT)) {
2044 server_client_set_key_table(c, NULL);
2045 server_status_client(c);
2046 goto out;
2049 forward_key:
2050 if (c->flags & CLIENT_READONLY)
2051 goto out;
2052 if (wp != NULL)
2053 window_pane_key(wp, c, s, wl, key, m);
2055 out:
2056 if (s != NULL && key != KEYC_FOCUS_OUT)
2057 server_client_update_latest(c);
2058 free(event);
2059 return (CMD_RETURN_NORMAL);
2062 /* Handle a key event. */
2064 server_client_handle_key(struct client *c, struct key_event *event)
2066 struct session *s = c->session;
2067 struct cmdq_item *item;
2069 /* Check the client is good to accept input. */
2070 if (s == NULL || (c->flags & CLIENT_UNATTACHEDFLAGS))
2071 return (0);
2074 * Key presses in overlay mode and the command prompt are a special
2075 * case. The queue might be blocked so they need to be processed
2076 * immediately rather than queued.
2078 if (~c->flags & CLIENT_READONLY) {
2079 if (c->message_string != NULL) {
2080 if (c->message_ignore_keys)
2081 return (0);
2082 status_message_clear(c);
2084 if (c->overlay_key != NULL) {
2085 switch (c->overlay_key(c, c->overlay_data, event)) {
2086 case 0:
2087 return (0);
2088 case 1:
2089 server_client_clear_overlay(c);
2090 return (0);
2093 server_client_clear_overlay(c);
2094 if (c->prompt_string != NULL) {
2095 if (status_prompt_key(c, event->key) == 0)
2096 return (0);
2101 * Add the key to the queue so it happens after any commands queued by
2102 * previous keys.
2104 item = cmdq_get_callback(server_client_key_callback, event);
2105 cmdq_append(c, item);
2106 return (1);
2109 /* Client functions that need to happen every loop. */
2110 void
2111 server_client_loop(void)
2113 struct client *c;
2114 struct window *w;
2115 struct window_pane *wp;
2117 /* Check for window resize. This is done before redrawing. */
2118 RB_FOREACH(w, windows, &windows)
2119 server_client_check_window_resize(w);
2121 /* Check clients. */
2122 TAILQ_FOREACH(c, &clients, entry) {
2123 server_client_check_exit(c);
2124 if (c->session != NULL) {
2125 server_client_check_modes(c);
2126 server_client_check_redraw(c);
2127 server_client_reset_state(c);
2132 * Any windows will have been redrawn as part of clients, so clear
2133 * their flags now.
2135 RB_FOREACH(w, windows, &windows) {
2136 TAILQ_FOREACH(wp, &w->panes, entry) {
2137 if (wp->fd != -1) {
2138 server_client_check_pane_resize(wp);
2139 server_client_check_pane_buffer(wp);
2141 wp->flags &= ~PANE_REDRAW;
2143 check_window_name(w);
2147 /* Check if window needs to be resized. */
2148 static void
2149 server_client_check_window_resize(struct window *w)
2151 struct winlink *wl;
2153 if (~w->flags & WINDOW_RESIZE)
2154 return;
2156 TAILQ_FOREACH(wl, &w->winlinks, wentry) {
2157 if (wl->session->attached != 0 && wl->session->curw == wl)
2158 break;
2160 if (wl == NULL)
2161 return;
2163 log_debug("%s: resizing window @%u", __func__, w->id);
2164 resize_window(w, w->new_sx, w->new_sy, w->new_xpixel, w->new_ypixel);
2167 /* Resize timer event. */
2168 static void
2169 server_client_resize_timer(__unused int fd, __unused short events, void *data)
2171 struct window_pane *wp = data;
2173 log_debug("%s: %%%u resize timer expired", __func__, wp->id);
2174 evtimer_del(&wp->resize_timer);
2177 /* Check if pane should be resized. */
2178 static void
2179 server_client_check_pane_resize(struct window_pane *wp)
2181 struct window_pane_resize *r;
2182 struct window_pane_resize *r1;
2183 struct window_pane_resize *first;
2184 struct window_pane_resize *last;
2185 struct timeval tv = { .tv_usec = 250000 };
2187 if (TAILQ_EMPTY(&wp->resize_queue))
2188 return;
2190 if (!event_initialized(&wp->resize_timer))
2191 evtimer_set(&wp->resize_timer, server_client_resize_timer, wp);
2192 if (evtimer_pending(&wp->resize_timer, NULL))
2193 return;
2195 log_debug("%s: %%%u needs to be resized", __func__, wp->id);
2196 TAILQ_FOREACH(r, &wp->resize_queue, entry) {
2197 log_debug("queued resize: %ux%u -> %ux%u", r->osx, r->osy,
2198 r->sx, r->sy);
2202 * There are three cases that matter:
2204 * - Only one resize. It can just be applied.
2206 * - Multiple resizes and the ending size is different from the
2207 * starting size. We can discard all resizes except the most recent.
2209 * - Multiple resizes and the ending size is the same as the starting
2210 * size. We must resize at least twice to force the application to
2211 * redraw. So apply the first and leave the last on the queue for
2212 * next time.
2214 first = TAILQ_FIRST(&wp->resize_queue);
2215 last = TAILQ_LAST(&wp->resize_queue, window_pane_resizes);
2216 if (first == last) {
2217 /* Only one resize. */
2218 window_pane_send_resize(wp, first->sx, first->sy);
2219 TAILQ_REMOVE(&wp->resize_queue, first, entry);
2220 free(first);
2221 } else if (last->sx != first->osx || last->sy != first->osy) {
2222 /* Multiple resizes ending up with a different size. */
2223 window_pane_send_resize(wp, last->sx, last->sy);
2224 TAILQ_FOREACH_SAFE(r, &wp->resize_queue, entry, r1) {
2225 TAILQ_REMOVE(&wp->resize_queue, r, entry);
2226 free(r);
2228 } else {
2230 * Multiple resizes ending up with the same size. There will
2231 * not be more than one to the same size in succession so we
2232 * can just use the last-but-one on the list and leave the last
2233 * for later. We reduce the time until the next check to avoid
2234 * a long delay between the resizes.
2236 r = TAILQ_PREV(last, window_pane_resizes, entry);
2237 window_pane_send_resize(wp, r->sx, r->sy);
2238 TAILQ_FOREACH_SAFE(r, &wp->resize_queue, entry, r1) {
2239 if (r == last)
2240 break;
2241 TAILQ_REMOVE(&wp->resize_queue, r, entry);
2242 free(r);
2244 tv.tv_usec = 10000;
2246 evtimer_add(&wp->resize_timer, &tv);
2249 /* Check pane buffer size. */
2250 static void
2251 server_client_check_pane_buffer(struct window_pane *wp)
2253 struct evbuffer *evb = wp->event->input;
2254 size_t minimum;
2255 struct client *c;
2256 struct window_pane_offset *wpo;
2257 int off = 1, flag;
2258 u_int attached_clients = 0;
2259 size_t new_size;
2262 * Work out the minimum used size. This is the most that can be removed
2263 * from the buffer.
2265 minimum = wp->offset.used;
2266 if (wp->pipe_fd != -1 && wp->pipe_offset.used < minimum)
2267 minimum = wp->pipe_offset.used;
2268 TAILQ_FOREACH(c, &clients, entry) {
2269 if (c->session == NULL)
2270 continue;
2271 attached_clients++;
2273 if (~c->flags & CLIENT_CONTROL) {
2274 off = 0;
2275 continue;
2277 wpo = control_pane_offset(c, wp, &flag);
2278 if (wpo == NULL) {
2279 if (!flag)
2280 off = 0;
2281 continue;
2283 if (!flag)
2284 off = 0;
2286 window_pane_get_new_data(wp, wpo, &new_size);
2287 log_debug("%s: %s has %zu bytes used and %zu left for %%%u",
2288 __func__, c->name, wpo->used - wp->base_offset, new_size,
2289 wp->id);
2290 if (wpo->used < minimum)
2291 minimum = wpo->used;
2293 if (attached_clients == 0)
2294 off = 0;
2295 minimum -= wp->base_offset;
2296 if (minimum == 0)
2297 goto out;
2299 /* Drain the buffer. */
2300 log_debug("%s: %%%u has %zu minimum (of %zu) bytes used", __func__,
2301 wp->id, minimum, EVBUFFER_LENGTH(evb));
2302 evbuffer_drain(evb, minimum);
2305 * Adjust the base offset. If it would roll over, all the offsets into
2306 * the buffer need to be adjusted.
2308 if (wp->base_offset > SIZE_MAX - minimum) {
2309 log_debug("%s: %%%u base offset has wrapped", __func__, wp->id);
2310 wp->offset.used -= wp->base_offset;
2311 if (wp->pipe_fd != -1)
2312 wp->pipe_offset.used -= wp->base_offset;
2313 TAILQ_FOREACH(c, &clients, entry) {
2314 if (c->session == NULL || (~c->flags & CLIENT_CONTROL))
2315 continue;
2316 wpo = control_pane_offset(c, wp, &flag);
2317 if (wpo != NULL && !flag)
2318 wpo->used -= wp->base_offset;
2320 wp->base_offset = minimum;
2321 } else
2322 wp->base_offset += minimum;
2324 out:
2326 * If there is data remaining, and there are no clients able to consume
2327 * it, do not read any more. This is true when there are attached
2328 * clients, all of which are control clients which are not able to
2329 * accept any more data.
2331 log_debug("%s: pane %%%u is %s", __func__, wp->id, off ? "off" : "on");
2332 if (off)
2333 bufferevent_disable(wp->event, EV_READ);
2334 else
2335 bufferevent_enable(wp->event, EV_READ);
2339 * Update cursor position and mode settings. The scroll region and attributes
2340 * are cleared when idle (waiting for an event) as this is the most likely time
2341 * a user may interrupt tmux, for example with ~^Z in ssh(1). This is a
2342 * compromise between excessive resets and likelihood of an interrupt.
2344 * tty_region/tty_reset/tty_update_mode already take care of not resetting
2345 * things that are already in their default state.
2347 static void
2348 server_client_reset_state(struct client *c)
2350 struct tty *tty = &c->tty;
2351 struct window *w = c->session->curw->window;
2352 struct window_pane *wp = server_client_get_pane(c), *loop;
2353 struct screen *s = NULL;
2354 struct options *oo = c->session->options;
2355 int mode = 0, cursor, flags, n;
2356 u_int cx = 0, cy = 0, ox, oy, sx, sy;
2358 if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
2359 return;
2361 /* Disable the block flag. */
2362 flags = (tty->flags & TTY_BLOCK);
2363 tty->flags &= ~TTY_BLOCK;
2365 /* Get mode from overlay if any, else from screen. */
2366 if (c->overlay_draw != NULL) {
2367 if (c->overlay_mode != NULL)
2368 s = c->overlay_mode(c, c->overlay_data, &cx, &cy);
2369 } else
2370 s = wp->screen;
2371 if (s != NULL)
2372 mode = s->mode;
2373 if (log_get_level() != 0) {
2374 log_debug("%s: client %s mode %s", __func__, c->name,
2375 screen_mode_to_string(mode));
2378 /* Reset region and margin. */
2379 tty_region_off(tty);
2380 tty_margin_off(tty);
2382 /* Move cursor to pane cursor and offset. */
2383 if (c->prompt_string != NULL) {
2384 n = options_get_number(c->session->options, "status-position");
2385 if (n == 0)
2386 cy = 0;
2387 else {
2388 n = status_line_size(c);
2389 if (n == 0)
2390 cy = tty->sy - 1;
2391 else
2392 cy = tty->sy - n;
2394 cx = c->prompt_cursor;
2395 mode &= ~MODE_CURSOR;
2396 } else if (c->overlay_draw == NULL) {
2397 cursor = 0;
2398 tty_window_offset(tty, &ox, &oy, &sx, &sy);
2399 if (wp->xoff + s->cx >= ox && wp->xoff + s->cx <= ox + sx &&
2400 wp->yoff + s->cy >= oy && wp->yoff + s->cy <= oy + sy) {
2401 cursor = 1;
2403 cx = wp->xoff + s->cx - ox;
2404 cy = wp->yoff + s->cy - oy;
2406 if (status_at_line(c) == 0)
2407 cy += status_line_size(c);
2409 if (!cursor)
2410 mode &= ~MODE_CURSOR;
2412 log_debug("%s: cursor to %u,%u", __func__, cx, cy);
2413 tty_cursor(tty, cx, cy);
2416 * Set mouse mode if requested. To support dragging, always use button
2417 * mode.
2419 if (options_get_number(oo, "mouse")) {
2420 if (c->overlay_draw == NULL) {
2421 mode &= ~ALL_MOUSE_MODES;
2422 TAILQ_FOREACH(loop, &w->panes, entry) {
2423 if (loop->screen->mode & MODE_MOUSE_ALL)
2424 mode |= MODE_MOUSE_ALL;
2427 if (~mode & MODE_MOUSE_ALL)
2428 mode |= MODE_MOUSE_BUTTON;
2431 /* Clear bracketed paste mode if at the prompt. */
2432 if (c->overlay_draw == NULL && c->prompt_string != NULL)
2433 mode &= ~MODE_BRACKETPASTE;
2435 /* Set the terminal mode and reset attributes. */
2436 tty_update_mode(tty, mode, s);
2437 tty_reset(tty);
2439 /* All writing must be done, send a sync end (if it was started). */
2440 tty_sync_end(tty);
2441 tty->flags |= flags;
2444 /* Repeat time callback. */
2445 static void
2446 server_client_repeat_timer(__unused int fd, __unused short events, void *data)
2448 struct client *c = data;
2450 if (c->flags & CLIENT_REPEAT) {
2451 server_client_set_key_table(c, NULL);
2452 c->flags &= ~CLIENT_REPEAT;
2453 server_status_client(c);
2457 /* Double-click callback. */
2458 static void
2459 server_client_click_timer(__unused int fd, __unused short events, void *data)
2461 struct client *c = data;
2462 struct key_event *event;
2464 log_debug("click timer expired");
2466 if (c->flags & CLIENT_TRIPLECLICK) {
2468 * Waiting for a third click that hasn't happened, so this must
2469 * have been a double click.
2471 event = xmalloc(sizeof *event);
2472 event->key = KEYC_DOUBLECLICK;
2473 memcpy(&event->m, &c->click_event, sizeof event->m);
2474 if (!server_client_handle_key(c, event))
2475 free(event);
2477 c->flags &= ~(CLIENT_DOUBLECLICK|CLIENT_TRIPLECLICK);
2480 /* Check if client should be exited. */
2481 static void
2482 server_client_check_exit(struct client *c)
2484 struct client_file *cf;
2485 const char *name = c->exit_session;
2486 char *data;
2487 size_t size, msize;
2489 if (c->flags & (CLIENT_DEAD|CLIENT_EXITED))
2490 return;
2491 if (~c->flags & CLIENT_EXIT)
2492 return;
2494 if (c->flags & CLIENT_CONTROL) {
2495 control_discard(c);
2496 if (!control_all_done(c))
2497 return;
2499 RB_FOREACH(cf, client_files, &c->files) {
2500 if (EVBUFFER_LENGTH(cf->buffer) != 0)
2501 return;
2503 c->flags |= CLIENT_EXITED;
2505 switch (c->exit_type) {
2506 case CLIENT_EXIT_RETURN:
2507 if (c->exit_message != NULL)
2508 msize = strlen(c->exit_message) + 1;
2509 else
2510 msize = 0;
2511 size = (sizeof c->retval) + msize;
2512 data = xmalloc(size);
2513 memcpy(data, &c->retval, sizeof c->retval);
2514 if (c->exit_message != NULL)
2515 memcpy(data + sizeof c->retval, c->exit_message, msize);
2516 proc_send(c->peer, MSG_EXIT, -1, data, size);
2517 free(data);
2518 break;
2519 case CLIENT_EXIT_SHUTDOWN:
2520 proc_send(c->peer, MSG_SHUTDOWN, -1, NULL, 0);
2521 break;
2522 case CLIENT_EXIT_DETACH:
2523 proc_send(c->peer, c->exit_msgtype, -1, name, strlen(name) + 1);
2524 break;
2526 free(c->exit_session);
2527 free(c->exit_message);
2530 /* Redraw timer callback. */
2531 static void
2532 server_client_redraw_timer(__unused int fd, __unused short events,
2533 __unused void *data)
2535 log_debug("redraw timer fired");
2539 * Check if modes need to be updated. Only modes in the current window are
2540 * updated and it is done when the status line is redrawn.
2542 static void
2543 server_client_check_modes(struct client *c)
2545 struct window *w = c->session->curw->window;
2546 struct window_pane *wp;
2547 struct window_mode_entry *wme;
2549 if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
2550 return;
2551 if (~c->flags & CLIENT_REDRAWSTATUS)
2552 return;
2553 TAILQ_FOREACH(wp, &w->panes, entry) {
2554 wme = TAILQ_FIRST(&wp->modes);
2555 if (wme != NULL && wme->mode->update != NULL)
2556 wme->mode->update(wme);
2560 /* Check for client redraws. */
2561 static void
2562 server_client_check_redraw(struct client *c)
2564 struct session *s = c->session;
2565 struct tty *tty = &c->tty;
2566 struct window *w = c->session->curw->window;
2567 struct window_pane *wp;
2568 int needed, flags, mode = tty->mode, new_flags = 0;
2569 int redraw;
2570 u_int bit = 0;
2571 struct timeval tv = { .tv_usec = 1000 };
2572 static struct event ev;
2573 size_t left;
2575 if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
2576 return;
2577 if (c->flags & CLIENT_ALLREDRAWFLAGS) {
2578 log_debug("%s: redraw%s%s%s%s%s", c->name,
2579 (c->flags & CLIENT_REDRAWWINDOW) ? " window" : "",
2580 (c->flags & CLIENT_REDRAWSTATUS) ? " status" : "",
2581 (c->flags & CLIENT_REDRAWBORDERS) ? " borders" : "",
2582 (c->flags & CLIENT_REDRAWOVERLAY) ? " overlay" : "",
2583 (c->flags & CLIENT_REDRAWPANES) ? " panes" : "");
2587 * If there is outstanding data, defer the redraw until it has been
2588 * consumed. We can just add a timer to get out of the event loop and
2589 * end up back here.
2591 needed = 0;
2592 if (c->flags & CLIENT_ALLREDRAWFLAGS)
2593 needed = 1;
2594 else {
2595 TAILQ_FOREACH(wp, &w->panes, entry) {
2596 if (wp->flags & PANE_REDRAW) {
2597 needed = 1;
2598 break;
2601 if (needed)
2602 new_flags |= CLIENT_REDRAWPANES;
2604 if (needed && (left = EVBUFFER_LENGTH(tty->out)) != 0) {
2605 log_debug("%s: redraw deferred (%zu left)", c->name, left);
2606 if (!evtimer_initialized(&ev))
2607 evtimer_set(&ev, server_client_redraw_timer, NULL);
2608 if (!evtimer_pending(&ev, NULL)) {
2609 log_debug("redraw timer started");
2610 evtimer_add(&ev, &tv);
2613 if (~c->flags & CLIENT_REDRAWWINDOW) {
2614 TAILQ_FOREACH(wp, &w->panes, entry) {
2615 if (wp->flags & PANE_REDRAW) {
2616 log_debug("%s: pane %%%u needs redraw",
2617 c->name, wp->id);
2618 c->redraw_panes |= (1 << bit);
2620 if (++bit == 64) {
2622 * If more that 64 panes, give up and
2623 * just redraw the window.
2625 new_flags &= CLIENT_REDRAWPANES;
2626 new_flags |= CLIENT_REDRAWWINDOW;
2627 break;
2630 if (c->redraw_panes != 0)
2631 c->flags |= CLIENT_REDRAWPANES;
2633 c->flags |= new_flags;
2634 return;
2635 } else if (needed)
2636 log_debug("%s: redraw needed", c->name);
2638 flags = tty->flags & (TTY_BLOCK|TTY_FREEZE|TTY_NOCURSOR);
2639 tty->flags = (tty->flags & ~(TTY_BLOCK|TTY_FREEZE))|TTY_NOCURSOR;
2641 if (~c->flags & CLIENT_REDRAWWINDOW) {
2643 * If not redrawing the entire window, check whether each pane
2644 * needs to be redrawn.
2646 TAILQ_FOREACH(wp, &w->panes, entry) {
2647 redraw = 0;
2648 if (wp->flags & PANE_REDRAW)
2649 redraw = 1;
2650 else if (c->flags & CLIENT_REDRAWPANES)
2651 redraw = !!(c->redraw_panes & (1 << bit));
2652 bit++;
2653 if (!redraw)
2654 continue;
2655 log_debug("%s: redrawing pane %%%u", __func__, wp->id);
2656 screen_redraw_pane(c, wp);
2658 c->redraw_panes = 0;
2659 c->flags &= ~CLIENT_REDRAWPANES;
2662 if (c->flags & CLIENT_ALLREDRAWFLAGS) {
2663 if (options_get_number(s->options, "set-titles")) {
2664 server_client_set_title(c);
2665 server_client_set_path(c);
2667 screen_redraw_screen(c);
2670 tty->flags = (tty->flags & ~TTY_NOCURSOR)|(flags & TTY_NOCURSOR);
2671 tty_update_mode(tty, mode, NULL);
2672 tty->flags = (tty->flags & ~(TTY_BLOCK|TTY_FREEZE|TTY_NOCURSOR))|flags;
2674 c->flags &= ~(CLIENT_ALLREDRAWFLAGS|CLIENT_STATUSFORCE);
2676 if (needed) {
2678 * We would have deferred the redraw unless the output buffer
2679 * was empty, so we can record how many bytes the redraw
2680 * generated.
2682 c->redraw = EVBUFFER_LENGTH(tty->out);
2683 log_debug("%s: redraw added %zu bytes", c->name, c->redraw);
2687 /* Set client title. */
2688 static void
2689 server_client_set_title(struct client *c)
2691 struct session *s = c->session;
2692 const char *template;
2693 char *title;
2694 struct format_tree *ft;
2696 template = options_get_string(s->options, "set-titles-string");
2698 ft = format_create(c, NULL, FORMAT_NONE, 0);
2699 format_defaults(ft, c, NULL, NULL, NULL);
2701 title = format_expand_time(ft, template);
2702 if (c->title == NULL || strcmp(title, c->title) != 0) {
2703 free(c->title);
2704 c->title = xstrdup(title);
2705 tty_set_title(&c->tty, c->title);
2707 free(title);
2709 format_free(ft);
2712 /* Set client path. */
2713 static void
2714 server_client_set_path(struct client *c)
2716 struct session *s = c->session;
2717 const char *path;
2719 if (s->curw == NULL)
2720 return;
2721 if (s->curw->window->active->base.path == NULL)
2722 path = "";
2723 else
2724 path = s->curw->window->active->base.path;
2725 if (c->path == NULL || strcmp(path, c->path) != 0) {
2726 free(c->path);
2727 c->path = xstrdup(path);
2728 tty_set_path(&c->tty, c->path);
2732 /* Dispatch message from client. */
2733 static void
2734 server_client_dispatch(struct imsg *imsg, void *arg)
2736 struct client *c = arg;
2737 ssize_t datalen;
2738 struct session *s;
2740 if (c->flags & CLIENT_DEAD)
2741 return;
2743 if (imsg == NULL) {
2744 server_client_lost(c);
2745 return;
2748 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
2750 switch (imsg->hdr.type) {
2751 case MSG_IDENTIFY_CLIENTPID:
2752 case MSG_IDENTIFY_CWD:
2753 case MSG_IDENTIFY_ENVIRON:
2754 case MSG_IDENTIFY_FEATURES:
2755 case MSG_IDENTIFY_FLAGS:
2756 case MSG_IDENTIFY_LONGFLAGS:
2757 case MSG_IDENTIFY_STDIN:
2758 case MSG_IDENTIFY_STDOUT:
2759 case MSG_IDENTIFY_TERM:
2760 case MSG_IDENTIFY_TERMINFO:
2761 case MSG_IDENTIFY_TTYNAME:
2762 case MSG_IDENTIFY_DONE:
2763 server_client_dispatch_identify(c, imsg);
2764 break;
2765 case MSG_COMMAND:
2766 server_client_dispatch_command(c, imsg);
2767 break;
2768 case MSG_RESIZE:
2769 if (datalen != 0)
2770 fatalx("bad MSG_RESIZE size");
2772 if (c->flags & CLIENT_CONTROL)
2773 break;
2774 server_client_update_latest(c);
2775 tty_resize(&c->tty);
2776 tty_repeat_requests(&c->tty);
2777 recalculate_sizes();
2778 if (c->overlay_resize == NULL)
2779 server_client_clear_overlay(c);
2780 else
2781 c->overlay_resize(c, c->overlay_data);
2782 server_redraw_client(c);
2783 if (c->session != NULL)
2784 notify_client("client-resized", c);
2785 break;
2786 case MSG_EXITING:
2787 if (datalen != 0)
2788 fatalx("bad MSG_EXITING size");
2789 server_client_set_session(c, NULL);
2790 recalculate_sizes();
2791 tty_close(&c->tty);
2792 proc_send(c->peer, MSG_EXITED, -1, NULL, 0);
2793 break;
2794 case MSG_WAKEUP:
2795 case MSG_UNLOCK:
2796 if (datalen != 0)
2797 fatalx("bad MSG_WAKEUP size");
2799 if (!(c->flags & CLIENT_SUSPENDED))
2800 break;
2801 c->flags &= ~CLIENT_SUSPENDED;
2803 if (c->fd == -1 || c->session == NULL) /* exited already */
2804 break;
2805 s = c->session;
2807 if (gettimeofday(&c->activity_time, NULL) != 0)
2808 fatal("gettimeofday failed");
2810 tty_start_tty(&c->tty);
2811 server_redraw_client(c);
2812 recalculate_sizes();
2814 if (s != NULL)
2815 session_update_activity(s, &c->activity_time);
2816 break;
2817 case MSG_SHELL:
2818 if (datalen != 0)
2819 fatalx("bad MSG_SHELL size");
2821 server_client_dispatch_shell(c);
2822 break;
2823 case MSG_WRITE_READY:
2824 file_write_ready(&c->files, imsg);
2825 break;
2826 case MSG_READ:
2827 file_read_data(&c->files, imsg);
2828 break;
2829 case MSG_READ_DONE:
2830 file_read_done(&c->files, imsg);
2831 break;
2835 /* Callback when command is not allowed. */
2836 static enum cmd_retval
2837 server_client_read_only(struct cmdq_item *item, __unused void *data)
2839 cmdq_error(item, "client is read-only");
2840 return (CMD_RETURN_ERROR);
2843 /* Callback when command is done. */
2844 static enum cmd_retval
2845 server_client_command_done(struct cmdq_item *item, __unused void *data)
2847 struct client *c = cmdq_get_client(item);
2849 if (~c->flags & CLIENT_ATTACHED)
2850 c->flags |= CLIENT_EXIT;
2851 else if (~c->flags & CLIENT_EXIT) {
2852 if (c->flags & CLIENT_CONTROL)
2853 control_ready(c);
2854 tty_send_requests(&c->tty);
2856 return (CMD_RETURN_NORMAL);
2859 /* Handle command message. */
2860 static void
2861 server_client_dispatch_command(struct client *c, struct imsg *imsg)
2863 struct msg_command data;
2864 char *buf;
2865 size_t len;
2866 int argc;
2867 char **argv, *cause;
2868 struct cmd_parse_result *pr;
2869 struct args_value *values;
2870 struct cmdq_item *new_item;
2872 if (c->flags & CLIENT_EXIT)
2873 return;
2875 if (imsg->hdr.len - IMSG_HEADER_SIZE < sizeof data)
2876 fatalx("bad MSG_COMMAND size");
2877 memcpy(&data, imsg->data, sizeof data);
2879 buf = (char *)imsg->data + sizeof data;
2880 len = imsg->hdr.len - IMSG_HEADER_SIZE - sizeof data;
2881 if (len > 0 && buf[len - 1] != '\0')
2882 fatalx("bad MSG_COMMAND string");
2884 argc = data.argc;
2885 if (cmd_unpack_argv(buf, len, argc, &argv) != 0) {
2886 cause = xstrdup("command too long");
2887 goto error;
2890 if (argc == 0) {
2891 argc = 1;
2892 argv = xcalloc(1, sizeof *argv);
2893 *argv = xstrdup("new-session");
2896 values = args_from_vector(argc, argv);
2897 pr = cmd_parse_from_arguments(values, argc, NULL);
2898 switch (pr->status) {
2899 case CMD_PARSE_ERROR:
2900 cause = pr->error;
2901 goto error;
2902 case CMD_PARSE_SUCCESS:
2903 break;
2905 args_free_values(values, argc);
2906 free(values);
2907 cmd_free_argv(argc, argv);
2909 if ((c->flags & CLIENT_READONLY) &&
2910 !cmd_list_all_have(pr->cmdlist, CMD_READONLY))
2911 new_item = cmdq_get_callback(server_client_read_only, NULL);
2912 else
2913 new_item = cmdq_get_command(pr->cmdlist, NULL);
2914 cmdq_append(c, new_item);
2915 cmdq_append(c, cmdq_get_callback(server_client_command_done, NULL));
2917 cmd_list_free(pr->cmdlist);
2918 return;
2920 error:
2921 cmd_free_argv(argc, argv);
2923 cmdq_append(c, cmdq_get_error(cause));
2924 free(cause);
2926 c->flags |= CLIENT_EXIT;
2929 /* Handle identify message. */
2930 static void
2931 server_client_dispatch_identify(struct client *c, struct imsg *imsg)
2933 const char *data, *home;
2934 size_t datalen;
2935 int flags, feat;
2936 uint64_t longflags;
2937 char *name;
2939 if (c->flags & CLIENT_IDENTIFIED)
2940 fatalx("out-of-order identify message");
2942 data = imsg->data;
2943 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
2945 switch (imsg->hdr.type) {
2946 case MSG_IDENTIFY_FEATURES:
2947 if (datalen != sizeof feat)
2948 fatalx("bad MSG_IDENTIFY_FEATURES size");
2949 memcpy(&feat, data, sizeof feat);
2950 c->term_features |= feat;
2951 log_debug("client %p IDENTIFY_FEATURES %s", c,
2952 tty_get_features(feat));
2953 break;
2954 case MSG_IDENTIFY_FLAGS:
2955 if (datalen != sizeof flags)
2956 fatalx("bad MSG_IDENTIFY_FLAGS size");
2957 memcpy(&flags, data, sizeof flags);
2958 c->flags |= flags;
2959 log_debug("client %p IDENTIFY_FLAGS %#x", c, flags);
2960 break;
2961 case MSG_IDENTIFY_LONGFLAGS:
2962 if (datalen != sizeof longflags)
2963 fatalx("bad MSG_IDENTIFY_LONGFLAGS size");
2964 memcpy(&longflags, data, sizeof longflags);
2965 c->flags |= longflags;
2966 log_debug("client %p IDENTIFY_LONGFLAGS %#llx", c,
2967 (unsigned long long)longflags);
2968 break;
2969 case MSG_IDENTIFY_TERM:
2970 if (datalen == 0 || data[datalen - 1] != '\0')
2971 fatalx("bad MSG_IDENTIFY_TERM string");
2972 if (*data == '\0')
2973 c->term_name = xstrdup("unknown");
2974 else
2975 c->term_name = xstrdup(data);
2976 log_debug("client %p IDENTIFY_TERM %s", c, data);
2977 break;
2978 case MSG_IDENTIFY_TERMINFO:
2979 if (datalen == 0 || data[datalen - 1] != '\0')
2980 fatalx("bad MSG_IDENTIFY_TERMINFO string");
2981 c->term_caps = xreallocarray(c->term_caps, c->term_ncaps + 1,
2982 sizeof *c->term_caps);
2983 c->term_caps[c->term_ncaps++] = xstrdup(data);
2984 log_debug("client %p IDENTIFY_TERMINFO %s", c, data);
2985 break;
2986 case MSG_IDENTIFY_TTYNAME:
2987 if (datalen == 0 || data[datalen - 1] != '\0')
2988 fatalx("bad MSG_IDENTIFY_TTYNAME string");
2989 c->ttyname = xstrdup(data);
2990 log_debug("client %p IDENTIFY_TTYNAME %s", c, data);
2991 break;
2992 case MSG_IDENTIFY_CWD:
2993 if (datalen == 0 || data[datalen - 1] != '\0')
2994 fatalx("bad MSG_IDENTIFY_CWD string");
2995 if (access(data, X_OK) == 0)
2996 c->cwd = xstrdup(data);
2997 else if ((home = find_home()) != NULL)
2998 c->cwd = xstrdup(home);
2999 else
3000 c->cwd = xstrdup("/");
3001 log_debug("client %p IDENTIFY_CWD %s", c, data);
3002 break;
3003 case MSG_IDENTIFY_STDIN:
3004 if (datalen != 0)
3005 fatalx("bad MSG_IDENTIFY_STDIN size");
3006 c->fd = imsg_get_fd(imsg);
3007 log_debug("client %p IDENTIFY_STDIN %d", c, c->fd);
3008 break;
3009 case MSG_IDENTIFY_STDOUT:
3010 if (datalen != 0)
3011 fatalx("bad MSG_IDENTIFY_STDOUT size");
3012 c->out_fd = imsg_get_fd(imsg);
3013 log_debug("client %p IDENTIFY_STDOUT %d", c, c->out_fd);
3014 break;
3015 case MSG_IDENTIFY_ENVIRON:
3016 if (datalen == 0 || data[datalen - 1] != '\0')
3017 fatalx("bad MSG_IDENTIFY_ENVIRON string");
3018 if (strchr(data, '=') != NULL)
3019 environ_put(c->environ, data, 0);
3020 log_debug("client %p IDENTIFY_ENVIRON %s", c, data);
3021 break;
3022 case MSG_IDENTIFY_CLIENTPID:
3023 if (datalen != sizeof c->pid)
3024 fatalx("bad MSG_IDENTIFY_CLIENTPID size");
3025 memcpy(&c->pid, data, sizeof c->pid);
3026 log_debug("client %p IDENTIFY_CLIENTPID %ld", c, (long)c->pid);
3027 break;
3028 default:
3029 break;
3032 if (imsg->hdr.type != MSG_IDENTIFY_DONE)
3033 return;
3034 c->flags |= CLIENT_IDENTIFIED;
3036 if (*c->ttyname != '\0')
3037 name = xstrdup(c->ttyname);
3038 else
3039 xasprintf(&name, "client-%ld", (long)c->pid);
3040 c->name = name;
3041 log_debug("client %p name is %s", c, c->name);
3043 #ifdef __CYGWIN__
3044 c->fd = open(c->ttyname, O_RDWR|O_NOCTTY);
3045 #endif
3047 if (c->flags & CLIENT_CONTROL)
3048 control_start(c);
3049 else if (c->fd != -1) {
3050 if (tty_init(&c->tty, c) != 0) {
3051 close(c->fd);
3052 c->fd = -1;
3053 } else {
3054 tty_resize(&c->tty);
3055 c->flags |= CLIENT_TERMINAL;
3057 close(c->out_fd);
3058 c->out_fd = -1;
3062 * If this is the first client, load configuration files. Any later
3063 * clients are allowed to continue with their command even if the
3064 * config has not been loaded - they might have been run from inside it
3066 if ((~c->flags & CLIENT_EXIT) &&
3067 !cfg_finished &&
3068 c == TAILQ_FIRST(&clients))
3069 start_cfg();
3072 /* Handle shell message. */
3073 static void
3074 server_client_dispatch_shell(struct client *c)
3076 const char *shell;
3078 shell = options_get_string(global_s_options, "default-shell");
3079 if (!checkshell(shell))
3080 shell = _PATH_BSHELL;
3081 proc_send(c->peer, MSG_SHELL, -1, shell, strlen(shell) + 1);
3083 proc_kill_peer(c->peer);
3086 /* Get client working directory. */
3087 const char *
3088 server_client_get_cwd(struct client *c, struct session *s)
3090 const char *home;
3092 if (!cfg_finished && cfg_client != NULL)
3093 return (cfg_client->cwd);
3094 if (c != NULL && c->session == NULL && c->cwd != NULL)
3095 return (c->cwd);
3096 if (s != NULL && s->cwd != NULL)
3097 return (s->cwd);
3098 if (c != NULL && (s = c->session) != NULL && s->cwd != NULL)
3099 return (s->cwd);
3100 if ((home = find_home()) != NULL)
3101 return (home);
3102 return ("/");
3105 /* Get control client flags. */
3106 static uint64_t
3107 server_client_control_flags(struct client *c, const char *next)
3109 if (strcmp(next, "pause-after") == 0) {
3110 c->pause_age = 0;
3111 return (CLIENT_CONTROL_PAUSEAFTER);
3113 if (sscanf(next, "pause-after=%u", &c->pause_age) == 1) {
3114 c->pause_age *= 1000;
3115 return (CLIENT_CONTROL_PAUSEAFTER);
3117 if (strcmp(next, "no-output") == 0)
3118 return (CLIENT_CONTROL_NOOUTPUT);
3119 if (strcmp(next, "wait-exit") == 0)
3120 return (CLIENT_CONTROL_WAITEXIT);
3121 return (0);
3124 /* Set client flags. */
3125 void
3126 server_client_set_flags(struct client *c, const char *flags)
3128 char *s, *copy, *next;
3129 uint64_t flag;
3130 int not;
3132 s = copy = xstrdup(flags);
3133 while ((next = strsep(&s, ",")) != NULL) {
3134 not = (*next == '!');
3135 if (not)
3136 next++;
3138 if (c->flags & CLIENT_CONTROL)
3139 flag = server_client_control_flags(c, next);
3140 else
3141 flag = 0;
3142 if (strcmp(next, "read-only") == 0)
3143 flag = CLIENT_READONLY;
3144 else if (strcmp(next, "ignore-size") == 0)
3145 flag = CLIENT_IGNORESIZE;
3146 else if (strcmp(next, "active-pane") == 0)
3147 flag = CLIENT_ACTIVEPANE;
3148 if (flag == 0)
3149 continue;
3151 log_debug("client %s set flag %s", c->name, next);
3152 if (not) {
3153 if (c->flags & CLIENT_READONLY)
3154 flag &= ~CLIENT_READONLY;
3155 c->flags &= ~flag;
3156 } else
3157 c->flags |= flag;
3158 if (flag == CLIENT_CONTROL_NOOUTPUT)
3159 control_reset_offsets(c);
3161 free(copy);
3162 proc_send(c->peer, MSG_FLAGS, -1, &c->flags, sizeof c->flags);
3165 /* Get client flags. This is only flags useful to show to users. */
3166 const char *
3167 server_client_get_flags(struct client *c)
3169 static char s[256];
3170 char tmp[32];
3172 *s = '\0';
3173 if (c->flags & CLIENT_ATTACHED)
3174 strlcat(s, "attached,", sizeof s);
3175 if (c->flags & CLIENT_FOCUSED)
3176 strlcat(s, "focused,", sizeof s);
3177 if (c->flags & CLIENT_CONTROL)
3178 strlcat(s, "control-mode,", sizeof s);
3179 if (c->flags & CLIENT_IGNORESIZE)
3180 strlcat(s, "ignore-size,", sizeof s);
3181 if (c->flags & CLIENT_CONTROL_NOOUTPUT)
3182 strlcat(s, "no-output,", sizeof s);
3183 if (c->flags & CLIENT_CONTROL_WAITEXIT)
3184 strlcat(s, "wait-exit,", sizeof s);
3185 if (c->flags & CLIENT_CONTROL_PAUSEAFTER) {
3186 xsnprintf(tmp, sizeof tmp, "pause-after=%u,",
3187 c->pause_age / 1000);
3188 strlcat(s, tmp, sizeof s);
3190 if (c->flags & CLIENT_READONLY)
3191 strlcat(s, "read-only,", sizeof s);
3192 if (c->flags & CLIENT_ACTIVEPANE)
3193 strlcat(s, "active-pane,", sizeof s);
3194 if (c->flags & CLIENT_SUSPENDED)
3195 strlcat(s, "suspended,", sizeof s);
3196 if (c->flags & CLIENT_UTF8)
3197 strlcat(s, "UTF-8,", sizeof s);
3198 if (*s != '\0')
3199 s[strlen(s) - 1] = '\0';
3200 return (s);
3203 /* Get client window. */
3204 struct client_window *
3205 server_client_get_client_window(struct client *c, u_int id)
3207 struct client_window cw = { .window = id };
3209 return (RB_FIND(client_windows, &c->windows, &cw));
3212 /* Add client window. */
3213 struct client_window *
3214 server_client_add_client_window(struct client *c, u_int id)
3216 struct client_window *cw;
3218 cw = server_client_get_client_window(c, id);
3219 if (cw == NULL) {
3220 cw = xcalloc(1, sizeof *cw);
3221 cw->window = id;
3222 RB_INSERT(client_windows, &c->windows, cw);
3224 return (cw);
3227 /* Get client active pane. */
3228 struct window_pane *
3229 server_client_get_pane(struct client *c)
3231 struct session *s = c->session;
3232 struct client_window *cw;
3234 if (s == NULL)
3235 return (NULL);
3237 if (~c->flags & CLIENT_ACTIVEPANE)
3238 return (s->curw->window->active);
3239 cw = server_client_get_client_window(c, s->curw->window->id);
3240 if (cw == NULL)
3241 return (s->curw->window->active);
3242 return (cw->pane);
3245 /* Set client active pane. */
3246 void
3247 server_client_set_pane(struct client *c, struct window_pane *wp)
3249 struct session *s = c->session;
3250 struct client_window *cw;
3252 if (s == NULL)
3253 return;
3255 cw = server_client_add_client_window(c, s->curw->window->id);
3256 cw->pane = wp;
3257 log_debug("%s pane now %%%u", c->name, wp->id);
3260 /* Remove pane from client lists. */
3261 void
3262 server_client_remove_pane(struct window_pane *wp)
3264 struct client *c;
3265 struct window *w = wp->window;
3266 struct client_window *cw;
3268 TAILQ_FOREACH(c, &clients, entry) {
3269 cw = server_client_get_client_window(c, w->id);
3270 if (cw != NULL && cw->pane == wp) {
3271 RB_REMOVE(client_windows, &c->windows, cw);
3272 free(cw);
3277 /* Print to a client. */
3278 void
3279 server_client_print(struct client *c, int parse, struct evbuffer *evb)
3281 void *data = EVBUFFER_DATA(evb);
3282 size_t size = EVBUFFER_LENGTH(evb);
3283 struct window_pane *wp;
3284 struct window_mode_entry *wme;
3285 char *sanitized, *msg, *line;
3287 if (!parse) {
3288 utf8_stravisx(&msg, data, size,
3289 VIS_OCTAL|VIS_CSTYLE|VIS_NOSLASH);
3290 log_debug("%s: %s", __func__, msg);
3291 } else {
3292 msg = EVBUFFER_DATA(evb);
3293 if (msg[size - 1] != '\0')
3294 evbuffer_add(evb, "", 1);
3297 if (c == NULL)
3298 goto out;
3300 if (c->session == NULL || (c->flags & CLIENT_CONTROL)) {
3301 if (~c->flags & CLIENT_UTF8) {
3302 sanitized = utf8_sanitize(msg);
3303 if (c->flags & CLIENT_CONTROL)
3304 control_write(c, "%s", sanitized);
3305 else
3306 file_print(c, "%s\n", sanitized);
3307 free(sanitized);
3308 } else {
3309 if (c->flags & CLIENT_CONTROL)
3310 control_write(c, "%s", msg);
3311 else
3312 file_print(c, "%s\n", msg);
3314 goto out;
3317 wp = server_client_get_pane(c);
3318 wme = TAILQ_FIRST(&wp->modes);
3319 if (wme == NULL || wme->mode != &window_view_mode)
3320 window_pane_set_mode(wp, NULL, &window_view_mode, NULL, NULL);
3321 if (parse) {
3322 do {
3323 line = evbuffer_readln(evb, NULL, EVBUFFER_EOL_LF);
3324 if (line != NULL) {
3325 window_copy_add(wp, 1, "%s", line);
3326 free(line);
3328 } while (line != NULL);
3330 size = EVBUFFER_LENGTH(evb);
3331 if (size != 0) {
3332 line = EVBUFFER_DATA(evb);
3333 window_copy_add(wp, 1, "%.*s", (int)size, line);
3335 } else
3336 window_copy_add(wp, 0, "%s", msg);
3338 out:
3339 if (!parse)
3340 free(msg);