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