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