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