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