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