4 * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
20 #include <sys/ioctl.h>
32 static void server_client_free(int, short, void *);
33 static void server_client_check_pane_resize(struct window_pane
*);
34 static void server_client_check_pane_buffer(struct window_pane
*);
35 static void server_client_check_window_resize(struct window
*);
36 static key_code
server_client_check_mouse(struct client
*, struct key_event
*);
37 static void server_client_repeat_timer(int, short, void *);
38 static void server_client_click_timer(int, short, void *);
39 static void server_client_check_exit(struct client
*);
40 static void server_client_check_redraw(struct client
*);
41 static void server_client_check_modes(struct client
*);
42 static void server_client_set_title(struct client
*);
43 static void server_client_set_path(struct client
*);
44 static void server_client_reset_state(struct client
*);
45 static int server_client_is_bracket_pasting(struct client
*, key_code
);
46 static int server_client_assume_paste(struct session
*);
47 static void server_client_update_latest(struct client
*);
49 static void server_client_dispatch(struct imsg
*, void *);
50 static void server_client_dispatch_command(struct client
*, struct imsg
*);
51 static void server_client_dispatch_identify(struct client
*, struct imsg
*);
52 static void server_client_dispatch_shell(struct client
*);
54 /* Compare client windows. */
56 server_client_window_cmp(struct client_window
*cw1
,
57 struct client_window
*cw2
)
59 if (cw1
->window
< cw2
->window
)
61 if (cw1
->window
> cw2
->window
)
65 RB_GENERATE(client_windows
, client_window
, entry
, server_client_window_cmp
);
67 /* Number of attached clients. */
69 server_client_how_many(void)
75 TAILQ_FOREACH(c
, &clients
, entry
) {
76 if (c
->session
!= NULL
&& (~c
->flags
& CLIENT_UNATTACHEDFLAGS
))
82 /* Overlay timer callback. */
84 server_client_overlay_timer(__unused
int fd
, __unused
short events
, void *data
)
86 server_client_clear_overlay(data
);
89 /* Set an overlay on client. */
91 server_client_set_overlay(struct client
*c
, u_int delay
,
92 overlay_check_cb checkcb
, overlay_mode_cb modecb
,
93 overlay_draw_cb drawcb
, overlay_key_cb keycb
, overlay_free_cb freecb
,
94 overlay_resize_cb resizecb
, void *data
)
98 if (c
->overlay_draw
!= NULL
)
99 server_client_clear_overlay(c
);
101 tv
.tv_sec
= delay
/ 1000;
102 tv
.tv_usec
= (delay
% 1000) * 1000L;
104 if (event_initialized(&c
->overlay_timer
))
105 evtimer_del(&c
->overlay_timer
);
106 evtimer_set(&c
->overlay_timer
, server_client_overlay_timer
, c
);
108 evtimer_add(&c
->overlay_timer
, &tv
);
110 c
->overlay_check
= checkcb
;
111 c
->overlay_mode
= modecb
;
112 c
->overlay_draw
= drawcb
;
113 c
->overlay_key
= keycb
;
114 c
->overlay_free
= freecb
;
115 c
->overlay_resize
= resizecb
;
116 c
->overlay_data
= data
;
118 if (c
->overlay_check
== NULL
)
119 c
->tty
.flags
|= TTY_FREEZE
;
120 if (c
->overlay_mode
== NULL
)
121 c
->tty
.flags
|= TTY_NOCURSOR
;
122 server_redraw_client(c
);
125 /* Clear overlay mode on client. */
127 server_client_clear_overlay(struct client
*c
)
129 if (c
->overlay_draw
== NULL
)
132 if (event_initialized(&c
->overlay_timer
))
133 evtimer_del(&c
->overlay_timer
);
135 if (c
->overlay_free
!= NULL
)
136 c
->overlay_free(c
, c
->overlay_data
);
138 c
->overlay_check
= NULL
;
139 c
->overlay_mode
= NULL
;
140 c
->overlay_draw
= NULL
;
141 c
->overlay_key
= NULL
;
142 c
->overlay_free
= NULL
;
143 c
->overlay_data
= NULL
;
145 c
->tty
.flags
&= ~(TTY_FREEZE
|TTY_NOCURSOR
);
146 server_redraw_client(c
);
150 * Given overlay position and dimensions, return parts of the input range which
154 server_client_overlay_range(u_int x
, u_int y
, u_int sx
, u_int sy
, u_int px
,
155 u_int py
, u_int nx
, struct overlay_ranges
*r
)
159 /* Return up to 2 ranges. */
163 /* Trivial case of no overlap in the y direction. */
164 if (py
< y
|| py
> y
+ sy
- 1) {
172 /* Visible bit to the left of the popup. */
183 /* Visible bit to the right of the popup. */
197 /* Check if this client is inside this server. */
199 server_client_check_nested(struct client
*c
)
201 struct environ_entry
*envent
;
202 struct window_pane
*wp
;
204 envent
= environ_find(c
->environ
, "TMUX");
205 if (envent
== NULL
|| *envent
->value
== '\0')
208 RB_FOREACH(wp
, window_pane_tree
, &all_window_panes
) {
209 if (strcmp(wp
->tty
, c
->ttyname
) == 0)
215 /* Set client key table. */
217 server_client_set_key_table(struct client
*c
, const char *name
)
220 name
= server_client_get_key_table(c
);
222 key_bindings_unref_table(c
->keytable
);
223 c
->keytable
= key_bindings_get_table(name
, 1);
224 c
->keytable
->references
++;
227 /* Get default key table. */
229 server_client_get_key_table(struct client
*c
)
231 struct session
*s
= c
->session
;
237 name
= options_get_string(s
->options
, "key-table");
243 /* Is this table the default key table? */
245 server_client_is_default_key_table(struct client
*c
, struct key_table
*table
)
247 return (strcmp(table
->name
, server_client_get_key_table(c
)) == 0);
250 /* Create a new client. */
252 server_client_create(int fd
)
258 c
= xcalloc(1, sizeof *c
);
260 c
->peer
= proc_add_peer(server_proc
, fd
, server_client_dispatch
, c
);
262 if (gettimeofday(&c
->creation_time
, NULL
) != 0)
263 fatal("gettimeofday failed");
264 memcpy(&c
->activity_time
, &c
->creation_time
, sizeof c
->activity_time
);
266 c
->environ
= environ_create();
271 c
->queue
= cmdq_new();
272 RB_INIT(&c
->windows
);
279 c
->flags
|= CLIENT_FOCUSED
;
281 c
->keytable
= key_bindings_get_table("root", 1);
282 c
->keytable
->references
++;
284 evtimer_set(&c
->repeat_timer
, server_client_repeat_timer
, c
);
285 evtimer_set(&c
->click_timer
, server_client_click_timer
, c
);
287 TAILQ_INSERT_TAIL(&clients
, c
, entry
);
288 log_debug("new client %p", c
);
292 /* Open client terminal if needed. */
294 server_client_open(struct client
*c
, char **cause
)
296 const char *ttynam
= _PATH_TTY
;
298 if (c
->flags
& CLIENT_CONTROL
)
301 if (strcmp(c
->ttyname
, ttynam
) == 0||
302 ((isatty(STDIN_FILENO
) &&
303 (ttynam
= ttyname(STDIN_FILENO
)) != NULL
&&
304 strcmp(c
->ttyname
, ttynam
) == 0) ||
305 (isatty(STDOUT_FILENO
) &&
306 (ttynam
= ttyname(STDOUT_FILENO
)) != NULL
&&
307 strcmp(c
->ttyname
, ttynam
) == 0) ||
308 (isatty(STDERR_FILENO
) &&
309 (ttynam
= ttyname(STDERR_FILENO
)) != NULL
&&
310 strcmp(c
->ttyname
, ttynam
) == 0))) {
311 xasprintf(cause
, "can't use %s", c
->ttyname
);
315 if (!(c
->flags
& CLIENT_TERMINAL
)) {
316 *cause
= xstrdup("not a terminal");
320 if (tty_open(&c
->tty
, cause
) != 0)
326 /* Lost an attached client. */
328 server_client_attached_lost(struct client
*c
)
333 struct client
*found
;
335 log_debug("lost attached client %p", c
);
338 * By this point the session in the client has been cleared so walk all
339 * windows to find any with this client as the latest.
341 RB_FOREACH(w
, windows
, &windows
) {
346 TAILQ_FOREACH(loop
, &clients
, entry
) {
348 if (loop
== c
|| s
== NULL
|| s
->curw
->window
!= w
)
350 if (found
== NULL
|| timercmp(&loop
->activity_time
,
351 &found
->activity_time
, >))
355 server_client_update_latest(found
);
359 /* Set client session. */
361 server_client_set_session(struct client
*c
, struct session
*s
)
363 struct session
*old
= c
->session
;
365 if (s
!= NULL
&& c
->session
!= NULL
&& c
->session
!= s
)
366 c
->last_session
= c
->session
;
368 c
->last_session
= NULL
;
370 c
->flags
|= CLIENT_FOCUSED
;
372 if (old
!= NULL
&& old
->curw
!= NULL
)
373 window_update_focus(old
->curw
->window
);
376 window_update_focus(s
->curw
->window
);
377 session_update_activity(s
, NULL
);
378 gettimeofday(&s
->last_attached_time
, NULL
);
379 s
->curw
->flags
&= ~WINLINK_ALERTFLAGS
;
380 s
->curw
->window
->latest
= c
;
381 alerts_check_session(s
);
382 tty_update_client_offset(c
);
383 status_timer_start(c
);
384 notify_client("client-session-changed", c
);
385 server_redraw_client(c
);
388 server_check_unattached();
389 server_update_socket();
394 server_client_lost(struct client
*c
)
396 struct client_file
*cf
, *cf1
;
397 struct client_window
*cw
, *cw1
;
399 c
->flags
|= CLIENT_DEAD
;
401 server_client_clear_overlay(c
);
402 status_prompt_clear(c
);
403 status_message_clear(c
);
405 RB_FOREACH_SAFE(cf
, client_files
, &c
->files
, cf1
) {
409 RB_FOREACH_SAFE(cw
, client_windows
, &c
->windows
, cw1
) {
410 RB_REMOVE(client_windows
, &c
->windows
, cw
);
414 TAILQ_REMOVE(&clients
, c
, entry
);
415 log_debug("lost client %p", c
);
417 if (c
->flags
& CLIENT_ATTACHED
) {
418 server_client_attached_lost(c
);
419 notify_client("client-detached", c
);
422 if (c
->flags
& CLIENT_CONTROL
)
424 if (c
->flags
& CLIENT_TERMINAL
)
427 free(c
->clipboard_panes
);
431 tty_term_free_list(c
->term_caps
, c
->term_ncaps
);
436 free((void *)c
->cwd
);
438 evtimer_del(&c
->repeat_timer
);
439 evtimer_del(&c
->click_timer
);
441 key_bindings_unref_table(c
->keytable
);
443 free(c
->message_string
);
444 if (event_initialized(&c
->message_timer
))
445 evtimer_del(&c
->message_timer
);
447 free(c
->prompt_saved
);
448 free(c
->prompt_string
);
449 free(c
->prompt_buffer
);
451 format_lost_client(c
);
452 environ_free(c
->environ
);
454 proc_remove_peer(c
->peer
);
463 server_client_unref(c
);
465 server_add_accept(0); /* may be more file descriptors now */
468 server_check_unattached();
469 server_update_socket();
472 /* Remove reference from a client. */
474 server_client_unref(struct client
*c
)
476 log_debug("unref client %p (%d references)", c
, c
->references
);
479 if (c
->references
== 0)
480 event_once(-1, EV_TIMEOUT
, server_client_free
, c
, NULL
);
483 /* Free dead client. */
485 server_client_free(__unused
int fd
, __unused
short events
, void *arg
)
487 struct client
*c
= arg
;
489 log_debug("free client %p (%d references)", c
, c
->references
);
493 if (c
->references
== 0) {
494 free((void *)c
->name
);
499 /* Suspend a client. */
501 server_client_suspend(struct client
*c
)
503 struct session
*s
= c
->session
;
505 if (s
== NULL
|| (c
->flags
& CLIENT_UNATTACHEDFLAGS
))
508 tty_stop_tty(&c
->tty
);
509 c
->flags
|= CLIENT_SUSPENDED
;
510 proc_send(c
->peer
, MSG_SUSPEND
, -1, NULL
, 0);
513 /* Detach a client. */
515 server_client_detach(struct client
*c
, enum msgtype msgtype
)
517 struct session
*s
= c
->session
;
519 if (s
== NULL
|| (c
->flags
& CLIENT_NODETACHFLAGS
))
522 c
->flags
|= CLIENT_EXIT
;
524 c
->exit_type
= CLIENT_EXIT_DETACH
;
525 c
->exit_msgtype
= msgtype
;
526 c
->exit_session
= xstrdup(s
->name
);
529 /* Execute command to replace a client. */
531 server_client_exec(struct client
*c
, const char *cmd
)
533 struct session
*s
= c
->session
;
536 size_t cmdsize
, shellsize
;
540 cmdsize
= strlen(cmd
) + 1;
543 shell
= options_get_string(s
->options
, "default-shell");
545 shell
= options_get_string(global_s_options
, "default-shell");
546 if (!checkshell(shell
))
547 shell
= _PATH_BSHELL
;
548 shellsize
= strlen(shell
) + 1;
550 msg
= xmalloc(cmdsize
+ shellsize
);
551 memcpy(msg
, cmd
, cmdsize
);
552 memcpy(msg
+ cmdsize
, shell
, shellsize
);
554 proc_send(c
->peer
, MSG_EXEC
, -1, msg
, cmdsize
+ shellsize
);
558 /* Check for mouse keys. */
560 server_client_check_mouse(struct client
*c
, struct key_event
*event
)
562 struct mouse_event
*m
= &event
->m
;
563 struct session
*s
= c
->session
, *fs
;
565 struct window_pane
*wp
, *fwp
;
566 u_int x
, y
, b
, sx
, sy
, px
, py
;
570 struct style_range
*sr
;
579 TRIPLE
} type
= NOTYPE
;
586 BORDER
} where
= NOWHERE
;
588 log_debug("%s mouse %02x at %u,%u (last %u,%u) (%d)", c
->name
, m
->b
,
589 m
->x
, m
->y
, m
->lx
, m
->ly
, c
->tty
.mouse_drag_flag
);
591 /* What type of event is this? */
592 if (event
->key
== KEYC_DOUBLECLICK
) {
594 x
= m
->x
, y
= m
->y
, b
= m
->b
;
596 log_debug("double-click at %u,%u", x
, y
);
597 } else if ((m
->sgr_type
!= ' ' &&
598 MOUSE_DRAG(m
->sgr_b
) &&
599 MOUSE_RELEASE(m
->sgr_b
)) ||
600 (m
->sgr_type
== ' ' &&
602 MOUSE_RELEASE(m
->b
) &&
603 MOUSE_RELEASE(m
->lb
))) {
605 x
= m
->x
, y
= m
->y
, b
= 0;
606 log_debug("move at %u,%u", x
, y
);
607 } else if (MOUSE_DRAG(m
->b
)) {
609 if (c
->tty
.mouse_drag_flag
) {
610 x
= m
->x
, y
= m
->y
, b
= m
->b
;
611 if (x
== m
->lx
&& y
== m
->ly
)
612 return (KEYC_UNKNOWN
);
613 log_debug("drag update at %u,%u", x
, y
);
615 x
= m
->lx
, y
= m
->ly
, b
= m
->lb
;
616 log_debug("drag start at %u,%u", x
, y
);
618 } else if (MOUSE_WHEEL(m
->b
)) {
620 x
= m
->x
, y
= m
->y
, b
= m
->b
;
621 log_debug("wheel at %u,%u", x
, y
);
622 } else if (MOUSE_RELEASE(m
->b
)) {
624 x
= m
->x
, y
= m
->y
, b
= m
->lb
;
625 log_debug("up at %u,%u", x
, y
);
627 if (c
->flags
& CLIENT_DOUBLECLICK
) {
628 evtimer_del(&c
->click_timer
);
629 c
->flags
&= ~CLIENT_DOUBLECLICK
;
630 if (m
->b
== c
->click_button
) {
632 x
= m
->x
, y
= m
->y
, b
= m
->b
;
633 log_debug("second-click at %u,%u", x
, y
);
634 c
->flags
|= CLIENT_TRIPLECLICK
;
636 } else if (c
->flags
& CLIENT_TRIPLECLICK
) {
637 evtimer_del(&c
->click_timer
);
638 c
->flags
&= ~CLIENT_TRIPLECLICK
;
639 if (m
->b
== c
->click_button
) {
641 x
= m
->x
, y
= m
->y
, b
= m
->b
;
642 log_debug("triple-click at %u,%u", x
, y
);
647 x
= m
->x
, y
= m
->y
, b
= m
->b
;
648 log_debug("down at %u,%u", x
, y
);
649 c
->flags
|= CLIENT_DOUBLECLICK
;
652 if (KEYC_CLICK_TIMEOUT
!= 0) {
653 memcpy(&c
->click_event
, m
, sizeof c
->click_event
);
654 c
->click_button
= m
->b
;
656 log_debug("click timer started");
657 tv
.tv_sec
= KEYC_CLICK_TIMEOUT
/ 1000;
658 tv
.tv_usec
= (KEYC_CLICK_TIMEOUT
% 1000) * 1000L;
659 evtimer_del(&c
->click_timer
);
660 evtimer_add(&c
->click_timer
, &tv
);
666 return (KEYC_UNKNOWN
);
668 /* Save the session. */
674 /* Is this on the status line? */
675 m
->statusat
= status_at_line(c
);
676 m
->statuslines
= status_line_size(c
);
677 if (m
->statusat
!= -1 &&
678 y
>= (u_int
)m
->statusat
&&
679 y
< m
->statusat
+ m
->statuslines
) {
680 sr
= status_get_range(c
, x
, y
- m
->statusat
);
682 where
= STATUS_DEFAULT
;
685 case STYLE_RANGE_NONE
:
686 return (KEYC_UNKNOWN
);
687 case STYLE_RANGE_LEFT
:
688 log_debug("mouse range: left");
691 case STYLE_RANGE_RIGHT
:
692 log_debug("mouse range: right");
693 where
= STATUS_RIGHT
;
695 case STYLE_RANGE_PANE
:
696 fwp
= window_pane_find_by_id(sr
->argument
);
698 return (KEYC_UNKNOWN
);
699 m
->wp
= sr
->argument
;
701 log_debug("mouse range: pane %%%u", m
->wp
);
704 case STYLE_RANGE_WINDOW
:
705 fwl
= winlink_find_by_index(&s
->windows
,
708 return (KEYC_UNKNOWN
);
709 m
->w
= fwl
->window
->id
;
711 log_debug("mouse range: window @%u", m
->w
);
714 case STYLE_RANGE_SESSION
:
715 fs
= session_find_by_id(sr
->argument
);
717 return (KEYC_UNKNOWN
);
720 log_debug("mouse range: session $%u", m
->s
);
723 case STYLE_RANGE_USER
:
730 /* Not on status line. Adjust position and check for border or pane. */
731 if (where
== NOWHERE
) {
733 if (m
->statusat
== 0 && y
>= m
->statuslines
)
734 py
= y
- m
->statuslines
;
735 else if (m
->statusat
> 0 && y
>= (u_int
)m
->statusat
)
736 py
= m
->statusat
- 1;
740 tty_window_offset(&c
->tty
, &m
->ox
, &m
->oy
, &sx
, &sy
);
741 log_debug("mouse window @%u at %u,%u (%ux%u)",
742 s
->curw
->window
->id
, m
->ox
, m
->oy
, sx
, sy
);
743 if (px
> sx
|| py
> sy
)
744 return (KEYC_UNKNOWN
);
748 /* Try the pane borders if not zoomed. */
749 if (~s
->curw
->window
->flags
& WINDOW_ZOOMED
) {
750 TAILQ_FOREACH(wp
, &s
->curw
->window
->panes
, entry
) {
751 if ((wp
->xoff
+ wp
->sx
== px
&&
752 wp
->yoff
<= 1 + py
&&
753 wp
->yoff
+ wp
->sy
>= py
) ||
754 (wp
->yoff
+ wp
->sy
== py
&&
755 wp
->xoff
<= 1 + px
&&
756 wp
->xoff
+ wp
->sx
>= px
))
763 /* Otherwise try inside the pane. */
764 if (where
== NOWHERE
) {
765 wp
= window_get_active_at(s
->curw
->window
, px
, py
);
769 return (KEYC_UNKNOWN
);
772 log_debug("mouse %u,%u on pane %%%u", x
, y
, wp
->id
);
773 else if (where
== BORDER
)
774 log_debug("mouse on pane %%%u border", wp
->id
);
776 m
->w
= wp
->window
->id
;
780 /* Stop dragging if needed. */
781 if (type
!= DRAG
&& type
!= WHEEL
&& c
->tty
.mouse_drag_flag
!= 0) {
782 if (c
->tty
.mouse_drag_release
!= NULL
)
783 c
->tty
.mouse_drag_release(c
, m
);
785 c
->tty
.mouse_drag_update
= NULL
;
786 c
->tty
.mouse_drag_release
= NULL
;
789 * End a mouse drag by passing a MouseDragEnd key corresponding
790 * to the button that started the drag.
792 switch (c
->tty
.mouse_drag_flag
- 1) {
795 key
= KEYC_MOUSEDRAGEND1_PANE
;
797 key
= KEYC_MOUSEDRAGEND1_STATUS
;
798 if (where
== STATUS_LEFT
)
799 key
= KEYC_MOUSEDRAGEND1_STATUS_LEFT
;
800 if (where
== STATUS_RIGHT
)
801 key
= KEYC_MOUSEDRAGEND1_STATUS_RIGHT
;
802 if (where
== STATUS_DEFAULT
)
803 key
= KEYC_MOUSEDRAGEND1_STATUS_DEFAULT
;
805 key
= KEYC_MOUSEDRAGEND1_BORDER
;
809 key
= KEYC_MOUSEDRAGEND2_PANE
;
811 key
= KEYC_MOUSEDRAGEND2_STATUS
;
812 if (where
== STATUS_LEFT
)
813 key
= KEYC_MOUSEDRAGEND2_STATUS_LEFT
;
814 if (where
== STATUS_RIGHT
)
815 key
= KEYC_MOUSEDRAGEND2_STATUS_RIGHT
;
816 if (where
== STATUS_DEFAULT
)
817 key
= KEYC_MOUSEDRAGEND2_STATUS_DEFAULT
;
819 key
= KEYC_MOUSEDRAGEND2_BORDER
;
823 key
= KEYC_MOUSEDRAGEND3_PANE
;
825 key
= KEYC_MOUSEDRAGEND3_STATUS
;
826 if (where
== STATUS_LEFT
)
827 key
= KEYC_MOUSEDRAGEND3_STATUS_LEFT
;
828 if (where
== STATUS_RIGHT
)
829 key
= KEYC_MOUSEDRAGEND3_STATUS_RIGHT
;
830 if (where
== STATUS_DEFAULT
)
831 key
= KEYC_MOUSEDRAGEND3_STATUS_DEFAULT
;
833 key
= KEYC_MOUSEDRAGEND3_BORDER
;
837 key
= KEYC_MOUSEDRAGEND6_PANE
;
839 key
= KEYC_MOUSEDRAGEND6_STATUS
;
840 if (where
== STATUS_LEFT
)
841 key
= KEYC_MOUSEDRAGEND6_STATUS_LEFT
;
842 if (where
== STATUS_RIGHT
)
843 key
= KEYC_MOUSEDRAGEND6_STATUS_RIGHT
;
844 if (where
== STATUS_DEFAULT
)
845 key
= KEYC_MOUSEDRAGEND6_STATUS_DEFAULT
;
847 key
= KEYC_MOUSEDRAGEND6_BORDER
;
851 key
= KEYC_MOUSEDRAGEND7_PANE
;
853 key
= KEYC_MOUSEDRAGEND7_STATUS
;
854 if (where
== STATUS_LEFT
)
855 key
= KEYC_MOUSEDRAGEND7_STATUS_LEFT
;
856 if (where
== STATUS_RIGHT
)
857 key
= KEYC_MOUSEDRAGEND7_STATUS_RIGHT
;
858 if (where
== STATUS_DEFAULT
)
859 key
= KEYC_MOUSEDRAGEND7_STATUS_DEFAULT
;
861 key
= KEYC_MOUSEDRAGEND7_BORDER
;
865 key
= KEYC_MOUSEDRAGEND8_PANE
;
867 key
= KEYC_MOUSEDRAGEND8_STATUS
;
868 if (where
== STATUS_LEFT
)
869 key
= KEYC_MOUSEDRAGEND8_STATUS_LEFT
;
870 if (where
== STATUS_RIGHT
)
871 key
= KEYC_MOUSEDRAGEND8_STATUS_RIGHT
;
872 if (where
== STATUS_DEFAULT
)
873 key
= KEYC_MOUSEDRAGEND8_STATUS_DEFAULT
;
875 key
= KEYC_MOUSEDRAGEND8_BORDER
;
879 key
= KEYC_MOUSEDRAGEND9_PANE
;
881 key
= KEYC_MOUSEDRAGEND9_STATUS
;
882 if (where
== STATUS_LEFT
)
883 key
= KEYC_MOUSEDRAGEND9_STATUS_LEFT
;
884 if (where
== STATUS_RIGHT
)
885 key
= KEYC_MOUSEDRAGEND9_STATUS_RIGHT
;
886 if (where
== STATUS_DEFAULT
)
887 key
= KEYC_MOUSEDRAGEND9_STATUS_DEFAULT
;
889 key
= KEYC_MOUSEDRAGEND9_BORDER
;
891 case MOUSE_BUTTON_10
:
893 key
= KEYC_MOUSEDRAGEND10_PANE
;
895 key
= KEYC_MOUSEDRAGEND10_STATUS
;
896 if (where
== STATUS_LEFT
)
897 key
= KEYC_MOUSEDRAGEND10_STATUS_LEFT
;
898 if (where
== STATUS_RIGHT
)
899 key
= KEYC_MOUSEDRAGEND10_STATUS_RIGHT
;
900 if (where
== STATUS_DEFAULT
)
901 key
= KEYC_MOUSEDRAGEND10_STATUS_DEFAULT
;
903 key
= KEYC_MOUSEDRAGEND10_BORDER
;
905 case MOUSE_BUTTON_11
:
907 key
= KEYC_MOUSEDRAGEND11_PANE
;
909 key
= KEYC_MOUSEDRAGEND11_STATUS
;
910 if (where
== STATUS_LEFT
)
911 key
= KEYC_MOUSEDRAGEND11_STATUS_LEFT
;
912 if (where
== STATUS_RIGHT
)
913 key
= KEYC_MOUSEDRAGEND11_STATUS_RIGHT
;
914 if (where
== STATUS_DEFAULT
)
915 key
= KEYC_MOUSEDRAGEND11_STATUS_DEFAULT
;
917 key
= KEYC_MOUSEDRAGEND11_BORDER
;
923 c
->tty
.mouse_drag_flag
= 0;
927 /* Convert to a key binding. */
934 key
= KEYC_MOUSEMOVE_PANE
;
936 key
= KEYC_MOUSEMOVE_STATUS
;
937 if (where
== STATUS_LEFT
)
938 key
= KEYC_MOUSEMOVE_STATUS_LEFT
;
939 if (where
== STATUS_RIGHT
)
940 key
= KEYC_MOUSEMOVE_STATUS_RIGHT
;
941 if (where
== STATUS_DEFAULT
)
942 key
= KEYC_MOUSEMOVE_STATUS_DEFAULT
;
944 key
= KEYC_MOUSEMOVE_BORDER
;
947 if (c
->tty
.mouse_drag_update
!= NULL
)
950 switch (MOUSE_BUTTONS(b
)) {
953 key
= KEYC_MOUSEDRAG1_PANE
;
955 key
= KEYC_MOUSEDRAG1_STATUS
;
956 if (where
== STATUS_LEFT
)
957 key
= KEYC_MOUSEDRAG1_STATUS_LEFT
;
958 if (where
== STATUS_RIGHT
)
959 key
= KEYC_MOUSEDRAG1_STATUS_RIGHT
;
960 if (where
== STATUS_DEFAULT
)
961 key
= KEYC_MOUSEDRAG1_STATUS_DEFAULT
;
963 key
= KEYC_MOUSEDRAG1_BORDER
;
967 key
= KEYC_MOUSEDRAG2_PANE
;
969 key
= KEYC_MOUSEDRAG2_STATUS
;
970 if (where
== STATUS_LEFT
)
971 key
= KEYC_MOUSEDRAG2_STATUS_LEFT
;
972 if (where
== STATUS_RIGHT
)
973 key
= KEYC_MOUSEDRAG2_STATUS_RIGHT
;
974 if (where
== STATUS_DEFAULT
)
975 key
= KEYC_MOUSEDRAG2_STATUS_DEFAULT
;
977 key
= KEYC_MOUSEDRAG2_BORDER
;
981 key
= KEYC_MOUSEDRAG3_PANE
;
983 key
= KEYC_MOUSEDRAG3_STATUS
;
984 if (where
== STATUS_LEFT
)
985 key
= KEYC_MOUSEDRAG3_STATUS_LEFT
;
986 if (where
== STATUS_RIGHT
)
987 key
= KEYC_MOUSEDRAG3_STATUS_RIGHT
;
988 if (where
== STATUS_DEFAULT
)
989 key
= KEYC_MOUSEDRAG3_STATUS_DEFAULT
;
991 key
= KEYC_MOUSEDRAG3_BORDER
;
995 key
= KEYC_MOUSEDRAG6_PANE
;
997 key
= KEYC_MOUSEDRAG6_STATUS
;
998 if (where
== STATUS_LEFT
)
999 key
= KEYC_MOUSEDRAG6_STATUS_LEFT
;
1000 if (where
== STATUS_RIGHT
)
1001 key
= KEYC_MOUSEDRAG6_STATUS_RIGHT
;
1002 if (where
== STATUS_DEFAULT
)
1003 key
= KEYC_MOUSEDRAG6_STATUS_DEFAULT
;
1004 if (where
== BORDER
)
1005 key
= KEYC_MOUSEDRAG6_BORDER
;
1007 case MOUSE_BUTTON_7
:
1009 key
= KEYC_MOUSEDRAG7_PANE
;
1010 if (where
== STATUS
)
1011 key
= KEYC_MOUSEDRAG7_STATUS
;
1012 if (where
== STATUS_LEFT
)
1013 key
= KEYC_MOUSEDRAG7_STATUS_LEFT
;
1014 if (where
== STATUS_RIGHT
)
1015 key
= KEYC_MOUSEDRAG7_STATUS_RIGHT
;
1016 if (where
== STATUS_DEFAULT
)
1017 key
= KEYC_MOUSEDRAG7_STATUS_DEFAULT
;
1018 if (where
== BORDER
)
1019 key
= KEYC_MOUSEDRAG7_BORDER
;
1021 case MOUSE_BUTTON_8
:
1023 key
= KEYC_MOUSEDRAG8_PANE
;
1024 if (where
== STATUS
)
1025 key
= KEYC_MOUSEDRAG8_STATUS
;
1026 if (where
== STATUS_LEFT
)
1027 key
= KEYC_MOUSEDRAG8_STATUS_LEFT
;
1028 if (where
== STATUS_RIGHT
)
1029 key
= KEYC_MOUSEDRAG8_STATUS_RIGHT
;
1030 if (where
== STATUS_DEFAULT
)
1031 key
= KEYC_MOUSEDRAG8_STATUS_DEFAULT
;
1032 if (where
== BORDER
)
1033 key
= KEYC_MOUSEDRAG8_BORDER
;
1035 case MOUSE_BUTTON_9
:
1037 key
= KEYC_MOUSEDRAG9_PANE
;
1038 if (where
== STATUS
)
1039 key
= KEYC_MOUSEDRAG9_STATUS
;
1040 if (where
== STATUS_LEFT
)
1041 key
= KEYC_MOUSEDRAG9_STATUS_LEFT
;
1042 if (where
== STATUS_RIGHT
)
1043 key
= KEYC_MOUSEDRAG9_STATUS_RIGHT
;
1044 if (where
== STATUS_DEFAULT
)
1045 key
= KEYC_MOUSEDRAG9_STATUS_DEFAULT
;
1046 if (where
== BORDER
)
1047 key
= KEYC_MOUSEDRAG9_BORDER
;
1049 case MOUSE_BUTTON_10
:
1051 key
= KEYC_MOUSEDRAG10_PANE
;
1052 if (where
== STATUS
)
1053 key
= KEYC_MOUSEDRAG10_STATUS
;
1054 if (where
== STATUS_LEFT
)
1055 key
= KEYC_MOUSEDRAG10_STATUS_LEFT
;
1056 if (where
== STATUS_RIGHT
)
1057 key
= KEYC_MOUSEDRAG10_STATUS_RIGHT
;
1058 if (where
== STATUS_DEFAULT
)
1059 key
= KEYC_MOUSEDRAG10_STATUS_DEFAULT
;
1060 if (where
== BORDER
)
1061 key
= KEYC_MOUSEDRAG10_BORDER
;
1063 case MOUSE_BUTTON_11
:
1065 key
= KEYC_MOUSEDRAG11_PANE
;
1066 if (where
== STATUS
)
1067 key
= KEYC_MOUSEDRAG11_STATUS
;
1068 if (where
== STATUS_LEFT
)
1069 key
= KEYC_MOUSEDRAG11_STATUS_LEFT
;
1070 if (where
== STATUS_RIGHT
)
1071 key
= KEYC_MOUSEDRAG11_STATUS_RIGHT
;
1072 if (where
== STATUS_DEFAULT
)
1073 key
= KEYC_MOUSEDRAG11_STATUS_DEFAULT
;
1074 if (where
== BORDER
)
1075 key
= KEYC_MOUSEDRAG11_BORDER
;
1081 * Begin a drag by setting the flag to a non-zero value that
1082 * corresponds to the mouse button in use.
1084 c
->tty
.mouse_drag_flag
= MOUSE_BUTTONS(b
) + 1;
1087 if (MOUSE_BUTTONS(b
) == MOUSE_WHEEL_UP
) {
1089 key
= KEYC_WHEELUP_PANE
;
1090 if (where
== STATUS
)
1091 key
= KEYC_WHEELUP_STATUS
;
1092 if (where
== STATUS_LEFT
)
1093 key
= KEYC_WHEELUP_STATUS_LEFT
;
1094 if (where
== STATUS_RIGHT
)
1095 key
= KEYC_WHEELUP_STATUS_RIGHT
;
1096 if (where
== STATUS_DEFAULT
)
1097 key
= KEYC_WHEELUP_STATUS_DEFAULT
;
1098 if (where
== BORDER
)
1099 key
= KEYC_WHEELUP_BORDER
;
1102 key
= KEYC_WHEELDOWN_PANE
;
1103 if (where
== STATUS
)
1104 key
= KEYC_WHEELDOWN_STATUS
;
1105 if (where
== STATUS_LEFT
)
1106 key
= KEYC_WHEELDOWN_STATUS_LEFT
;
1107 if (where
== STATUS_RIGHT
)
1108 key
= KEYC_WHEELDOWN_STATUS_RIGHT
;
1109 if (where
== STATUS_DEFAULT
)
1110 key
= KEYC_WHEELDOWN_STATUS_DEFAULT
;
1111 if (where
== BORDER
)
1112 key
= KEYC_WHEELDOWN_BORDER
;
1116 switch (MOUSE_BUTTONS(b
)) {
1117 case MOUSE_BUTTON_1
:
1119 key
= KEYC_MOUSEUP1_PANE
;
1120 if (where
== STATUS
)
1121 key
= KEYC_MOUSEUP1_STATUS
;
1122 if (where
== STATUS_LEFT
)
1123 key
= KEYC_MOUSEUP1_STATUS_LEFT
;
1124 if (where
== STATUS_RIGHT
)
1125 key
= KEYC_MOUSEUP1_STATUS_RIGHT
;
1126 if (where
== STATUS_DEFAULT
)
1127 key
= KEYC_MOUSEUP1_STATUS_DEFAULT
;
1128 if (where
== BORDER
)
1129 key
= KEYC_MOUSEUP1_BORDER
;
1131 case MOUSE_BUTTON_2
:
1133 key
= KEYC_MOUSEUP2_PANE
;
1134 if (where
== STATUS
)
1135 key
= KEYC_MOUSEUP2_STATUS
;
1136 if (where
== STATUS_LEFT
)
1137 key
= KEYC_MOUSEUP2_STATUS_LEFT
;
1138 if (where
== STATUS_RIGHT
)
1139 key
= KEYC_MOUSEUP2_STATUS_RIGHT
;
1140 if (where
== STATUS_DEFAULT
)
1141 key
= KEYC_MOUSEUP2_STATUS_DEFAULT
;
1142 if (where
== BORDER
)
1143 key
= KEYC_MOUSEUP2_BORDER
;
1145 case MOUSE_BUTTON_3
:
1147 key
= KEYC_MOUSEUP3_PANE
;
1148 if (where
== STATUS
)
1149 key
= KEYC_MOUSEUP3_STATUS
;
1150 if (where
== STATUS_LEFT
)
1151 key
= KEYC_MOUSEUP3_STATUS_LEFT
;
1152 if (where
== STATUS_RIGHT
)
1153 key
= KEYC_MOUSEUP3_STATUS_RIGHT
;
1154 if (where
== STATUS_DEFAULT
)
1155 key
= KEYC_MOUSEUP3_STATUS_DEFAULT
;
1156 if (where
== BORDER
)
1157 key
= KEYC_MOUSEUP3_BORDER
;
1159 case MOUSE_BUTTON_6
:
1161 key
= KEYC_MOUSEUP6_PANE
;
1162 if (where
== STATUS
)
1163 key
= KEYC_MOUSEUP6_STATUS
;
1164 if (where
== STATUS_LEFT
)
1165 key
= KEYC_MOUSEUP6_STATUS_LEFT
;
1166 if (where
== STATUS_RIGHT
)
1167 key
= KEYC_MOUSEUP6_STATUS_RIGHT
;
1168 if (where
== STATUS_DEFAULT
)
1169 key
= KEYC_MOUSEUP6_STATUS_DEFAULT
;
1170 if (where
== BORDER
)
1171 key
= KEYC_MOUSEUP6_BORDER
;
1173 case MOUSE_BUTTON_7
:
1175 key
= KEYC_MOUSEUP7_PANE
;
1176 if (where
== STATUS
)
1177 key
= KEYC_MOUSEUP7_STATUS
;
1178 if (where
== STATUS_LEFT
)
1179 key
= KEYC_MOUSEUP7_STATUS_LEFT
;
1180 if (where
== STATUS_RIGHT
)
1181 key
= KEYC_MOUSEUP7_STATUS_RIGHT
;
1182 if (where
== STATUS_DEFAULT
)
1183 key
= KEYC_MOUSEUP7_STATUS_DEFAULT
;
1184 if (where
== BORDER
)
1185 key
= KEYC_MOUSEUP7_BORDER
;
1187 case MOUSE_BUTTON_8
:
1189 key
= KEYC_MOUSEUP8_PANE
;
1190 if (where
== STATUS
)
1191 key
= KEYC_MOUSEUP8_STATUS
;
1192 if (where
== STATUS_LEFT
)
1193 key
= KEYC_MOUSEUP8_STATUS_LEFT
;
1194 if (where
== STATUS_RIGHT
)
1195 key
= KEYC_MOUSEUP8_STATUS_RIGHT
;
1196 if (where
== STATUS_DEFAULT
)
1197 key
= KEYC_MOUSEUP8_STATUS_DEFAULT
;
1198 if (where
== BORDER
)
1199 key
= KEYC_MOUSEUP8_BORDER
;
1201 case MOUSE_BUTTON_9
:
1203 key
= KEYC_MOUSEUP9_PANE
;
1204 if (where
== STATUS
)
1205 key
= KEYC_MOUSEUP9_STATUS
;
1206 if (where
== STATUS_LEFT
)
1207 key
= KEYC_MOUSEUP9_STATUS_LEFT
;
1208 if (where
== STATUS_RIGHT
)
1209 key
= KEYC_MOUSEUP9_STATUS_RIGHT
;
1210 if (where
== STATUS_DEFAULT
)
1211 key
= KEYC_MOUSEUP9_STATUS_DEFAULT
;
1212 if (where
== BORDER
)
1213 key
= KEYC_MOUSEUP9_BORDER
;
1215 case MOUSE_BUTTON_10
:
1217 key
= KEYC_MOUSEUP1_PANE
;
1218 if (where
== STATUS
)
1219 key
= KEYC_MOUSEUP1_STATUS
;
1220 if (where
== STATUS_LEFT
)
1221 key
= KEYC_MOUSEUP1_STATUS_LEFT
;
1222 if (where
== STATUS_RIGHT
)
1223 key
= KEYC_MOUSEUP1_STATUS_RIGHT
;
1224 if (where
== STATUS_DEFAULT
)
1225 key
= KEYC_MOUSEUP1_STATUS_DEFAULT
;
1226 if (where
== BORDER
)
1227 key
= KEYC_MOUSEUP1_BORDER
;
1229 case MOUSE_BUTTON_11
:
1231 key
= KEYC_MOUSEUP11_PANE
;
1232 if (where
== STATUS
)
1233 key
= KEYC_MOUSEUP11_STATUS
;
1234 if (where
== STATUS_LEFT
)
1235 key
= KEYC_MOUSEUP11_STATUS_LEFT
;
1236 if (where
== STATUS_RIGHT
)
1237 key
= KEYC_MOUSEUP11_STATUS_RIGHT
;
1238 if (where
== STATUS_DEFAULT
)
1239 key
= KEYC_MOUSEUP11_STATUS_DEFAULT
;
1240 if (where
== BORDER
)
1241 key
= KEYC_MOUSEUP11_BORDER
;
1246 switch (MOUSE_BUTTONS(b
)) {
1247 case MOUSE_BUTTON_1
:
1249 key
= KEYC_MOUSEDOWN1_PANE
;
1250 if (where
== STATUS
)
1251 key
= KEYC_MOUSEDOWN1_STATUS
;
1252 if (where
== STATUS_LEFT
)
1253 key
= KEYC_MOUSEDOWN1_STATUS_LEFT
;
1254 if (where
== STATUS_RIGHT
)
1255 key
= KEYC_MOUSEDOWN1_STATUS_RIGHT
;
1256 if (where
== STATUS_DEFAULT
)
1257 key
= KEYC_MOUSEDOWN1_STATUS_DEFAULT
;
1258 if (where
== BORDER
)
1259 key
= KEYC_MOUSEDOWN1_BORDER
;
1261 case MOUSE_BUTTON_2
:
1263 key
= KEYC_MOUSEDOWN2_PANE
;
1264 if (where
== STATUS
)
1265 key
= KEYC_MOUSEDOWN2_STATUS
;
1266 if (where
== STATUS_LEFT
)
1267 key
= KEYC_MOUSEDOWN2_STATUS_LEFT
;
1268 if (where
== STATUS_RIGHT
)
1269 key
= KEYC_MOUSEDOWN2_STATUS_RIGHT
;
1270 if (where
== STATUS_DEFAULT
)
1271 key
= KEYC_MOUSEDOWN2_STATUS_DEFAULT
;
1272 if (where
== BORDER
)
1273 key
= KEYC_MOUSEDOWN2_BORDER
;
1275 case MOUSE_BUTTON_3
:
1277 key
= KEYC_MOUSEDOWN3_PANE
;
1278 if (where
== STATUS
)
1279 key
= KEYC_MOUSEDOWN3_STATUS
;
1280 if (where
== STATUS_LEFT
)
1281 key
= KEYC_MOUSEDOWN3_STATUS_LEFT
;
1282 if (where
== STATUS_RIGHT
)
1283 key
= KEYC_MOUSEDOWN3_STATUS_RIGHT
;
1284 if (where
== STATUS_DEFAULT
)
1285 key
= KEYC_MOUSEDOWN3_STATUS_DEFAULT
;
1286 if (where
== BORDER
)
1287 key
= KEYC_MOUSEDOWN3_BORDER
;
1289 case MOUSE_BUTTON_6
:
1291 key
= KEYC_MOUSEDOWN6_PANE
;
1292 if (where
== STATUS
)
1293 key
= KEYC_MOUSEDOWN6_STATUS
;
1294 if (where
== STATUS_LEFT
)
1295 key
= KEYC_MOUSEDOWN6_STATUS_LEFT
;
1296 if (where
== STATUS_RIGHT
)
1297 key
= KEYC_MOUSEDOWN6_STATUS_RIGHT
;
1298 if (where
== STATUS_DEFAULT
)
1299 key
= KEYC_MOUSEDOWN6_STATUS_DEFAULT
;
1300 if (where
== BORDER
)
1301 key
= KEYC_MOUSEDOWN6_BORDER
;
1303 case MOUSE_BUTTON_7
:
1305 key
= KEYC_MOUSEDOWN7_PANE
;
1306 if (where
== STATUS
)
1307 key
= KEYC_MOUSEDOWN7_STATUS
;
1308 if (where
== STATUS_LEFT
)
1309 key
= KEYC_MOUSEDOWN7_STATUS_LEFT
;
1310 if (where
== STATUS_RIGHT
)
1311 key
= KEYC_MOUSEDOWN7_STATUS_RIGHT
;
1312 if (where
== STATUS_DEFAULT
)
1313 key
= KEYC_MOUSEDOWN7_STATUS_DEFAULT
;
1314 if (where
== BORDER
)
1315 key
= KEYC_MOUSEDOWN7_BORDER
;
1317 case MOUSE_BUTTON_8
:
1319 key
= KEYC_MOUSEDOWN8_PANE
;
1320 if (where
== STATUS
)
1321 key
= KEYC_MOUSEDOWN8_STATUS
;
1322 if (where
== STATUS_LEFT
)
1323 key
= KEYC_MOUSEDOWN8_STATUS_LEFT
;
1324 if (where
== STATUS_RIGHT
)
1325 key
= KEYC_MOUSEDOWN8_STATUS_RIGHT
;
1326 if (where
== STATUS_DEFAULT
)
1327 key
= KEYC_MOUSEDOWN8_STATUS_DEFAULT
;
1328 if (where
== BORDER
)
1329 key
= KEYC_MOUSEDOWN8_BORDER
;
1331 case MOUSE_BUTTON_9
:
1333 key
= KEYC_MOUSEDOWN9_PANE
;
1334 if (where
== STATUS
)
1335 key
= KEYC_MOUSEDOWN9_STATUS
;
1336 if (where
== STATUS_LEFT
)
1337 key
= KEYC_MOUSEDOWN9_STATUS_LEFT
;
1338 if (where
== STATUS_RIGHT
)
1339 key
= KEYC_MOUSEDOWN9_STATUS_RIGHT
;
1340 if (where
== STATUS_DEFAULT
)
1341 key
= KEYC_MOUSEDOWN9_STATUS_DEFAULT
;
1342 if (where
== BORDER
)
1343 key
= KEYC_MOUSEDOWN9_BORDER
;
1345 case MOUSE_BUTTON_10
:
1347 key
= KEYC_MOUSEDOWN10_PANE
;
1348 if (where
== STATUS
)
1349 key
= KEYC_MOUSEDOWN10_STATUS
;
1350 if (where
== STATUS_LEFT
)
1351 key
= KEYC_MOUSEDOWN10_STATUS_LEFT
;
1352 if (where
== STATUS_RIGHT
)
1353 key
= KEYC_MOUSEDOWN10_STATUS_RIGHT
;
1354 if (where
== STATUS_DEFAULT
)
1355 key
= KEYC_MOUSEDOWN10_STATUS_DEFAULT
;
1356 if (where
== BORDER
)
1357 key
= KEYC_MOUSEDOWN10_BORDER
;
1359 case MOUSE_BUTTON_11
:
1361 key
= KEYC_MOUSEDOWN11_PANE
;
1362 if (where
== STATUS
)
1363 key
= KEYC_MOUSEDOWN11_STATUS
;
1364 if (where
== STATUS_LEFT
)
1365 key
= KEYC_MOUSEDOWN11_STATUS_LEFT
;
1366 if (where
== STATUS_RIGHT
)
1367 key
= KEYC_MOUSEDOWN11_STATUS_RIGHT
;
1368 if (where
== STATUS_DEFAULT
)
1369 key
= KEYC_MOUSEDOWN11_STATUS_DEFAULT
;
1370 if (where
== BORDER
)
1371 key
= KEYC_MOUSEDOWN11_BORDER
;
1376 switch (MOUSE_BUTTONS(b
)) {
1377 case MOUSE_BUTTON_1
:
1379 key
= KEYC_SECONDCLICK1_PANE
;
1380 if (where
== STATUS
)
1381 key
= KEYC_SECONDCLICK1_STATUS
;
1382 if (where
== STATUS_LEFT
)
1383 key
= KEYC_SECONDCLICK1_STATUS_LEFT
;
1384 if (where
== STATUS_RIGHT
)
1385 key
= KEYC_SECONDCLICK1_STATUS_RIGHT
;
1386 if (where
== STATUS_DEFAULT
)
1387 key
= KEYC_SECONDCLICK1_STATUS_DEFAULT
;
1388 if (where
== BORDER
)
1389 key
= KEYC_SECONDCLICK1_BORDER
;
1391 case MOUSE_BUTTON_2
:
1393 key
= KEYC_SECONDCLICK2_PANE
;
1394 if (where
== STATUS
)
1395 key
= KEYC_SECONDCLICK2_STATUS
;
1396 if (where
== STATUS_LEFT
)
1397 key
= KEYC_SECONDCLICK2_STATUS_LEFT
;
1398 if (where
== STATUS_RIGHT
)
1399 key
= KEYC_SECONDCLICK2_STATUS_RIGHT
;
1400 if (where
== STATUS_DEFAULT
)
1401 key
= KEYC_SECONDCLICK2_STATUS_DEFAULT
;
1402 if (where
== BORDER
)
1403 key
= KEYC_SECONDCLICK2_BORDER
;
1405 case MOUSE_BUTTON_3
:
1407 key
= KEYC_SECONDCLICK3_PANE
;
1408 if (where
== STATUS
)
1409 key
= KEYC_SECONDCLICK3_STATUS
;
1410 if (where
== STATUS_LEFT
)
1411 key
= KEYC_SECONDCLICK3_STATUS_LEFT
;
1412 if (where
== STATUS_RIGHT
)
1413 key
= KEYC_SECONDCLICK3_STATUS_RIGHT
;
1414 if (where
== STATUS_DEFAULT
)
1415 key
= KEYC_SECONDCLICK3_STATUS_DEFAULT
;
1416 if (where
== BORDER
)
1417 key
= KEYC_SECONDCLICK3_BORDER
;
1419 case MOUSE_BUTTON_6
:
1421 key
= KEYC_SECONDCLICK6_PANE
;
1422 if (where
== STATUS
)
1423 key
= KEYC_SECONDCLICK6_STATUS
;
1424 if (where
== STATUS_LEFT
)
1425 key
= KEYC_SECONDCLICK6_STATUS_LEFT
;
1426 if (where
== STATUS_RIGHT
)
1427 key
= KEYC_SECONDCLICK6_STATUS_RIGHT
;
1428 if (where
== STATUS_DEFAULT
)
1429 key
= KEYC_SECONDCLICK6_STATUS_DEFAULT
;
1430 if (where
== BORDER
)
1431 key
= KEYC_SECONDCLICK6_BORDER
;
1433 case MOUSE_BUTTON_7
:
1435 key
= KEYC_SECONDCLICK7_PANE
;
1436 if (where
== STATUS
)
1437 key
= KEYC_SECONDCLICK7_STATUS
;
1438 if (where
== STATUS_LEFT
)
1439 key
= KEYC_SECONDCLICK7_STATUS_LEFT
;
1440 if (where
== STATUS_RIGHT
)
1441 key
= KEYC_SECONDCLICK7_STATUS_RIGHT
;
1442 if (where
== STATUS_DEFAULT
)
1443 key
= KEYC_SECONDCLICK7_STATUS_DEFAULT
;
1444 if (where
== BORDER
)
1445 key
= KEYC_SECONDCLICK7_BORDER
;
1447 case MOUSE_BUTTON_8
:
1449 key
= KEYC_SECONDCLICK8_PANE
;
1450 if (where
== STATUS
)
1451 key
= KEYC_SECONDCLICK8_STATUS
;
1452 if (where
== STATUS_LEFT
)
1453 key
= KEYC_SECONDCLICK8_STATUS_LEFT
;
1454 if (where
== STATUS_RIGHT
)
1455 key
= KEYC_SECONDCLICK8_STATUS_RIGHT
;
1456 if (where
== STATUS_DEFAULT
)
1457 key
= KEYC_SECONDCLICK8_STATUS_DEFAULT
;
1458 if (where
== BORDER
)
1459 key
= KEYC_SECONDCLICK8_BORDER
;
1461 case MOUSE_BUTTON_9
:
1463 key
= KEYC_SECONDCLICK9_PANE
;
1464 if (where
== STATUS
)
1465 key
= KEYC_SECONDCLICK9_STATUS
;
1466 if (where
== STATUS_LEFT
)
1467 key
= KEYC_SECONDCLICK9_STATUS_LEFT
;
1468 if (where
== STATUS_RIGHT
)
1469 key
= KEYC_SECONDCLICK9_STATUS_RIGHT
;
1470 if (where
== STATUS_DEFAULT
)
1471 key
= KEYC_SECONDCLICK9_STATUS_DEFAULT
;
1472 if (where
== BORDER
)
1473 key
= KEYC_SECONDCLICK9_BORDER
;
1475 case MOUSE_BUTTON_10
:
1477 key
= KEYC_SECONDCLICK10_PANE
;
1478 if (where
== STATUS
)
1479 key
= KEYC_SECONDCLICK10_STATUS
;
1480 if (where
== STATUS_LEFT
)
1481 key
= KEYC_SECONDCLICK10_STATUS_LEFT
;
1482 if (where
== STATUS_RIGHT
)
1483 key
= KEYC_SECONDCLICK10_STATUS_RIGHT
;
1484 if (where
== STATUS_DEFAULT
)
1485 key
= KEYC_SECONDCLICK10_STATUS_DEFAULT
;
1486 if (where
== BORDER
)
1487 key
= KEYC_SECONDCLICK10_BORDER
;
1489 case MOUSE_BUTTON_11
:
1491 key
= KEYC_SECONDCLICK11_PANE
;
1492 if (where
== STATUS
)
1493 key
= KEYC_SECONDCLICK11_STATUS
;
1494 if (where
== STATUS_LEFT
)
1495 key
= KEYC_SECONDCLICK11_STATUS_LEFT
;
1496 if (where
== STATUS_RIGHT
)
1497 key
= KEYC_SECONDCLICK11_STATUS_RIGHT
;
1498 if (where
== STATUS_DEFAULT
)
1499 key
= KEYC_SECONDCLICK11_STATUS_DEFAULT
;
1500 if (where
== BORDER
)
1501 key
= KEYC_SECONDCLICK11_BORDER
;
1506 switch (MOUSE_BUTTONS(b
)) {
1507 case MOUSE_BUTTON_1
:
1509 key
= KEYC_DOUBLECLICK1_PANE
;
1510 if (where
== STATUS
)
1511 key
= KEYC_DOUBLECLICK1_STATUS
;
1512 if (where
== STATUS_LEFT
)
1513 key
= KEYC_DOUBLECLICK1_STATUS_LEFT
;
1514 if (where
== STATUS_RIGHT
)
1515 key
= KEYC_DOUBLECLICK1_STATUS_RIGHT
;
1516 if (where
== STATUS_DEFAULT
)
1517 key
= KEYC_DOUBLECLICK1_STATUS_DEFAULT
;
1518 if (where
== BORDER
)
1519 key
= KEYC_DOUBLECLICK1_BORDER
;
1521 case MOUSE_BUTTON_2
:
1523 key
= KEYC_DOUBLECLICK2_PANE
;
1524 if (where
== STATUS
)
1525 key
= KEYC_DOUBLECLICK2_STATUS
;
1526 if (where
== STATUS_LEFT
)
1527 key
= KEYC_DOUBLECLICK2_STATUS_LEFT
;
1528 if (where
== STATUS_RIGHT
)
1529 key
= KEYC_DOUBLECLICK2_STATUS_RIGHT
;
1530 if (where
== STATUS_DEFAULT
)
1531 key
= KEYC_DOUBLECLICK2_STATUS_DEFAULT
;
1532 if (where
== BORDER
)
1533 key
= KEYC_DOUBLECLICK2_BORDER
;
1535 case MOUSE_BUTTON_3
:
1537 key
= KEYC_DOUBLECLICK3_PANE
;
1538 if (where
== STATUS
)
1539 key
= KEYC_DOUBLECLICK3_STATUS
;
1540 if (where
== STATUS_LEFT
)
1541 key
= KEYC_DOUBLECLICK3_STATUS_LEFT
;
1542 if (where
== STATUS_RIGHT
)
1543 key
= KEYC_DOUBLECLICK3_STATUS_RIGHT
;
1544 if (where
== STATUS_DEFAULT
)
1545 key
= KEYC_DOUBLECLICK3_STATUS_DEFAULT
;
1546 if (where
== BORDER
)
1547 key
= KEYC_DOUBLECLICK3_BORDER
;
1549 case MOUSE_BUTTON_6
:
1551 key
= KEYC_DOUBLECLICK6_PANE
;
1552 if (where
== STATUS
)
1553 key
= KEYC_DOUBLECLICK6_STATUS
;
1554 if (where
== STATUS_LEFT
)
1555 key
= KEYC_DOUBLECLICK6_STATUS_LEFT
;
1556 if (where
== STATUS_RIGHT
)
1557 key
= KEYC_DOUBLECLICK6_STATUS_RIGHT
;
1558 if (where
== STATUS_DEFAULT
)
1559 key
= KEYC_DOUBLECLICK6_STATUS_DEFAULT
;
1560 if (where
== BORDER
)
1561 key
= KEYC_DOUBLECLICK6_BORDER
;
1563 case MOUSE_BUTTON_7
:
1565 key
= KEYC_DOUBLECLICK7_PANE
;
1566 if (where
== STATUS
)
1567 key
= KEYC_DOUBLECLICK7_STATUS
;
1568 if (where
== STATUS_LEFT
)
1569 key
= KEYC_DOUBLECLICK7_STATUS_LEFT
;
1570 if (where
== STATUS_RIGHT
)
1571 key
= KEYC_DOUBLECLICK7_STATUS_RIGHT
;
1572 if (where
== STATUS_DEFAULT
)
1573 key
= KEYC_DOUBLECLICK7_STATUS_DEFAULT
;
1574 if (where
== BORDER
)
1575 key
= KEYC_DOUBLECLICK7_BORDER
;
1577 case MOUSE_BUTTON_8
:
1579 key
= KEYC_DOUBLECLICK8_PANE
;
1580 if (where
== STATUS
)
1581 key
= KEYC_DOUBLECLICK8_STATUS
;
1582 if (where
== STATUS_LEFT
)
1583 key
= KEYC_DOUBLECLICK8_STATUS_LEFT
;
1584 if (where
== STATUS_RIGHT
)
1585 key
= KEYC_DOUBLECLICK8_STATUS_RIGHT
;
1586 if (where
== STATUS_DEFAULT
)
1587 key
= KEYC_DOUBLECLICK8_STATUS_DEFAULT
;
1588 if (where
== BORDER
)
1589 key
= KEYC_DOUBLECLICK8_BORDER
;
1591 case MOUSE_BUTTON_9
:
1593 key
= KEYC_DOUBLECLICK9_PANE
;
1594 if (where
== STATUS
)
1595 key
= KEYC_DOUBLECLICK9_STATUS
;
1596 if (where
== STATUS_LEFT
)
1597 key
= KEYC_DOUBLECLICK9_STATUS_LEFT
;
1598 if (where
== STATUS_RIGHT
)
1599 key
= KEYC_DOUBLECLICK9_STATUS_RIGHT
;
1600 if (where
== STATUS_DEFAULT
)
1601 key
= KEYC_DOUBLECLICK9_STATUS_DEFAULT
;
1602 if (where
== BORDER
)
1603 key
= KEYC_DOUBLECLICK9_BORDER
;
1605 case MOUSE_BUTTON_10
:
1607 key
= KEYC_DOUBLECLICK10_PANE
;
1608 if (where
== STATUS
)
1609 key
= KEYC_DOUBLECLICK10_STATUS
;
1610 if (where
== STATUS_LEFT
)
1611 key
= KEYC_DOUBLECLICK10_STATUS_LEFT
;
1612 if (where
== STATUS_RIGHT
)
1613 key
= KEYC_DOUBLECLICK10_STATUS_RIGHT
;
1614 if (where
== STATUS_DEFAULT
)
1615 key
= KEYC_DOUBLECLICK10_STATUS_DEFAULT
;
1616 if (where
== BORDER
)
1617 key
= KEYC_DOUBLECLICK10_BORDER
;
1619 case MOUSE_BUTTON_11
:
1621 key
= KEYC_DOUBLECLICK11_PANE
;
1622 if (where
== STATUS
)
1623 key
= KEYC_DOUBLECLICK11_STATUS
;
1624 if (where
== STATUS_LEFT
)
1625 key
= KEYC_DOUBLECLICK11_STATUS_LEFT
;
1626 if (where
== STATUS_RIGHT
)
1627 key
= KEYC_DOUBLECLICK11_STATUS_RIGHT
;
1628 if (where
== STATUS_DEFAULT
)
1629 key
= KEYC_DOUBLECLICK11_STATUS_DEFAULT
;
1630 if (where
== BORDER
)
1631 key
= KEYC_DOUBLECLICK11_BORDER
;
1636 switch (MOUSE_BUTTONS(b
)) {
1637 case MOUSE_BUTTON_1
:
1639 key
= KEYC_TRIPLECLICK1_PANE
;
1640 if (where
== STATUS
)
1641 key
= KEYC_TRIPLECLICK1_STATUS
;
1642 if (where
== STATUS_LEFT
)
1643 key
= KEYC_TRIPLECLICK1_STATUS_LEFT
;
1644 if (where
== STATUS_RIGHT
)
1645 key
= KEYC_TRIPLECLICK1_STATUS_RIGHT
;
1646 if (where
== STATUS_DEFAULT
)
1647 key
= KEYC_TRIPLECLICK1_STATUS_DEFAULT
;
1648 if (where
== BORDER
)
1649 key
= KEYC_TRIPLECLICK1_BORDER
;
1651 case MOUSE_BUTTON_2
:
1653 key
= KEYC_TRIPLECLICK2_PANE
;
1654 if (where
== STATUS
)
1655 key
= KEYC_TRIPLECLICK2_STATUS
;
1656 if (where
== STATUS_LEFT
)
1657 key
= KEYC_TRIPLECLICK2_STATUS_LEFT
;
1658 if (where
== STATUS_RIGHT
)
1659 key
= KEYC_TRIPLECLICK2_STATUS_RIGHT
;
1660 if (where
== STATUS_DEFAULT
)
1661 key
= KEYC_TRIPLECLICK2_STATUS_DEFAULT
;
1662 if (where
== BORDER
)
1663 key
= KEYC_TRIPLECLICK2_BORDER
;
1665 case MOUSE_BUTTON_3
:
1667 key
= KEYC_TRIPLECLICK3_PANE
;
1668 if (where
== STATUS
)
1669 key
= KEYC_TRIPLECLICK3_STATUS
;
1670 if (where
== STATUS_LEFT
)
1671 key
= KEYC_TRIPLECLICK3_STATUS_LEFT
;
1672 if (where
== STATUS_RIGHT
)
1673 key
= KEYC_TRIPLECLICK3_STATUS_RIGHT
;
1674 if (where
== STATUS_DEFAULT
)
1675 key
= KEYC_TRIPLECLICK3_STATUS_DEFAULT
;
1676 if (where
== BORDER
)
1677 key
= KEYC_TRIPLECLICK3_BORDER
;
1679 case MOUSE_BUTTON_6
:
1681 key
= KEYC_TRIPLECLICK6_PANE
;
1682 if (where
== STATUS
)
1683 key
= KEYC_TRIPLECLICK6_STATUS
;
1684 if (where
== STATUS_LEFT
)
1685 key
= KEYC_TRIPLECLICK6_STATUS_LEFT
;
1686 if (where
== STATUS_RIGHT
)
1687 key
= KEYC_TRIPLECLICK6_STATUS_RIGHT
;
1688 if (where
== STATUS_DEFAULT
)
1689 key
= KEYC_TRIPLECLICK6_STATUS_DEFAULT
;
1690 if (where
== BORDER
)
1691 key
= KEYC_TRIPLECLICK6_BORDER
;
1693 case MOUSE_BUTTON_7
:
1695 key
= KEYC_TRIPLECLICK7_PANE
;
1696 if (where
== STATUS
)
1697 key
= KEYC_TRIPLECLICK7_STATUS
;
1698 if (where
== STATUS_LEFT
)
1699 key
= KEYC_TRIPLECLICK7_STATUS_LEFT
;
1700 if (where
== STATUS_RIGHT
)
1701 key
= KEYC_TRIPLECLICK7_STATUS_RIGHT
;
1702 if (where
== STATUS_DEFAULT
)
1703 key
= KEYC_TRIPLECLICK7_STATUS_DEFAULT
;
1704 if (where
== BORDER
)
1705 key
= KEYC_TRIPLECLICK7_BORDER
;
1707 case MOUSE_BUTTON_8
:
1709 key
= KEYC_TRIPLECLICK8_PANE
;
1710 if (where
== STATUS
)
1711 key
= KEYC_TRIPLECLICK8_STATUS
;
1712 if (where
== STATUS_LEFT
)
1713 key
= KEYC_TRIPLECLICK8_STATUS_LEFT
;
1714 if (where
== STATUS_RIGHT
)
1715 key
= KEYC_TRIPLECLICK8_STATUS_RIGHT
;
1716 if (where
== STATUS_DEFAULT
)
1717 key
= KEYC_TRIPLECLICK8_STATUS_DEFAULT
;
1718 if (where
== BORDER
)
1719 key
= KEYC_TRIPLECLICK8_BORDER
;
1721 case MOUSE_BUTTON_9
:
1723 key
= KEYC_TRIPLECLICK9_PANE
;
1724 if (where
== STATUS
)
1725 key
= KEYC_TRIPLECLICK9_STATUS
;
1726 if (where
== STATUS_LEFT
)
1727 key
= KEYC_TRIPLECLICK9_STATUS_LEFT
;
1728 if (where
== STATUS_RIGHT
)
1729 key
= KEYC_TRIPLECLICK9_STATUS_RIGHT
;
1730 if (where
== STATUS_DEFAULT
)
1731 key
= KEYC_TRIPLECLICK9_STATUS_DEFAULT
;
1732 if (where
== BORDER
)
1733 key
= KEYC_TRIPLECLICK9_BORDER
;
1735 case MOUSE_BUTTON_10
:
1737 key
= KEYC_TRIPLECLICK10_PANE
;
1738 if (where
== STATUS
)
1739 key
= KEYC_TRIPLECLICK10_STATUS
;
1740 if (where
== STATUS_LEFT
)
1741 key
= KEYC_TRIPLECLICK10_STATUS_LEFT
;
1742 if (where
== STATUS_RIGHT
)
1743 key
= KEYC_TRIPLECLICK10_STATUS_RIGHT
;
1744 if (where
== STATUS_DEFAULT
)
1745 key
= KEYC_TRIPLECLICK10_STATUS_DEFAULT
;
1746 if (where
== BORDER
)
1747 key
= KEYC_TRIPLECLICK10_BORDER
;
1749 case MOUSE_BUTTON_11
:
1751 key
= KEYC_TRIPLECLICK11_PANE
;
1752 if (where
== STATUS
)
1753 key
= KEYC_TRIPLECLICK11_STATUS
;
1754 if (where
== STATUS_LEFT
)
1755 key
= KEYC_TRIPLECLICK11_STATUS_LEFT
;
1756 if (where
== STATUS_RIGHT
)
1757 key
= KEYC_TRIPLECLICK11_STATUS_RIGHT
;
1758 if (where
== STATUS_DEFAULT
)
1759 key
= KEYC_TRIPLECLICK11_STATUS_DEFAULT
;
1760 if (where
== BORDER
)
1761 key
= KEYC_TRIPLECLICK11_BORDER
;
1766 if (key
== KEYC_UNKNOWN
)
1767 return (KEYC_UNKNOWN
);
1770 /* Apply modifiers if any. */
1771 if (b
& MOUSE_MASK_META
)
1773 if (b
& MOUSE_MASK_CTRL
)
1775 if (b
& MOUSE_MASK_SHIFT
)
1778 if (log_get_level() != 0)
1779 log_debug("mouse key is %s", key_string_lookup_key (key
, 1));
1783 /* Is this a bracket paste key? */
1785 server_client_is_bracket_pasting(struct client
*c
, key_code key
)
1787 if (key
== KEYC_PASTE_START
) {
1788 c
->flags
|= CLIENT_BRACKETPASTING
;
1789 log_debug("%s: bracket paste on", c
->name
);
1793 if (key
== KEYC_PASTE_END
) {
1794 c
->flags
&= ~CLIENT_BRACKETPASTING
;
1795 log_debug("%s: bracket paste off", c
->name
);
1799 return !!(c
->flags
& CLIENT_BRACKETPASTING
);
1802 /* Is this fast enough to probably be a paste? */
1804 server_client_assume_paste(struct session
*s
)
1809 if ((t
= options_get_number(s
->options
, "assume-paste-time")) == 0)
1812 timersub(&s
->activity_time
, &s
->last_activity_time
, &tv
);
1813 if (tv
.tv_sec
== 0 && tv
.tv_usec
< t
* 1000) {
1814 log_debug("session %s pasting (flag %d)", s
->name
,
1815 !!(s
->flags
& SESSION_PASTING
));
1816 if (s
->flags
& SESSION_PASTING
)
1818 s
->flags
|= SESSION_PASTING
;
1821 log_debug("session %s not pasting", s
->name
);
1822 s
->flags
&= ~SESSION_PASTING
;
1826 /* Has the latest client changed? */
1828 server_client_update_latest(struct client
*c
)
1832 if (c
->session
== NULL
)
1834 w
= c
->session
->curw
->window
;
1840 if (options_get_number(w
->options
, "window-size") == WINDOW_SIZE_LATEST
)
1841 recalculate_size(w
, 0);
1843 notify_client("client-active", c
);
1847 * Handle data key input from client. This owns and can modify the key event it
1848 * is given and is responsible for freeing it.
1850 static enum cmd_retval
1851 server_client_key_callback(struct cmdq_item
*item
, void *data
)
1853 struct client
*c
= cmdq_get_client(item
);
1854 struct key_event
*event
= data
;
1855 key_code key
= event
->key
;
1856 struct mouse_event
*m
= &event
->m
;
1857 struct session
*s
= c
->session
;
1859 struct window_pane
*wp
;
1860 struct window_mode_entry
*wme
;
1862 struct key_table
*table
, *first
;
1863 struct key_binding
*bd
;
1864 int xtimeout
, flags
;
1865 struct cmd_find_state fs
;
1866 key_code key0
, prefix
, prefix2
;
1868 /* Check the client is good to accept input. */
1869 if (s
== NULL
|| (c
->flags
& CLIENT_UNATTACHEDFLAGS
))
1873 /* Update the activity timer. */
1874 if (gettimeofday(&c
->activity_time
, NULL
) != 0)
1875 fatal("gettimeofday failed");
1876 session_update_activity(s
, &c
->activity_time
);
1878 /* Check for mouse keys. */
1880 if (key
== KEYC_MOUSE
|| key
== KEYC_DOUBLECLICK
) {
1881 if (c
->flags
& CLIENT_READONLY
)
1883 key
= server_client_check_mouse(c
, event
);
1884 if (key
== KEYC_UNKNOWN
)
1891 * Mouse drag is in progress, so fire the callback (now that
1892 * the mouse event is valid).
1894 if ((key
& KEYC_MASK_KEY
) == KEYC_DRAGGING
) {
1895 c
->tty
.mouse_drag_update(c
, m
);
1901 /* Find affected pane. */
1902 if (!KEYC_IS_MOUSE(key
) || cmd_find_from_mouse(&fs
, m
, 0) != 0)
1903 cmd_find_from_client(&fs
, c
, 0);
1906 /* Forward mouse keys if disabled. */
1907 if (KEYC_IS_MOUSE(key
) && !options_get_number(s
->options
, "mouse"))
1910 /* Forward if bracket pasting. */
1911 if (server_client_is_bracket_pasting(c
, key
))
1914 /* Treat everything as a regular key when pasting is detected. */
1915 if (!KEYC_IS_MOUSE(key
) &&
1916 (~key
& KEYC_SENT
) &&
1917 server_client_assume_paste(s
))
1921 * Work out the current key table. If the pane is in a mode, use
1922 * the mode table instead of the default key table.
1924 if (server_client_is_default_key_table(c
, c
->keytable
) &&
1926 (wme
= TAILQ_FIRST(&wp
->modes
)) != NULL
&&
1927 wme
->mode
->key_table
!= NULL
)
1928 table
= key_bindings_get_table(wme
->mode
->key_table(wme
), 1);
1930 table
= c
->keytable
;
1935 * The prefix always takes precedence and forces a switch to the prefix
1936 * table, unless we are already there.
1938 prefix
= (key_code
)options_get_number(s
->options
, "prefix");
1939 prefix2
= (key_code
)options_get_number(s
->options
, "prefix2");
1940 key0
= (key
& (KEYC_MASK_KEY
|KEYC_MASK_MODIFIERS
));
1941 if ((key0
== (prefix
& (KEYC_MASK_KEY
|KEYC_MASK_MODIFIERS
)) ||
1942 key0
== (prefix2
& (KEYC_MASK_KEY
|KEYC_MASK_MODIFIERS
))) &&
1943 strcmp(table
->name
, "prefix") != 0) {
1944 server_client_set_key_table(c
, "prefix");
1945 server_status_client(c
);
1951 /* Log key table. */
1953 log_debug("key table %s (no pane)", table
->name
);
1955 log_debug("key table %s (pane %%%u)", table
->name
, wp
->id
);
1956 if (c
->flags
& CLIENT_REPEAT
)
1957 log_debug("currently repeating");
1959 /* Try to see if there is a key binding in the current table. */
1960 bd
= key_bindings_get(table
, key0
);
1963 * Key was matched in this table. If currently repeating but a
1964 * non-repeating binding was found, stop repeating and try
1965 * again in the root table.
1967 if ((c
->flags
& CLIENT_REPEAT
) &&
1968 (~bd
->flags
& KEY_BINDING_REPEAT
)) {
1969 log_debug("found in key table %s (not repeating)",
1971 server_client_set_key_table(c
, NULL
);
1972 first
= table
= c
->keytable
;
1973 c
->flags
&= ~CLIENT_REPEAT
;
1974 server_status_client(c
);
1977 log_debug("found in key table %s", table
->name
);
1980 * Take a reference to this table to make sure the key binding
1981 * doesn't disappear.
1983 table
->references
++;
1986 * If this is a repeating key, start the timer. Otherwise reset
1987 * the client back to the root table.
1989 xtimeout
= options_get_number(s
->options
, "repeat-time");
1990 if (xtimeout
!= 0 && (bd
->flags
& KEY_BINDING_REPEAT
)) {
1991 c
->flags
|= CLIENT_REPEAT
;
1993 tv
.tv_sec
= xtimeout
/ 1000;
1994 tv
.tv_usec
= (xtimeout
% 1000) * 1000L;
1995 evtimer_del(&c
->repeat_timer
);
1996 evtimer_add(&c
->repeat_timer
, &tv
);
1998 c
->flags
&= ~CLIENT_REPEAT
;
1999 server_client_set_key_table(c
, NULL
);
2001 server_status_client(c
);
2003 /* Execute the key binding. */
2004 key_bindings_dispatch(bd
, item
, c
, event
, &fs
);
2005 key_bindings_unref_table(table
);
2010 * No match, try the ANY key.
2012 if (key0
!= KEYC_ANY
) {
2018 * No match in this table. If not in the root table or if repeating,
2019 * switch the client back to the root table and try again.
2021 log_debug("not found in key table %s", table
->name
);
2022 if (!server_client_is_default_key_table(c
, table
) ||
2023 (c
->flags
& CLIENT_REPEAT
)) {
2024 log_debug("trying in root table");
2025 server_client_set_key_table(c
, NULL
);
2026 table
= c
->keytable
;
2027 if (c
->flags
& CLIENT_REPEAT
)
2029 c
->flags
&= ~CLIENT_REPEAT
;
2030 server_status_client(c
);
2035 * No match in the root table either. If this wasn't the first table
2036 * tried, don't pass the key to the pane.
2038 if (first
!= table
&& (~flags
& CLIENT_REPEAT
)) {
2039 server_client_set_key_table(c
, NULL
);
2040 server_status_client(c
);
2045 if (c
->flags
& CLIENT_READONLY
)
2048 window_pane_key(wp
, c
, s
, wl
, key
, m
);
2051 if (s
!= NULL
&& key
!= KEYC_FOCUS_OUT
)
2052 server_client_update_latest(c
);
2054 return (CMD_RETURN_NORMAL
);
2057 /* Handle a key event. */
2059 server_client_handle_key(struct client
*c
, struct key_event
*event
)
2061 struct session
*s
= c
->session
;
2062 struct cmdq_item
*item
;
2064 /* Check the client is good to accept input. */
2065 if (s
== NULL
|| (c
->flags
& CLIENT_UNATTACHEDFLAGS
))
2069 * Key presses in overlay mode and the command prompt are a special
2070 * case. The queue might be blocked so they need to be processed
2071 * immediately rather than queued.
2073 if (~c
->flags
& CLIENT_READONLY
) {
2074 if (c
->message_string
!= NULL
) {
2075 if (c
->message_ignore_keys
)
2077 status_message_clear(c
);
2079 if (c
->overlay_key
!= NULL
) {
2080 switch (c
->overlay_key(c
, c
->overlay_data
, event
)) {
2084 server_client_clear_overlay(c
);
2088 server_client_clear_overlay(c
);
2089 if (c
->prompt_string
!= NULL
) {
2090 if (status_prompt_key(c
, event
->key
) == 0)
2096 * Add the key to the queue so it happens after any commands queued by
2099 item
= cmdq_get_callback(server_client_key_callback
, event
);
2100 cmdq_append(c
, item
);
2104 /* Client functions that need to happen every loop. */
2106 server_client_loop(void)
2110 struct window_pane
*wp
;
2112 /* Check for window resize. This is done before redrawing. */
2113 RB_FOREACH(w
, windows
, &windows
)
2114 server_client_check_window_resize(w
);
2116 /* Check clients. */
2117 TAILQ_FOREACH(c
, &clients
, entry
) {
2118 server_client_check_exit(c
);
2119 if (c
->session
!= NULL
) {
2120 server_client_check_modes(c
);
2121 server_client_check_redraw(c
);
2122 server_client_reset_state(c
);
2127 * Any windows will have been redrawn as part of clients, so clear
2130 RB_FOREACH(w
, windows
, &windows
) {
2131 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
2133 server_client_check_pane_resize(wp
);
2134 server_client_check_pane_buffer(wp
);
2136 wp
->flags
&= ~PANE_REDRAW
;
2138 check_window_name(w
);
2142 /* Check if window needs to be resized. */
2144 server_client_check_window_resize(struct window
*w
)
2148 if (~w
->flags
& WINDOW_RESIZE
)
2151 TAILQ_FOREACH(wl
, &w
->winlinks
, wentry
) {
2152 if (wl
->session
->attached
!= 0 && wl
->session
->curw
== wl
)
2158 log_debug("%s: resizing window @%u", __func__
, w
->id
);
2159 resize_window(w
, w
->new_sx
, w
->new_sy
, w
->new_xpixel
, w
->new_ypixel
);
2162 /* Resize timer event. */
2164 server_client_resize_timer(__unused
int fd
, __unused
short events
, void *data
)
2166 struct window_pane
*wp
= data
;
2168 log_debug("%s: %%%u resize timer expired", __func__
, wp
->id
);
2169 evtimer_del(&wp
->resize_timer
);
2172 /* Check if pane should be resized. */
2174 server_client_check_pane_resize(struct window_pane
*wp
)
2176 struct window_pane_resize
*r
;
2177 struct window_pane_resize
*r1
;
2178 struct window_pane_resize
*first
;
2179 struct window_pane_resize
*last
;
2180 struct timeval tv
= { .tv_usec
= 250000 };
2182 if (TAILQ_EMPTY(&wp
->resize_queue
))
2185 if (!event_initialized(&wp
->resize_timer
))
2186 evtimer_set(&wp
->resize_timer
, server_client_resize_timer
, wp
);
2187 if (evtimer_pending(&wp
->resize_timer
, NULL
))
2190 log_debug("%s: %%%u needs to be resized", __func__
, wp
->id
);
2191 TAILQ_FOREACH(r
, &wp
->resize_queue
, entry
) {
2192 log_debug("queued resize: %ux%u -> %ux%u", r
->osx
, r
->osy
,
2197 * There are three cases that matter:
2199 * - Only one resize. It can just be applied.
2201 * - Multiple resizes and the ending size is different from the
2202 * starting size. We can discard all resizes except the most recent.
2204 * - Multiple resizes and the ending size is the same as the starting
2205 * size. We must resize at least twice to force the application to
2206 * redraw. So apply the first and leave the last on the queue for
2209 first
= TAILQ_FIRST(&wp
->resize_queue
);
2210 last
= TAILQ_LAST(&wp
->resize_queue
, window_pane_resizes
);
2211 if (first
== last
) {
2212 /* Only one resize. */
2213 window_pane_send_resize(wp
, first
->sx
, first
->sy
);
2214 TAILQ_REMOVE(&wp
->resize_queue
, first
, entry
);
2216 } else if (last
->sx
!= first
->osx
|| last
->sy
!= first
->osy
) {
2217 /* Multiple resizes ending up with a different size. */
2218 window_pane_send_resize(wp
, last
->sx
, last
->sy
);
2219 TAILQ_FOREACH_SAFE(r
, &wp
->resize_queue
, entry
, r1
) {
2220 TAILQ_REMOVE(&wp
->resize_queue
, r
, entry
);
2225 * Multiple resizes ending up with the same size. There will
2226 * not be more than one to the same size in succession so we
2227 * can just use the last-but-one on the list and leave the last
2228 * for later. We reduce the time until the next check to avoid
2229 * a long delay between the resizes.
2231 r
= TAILQ_PREV(last
, window_pane_resizes
, entry
);
2232 window_pane_send_resize(wp
, r
->sx
, r
->sy
);
2233 TAILQ_FOREACH_SAFE(r
, &wp
->resize_queue
, entry
, r1
) {
2236 TAILQ_REMOVE(&wp
->resize_queue
, r
, entry
);
2241 evtimer_add(&wp
->resize_timer
, &tv
);
2244 /* Check pane buffer size. */
2246 server_client_check_pane_buffer(struct window_pane
*wp
)
2248 struct evbuffer
*evb
= wp
->event
->input
;
2251 struct window_pane_offset
*wpo
;
2253 u_int attached_clients
= 0;
2257 * Work out the minimum used size. This is the most that can be removed
2260 minimum
= wp
->offset
.used
;
2261 if (wp
->pipe_fd
!= -1 && wp
->pipe_offset
.used
< minimum
)
2262 minimum
= wp
->pipe_offset
.used
;
2263 TAILQ_FOREACH(c
, &clients
, entry
) {
2264 if (c
->session
== NULL
)
2268 if (~c
->flags
& CLIENT_CONTROL
) {
2272 wpo
= control_pane_offset(c
, wp
, &flag
);
2281 window_pane_get_new_data(wp
, wpo
, &new_size
);
2282 log_debug("%s: %s has %zu bytes used and %zu left for %%%u",
2283 __func__
, c
->name
, wpo
->used
- wp
->base_offset
, new_size
,
2285 if (wpo
->used
< minimum
)
2286 minimum
= wpo
->used
;
2288 if (attached_clients
== 0)
2290 minimum
-= wp
->base_offset
;
2294 /* Drain the buffer. */
2295 log_debug("%s: %%%u has %zu minimum (of %zu) bytes used", __func__
,
2296 wp
->id
, minimum
, EVBUFFER_LENGTH(evb
));
2297 evbuffer_drain(evb
, minimum
);
2300 * Adjust the base offset. If it would roll over, all the offsets into
2301 * the buffer need to be adjusted.
2303 if (wp
->base_offset
> SIZE_MAX
- minimum
) {
2304 log_debug("%s: %%%u base offset has wrapped", __func__
, wp
->id
);
2305 wp
->offset
.used
-= wp
->base_offset
;
2306 if (wp
->pipe_fd
!= -1)
2307 wp
->pipe_offset
.used
-= wp
->base_offset
;
2308 TAILQ_FOREACH(c
, &clients
, entry
) {
2309 if (c
->session
== NULL
|| (~c
->flags
& CLIENT_CONTROL
))
2311 wpo
= control_pane_offset(c
, wp
, &flag
);
2312 if (wpo
!= NULL
&& !flag
)
2313 wpo
->used
-= wp
->base_offset
;
2315 wp
->base_offset
= minimum
;
2317 wp
->base_offset
+= minimum
;
2321 * If there is data remaining, and there are no clients able to consume
2322 * it, do not read any more. This is true when there are attached
2323 * clients, all of which are control clients which are not able to
2324 * accept any more data.
2326 log_debug("%s: pane %%%u is %s", __func__
, wp
->id
, off
? "off" : "on");
2328 bufferevent_disable(wp
->event
, EV_READ
);
2330 bufferevent_enable(wp
->event
, EV_READ
);
2334 * Update cursor position and mode settings. The scroll region and attributes
2335 * are cleared when idle (waiting for an event) as this is the most likely time
2336 * a user may interrupt tmux, for example with ~^Z in ssh(1). This is a
2337 * compromise between excessive resets and likelihood of an interrupt.
2339 * tty_region/tty_reset/tty_update_mode already take care of not resetting
2340 * things that are already in their default state.
2343 server_client_reset_state(struct client
*c
)
2345 struct tty
*tty
= &c
->tty
;
2346 struct window
*w
= c
->session
->curw
->window
;
2347 struct window_pane
*wp
= server_client_get_pane(c
), *loop
;
2348 struct screen
*s
= NULL
;
2349 struct options
*oo
= c
->session
->options
;
2350 int mode
= 0, cursor
, flags
, n
;
2351 u_int cx
= 0, cy
= 0, ox
, oy
, sx
, sy
;
2353 if (c
->flags
& (CLIENT_CONTROL
|CLIENT_SUSPENDED
))
2356 /* Disable the block flag. */
2357 flags
= (tty
->flags
& TTY_BLOCK
);
2358 tty
->flags
&= ~TTY_BLOCK
;
2360 /* Get mode from overlay if any, else from screen. */
2361 if (c
->overlay_draw
!= NULL
) {
2362 if (c
->overlay_mode
!= NULL
)
2363 s
= c
->overlay_mode(c
, c
->overlay_data
, &cx
, &cy
);
2368 if (log_get_level() != 0) {
2369 log_debug("%s: client %s mode %s", __func__
, c
->name
,
2370 screen_mode_to_string(mode
));
2373 /* Reset region and margin. */
2374 tty_region_off(tty
);
2375 tty_margin_off(tty
);
2377 /* Move cursor to pane cursor and offset. */
2378 if (c
->prompt_string
!= NULL
) {
2379 n
= options_get_number(c
->session
->options
, "status-position");
2383 n
= status_line_size(c
);
2389 cx
= c
->prompt_cursor
;
2390 mode
&= ~MODE_CURSOR
;
2391 } else if (c
->overlay_draw
== NULL
) {
2393 tty_window_offset(tty
, &ox
, &oy
, &sx
, &sy
);
2394 if (wp
->xoff
+ s
->cx
>= ox
&& wp
->xoff
+ s
->cx
<= ox
+ sx
&&
2395 wp
->yoff
+ s
->cy
>= oy
&& wp
->yoff
+ s
->cy
<= oy
+ sy
) {
2398 cx
= wp
->xoff
+ s
->cx
- ox
;
2399 cy
= wp
->yoff
+ s
->cy
- oy
;
2401 if (status_at_line(c
) == 0)
2402 cy
+= status_line_size(c
);
2405 mode
&= ~MODE_CURSOR
;
2407 log_debug("%s: cursor to %u,%u", __func__
, cx
, cy
);
2408 tty_cursor(tty
, cx
, cy
);
2411 * Set mouse mode if requested. To support dragging, always use button
2414 if (options_get_number(oo
, "mouse")) {
2415 if (c
->overlay_draw
== NULL
) {
2416 mode
&= ~ALL_MOUSE_MODES
;
2417 TAILQ_FOREACH(loop
, &w
->panes
, entry
) {
2418 if (loop
->screen
->mode
& MODE_MOUSE_ALL
)
2419 mode
|= MODE_MOUSE_ALL
;
2422 if (~mode
& MODE_MOUSE_ALL
)
2423 mode
|= MODE_MOUSE_BUTTON
;
2426 /* Clear bracketed paste mode if at the prompt. */
2427 if (c
->overlay_draw
== NULL
&& c
->prompt_string
!= NULL
)
2428 mode
&= ~MODE_BRACKETPASTE
;
2430 /* Set the terminal mode and reset attributes. */
2431 tty_update_mode(tty
, mode
, s
);
2434 /* All writing must be done, send a sync end (if it was started). */
2436 tty
->flags
|= flags
;
2439 /* Repeat time callback. */
2441 server_client_repeat_timer(__unused
int fd
, __unused
short events
, void *data
)
2443 struct client
*c
= data
;
2445 if (c
->flags
& CLIENT_REPEAT
) {
2446 server_client_set_key_table(c
, NULL
);
2447 c
->flags
&= ~CLIENT_REPEAT
;
2448 server_status_client(c
);
2452 /* Double-click callback. */
2454 server_client_click_timer(__unused
int fd
, __unused
short events
, void *data
)
2456 struct client
*c
= data
;
2457 struct key_event
*event
;
2459 log_debug("click timer expired");
2461 if (c
->flags
& CLIENT_TRIPLECLICK
) {
2463 * Waiting for a third click that hasn't happened, so this must
2464 * have been a double click.
2466 event
= xmalloc(sizeof *event
);
2467 event
->key
= KEYC_DOUBLECLICK
;
2468 memcpy(&event
->m
, &c
->click_event
, sizeof event
->m
);
2469 if (!server_client_handle_key(c
, event
))
2472 c
->flags
&= ~(CLIENT_DOUBLECLICK
|CLIENT_TRIPLECLICK
);
2475 /* Check if client should be exited. */
2477 server_client_check_exit(struct client
*c
)
2479 struct client_file
*cf
;
2480 const char *name
= c
->exit_session
;
2484 if (c
->flags
& (CLIENT_DEAD
|CLIENT_EXITED
))
2486 if (~c
->flags
& CLIENT_EXIT
)
2489 if (c
->flags
& CLIENT_CONTROL
) {
2491 if (!control_all_done(c
))
2494 RB_FOREACH(cf
, client_files
, &c
->files
) {
2495 if (EVBUFFER_LENGTH(cf
->buffer
) != 0)
2498 c
->flags
|= CLIENT_EXITED
;
2500 switch (c
->exit_type
) {
2501 case CLIENT_EXIT_RETURN
:
2502 if (c
->exit_message
!= NULL
)
2503 msize
= strlen(c
->exit_message
) + 1;
2506 size
= (sizeof c
->retval
) + msize
;
2507 data
= xmalloc(size
);
2508 memcpy(data
, &c
->retval
, sizeof c
->retval
);
2509 if (c
->exit_message
!= NULL
)
2510 memcpy(data
+ sizeof c
->retval
, c
->exit_message
, msize
);
2511 proc_send(c
->peer
, MSG_EXIT
, -1, data
, size
);
2514 case CLIENT_EXIT_SHUTDOWN
:
2515 proc_send(c
->peer
, MSG_SHUTDOWN
, -1, NULL
, 0);
2517 case CLIENT_EXIT_DETACH
:
2518 proc_send(c
->peer
, c
->exit_msgtype
, -1, name
, strlen(name
) + 1);
2521 free(c
->exit_session
);
2522 free(c
->exit_message
);
2525 /* Redraw timer callback. */
2527 server_client_redraw_timer(__unused
int fd
, __unused
short events
,
2528 __unused
void *data
)
2530 log_debug("redraw timer fired");
2534 * Check if modes need to be updated. Only modes in the current window are
2535 * updated and it is done when the status line is redrawn.
2538 server_client_check_modes(struct client
*c
)
2540 struct window
*w
= c
->session
->curw
->window
;
2541 struct window_pane
*wp
;
2542 struct window_mode_entry
*wme
;
2544 if (c
->flags
& (CLIENT_CONTROL
|CLIENT_SUSPENDED
))
2546 if (~c
->flags
& CLIENT_REDRAWSTATUS
)
2548 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
2549 wme
= TAILQ_FIRST(&wp
->modes
);
2550 if (wme
!= NULL
&& wme
->mode
->update
!= NULL
)
2551 wme
->mode
->update(wme
);
2555 /* Check for client redraws. */
2557 server_client_check_redraw(struct client
*c
)
2559 struct session
*s
= c
->session
;
2560 struct tty
*tty
= &c
->tty
;
2561 struct window
*w
= c
->session
->curw
->window
;
2562 struct window_pane
*wp
;
2563 int needed
, flags
, mode
= tty
->mode
, new_flags
= 0;
2566 struct timeval tv
= { .tv_usec
= 1000 };
2567 static struct event ev
;
2570 if (c
->flags
& (CLIENT_CONTROL
|CLIENT_SUSPENDED
))
2572 if (c
->flags
& CLIENT_ALLREDRAWFLAGS
) {
2573 log_debug("%s: redraw%s%s%s%s%s", c
->name
,
2574 (c
->flags
& CLIENT_REDRAWWINDOW
) ? " window" : "",
2575 (c
->flags
& CLIENT_REDRAWSTATUS
) ? " status" : "",
2576 (c
->flags
& CLIENT_REDRAWBORDERS
) ? " borders" : "",
2577 (c
->flags
& CLIENT_REDRAWOVERLAY
) ? " overlay" : "",
2578 (c
->flags
& CLIENT_REDRAWPANES
) ? " panes" : "");
2582 * If there is outstanding data, defer the redraw until it has been
2583 * consumed. We can just add a timer to get out of the event loop and
2587 if (c
->flags
& CLIENT_ALLREDRAWFLAGS
)
2590 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
2591 if (wp
->flags
& PANE_REDRAW
) {
2597 new_flags
|= CLIENT_REDRAWPANES
;
2599 if (needed
&& (left
= EVBUFFER_LENGTH(tty
->out
)) != 0) {
2600 log_debug("%s: redraw deferred (%zu left)", c
->name
, left
);
2601 if (!evtimer_initialized(&ev
))
2602 evtimer_set(&ev
, server_client_redraw_timer
, NULL
);
2603 if (!evtimer_pending(&ev
, NULL
)) {
2604 log_debug("redraw timer started");
2605 evtimer_add(&ev
, &tv
);
2608 if (~c
->flags
& CLIENT_REDRAWWINDOW
) {
2609 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
2610 if (wp
->flags
& PANE_REDRAW
) {
2611 log_debug("%s: pane %%%u needs redraw",
2613 c
->redraw_panes
|= (1 << bit
);
2617 * If more that 64 panes, give up and
2618 * just redraw the window.
2620 new_flags
&= CLIENT_REDRAWPANES
;
2621 new_flags
|= CLIENT_REDRAWWINDOW
;
2625 if (c
->redraw_panes
!= 0)
2626 c
->flags
|= CLIENT_REDRAWPANES
;
2628 c
->flags
|= new_flags
;
2631 log_debug("%s: redraw needed", c
->name
);
2633 flags
= tty
->flags
& (TTY_BLOCK
|TTY_FREEZE
|TTY_NOCURSOR
);
2634 tty
->flags
= (tty
->flags
& ~(TTY_BLOCK
|TTY_FREEZE
))|TTY_NOCURSOR
;
2636 if (~c
->flags
& CLIENT_REDRAWWINDOW
) {
2638 * If not redrawing the entire window, check whether each pane
2639 * needs to be redrawn.
2641 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
2643 if (wp
->flags
& PANE_REDRAW
)
2645 else if (c
->flags
& CLIENT_REDRAWPANES
)
2646 redraw
= !!(c
->redraw_panes
& (1 << bit
));
2650 log_debug("%s: redrawing pane %%%u", __func__
, wp
->id
);
2651 screen_redraw_pane(c
, wp
);
2653 c
->redraw_panes
= 0;
2654 c
->flags
&= ~CLIENT_REDRAWPANES
;
2657 if (c
->flags
& CLIENT_ALLREDRAWFLAGS
) {
2658 if (options_get_number(s
->options
, "set-titles")) {
2659 server_client_set_title(c
);
2660 server_client_set_path(c
);
2662 screen_redraw_screen(c
);
2665 tty
->flags
= (tty
->flags
& ~TTY_NOCURSOR
)|(flags
& TTY_NOCURSOR
);
2666 tty_update_mode(tty
, mode
, NULL
);
2667 tty
->flags
= (tty
->flags
& ~(TTY_BLOCK
|TTY_FREEZE
|TTY_NOCURSOR
))|flags
;
2669 c
->flags
&= ~(CLIENT_ALLREDRAWFLAGS
|CLIENT_STATUSFORCE
);
2673 * We would have deferred the redraw unless the output buffer
2674 * was empty, so we can record how many bytes the redraw
2677 c
->redraw
= EVBUFFER_LENGTH(tty
->out
);
2678 log_debug("%s: redraw added %zu bytes", c
->name
, c
->redraw
);
2682 /* Set client title. */
2684 server_client_set_title(struct client
*c
)
2686 struct session
*s
= c
->session
;
2687 const char *template;
2689 struct format_tree
*ft
;
2691 template = options_get_string(s
->options
, "set-titles-string");
2693 ft
= format_create(c
, NULL
, FORMAT_NONE
, 0);
2694 format_defaults(ft
, c
, NULL
, NULL
, NULL
);
2696 title
= format_expand_time(ft
, template);
2697 if (c
->title
== NULL
|| strcmp(title
, c
->title
) != 0) {
2699 c
->title
= xstrdup(title
);
2700 tty_set_title(&c
->tty
, c
->title
);
2707 /* Set client path. */
2709 server_client_set_path(struct client
*c
)
2711 struct session
*s
= c
->session
;
2714 if (s
->curw
== NULL
)
2716 if (s
->curw
->window
->active
->base
.path
== NULL
)
2719 path
= s
->curw
->window
->active
->base
.path
;
2720 if (c
->path
== NULL
|| strcmp(path
, c
->path
) != 0) {
2722 c
->path
= xstrdup(path
);
2723 tty_set_path(&c
->tty
, c
->path
);
2727 /* Dispatch message from client. */
2729 server_client_dispatch(struct imsg
*imsg
, void *arg
)
2731 struct client
*c
= arg
;
2735 if (c
->flags
& CLIENT_DEAD
)
2739 server_client_lost(c
);
2743 datalen
= imsg
->hdr
.len
- IMSG_HEADER_SIZE
;
2745 switch (imsg
->hdr
.type
) {
2746 case MSG_IDENTIFY_CLIENTPID
:
2747 case MSG_IDENTIFY_CWD
:
2748 case MSG_IDENTIFY_ENVIRON
:
2749 case MSG_IDENTIFY_FEATURES
:
2750 case MSG_IDENTIFY_FLAGS
:
2751 case MSG_IDENTIFY_LONGFLAGS
:
2752 case MSG_IDENTIFY_STDIN
:
2753 case MSG_IDENTIFY_STDOUT
:
2754 case MSG_IDENTIFY_TERM
:
2755 case MSG_IDENTIFY_TERMINFO
:
2756 case MSG_IDENTIFY_TTYNAME
:
2757 case MSG_IDENTIFY_DONE
:
2758 server_client_dispatch_identify(c
, imsg
);
2761 server_client_dispatch_command(c
, imsg
);
2765 fatalx("bad MSG_RESIZE size");
2767 if (c
->flags
& CLIENT_CONTROL
)
2769 server_client_update_latest(c
);
2770 tty_resize(&c
->tty
);
2771 tty_repeat_requests(&c
->tty
);
2772 recalculate_sizes();
2773 if (c
->overlay_resize
== NULL
)
2774 server_client_clear_overlay(c
);
2776 c
->overlay_resize(c
, c
->overlay_data
);
2777 server_redraw_client(c
);
2778 if (c
->session
!= NULL
)
2779 notify_client("client-resized", c
);
2783 fatalx("bad MSG_EXITING size");
2784 server_client_set_session(c
, NULL
);
2785 recalculate_sizes();
2787 proc_send(c
->peer
, MSG_EXITED
, -1, NULL
, 0);
2792 fatalx("bad MSG_WAKEUP size");
2794 if (!(c
->flags
& CLIENT_SUSPENDED
))
2796 c
->flags
&= ~CLIENT_SUSPENDED
;
2798 if (c
->fd
== -1 || c
->session
== NULL
) /* exited already */
2802 if (gettimeofday(&c
->activity_time
, NULL
) != 0)
2803 fatal("gettimeofday failed");
2805 tty_start_tty(&c
->tty
);
2806 server_redraw_client(c
);
2807 recalculate_sizes();
2810 session_update_activity(s
, &c
->activity_time
);
2814 fatalx("bad MSG_SHELL size");
2816 server_client_dispatch_shell(c
);
2818 case MSG_WRITE_READY
:
2819 file_write_ready(&c
->files
, imsg
);
2822 file_read_data(&c
->files
, imsg
);
2825 file_read_done(&c
->files
, imsg
);
2830 /* Callback when command is not allowed. */
2831 static enum cmd_retval
2832 server_client_read_only(struct cmdq_item
*item
, __unused
void *data
)
2834 cmdq_error(item
, "client is read-only");
2835 return (CMD_RETURN_ERROR
);
2838 /* Callback when command is done. */
2839 static enum cmd_retval
2840 server_client_command_done(struct cmdq_item
*item
, __unused
void *data
)
2842 struct client
*c
= cmdq_get_client(item
);
2844 if (~c
->flags
& CLIENT_ATTACHED
)
2845 c
->flags
|= CLIENT_EXIT
;
2846 else if (~c
->flags
& CLIENT_EXIT
) {
2847 if (c
->flags
& CLIENT_CONTROL
)
2849 tty_send_requests(&c
->tty
);
2851 return (CMD_RETURN_NORMAL
);
2854 /* Handle command message. */
2856 server_client_dispatch_command(struct client
*c
, struct imsg
*imsg
)
2858 struct msg_command data
;
2862 char **argv
, *cause
;
2863 struct cmd_parse_result
*pr
;
2864 struct args_value
*values
;
2865 struct cmdq_item
*new_item
;
2867 if (c
->flags
& CLIENT_EXIT
)
2870 if (imsg
->hdr
.len
- IMSG_HEADER_SIZE
< sizeof data
)
2871 fatalx("bad MSG_COMMAND size");
2872 memcpy(&data
, imsg
->data
, sizeof data
);
2874 buf
= (char *)imsg
->data
+ sizeof data
;
2875 len
= imsg
->hdr
.len
- IMSG_HEADER_SIZE
- sizeof data
;
2876 if (len
> 0 && buf
[len
- 1] != '\0')
2877 fatalx("bad MSG_COMMAND string");
2880 if (cmd_unpack_argv(buf
, len
, argc
, &argv
) != 0) {
2881 cause
= xstrdup("command too long");
2887 argv
= xcalloc(1, sizeof *argv
);
2888 *argv
= xstrdup("new-session");
2891 values
= args_from_vector(argc
, argv
);
2892 pr
= cmd_parse_from_arguments(values
, argc
, NULL
);
2893 switch (pr
->status
) {
2894 case CMD_PARSE_ERROR
:
2897 case CMD_PARSE_SUCCESS
:
2900 args_free_values(values
, argc
);
2902 cmd_free_argv(argc
, argv
);
2904 if ((c
->flags
& CLIENT_READONLY
) &&
2905 !cmd_list_all_have(pr
->cmdlist
, CMD_READONLY
))
2906 new_item
= cmdq_get_callback(server_client_read_only
, NULL
);
2908 new_item
= cmdq_get_command(pr
->cmdlist
, NULL
);
2909 cmdq_append(c
, new_item
);
2910 cmdq_append(c
, cmdq_get_callback(server_client_command_done
, NULL
));
2912 cmd_list_free(pr
->cmdlist
);
2916 cmd_free_argv(argc
, argv
);
2918 cmdq_append(c
, cmdq_get_error(cause
));
2921 c
->flags
|= CLIENT_EXIT
;
2924 /* Handle identify message. */
2926 server_client_dispatch_identify(struct client
*c
, struct imsg
*imsg
)
2928 const char *data
, *home
;
2934 if (c
->flags
& CLIENT_IDENTIFIED
)
2935 fatalx("out-of-order identify message");
2938 datalen
= imsg
->hdr
.len
- IMSG_HEADER_SIZE
;
2940 switch (imsg
->hdr
.type
) {
2941 case MSG_IDENTIFY_FEATURES
:
2942 if (datalen
!= sizeof feat
)
2943 fatalx("bad MSG_IDENTIFY_FEATURES size");
2944 memcpy(&feat
, data
, sizeof feat
);
2945 c
->term_features
|= feat
;
2946 log_debug("client %p IDENTIFY_FEATURES %s", c
,
2947 tty_get_features(feat
));
2949 case MSG_IDENTIFY_FLAGS
:
2950 if (datalen
!= sizeof flags
)
2951 fatalx("bad MSG_IDENTIFY_FLAGS size");
2952 memcpy(&flags
, data
, sizeof flags
);
2954 log_debug("client %p IDENTIFY_FLAGS %#x", c
, flags
);
2956 case MSG_IDENTIFY_LONGFLAGS
:
2957 if (datalen
!= sizeof longflags
)
2958 fatalx("bad MSG_IDENTIFY_LONGFLAGS size");
2959 memcpy(&longflags
, data
, sizeof longflags
);
2960 c
->flags
|= longflags
;
2961 log_debug("client %p IDENTIFY_LONGFLAGS %#llx", c
,
2962 (unsigned long long)longflags
);
2964 case MSG_IDENTIFY_TERM
:
2965 if (datalen
== 0 || data
[datalen
- 1] != '\0')
2966 fatalx("bad MSG_IDENTIFY_TERM string");
2968 c
->term_name
= xstrdup("unknown");
2970 c
->term_name
= xstrdup(data
);
2971 log_debug("client %p IDENTIFY_TERM %s", c
, data
);
2973 case MSG_IDENTIFY_TERMINFO
:
2974 if (datalen
== 0 || data
[datalen
- 1] != '\0')
2975 fatalx("bad MSG_IDENTIFY_TERMINFO string");
2976 c
->term_caps
= xreallocarray(c
->term_caps
, c
->term_ncaps
+ 1,
2977 sizeof *c
->term_caps
);
2978 c
->term_caps
[c
->term_ncaps
++] = xstrdup(data
);
2979 log_debug("client %p IDENTIFY_TERMINFO %s", c
, data
);
2981 case MSG_IDENTIFY_TTYNAME
:
2982 if (datalen
== 0 || data
[datalen
- 1] != '\0')
2983 fatalx("bad MSG_IDENTIFY_TTYNAME string");
2984 c
->ttyname
= xstrdup(data
);
2985 log_debug("client %p IDENTIFY_TTYNAME %s", c
, data
);
2987 case MSG_IDENTIFY_CWD
:
2988 if (datalen
== 0 || data
[datalen
- 1] != '\0')
2989 fatalx("bad MSG_IDENTIFY_CWD string");
2990 if (access(data
, X_OK
) == 0)
2991 c
->cwd
= xstrdup(data
);
2992 else if ((home
= find_home()) != NULL
)
2993 c
->cwd
= xstrdup(home
);
2995 c
->cwd
= xstrdup("/");
2996 log_debug("client %p IDENTIFY_CWD %s", c
, data
);
2998 case MSG_IDENTIFY_STDIN
:
3000 fatalx("bad MSG_IDENTIFY_STDIN size");
3001 c
->fd
= imsg_get_fd(imsg
);
3002 log_debug("client %p IDENTIFY_STDIN %d", c
, c
->fd
);
3004 case MSG_IDENTIFY_STDOUT
:
3006 fatalx("bad MSG_IDENTIFY_STDOUT size");
3007 c
->out_fd
= imsg_get_fd(imsg
);
3008 log_debug("client %p IDENTIFY_STDOUT %d", c
, c
->out_fd
);
3010 case MSG_IDENTIFY_ENVIRON
:
3011 if (datalen
== 0 || data
[datalen
- 1] != '\0')
3012 fatalx("bad MSG_IDENTIFY_ENVIRON string");
3013 if (strchr(data
, '=') != NULL
)
3014 environ_put(c
->environ
, data
, 0);
3015 log_debug("client %p IDENTIFY_ENVIRON %s", c
, data
);
3017 case MSG_IDENTIFY_CLIENTPID
:
3018 if (datalen
!= sizeof c
->pid
)
3019 fatalx("bad MSG_IDENTIFY_CLIENTPID size");
3020 memcpy(&c
->pid
, data
, sizeof c
->pid
);
3021 log_debug("client %p IDENTIFY_CLIENTPID %ld", c
, (long)c
->pid
);
3027 if (imsg
->hdr
.type
!= MSG_IDENTIFY_DONE
)
3029 c
->flags
|= CLIENT_IDENTIFIED
;
3031 if (*c
->ttyname
!= '\0')
3032 name
= xstrdup(c
->ttyname
);
3034 xasprintf(&name
, "client-%ld", (long)c
->pid
);
3036 log_debug("client %p name is %s", c
, c
->name
);
3039 c
->fd
= open(c
->ttyname
, O_RDWR
|O_NOCTTY
);
3042 if (c
->flags
& CLIENT_CONTROL
)
3044 else if (c
->fd
!= -1) {
3045 if (tty_init(&c
->tty
, c
) != 0) {
3049 tty_resize(&c
->tty
);
3050 c
->flags
|= CLIENT_TERMINAL
;
3057 * If this is the first client, load configuration files. Any later
3058 * clients are allowed to continue with their command even if the
3059 * config has not been loaded - they might have been run from inside it
3061 if ((~c
->flags
& CLIENT_EXIT
) &&
3063 c
== TAILQ_FIRST(&clients
))
3067 /* Handle shell message. */
3069 server_client_dispatch_shell(struct client
*c
)
3073 shell
= options_get_string(global_s_options
, "default-shell");
3074 if (!checkshell(shell
))
3075 shell
= _PATH_BSHELL
;
3076 proc_send(c
->peer
, MSG_SHELL
, -1, shell
, strlen(shell
) + 1);
3078 proc_kill_peer(c
->peer
);
3081 /* Get client working directory. */
3083 server_client_get_cwd(struct client
*c
, struct session
*s
)
3087 if (!cfg_finished
&& cfg_client
!= NULL
)
3088 return (cfg_client
->cwd
);
3089 if (c
!= NULL
&& c
->session
== NULL
&& c
->cwd
!= NULL
)
3091 if (s
!= NULL
&& s
->cwd
!= NULL
)
3093 if (c
!= NULL
&& (s
= c
->session
) != NULL
&& s
->cwd
!= NULL
)
3095 if ((home
= find_home()) != NULL
)
3100 /* Get control client flags. */
3102 server_client_control_flags(struct client
*c
, const char *next
)
3104 if (strcmp(next
, "pause-after") == 0) {
3106 return (CLIENT_CONTROL_PAUSEAFTER
);
3108 if (sscanf(next
, "pause-after=%u", &c
->pause_age
) == 1) {
3109 c
->pause_age
*= 1000;
3110 return (CLIENT_CONTROL_PAUSEAFTER
);
3112 if (strcmp(next
, "no-output") == 0)
3113 return (CLIENT_CONTROL_NOOUTPUT
);
3114 if (strcmp(next
, "wait-exit") == 0)
3115 return (CLIENT_CONTROL_WAITEXIT
);
3119 /* Set client flags. */
3121 server_client_set_flags(struct client
*c
, const char *flags
)
3123 char *s
, *copy
, *next
;
3127 s
= copy
= xstrdup(flags
);
3128 while ((next
= strsep(&s
, ",")) != NULL
) {
3129 not = (*next
== '!');
3133 if (c
->flags
& CLIENT_CONTROL
)
3134 flag
= server_client_control_flags(c
, next
);
3137 if (strcmp(next
, "read-only") == 0)
3138 flag
= CLIENT_READONLY
;
3139 else if (strcmp(next
, "ignore-size") == 0)
3140 flag
= CLIENT_IGNORESIZE
;
3141 else if (strcmp(next
, "active-pane") == 0)
3142 flag
= CLIENT_ACTIVEPANE
;
3146 log_debug("client %s set flag %s", c
->name
, next
);
3148 if (c
->flags
& CLIENT_READONLY
)
3149 flag
&= ~CLIENT_READONLY
;
3153 if (flag
== CLIENT_CONTROL_NOOUTPUT
)
3154 control_reset_offsets(c
);
3157 proc_send(c
->peer
, MSG_FLAGS
, -1, &c
->flags
, sizeof c
->flags
);
3160 /* Get client flags. This is only flags useful to show to users. */
3162 server_client_get_flags(struct client
*c
)
3168 if (c
->flags
& CLIENT_ATTACHED
)
3169 strlcat(s
, "attached,", sizeof s
);
3170 if (c
->flags
& CLIENT_FOCUSED
)
3171 strlcat(s
, "focused,", sizeof s
);
3172 if (c
->flags
& CLIENT_CONTROL
)
3173 strlcat(s
, "control-mode,", sizeof s
);
3174 if (c
->flags
& CLIENT_IGNORESIZE
)
3175 strlcat(s
, "ignore-size,", sizeof s
);
3176 if (c
->flags
& CLIENT_CONTROL_NOOUTPUT
)
3177 strlcat(s
, "no-output,", sizeof s
);
3178 if (c
->flags
& CLIENT_CONTROL_WAITEXIT
)
3179 strlcat(s
, "wait-exit,", sizeof s
);
3180 if (c
->flags
& CLIENT_CONTROL_PAUSEAFTER
) {
3181 xsnprintf(tmp
, sizeof tmp
, "pause-after=%u,",
3182 c
->pause_age
/ 1000);
3183 strlcat(s
, tmp
, sizeof s
);
3185 if (c
->flags
& CLIENT_READONLY
)
3186 strlcat(s
, "read-only,", sizeof s
);
3187 if (c
->flags
& CLIENT_ACTIVEPANE
)
3188 strlcat(s
, "active-pane,", sizeof s
);
3189 if (c
->flags
& CLIENT_SUSPENDED
)
3190 strlcat(s
, "suspended,", sizeof s
);
3191 if (c
->flags
& CLIENT_UTF8
)
3192 strlcat(s
, "UTF-8,", sizeof s
);
3194 s
[strlen(s
) - 1] = '\0';
3198 /* Get client window. */
3199 struct client_window
*
3200 server_client_get_client_window(struct client
*c
, u_int id
)
3202 struct client_window cw
= { .window
= id
};
3204 return (RB_FIND(client_windows
, &c
->windows
, &cw
));
3207 /* Add client window. */
3208 struct client_window
*
3209 server_client_add_client_window(struct client
*c
, u_int id
)
3211 struct client_window
*cw
;
3213 cw
= server_client_get_client_window(c
, id
);
3215 cw
= xcalloc(1, sizeof *cw
);
3217 RB_INSERT(client_windows
, &c
->windows
, cw
);
3222 /* Get client active pane. */
3223 struct window_pane
*
3224 server_client_get_pane(struct client
*c
)
3226 struct session
*s
= c
->session
;
3227 struct client_window
*cw
;
3232 if (~c
->flags
& CLIENT_ACTIVEPANE
)
3233 return (s
->curw
->window
->active
);
3234 cw
= server_client_get_client_window(c
, s
->curw
->window
->id
);
3236 return (s
->curw
->window
->active
);
3240 /* Set client active pane. */
3242 server_client_set_pane(struct client
*c
, struct window_pane
*wp
)
3244 struct session
*s
= c
->session
;
3245 struct client_window
*cw
;
3250 cw
= server_client_add_client_window(c
, s
->curw
->window
->id
);
3252 log_debug("%s pane now %%%u", c
->name
, wp
->id
);
3255 /* Remove pane from client lists. */
3257 server_client_remove_pane(struct window_pane
*wp
)
3260 struct window
*w
= wp
->window
;
3261 struct client_window
*cw
;
3263 TAILQ_FOREACH(c
, &clients
, entry
) {
3264 cw
= server_client_get_client_window(c
, w
->id
);
3265 if (cw
!= NULL
&& cw
->pane
== wp
) {
3266 RB_REMOVE(client_windows
, &c
->windows
, cw
);
3272 /* Print to a client. */
3274 server_client_print(struct client
*c
, int parse
, struct evbuffer
*evb
)
3276 void *data
= EVBUFFER_DATA(evb
);
3277 size_t size
= EVBUFFER_LENGTH(evb
);
3278 struct window_pane
*wp
;
3279 struct window_mode_entry
*wme
;
3280 char *sanitized
, *msg
, *line
;
3283 utf8_stravisx(&msg
, data
, size
,
3284 VIS_OCTAL
|VIS_CSTYLE
|VIS_NOSLASH
);
3285 log_debug("%s: %s", __func__
, msg
);
3287 msg
= EVBUFFER_DATA(evb
);
3288 if (msg
[size
- 1] != '\0')
3289 evbuffer_add(evb
, "", 1);
3295 if (c
->session
== NULL
|| (c
->flags
& CLIENT_CONTROL
)) {
3296 if (~c
->flags
& CLIENT_UTF8
) {
3297 sanitized
= utf8_sanitize(msg
);
3298 if (c
->flags
& CLIENT_CONTROL
)
3299 control_write(c
, "%s", sanitized
);
3301 file_print(c
, "%s\n", sanitized
);
3304 if (c
->flags
& CLIENT_CONTROL
)
3305 control_write(c
, "%s", msg
);
3307 file_print(c
, "%s\n", msg
);
3312 wp
= server_client_get_pane(c
);
3313 wme
= TAILQ_FIRST(&wp
->modes
);
3314 if (wme
== NULL
|| wme
->mode
!= &window_view_mode
)
3315 window_pane_set_mode(wp
, NULL
, &window_view_mode
, NULL
, NULL
);
3318 line
= evbuffer_readln(evb
, NULL
, EVBUFFER_EOL_LF
);
3320 window_copy_add(wp
, 1, "%s", line
);
3323 } while (line
!= NULL
);
3325 size
= EVBUFFER_LENGTH(evb
);
3327 line
= EVBUFFER_DATA(evb
);
3328 window_copy_add(wp
, 1, "%.*s", (int)size
, line
);
3331 window_copy_add(wp
, 0, "%s", msg
);