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>
35 static void server_client_free(int, short, void *);
36 static void server_client_check_pane_resize(struct window_pane
*);
37 static void server_client_check_pane_buffer(struct window_pane
*);
38 static void server_client_check_window_resize(struct window
*);
39 static key_code
server_client_check_mouse(struct client
*, struct key_event
*);
40 static void server_client_repeat_timer(int, short, void *);
41 static void server_client_click_timer(int, short, void *);
42 static void server_client_check_exit(struct client
*);
43 static void server_client_check_redraw(struct client
*);
44 static void server_client_check_modes(struct client
*);
45 static void server_client_set_title(struct client
*);
46 static void server_client_reset_state(struct client
*);
47 static int server_client_assume_paste(struct session
*);
48 static void server_client_update_latest(struct client
*);
50 static void server_client_dispatch(struct imsg
*, void *);
51 static void server_client_dispatch_command(struct client
*, struct imsg
*);
52 static void server_client_dispatch_identify(struct client
*, struct imsg
*);
53 static void server_client_dispatch_shell(struct client
*);
55 /* Compare client windows. */
57 server_client_window_cmp(struct client_window
*cw1
,
58 struct client_window
*cw2
)
60 if (cw1
->window
< cw2
->window
)
62 if (cw1
->window
> cw2
->window
)
66 RB_GENERATE(client_windows
, client_window
, entry
, server_client_window_cmp
);
68 /* Number of attached clients. */
70 server_client_how_many(void)
76 TAILQ_FOREACH(c
, &clients
, entry
) {
77 if (c
->session
!= NULL
&& (~c
->flags
& CLIENT_UNATTACHEDFLAGS
))
83 /* Overlay timer callback. */
85 server_client_overlay_timer(__unused
int fd
, __unused
short events
, void *data
)
87 server_client_clear_overlay(data
);
90 /* Set an overlay on client. */
92 server_client_set_overlay(struct client
*c
, u_int delay
,
93 overlay_check_cb checkcb
, overlay_mode_cb modecb
,
94 overlay_draw_cb drawcb
, overlay_key_cb keycb
, overlay_free_cb freecb
,
95 overlay_resize_cb resizecb
, void *data
)
99 if (c
->overlay_draw
!= NULL
)
100 server_client_clear_overlay(c
);
102 tv
.tv_sec
= delay
/ 1000;
103 tv
.tv_usec
= (delay
% 1000) * 1000L;
105 if (event_initialized(&c
->overlay_timer
))
106 evtimer_del(&c
->overlay_timer
);
107 evtimer_set(&c
->overlay_timer
, server_client_overlay_timer
, c
);
109 evtimer_add(&c
->overlay_timer
, &tv
);
111 c
->overlay_check
= checkcb
;
112 c
->overlay_mode
= modecb
;
113 c
->overlay_draw
= drawcb
;
114 c
->overlay_key
= keycb
;
115 c
->overlay_free
= freecb
;
116 c
->overlay_resize
= resizecb
;
117 c
->overlay_data
= data
;
119 if (c
->overlay_check
== NULL
)
120 c
->tty
.flags
|= TTY_FREEZE
;
121 if (c
->overlay_mode
== NULL
)
122 c
->tty
.flags
|= TTY_NOCURSOR
;
123 server_redraw_client(c
);
126 /* Clear overlay mode on client. */
128 server_client_clear_overlay(struct client
*c
)
130 if (c
->overlay_draw
== NULL
)
133 if (event_initialized(&c
->overlay_timer
))
134 evtimer_del(&c
->overlay_timer
);
136 if (c
->overlay_free
!= NULL
)
137 c
->overlay_free(c
, c
->overlay_data
);
139 c
->overlay_check
= NULL
;
140 c
->overlay_mode
= NULL
;
141 c
->overlay_draw
= NULL
;
142 c
->overlay_key
= NULL
;
143 c
->overlay_free
= NULL
;
144 c
->overlay_data
= NULL
;
146 c
->tty
.flags
&= ~(TTY_FREEZE
|TTY_NOCURSOR
);
147 server_redraw_client(c
);
151 * Given overlay position and dimensions, return parts of the input range which
155 server_client_overlay_range(u_int x
, u_int y
, u_int sx
, u_int sy
, u_int px
,
156 u_int py
, u_int nx
, struct overlay_ranges
*r
)
160 /* Return up to 2 ranges. */
164 /* Trivial case of no overlap in the y direction. */
165 if (py
< y
|| py
> y
+ sy
- 1) {
173 /* Visible bit to the left of the popup. */
184 /* Visible bit to the right of the popup. */
198 /* Check if this client is inside this server. */
200 server_client_check_nested(struct client
*c
)
202 struct environ_entry
*envent
;
203 struct window_pane
*wp
;
205 envent
= environ_find(c
->environ
, "TMUX");
206 if (envent
== NULL
|| *envent
->value
== '\0')
209 RB_FOREACH(wp
, window_pane_tree
, &all_window_panes
) {
210 if (strcmp(wp
->tty
, c
->ttyname
) == 0)
216 /* Set client key table. */
218 server_client_set_key_table(struct client
*c
, const char *name
)
221 name
= server_client_get_key_table(c
);
223 key_bindings_unref_table(c
->keytable
);
224 c
->keytable
= key_bindings_get_table(name
, 1);
225 c
->keytable
->references
++;
228 /* Get default key table. */
230 server_client_get_key_table(struct client
*c
)
232 struct session
*s
= c
->session
;
238 name
= options_get_string(s
->options
, "key-table");
244 /* Is this table the default key table? */
246 server_client_is_default_key_table(struct client
*c
, struct key_table
*table
)
248 return (strcmp(table
->name
, server_client_get_key_table(c
)) == 0);
251 /* Create a new client. */
253 server_client_create(int fd
)
259 c
= xcalloc(1, sizeof *c
);
261 c
->peer
= proc_add_peer(server_proc
, fd
, server_client_dispatch
, c
);
263 if (gettimeofday(&c
->creation_time
, NULL
) != 0)
264 fatal("gettimeofday failed");
265 memcpy(&c
->activity_time
, &c
->creation_time
, sizeof c
->activity_time
);
267 c
->environ
= environ_create();
272 c
->queue
= cmdq_new();
273 RB_INIT(&c
->windows
);
280 c
->flags
|= CLIENT_FOCUSED
;
282 c
->keytable
= key_bindings_get_table("root", 1);
283 c
->keytable
->references
++;
285 evtimer_set(&c
->repeat_timer
, server_client_repeat_timer
, c
);
286 evtimer_set(&c
->click_timer
, server_client_click_timer
, c
);
288 TAILQ_INSERT_TAIL(&clients
, c
, entry
);
289 log_debug("new client %p", c
);
293 /* Open client terminal if needed. */
295 server_client_open(struct client
*c
, char **cause
)
297 const char *ttynam
= _PATH_TTY
;
299 if (c
->flags
& CLIENT_CONTROL
)
302 if (strcmp(c
->ttyname
, ttynam
) == 0||
303 ((isatty(STDIN_FILENO
) &&
304 (ttynam
= ttyname(STDIN_FILENO
)) != NULL
&&
305 strcmp(c
->ttyname
, ttynam
) == 0) ||
306 (isatty(STDOUT_FILENO
) &&
307 (ttynam
= ttyname(STDOUT_FILENO
)) != NULL
&&
308 strcmp(c
->ttyname
, ttynam
) == 0) ||
309 (isatty(STDERR_FILENO
) &&
310 (ttynam
= ttyname(STDERR_FILENO
)) != NULL
&&
311 strcmp(c
->ttyname
, ttynam
) == 0))) {
312 xasprintf(cause
, "can't use %s", c
->ttyname
);
316 if (!(c
->flags
& CLIENT_TERMINAL
)) {
317 *cause
= xstrdup("not a terminal");
321 if (tty_open(&c
->tty
, cause
) != 0)
327 /* Lost an attached client. */
329 server_client_attached_lost(struct client
*c
)
334 struct client
*found
;
336 log_debug("lost attached client %p", c
);
339 * By this point the session in the client has been cleared so walk all
340 * windows to find any with this client as the latest.
342 RB_FOREACH(w
, windows
, &windows
) {
347 TAILQ_FOREACH(loop
, &clients
, entry
) {
349 if (loop
== c
|| s
== NULL
|| s
->curw
->window
!= w
)
351 if (found
== NULL
|| timercmp(&loop
->activity_time
,
352 &found
->activity_time
, >))
356 server_client_update_latest(found
);
360 /* Set client session. */
362 server_client_set_session(struct client
*c
, struct session
*s
)
364 struct session
*old
= c
->session
;
366 if (s
!= NULL
&& c
->session
!= NULL
&& c
->session
!= s
)
367 c
->last_session
= c
->session
;
369 c
->last_session
= NULL
;
371 c
->flags
|= CLIENT_FOCUSED
;
373 if (old
!= NULL
&& old
->curw
!= NULL
)
374 window_update_focus(old
->curw
->window
);
377 window_update_focus(s
->curw
->window
);
378 session_update_activity(s
, NULL
);
379 gettimeofday(&s
->last_attached_time
, NULL
);
380 s
->curw
->flags
&= ~WINLINK_ALERTFLAGS
;
381 s
->curw
->window
->latest
= c
;
382 alerts_check_session(s
);
383 tty_update_client_offset(c
);
384 status_timer_start(c
);
385 notify_client("client-session-changed", c
);
386 server_redraw_client(c
);
389 server_check_unattached();
390 server_update_socket();
395 server_client_lost(struct client
*c
)
397 struct client_file
*cf
, *cf1
;
398 struct client_window
*cw
, *cw1
;
400 c
->flags
|= CLIENT_DEAD
;
402 server_client_clear_overlay(c
);
403 status_prompt_clear(c
);
404 status_message_clear(c
);
406 RB_FOREACH_SAFE(cf
, client_files
, &c
->files
, cf1
) {
410 RB_FOREACH_SAFE(cw
, client_windows
, &c
->windows
, cw1
) {
411 RB_REMOVE(client_windows
, &c
->windows
, cw
);
415 TAILQ_REMOVE(&clients
, c
, entry
);
416 log_debug("lost client %p", c
);
418 if (c
->flags
& CLIENT_ATTACHED
) {
419 server_client_attached_lost(c
);
420 notify_client("client-detached", c
);
423 if (c
->flags
& CLIENT_CONTROL
)
425 if (c
->flags
& CLIENT_TERMINAL
)
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
;
565 struct window_pane
*wp
;
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. */
673 /* Is this on the status line? */
674 m
->statusat
= status_at_line(c
);
675 m
->statuslines
= status_line_size(c
);
676 if (m
->statusat
!= -1 &&
677 y
>= (u_int
)m
->statusat
&&
678 y
< m
->statusat
+ m
->statuslines
) {
679 sr
= status_get_range(c
, x
, y
- m
->statusat
);
681 where
= STATUS_DEFAULT
;
684 case STYLE_RANGE_NONE
:
685 return (KEYC_UNKNOWN
);
686 case STYLE_RANGE_LEFT
:
689 case STYLE_RANGE_RIGHT
:
690 where
= STATUS_RIGHT
;
692 case STYLE_RANGE_WINDOW
:
693 wl
= winlink_find_by_index(&s
->windows
,
696 return (KEYC_UNKNOWN
);
697 m
->w
= wl
->window
->id
;
705 /* Not on status line. Adjust position and check for border or pane. */
706 if (where
== NOWHERE
) {
708 if (m
->statusat
== 0 && y
>= m
->statuslines
)
709 py
= y
- m
->statuslines
;
710 else if (m
->statusat
> 0 && y
>= (u_int
)m
->statusat
)
711 py
= m
->statusat
- 1;
715 tty_window_offset(&c
->tty
, &m
->ox
, &m
->oy
, &sx
, &sy
);
716 log_debug("mouse window @%u at %u,%u (%ux%u)",
717 s
->curw
->window
->id
, m
->ox
, m
->oy
, sx
, sy
);
718 if (px
> sx
|| py
> sy
)
719 return (KEYC_UNKNOWN
);
723 /* Try the pane borders if not zoomed. */
724 if (~s
->curw
->window
->flags
& WINDOW_ZOOMED
) {
725 TAILQ_FOREACH(wp
, &s
->curw
->window
->panes
, entry
) {
726 if ((wp
->xoff
+ wp
->sx
== px
&&
727 wp
->yoff
<= 1 + py
&&
728 wp
->yoff
+ wp
->sy
>= py
) ||
729 (wp
->yoff
+ wp
->sy
== py
&&
730 wp
->xoff
<= 1 + px
&&
731 wp
->xoff
+ wp
->sx
>= px
))
738 /* Otherwise try inside the pane. */
739 if (where
== NOWHERE
) {
740 wp
= window_get_active_at(s
->curw
->window
, px
, py
);
744 return (KEYC_UNKNOWN
);
747 log_debug("mouse %u,%u on pane %%%u", x
, y
, wp
->id
);
748 else if (where
== BORDER
)
749 log_debug("mouse on pane %%%u border", wp
->id
);
751 m
->w
= wp
->window
->id
;
755 /* Stop dragging if needed. */
756 if (type
!= DRAG
&& type
!= WHEEL
&& c
->tty
.mouse_drag_flag
!= 0) {
757 if (c
->tty
.mouse_drag_release
!= NULL
)
758 c
->tty
.mouse_drag_release(c
, m
);
760 c
->tty
.mouse_drag_update
= NULL
;
761 c
->tty
.mouse_drag_release
= NULL
;
764 * End a mouse drag by passing a MouseDragEnd key corresponding
765 * to the button that started the drag.
767 switch (c
->tty
.mouse_drag_flag
- 1) {
770 key
= KEYC_MOUSEDRAGEND1_PANE
;
772 key
= KEYC_MOUSEDRAGEND1_STATUS
;
773 if (where
== STATUS_LEFT
)
774 key
= KEYC_MOUSEDRAGEND1_STATUS_LEFT
;
775 if (where
== STATUS_RIGHT
)
776 key
= KEYC_MOUSEDRAGEND1_STATUS_RIGHT
;
777 if (where
== STATUS_DEFAULT
)
778 key
= KEYC_MOUSEDRAGEND1_STATUS_DEFAULT
;
780 key
= KEYC_MOUSEDRAGEND1_BORDER
;
784 key
= KEYC_MOUSEDRAGEND2_PANE
;
786 key
= KEYC_MOUSEDRAGEND2_STATUS
;
787 if (where
== STATUS_LEFT
)
788 key
= KEYC_MOUSEDRAGEND2_STATUS_LEFT
;
789 if (where
== STATUS_RIGHT
)
790 key
= KEYC_MOUSEDRAGEND2_STATUS_RIGHT
;
791 if (where
== STATUS_DEFAULT
)
792 key
= KEYC_MOUSEDRAGEND2_STATUS_DEFAULT
;
794 key
= KEYC_MOUSEDRAGEND2_BORDER
;
798 key
= KEYC_MOUSEDRAGEND3_PANE
;
800 key
= KEYC_MOUSEDRAGEND3_STATUS
;
801 if (where
== STATUS_LEFT
)
802 key
= KEYC_MOUSEDRAGEND3_STATUS_LEFT
;
803 if (where
== STATUS_RIGHT
)
804 key
= KEYC_MOUSEDRAGEND3_STATUS_RIGHT
;
805 if (where
== STATUS_DEFAULT
)
806 key
= KEYC_MOUSEDRAGEND3_STATUS_DEFAULT
;
808 key
= KEYC_MOUSEDRAGEND3_BORDER
;
812 key
= KEYC_MOUSEDRAGEND6_PANE
;
814 key
= KEYC_MOUSEDRAGEND6_STATUS
;
815 if (where
== STATUS_LEFT
)
816 key
= KEYC_MOUSEDRAGEND6_STATUS_LEFT
;
817 if (where
== STATUS_RIGHT
)
818 key
= KEYC_MOUSEDRAGEND6_STATUS_RIGHT
;
819 if (where
== STATUS_DEFAULT
)
820 key
= KEYC_MOUSEDRAGEND6_STATUS_DEFAULT
;
822 key
= KEYC_MOUSEDRAGEND6_BORDER
;
826 key
= KEYC_MOUSEDRAGEND7_PANE
;
828 key
= KEYC_MOUSEDRAGEND7_STATUS
;
829 if (where
== STATUS_LEFT
)
830 key
= KEYC_MOUSEDRAGEND7_STATUS_LEFT
;
831 if (where
== STATUS_RIGHT
)
832 key
= KEYC_MOUSEDRAGEND7_STATUS_RIGHT
;
833 if (where
== STATUS_DEFAULT
)
834 key
= KEYC_MOUSEDRAGEND7_STATUS_DEFAULT
;
836 key
= KEYC_MOUSEDRAGEND7_BORDER
;
840 key
= KEYC_MOUSEDRAGEND8_PANE
;
842 key
= KEYC_MOUSEDRAGEND8_STATUS
;
843 if (where
== STATUS_LEFT
)
844 key
= KEYC_MOUSEDRAGEND8_STATUS_LEFT
;
845 if (where
== STATUS_RIGHT
)
846 key
= KEYC_MOUSEDRAGEND8_STATUS_RIGHT
;
847 if (where
== STATUS_DEFAULT
)
848 key
= KEYC_MOUSEDRAGEND8_STATUS_DEFAULT
;
850 key
= KEYC_MOUSEDRAGEND8_BORDER
;
854 key
= KEYC_MOUSEDRAGEND9_PANE
;
856 key
= KEYC_MOUSEDRAGEND9_STATUS
;
857 if (where
== STATUS_LEFT
)
858 key
= KEYC_MOUSEDRAGEND9_STATUS_LEFT
;
859 if (where
== STATUS_RIGHT
)
860 key
= KEYC_MOUSEDRAGEND9_STATUS_RIGHT
;
861 if (where
== STATUS_DEFAULT
)
862 key
= KEYC_MOUSEDRAGEND9_STATUS_DEFAULT
;
864 key
= KEYC_MOUSEDRAGEND9_BORDER
;
866 case MOUSE_BUTTON_10
:
868 key
= KEYC_MOUSEDRAGEND10_PANE
;
870 key
= KEYC_MOUSEDRAGEND10_STATUS
;
871 if (where
== STATUS_LEFT
)
872 key
= KEYC_MOUSEDRAGEND10_STATUS_LEFT
;
873 if (where
== STATUS_RIGHT
)
874 key
= KEYC_MOUSEDRAGEND10_STATUS_RIGHT
;
875 if (where
== STATUS_DEFAULT
)
876 key
= KEYC_MOUSEDRAGEND10_STATUS_DEFAULT
;
878 key
= KEYC_MOUSEDRAGEND10_BORDER
;
880 case MOUSE_BUTTON_11
:
882 key
= KEYC_MOUSEDRAGEND11_PANE
;
884 key
= KEYC_MOUSEDRAGEND11_STATUS
;
885 if (where
== STATUS_LEFT
)
886 key
= KEYC_MOUSEDRAGEND11_STATUS_LEFT
;
887 if (where
== STATUS_RIGHT
)
888 key
= KEYC_MOUSEDRAGEND11_STATUS_RIGHT
;
889 if (where
== STATUS_DEFAULT
)
890 key
= KEYC_MOUSEDRAGEND11_STATUS_DEFAULT
;
892 key
= KEYC_MOUSEDRAGEND11_BORDER
;
898 c
->tty
.mouse_drag_flag
= 0;
902 /* Convert to a key binding. */
909 key
= KEYC_MOUSEMOVE_PANE
;
911 key
= KEYC_MOUSEMOVE_STATUS
;
912 if (where
== STATUS_LEFT
)
913 key
= KEYC_MOUSEMOVE_STATUS_LEFT
;
914 if (where
== STATUS_RIGHT
)
915 key
= KEYC_MOUSEMOVE_STATUS_RIGHT
;
916 if (where
== STATUS_DEFAULT
)
917 key
= KEYC_MOUSEMOVE_STATUS_DEFAULT
;
919 key
= KEYC_MOUSEMOVE_BORDER
;
922 if (c
->tty
.mouse_drag_update
!= NULL
)
925 switch (MOUSE_BUTTONS(b
)) {
928 key
= KEYC_MOUSEDRAG1_PANE
;
930 key
= KEYC_MOUSEDRAG1_STATUS
;
931 if (where
== STATUS_LEFT
)
932 key
= KEYC_MOUSEDRAG1_STATUS_LEFT
;
933 if (where
== STATUS_RIGHT
)
934 key
= KEYC_MOUSEDRAG1_STATUS_RIGHT
;
935 if (where
== STATUS_DEFAULT
)
936 key
= KEYC_MOUSEDRAG1_STATUS_DEFAULT
;
938 key
= KEYC_MOUSEDRAG1_BORDER
;
942 key
= KEYC_MOUSEDRAG2_PANE
;
944 key
= KEYC_MOUSEDRAG2_STATUS
;
945 if (where
== STATUS_LEFT
)
946 key
= KEYC_MOUSEDRAG2_STATUS_LEFT
;
947 if (where
== STATUS_RIGHT
)
948 key
= KEYC_MOUSEDRAG2_STATUS_RIGHT
;
949 if (where
== STATUS_DEFAULT
)
950 key
= KEYC_MOUSEDRAG2_STATUS_DEFAULT
;
952 key
= KEYC_MOUSEDRAG2_BORDER
;
956 key
= KEYC_MOUSEDRAG3_PANE
;
958 key
= KEYC_MOUSEDRAG3_STATUS
;
959 if (where
== STATUS_LEFT
)
960 key
= KEYC_MOUSEDRAG3_STATUS_LEFT
;
961 if (where
== STATUS_RIGHT
)
962 key
= KEYC_MOUSEDRAG3_STATUS_RIGHT
;
963 if (where
== STATUS_DEFAULT
)
964 key
= KEYC_MOUSEDRAG3_STATUS_DEFAULT
;
966 key
= KEYC_MOUSEDRAG3_BORDER
;
970 key
= KEYC_MOUSEDRAG6_PANE
;
972 key
= KEYC_MOUSEDRAG6_STATUS
;
973 if (where
== STATUS_LEFT
)
974 key
= KEYC_MOUSEDRAG6_STATUS_LEFT
;
975 if (where
== STATUS_RIGHT
)
976 key
= KEYC_MOUSEDRAG6_STATUS_RIGHT
;
977 if (where
== STATUS_DEFAULT
)
978 key
= KEYC_MOUSEDRAG6_STATUS_DEFAULT
;
980 key
= KEYC_MOUSEDRAG6_BORDER
;
984 key
= KEYC_MOUSEDRAG7_PANE
;
986 key
= KEYC_MOUSEDRAG7_STATUS
;
987 if (where
== STATUS_LEFT
)
988 key
= KEYC_MOUSEDRAG7_STATUS_LEFT
;
989 if (where
== STATUS_RIGHT
)
990 key
= KEYC_MOUSEDRAG7_STATUS_RIGHT
;
991 if (where
== STATUS_DEFAULT
)
992 key
= KEYC_MOUSEDRAG7_STATUS_DEFAULT
;
994 key
= KEYC_MOUSEDRAG7_BORDER
;
998 key
= KEYC_MOUSEDRAG8_PANE
;
1000 key
= KEYC_MOUSEDRAG8_STATUS
;
1001 if (where
== STATUS_LEFT
)
1002 key
= KEYC_MOUSEDRAG8_STATUS_LEFT
;
1003 if (where
== STATUS_RIGHT
)
1004 key
= KEYC_MOUSEDRAG8_STATUS_RIGHT
;
1005 if (where
== STATUS_DEFAULT
)
1006 key
= KEYC_MOUSEDRAG8_STATUS_DEFAULT
;
1007 if (where
== BORDER
)
1008 key
= KEYC_MOUSEDRAG8_BORDER
;
1010 case MOUSE_BUTTON_9
:
1012 key
= KEYC_MOUSEDRAG9_PANE
;
1013 if (where
== STATUS
)
1014 key
= KEYC_MOUSEDRAG9_STATUS
;
1015 if (where
== STATUS_LEFT
)
1016 key
= KEYC_MOUSEDRAG9_STATUS_LEFT
;
1017 if (where
== STATUS_RIGHT
)
1018 key
= KEYC_MOUSEDRAG9_STATUS_RIGHT
;
1019 if (where
== STATUS_DEFAULT
)
1020 key
= KEYC_MOUSEDRAG9_STATUS_DEFAULT
;
1021 if (where
== BORDER
)
1022 key
= KEYC_MOUSEDRAG9_BORDER
;
1024 case MOUSE_BUTTON_10
:
1026 key
= KEYC_MOUSEDRAG10_PANE
;
1027 if (where
== STATUS
)
1028 key
= KEYC_MOUSEDRAG10_STATUS
;
1029 if (where
== STATUS_LEFT
)
1030 key
= KEYC_MOUSEDRAG10_STATUS_LEFT
;
1031 if (where
== STATUS_RIGHT
)
1032 key
= KEYC_MOUSEDRAG10_STATUS_RIGHT
;
1033 if (where
== STATUS_DEFAULT
)
1034 key
= KEYC_MOUSEDRAG10_STATUS_DEFAULT
;
1035 if (where
== BORDER
)
1036 key
= KEYC_MOUSEDRAG10_BORDER
;
1038 case MOUSE_BUTTON_11
:
1040 key
= KEYC_MOUSEDRAG11_PANE
;
1041 if (where
== STATUS
)
1042 key
= KEYC_MOUSEDRAG11_STATUS
;
1043 if (where
== STATUS_LEFT
)
1044 key
= KEYC_MOUSEDRAG11_STATUS_LEFT
;
1045 if (where
== STATUS_RIGHT
)
1046 key
= KEYC_MOUSEDRAG11_STATUS_RIGHT
;
1047 if (where
== STATUS_DEFAULT
)
1048 key
= KEYC_MOUSEDRAG11_STATUS_DEFAULT
;
1049 if (where
== BORDER
)
1050 key
= KEYC_MOUSEDRAG11_BORDER
;
1056 * Begin a drag by setting the flag to a non-zero value that
1057 * corresponds to the mouse button in use.
1059 c
->tty
.mouse_drag_flag
= MOUSE_BUTTONS(b
) + 1;
1062 if (MOUSE_BUTTONS(b
) == MOUSE_WHEEL_UP
) {
1064 key
= KEYC_WHEELUP_PANE
;
1065 if (where
== STATUS
)
1066 key
= KEYC_WHEELUP_STATUS
;
1067 if (where
== STATUS_LEFT
)
1068 key
= KEYC_WHEELUP_STATUS_LEFT
;
1069 if (where
== STATUS_RIGHT
)
1070 key
= KEYC_WHEELUP_STATUS_RIGHT
;
1071 if (where
== STATUS_DEFAULT
)
1072 key
= KEYC_WHEELUP_STATUS_DEFAULT
;
1073 if (where
== BORDER
)
1074 key
= KEYC_WHEELUP_BORDER
;
1077 key
= KEYC_WHEELDOWN_PANE
;
1078 if (where
== STATUS
)
1079 key
= KEYC_WHEELDOWN_STATUS
;
1080 if (where
== STATUS_LEFT
)
1081 key
= KEYC_WHEELDOWN_STATUS_LEFT
;
1082 if (where
== STATUS_RIGHT
)
1083 key
= KEYC_WHEELDOWN_STATUS_RIGHT
;
1084 if (where
== STATUS_DEFAULT
)
1085 key
= KEYC_WHEELDOWN_STATUS_DEFAULT
;
1086 if (where
== BORDER
)
1087 key
= KEYC_WHEELDOWN_BORDER
;
1091 switch (MOUSE_BUTTONS(b
)) {
1092 case MOUSE_BUTTON_1
:
1094 key
= KEYC_MOUSEUP1_PANE
;
1095 if (where
== STATUS
)
1096 key
= KEYC_MOUSEUP1_STATUS
;
1097 if (where
== STATUS_LEFT
)
1098 key
= KEYC_MOUSEUP1_STATUS_LEFT
;
1099 if (where
== STATUS_RIGHT
)
1100 key
= KEYC_MOUSEUP1_STATUS_RIGHT
;
1101 if (where
== STATUS_DEFAULT
)
1102 key
= KEYC_MOUSEUP1_STATUS_DEFAULT
;
1103 if (where
== BORDER
)
1104 key
= KEYC_MOUSEUP1_BORDER
;
1106 case MOUSE_BUTTON_2
:
1108 key
= KEYC_MOUSEUP2_PANE
;
1109 if (where
== STATUS
)
1110 key
= KEYC_MOUSEUP2_STATUS
;
1111 if (where
== STATUS_LEFT
)
1112 key
= KEYC_MOUSEUP2_STATUS_LEFT
;
1113 if (where
== STATUS_RIGHT
)
1114 key
= KEYC_MOUSEUP2_STATUS_RIGHT
;
1115 if (where
== STATUS_DEFAULT
)
1116 key
= KEYC_MOUSEUP2_STATUS_DEFAULT
;
1117 if (where
== BORDER
)
1118 key
= KEYC_MOUSEUP2_BORDER
;
1120 case MOUSE_BUTTON_3
:
1122 key
= KEYC_MOUSEUP3_PANE
;
1123 if (where
== STATUS
)
1124 key
= KEYC_MOUSEUP3_STATUS
;
1125 if (where
== STATUS_LEFT
)
1126 key
= KEYC_MOUSEUP3_STATUS_LEFT
;
1127 if (where
== STATUS_RIGHT
)
1128 key
= KEYC_MOUSEUP3_STATUS_RIGHT
;
1129 if (where
== STATUS_DEFAULT
)
1130 key
= KEYC_MOUSEUP3_STATUS_DEFAULT
;
1131 if (where
== BORDER
)
1132 key
= KEYC_MOUSEUP3_BORDER
;
1134 case MOUSE_BUTTON_6
:
1136 key
= KEYC_MOUSEUP6_PANE
;
1137 if (where
== STATUS
)
1138 key
= KEYC_MOUSEUP6_STATUS
;
1139 if (where
== STATUS_LEFT
)
1140 key
= KEYC_MOUSEUP6_STATUS_LEFT
;
1141 if (where
== STATUS_RIGHT
)
1142 key
= KEYC_MOUSEUP6_STATUS_RIGHT
;
1143 if (where
== STATUS_DEFAULT
)
1144 key
= KEYC_MOUSEUP6_STATUS_DEFAULT
;
1145 if (where
== BORDER
)
1146 key
= KEYC_MOUSEUP6_BORDER
;
1148 case MOUSE_BUTTON_7
:
1150 key
= KEYC_MOUSEUP7_PANE
;
1151 if (where
== STATUS
)
1152 key
= KEYC_MOUSEUP7_STATUS
;
1153 if (where
== STATUS_LEFT
)
1154 key
= KEYC_MOUSEUP7_STATUS_LEFT
;
1155 if (where
== STATUS_RIGHT
)
1156 key
= KEYC_MOUSEUP7_STATUS_RIGHT
;
1157 if (where
== STATUS_DEFAULT
)
1158 key
= KEYC_MOUSEUP7_STATUS_DEFAULT
;
1159 if (where
== BORDER
)
1160 key
= KEYC_MOUSEUP7_BORDER
;
1162 case MOUSE_BUTTON_8
:
1164 key
= KEYC_MOUSEUP8_PANE
;
1165 if (where
== STATUS
)
1166 key
= KEYC_MOUSEUP8_STATUS
;
1167 if (where
== STATUS_LEFT
)
1168 key
= KEYC_MOUSEUP8_STATUS_LEFT
;
1169 if (where
== STATUS_RIGHT
)
1170 key
= KEYC_MOUSEUP8_STATUS_RIGHT
;
1171 if (where
== STATUS_DEFAULT
)
1172 key
= KEYC_MOUSEUP8_STATUS_DEFAULT
;
1173 if (where
== BORDER
)
1174 key
= KEYC_MOUSEUP8_BORDER
;
1176 case MOUSE_BUTTON_9
:
1178 key
= KEYC_MOUSEUP9_PANE
;
1179 if (where
== STATUS
)
1180 key
= KEYC_MOUSEUP9_STATUS
;
1181 if (where
== STATUS_LEFT
)
1182 key
= KEYC_MOUSEUP9_STATUS_LEFT
;
1183 if (where
== STATUS_RIGHT
)
1184 key
= KEYC_MOUSEUP9_STATUS_RIGHT
;
1185 if (where
== STATUS_DEFAULT
)
1186 key
= KEYC_MOUSEUP9_STATUS_DEFAULT
;
1187 if (where
== BORDER
)
1188 key
= KEYC_MOUSEUP9_BORDER
;
1190 case MOUSE_BUTTON_10
:
1192 key
= KEYC_MOUSEUP1_PANE
;
1193 if (where
== STATUS
)
1194 key
= KEYC_MOUSEUP1_STATUS
;
1195 if (where
== STATUS_LEFT
)
1196 key
= KEYC_MOUSEUP1_STATUS_LEFT
;
1197 if (where
== STATUS_RIGHT
)
1198 key
= KEYC_MOUSEUP1_STATUS_RIGHT
;
1199 if (where
== STATUS_DEFAULT
)
1200 key
= KEYC_MOUSEUP1_STATUS_DEFAULT
;
1201 if (where
== BORDER
)
1202 key
= KEYC_MOUSEUP1_BORDER
;
1204 case MOUSE_BUTTON_11
:
1206 key
= KEYC_MOUSEUP11_PANE
;
1207 if (where
== STATUS
)
1208 key
= KEYC_MOUSEUP11_STATUS
;
1209 if (where
== STATUS_LEFT
)
1210 key
= KEYC_MOUSEUP11_STATUS_LEFT
;
1211 if (where
== STATUS_RIGHT
)
1212 key
= KEYC_MOUSEUP11_STATUS_RIGHT
;
1213 if (where
== STATUS_DEFAULT
)
1214 key
= KEYC_MOUSEUP11_STATUS_DEFAULT
;
1215 if (where
== BORDER
)
1216 key
= KEYC_MOUSEUP11_BORDER
;
1221 switch (MOUSE_BUTTONS(b
)) {
1222 case MOUSE_BUTTON_1
:
1224 key
= KEYC_MOUSEDOWN1_PANE
;
1225 if (where
== STATUS
)
1226 key
= KEYC_MOUSEDOWN1_STATUS
;
1227 if (where
== STATUS_LEFT
)
1228 key
= KEYC_MOUSEDOWN1_STATUS_LEFT
;
1229 if (where
== STATUS_RIGHT
)
1230 key
= KEYC_MOUSEDOWN1_STATUS_RIGHT
;
1231 if (where
== STATUS_DEFAULT
)
1232 key
= KEYC_MOUSEDOWN1_STATUS_DEFAULT
;
1233 if (where
== BORDER
)
1234 key
= KEYC_MOUSEDOWN1_BORDER
;
1236 case MOUSE_BUTTON_2
:
1238 key
= KEYC_MOUSEDOWN2_PANE
;
1239 if (where
== STATUS
)
1240 key
= KEYC_MOUSEDOWN2_STATUS
;
1241 if (where
== STATUS_LEFT
)
1242 key
= KEYC_MOUSEDOWN2_STATUS_LEFT
;
1243 if (where
== STATUS_RIGHT
)
1244 key
= KEYC_MOUSEDOWN2_STATUS_RIGHT
;
1245 if (where
== STATUS_DEFAULT
)
1246 key
= KEYC_MOUSEDOWN2_STATUS_DEFAULT
;
1247 if (where
== BORDER
)
1248 key
= KEYC_MOUSEDOWN2_BORDER
;
1250 case MOUSE_BUTTON_3
:
1252 key
= KEYC_MOUSEDOWN3_PANE
;
1253 if (where
== STATUS
)
1254 key
= KEYC_MOUSEDOWN3_STATUS
;
1255 if (where
== STATUS_LEFT
)
1256 key
= KEYC_MOUSEDOWN3_STATUS_LEFT
;
1257 if (where
== STATUS_RIGHT
)
1258 key
= KEYC_MOUSEDOWN3_STATUS_RIGHT
;
1259 if (where
== STATUS_DEFAULT
)
1260 key
= KEYC_MOUSEDOWN3_STATUS_DEFAULT
;
1261 if (where
== BORDER
)
1262 key
= KEYC_MOUSEDOWN3_BORDER
;
1264 case MOUSE_BUTTON_6
:
1266 key
= KEYC_MOUSEDOWN6_PANE
;
1267 if (where
== STATUS
)
1268 key
= KEYC_MOUSEDOWN6_STATUS
;
1269 if (where
== STATUS_LEFT
)
1270 key
= KEYC_MOUSEDOWN6_STATUS_LEFT
;
1271 if (where
== STATUS_RIGHT
)
1272 key
= KEYC_MOUSEDOWN6_STATUS_RIGHT
;
1273 if (where
== STATUS_DEFAULT
)
1274 key
= KEYC_MOUSEDOWN6_STATUS_DEFAULT
;
1275 if (where
== BORDER
)
1276 key
= KEYC_MOUSEDOWN6_BORDER
;
1278 case MOUSE_BUTTON_7
:
1280 key
= KEYC_MOUSEDOWN7_PANE
;
1281 if (where
== STATUS
)
1282 key
= KEYC_MOUSEDOWN7_STATUS
;
1283 if (where
== STATUS_LEFT
)
1284 key
= KEYC_MOUSEDOWN7_STATUS_LEFT
;
1285 if (where
== STATUS_RIGHT
)
1286 key
= KEYC_MOUSEDOWN7_STATUS_RIGHT
;
1287 if (where
== STATUS_DEFAULT
)
1288 key
= KEYC_MOUSEDOWN7_STATUS_DEFAULT
;
1289 if (where
== BORDER
)
1290 key
= KEYC_MOUSEDOWN7_BORDER
;
1292 case MOUSE_BUTTON_8
:
1294 key
= KEYC_MOUSEDOWN8_PANE
;
1295 if (where
== STATUS
)
1296 key
= KEYC_MOUSEDOWN8_STATUS
;
1297 if (where
== STATUS_LEFT
)
1298 key
= KEYC_MOUSEDOWN8_STATUS_LEFT
;
1299 if (where
== STATUS_RIGHT
)
1300 key
= KEYC_MOUSEDOWN8_STATUS_RIGHT
;
1301 if (where
== STATUS_DEFAULT
)
1302 key
= KEYC_MOUSEDOWN8_STATUS_DEFAULT
;
1303 if (where
== BORDER
)
1304 key
= KEYC_MOUSEDOWN8_BORDER
;
1306 case MOUSE_BUTTON_9
:
1308 key
= KEYC_MOUSEDOWN9_PANE
;
1309 if (where
== STATUS
)
1310 key
= KEYC_MOUSEDOWN9_STATUS
;
1311 if (where
== STATUS_LEFT
)
1312 key
= KEYC_MOUSEDOWN9_STATUS_LEFT
;
1313 if (where
== STATUS_RIGHT
)
1314 key
= KEYC_MOUSEDOWN9_STATUS_RIGHT
;
1315 if (where
== STATUS_DEFAULT
)
1316 key
= KEYC_MOUSEDOWN9_STATUS_DEFAULT
;
1317 if (where
== BORDER
)
1318 key
= KEYC_MOUSEDOWN9_BORDER
;
1320 case MOUSE_BUTTON_10
:
1322 key
= KEYC_MOUSEDOWN10_PANE
;
1323 if (where
== STATUS
)
1324 key
= KEYC_MOUSEDOWN10_STATUS
;
1325 if (where
== STATUS_LEFT
)
1326 key
= KEYC_MOUSEDOWN10_STATUS_LEFT
;
1327 if (where
== STATUS_RIGHT
)
1328 key
= KEYC_MOUSEDOWN10_STATUS_RIGHT
;
1329 if (where
== STATUS_DEFAULT
)
1330 key
= KEYC_MOUSEDOWN10_STATUS_DEFAULT
;
1331 if (where
== BORDER
)
1332 key
= KEYC_MOUSEDOWN10_BORDER
;
1334 case MOUSE_BUTTON_11
:
1336 key
= KEYC_MOUSEDOWN11_PANE
;
1337 if (where
== STATUS
)
1338 key
= KEYC_MOUSEDOWN11_STATUS
;
1339 if (where
== STATUS_LEFT
)
1340 key
= KEYC_MOUSEDOWN11_STATUS_LEFT
;
1341 if (where
== STATUS_RIGHT
)
1342 key
= KEYC_MOUSEDOWN11_STATUS_RIGHT
;
1343 if (where
== STATUS_DEFAULT
)
1344 key
= KEYC_MOUSEDOWN11_STATUS_DEFAULT
;
1345 if (where
== BORDER
)
1346 key
= KEYC_MOUSEDOWN11_BORDER
;
1351 switch (MOUSE_BUTTONS(b
)) {
1352 case MOUSE_BUTTON_1
:
1354 key
= KEYC_SECONDCLICK1_PANE
;
1355 if (where
== STATUS
)
1356 key
= KEYC_SECONDCLICK1_STATUS
;
1357 if (where
== STATUS_LEFT
)
1358 key
= KEYC_SECONDCLICK1_STATUS_LEFT
;
1359 if (where
== STATUS_RIGHT
)
1360 key
= KEYC_SECONDCLICK1_STATUS_RIGHT
;
1361 if (where
== STATUS_DEFAULT
)
1362 key
= KEYC_SECONDCLICK1_STATUS_DEFAULT
;
1363 if (where
== BORDER
)
1364 key
= KEYC_SECONDCLICK1_BORDER
;
1366 case MOUSE_BUTTON_2
:
1368 key
= KEYC_SECONDCLICK2_PANE
;
1369 if (where
== STATUS
)
1370 key
= KEYC_SECONDCLICK2_STATUS
;
1371 if (where
== STATUS_LEFT
)
1372 key
= KEYC_SECONDCLICK2_STATUS_LEFT
;
1373 if (where
== STATUS_RIGHT
)
1374 key
= KEYC_SECONDCLICK2_STATUS_RIGHT
;
1375 if (where
== STATUS_DEFAULT
)
1376 key
= KEYC_SECONDCLICK2_STATUS_DEFAULT
;
1377 if (where
== BORDER
)
1378 key
= KEYC_SECONDCLICK2_BORDER
;
1380 case MOUSE_BUTTON_3
:
1382 key
= KEYC_SECONDCLICK3_PANE
;
1383 if (where
== STATUS
)
1384 key
= KEYC_SECONDCLICK3_STATUS
;
1385 if (where
== STATUS_LEFT
)
1386 key
= KEYC_SECONDCLICK3_STATUS_LEFT
;
1387 if (where
== STATUS_RIGHT
)
1388 key
= KEYC_SECONDCLICK3_STATUS_RIGHT
;
1389 if (where
== STATUS_DEFAULT
)
1390 key
= KEYC_SECONDCLICK3_STATUS_DEFAULT
;
1391 if (where
== BORDER
)
1392 key
= KEYC_SECONDCLICK3_BORDER
;
1394 case MOUSE_BUTTON_6
:
1396 key
= KEYC_SECONDCLICK6_PANE
;
1397 if (where
== STATUS
)
1398 key
= KEYC_SECONDCLICK6_STATUS
;
1399 if (where
== STATUS_LEFT
)
1400 key
= KEYC_SECONDCLICK6_STATUS_LEFT
;
1401 if (where
== STATUS_RIGHT
)
1402 key
= KEYC_SECONDCLICK6_STATUS_RIGHT
;
1403 if (where
== STATUS_DEFAULT
)
1404 key
= KEYC_SECONDCLICK6_STATUS_DEFAULT
;
1405 if (where
== BORDER
)
1406 key
= KEYC_SECONDCLICK6_BORDER
;
1408 case MOUSE_BUTTON_7
:
1410 key
= KEYC_SECONDCLICK7_PANE
;
1411 if (where
== STATUS
)
1412 key
= KEYC_SECONDCLICK7_STATUS
;
1413 if (where
== STATUS_LEFT
)
1414 key
= KEYC_SECONDCLICK7_STATUS_LEFT
;
1415 if (where
== STATUS_RIGHT
)
1416 key
= KEYC_SECONDCLICK7_STATUS_RIGHT
;
1417 if (where
== STATUS_DEFAULT
)
1418 key
= KEYC_SECONDCLICK7_STATUS_DEFAULT
;
1419 if (where
== BORDER
)
1420 key
= KEYC_SECONDCLICK7_BORDER
;
1422 case MOUSE_BUTTON_8
:
1424 key
= KEYC_SECONDCLICK8_PANE
;
1425 if (where
== STATUS
)
1426 key
= KEYC_SECONDCLICK8_STATUS
;
1427 if (where
== STATUS_LEFT
)
1428 key
= KEYC_SECONDCLICK8_STATUS_LEFT
;
1429 if (where
== STATUS_RIGHT
)
1430 key
= KEYC_SECONDCLICK8_STATUS_RIGHT
;
1431 if (where
== STATUS_DEFAULT
)
1432 key
= KEYC_SECONDCLICK8_STATUS_DEFAULT
;
1433 if (where
== BORDER
)
1434 key
= KEYC_SECONDCLICK8_BORDER
;
1436 case MOUSE_BUTTON_9
:
1438 key
= KEYC_SECONDCLICK9_PANE
;
1439 if (where
== STATUS
)
1440 key
= KEYC_SECONDCLICK9_STATUS
;
1441 if (where
== STATUS_LEFT
)
1442 key
= KEYC_SECONDCLICK9_STATUS_LEFT
;
1443 if (where
== STATUS_RIGHT
)
1444 key
= KEYC_SECONDCLICK9_STATUS_RIGHT
;
1445 if (where
== STATUS_DEFAULT
)
1446 key
= KEYC_SECONDCLICK9_STATUS_DEFAULT
;
1447 if (where
== BORDER
)
1448 key
= KEYC_SECONDCLICK9_BORDER
;
1450 case MOUSE_BUTTON_10
:
1452 key
= KEYC_SECONDCLICK10_PANE
;
1453 if (where
== STATUS
)
1454 key
= KEYC_SECONDCLICK10_STATUS
;
1455 if (where
== STATUS_LEFT
)
1456 key
= KEYC_SECONDCLICK10_STATUS_LEFT
;
1457 if (where
== STATUS_RIGHT
)
1458 key
= KEYC_SECONDCLICK10_STATUS_RIGHT
;
1459 if (where
== STATUS_DEFAULT
)
1460 key
= KEYC_SECONDCLICK10_STATUS_DEFAULT
;
1461 if (where
== BORDER
)
1462 key
= KEYC_SECONDCLICK10_BORDER
;
1464 case MOUSE_BUTTON_11
:
1466 key
= KEYC_SECONDCLICK11_PANE
;
1467 if (where
== STATUS
)
1468 key
= KEYC_SECONDCLICK11_STATUS
;
1469 if (where
== STATUS_LEFT
)
1470 key
= KEYC_SECONDCLICK11_STATUS_LEFT
;
1471 if (where
== STATUS_RIGHT
)
1472 key
= KEYC_SECONDCLICK11_STATUS_RIGHT
;
1473 if (where
== STATUS_DEFAULT
)
1474 key
= KEYC_SECONDCLICK11_STATUS_DEFAULT
;
1475 if (where
== BORDER
)
1476 key
= KEYC_SECONDCLICK11_BORDER
;
1481 switch (MOUSE_BUTTONS(b
)) {
1482 case MOUSE_BUTTON_1
:
1484 key
= KEYC_DOUBLECLICK1_PANE
;
1485 if (where
== STATUS
)
1486 key
= KEYC_DOUBLECLICK1_STATUS
;
1487 if (where
== STATUS_LEFT
)
1488 key
= KEYC_DOUBLECLICK1_STATUS_LEFT
;
1489 if (where
== STATUS_RIGHT
)
1490 key
= KEYC_DOUBLECLICK1_STATUS_RIGHT
;
1491 if (where
== STATUS_DEFAULT
)
1492 key
= KEYC_DOUBLECLICK1_STATUS_DEFAULT
;
1493 if (where
== BORDER
)
1494 key
= KEYC_DOUBLECLICK1_BORDER
;
1496 case MOUSE_BUTTON_2
:
1498 key
= KEYC_DOUBLECLICK2_PANE
;
1499 if (where
== STATUS
)
1500 key
= KEYC_DOUBLECLICK2_STATUS
;
1501 if (where
== STATUS_LEFT
)
1502 key
= KEYC_DOUBLECLICK2_STATUS_LEFT
;
1503 if (where
== STATUS_RIGHT
)
1504 key
= KEYC_DOUBLECLICK2_STATUS_RIGHT
;
1505 if (where
== STATUS_DEFAULT
)
1506 key
= KEYC_DOUBLECLICK2_STATUS_DEFAULT
;
1507 if (where
== BORDER
)
1508 key
= KEYC_DOUBLECLICK2_BORDER
;
1510 case MOUSE_BUTTON_3
:
1512 key
= KEYC_DOUBLECLICK3_PANE
;
1513 if (where
== STATUS
)
1514 key
= KEYC_DOUBLECLICK3_STATUS
;
1515 if (where
== STATUS_LEFT
)
1516 key
= KEYC_DOUBLECLICK3_STATUS_LEFT
;
1517 if (where
== STATUS_RIGHT
)
1518 key
= KEYC_DOUBLECLICK3_STATUS_RIGHT
;
1519 if (where
== STATUS_DEFAULT
)
1520 key
= KEYC_DOUBLECLICK3_STATUS_DEFAULT
;
1521 if (where
== BORDER
)
1522 key
= KEYC_DOUBLECLICK3_BORDER
;
1524 case MOUSE_BUTTON_6
:
1526 key
= KEYC_DOUBLECLICK6_PANE
;
1527 if (where
== STATUS
)
1528 key
= KEYC_DOUBLECLICK6_STATUS
;
1529 if (where
== STATUS_LEFT
)
1530 key
= KEYC_DOUBLECLICK6_STATUS_LEFT
;
1531 if (where
== STATUS_RIGHT
)
1532 key
= KEYC_DOUBLECLICK6_STATUS_RIGHT
;
1533 if (where
== STATUS_DEFAULT
)
1534 key
= KEYC_DOUBLECLICK6_STATUS_DEFAULT
;
1535 if (where
== BORDER
)
1536 key
= KEYC_DOUBLECLICK6_BORDER
;
1538 case MOUSE_BUTTON_7
:
1540 key
= KEYC_DOUBLECLICK7_PANE
;
1541 if (where
== STATUS
)
1542 key
= KEYC_DOUBLECLICK7_STATUS
;
1543 if (where
== STATUS_LEFT
)
1544 key
= KEYC_DOUBLECLICK7_STATUS_LEFT
;
1545 if (where
== STATUS_RIGHT
)
1546 key
= KEYC_DOUBLECLICK7_STATUS_RIGHT
;
1547 if (where
== STATUS_DEFAULT
)
1548 key
= KEYC_DOUBLECLICK7_STATUS_DEFAULT
;
1549 if (where
== BORDER
)
1550 key
= KEYC_DOUBLECLICK7_BORDER
;
1552 case MOUSE_BUTTON_8
:
1554 key
= KEYC_DOUBLECLICK8_PANE
;
1555 if (where
== STATUS
)
1556 key
= KEYC_DOUBLECLICK8_STATUS
;
1557 if (where
== STATUS_LEFT
)
1558 key
= KEYC_DOUBLECLICK8_STATUS_LEFT
;
1559 if (where
== STATUS_RIGHT
)
1560 key
= KEYC_DOUBLECLICK8_STATUS_RIGHT
;
1561 if (where
== STATUS_DEFAULT
)
1562 key
= KEYC_DOUBLECLICK8_STATUS_DEFAULT
;
1563 if (where
== BORDER
)
1564 key
= KEYC_DOUBLECLICK8_BORDER
;
1566 case MOUSE_BUTTON_9
:
1568 key
= KEYC_DOUBLECLICK9_PANE
;
1569 if (where
== STATUS
)
1570 key
= KEYC_DOUBLECLICK9_STATUS
;
1571 if (where
== STATUS_LEFT
)
1572 key
= KEYC_DOUBLECLICK9_STATUS_LEFT
;
1573 if (where
== STATUS_RIGHT
)
1574 key
= KEYC_DOUBLECLICK9_STATUS_RIGHT
;
1575 if (where
== STATUS_DEFAULT
)
1576 key
= KEYC_DOUBLECLICK9_STATUS_DEFAULT
;
1577 if (where
== BORDER
)
1578 key
= KEYC_DOUBLECLICK9_BORDER
;
1580 case MOUSE_BUTTON_10
:
1582 key
= KEYC_DOUBLECLICK10_PANE
;
1583 if (where
== STATUS
)
1584 key
= KEYC_DOUBLECLICK10_STATUS
;
1585 if (where
== STATUS_LEFT
)
1586 key
= KEYC_DOUBLECLICK10_STATUS_LEFT
;
1587 if (where
== STATUS_RIGHT
)
1588 key
= KEYC_DOUBLECLICK10_STATUS_RIGHT
;
1589 if (where
== STATUS_DEFAULT
)
1590 key
= KEYC_DOUBLECLICK10_STATUS_DEFAULT
;
1591 if (where
== BORDER
)
1592 key
= KEYC_DOUBLECLICK10_BORDER
;
1594 case MOUSE_BUTTON_11
:
1596 key
= KEYC_DOUBLECLICK11_PANE
;
1597 if (where
== STATUS
)
1598 key
= KEYC_DOUBLECLICK11_STATUS
;
1599 if (where
== STATUS_LEFT
)
1600 key
= KEYC_DOUBLECLICK11_STATUS_LEFT
;
1601 if (where
== STATUS_RIGHT
)
1602 key
= KEYC_DOUBLECLICK11_STATUS_RIGHT
;
1603 if (where
== STATUS_DEFAULT
)
1604 key
= KEYC_DOUBLECLICK11_STATUS_DEFAULT
;
1605 if (where
== BORDER
)
1606 key
= KEYC_DOUBLECLICK11_BORDER
;
1611 switch (MOUSE_BUTTONS(b
)) {
1612 case MOUSE_BUTTON_1
:
1614 key
= KEYC_TRIPLECLICK1_PANE
;
1615 if (where
== STATUS
)
1616 key
= KEYC_TRIPLECLICK1_STATUS
;
1617 if (where
== STATUS_LEFT
)
1618 key
= KEYC_TRIPLECLICK1_STATUS_LEFT
;
1619 if (where
== STATUS_RIGHT
)
1620 key
= KEYC_TRIPLECLICK1_STATUS_RIGHT
;
1621 if (where
== STATUS_DEFAULT
)
1622 key
= KEYC_TRIPLECLICK1_STATUS_DEFAULT
;
1623 if (where
== BORDER
)
1624 key
= KEYC_TRIPLECLICK1_BORDER
;
1626 case MOUSE_BUTTON_2
:
1628 key
= KEYC_TRIPLECLICK2_PANE
;
1629 if (where
== STATUS
)
1630 key
= KEYC_TRIPLECLICK2_STATUS
;
1631 if (where
== STATUS_LEFT
)
1632 key
= KEYC_TRIPLECLICK2_STATUS_LEFT
;
1633 if (where
== STATUS_RIGHT
)
1634 key
= KEYC_TRIPLECLICK2_STATUS_RIGHT
;
1635 if (where
== STATUS_DEFAULT
)
1636 key
= KEYC_TRIPLECLICK2_STATUS_DEFAULT
;
1637 if (where
== BORDER
)
1638 key
= KEYC_TRIPLECLICK2_BORDER
;
1640 case MOUSE_BUTTON_3
:
1642 key
= KEYC_TRIPLECLICK3_PANE
;
1643 if (where
== STATUS
)
1644 key
= KEYC_TRIPLECLICK3_STATUS
;
1645 if (where
== STATUS_LEFT
)
1646 key
= KEYC_TRIPLECLICK3_STATUS_LEFT
;
1647 if (where
== STATUS_RIGHT
)
1648 key
= KEYC_TRIPLECLICK3_STATUS_RIGHT
;
1649 if (where
== STATUS_DEFAULT
)
1650 key
= KEYC_TRIPLECLICK3_STATUS_DEFAULT
;
1651 if (where
== BORDER
)
1652 key
= KEYC_TRIPLECLICK3_BORDER
;
1654 case MOUSE_BUTTON_6
:
1656 key
= KEYC_TRIPLECLICK6_PANE
;
1657 if (where
== STATUS
)
1658 key
= KEYC_TRIPLECLICK6_STATUS
;
1659 if (where
== STATUS_LEFT
)
1660 key
= KEYC_TRIPLECLICK6_STATUS_LEFT
;
1661 if (where
== STATUS_RIGHT
)
1662 key
= KEYC_TRIPLECLICK6_STATUS_RIGHT
;
1663 if (where
== STATUS_DEFAULT
)
1664 key
= KEYC_TRIPLECLICK6_STATUS_DEFAULT
;
1665 if (where
== BORDER
)
1666 key
= KEYC_TRIPLECLICK6_BORDER
;
1668 case MOUSE_BUTTON_7
:
1670 key
= KEYC_TRIPLECLICK7_PANE
;
1671 if (where
== STATUS
)
1672 key
= KEYC_TRIPLECLICK7_STATUS
;
1673 if (where
== STATUS_LEFT
)
1674 key
= KEYC_TRIPLECLICK7_STATUS_LEFT
;
1675 if (where
== STATUS_RIGHT
)
1676 key
= KEYC_TRIPLECLICK7_STATUS_RIGHT
;
1677 if (where
== STATUS_DEFAULT
)
1678 key
= KEYC_TRIPLECLICK7_STATUS_DEFAULT
;
1679 if (where
== BORDER
)
1680 key
= KEYC_TRIPLECLICK7_BORDER
;
1682 case MOUSE_BUTTON_8
:
1684 key
= KEYC_TRIPLECLICK8_PANE
;
1685 if (where
== STATUS
)
1686 key
= KEYC_TRIPLECLICK8_STATUS
;
1687 if (where
== STATUS_LEFT
)
1688 key
= KEYC_TRIPLECLICK8_STATUS_LEFT
;
1689 if (where
== STATUS_RIGHT
)
1690 key
= KEYC_TRIPLECLICK8_STATUS_RIGHT
;
1691 if (where
== STATUS_DEFAULT
)
1692 key
= KEYC_TRIPLECLICK8_STATUS_DEFAULT
;
1693 if (where
== BORDER
)
1694 key
= KEYC_TRIPLECLICK8_BORDER
;
1696 case MOUSE_BUTTON_9
:
1698 key
= KEYC_TRIPLECLICK9_PANE
;
1699 if (where
== STATUS
)
1700 key
= KEYC_TRIPLECLICK9_STATUS
;
1701 if (where
== STATUS_LEFT
)
1702 key
= KEYC_TRIPLECLICK9_STATUS_LEFT
;
1703 if (where
== STATUS_RIGHT
)
1704 key
= KEYC_TRIPLECLICK9_STATUS_RIGHT
;
1705 if (where
== STATUS_DEFAULT
)
1706 key
= KEYC_TRIPLECLICK9_STATUS_DEFAULT
;
1707 if (where
== BORDER
)
1708 key
= KEYC_TRIPLECLICK9_BORDER
;
1710 case MOUSE_BUTTON_10
:
1712 key
= KEYC_TRIPLECLICK10_PANE
;
1713 if (where
== STATUS
)
1714 key
= KEYC_TRIPLECLICK10_STATUS
;
1715 if (where
== STATUS_LEFT
)
1716 key
= KEYC_TRIPLECLICK10_STATUS_LEFT
;
1717 if (where
== STATUS_RIGHT
)
1718 key
= KEYC_TRIPLECLICK10_STATUS_RIGHT
;
1719 if (where
== STATUS_DEFAULT
)
1720 key
= KEYC_TRIPLECLICK10_STATUS_DEFAULT
;
1721 if (where
== BORDER
)
1722 key
= KEYC_TRIPLECLICK10_BORDER
;
1724 case MOUSE_BUTTON_11
:
1726 key
= KEYC_TRIPLECLICK11_PANE
;
1727 if (where
== STATUS
)
1728 key
= KEYC_TRIPLECLICK11_STATUS
;
1729 if (where
== STATUS_LEFT
)
1730 key
= KEYC_TRIPLECLICK11_STATUS_LEFT
;
1731 if (where
== STATUS_RIGHT
)
1732 key
= KEYC_TRIPLECLICK11_STATUS_RIGHT
;
1733 if (where
== STATUS_DEFAULT
)
1734 key
= KEYC_TRIPLECLICK11_STATUS_DEFAULT
;
1735 if (where
== BORDER
)
1736 key
= KEYC_TRIPLECLICK11_BORDER
;
1741 if (key
== KEYC_UNKNOWN
)
1742 return (KEYC_UNKNOWN
);
1745 /* Apply modifiers if any. */
1746 if (b
& MOUSE_MASK_META
)
1748 if (b
& MOUSE_MASK_CTRL
)
1750 if (b
& MOUSE_MASK_SHIFT
)
1753 if (log_get_level() != 0)
1754 log_debug("mouse key is %s", key_string_lookup_key (key
, 1));
1758 /* Is this fast enough to probably be a paste? */
1760 server_client_assume_paste(struct session
*s
)
1765 if ((t
= options_get_number(s
->options
, "assume-paste-time")) == 0)
1768 timersub(&s
->activity_time
, &s
->last_activity_time
, &tv
);
1769 if (tv
.tv_sec
== 0 && tv
.tv_usec
< t
* 1000) {
1770 log_debug("session %s pasting (flag %d)", s
->name
,
1771 !!(s
->flags
& SESSION_PASTING
));
1772 if (s
->flags
& SESSION_PASTING
)
1774 s
->flags
|= SESSION_PASTING
;
1777 log_debug("session %s not pasting", s
->name
);
1778 s
->flags
&= ~SESSION_PASTING
;
1782 /* Has the latest client changed? */
1784 server_client_update_latest(struct client
*c
)
1788 if (c
->session
== NULL
)
1790 w
= c
->session
->curw
->window
;
1796 if (options_get_number(w
->options
, "window-size") == WINDOW_SIZE_LATEST
)
1797 recalculate_size(w
, 0);
1799 notify_client("client-active", c
);
1803 * Handle data key input from client. This owns and can modify the key event it
1804 * is given and is responsible for freeing it.
1806 static enum cmd_retval
1807 server_client_key_callback(struct cmdq_item
*item
, void *data
)
1809 struct client
*c
= cmdq_get_client(item
);
1810 struct key_event
*event
= data
;
1811 key_code key
= event
->key
;
1812 struct mouse_event
*m
= &event
->m
;
1813 struct session
*s
= c
->session
;
1815 struct window_pane
*wp
;
1816 struct window_mode_entry
*wme
;
1818 struct key_table
*table
, *first
;
1819 struct key_binding
*bd
;
1820 int xtimeout
, flags
;
1821 struct cmd_find_state fs
;
1824 /* Check the client is good to accept input. */
1825 if (s
== NULL
|| (c
->flags
& CLIENT_UNATTACHEDFLAGS
))
1829 /* Update the activity timer. */
1830 if (gettimeofday(&c
->activity_time
, NULL
) != 0)
1831 fatal("gettimeofday failed");
1832 session_update_activity(s
, &c
->activity_time
);
1834 /* Check for mouse keys. */
1836 if (key
== KEYC_MOUSE
|| key
== KEYC_DOUBLECLICK
) {
1837 if (c
->flags
& CLIENT_READONLY
)
1839 key
= server_client_check_mouse(c
, event
);
1840 if (key
== KEYC_UNKNOWN
)
1847 * Mouse drag is in progress, so fire the callback (now that
1848 * the mouse event is valid).
1850 if ((key
& KEYC_MASK_KEY
) == KEYC_DRAGGING
) {
1851 c
->tty
.mouse_drag_update(c
, m
);
1857 /* Find affected pane. */
1858 if (!KEYC_IS_MOUSE(key
) || cmd_find_from_mouse(&fs
, m
, 0) != 0)
1859 cmd_find_from_client(&fs
, c
, 0);
1862 /* Forward mouse keys if disabled. */
1863 if (KEYC_IS_MOUSE(key
) && !options_get_number(s
->options
, "mouse"))
1866 /* Treat everything as a regular key when pasting is detected. */
1867 if (!KEYC_IS_MOUSE(key
) && server_client_assume_paste(s
))
1871 * Work out the current key table. If the pane is in a mode, use
1872 * the mode table instead of the default key table.
1874 if (server_client_is_default_key_table(c
, c
->keytable
) &&
1876 (wme
= TAILQ_FIRST(&wp
->modes
)) != NULL
&&
1877 wme
->mode
->key_table
!= NULL
)
1878 table
= key_bindings_get_table(wme
->mode
->key_table(wme
), 1);
1880 table
= c
->keytable
;
1885 * The prefix always takes precedence and forces a switch to the prefix
1886 * table, unless we are already there.
1888 key0
= (key
& (KEYC_MASK_KEY
|KEYC_MASK_MODIFIERS
));
1889 if ((key0
== (key_code
)options_get_number(s
->options
, "prefix") ||
1890 key0
== (key_code
)options_get_number(s
->options
, "prefix2")) &&
1891 strcmp(table
->name
, "prefix") != 0) {
1892 server_client_set_key_table(c
, "prefix");
1893 server_status_client(c
);
1899 /* Log key table. */
1901 log_debug("key table %s (no pane)", table
->name
);
1903 log_debug("key table %s (pane %%%u)", table
->name
, wp
->id
);
1904 if (c
->flags
& CLIENT_REPEAT
)
1905 log_debug("currently repeating");
1907 /* Try to see if there is a key binding in the current table. */
1908 bd
= key_bindings_get(table
, key0
);
1911 * Key was matched in this table. If currently repeating but a
1912 * non-repeating binding was found, stop repeating and try
1913 * again in the root table.
1915 if ((c
->flags
& CLIENT_REPEAT
) &&
1916 (~bd
->flags
& KEY_BINDING_REPEAT
)) {
1917 log_debug("found in key table %s (not repeating)",
1919 server_client_set_key_table(c
, NULL
);
1920 first
= table
= c
->keytable
;
1921 c
->flags
&= ~CLIENT_REPEAT
;
1922 server_status_client(c
);
1925 log_debug("found in key table %s", table
->name
);
1928 * Take a reference to this table to make sure the key binding
1929 * doesn't disappear.
1931 table
->references
++;
1934 * If this is a repeating key, start the timer. Otherwise reset
1935 * the client back to the root table.
1937 xtimeout
= options_get_number(s
->options
, "repeat-time");
1938 if (xtimeout
!= 0 && (bd
->flags
& KEY_BINDING_REPEAT
)) {
1939 c
->flags
|= CLIENT_REPEAT
;
1941 tv
.tv_sec
= xtimeout
/ 1000;
1942 tv
.tv_usec
= (xtimeout
% 1000) * 1000L;
1943 evtimer_del(&c
->repeat_timer
);
1944 evtimer_add(&c
->repeat_timer
, &tv
);
1946 c
->flags
&= ~CLIENT_REPEAT
;
1947 server_client_set_key_table(c
, NULL
);
1949 server_status_client(c
);
1951 /* Execute the key binding. */
1952 key_bindings_dispatch(bd
, item
, c
, event
, &fs
);
1953 key_bindings_unref_table(table
);
1958 * No match, try the ANY key.
1960 if (key0
!= KEYC_ANY
) {
1966 * No match in this table. If not in the root table or if repeating,
1967 * switch the client back to the root table and try again.
1969 log_debug("not found in key table %s", table
->name
);
1970 if (!server_client_is_default_key_table(c
, table
) ||
1971 (c
->flags
& CLIENT_REPEAT
)) {
1972 log_debug("trying in root table");
1973 server_client_set_key_table(c
, NULL
);
1974 table
= c
->keytable
;
1975 if (c
->flags
& CLIENT_REPEAT
)
1977 c
->flags
&= ~CLIENT_REPEAT
;
1978 server_status_client(c
);
1983 * No match in the root table either. If this wasn't the first table
1984 * tried, don't pass the key to the pane.
1986 if (first
!= table
&& (~flags
& CLIENT_REPEAT
)) {
1987 server_client_set_key_table(c
, NULL
);
1988 server_status_client(c
);
1993 if (c
->flags
& CLIENT_READONLY
)
1996 window_pane_key(wp
, c
, s
, wl
, key
, m
);
1999 if (s
!= NULL
&& key
!= KEYC_FOCUS_OUT
)
2000 server_client_update_latest(c
);
2002 return (CMD_RETURN_NORMAL
);
2005 /* Handle a key event. */
2007 server_client_handle_key(struct client
*c
, struct key_event
*event
)
2009 struct session
*s
= c
->session
;
2010 struct cmdq_item
*item
;
2012 /* Check the client is good to accept input. */
2013 if (s
== NULL
|| (c
->flags
& CLIENT_UNATTACHEDFLAGS
))
2017 * Key presses in overlay mode and the command prompt are a special
2018 * case. The queue might be blocked so they need to be processed
2019 * immediately rather than queued.
2021 if (~c
->flags
& CLIENT_READONLY
) {
2022 if (c
->message_string
!= NULL
) {
2023 if (c
->message_ignore_keys
)
2025 status_message_clear(c
);
2027 if (c
->overlay_key
!= NULL
) {
2028 switch (c
->overlay_key(c
, c
->overlay_data
, event
)) {
2032 server_client_clear_overlay(c
);
2036 server_client_clear_overlay(c
);
2037 if (c
->prompt_string
!= NULL
) {
2038 if (status_prompt_key(c
, event
->key
) == 0)
2044 * Add the key to the queue so it happens after any commands queued by
2047 item
= cmdq_get_callback(server_client_key_callback
, event
);
2048 cmdq_append(c
, item
);
2052 /* Client functions that need to happen every loop. */
2054 server_client_loop(void)
2058 struct window_pane
*wp
;
2060 /* Check for window resize. This is done before redrawing. */
2061 RB_FOREACH(w
, windows
, &windows
)
2062 server_client_check_window_resize(w
);
2064 /* Check clients. */
2065 TAILQ_FOREACH(c
, &clients
, entry
) {
2066 server_client_check_exit(c
);
2067 if (c
->session
!= NULL
) {
2068 server_client_check_modes(c
);
2069 server_client_check_redraw(c
);
2070 server_client_reset_state(c
);
2075 * Any windows will have been redrawn as part of clients, so clear
2078 RB_FOREACH(w
, windows
, &windows
) {
2079 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
2081 server_client_check_pane_resize(wp
);
2082 server_client_check_pane_buffer(wp
);
2084 wp
->flags
&= ~PANE_REDRAW
;
2086 check_window_name(w
);
2090 /* Check if window needs to be resized. */
2092 server_client_check_window_resize(struct window
*w
)
2096 if (~w
->flags
& WINDOW_RESIZE
)
2099 TAILQ_FOREACH(wl
, &w
->winlinks
, wentry
) {
2100 if (wl
->session
->attached
!= 0 && wl
->session
->curw
== wl
)
2106 log_debug("%s: resizing window @%u", __func__
, w
->id
);
2107 resize_window(w
, w
->new_sx
, w
->new_sy
, w
->new_xpixel
, w
->new_ypixel
);
2110 /* Resize timer event. */
2112 server_client_resize_timer(__unused
int fd
, __unused
short events
, void *data
)
2114 struct window_pane
*wp
= data
;
2116 log_debug("%s: %%%u resize timer expired", __func__
, wp
->id
);
2117 evtimer_del(&wp
->resize_timer
);
2120 /* Check if pane should be resized. */
2122 server_client_check_pane_resize(struct window_pane
*wp
)
2124 struct window_pane_resize
*r
;
2125 struct window_pane_resize
*r1
;
2126 struct window_pane_resize
*first
;
2127 struct window_pane_resize
*last
;
2128 struct timeval tv
= { .tv_usec
= 250000 };
2130 if (TAILQ_EMPTY(&wp
->resize_queue
))
2133 if (!event_initialized(&wp
->resize_timer
))
2134 evtimer_set(&wp
->resize_timer
, server_client_resize_timer
, wp
);
2135 if (evtimer_pending(&wp
->resize_timer
, NULL
))
2138 log_debug("%s: %%%u needs to be resized", __func__
, wp
->id
);
2139 TAILQ_FOREACH(r
, &wp
->resize_queue
, entry
) {
2140 log_debug("queued resize: %ux%u -> %ux%u", r
->osx
, r
->osy
,
2145 * There are three cases that matter:
2147 * - Only one resize. It can just be applied.
2149 * - Multiple resizes and the ending size is different from the
2150 * starting size. We can discard all resizes except the most recent.
2152 * - Multiple resizes and the ending size is the same as the starting
2153 * size. We must resize at least twice to force the application to
2154 * redraw. So apply the first and leave the last on the queue for
2157 first
= TAILQ_FIRST(&wp
->resize_queue
);
2158 last
= TAILQ_LAST(&wp
->resize_queue
, window_pane_resizes
);
2159 if (first
== last
) {
2160 /* Only one resize. */
2161 window_pane_send_resize(wp
, first
->sx
, first
->sy
);
2162 TAILQ_REMOVE(&wp
->resize_queue
, first
, entry
);
2164 } else if (last
->sx
!= first
->osx
|| last
->sy
!= first
->osy
) {
2165 /* Multiple resizes ending up with a different size. */
2166 window_pane_send_resize(wp
, last
->sx
, last
->sy
);
2167 TAILQ_FOREACH_SAFE(r
, &wp
->resize_queue
, entry
, r1
) {
2168 TAILQ_REMOVE(&wp
->resize_queue
, r
, entry
);
2173 * Multiple resizes ending up with the same size. There will
2174 * not be more than one to the same size in succession so we
2175 * can just use the last-but-one on the list and leave the last
2176 * for later. We reduce the time until the next check to avoid
2177 * a long delay between the resizes.
2179 r
= TAILQ_PREV(last
, window_pane_resizes
, entry
);
2180 window_pane_send_resize(wp
, r
->sx
, r
->sy
);
2181 TAILQ_FOREACH_SAFE(r
, &wp
->resize_queue
, entry
, r1
) {
2184 TAILQ_REMOVE(&wp
->resize_queue
, r
, entry
);
2189 evtimer_add(&wp
->resize_timer
, &tv
);
2192 /* Check pane buffer size. */
2194 server_client_check_pane_buffer(struct window_pane
*wp
)
2196 struct evbuffer
*evb
= wp
->event
->input
;
2199 struct window_pane_offset
*wpo
;
2201 u_int attached_clients
= 0;
2205 * Work out the minimum used size. This is the most that can be removed
2208 minimum
= wp
->offset
.used
;
2209 if (wp
->pipe_fd
!= -1 && wp
->pipe_offset
.used
< minimum
)
2210 minimum
= wp
->pipe_offset
.used
;
2211 TAILQ_FOREACH(c
, &clients
, entry
) {
2212 if (c
->session
== NULL
)
2216 if (~c
->flags
& CLIENT_CONTROL
) {
2220 wpo
= control_pane_offset(c
, wp
, &flag
);
2228 window_pane_get_new_data(wp
, wpo
, &new_size
);
2229 log_debug("%s: %s has %zu bytes used and %zu left for %%%u",
2230 __func__
, c
->name
, wpo
->used
- wp
->base_offset
, new_size
,
2232 if (wpo
->used
< minimum
)
2233 minimum
= wpo
->used
;
2235 if (attached_clients
== 0)
2237 minimum
-= wp
->base_offset
;
2241 /* Drain the buffer. */
2242 log_debug("%s: %%%u has %zu minimum (of %zu) bytes used", __func__
,
2243 wp
->id
, minimum
, EVBUFFER_LENGTH(evb
));
2244 evbuffer_drain(evb
, minimum
);
2247 * Adjust the base offset. If it would roll over, all the offsets into
2248 * the buffer need to be adjusted.
2250 if (wp
->base_offset
> SIZE_MAX
- minimum
) {
2251 log_debug("%s: %%%u base offset has wrapped", __func__
, wp
->id
);
2252 wp
->offset
.used
-= wp
->base_offset
;
2253 if (wp
->pipe_fd
!= -1)
2254 wp
->pipe_offset
.used
-= wp
->base_offset
;
2255 TAILQ_FOREACH(c
, &clients
, entry
) {
2256 if (c
->session
== NULL
|| (~c
->flags
& CLIENT_CONTROL
))
2258 wpo
= control_pane_offset(c
, wp
, &flag
);
2259 if (wpo
!= NULL
&& !flag
)
2260 wpo
->used
-= wp
->base_offset
;
2262 wp
->base_offset
= minimum
;
2264 wp
->base_offset
+= minimum
;
2268 * If there is data remaining, and there are no clients able to consume
2269 * it, do not read any more. This is true when there are attached
2270 * clients, all of which are control clients which are not able to
2271 * accept any more data.
2273 log_debug("%s: pane %%%u is %s", __func__
, wp
->id
, off
? "off" : "on");
2275 bufferevent_disable(wp
->event
, EV_READ
);
2277 bufferevent_enable(wp
->event
, EV_READ
);
2281 * Update cursor position and mode settings. The scroll region and attributes
2282 * are cleared when idle (waiting for an event) as this is the most likely time
2283 * a user may interrupt tmux, for example with ~^Z in ssh(1). This is a
2284 * compromise between excessive resets and likelihood of an interrupt.
2286 * tty_region/tty_reset/tty_update_mode already take care of not resetting
2287 * things that are already in their default state.
2290 server_client_reset_state(struct client
*c
)
2292 struct tty
*tty
= &c
->tty
;
2293 struct window
*w
= c
->session
->curw
->window
;
2294 struct window_pane
*wp
= server_client_get_pane(c
), *loop
;
2295 struct screen
*s
= NULL
;
2296 struct options
*oo
= c
->session
->options
;
2297 int mode
= 0, cursor
, flags
, n
;
2298 u_int cx
= 0, cy
= 0, ox
, oy
, sx
, sy
;
2300 if (c
->flags
& (CLIENT_CONTROL
|CLIENT_SUSPENDED
))
2303 /* Disable the block flag. */
2304 flags
= (tty
->flags
& TTY_BLOCK
);
2305 tty
->flags
&= ~TTY_BLOCK
;
2307 /* Get mode from overlay if any, else from screen. */
2308 if (c
->overlay_draw
!= NULL
) {
2309 if (c
->overlay_mode
!= NULL
)
2310 s
= c
->overlay_mode(c
, c
->overlay_data
, &cx
, &cy
);
2315 if (log_get_level() != 0) {
2316 log_debug("%s: client %s mode %s", __func__
, c
->name
,
2317 screen_mode_to_string(mode
));
2320 /* Reset region and margin. */
2321 tty_region_off(tty
);
2322 tty_margin_off(tty
);
2324 /* Move cursor to pane cursor and offset. */
2325 if (c
->prompt_string
!= NULL
) {
2326 n
= options_get_number(c
->session
->options
, "status-position");
2330 n
= status_line_size(c
);
2336 cx
= c
->prompt_cursor
;
2337 mode
&= ~MODE_CURSOR
;
2338 } else if (c
->overlay_draw
== NULL
) {
2340 tty_window_offset(tty
, &ox
, &oy
, &sx
, &sy
);
2341 if (wp
->xoff
+ s
->cx
>= ox
&& wp
->xoff
+ s
->cx
<= ox
+ sx
&&
2342 wp
->yoff
+ s
->cy
>= oy
&& wp
->yoff
+ s
->cy
<= oy
+ sy
) {
2345 cx
= wp
->xoff
+ s
->cx
- ox
;
2346 cy
= wp
->yoff
+ s
->cy
- oy
;
2348 if (status_at_line(c
) == 0)
2349 cy
+= status_line_size(c
);
2352 mode
&= ~MODE_CURSOR
;
2354 log_debug("%s: cursor to %u,%u", __func__
, cx
, cy
);
2355 tty_cursor(tty
, cx
, cy
);
2358 * Set mouse mode if requested. To support dragging, always use button
2361 if (options_get_number(oo
, "mouse")) {
2362 if (c
->overlay_draw
== NULL
) {
2363 mode
&= ~ALL_MOUSE_MODES
;
2364 TAILQ_FOREACH(loop
, &w
->panes
, entry
) {
2365 if (loop
->screen
->mode
& MODE_MOUSE_ALL
)
2366 mode
|= MODE_MOUSE_ALL
;
2369 if (~mode
& MODE_MOUSE_ALL
)
2370 mode
|= MODE_MOUSE_BUTTON
;
2373 /* Clear bracketed paste mode if at the prompt. */
2374 if (c
->overlay_draw
== NULL
&& c
->prompt_string
!= NULL
)
2375 mode
&= ~MODE_BRACKETPASTE
;
2377 /* Set the terminal mode and reset attributes. */
2378 tty_update_mode(tty
, mode
, s
);
2381 /* All writing must be done, send a sync end (if it was started). */
2383 tty
->flags
|= flags
;
2386 /* Repeat time callback. */
2388 server_client_repeat_timer(__unused
int fd
, __unused
short events
, void *data
)
2390 struct client
*c
= data
;
2392 if (c
->flags
& CLIENT_REPEAT
) {
2393 server_client_set_key_table(c
, NULL
);
2394 c
->flags
&= ~CLIENT_REPEAT
;
2395 server_status_client(c
);
2399 /* Double-click callback. */
2401 server_client_click_timer(__unused
int fd
, __unused
short events
, void *data
)
2403 struct client
*c
= data
;
2404 struct key_event
*event
;
2406 log_debug("click timer expired");
2408 if (c
->flags
& CLIENT_TRIPLECLICK
) {
2410 * Waiting for a third click that hasn't happened, so this must
2411 * have been a double click.
2413 event
= xmalloc(sizeof *event
);
2414 event
->key
= KEYC_DOUBLECLICK
;
2415 memcpy(&event
->m
, &c
->click_event
, sizeof event
->m
);
2416 if (!server_client_handle_key(c
, event
))
2419 c
->flags
&= ~(CLIENT_DOUBLECLICK
|CLIENT_TRIPLECLICK
);
2422 /* Check if client should be exited. */
2424 server_client_check_exit(struct client
*c
)
2426 struct client_file
*cf
;
2427 const char *name
= c
->exit_session
;
2431 if (c
->flags
& (CLIENT_DEAD
|CLIENT_EXITED
))
2433 if (~c
->flags
& CLIENT_EXIT
)
2436 if (c
->flags
& CLIENT_CONTROL
) {
2438 if (!control_all_done(c
))
2441 RB_FOREACH(cf
, client_files
, &c
->files
) {
2442 if (EVBUFFER_LENGTH(cf
->buffer
) != 0)
2445 c
->flags
|= CLIENT_EXITED
;
2447 switch (c
->exit_type
) {
2448 case CLIENT_EXIT_RETURN
:
2449 if (c
->exit_message
!= NULL
)
2450 msize
= strlen(c
->exit_message
) + 1;
2453 size
= (sizeof c
->retval
) + msize
;
2454 data
= xmalloc(size
);
2455 memcpy(data
, &c
->retval
, sizeof c
->retval
);
2456 if (c
->exit_message
!= NULL
)
2457 memcpy(data
+ sizeof c
->retval
, c
->exit_message
, msize
);
2458 proc_send(c
->peer
, MSG_EXIT
, -1, data
, size
);
2461 case CLIENT_EXIT_SHUTDOWN
:
2462 proc_send(c
->peer
, MSG_SHUTDOWN
, -1, NULL
, 0);
2464 case CLIENT_EXIT_DETACH
:
2465 proc_send(c
->peer
, c
->exit_msgtype
, -1, name
, strlen(name
) + 1);
2468 free(c
->exit_session
);
2469 free(c
->exit_message
);
2472 /* Redraw timer callback. */
2474 server_client_redraw_timer(__unused
int fd
, __unused
short events
,
2475 __unused
void *data
)
2477 log_debug("redraw timer fired");
2481 * Check if modes need to be updated. Only modes in the current window are
2482 * updated and it is done when the status line is redrawn.
2485 server_client_check_modes(struct client
*c
)
2487 struct window
*w
= c
->session
->curw
->window
;
2488 struct window_pane
*wp
;
2489 struct window_mode_entry
*wme
;
2491 if (c
->flags
& (CLIENT_CONTROL
|CLIENT_SUSPENDED
))
2493 if (~c
->flags
& CLIENT_REDRAWSTATUS
)
2495 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
2496 wme
= TAILQ_FIRST(&wp
->modes
);
2497 if (wme
!= NULL
&& wme
->mode
->update
!= NULL
)
2498 wme
->mode
->update(wme
);
2502 /* Check for client redraws. */
2504 server_client_check_redraw(struct client
*c
)
2506 struct session
*s
= c
->session
;
2507 struct tty
*tty
= &c
->tty
;
2508 struct window
*w
= c
->session
->curw
->window
;
2509 struct window_pane
*wp
;
2510 int needed
, flags
, mode
= tty
->mode
, new_flags
= 0;
2513 struct timeval tv
= { .tv_usec
= 1000 };
2514 static struct event ev
;
2517 if (c
->flags
& (CLIENT_CONTROL
|CLIENT_SUSPENDED
))
2519 if (c
->flags
& CLIENT_ALLREDRAWFLAGS
) {
2520 log_debug("%s: redraw%s%s%s%s%s", c
->name
,
2521 (c
->flags
& CLIENT_REDRAWWINDOW
) ? " window" : "",
2522 (c
->flags
& CLIENT_REDRAWSTATUS
) ? " status" : "",
2523 (c
->flags
& CLIENT_REDRAWBORDERS
) ? " borders" : "",
2524 (c
->flags
& CLIENT_REDRAWOVERLAY
) ? " overlay" : "",
2525 (c
->flags
& CLIENT_REDRAWPANES
) ? " panes" : "");
2529 * If there is outstanding data, defer the redraw until it has been
2530 * consumed. We can just add a timer to get out of the event loop and
2534 if (c
->flags
& CLIENT_ALLREDRAWFLAGS
)
2537 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
2538 if (wp
->flags
& PANE_REDRAW
) {
2544 new_flags
|= CLIENT_REDRAWPANES
;
2546 if (needed
&& (left
= EVBUFFER_LENGTH(tty
->out
)) != 0) {
2547 log_debug("%s: redraw deferred (%zu left)", c
->name
, left
);
2548 if (!evtimer_initialized(&ev
))
2549 evtimer_set(&ev
, server_client_redraw_timer
, NULL
);
2550 if (!evtimer_pending(&ev
, NULL
)) {
2551 log_debug("redraw timer started");
2552 evtimer_add(&ev
, &tv
);
2555 if (~c
->flags
& CLIENT_REDRAWWINDOW
) {
2556 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
2557 if (wp
->flags
& PANE_REDRAW
) {
2558 log_debug("%s: pane %%%u needs redraw",
2560 c
->redraw_panes
|= (1 << bit
);
2564 * If more that 64 panes, give up and
2565 * just redraw the window.
2567 new_flags
&= CLIENT_REDRAWPANES
;
2568 new_flags
|= CLIENT_REDRAWWINDOW
;
2572 if (c
->redraw_panes
!= 0)
2573 c
->flags
|= CLIENT_REDRAWPANES
;
2575 c
->flags
|= new_flags
;
2578 log_debug("%s: redraw needed", c
->name
);
2580 flags
= tty
->flags
& (TTY_BLOCK
|TTY_FREEZE
|TTY_NOCURSOR
);
2581 tty
->flags
= (tty
->flags
& ~(TTY_BLOCK
|TTY_FREEZE
))|TTY_NOCURSOR
;
2583 if (~c
->flags
& CLIENT_REDRAWWINDOW
) {
2585 * If not redrawing the entire window, check whether each pane
2586 * needs to be redrawn.
2588 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
2590 if (wp
->flags
& PANE_REDRAW
)
2592 else if (c
->flags
& CLIENT_REDRAWPANES
)
2593 redraw
= !!(c
->redraw_panes
& (1 << bit
));
2597 log_debug("%s: redrawing pane %%%u", __func__
, wp
->id
);
2598 screen_redraw_pane(c
, wp
);
2600 c
->redraw_panes
= 0;
2601 c
->flags
&= ~CLIENT_REDRAWPANES
;
2604 if (c
->flags
& CLIENT_ALLREDRAWFLAGS
) {
2605 if (options_get_number(s
->options
, "set-titles"))
2606 server_client_set_title(c
);
2607 screen_redraw_screen(c
);
2610 tty
->flags
= (tty
->flags
& ~TTY_NOCURSOR
)|(flags
& TTY_NOCURSOR
);
2611 tty_update_mode(tty
, mode
, NULL
);
2612 tty
->flags
= (tty
->flags
& ~(TTY_BLOCK
|TTY_FREEZE
|TTY_NOCURSOR
))|flags
;
2614 c
->flags
&= ~(CLIENT_ALLREDRAWFLAGS
|CLIENT_STATUSFORCE
);
2618 * We would have deferred the redraw unless the output buffer
2619 * was empty, so we can record how many bytes the redraw
2622 c
->redraw
= EVBUFFER_LENGTH(tty
->out
);
2623 log_debug("%s: redraw added %zu bytes", c
->name
, c
->redraw
);
2627 /* Set client title. */
2629 server_client_set_title(struct client
*c
)
2631 struct session
*s
= c
->session
;
2632 const char *template;
2634 struct format_tree
*ft
;
2636 template = options_get_string(s
->options
, "set-titles-string");
2638 ft
= format_create(c
, NULL
, FORMAT_NONE
, 0);
2639 format_defaults(ft
, c
, NULL
, NULL
, NULL
);
2641 title
= format_expand_time(ft
, template);
2642 if (c
->title
== NULL
|| strcmp(title
, c
->title
) != 0) {
2644 c
->title
= xstrdup(title
);
2645 tty_set_title(&c
->tty
, c
->title
);
2652 /* Dispatch message from client. */
2654 server_client_dispatch(struct imsg
*imsg
, void *arg
)
2656 struct client
*c
= arg
;
2660 if (c
->flags
& CLIENT_DEAD
)
2664 server_client_lost(c
);
2668 datalen
= imsg
->hdr
.len
- IMSG_HEADER_SIZE
;
2670 switch (imsg
->hdr
.type
) {
2671 case MSG_IDENTIFY_CLIENTPID
:
2672 case MSG_IDENTIFY_CWD
:
2673 case MSG_IDENTIFY_ENVIRON
:
2674 case MSG_IDENTIFY_FEATURES
:
2675 case MSG_IDENTIFY_FLAGS
:
2676 case MSG_IDENTIFY_LONGFLAGS
:
2677 case MSG_IDENTIFY_STDIN
:
2678 case MSG_IDENTIFY_STDOUT
:
2679 case MSG_IDENTIFY_TERM
:
2680 case MSG_IDENTIFY_TERMINFO
:
2681 case MSG_IDENTIFY_TTYNAME
:
2682 case MSG_IDENTIFY_DONE
:
2683 server_client_dispatch_identify(c
, imsg
);
2686 server_client_dispatch_command(c
, imsg
);
2690 fatalx("bad MSG_RESIZE size");
2692 if (c
->flags
& CLIENT_CONTROL
)
2694 server_client_update_latest(c
);
2695 tty_resize(&c
->tty
);
2696 recalculate_sizes();
2697 if (c
->overlay_resize
== NULL
)
2698 server_client_clear_overlay(c
);
2700 c
->overlay_resize(c
, c
->overlay_data
);
2701 server_redraw_client(c
);
2702 if (c
->session
!= NULL
)
2703 notify_client("client-resized", c
);
2707 fatalx("bad MSG_EXITING size");
2708 server_client_set_session(c
, NULL
);
2709 recalculate_sizes();
2711 proc_send(c
->peer
, MSG_EXITED
, -1, NULL
, 0);
2716 fatalx("bad MSG_WAKEUP size");
2718 if (!(c
->flags
& CLIENT_SUSPENDED
))
2720 c
->flags
&= ~CLIENT_SUSPENDED
;
2722 if (c
->fd
== -1 || c
->session
== NULL
) /* exited already */
2726 if (gettimeofday(&c
->activity_time
, NULL
) != 0)
2727 fatal("gettimeofday failed");
2729 tty_start_tty(&c
->tty
);
2730 server_redraw_client(c
);
2731 recalculate_sizes();
2734 session_update_activity(s
, &c
->activity_time
);
2738 fatalx("bad MSG_SHELL size");
2740 server_client_dispatch_shell(c
);
2742 case MSG_WRITE_READY
:
2743 file_write_ready(&c
->files
, imsg
);
2746 file_read_data(&c
->files
, imsg
);
2749 file_read_done(&c
->files
, imsg
);
2754 /* Callback when command is done. */
2755 static enum cmd_retval
2756 server_client_command_done(struct cmdq_item
*item
, __unused
void *data
)
2758 struct client
*c
= cmdq_get_client(item
);
2760 if (~c
->flags
& CLIENT_ATTACHED
)
2761 c
->flags
|= CLIENT_EXIT
;
2762 else if (~c
->flags
& CLIENT_EXIT
)
2763 tty_send_requests(&c
->tty
);
2764 return (CMD_RETURN_NORMAL
);
2767 /* Handle command message. */
2769 server_client_dispatch_command(struct client
*c
, struct imsg
*imsg
)
2771 struct msg_command data
;
2775 char **argv
, *cause
;
2776 struct cmd_parse_result
*pr
;
2777 struct args_value
*values
;
2779 if (c
->flags
& CLIENT_EXIT
)
2782 if (imsg
->hdr
.len
- IMSG_HEADER_SIZE
< sizeof data
)
2783 fatalx("bad MSG_COMMAND size");
2784 memcpy(&data
, imsg
->data
, sizeof data
);
2786 buf
= (char *)imsg
->data
+ sizeof data
;
2787 len
= imsg
->hdr
.len
- IMSG_HEADER_SIZE
- sizeof data
;
2788 if (len
> 0 && buf
[len
- 1] != '\0')
2789 fatalx("bad MSG_COMMAND string");
2792 if (cmd_unpack_argv(buf
, len
, argc
, &argv
) != 0) {
2793 cause
= xstrdup("command too long");
2799 argv
= xcalloc(1, sizeof *argv
);
2800 *argv
= xstrdup("new-session");
2803 values
= args_from_vector(argc
, argv
);
2804 pr
= cmd_parse_from_arguments(values
, argc
, NULL
);
2805 switch (pr
->status
) {
2806 case CMD_PARSE_ERROR
:
2809 case CMD_PARSE_SUCCESS
:
2812 args_free_values(values
, argc
);
2814 cmd_free_argv(argc
, argv
);
2816 cmdq_append(c
, cmdq_get_command(pr
->cmdlist
, NULL
));
2817 cmdq_append(c
, cmdq_get_callback(server_client_command_done
, NULL
));
2819 cmd_list_free(pr
->cmdlist
);
2823 cmd_free_argv(argc
, argv
);
2825 cmdq_append(c
, cmdq_get_error(cause
));
2828 c
->flags
|= CLIENT_EXIT
;
2831 /* Handle identify message. */
2833 server_client_dispatch_identify(struct client
*c
, struct imsg
*imsg
)
2835 const char *data
, *home
;
2841 if (c
->flags
& CLIENT_IDENTIFIED
)
2842 fatalx("out-of-order identify message");
2845 datalen
= imsg
->hdr
.len
- IMSG_HEADER_SIZE
;
2847 switch (imsg
->hdr
.type
) {
2848 case MSG_IDENTIFY_FEATURES
:
2849 if (datalen
!= sizeof feat
)
2850 fatalx("bad MSG_IDENTIFY_FEATURES size");
2851 memcpy(&feat
, data
, sizeof feat
);
2852 c
->term_features
|= feat
;
2853 log_debug("client %p IDENTIFY_FEATURES %s", c
,
2854 tty_get_features(feat
));
2856 case MSG_IDENTIFY_FLAGS
:
2857 if (datalen
!= sizeof flags
)
2858 fatalx("bad MSG_IDENTIFY_FLAGS size");
2859 memcpy(&flags
, data
, sizeof flags
);
2861 log_debug("client %p IDENTIFY_FLAGS %#x", c
, flags
);
2863 case MSG_IDENTIFY_LONGFLAGS
:
2864 if (datalen
!= sizeof longflags
)
2865 fatalx("bad MSG_IDENTIFY_LONGFLAGS size");
2866 memcpy(&longflags
, data
, sizeof longflags
);
2867 c
->flags
|= longflags
;
2868 log_debug("client %p IDENTIFY_LONGFLAGS %#llx", c
,
2869 (unsigned long long)longflags
);
2871 case MSG_IDENTIFY_TERM
:
2872 if (datalen
== 0 || data
[datalen
- 1] != '\0')
2873 fatalx("bad MSG_IDENTIFY_TERM string");
2875 c
->term_name
= xstrdup("unknown");
2877 c
->term_name
= xstrdup(data
);
2878 log_debug("client %p IDENTIFY_TERM %s", c
, data
);
2880 case MSG_IDENTIFY_TERMINFO
:
2881 if (datalen
== 0 || data
[datalen
- 1] != '\0')
2882 fatalx("bad MSG_IDENTIFY_TERMINFO string");
2883 c
->term_caps
= xreallocarray(c
->term_caps
, c
->term_ncaps
+ 1,
2884 sizeof *c
->term_caps
);
2885 c
->term_caps
[c
->term_ncaps
++] = xstrdup(data
);
2886 log_debug("client %p IDENTIFY_TERMINFO %s", c
, data
);
2888 case MSG_IDENTIFY_TTYNAME
:
2889 if (datalen
== 0 || data
[datalen
- 1] != '\0')
2890 fatalx("bad MSG_IDENTIFY_TTYNAME string");
2891 c
->ttyname
= xstrdup(data
);
2892 log_debug("client %p IDENTIFY_TTYNAME %s", c
, data
);
2894 case MSG_IDENTIFY_CWD
:
2895 if (datalen
== 0 || data
[datalen
- 1] != '\0')
2896 fatalx("bad MSG_IDENTIFY_CWD string");
2897 if (access(data
, X_OK
) == 0)
2898 c
->cwd
= xstrdup(data
);
2899 else if ((home
= find_home()) != NULL
)
2900 c
->cwd
= xstrdup(home
);
2902 c
->cwd
= xstrdup("/");
2903 log_debug("client %p IDENTIFY_CWD %s", c
, data
);
2905 case MSG_IDENTIFY_STDIN
:
2907 fatalx("bad MSG_IDENTIFY_STDIN size");
2909 log_debug("client %p IDENTIFY_STDIN %d", c
, imsg
->fd
);
2911 case MSG_IDENTIFY_STDOUT
:
2913 fatalx("bad MSG_IDENTIFY_STDOUT size");
2914 c
->out_fd
= imsg
->fd
;
2915 log_debug("client %p IDENTIFY_STDOUT %d", c
, imsg
->fd
);
2917 case MSG_IDENTIFY_ENVIRON
:
2918 if (datalen
== 0 || data
[datalen
- 1] != '\0')
2919 fatalx("bad MSG_IDENTIFY_ENVIRON string");
2920 if (strchr(data
, '=') != NULL
)
2921 environ_put(c
->environ
, data
, 0);
2922 log_debug("client %p IDENTIFY_ENVIRON %s", c
, data
);
2924 case MSG_IDENTIFY_CLIENTPID
:
2925 if (datalen
!= sizeof c
->pid
)
2926 fatalx("bad MSG_IDENTIFY_CLIENTPID size");
2927 memcpy(&c
->pid
, data
, sizeof c
->pid
);
2928 log_debug("client %p IDENTIFY_CLIENTPID %ld", c
, (long)c
->pid
);
2934 if (imsg
->hdr
.type
!= MSG_IDENTIFY_DONE
)
2936 c
->flags
|= CLIENT_IDENTIFIED
;
2938 if (*c
->ttyname
!= '\0')
2939 name
= xstrdup(c
->ttyname
);
2941 xasprintf(&name
, "client-%ld", (long)c
->pid
);
2943 log_debug("client %p name is %s", c
, c
->name
);
2945 if (c
->flags
& CLIENT_CONTROL
)
2947 else if (c
->fd
!= -1) {
2948 if (tty_init(&c
->tty
, c
) != 0) {
2952 tty_resize(&c
->tty
);
2953 c
->flags
|= CLIENT_TERMINAL
;
2960 * If this is the first client, load configuration files. Any later
2961 * clients are allowed to continue with their command even if the
2962 * config has not been loaded - they might have been run from inside it
2964 if ((~c
->flags
& CLIENT_EXIT
) &&
2966 c
== TAILQ_FIRST(&clients
))
2970 /* Handle shell message. */
2972 server_client_dispatch_shell(struct client
*c
)
2976 shell
= options_get_string(global_s_options
, "default-shell");
2977 if (!checkshell(shell
))
2978 shell
= _PATH_BSHELL
;
2979 proc_send(c
->peer
, MSG_SHELL
, -1, shell
, strlen(shell
) + 1);
2981 proc_kill_peer(c
->peer
);
2984 /* Get client working directory. */
2986 server_client_get_cwd(struct client
*c
, struct session
*s
)
2990 if (!cfg_finished
&& cfg_client
!= NULL
)
2991 return (cfg_client
->cwd
);
2992 if (c
!= NULL
&& c
->session
== NULL
&& c
->cwd
!= NULL
)
2994 if (s
!= NULL
&& s
->cwd
!= NULL
)
2996 if (c
!= NULL
&& (s
= c
->session
) != NULL
&& s
->cwd
!= NULL
)
2998 if ((home
= find_home()) != NULL
)
3003 /* Get control client flags. */
3005 server_client_control_flags(struct client
*c
, const char *next
)
3007 if (strcmp(next
, "pause-after") == 0) {
3009 return (CLIENT_CONTROL_PAUSEAFTER
);
3011 if (sscanf(next
, "pause-after=%u", &c
->pause_age
) == 1) {
3012 c
->pause_age
*= 1000;
3013 return (CLIENT_CONTROL_PAUSEAFTER
);
3015 if (strcmp(next
, "no-output") == 0)
3016 return (CLIENT_CONTROL_NOOUTPUT
);
3017 if (strcmp(next
, "wait-exit") == 0)
3018 return (CLIENT_CONTROL_WAITEXIT
);
3022 /* Set client flags. */
3024 server_client_set_flags(struct client
*c
, const char *flags
)
3026 char *s
, *copy
, *next
;
3030 s
= copy
= xstrdup(flags
);
3031 while ((next
= strsep(&s
, ",")) != NULL
) {
3032 not = (*next
== '!');
3036 if (c
->flags
& CLIENT_CONTROL
)
3037 flag
= server_client_control_flags(c
, next
);
3040 if (strcmp(next
, "read-only") == 0)
3041 flag
= CLIENT_READONLY
;
3042 else if (strcmp(next
, "ignore-size") == 0)
3043 flag
= CLIENT_IGNORESIZE
;
3044 else if (strcmp(next
, "active-pane") == 0)
3045 flag
= CLIENT_ACTIVEPANE
;
3049 log_debug("client %s set flag %s", c
->name
, next
);
3054 if (flag
== CLIENT_CONTROL_NOOUTPUT
)
3055 control_reset_offsets(c
);
3058 proc_send(c
->peer
, MSG_FLAGS
, -1, &c
->flags
, sizeof c
->flags
);
3061 /* Get client flags. This is only flags useful to show to users. */
3063 server_client_get_flags(struct client
*c
)
3069 if (c
->flags
& CLIENT_ATTACHED
)
3070 strlcat(s
, "attached,", sizeof s
);
3071 if (c
->flags
& CLIENT_FOCUSED
)
3072 strlcat(s
, "focused,", sizeof s
);
3073 if (c
->flags
& CLIENT_CONTROL
)
3074 strlcat(s
, "control-mode,", sizeof s
);
3075 if (c
->flags
& CLIENT_IGNORESIZE
)
3076 strlcat(s
, "ignore-size,", sizeof s
);
3077 if (c
->flags
& CLIENT_CONTROL_NOOUTPUT
)
3078 strlcat(s
, "no-output,", sizeof s
);
3079 if (c
->flags
& CLIENT_CONTROL_WAITEXIT
)
3080 strlcat(s
, "wait-exit,", sizeof s
);
3081 if (c
->flags
& CLIENT_CONTROL_PAUSEAFTER
) {
3082 xsnprintf(tmp
, sizeof tmp
, "pause-after=%u,",
3083 c
->pause_age
/ 1000);
3084 strlcat(s
, tmp
, sizeof s
);
3086 if (c
->flags
& CLIENT_READONLY
)
3087 strlcat(s
, "read-only,", sizeof s
);
3088 if (c
->flags
& CLIENT_ACTIVEPANE
)
3089 strlcat(s
, "active-pane,", sizeof s
);
3090 if (c
->flags
& CLIENT_SUSPENDED
)
3091 strlcat(s
, "suspended,", sizeof s
);
3092 if (c
->flags
& CLIENT_UTF8
)
3093 strlcat(s
, "UTF-8,", sizeof s
);
3095 s
[strlen(s
) - 1] = '\0';
3099 /* Get client window. */
3100 struct client_window
*
3101 server_client_get_client_window(struct client
*c
, u_int id
)
3103 struct client_window cw
= { .window
= id
};
3105 return (RB_FIND(client_windows
, &c
->windows
, &cw
));
3108 /* Add client window. */
3109 struct client_window
*
3110 server_client_add_client_window(struct client
*c
, u_int id
)
3112 struct client_window
*cw
;
3114 cw
= server_client_get_client_window(c
, id
);
3116 cw
= xcalloc(1, sizeof *cw
);
3118 RB_INSERT(client_windows
, &c
->windows
, cw
);
3123 /* Get client active pane. */
3124 struct window_pane
*
3125 server_client_get_pane(struct client
*c
)
3127 struct session
*s
= c
->session
;
3128 struct client_window
*cw
;
3133 if (~c
->flags
& CLIENT_ACTIVEPANE
)
3134 return (s
->curw
->window
->active
);
3135 cw
= server_client_get_client_window(c
, s
->curw
->window
->id
);
3137 return (s
->curw
->window
->active
);
3141 /* Set client active pane. */
3143 server_client_set_pane(struct client
*c
, struct window_pane
*wp
)
3145 struct session
*s
= c
->session
;
3146 struct client_window
*cw
;
3151 cw
= server_client_add_client_window(c
, s
->curw
->window
->id
);
3153 log_debug("%s pane now %%%u", c
->name
, wp
->id
);
3156 /* Remove pane from client lists. */
3158 server_client_remove_pane(struct window_pane
*wp
)
3161 struct window
*w
= wp
->window
;
3162 struct client_window
*cw
;
3164 TAILQ_FOREACH(c
, &clients
, entry
) {
3165 cw
= server_client_get_client_window(c
, w
->id
);
3166 if (cw
!= NULL
&& cw
->pane
== wp
) {
3167 RB_REMOVE(client_windows
, &c
->windows
, cw
);