xref: /openbsd-src/usr.bin/tmux/tmux.h (revision 03adc85b7600a1f8f04886b8321c1c1c0c4933d4)
1 /* $OpenBSD: tmux.h,v 1.702 2017/01/23 10:09:43 nicm Exp $ */
2 
3 /*
4  * Copyright (c) 2007 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 #ifndef TMUX_H
20 #define TMUX_H
21 
22 #define PROTOCOL_VERSION 8
23 
24 #include <sys/time.h>
25 #include <sys/queue.h>
26 #include <sys/tree.h>
27 
28 #include <bitstring.h>
29 #include <event.h>
30 #include <limits.h>
31 #include <stdarg.h>
32 #include <stdint.h>
33 #include <stdio.h>
34 #include <termios.h>
35 #include <wchar.h>
36 
37 #include "xmalloc.h"
38 
39 extern char   **environ;
40 
41 struct args;
42 struct client;
43 struct cmdq_item;
44 struct cmdq_list;
45 struct environ;
46 struct input_ctx;
47 struct mode_key_cmdstr;
48 struct mouse_event;
49 struct options;
50 struct options_entry;
51 struct session;
52 struct tmuxpeer;
53 struct tmuxproc;
54 
55 /* Default global configuration file. */
56 #define TMUX_CONF "/etc/tmux.conf"
57 
58 /*
59  * Minimum layout cell size, NOT including separator line. The scroll region
60  * cannot be one line in height so this must be at least two.
61  */
62 #define PANE_MINIMUM 2
63 
64 /* Automatic name refresh interval, in microseconds. Must be < 1 second. */
65 #define NAME_INTERVAL 500000
66 
67 /*
68  * Event watermarks. We start with FAST then if we hit full size for HITS reads
69  * in succession switch to SLOW, and return when we hit EMPTY the same number
70  * of times.
71  */
72 #define READ_FAST_SIZE 4096
73 #define READ_SLOW_SIZE 128
74 
75 #define READ_FULL_SIZE (4096 - 16)
76 #define READ_EMPTY_SIZE 16
77 
78 #define READ_CHANGE_HITS 3
79 
80 /* Attribute to make GCC check printf-like arguments. */
81 #define printflike(a, b) __attribute__ ((format (printf, a, b)))
82 
83 /* Number of items in array. */
84 #ifndef nitems
85 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
86 #endif
87 
88 /* Bell option values. */
89 #define BELL_NONE 0
90 #define BELL_ANY 1
91 #define BELL_CURRENT 2
92 #define BELL_OTHER 3
93 
94 /* Special key codes. */
95 #define KEYC_NONE 0xffff00000000ULL
96 #define KEYC_UNKNOWN 0xfffe00000000ULL
97 #define KEYC_BASE 0x000010000000ULL
98 
99 /* Key modifier bits. */
100 #define KEYC_ESCAPE 0x200000000000ULL
101 #define KEYC_CTRL   0x400000000000ULL
102 #define KEYC_SHIFT  0x800000000000ULL
103 
104 /* Mask to obtain key w/o modifiers. */
105 #define KEYC_MASK_MOD (KEYC_ESCAPE|KEYC_CTRL|KEYC_SHIFT)
106 #define KEYC_MASK_KEY (~KEYC_MASK_MOD)
107 
108 /* Is this a mouse key? */
109 #define KEYC_IS_MOUSE(key) (((key) & KEYC_MASK_KEY) >= KEYC_MOUSE &&	\
110     ((key) & KEYC_MASK_KEY) < KEYC_BSPACE)
111 
112 /* Multiple click timeout. */
113 #define KEYC_CLICK_TIMEOUT 300
114 
115 /* Mouse key codes. */
116 #define KEYC_MOUSE_KEY(name)				\
117 	KEYC_ ## name ## _PANE,				\
118 	KEYC_ ## name ## _STATUS,			\
119 	KEYC_ ## name ## _BORDER
120 #define KEYC_MOUSE_STRING(name, s)			\
121 	{ #s "Pane", KEYC_ ## name ## _PANE },		\
122 	{ #s "Status", KEYC_ ## name ## _STATUS },	\
123 	{ #s "Border", KEYC_ ## name ## _BORDER }
124 
125 /*
126  * A single key. This can be ASCII or Unicode or one of the keys starting at
127  * KEYC_BASE.
128  */
129 typedef unsigned long long key_code;
130 
131 /* Special key codes. */
132 enum {
133 	/* Focus events. */
134 	KEYC_FOCUS_IN = KEYC_BASE,
135 	KEYC_FOCUS_OUT,
136 
137 	/* Mouse keys. */
138 	KEYC_MOUSE, /* unclassified mouse event */
139 	KEYC_DRAGGING, /* dragging in progress */
140 	KEYC_MOUSE_KEY(MOUSEDOWN1),
141 	KEYC_MOUSE_KEY(MOUSEDOWN2),
142 	KEYC_MOUSE_KEY(MOUSEDOWN3),
143 	KEYC_MOUSE_KEY(MOUSEUP1),
144 	KEYC_MOUSE_KEY(MOUSEUP2),
145 	KEYC_MOUSE_KEY(MOUSEUP3),
146 	KEYC_MOUSE_KEY(MOUSEDRAG1),
147 	KEYC_MOUSE_KEY(MOUSEDRAG2),
148 	KEYC_MOUSE_KEY(MOUSEDRAG3),
149 	KEYC_MOUSE_KEY(MOUSEDRAGEND1),
150 	KEYC_MOUSE_KEY(MOUSEDRAGEND2),
151 	KEYC_MOUSE_KEY(MOUSEDRAGEND3),
152 	KEYC_MOUSE_KEY(WHEELUP),
153 	KEYC_MOUSE_KEY(WHEELDOWN),
154 	KEYC_MOUSE_KEY(DOUBLECLICK1),
155 	KEYC_MOUSE_KEY(DOUBLECLICK2),
156 	KEYC_MOUSE_KEY(DOUBLECLICK3),
157 	KEYC_MOUSE_KEY(TRIPLECLICK1),
158 	KEYC_MOUSE_KEY(TRIPLECLICK2),
159 	KEYC_MOUSE_KEY(TRIPLECLICK3),
160 
161 	/* Backspace key. */
162 	KEYC_BSPACE,
163 
164 	/* Function keys. */
165 	KEYC_F1,
166 	KEYC_F2,
167 	KEYC_F3,
168 	KEYC_F4,
169 	KEYC_F5,
170 	KEYC_F6,
171 	KEYC_F7,
172 	KEYC_F8,
173 	KEYC_F9,
174 	KEYC_F10,
175 	KEYC_F11,
176 	KEYC_F12,
177 	KEYC_IC,
178 	KEYC_DC,
179 	KEYC_HOME,
180 	KEYC_END,
181 	KEYC_NPAGE,
182 	KEYC_PPAGE,
183 	KEYC_BTAB,
184 
185 	/* Arrow keys. */
186 	KEYC_UP,
187 	KEYC_DOWN,
188 	KEYC_LEFT,
189 	KEYC_RIGHT,
190 
191 	/* Numeric keypad. */
192 	KEYC_KP_SLASH,
193 	KEYC_KP_STAR,
194 	KEYC_KP_MINUS,
195 	KEYC_KP_SEVEN,
196 	KEYC_KP_EIGHT,
197 	KEYC_KP_NINE,
198 	KEYC_KP_PLUS,
199 	KEYC_KP_FOUR,
200 	KEYC_KP_FIVE,
201 	KEYC_KP_SIX,
202 	KEYC_KP_ONE,
203 	KEYC_KP_TWO,
204 	KEYC_KP_THREE,
205 	KEYC_KP_ENTER,
206 	KEYC_KP_ZERO,
207 	KEYC_KP_PERIOD,
208 };
209 
210 /* Termcap codes. */
211 enum tty_code_code {
212 	TTYC_AX = 0,
213 	TTYC_ACSC,	/* acs_chars, ac */
214 	TTYC_BCE,	/* back_color_erase, ut */
215 	TTYC_BEL,	/* bell, bl */
216 	TTYC_BLINK,	/* enter_blink_mode, mb */
217 	TTYC_BOLD,	/* enter_bold_mode, md */
218 	TTYC_CIVIS,	/* cursor_invisible, vi */
219 	TTYC_CLEAR,	/* clear_screen, cl */
220 	TTYC_CNORM,	/* cursor_normal, ve */
221 	TTYC_COLORS,	/* max_colors, Co */
222 	TTYC_CR,	/* restore cursor colour, Cr */
223 	TTYC_CS,	/* set cursor colour, Cs */
224 	TTYC_CSR,	/* change_scroll_region, cs */
225 	TTYC_CUB,	/* parm_left_cursor, LE */
226 	TTYC_CUB1,	/* cursor_left, le */
227 	TTYC_CUD,	/* parm_down_cursor, DO */
228 	TTYC_CUD1,	/* cursor_down, do */
229 	TTYC_CUF,	/* parm_right_cursor, RI */
230 	TTYC_CUF1,	/* cursor_right, nd */
231 	TTYC_CUP,	/* cursor_address, cm */
232 	TTYC_CUU,	/* parm_up_cursor, UP */
233 	TTYC_CUU1,	/* cursor_up, up */
234 	TTYC_CVVIS,	/* cursor_visible, vs */
235 	TTYC_DCH,	/* parm_dch, DC */
236 	TTYC_DCH1,	/* delete_character, dc */
237 	TTYC_DIM,	/* enter_dim_mode, mh */
238 	TTYC_DL,	/* parm_delete_line, DL */
239 	TTYC_DL1,	/* delete_line, dl */
240 	TTYC_E3,
241 	TTYC_ECH,	/* erase_chars, ec */
242 	TTYC_EL,	/* clr_eol, ce */
243 	TTYC_EL1,	/* clr_bol, cb */
244 	TTYC_ENACS,	/* ena_acs, eA */
245 	TTYC_FSL,	/* from_status_line, fsl */
246 	TTYC_HOME,	/* cursor_home, ho */
247 	TTYC_HPA,	/* column_address, ch */
248 	TTYC_ICH,	/* parm_ich, IC */
249 	TTYC_ICH1,	/* insert_character, ic */
250 	TTYC_IL,	/* parm_insert_line, IL */
251 	TTYC_IL1,	/* insert_line, il */
252 	TTYC_INVIS,	/* enter_secure_mode, mk */
253 	TTYC_IS1,	/* init_1string, i1 */
254 	TTYC_IS2,	/* init_2string, i2 */
255 	TTYC_IS3,	/* init_3string, i3 */
256 	TTYC_KCBT,	/* key_btab, kB */
257 	TTYC_KCUB1,	/* key_left, kl */
258 	TTYC_KCUD1,	/* key_down, kd */
259 	TTYC_KCUF1,	/* key_right, kr */
260 	TTYC_KCUU1,	/* key_up, ku */
261 	TTYC_KDC2,
262 	TTYC_KDC3,
263 	TTYC_KDC4,
264 	TTYC_KDC5,
265 	TTYC_KDC6,
266 	TTYC_KDC7,
267 	TTYC_KDCH1,	/* key_dc, kD */
268 	TTYC_KDN2,
269 	TTYC_KDN3,
270 	TTYC_KDN4,
271 	TTYC_KDN5,
272 	TTYC_KDN6,
273 	TTYC_KDN7,
274 	TTYC_KEND,	/* key_end, ke */
275 	TTYC_KEND2,
276 	TTYC_KEND3,
277 	TTYC_KEND4,
278 	TTYC_KEND5,
279 	TTYC_KEND6,
280 	TTYC_KEND7,
281 	TTYC_KF1,
282 	TTYC_KF10,
283 	TTYC_KF11,
284 	TTYC_KF12,
285 	TTYC_KF13,
286 	TTYC_KF14,
287 	TTYC_KF15,
288 	TTYC_KF16,
289 	TTYC_KF17,
290 	TTYC_KF18,
291 	TTYC_KF19,
292 	TTYC_KF2,
293 	TTYC_KF20,
294 	TTYC_KF21,
295 	TTYC_KF22,
296 	TTYC_KF23,
297 	TTYC_KF24,
298 	TTYC_KF25,
299 	TTYC_KF26,
300 	TTYC_KF27,
301 	TTYC_KF28,
302 	TTYC_KF29,
303 	TTYC_KF3,
304 	TTYC_KF30,
305 	TTYC_KF31,
306 	TTYC_KF32,
307 	TTYC_KF33,
308 	TTYC_KF34,
309 	TTYC_KF35,
310 	TTYC_KF36,
311 	TTYC_KF37,
312 	TTYC_KF38,
313 	TTYC_KF39,
314 	TTYC_KF4,
315 	TTYC_KF40,
316 	TTYC_KF41,
317 	TTYC_KF42,
318 	TTYC_KF43,
319 	TTYC_KF44,
320 	TTYC_KF45,
321 	TTYC_KF46,
322 	TTYC_KF47,
323 	TTYC_KF48,
324 	TTYC_KF49,
325 	TTYC_KF5,
326 	TTYC_KF50,
327 	TTYC_KF51,
328 	TTYC_KF52,
329 	TTYC_KF53,
330 	TTYC_KF54,
331 	TTYC_KF55,
332 	TTYC_KF56,
333 	TTYC_KF57,
334 	TTYC_KF58,
335 	TTYC_KF59,
336 	TTYC_KF6,
337 	TTYC_KF60,
338 	TTYC_KF61,
339 	TTYC_KF62,
340 	TTYC_KF63,
341 	TTYC_KF7,
342 	TTYC_KF8,
343 	TTYC_KF9,
344 	TTYC_KHOM2,
345 	TTYC_KHOM3,
346 	TTYC_KHOM4,
347 	TTYC_KHOM5,
348 	TTYC_KHOM6,
349 	TTYC_KHOM7,
350 	TTYC_KHOME,	/* key_home, kh */
351 	TTYC_KIC2,
352 	TTYC_KIC3,
353 	TTYC_KIC4,
354 	TTYC_KIC5,
355 	TTYC_KIC6,
356 	TTYC_KIC7,
357 	TTYC_KICH1,	/* key_ic, kI */
358 	TTYC_KLFT2,
359 	TTYC_KLFT3,
360 	TTYC_KLFT4,
361 	TTYC_KLFT5,
362 	TTYC_KLFT6,
363 	TTYC_KLFT7,
364 	TTYC_KMOUS,	/* key_mouse, Km */
365 	TTYC_KNP,	/* key_npage, kN */
366 	TTYC_KNXT2,
367 	TTYC_KNXT3,
368 	TTYC_KNXT4,
369 	TTYC_KNXT5,
370 	TTYC_KNXT6,
371 	TTYC_KNXT7,
372 	TTYC_KPP,	/* key_ppage, kP */
373 	TTYC_KPRV2,
374 	TTYC_KPRV3,
375 	TTYC_KPRV4,
376 	TTYC_KPRV5,
377 	TTYC_KPRV6,
378 	TTYC_KPRV7,
379 	TTYC_KRIT2,
380 	TTYC_KRIT3,
381 	TTYC_KRIT4,
382 	TTYC_KRIT5,
383 	TTYC_KRIT6,
384 	TTYC_KRIT7,
385 	TTYC_KUP2,
386 	TTYC_KUP3,
387 	TTYC_KUP4,
388 	TTYC_KUP5,
389 	TTYC_KUP6,
390 	TTYC_KUP7,
391 	TTYC_MS,	/* modify xterm(1) selection */
392 	TTYC_OP,	/* orig_pair, op */
393 	TTYC_REV,	/* enter_reverse_mode, mr */
394 	TTYC_RI,	/* scroll_reverse, sr */
395 	TTYC_RMACS,	/* exit_alt_charset_mode */
396 	TTYC_RMCUP,	/* exit_ca_mode, te */
397 	TTYC_RMKX,	/* keypad_local, ke */
398 	TTYC_SE,	/* reset cursor style, Se */
399 	TTYC_SETAB,	/* set_a_background, AB */
400 	TTYC_SETAF,	/* set_a_foreground, AF */
401 	TTYC_SGR0,	/* exit_attribute_mode, me */
402 	TTYC_SITM,	/* enter_italics_mode, it */
403 	TTYC_SMACS,	/* enter_alt_charset_mode, as */
404 	TTYC_SMCUP,	/* enter_ca_mode, ti */
405 	TTYC_SMKX,	/* keypad_xmit, ks */
406 	TTYC_SMSO,	/* enter_standout_mode, so */
407 	TTYC_SMUL,	/* enter_underline_mode, us */
408 	TTYC_SS,	/* set cursor style, Ss */
409 	TTYC_TC,	/* 24-bit "true" colour, Tc */
410 	TTYC_TSL,	/* to_status_line, tsl */
411 	TTYC_VPA,	/* row_address, cv */
412 	TTYC_XENL,	/* eat_newline_glitch, xn */
413 	TTYC_XT,	/* xterm(1)-compatible title, XT */
414 };
415 
416 /* Message codes. */
417 enum msgtype {
418 	MSG_VERSION = 12,
419 
420 	MSG_IDENTIFY_FLAGS = 100,
421 	MSG_IDENTIFY_TERM,
422 	MSG_IDENTIFY_TTYNAME,
423 	MSG_IDENTIFY_OLDCWD, /* unused */
424 	MSG_IDENTIFY_STDIN,
425 	MSG_IDENTIFY_ENVIRON,
426 	MSG_IDENTIFY_DONE,
427 	MSG_IDENTIFY_CLIENTPID,
428 	MSG_IDENTIFY_CWD,
429 
430 	MSG_COMMAND = 200,
431 	MSG_DETACH,
432 	MSG_DETACHKILL,
433 	MSG_EXIT,
434 	MSG_EXITED,
435 	MSG_EXITING,
436 	MSG_LOCK,
437 	MSG_READY,
438 	MSG_RESIZE,
439 	MSG_SHELL,
440 	MSG_SHUTDOWN,
441 	MSG_STDERR,
442 	MSG_STDIN,
443 	MSG_STDOUT,
444 	MSG_SUSPEND,
445 	MSG_UNLOCK,
446 	MSG_WAKEUP,
447 	MSG_EXEC,
448 };
449 
450 /*
451  * Message data.
452  *
453  * Don't forget to bump PROTOCOL_VERSION if any of these change!
454  */
455 struct msg_command_data {
456 	int	argc;
457 }; /* followed by packed argv */
458 
459 struct msg_stdin_data {
460 	ssize_t	size;
461 	char	data[BUFSIZ];
462 };
463 
464 struct msg_stdout_data {
465 	ssize_t	size;
466 	char	data[BUFSIZ];
467 };
468 
469 struct msg_stderr_data {
470 	ssize_t	size;
471 	char	data[BUFSIZ];
472 };
473 
474 /* Mode key commands. */
475 enum mode_key_cmd {
476 	MODEKEY_NONE,
477 	MODEKEY_OTHER,
478 
479 	/* Menu (choice) keys. */
480 	MODEKEYCHOICE_BACKSPACE,
481 	MODEKEYCHOICE_BOTTOMLINE,
482 	MODEKEYCHOICE_CANCEL,
483 	MODEKEYCHOICE_CHOOSE,
484 	MODEKEYCHOICE_DOWN,
485 	MODEKEYCHOICE_ENDOFLIST,
486 	MODEKEYCHOICE_PAGEDOWN,
487 	MODEKEYCHOICE_PAGEUP,
488 	MODEKEYCHOICE_SCROLLDOWN,
489 	MODEKEYCHOICE_SCROLLUP,
490 	MODEKEYCHOICE_STARTNUMBERPREFIX,
491 	MODEKEYCHOICE_STARTOFLIST,
492 	MODEKEYCHOICE_TOPLINE,
493 	MODEKEYCHOICE_TREE_COLLAPSE,
494 	MODEKEYCHOICE_TREE_COLLAPSE_ALL,
495 	MODEKEYCHOICE_TREE_EXPAND,
496 	MODEKEYCHOICE_TREE_EXPAND_ALL,
497 	MODEKEYCHOICE_TREE_TOGGLE,
498 	MODEKEYCHOICE_UP,
499 };
500 
501 /* Data required while mode keys are in use. */
502 struct mode_key_data {
503 	struct mode_key_tree   *tree;
504 };
505 #define MODEKEY_EMACS 0
506 #define MODEKEY_VI 1
507 
508 /* Binding between a key and a command. */
509 struct mode_key_binding {
510 	key_code			 key;
511 	enum mode_key_cmd		 cmd;
512 
513 	RB_ENTRY(mode_key_binding)	 entry;
514 };
515 RB_HEAD(mode_key_tree, mode_key_binding);
516 
517 /* Named mode key table description. */
518 struct mode_key_entry;
519 struct mode_key_table {
520 	const char			*name;
521 	const struct mode_key_cmdstr	*cmdstr;
522 	struct mode_key_tree		*tree;
523 	const struct mode_key_entry	*table;	/* default entries */
524 };
525 
526 /* Modes. */
527 #define MODE_CURSOR 0x1
528 #define MODE_INSERT 0x2
529 #define MODE_KCURSOR 0x4
530 #define MODE_KKEYPAD 0x8	/* set = application, clear = number */
531 #define MODE_WRAP 0x10		/* whether lines wrap */
532 #define MODE_MOUSE_STANDARD 0x20
533 #define MODE_MOUSE_BUTTON 0x40
534 #define MODE_BLINKING 0x80
535 #define MODE_MOUSE_UTF8 0x100
536 #define MODE_MOUSE_SGR 0x200
537 #define MODE_BRACKETPASTE 0x400
538 #define MODE_FOCUSON 0x800
539 
540 #define ALL_MOUSE_MODES (MODE_MOUSE_STANDARD|MODE_MOUSE_BUTTON)
541 
542 /*
543  * A single UTF-8 character. UTF8_SIZE must be big enough to hold at least one
544  * combining character as well.
545 */
546 #define UTF8_SIZE 9
547 struct utf8_data {
548 	u_char	data[UTF8_SIZE];
549 
550 	u_char	have;
551 	u_char	size;
552 
553 	u_char	width;	/* 0xff if invalid */
554 } __packed;
555 enum utf8_state {
556 	UTF8_MORE,
557 	UTF8_DONE,
558 	UTF8_ERROR
559 };
560 
561 /* Colour flags. */
562 #define COLOUR_FLAG_256 0x01000000
563 #define COLOUR_FLAG_RGB 0x02000000
564 
565 /* Grid attributes. */
566 #define GRID_ATTR_BRIGHT 0x1
567 #define GRID_ATTR_DIM 0x2
568 #define GRID_ATTR_UNDERSCORE 0x4
569 #define GRID_ATTR_BLINK 0x8
570 #define GRID_ATTR_REVERSE 0x10
571 #define GRID_ATTR_HIDDEN 0x20
572 #define GRID_ATTR_ITALICS 0x40
573 #define GRID_ATTR_CHARSET 0x80	/* alternative character set */
574 
575 /* Grid flags. */
576 #define GRID_FLAG_FG256 0x1
577 #define GRID_FLAG_BG256 0x2
578 #define GRID_FLAG_PADDING 0x4
579 #define GRID_FLAG_EXTENDED 0x8
580 #define GRID_FLAG_SELECTED 0x10
581 #define GRID_FLAG_NOPALETTE 0x20
582 
583 /* Grid line flags. */
584 #define GRID_LINE_WRAPPED 0x1
585 #define GRID_LINE_EXTENDED 0x2
586 
587 /* Grid cell data. */
588 struct grid_cell {
589 	u_char			flags;
590 	u_char			attr;
591 	int			fg;
592 	int			bg;
593 	struct utf8_data	data;
594 
595 };
596 struct grid_cell_entry {
597 	u_char			flags;
598 	union {
599 		u_int		offset;
600 		struct {
601 			u_char	attr;
602 			u_char	fg;
603 			u_char	bg;
604 			u_char	data;
605 		} data;
606 	};
607 } __packed;
608 
609 /* Grid line. */
610 struct grid_line {
611 	u_int			 cellused;
612 	u_int			 cellsize;
613 	struct grid_cell_entry	*celldata;
614 
615 	u_int			 extdsize;
616 	struct grid_cell	*extddata;
617 
618 	int			 flags;
619 } __packed;
620 
621 /* Entire grid of cells. */
622 struct grid {
623 	int			 flags;
624 #define GRID_HISTORY 0x1 /* scroll lines into history */
625 
626 	u_int			 sx;
627 	u_int			 sy;
628 
629 	u_int			 hscrolled;
630 	u_int			 hsize;
631 	u_int			 hlimit;
632 
633 	struct grid_line	*linedata;
634 };
635 
636 /* Hook data structures. */
637 struct hook {
638 	const char	*name;
639 
640 	struct cmd_list	*cmdlist;
641 
642 	RB_ENTRY(hook)	 entry;
643 };
644 
645 /* Scheduled job. */
646 struct job {
647 	enum {
648 		JOB_RUNNING,
649 		JOB_DEAD,
650 		JOB_CLOSED
651 	} state;
652 
653 	char		*cmd;
654 	pid_t		 pid;
655 	int		 status;
656 
657 	int		 fd;
658 	struct bufferevent *event;
659 
660 	void		(*callbackfn)(struct job *);
661 	void		(*freefn)(void *);
662 	void		*data;
663 
664 	LIST_ENTRY(job)	 lentry;
665 };
666 LIST_HEAD(joblist, job);
667 
668 /* Screen selection. */
669 struct screen_sel {
670 	int		 flag;
671 	int		 hidden;
672 
673 	int		 rectflag;
674 	enum {
675 		LINE_SEL_NONE,
676 		LINE_SEL_LEFT_RIGHT,
677 		LINE_SEL_RIGHT_LEFT,
678 	} lineflag;
679 
680 	int		 modekeys;
681 
682 	u_int		 sx;
683 	u_int		 sy;
684 
685 	u_int		 ex;
686 	u_int		 ey;
687 
688 	struct grid_cell cell;
689 };
690 
691 /* Virtual screen. */
692 struct screen {
693 	char			*title;
694 
695 	struct grid		*grid;		/* grid data */
696 
697 	u_int			 cx;		/* cursor x */
698 	u_int			 cy;		/* cursor y */
699 
700 	u_int			 cstyle;	/* cursor style */
701 	char			*ccolour;	/* cursor colour string */
702 
703 	u_int			 rupper;	/* scroll region top */
704 	u_int			 rlower;	/* scroll region bottom */
705 
706 	int			 mode;
707 
708 	bitstr_t		*tabs;
709 
710 	bitstr_t		*dirty;
711 	u_int			 dirtysize;
712 
713 	struct screen_sel	 sel;
714 };
715 
716 /* Screen write context. */
717 struct screen_write_ctx {
718 	struct window_pane	*wp;
719 	struct screen		*s;
720 	u_int			 dirty;
721 
722 	u_int			 cells;
723 	u_int			 written;
724 	u_int			 skipped;
725 };
726 
727 /* Screen size. */
728 #define screen_size_x(s) ((s)->grid->sx)
729 #define screen_size_y(s) ((s)->grid->sy)
730 #define screen_hsize(s) ((s)->grid->hsize)
731 #define screen_hlimit(s) ((s)->grid->hlimit)
732 
733 /*
734  * Window mode. Windows can be in several modes and this is used to call the
735  * right function to handle input and output.
736  */
737 struct window_mode {
738 	struct screen *(*init)(struct window_pane *);
739 	void	(*free)(struct window_pane *);
740 	void	(*resize)(struct window_pane *, u_int, u_int);
741 	void	(*key)(struct window_pane *, struct client *, struct session *,
742 		    key_code, struct mouse_event *);
743 
744 	const char *(*key_table)(struct window_pane *);
745 	void	(*command)(struct window_pane *, struct client *,
746 		    struct session *, struct args *, struct mouse_event *);
747 };
748 #define WINDOW_MODE_TIMEOUT 180
749 
750 /* Structures for choose mode. */
751 struct window_choose_data {
752 	struct client		*start_client;
753 	struct session		*start_session;
754 
755 	u_int			 idx;
756 	int			 type;
757 #define TREE_OTHER 0x0
758 #define TREE_WINDOW 0x1
759 #define TREE_SESSION 0x2
760 
761 	struct session		*tree_session; /* session of items in tree */
762 
763 	struct winlink		*wl;
764 	int			 pane_id;
765 
766 	char			*ft_template;
767 	struct format_tree	*ft;
768 
769 	char			*command;
770 };
771 
772 /* Child window structure. */
773 struct window_pane {
774 	u_int		 id;
775 	u_int		 active_point;
776 
777 	struct window	*window;
778 
779 	struct layout_cell *layout_cell;
780 	struct layout_cell *saved_layout_cell;
781 
782 	u_int		 sx;
783 	u_int		 sy;
784 
785 	u_int		 xoff;
786 	u_int		 yoff;
787 
788 	int		 flags;
789 #define PANE_REDRAW 0x1
790 #define PANE_DROP 0x2
791 #define PANE_FOCUSED 0x4
792 #define PANE_RESIZE 0x8
793 #define PANE_FOCUSPUSH 0x10
794 #define PANE_INPUTOFF 0x20
795 #define PANE_CHANGED 0x40
796 
797 	int		 argc;
798 	char	       **argv;
799 	char		*shell;
800 	const char	*cwd;
801 
802 	pid_t		 pid;
803 	char		 tty[TTY_NAME_MAX];
804 	int		 status;
805 
806 	int		 fd;
807 	struct bufferevent *event;
808 
809 	struct event	 resize_timer;
810 
811 	u_int		 wmark_size;
812 	u_int		 wmark_hits;
813 
814 	struct input_ctx *ictx;
815 
816 	struct grid_cell colgc;
817 
818 	int		*palette;
819 
820 	int		 pipe_fd;
821 	struct bufferevent *pipe_event;
822 	size_t		 pipe_off;
823 
824 	struct screen	*screen;
825 	struct screen	 base;
826 
827 	struct screen	 status_screen;
828 	size_t		 status_size;
829 
830 	/* Saved in alternative screen mode. */
831 	u_int		 saved_cx;
832 	u_int		 saved_cy;
833 	struct grid	*saved_grid;
834 	struct grid_cell saved_cell;
835 
836 	const struct window_mode *mode;
837 	void		*modedata;
838 	struct event	 modetimer;
839 	time_t		 modelast;
840 	u_int		 modeprefix;
841 
842 	TAILQ_ENTRY(window_pane) entry;
843 	RB_ENTRY(window_pane) tree_entry;
844 };
845 TAILQ_HEAD(window_panes, window_pane);
846 RB_HEAD(window_pane_tree, window_pane);
847 
848 /* Window structure. */
849 struct window {
850 	u_int		 id;
851 
852 	char		*name;
853 	struct event	 name_event;
854 	struct timeval	 name_time;
855 
856 	struct event	 alerts_timer;
857 
858 	struct timeval	 activity_time;
859 
860 	struct window_pane *active;
861 	struct window_pane *last;
862 	struct window_panes panes;
863 
864 	int		 lastlayout;
865 	struct layout_cell *layout_root;
866 	struct layout_cell *saved_layout_root;
867 	char		*old_layout;
868 
869 	u_int		 sx;
870 	u_int		 sy;
871 
872 	int		 flags;
873 #define WINDOW_BELL 0x1
874 #define WINDOW_ACTIVITY 0x2
875 #define WINDOW_REDRAW 0x4
876 #define WINDOW_SILENCE 0x8
877 #define WINDOW_ZOOMED 0x1000
878 #define WINDOW_FORCEWIDTH 0x2000
879 #define WINDOW_FORCEHEIGHT 0x4000
880 #define WINDOW_STYLECHANGED 0x8000
881 #define WINDOW_ALERTFLAGS (WINDOW_BELL|WINDOW_ACTIVITY|WINDOW_SILENCE)
882 
883 	int		 alerts_queued;
884 	TAILQ_ENTRY(window) alerts_entry;
885 
886 	struct options	*options;
887 
888 	struct grid_cell style;
889 	struct grid_cell active_style;
890 
891 	u_int		 references;
892 	TAILQ_HEAD(, winlink) winlinks;
893 
894 	RB_ENTRY(window) entry;
895 };
896 RB_HEAD(windows, window);
897 
898 /* Entry on local window list. */
899 struct winlink {
900 	int		 idx;
901 	struct session	*session;
902 	struct window	*window;
903 
904 	size_t		 status_width;
905 	struct grid_cell status_cell;
906 	char		*status_text;
907 
908 	int		 flags;
909 #define WINLINK_BELL 0x1
910 #define WINLINK_ACTIVITY 0x2
911 #define WINLINK_SILENCE 0x4
912 #define WINLINK_ALERTFLAGS (WINLINK_BELL|WINLINK_ACTIVITY|WINLINK_SILENCE)
913 
914 	RB_ENTRY(winlink) entry;
915 	TAILQ_ENTRY(winlink) wentry;
916 	TAILQ_ENTRY(winlink) sentry;
917 };
918 RB_HEAD(winlinks, winlink);
919 TAILQ_HEAD(winlink_stack, winlink);
920 
921 /* Layout direction. */
922 enum layout_type {
923 	LAYOUT_LEFTRIGHT,
924 	LAYOUT_TOPBOTTOM,
925 	LAYOUT_WINDOWPANE
926 };
927 
928 /* Layout cells queue. */
929 TAILQ_HEAD(layout_cells, layout_cell);
930 
931 /* Layout cell. */
932 struct layout_cell {
933 	enum layout_type type;
934 
935 	struct layout_cell *parent;
936 
937 	u_int		 sx;
938 	u_int		 sy;
939 
940 	u_int		 xoff;
941 	u_int		 yoff;
942 
943 	struct window_pane *wp;
944 	struct layout_cells cells;
945 
946 	TAILQ_ENTRY(layout_cell) entry;
947 };
948 
949 /* Environment variable. */
950 struct environ_entry {
951 	char		*name;
952 	char		*value;
953 
954 	RB_ENTRY(environ_entry) entry;
955 };
956 
957 /* Client session. */
958 struct session_group {
959 	TAILQ_HEAD(, session) sessions;
960 
961 	TAILQ_ENTRY(session_group) entry;
962 };
963 TAILQ_HEAD(session_groups, session_group);
964 
965 struct session {
966 	u_int		 id;
967 
968 	char		*name;
969 	const char	*cwd;
970 
971 	struct timeval	 creation_time;
972 	struct timeval	 last_attached_time;
973 	struct timeval	 activity_time;
974 	struct timeval	 last_activity_time;
975 
976 	struct event	 lock_timer;
977 
978 	u_int		 sx;
979 	u_int		 sy;
980 
981 	struct winlink	*curw;
982 	struct winlink_stack lastw;
983 	struct winlinks	 windows;
984 
985 	struct hooks	*hooks;
986 	struct options	*options;
987 
988 #define SESSION_UNATTACHED 0x1	/* not attached to any clients */
989 #define SESSION_PASTING 0x2
990 #define SESSION_ALERTED 0x4
991 	int		 flags;
992 
993 	u_int		 attached;
994 
995 	struct termios	*tio;
996 
997 	struct environ	*environ;
998 
999 	int		 references;
1000 
1001 	TAILQ_ENTRY(session) gentry;
1002 	RB_ENTRY(session)    entry;
1003 };
1004 RB_HEAD(sessions, session);
1005 
1006 /* Mouse button masks. */
1007 #define MOUSE_MASK_BUTTONS 3
1008 #define MOUSE_MASK_SHIFT 4
1009 #define MOUSE_MASK_META 8
1010 #define MOUSE_MASK_CTRL 16
1011 #define MOUSE_MASK_DRAG 32
1012 #define MOUSE_MASK_WHEEL 64
1013 
1014 /* Mouse wheel states. */
1015 #define MOUSE_WHEEL_UP 0
1016 #define MOUSE_WHEEL_DOWN 64
1017 
1018 /* Mouse helpers. */
1019 #define MOUSE_BUTTONS(b) ((b) & MOUSE_MASK_BUTTONS)
1020 #define MOUSE_WHEEL(b) ((b) & MOUSE_MASK_WHEEL)
1021 #define MOUSE_DRAG(b) ((b) & MOUSE_MASK_DRAG)
1022 #define MOUSE_RELEASE(b) (((b) & MOUSE_MASK_BUTTONS) == 3)
1023 
1024 /* Mouse input. */
1025 struct mouse_event {
1026 	int		valid;
1027 
1028 	key_code	key;
1029 	int		statusat;
1030 
1031 	u_int		x;
1032 	u_int		y;
1033 	u_int		b;
1034 
1035 	u_int		lx;
1036 	u_int		ly;
1037 	u_int		lb;
1038 
1039 	int		s;
1040 	int		w;
1041 	int		wp;
1042 
1043 	u_int		sgr_type;
1044 	u_int		sgr_b;
1045 };
1046 
1047 /* TTY information. */
1048 struct tty_key {
1049 	char		 ch;
1050 	key_code	 key;
1051 
1052 	struct tty_key	*left;
1053 	struct tty_key	*right;
1054 
1055 	struct tty_key	*next;
1056 };
1057 
1058 struct tty_code;
1059 struct tty_term {
1060 	char		*name;
1061 	u_int		 references;
1062 
1063 	char		 acs[UCHAR_MAX + 1][2];
1064 
1065 	struct tty_code	*codes;
1066 
1067 #define TERM_256COLOURS 0x1
1068 #define TERM_EARLYWRAP 0x2
1069 	int		 flags;
1070 
1071 	LIST_ENTRY(tty_term) entry;
1072 };
1073 LIST_HEAD(tty_terms, tty_term);
1074 
1075 struct tty {
1076 	struct client	*client;
1077 	char		*path;
1078 
1079 	u_int		 sx;
1080 	u_int		 sy;
1081 
1082 	u_int		 cx;
1083 	u_int		 cy;
1084 	u_int		 cstyle;
1085 	char		*ccolour;
1086 
1087 	int		 mode;
1088 
1089 	u_int		 rlower;
1090 	u_int		 rupper;
1091 
1092 	u_int		 rleft;
1093 	u_int		 rright;
1094 
1095 	int		 fd;
1096 	struct bufferevent *event;
1097 
1098 	struct termios	 tio;
1099 
1100 	struct grid_cell cell;
1101 
1102 #define TTY_NOCURSOR 0x1
1103 #define TTY_FREEZE 0x2
1104 #define TTY_TIMER 0x4
1105 #define TTY_UTF8 0x8
1106 #define TTY_STARTED 0x10
1107 #define TTY_OPENED 0x20
1108 #define TTY_FOCUS 0x40
1109 	int		 flags;
1110 
1111 	struct tty_term	*term;
1112 	char		*term_name;
1113 	int		 term_flags;
1114 	enum {
1115 		TTY_VT100,
1116 		TTY_VT101,
1117 		TTY_VT102,
1118 		TTY_VT220,
1119 		TTY_VT320,
1120 		TTY_VT420,
1121 		TTY_UNKNOWN
1122 	} term_type;
1123 
1124 	struct mouse_event mouse;
1125 	int		 mouse_drag_flag;
1126 	void		(*mouse_drag_update)(struct client *,
1127 			    struct mouse_event *);
1128 	void		(*mouse_drag_release)(struct client *,
1129 			    struct mouse_event *);
1130 
1131 	struct event	 key_timer;
1132 	struct tty_key	*key_tree;
1133 };
1134 #define TTY_TYPES \
1135 	{ "VT100", "VT101", "VT102", "VT220", "VT320", "VT420", "UNKNOWN" }
1136 
1137 /* TTY command context. */
1138 struct tty_ctx {
1139 	struct window_pane *wp;
1140 
1141 	const struct grid_cell *cell;
1142 
1143 	u_int		 num;
1144 	void		*ptr;
1145 
1146 	/*
1147 	 * Cursor and region position before the screen was updated - this is
1148 	 * where the command should be applied; the values in the screen have
1149 	 * already been updated.
1150 	 */
1151 	u_int		 ocx;
1152 	u_int		 ocy;
1153 
1154 	u_int		 orupper;
1155 	u_int		 orlower;
1156 
1157 	u_int		 xoff;
1158 	u_int		 yoff;
1159 
1160 	/* The background colour used for clearing (erasing). */
1161 	u_int		 bg;
1162 
1163 	/* Saved last cell on line. */
1164 	struct grid_cell last_cell;
1165 };
1166 
1167 /* Saved message entry. */
1168 struct message_entry {
1169 	char	*msg;
1170 	u_int	 msg_num;
1171 	time_t	 msg_time;
1172 	TAILQ_ENTRY(message_entry) entry;
1173 };
1174 
1175 /* Parsed arguments structures. */
1176 struct args_entry;
1177 RB_HEAD(args_tree, args_entry);
1178 struct args {
1179 	struct args_tree	  tree;
1180 	int			  argc;
1181 	char			**argv;
1182 };
1183 
1184 /* Command find structures. */
1185 enum cmd_find_type {
1186 	CMD_FIND_PANE,
1187 	CMD_FIND_WINDOW,
1188 	CMD_FIND_SESSION,
1189 };
1190 struct cmd_find_state {
1191 	struct cmdq_item	*item;
1192 	int			 flags;
1193 	struct cmd_find_state	*current;
1194 
1195 	struct session		*s;
1196 	struct winlink		*wl;
1197 	struct window		*w;
1198 	struct window_pane	*wp;
1199 	int			 idx;
1200 };
1201 
1202 /* Command find flags. */
1203 #define CMD_FIND_PREFER_UNATTACHED 0x1
1204 #define CMD_FIND_QUIET 0x2
1205 #define CMD_FIND_WINDOW_INDEX 0x4
1206 #define CMD_FIND_DEFAULT_MARKED 0x8
1207 #define CMD_FIND_EXACT_SESSION 0x10
1208 #define CMD_FIND_EXACT_WINDOW 0x20
1209 
1210 /* Context for command being executed. */
1211 struct cmd_state {
1212 	struct client		*c;
1213 	struct cmd_find_state	 tflag;
1214 	struct cmd_find_state	 sflag;
1215 };
1216 
1217 /* Command and list of commands. */
1218 struct cmd {
1219 	const struct cmd_entry	*entry;
1220 	struct args		*args;
1221 
1222 	char			*file;
1223 	u_int			 line;
1224 
1225 #define CMD_CONTROL 0x1
1226 	int			 flags;
1227 
1228 	TAILQ_ENTRY(cmd)	 qentry;
1229 };
1230 
1231 struct cmd_list {
1232 	int			 references;
1233 	TAILQ_HEAD(, cmd)	 list;
1234 };
1235 
1236 /* Command return values. */
1237 enum cmd_retval {
1238 	CMD_RETURN_ERROR = -1,
1239 	CMD_RETURN_NORMAL = 0,
1240 	CMD_RETURN_WAIT,
1241 	CMD_RETURN_STOP
1242 };
1243 
1244 /* Command queue item type. */
1245 enum cmdq_type {
1246 	CMDQ_COMMAND,
1247 	CMDQ_CALLBACK,
1248 };
1249 
1250 /* Command queue item. */
1251 typedef enum cmd_retval (*cmdq_cb) (struct cmdq_item *, void *);
1252 struct cmdq_item {
1253 	const char		*name;
1254 	struct cmdq_list	*queue;
1255 	struct cmdq_item	*next;
1256 
1257 	struct client		*client;
1258 
1259 	enum cmdq_type		 type;
1260 	u_int			 group;
1261 
1262 	u_int			 number;
1263 	time_t			 time;
1264 
1265 	struct format_tree	*formats;
1266 
1267 	int			 flags;
1268 #define CMDQ_FIRED 0x1
1269 #define CMDQ_WAITING 0x2
1270 #define CMDQ_NOHOOKS 0x4
1271 
1272 	struct cmd_list		*cmdlist;
1273 	struct cmd		*cmd;
1274 
1275 	cmdq_cb			 cb;
1276 	void			*data;
1277 
1278 	struct cmd_find_state	 current;
1279 	struct cmd_state	 state;
1280 
1281 	struct mouse_event	 mouse;
1282 
1283 	TAILQ_ENTRY(cmdq_item)	 entry;
1284 };
1285 TAILQ_HEAD(cmdq_list, cmdq_item);
1286 
1287 /* Command -c, -t or -s flags. */
1288 enum cmd_entry_flag {
1289 	CMD_NONE,
1290 
1291 	CMD_CLIENT,
1292 	CMD_CLIENT_CANFAIL,
1293 
1294 	CMD_SESSION,
1295 	CMD_SESSION_CANFAIL,
1296 	CMD_SESSION_PREFERUNATTACHED,
1297 	CMD_SESSION_WITHPANE, /* implies PREFERUNATTACHED */
1298 
1299 	CMD_WINDOW,
1300 	CMD_WINDOW_CANFAIL,
1301 	CMD_WINDOW_MARKED,
1302 	CMD_WINDOW_INDEX,
1303 
1304 	CMD_PANE,
1305 	CMD_PANE_CANFAIL,
1306 	CMD_PANE_MARKED,
1307 
1308 	CMD_MOVEW_R,
1309 };
1310 
1311 /* Command definition. */
1312 struct cmd_entry {
1313 	const char		*name;
1314 	const char		*alias;
1315 
1316 	struct {
1317 		const char	*template;
1318 		int		 lower;
1319 		int		 upper;
1320 	} args;
1321 	const char		*usage;
1322 
1323 	enum cmd_entry_flag	 tflag;
1324 	enum cmd_entry_flag	 sflag;
1325 	enum cmd_entry_flag	 cflag;
1326 
1327 #define CMD_STARTSERVER 0x1
1328 #define CMD_READONLY 0x2
1329 #define CMD_AFTERHOOK 0x4
1330 	int		 flags;
1331 
1332 	enum cmd_retval		 (*exec)(struct cmd *, struct cmdq_item *);
1333 };
1334 
1335 /* Client connection. */
1336 struct client {
1337 	struct tmuxpeer	*peer;
1338 	struct cmdq_list queue;
1339 
1340 	pid_t		 pid;
1341 	int		 fd;
1342 	struct event	 event;
1343 	int		 retval;
1344 
1345 	struct timeval	 creation_time;
1346 	struct timeval	 activity_time;
1347 
1348 	struct environ	*environ;
1349 
1350 	char		*title;
1351 	const char	*cwd;
1352 
1353 	char		*term;
1354 	char		*ttyname;
1355 	struct tty	 tty;
1356 
1357 	void		(*stdin_callback)(struct client *, int, void *);
1358 	void		*stdin_callback_data;
1359 	struct evbuffer	*stdin_data;
1360 	int		 stdin_closed;
1361 	struct evbuffer	*stdout_data;
1362 	struct evbuffer	*stderr_data;
1363 
1364 	struct event	 repeat_timer;
1365 
1366 	struct event	 click_timer;
1367 	u_int		 click_button;
1368 
1369 	struct event	 status_timer;
1370 	struct screen	 status;
1371 
1372 #define CLIENT_TERMINAL 0x1
1373 #define CLIENT_LOGIN 0x2
1374 #define CLIENT_EXIT 0x4
1375 #define CLIENT_REDRAW 0x8
1376 #define CLIENT_STATUS 0x10
1377 #define CLIENT_REPEAT 0x20
1378 #define CLIENT_SUSPENDED 0x40
1379 #define CLIENT_ATTACHED 0x80
1380 #define CLIENT_IDENTIFY 0x100
1381 #define CLIENT_DEAD 0x200
1382 #define CLIENT_BORDERS 0x400
1383 #define CLIENT_READONLY 0x800
1384 #define CLIENT_REDRAWWINDOW 0x1000
1385 #define CLIENT_CONTROL 0x2000
1386 #define CLIENT_CONTROLCONTROL 0x4000
1387 #define CLIENT_FOCUSED 0x8000
1388 #define CLIENT_UTF8 0x10000
1389 #define CLIENT_256COLOURS 0x20000
1390 #define CLIENT_IDENTIFIED 0x40000
1391 #define CLIENT_STATUSFORCE 0x80000
1392 #define CLIENT_DOUBLECLICK 0x100000
1393 #define CLIENT_TRIPLECLICK 0x200000
1394 	int		 flags;
1395 	struct key_table *keytable;
1396 
1397 	struct event	 identify_timer;
1398 	void		(*identify_callback)(struct client *, struct window_pane *);
1399 	void		*identify_callback_data;
1400 
1401 	char		*message_string;
1402 	struct event	 message_timer;
1403 	u_int		 message_next;
1404 	TAILQ_HEAD(, message_entry) message_log;
1405 
1406 	char		*prompt_string;
1407 	struct utf8_data *prompt_buffer;
1408 	size_t		 prompt_index;
1409 	int		 (*prompt_callbackfn)(void *, const char *, int);
1410 	void		 (*prompt_freefn)(void *);
1411 	void		*prompt_data;
1412 	u_int		 prompt_hindex;
1413 	enum { PROMPT_ENTRY, PROMPT_COMMAND } prompt_mode;
1414 
1415 #define PROMPT_SINGLE 0x1
1416 #define PROMPT_NUMERIC 0x2
1417 #define PROMPT_INCREMENTAL 0x4
1418 	int		 prompt_flags;
1419 
1420 	struct session	*session;
1421 	struct session	*last_session;
1422 
1423 	int		 wlmouse;
1424 
1425 	int		 references;
1426 
1427 	TAILQ_ENTRY(client) entry;
1428 };
1429 TAILQ_HEAD(clients, client);
1430 
1431 /* Key binding and key table. */
1432 struct key_binding {
1433 	key_code		 key;
1434 	struct cmd_list		*cmdlist;
1435 	int			 can_repeat;
1436 
1437 	RB_ENTRY(key_binding)	 entry;
1438 };
1439 RB_HEAD(key_bindings, key_binding);
1440 
1441 struct key_table {
1442 	const char		 *name;
1443 	struct key_bindings	 key_bindings;
1444 
1445 	u_int			 references;
1446 
1447 	RB_ENTRY(key_table)	 entry;
1448 };
1449 RB_HEAD(key_tables, key_table);
1450 
1451 /* Option table entries. */
1452 enum options_table_type {
1453 	OPTIONS_TABLE_STRING,
1454 	OPTIONS_TABLE_NUMBER,
1455 	OPTIONS_TABLE_KEY,
1456 	OPTIONS_TABLE_COLOUR,
1457 	OPTIONS_TABLE_ATTRIBUTES,
1458 	OPTIONS_TABLE_FLAG,
1459 	OPTIONS_TABLE_CHOICE,
1460 	OPTIONS_TABLE_STYLE,
1461 	OPTIONS_TABLE_ARRAY,
1462 };
1463 enum options_table_scope {
1464 	OPTIONS_TABLE_NONE,
1465 	OPTIONS_TABLE_SERVER,
1466 	OPTIONS_TABLE_SESSION,
1467 	OPTIONS_TABLE_WINDOW,
1468 };
1469 
1470 struct options_table_entry {
1471 	const char		 *name;
1472 	enum options_table_type	  type;
1473 	enum options_table_scope  scope;
1474 
1475 	u_int			  minimum;
1476 	u_int			  maximum;
1477 	const char		**choices;
1478 
1479 	const char		 *default_str;
1480 	long long		  default_num;
1481 
1482 	const char		 *style;
1483 };
1484 
1485 /* Common command usages. */
1486 #define CMD_TARGET_PANE_USAGE "[-t target-pane]"
1487 #define CMD_TARGET_WINDOW_USAGE "[-t target-window]"
1488 #define CMD_TARGET_SESSION_USAGE "[-t target-session]"
1489 #define CMD_TARGET_CLIENT_USAGE "[-t target-client]"
1490 #define CMD_SRCDST_PANE_USAGE "[-s src-pane] [-t dst-pane]"
1491 #define CMD_SRCDST_WINDOW_USAGE "[-s src-window] [-t dst-window]"
1492 #define CMD_SRCDST_SESSION_USAGE "[-s src-session] [-t dst-session]"
1493 #define CMD_SRCDST_CLIENT_USAGE "[-s src-client] [-t dst-client]"
1494 #define CMD_BUFFER_USAGE "[-b buffer-name]"
1495 
1496 /* tmux.c */
1497 extern struct hooks	*global_hooks;
1498 extern struct options	*global_options;
1499 extern struct options	*global_s_options;
1500 extern struct options	*global_w_options;
1501 extern struct environ	*global_environ;
1502 extern struct timeval	 start_time;
1503 extern const char	*socket_path;
1504 extern int		 ptm_fd;
1505 int		 areshell(const char *);
1506 void		 setblocking(int, int);
1507 const char	*find_home(void);
1508 
1509 /* proc.c */
1510 struct imsg;
1511 int	proc_send(struct tmuxpeer *, enum msgtype, int, const void *, size_t);
1512 int	proc_send_s(struct tmuxpeer *, enum msgtype, const char *);
1513 struct tmuxproc *proc_start(const char *, struct event_base *, int,
1514 	    void (*)(int));
1515 void	proc_loop(struct tmuxproc *, int (*)(void));
1516 void	proc_exit(struct tmuxproc *);
1517 struct tmuxpeer *proc_add_peer(struct tmuxproc *, int,
1518 	    void (*)(struct imsg *, void *), void *);
1519 void	proc_remove_peer(struct tmuxpeer *);
1520 void	proc_kill_peer(struct tmuxpeer *);
1521 
1522 /* cfg.c */
1523 extern int cfg_finished;
1524 extern struct client *cfg_client;
1525 void	start_cfg(void);
1526 int	load_cfg(const char *, struct client *, struct cmdq_item *, int);
1527 void	set_cfg_file(const char *);
1528 void printflike(1, 2) cfg_add_cause(const char *, ...);
1529 void	cfg_print_causes(struct cmdq_item *);
1530 void	cfg_show_causes(struct session *);
1531 
1532 /* paste.c */
1533 struct paste_buffer;
1534 const char	*paste_buffer_name(struct paste_buffer *);
1535 u_int		 paste_buffer_order(struct paste_buffer *);
1536 time_t		 paste_buffer_created(struct paste_buffer *);
1537 const char	*paste_buffer_data(struct paste_buffer *, size_t *);
1538 struct paste_buffer *paste_walk(struct paste_buffer *);
1539 struct paste_buffer *paste_get_top(const char **);
1540 struct paste_buffer *paste_get_name(const char *);
1541 void		 paste_free(struct paste_buffer *);
1542 void		 paste_add(char *, size_t);
1543 int		 paste_rename(const char *, const char *, char **);
1544 int		 paste_set(char *, size_t, const char *, char **);
1545 char		*paste_make_sample(struct paste_buffer *);
1546 
1547 /* format.c */
1548 #define FORMAT_STATUS 0x1
1549 #define FORMAT_FORCE 0x2
1550 #define FORMAT_NOJOBS 0x4
1551 struct format_tree;
1552 struct format_tree *format_create(struct cmdq_item *, int);
1553 void		 format_free(struct format_tree *);
1554 void printflike(3, 4) format_add(struct format_tree *, const char *,
1555 		     const char *, ...);
1556 char		*format_expand_time(struct format_tree *, const char *, time_t);
1557 char		*format_expand(struct format_tree *, const char *);
1558 void		 format_defaults(struct format_tree *, struct client *,
1559 		     struct session *, struct winlink *, struct window_pane *);
1560 void		 format_defaults_window(struct format_tree *, struct window *);
1561 void		 format_defaults_pane(struct format_tree *,
1562 		     struct window_pane *);
1563 void		 format_defaults_paste_buffer(struct format_tree *,
1564 		     struct paste_buffer *);
1565 
1566 /* hooks.c */
1567 struct hook;
1568 struct hooks	*hooks_get(struct session *);
1569 struct hooks	*hooks_create(struct hooks *);
1570 void		 hooks_free(struct hooks *);
1571 struct hook	*hooks_first(struct hooks *);
1572 struct hook	*hooks_next(struct hook *);
1573 void		 hooks_add(struct hooks *, const char *, struct cmd_list *);
1574 void		 hooks_copy(struct hooks *, struct hooks *);
1575 void		 hooks_remove(struct hooks *, const char *);
1576 struct hook	*hooks_find(struct hooks *, const char *);
1577 void printflike(4, 5) hooks_run(struct hooks *, struct client *,
1578 		    struct cmd_find_state *, const char *, ...);
1579 void printflike(4, 5) hooks_insert(struct hooks *, struct cmdq_item *,
1580 		    struct cmd_find_state *, const char *, ...);
1581 
1582 /* mode-key.c */
1583 extern struct mode_key_tree mode_key_tree_vi_choice;
1584 extern struct mode_key_tree mode_key_tree_emacs_choice;
1585 int	mode_key_cmp(struct mode_key_binding *, struct mode_key_binding *);
1586 RB_PROTOTYPE(mode_key_tree, mode_key_binding, entry, mode_key_cmp);
1587 const char *mode_key_tostring(const struct mode_key_cmdstr *,
1588 	    enum mode_key_cmd);
1589 enum mode_key_cmd mode_key_fromstring(const struct mode_key_cmdstr *,
1590 	    const char *);
1591 const struct mode_key_table *mode_key_findtable(const char *);
1592 void	mode_key_init_trees(void);
1593 void	mode_key_init(struct mode_key_data *, struct mode_key_tree *);
1594 enum mode_key_cmd mode_key_lookup(struct mode_key_data *, key_code);
1595 
1596 /* notify.c */
1597 void	notify_input(struct window_pane *, struct evbuffer *);
1598 void	notify_client(const char *, struct client *);
1599 void	notify_session(const char *, struct session *);
1600 void	notify_winlink(const char *, struct session *, struct winlink *);
1601 void	notify_session_window(const char *, struct session *, struct window *);
1602 void	notify_window(const char *, struct window *);
1603 void	notify_pane(const char *, struct window_pane *);
1604 
1605 /* options.c */
1606 struct options	*options_create(struct options *);
1607 void		 options_free(struct options *);
1608 struct options_entry *options_first(struct options *);
1609 struct options_entry *options_next(struct options_entry *);
1610 struct options_entry *options_empty(struct options *,
1611 		     const struct options_table_entry *);
1612 struct options_entry *options_default(struct options *,
1613 		     const struct options_table_entry *);
1614 const char	*options_name(struct options_entry *);
1615 const struct options_table_entry *options_table_entry(struct options_entry *);
1616 struct options_entry *options_get_only(struct options *, const char *);
1617 struct options_entry *options_get(struct options *, const char *);
1618 void		 options_remove(struct options_entry *);
1619 const char	*options_array_get(struct options_entry *, u_int);
1620 int		 options_array_set(struct options_entry *, u_int, const char *);
1621 int		 options_array_size(struct options_entry *, u_int *);
1622 int		 options_isstring(struct options_entry *);
1623 const char	*options_tostring(struct options_entry *, int);
1624 char		*options_parse(const char *, int *);
1625 struct options_entry *options_parse_get(struct options *, const char *, int *,
1626 		    int);
1627 char		*options_match(const char *, int *, int *);
1628 struct options_entry *options_match_get(struct options *, const char *, int *,
1629 		    int, int *);
1630 const char	*options_get_string(struct options *, const char *);
1631 long long	 options_get_number(struct options *, const char *);
1632 const struct grid_cell *options_get_style(struct options *, const char *);
1633 struct options_entry * printflike(4, 5) options_set_string(struct options *,
1634 		     const char *, int, const char *, ...);
1635 struct options_entry *options_set_number(struct options *, const char *,
1636 		     long long);
1637 struct options_entry *options_set_style(struct options *, const char *, int,
1638 		     const char *);
1639 enum options_table_scope options_scope_from_flags(struct args *, int,
1640 		     struct cmd_find_state *, struct options **, char **);
1641 void		 options_style_update_new(struct options *,
1642 		     struct options_entry *);
1643 void		 options_style_update_old(struct options *,
1644 		     struct options_entry *);
1645 
1646 /* options-table.c */
1647 extern const struct options_table_entry options_table[];
1648 
1649 /* job.c */
1650 extern struct joblist all_jobs;
1651 struct job *job_run(const char *, struct session *, const char *,
1652 	    void (*)(struct job *), void (*)(void *), void *);
1653 void	job_free(struct job *);
1654 void	job_died(struct job *, int);
1655 
1656 /* environ.c */
1657 struct environ *environ_create(void);
1658 void	environ_free(struct environ *);
1659 struct environ_entry *environ_first(struct environ *);
1660 struct environ_entry *environ_next(struct environ_entry *);
1661 void	environ_copy(struct environ *, struct environ *);
1662 struct environ_entry *environ_find(struct environ *, const char *);
1663 void printflike(3, 4) environ_set(struct environ *, const char *, const char *,
1664 	    ...);
1665 void	environ_clear(struct environ *, const char *);
1666 void	environ_put(struct environ *, const char *);
1667 void	environ_unset(struct environ *, const char *);
1668 void	environ_update(const char *, struct environ *, struct environ *);
1669 void	environ_push(struct environ *);
1670 void	environ_log(struct environ *, const char *);
1671 
1672 /* tty.c */
1673 void	tty_create_log(void);
1674 void	tty_raw(struct tty *, const char *);
1675 void	tty_attributes(struct tty *, const struct grid_cell *,
1676 	    const struct window_pane *);
1677 void	tty_reset(struct tty *);
1678 void	tty_region_off(struct tty *);
1679 void	tty_margin_off(struct tty *);
1680 void	tty_cursor(struct tty *, u_int, u_int);
1681 void	tty_putcode(struct tty *, enum tty_code_code);
1682 void	tty_putcode1(struct tty *, enum tty_code_code, int);
1683 void	tty_putcode2(struct tty *, enum tty_code_code, int, int);
1684 void	tty_putcode_ptr1(struct tty *, enum tty_code_code, const void *);
1685 void	tty_putcode_ptr2(struct tty *, enum tty_code_code, const void *,
1686 	    const void *);
1687 void	tty_puts(struct tty *, const char *);
1688 void	tty_putc(struct tty *, u_char);
1689 void	tty_putn(struct tty *, const void *, size_t, u_int);
1690 int	tty_init(struct tty *, struct client *, int, char *);
1691 int	tty_resize(struct tty *);
1692 int	tty_set_size(struct tty *, u_int, u_int);
1693 void	tty_start_tty(struct tty *);
1694 void	tty_stop_tty(struct tty *);
1695 void	tty_set_title(struct tty *, const char *);
1696 void	tty_update_mode(struct tty *, int, struct screen *);
1697 void	tty_draw_pane(struct tty *, const struct window_pane *, u_int, u_int,
1698 	    u_int);
1699 void	tty_draw_line(struct tty *, const struct window_pane *, struct screen *,
1700 	    u_int, u_int, u_int);
1701 int	tty_open(struct tty *, char **);
1702 void	tty_close(struct tty *);
1703 void	tty_free(struct tty *);
1704 void	tty_set_type(struct tty *, int);
1705 void	tty_write(void (*)(struct tty *, const struct tty_ctx *),
1706 	    struct tty_ctx *);
1707 void	tty_cmd_alignmenttest(struct tty *, const struct tty_ctx *);
1708 void	tty_cmd_cell(struct tty *, const struct tty_ctx *);
1709 void	tty_cmd_clearendofline(struct tty *, const struct tty_ctx *);
1710 void	tty_cmd_clearendofscreen(struct tty *, const struct tty_ctx *);
1711 void	tty_cmd_clearline(struct tty *, const struct tty_ctx *);
1712 void	tty_cmd_clearscreen(struct tty *, const struct tty_ctx *);
1713 void	tty_cmd_clearstartofline(struct tty *, const struct tty_ctx *);
1714 void	tty_cmd_clearstartofscreen(struct tty *, const struct tty_ctx *);
1715 void	tty_cmd_deletecharacter(struct tty *, const struct tty_ctx *);
1716 void	tty_cmd_clearcharacter(struct tty *, const struct tty_ctx *);
1717 void	tty_cmd_deleteline(struct tty *, const struct tty_ctx *);
1718 void	tty_cmd_erasecharacter(struct tty *, const struct tty_ctx *);
1719 void	tty_cmd_insertcharacter(struct tty *, const struct tty_ctx *);
1720 void	tty_cmd_insertline(struct tty *, const struct tty_ctx *);
1721 void	tty_cmd_linefeed(struct tty *, const struct tty_ctx *);
1722 void	tty_cmd_utf8character(struct tty *, const struct tty_ctx *);
1723 void	tty_cmd_reverseindex(struct tty *, const struct tty_ctx *);
1724 void	tty_cmd_setselection(struct tty *, const struct tty_ctx *);
1725 void	tty_cmd_rawstring(struct tty *, const struct tty_ctx *);
1726 
1727 /* tty-term.c */
1728 extern struct tty_terms tty_terms;
1729 u_int		 tty_term_ncodes(void);
1730 struct tty_term *tty_term_find(char *, int, char **);
1731 void		 tty_term_free(struct tty_term *);
1732 int		 tty_term_has(struct tty_term *, enum tty_code_code);
1733 const char	*tty_term_string(struct tty_term *, enum tty_code_code);
1734 const char	*tty_term_string1(struct tty_term *, enum tty_code_code, int);
1735 const char	*tty_term_string2(struct tty_term *, enum tty_code_code, int,
1736 		     int);
1737 const char	*tty_term_ptr1(struct tty_term *, enum tty_code_code,
1738 		     const void *);
1739 const char	*tty_term_ptr2(struct tty_term *, enum tty_code_code,
1740 		     const void *, const void *);
1741 int		 tty_term_number(struct tty_term *, enum tty_code_code);
1742 int		 tty_term_flag(struct tty_term *, enum tty_code_code);
1743 const char	*tty_term_describe(struct tty_term *, enum tty_code_code);
1744 
1745 /* tty-acs.c */
1746 const char	*tty_acs_get(struct tty *, u_char);
1747 
1748 /* tty-keys.c */
1749 void		tty_keys_build(struct tty *);
1750 void		tty_keys_free(struct tty *);
1751 key_code	tty_keys_next(struct tty *);
1752 
1753 /* arguments.c */
1754 struct args	*args_parse(const char *, int, char **);
1755 void		 args_free(struct args *);
1756 char		*args_print(struct args *);
1757 int		 args_has(struct args *, u_char);
1758 const char	*args_get(struct args *, u_char);
1759 long long	 args_strtonum(struct args *, u_char, long long, long long,
1760 		     char **);
1761 
1762 /* cmd-find.c */
1763 int		 cmd_find_current(struct cmd_find_state *, struct cmdq_item *,
1764 		     int);
1765 int		 cmd_find_target(struct cmd_find_state *,
1766 		     struct cmd_find_state *, struct cmdq_item *, const char *,
1767 		     enum cmd_find_type, int);
1768 struct client	*cmd_find_client(struct cmdq_item *, const char *, int);
1769 void		 cmd_find_clear_state(struct cmd_find_state *,
1770 		     struct cmdq_item *, int);
1771 int		 cmd_find_empty_state(struct cmd_find_state *);
1772 int		 cmd_find_valid_state(struct cmd_find_state *);
1773 void		 cmd_find_copy_state(struct cmd_find_state *,
1774 		     struct cmd_find_state *);
1775 void		 cmd_find_log_state(const char *, struct cmd_find_state *);
1776 int		 cmd_find_from_session(struct cmd_find_state *,
1777 		     struct session *);
1778 int		 cmd_find_from_winlink(struct cmd_find_state *,
1779 		     struct session *, struct winlink *);
1780 int		 cmd_find_from_session_window(struct cmd_find_state *,
1781 		     struct session *, struct window *);
1782 int		 cmd_find_from_window(struct cmd_find_state *, struct window *);
1783 int		 cmd_find_from_pane(struct cmd_find_state *,
1784 		     struct window_pane *);
1785 
1786 /* cmd.c */
1787 int		 cmd_pack_argv(int, char **, char *, size_t);
1788 int		 cmd_unpack_argv(char *, size_t, int, char ***);
1789 char	       **cmd_copy_argv(int, char **);
1790 void		 cmd_free_argv(int, char **);
1791 char		*cmd_stringify_argv(int, char **);
1792 struct cmd	*cmd_parse(int, char **, const char *, u_int, char **);
1793 int		 cmd_prepare_state(struct cmd *, struct cmdq_item *);
1794 char		*cmd_print(struct cmd *);
1795 int		 cmd_mouse_at(struct window_pane *, struct mouse_event *,
1796 		     u_int *, u_int *, int);
1797 struct winlink	*cmd_mouse_window(struct mouse_event *, struct session **);
1798 struct window_pane *cmd_mouse_pane(struct mouse_event *, struct session **,
1799 		     struct winlink **);
1800 char		*cmd_template_replace(const char *, const char *, int);
1801 extern const struct cmd_entry *cmd_table[];
1802 
1803 /* cmd-attach-session.c */
1804 enum cmd_retval	 cmd_attach_session(struct cmdq_item *, int, int, const char *,
1805 		     int);
1806 
1807 /* cmd-list.c */
1808 struct cmd_list	*cmd_list_parse(int, char **, const char *, u_int, char **);
1809 void		 cmd_list_free(struct cmd_list *);
1810 char		*cmd_list_print(struct cmd_list *);
1811 
1812 /* cmd-queue.c */
1813 struct cmdq_item *cmdq_get_command(struct cmd_list *, struct cmd_find_state *,
1814 		     struct mouse_event *, int);
1815 #define cmdq_get_callback(cb, data) cmdq_get_callback1(#cb, cb, data)
1816 struct cmdq_item *cmdq_get_callback1(const char *, cmdq_cb, void *);
1817 void		 cmdq_insert_after(struct cmdq_item *, struct cmdq_item *);
1818 void		 cmdq_append(struct client *, struct cmdq_item *);
1819 void printflike(3, 4) cmdq_format(struct cmdq_item *, const char *,
1820 		     const char *, ...);
1821 u_int		 cmdq_next(struct client *);
1822 void		 cmdq_guard(struct cmdq_item *, const char *, int);
1823 void printflike(2, 3) cmdq_print(struct cmdq_item *, const char *, ...);
1824 void printflike(2, 3) cmdq_error(struct cmdq_item *, const char *, ...);
1825 
1826 /* cmd-string.c */
1827 struct cmd_list	*cmd_string_parse(const char *, const char *, u_int, char **);
1828 
1829 /* cmd-wait-for.c */
1830 void	cmd_wait_for_flush(void);
1831 
1832 /* client.c */
1833 int	client_main(struct event_base *, int, char **, int, const char *);
1834 
1835 /* key-bindings.c */
1836 RB_PROTOTYPE(key_bindings, key_binding, entry, key_bindings_cmp);
1837 RB_PROTOTYPE(key_tables, key_table, entry, key_table_cmp);
1838 extern struct key_tables key_tables;
1839 int	 key_table_cmp(struct key_table *, struct key_table *);
1840 int	 key_bindings_cmp(struct key_binding *, struct key_binding *);
1841 struct key_table *key_bindings_get_table(const char *, int);
1842 void	 key_bindings_unref_table(struct key_table *);
1843 void	 key_bindings_add(const char *, key_code, int, struct cmd_list *);
1844 void	 key_bindings_remove(const char *, key_code);
1845 void	 key_bindings_remove_table(const char *);
1846 void	 key_bindings_init(void);
1847 void	 key_bindings_dispatch(struct key_binding *, struct client *,
1848 	     struct mouse_event *, struct cmd_find_state *);
1849 
1850 /* key-string.c */
1851 key_code	 key_string_lookup_string(const char *);
1852 const char	*key_string_lookup_key(key_code);
1853 
1854 /* alerts.c */
1855 void	alerts_reset_all(void);
1856 void	alerts_queue(struct window *, int);
1857 void	alerts_check_session(struct session *);
1858 
1859 /* server.c */
1860 extern struct tmuxproc *server_proc;
1861 extern struct clients clients;
1862 extern struct cmd_find_state marked_pane;
1863 void	 server_set_marked(struct session *, struct winlink *,
1864 	     struct window_pane *);
1865 void	 server_clear_marked(void);
1866 int	 server_is_marked(struct session *, struct winlink *,
1867 	     struct window_pane *);
1868 int	 server_check_marked(void);
1869 int	 server_start(struct event_base *, int, char *);
1870 void	 server_update_socket(void);
1871 void	 server_add_accept(int);
1872 
1873 /* server-client.c */
1874 void	 server_client_set_key_table(struct client *, const char *);
1875 const char *server_client_get_key_table(struct client *);
1876 int	 server_client_is_default_key_table(struct client *);
1877 int	 server_client_check_nested(struct client *);
1878 void	 server_client_handle_key(struct client *, key_code);
1879 void	 server_client_create(int);
1880 int	 server_client_open(struct client *, char **);
1881 void	 server_client_unref(struct client *);
1882 void	 server_client_lost(struct client *);
1883 void	 server_client_detach(struct client *, enum msgtype);
1884 void	 server_client_exec(struct client *, const char *);
1885 void	 server_client_loop(void);
1886 void	 server_client_push_stdout(struct client *);
1887 void	 server_client_push_stderr(struct client *);
1888 
1889 /* server-fn.c */
1890 void	 server_fill_environ(struct session *, struct environ *);
1891 void	 server_redraw_client(struct client *);
1892 void	 server_status_client(struct client *);
1893 void	 server_redraw_session(struct session *);
1894 void	 server_redraw_session_group(struct session *);
1895 void	 server_status_session(struct session *);
1896 void	 server_status_session_group(struct session *);
1897 void	 server_redraw_window(struct window *);
1898 void	 server_redraw_window_borders(struct window *);
1899 void	 server_status_window(struct window *);
1900 void	 server_lock(void);
1901 void	 server_lock_session(struct session *);
1902 void	 server_lock_client(struct client *);
1903 void	 server_kill_window(struct window *);
1904 int	 server_link_window(struct session *,
1905 	     struct winlink *, struct session *, int, int, int, char **);
1906 void	 server_unlink_window(struct session *, struct winlink *);
1907 void	 server_destroy_pane(struct window_pane *, int);
1908 void	 server_destroy_session(struct session *);
1909 void	 server_check_unattached(void);
1910 void	 server_set_identify(struct client *);
1911 void	 server_clear_identify(struct client *, struct window_pane *);
1912 int	 server_set_stdin_callback(struct client *, void (*)(struct client *,
1913 	     int, void *), void *, char **);
1914 void	 server_unzoom_window(struct window *);
1915 
1916 /* status.c */
1917 void	 status_timer_start(struct client *);
1918 void	 status_timer_start_all(void);
1919 int	 status_at_line(struct client *);
1920 struct window *status_get_window_at(struct client *, u_int);
1921 int	 status_redraw(struct client *);
1922 void printflike(2, 3) status_message_set(struct client *, const char *, ...);
1923 void	 status_message_clear(struct client *);
1924 int	 status_message_redraw(struct client *);
1925 void	 status_prompt_set(struct client *, const char *, const char *,
1926 	     int (*)(void *, const char *, int), void (*)(void *), void *, int);
1927 void	 status_prompt_clear(struct client *);
1928 int	 status_prompt_redraw(struct client *);
1929 int	 status_prompt_key(struct client *, key_code);
1930 void	 status_prompt_update(struct client *, const char *, const char *);
1931 void	 status_prompt_load_history(void);
1932 void	 status_prompt_save_history(void);
1933 
1934 /* resize.c */
1935 void	 recalculate_sizes(void);
1936 
1937 /* input.c */
1938 void	 input_init(struct window_pane *);
1939 void	 input_free(struct window_pane *);
1940 void	 input_reset(struct window_pane *, int);
1941 struct evbuffer *input_pending(struct window_pane *);
1942 void	 input_parse(struct window_pane *);
1943 
1944 /* input-key.c */
1945 void	 input_key(struct window_pane *, key_code, struct mouse_event *);
1946 
1947 /* xterm-keys.c */
1948 char	*xterm_keys_lookup(key_code);
1949 int	 xterm_keys_find(const char *, size_t, size_t *, key_code *);
1950 
1951 /* colour.c */
1952 int	 colour_find_rgb(u_char, u_char, u_char);
1953 int	 colour_join_rgb(u_char, u_char, u_char);
1954 void	 colour_split_rgb(int, u_char *, u_char *, u_char *);
1955 const char *colour_tostring(int);
1956 int	 colour_fromstring(const char *s);
1957 u_char	 colour_256to16(u_char);
1958 
1959 /* attributes.c */
1960 const char *attributes_tostring(u_char);
1961 int	 attributes_fromstring(const char *);
1962 
1963 /* grid.c */
1964 extern const struct grid_cell grid_default_cell;
1965 int	 grid_cells_equal(const struct grid_cell *, const struct grid_cell *);
1966 struct grid *grid_create(u_int, u_int, u_int);
1967 void	 grid_destroy(struct grid *);
1968 int	 grid_compare(struct grid *, struct grid *);
1969 void	 grid_collect_history(struct grid *, u_int);
1970 void	 grid_scroll_history(struct grid *, u_int);
1971 void	 grid_scroll_history_region(struct grid *, u_int, u_int);
1972 void	 grid_clear_history(struct grid *);
1973 const struct grid_line *grid_peek_line(struct grid *, u_int);
1974 void	 grid_get_cell(struct grid *, u_int, u_int, struct grid_cell *);
1975 void	 grid_set_cell(struct grid *, u_int, u_int, const struct grid_cell *);
1976 void	 grid_clear(struct grid *, u_int, u_int, u_int, u_int, u_int);
1977 void	 grid_clear_lines(struct grid *, u_int, u_int, u_int);
1978 void	 grid_move_lines(struct grid *, u_int, u_int, u_int, u_int);
1979 void	 grid_move_cells(struct grid *, u_int, u_int, u_int, u_int, u_int);
1980 char	*grid_string_cells(struct grid *, u_int, u_int, u_int,
1981 	     struct grid_cell **, int, int, int);
1982 void	 grid_duplicate_lines(struct grid *, u_int, struct grid *, u_int,
1983 	     u_int);
1984 u_int	 grid_reflow(struct grid *, struct grid *, u_int);
1985 
1986 /* grid-view.c */
1987 void	 grid_view_get_cell(struct grid *, u_int, u_int, struct grid_cell *);
1988 void	 grid_view_set_cell(struct grid *, u_int, u_int,
1989 	     const struct grid_cell *);
1990 void	 grid_view_clear_history(struct grid *, u_int);
1991 void	 grid_view_clear(struct grid *, u_int, u_int, u_int, u_int, u_int);
1992 void	 grid_view_scroll_region_up(struct grid *, u_int, u_int);
1993 void	 grid_view_scroll_region_down(struct grid *, u_int, u_int);
1994 void	 grid_view_insert_lines(struct grid *, u_int, u_int, u_int);
1995 void	 grid_view_insert_lines_region(struct grid *, u_int, u_int, u_int,
1996 	u_int);
1997 void	 grid_view_delete_lines(struct grid *, u_int, u_int, u_int);
1998 void	 grid_view_delete_lines_region(struct grid *, u_int, u_int, u_int,
1999 	u_int);
2000 void	 grid_view_insert_cells(struct grid *, u_int, u_int, u_int, u_int);
2001 void	 grid_view_delete_cells(struct grid *, u_int, u_int, u_int, u_int);
2002 char	*grid_view_string_cells(struct grid *, u_int, u_int, u_int);
2003 
2004 /* screen-write.c */
2005 void	 screen_write_start(struct screen_write_ctx *, struct window_pane *,
2006 	     struct screen *);
2007 void	 screen_write_stop(struct screen_write_ctx *);
2008 void	 screen_write_reset(struct screen_write_ctx *);
2009 size_t printflike(1, 2) screen_write_cstrlen(const char *, ...);
2010 void printflike(4, 5) screen_write_cnputs(struct screen_write_ctx *,
2011 	     ssize_t, const struct grid_cell *, const char *, ...);
2012 size_t printflike(1, 2) screen_write_strlen(const char *, ...);
2013 void printflike(3, 4) screen_write_puts(struct screen_write_ctx *,
2014 	     const struct grid_cell *, const char *, ...);
2015 void printflike(4, 5) screen_write_nputs(struct screen_write_ctx *,
2016 	     ssize_t, const struct grid_cell *, const char *, ...);
2017 void	 screen_write_vnputs(struct screen_write_ctx *, ssize_t,
2018 	     const struct grid_cell *, const char *, va_list);
2019 void	 screen_write_putc(struct screen_write_ctx *, const struct grid_cell *,
2020 	     u_char);
2021 void	 screen_write_copy(struct screen_write_ctx *, struct screen *, u_int,
2022 	     u_int, u_int, u_int, bitstr_t *, const struct grid_cell *);
2023 void	 screen_write_backspace(struct screen_write_ctx *);
2024 void	 screen_write_mode_set(struct screen_write_ctx *, int);
2025 void	 screen_write_mode_clear(struct screen_write_ctx *, int);
2026 void	 screen_write_cursorup(struct screen_write_ctx *, u_int);
2027 void	 screen_write_cursordown(struct screen_write_ctx *, u_int);
2028 void	 screen_write_cursorright(struct screen_write_ctx *, u_int);
2029 void	 screen_write_cursorleft(struct screen_write_ctx *, u_int);
2030 void	 screen_write_alignmenttest(struct screen_write_ctx *);
2031 void	 screen_write_insertcharacter(struct screen_write_ctx *, u_int, u_int);
2032 void	 screen_write_deletecharacter(struct screen_write_ctx *, u_int, u_int);
2033 void	 screen_write_clearcharacter(struct screen_write_ctx *, u_int);
2034 void	 screen_write_insertline(struct screen_write_ctx *, u_int, u_int);
2035 void	 screen_write_deleteline(struct screen_write_ctx *, u_int, u_int);
2036 void	 screen_write_clearline(struct screen_write_ctx *, u_int);
2037 void	 screen_write_clearendofline(struct screen_write_ctx *, u_int);
2038 void	 screen_write_clearstartofline(struct screen_write_ctx *, u_int);
2039 void	 screen_write_cursormove(struct screen_write_ctx *, u_int, u_int);
2040 void	 screen_write_reverseindex(struct screen_write_ctx *);
2041 void	 screen_write_scrollregion(struct screen_write_ctx *, u_int, u_int);
2042 void	 screen_write_linefeed(struct screen_write_ctx *, int);
2043 void	 screen_write_carriagereturn(struct screen_write_ctx *);
2044 void	 screen_write_clearendofscreen(struct screen_write_ctx *, u_int);
2045 void	 screen_write_clearstartofscreen(struct screen_write_ctx *);
2046 void	 screen_write_clearscreen(struct screen_write_ctx *, u_int);
2047 void	 screen_write_clearhistory(struct screen_write_ctx *);
2048 void	 screen_write_cell(struct screen_write_ctx *, const struct grid_cell *);
2049 void	 screen_write_setselection(struct screen_write_ctx *, u_char *, u_int);
2050 void	 screen_write_rawstring(struct screen_write_ctx *, u_char *, u_int);
2051 
2052 /* screen-redraw.c */
2053 void	 screen_redraw_update(struct client *);
2054 void	 screen_redraw_screen(struct client *, int, int, int);
2055 void	 screen_redraw_pane(struct client *, struct window_pane *);
2056 
2057 /* screen.c */
2058 void	 screen_init(struct screen *, u_int, u_int, u_int);
2059 void	 screen_reinit(struct screen *);
2060 void	 screen_free(struct screen *);
2061 void	 screen_reset_tabs(struct screen *);
2062 void	 screen_set_cursor_style(struct screen *, u_int);
2063 void	 screen_set_cursor_colour(struct screen *, const char *);
2064 void	 screen_set_title(struct screen *, const char *);
2065 void	 screen_resize(struct screen *, u_int, u_int, int);
2066 void	 screen_set_selection(struct screen *,
2067 	     u_int, u_int, u_int, u_int, u_int, struct grid_cell *);
2068 void	 screen_clear_selection(struct screen *);
2069 void	 screen_hide_selection(struct screen *);
2070 int	 screen_check_selection(struct screen *, u_int, u_int);
2071 void	 screen_select_cell(struct screen *, struct grid_cell *,
2072 	     const struct grid_cell *);
2073 
2074 /* window.c */
2075 extern struct windows windows;
2076 extern struct window_pane_tree all_window_panes;
2077 int		 window_cmp(struct window *, struct window *);
2078 RB_PROTOTYPE(windows, window, entry, window_cmp);
2079 int		 winlink_cmp(struct winlink *, struct winlink *);
2080 RB_PROTOTYPE(winlinks, winlink, entry, winlink_cmp);
2081 int		 window_pane_cmp(struct window_pane *, struct window_pane *);
2082 RB_PROTOTYPE(window_pane_tree, window_pane, tree_entry, window_pane_cmp);
2083 struct winlink	*winlink_find_by_index(struct winlinks *, int);
2084 struct winlink	*winlink_find_by_window(struct winlinks *, struct window *);
2085 struct winlink	*winlink_find_by_window_id(struct winlinks *, u_int);
2086 u_int		 winlink_count(struct winlinks *);
2087 struct winlink	*winlink_add(struct winlinks *, int);
2088 void		 winlink_set_window(struct winlink *, struct window *);
2089 void		 winlink_remove(struct winlinks *, struct winlink *);
2090 struct winlink	*winlink_next(struct winlink *);
2091 struct winlink	*winlink_previous(struct winlink *);
2092 struct winlink	*winlink_next_by_number(struct winlink *, struct session *,
2093 		     int);
2094 struct winlink	*winlink_previous_by_number(struct winlink *, struct session *,
2095 		     int);
2096 void		 winlink_stack_push(struct winlink_stack *, struct winlink *);
2097 void		 winlink_stack_remove(struct winlink_stack *, struct winlink *);
2098 struct window	*window_find_by_id_str(const char *);
2099 struct window	*window_find_by_id(u_int);
2100 void		 window_update_activity(struct window *);
2101 struct window	*window_create(u_int, u_int);
2102 struct window	*window_create_spawn(const char *, int, char **, const char *,
2103 		     const char *, const char *, struct environ *,
2104 		     struct termios *, u_int, u_int, u_int, char **);
2105 struct window_pane *window_get_active_at(struct window *, u_int, u_int);
2106 struct window_pane *window_find_string(struct window *, const char *);
2107 int		 window_has_pane(struct window *, struct window_pane *);
2108 int		 window_set_active_pane(struct window *, struct window_pane *);
2109 void		 window_redraw_active_switch(struct window *,
2110 		     struct window_pane *);
2111 struct window_pane *window_add_pane(struct window *, struct window_pane *,
2112 		     u_int);
2113 void		 window_resize(struct window *, u_int, u_int);
2114 int		 window_zoom(struct window_pane *);
2115 int		 window_unzoom(struct window *);
2116 void		 window_lost_pane(struct window *, struct window_pane *);
2117 void		 window_remove_pane(struct window *, struct window_pane *);
2118 struct window_pane *window_pane_at_index(struct window *, u_int);
2119 struct window_pane *window_pane_next_by_number(struct window *,
2120 			struct window_pane *, u_int);
2121 struct window_pane *window_pane_previous_by_number(struct window *,
2122 			struct window_pane *, u_int);
2123 int		 window_pane_index(struct window_pane *, u_int *);
2124 u_int		 window_count_panes(struct window *);
2125 void		 window_destroy_panes(struct window *);
2126 struct window_pane *window_pane_find_by_id_str(const char *);
2127 struct window_pane *window_pane_find_by_id(u_int);
2128 int		 window_pane_spawn(struct window_pane *, int, char **,
2129 		     const char *, const char *, const char *, struct environ *,
2130 		     struct termios *, char **);
2131 void		 window_pane_resize(struct window_pane *, u_int, u_int);
2132 void		 window_pane_alternate_on(struct window_pane *,
2133 		     struct grid_cell *, int);
2134 void		 window_pane_alternate_off(struct window_pane *,
2135 		     struct grid_cell *, int);
2136 void		 window_pane_set_palette(struct window_pane *, u_int, int);
2137 void		 window_pane_unset_palette(struct window_pane *, u_int);
2138 void		 window_pane_reset_palette(struct window_pane *);
2139 int		 window_pane_get_palette(const struct window_pane *, int);
2140 int		 window_pane_set_mode(struct window_pane *,
2141 		     const struct window_mode *);
2142 void		 window_pane_reset_mode(struct window_pane *);
2143 void		 window_pane_key(struct window_pane *, struct client *,
2144 		     struct session *, key_code, struct mouse_event *);
2145 int		 window_pane_outside(struct window_pane *);
2146 int		 window_pane_visible(struct window_pane *);
2147 char		*window_pane_search(struct window_pane *, const char *,
2148 		     u_int *);
2149 char		*window_printable_flags(struct session *, struct winlink *);
2150 struct window_pane *window_pane_find_up(struct window_pane *);
2151 struct window_pane *window_pane_find_down(struct window_pane *);
2152 struct window_pane *window_pane_find_left(struct window_pane *);
2153 struct window_pane *window_pane_find_right(struct window_pane *);
2154 void		 window_set_name(struct window *, const char *);
2155 void		 window_remove_ref(struct window *);
2156 void		 winlink_clear_flags(struct winlink *);
2157 int		 winlink_shuffle_up(struct session *, struct winlink *);
2158 
2159 /* layout.c */
2160 u_int		 layout_count_cells(struct layout_cell *);
2161 struct layout_cell *layout_create_cell(struct layout_cell *);
2162 void		 layout_free_cell(struct layout_cell *);
2163 void		 layout_print_cell(struct layout_cell *, const char *, u_int);
2164 void		 layout_destroy_cell(struct window *, struct layout_cell *,
2165 		     struct layout_cell **);
2166 void		 layout_set_size(struct layout_cell *, u_int, u_int, u_int,
2167 		     u_int);
2168 void		 layout_make_leaf(struct layout_cell *, struct window_pane *);
2169 void		 layout_make_node(struct layout_cell *, enum layout_type);
2170 void		 layout_fix_offsets(struct layout_cell *);
2171 void		 layout_fix_panes(struct window *, u_int, u_int);
2172 void		 layout_resize_adjust(struct window *, struct layout_cell *,
2173 		     enum layout_type, int);
2174 void		 layout_init(struct window *, struct window_pane *);
2175 void		 layout_free(struct window *);
2176 void		 layout_resize(struct window *, u_int, u_int);
2177 void		 layout_resize_pane(struct window_pane *, enum layout_type,
2178 		     int, int);
2179 void		 layout_resize_pane_to(struct window_pane *, enum layout_type,
2180 		     u_int);
2181 void		 layout_assign_pane(struct layout_cell *, struct window_pane *);
2182 struct layout_cell *layout_split_pane(struct window_pane *, enum layout_type,
2183 		     int, int, int);
2184 void		 layout_close_pane(struct window_pane *);
2185 
2186 /* layout-custom.c */
2187 char		*layout_dump(struct layout_cell *);
2188 int		 layout_parse(struct window *, const char *);
2189 
2190 /* layout-set.c */
2191 int		 layout_set_lookup(const char *);
2192 u_int		 layout_set_select(struct window *, u_int);
2193 u_int		 layout_set_next(struct window *);
2194 u_int		 layout_set_previous(struct window *);
2195 
2196 /* window-clock.c */
2197 extern const struct window_mode window_clock_mode;
2198 extern const char window_clock_table[14][5][5];
2199 
2200 /* window-copy.c */
2201 extern const struct window_mode window_copy_mode;
2202 void		 window_copy_init_from_pane(struct window_pane *, int);
2203 void		 window_copy_init_for_output(struct window_pane *);
2204 void printflike(2, 3) window_copy_add(struct window_pane *, const char *, ...);
2205 void		 window_copy_vadd(struct window_pane *, const char *, va_list);
2206 void		 window_copy_pageup(struct window_pane *, int);
2207 void		 window_copy_start_drag(struct client *, struct mouse_event *);
2208 int		 window_copy_scroll_position(struct window_pane *);
2209 
2210 /* window-choose.c */
2211 extern const struct window_mode window_choose_mode;
2212 void		 window_choose_add(struct window_pane *,
2213 			 struct window_choose_data *);
2214 void		 window_choose_ready(struct window_pane *,
2215 		     u_int, void (*)(struct window_choose_data *));
2216 struct window_choose_data	*window_choose_data_create (int,
2217 		     struct client *, struct session *);
2218 void	window_choose_data_run(struct window_choose_data *);
2219 struct window_choose_data	*window_choose_add_window(struct window_pane *,
2220 			struct client *, struct session *, struct winlink *,
2221 			const char *, const char *, u_int);
2222 struct window_choose_data	*window_choose_add_session(struct window_pane *,
2223 			struct client *, struct session *, const char *,
2224 			const char *, u_int);
2225 void	window_choose_expand_all(struct window_pane *);
2226 void	window_choose_set_current(struct window_pane *, u_int);
2227 
2228 /* names.c */
2229 void	 check_window_name(struct window *);
2230 char	*default_window_name(struct window *);
2231 char	*parse_window_name(const char *);
2232 
2233 /* signal.c */
2234 void	set_signals(void(*)(int, short, void *), void *);
2235 void	clear_signals(int);
2236 
2237 /* control.c */
2238 void	control_callback(struct client *, int, void *);
2239 void printflike(2, 3) control_write(struct client *, const char *, ...);
2240 void	control_write_buffer(struct client *, struct evbuffer *);
2241 
2242 /* control-notify.c */
2243 void	control_notify_input(struct client *, struct window_pane *,
2244 	    struct evbuffer *);
2245 void	control_notify_window_layout_changed(struct window *);
2246 void	control_notify_window_unlinked(struct session *, struct window *);
2247 void	control_notify_window_linked(struct session *, struct window *);
2248 void	control_notify_window_renamed(struct window *);
2249 void	control_notify_client_session_changed(struct client *);
2250 void	control_notify_session_renamed(struct session *);
2251 void	control_notify_session_created(struct session *);
2252 void	control_notify_session_closed(struct session *);
2253 
2254 /* session.c */
2255 extern struct sessions sessions;
2256 extern struct session_groups session_groups;
2257 int	session_cmp(struct session *, struct session *);
2258 RB_PROTOTYPE(sessions, session, entry, session_cmp);
2259 int		 session_alive(struct session *);
2260 struct session	*session_find(const char *);
2261 struct session	*session_find_by_id_str(const char *);
2262 struct session	*session_find_by_id(u_int);
2263 struct session	*session_create(const char *, int, char **, const char *,
2264 		     const char *, struct environ *, struct termios *, int,
2265 		     u_int, u_int, char **);
2266 void		 session_destroy(struct session *);
2267 void		 session_unref(struct session *);
2268 int		 session_check_name(const char *);
2269 void		 session_update_activity(struct session *, struct timeval *);
2270 struct session	*session_next_session(struct session *);
2271 struct session	*session_previous_session(struct session *);
2272 struct winlink	*session_new(struct session *, const char *, int, char **,
2273 		     const char *, const char *, int, char **);
2274 struct winlink	*session_attach(struct session *, struct window *, int,
2275 		     char **);
2276 int		 session_detach(struct session *, struct winlink *);
2277 int		 session_has(struct session *, struct window *);
2278 int		 session_is_linked(struct session *, struct window *);
2279 int		 session_next(struct session *, int);
2280 int		 session_previous(struct session *, int);
2281 int		 session_select(struct session *, int);
2282 int		 session_last(struct session *);
2283 int		 session_set_current(struct session *, struct winlink *);
2284 struct session_group *session_group_find(struct session *);
2285 u_int		 session_group_index(struct session_group *);
2286 void		 session_group_add(struct session *, struct session *);
2287 void		 session_group_synchronize_to(struct session *);
2288 void		 session_group_synchronize_from(struct session *);
2289 void		 session_renumber_windows(struct session *);
2290 
2291 /* utf8.c */
2292 void		 utf8_set(struct utf8_data *, u_char);
2293 void		 utf8_copy(struct utf8_data *, const struct utf8_data *);
2294 enum utf8_state	 utf8_open(struct utf8_data *, u_char);
2295 enum utf8_state	 utf8_append(struct utf8_data *, u_char);
2296 enum utf8_state	 utf8_combine(const struct utf8_data *, wchar_t *);
2297 enum utf8_state	 utf8_split(wchar_t, struct utf8_data *);
2298 int		 utf8_strvis(char *, const char *, size_t, int);
2299 int		 utf8_stravis(char **, const char *, int);
2300 char		*utf8_sanitize(const char *);
2301 size_t		 utf8_strlen(const struct utf8_data *);
2302 u_int		 utf8_strwidth(const struct utf8_data *, ssize_t);
2303 struct utf8_data *utf8_fromcstr(const char *);
2304 char		*utf8_tocstr(struct utf8_data *);
2305 u_int		 utf8_cstrwidth(const char *);
2306 char		*utf8_rtrimcstr(const char *, u_int);
2307 char		*utf8_trimcstr(const char *, u_int);
2308 char		*utf8_padcstr(const char *, u_int);
2309 
2310 /* procname.c */
2311 char   *get_proc_name(int, char *);
2312 
2313 /* log.c */
2314 void	log_add_level(void);
2315 int	log_get_level(void);
2316 void	log_open(const char *);
2317 void	log_close(void);
2318 void printflike(1, 2) log_debug(const char *, ...);
2319 __dead void printflike(1, 2) fatal(const char *, ...);
2320 __dead void printflike(1, 2) fatalx(const char *, ...);
2321 
2322 /* style.c */
2323 int		 style_parse(const struct grid_cell *,
2324 		     struct grid_cell *, const char *);
2325 const char	*style_tostring(struct grid_cell *);
2326 void		 style_apply(struct grid_cell *, struct options *,
2327 		     const char *);
2328 void		 style_apply_update(struct grid_cell *, struct options *,
2329 		     const char *);
2330 int		 style_equal(const struct grid_cell *,
2331 		     const struct grid_cell *);
2332 
2333 /* pty.c */
2334 int		 pty_open(int *);
2335 pid_t		 pty_fork(int, int *, char *, size_t, struct winsize *);
2336 
2337 #endif /* TMUX_H */
2338