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>
36 static void server_client_free(int, short, void *);
37 static void server_client_check_pane_resize(struct window_pane
*);
38 static void server_client_check_pane_buffer(struct window_pane
*);
39 static void server_client_check_window_resize(struct window
*);
40 static key_code
server_client_check_mouse(struct client
*, struct key_event
*);
41 static void server_client_repeat_timer(int, short, void *);
42 static void server_client_click_timer(int, short, void *);
43 static void server_client_check_exit(struct client
*);
44 static void server_client_check_redraw(struct client
*);
45 static void server_client_check_modes(struct client
*);
46 static void server_client_set_title(struct client
*);
47 static void server_client_set_path(struct client
*);
48 static void server_client_reset_state(struct client
*);
49 static int server_client_is_bracket_pasting(struct client
*, key_code
);
50 static int server_client_assume_paste(struct session
*);
51 static void server_client_update_latest(struct client
*);
53 static void server_client_dispatch(struct imsg
*, void *);
54 static void server_client_dispatch_command(struct client
*, struct imsg
*);
55 static void server_client_dispatch_identify(struct client
*, struct imsg
*);
56 static void server_client_dispatch_shell(struct client
*);
58 /* Compare client windows. */
60 server_client_window_cmp(struct client_window
*cw1
,
61 struct client_window
*cw2
)
63 if (cw1
->window
< cw2
->window
)
65 if (cw1
->window
> cw2
->window
)
69 RB_GENERATE(client_windows
, client_window
, entry
, server_client_window_cmp
);
71 /* Number of attached clients. */
73 server_client_how_many(void)
79 TAILQ_FOREACH(c
, &clients
, entry
) {
80 if (c
->session
!= NULL
&& (~c
->flags
& CLIENT_UNATTACHEDFLAGS
))
86 /* Overlay timer callback. */
88 server_client_overlay_timer(__unused
int fd
, __unused
short events
, void *data
)
90 server_client_clear_overlay(data
);
93 /* Set an overlay on client. */
95 server_client_set_overlay(struct client
*c
, u_int delay
,
96 overlay_check_cb checkcb
, overlay_mode_cb modecb
,
97 overlay_draw_cb drawcb
, overlay_key_cb keycb
, overlay_free_cb freecb
,
98 overlay_resize_cb resizecb
, void *data
)
102 if (c
->overlay_draw
!= NULL
)
103 server_client_clear_overlay(c
);
105 tv
.tv_sec
= delay
/ 1000;
106 tv
.tv_usec
= (delay
% 1000) * 1000L;
108 if (event_initialized(&c
->overlay_timer
))
109 evtimer_del(&c
->overlay_timer
);
110 evtimer_set(&c
->overlay_timer
, server_client_overlay_timer
, c
);
112 evtimer_add(&c
->overlay_timer
, &tv
);
114 c
->overlay_check
= checkcb
;
115 c
->overlay_mode
= modecb
;
116 c
->overlay_draw
= drawcb
;
117 c
->overlay_key
= keycb
;
118 c
->overlay_free
= freecb
;
119 c
->overlay_resize
= resizecb
;
120 c
->overlay_data
= data
;
122 if (c
->overlay_check
== NULL
)
123 c
->tty
.flags
|= TTY_FREEZE
;
124 if (c
->overlay_mode
== NULL
)
125 c
->tty
.flags
|= TTY_NOCURSOR
;
126 server_redraw_client(c
);
129 /* Clear overlay mode on client. */
131 server_client_clear_overlay(struct client
*c
)
133 if (c
->overlay_draw
== NULL
)
136 if (event_initialized(&c
->overlay_timer
))
137 evtimer_del(&c
->overlay_timer
);
139 if (c
->overlay_free
!= NULL
)
140 c
->overlay_free(c
, c
->overlay_data
);
142 c
->overlay_check
= NULL
;
143 c
->overlay_mode
= NULL
;
144 c
->overlay_draw
= NULL
;
145 c
->overlay_key
= NULL
;
146 c
->overlay_free
= NULL
;
147 c
->overlay_data
= NULL
;
149 c
->tty
.flags
&= ~(TTY_FREEZE
|TTY_NOCURSOR
);
150 server_redraw_client(c
);
154 * Given overlay position and dimensions, return parts of the input range which
158 server_client_overlay_range(u_int x
, u_int y
, u_int sx
, u_int sy
, u_int px
,
159 u_int py
, u_int nx
, struct overlay_ranges
*r
)
163 /* Return up to 2 ranges. */
167 /* Trivial case of no overlap in the y direction. */
168 if (py
< y
|| py
> y
+ sy
- 1) {
176 /* Visible bit to the left of the popup. */
187 /* Visible bit to the right of the popup. */
201 /* Check if this client is inside this server. */
203 server_client_check_nested(struct client
*c
)
205 struct environ_entry
*envent
;
206 struct window_pane
*wp
;
208 envent
= environ_find(c
->environ
, "TMUX");
209 if (envent
== NULL
|| *envent
->value
== '\0')
212 RB_FOREACH(wp
, window_pane_tree
, &all_window_panes
) {
213 if (strcmp(wp
->tty
, c
->ttyname
) == 0)
219 /* Set client key table. */
221 server_client_set_key_table(struct client
*c
, const char *name
)
224 name
= server_client_get_key_table(c
);
226 key_bindings_unref_table(c
->keytable
);
227 c
->keytable
= key_bindings_get_table(name
, 1);
228 c
->keytable
->references
++;
231 /* Get default key table. */
233 server_client_get_key_table(struct client
*c
)
235 struct session
*s
= c
->session
;
241 name
= options_get_string(s
->options
, "key-table");
247 /* Is this table the default key table? */
249 server_client_is_default_key_table(struct client
*c
, struct key_table
*table
)
251 return (strcmp(table
->name
, server_client_get_key_table(c
)) == 0);
254 /* Create a new client. */
256 server_client_create(int fd
)
262 c
= xcalloc(1, sizeof *c
);
264 c
->peer
= proc_add_peer(server_proc
, fd
, server_client_dispatch
, c
);
266 if (gettimeofday(&c
->creation_time
, NULL
) != 0)
267 fatal("gettimeofday failed");
268 memcpy(&c
->activity_time
, &c
->creation_time
, sizeof c
->activity_time
);
270 c
->environ
= environ_create();
275 c
->queue
= cmdq_new();
276 RB_INIT(&c
->windows
);
283 c
->flags
|= CLIENT_FOCUSED
;
285 c
->keytable
= key_bindings_get_table("root", 1);
286 c
->keytable
->references
++;
288 evtimer_set(&c
->repeat_timer
, server_client_repeat_timer
, c
);
289 evtimer_set(&c
->click_timer
, server_client_click_timer
, c
);
291 TAILQ_INSERT_TAIL(&clients
, c
, entry
);
292 log_debug("new client %p", c
);
296 /* Open client terminal if needed. */
298 server_client_open(struct client
*c
, char **cause
)
300 const char *ttynam
= _PATH_TTY
;
302 if (c
->flags
& CLIENT_CONTROL
)
305 if (strcmp(c
->ttyname
, ttynam
) == 0||
306 ((isatty(STDIN_FILENO
) &&
307 (ttynam
= ttyname(STDIN_FILENO
)) != NULL
&&
308 strcmp(c
->ttyname
, ttynam
) == 0) ||
309 (isatty(STDOUT_FILENO
) &&
310 (ttynam
= ttyname(STDOUT_FILENO
)) != NULL
&&
311 strcmp(c
->ttyname
, ttynam
) == 0) ||
312 (isatty(STDERR_FILENO
) &&
313 (ttynam
= ttyname(STDERR_FILENO
)) != NULL
&&
314 strcmp(c
->ttyname
, ttynam
) == 0))) {
315 xasprintf(cause
, "can't use %s", c
->ttyname
);
319 if (!(c
->flags
& CLIENT_TERMINAL
)) {
320 *cause
= xstrdup("not a terminal");
324 if (tty_open(&c
->tty
, cause
) != 0)
330 /* Lost an attached client. */
332 server_client_attached_lost(struct client
*c
)
337 struct client
*found
;
339 log_debug("lost attached client %p", c
);
342 * By this point the session in the client has been cleared so walk all
343 * windows to find any with this client as the latest.
345 RB_FOREACH(w
, windows
, &windows
) {
350 TAILQ_FOREACH(loop
, &clients
, entry
) {
352 if (loop
== c
|| s
== NULL
|| s
->curw
->window
!= w
)
354 if (found
== NULL
|| timercmp(&loop
->activity_time
,
355 &found
->activity_time
, >))
359 server_client_update_latest(found
);
363 /* Set client session. */
365 server_client_set_session(struct client
*c
, struct session
*s
)
367 struct session
*old
= c
->session
;
369 if (s
!= NULL
&& c
->session
!= NULL
&& c
->session
!= s
)
370 c
->last_session
= c
->session
;
372 c
->last_session
= NULL
;
374 c
->flags
|= CLIENT_FOCUSED
;
376 if (old
!= NULL
&& old
->curw
!= NULL
)
377 window_update_focus(old
->curw
->window
);
380 window_update_focus(s
->curw
->window
);
381 session_update_activity(s
, NULL
);
382 gettimeofday(&s
->last_attached_time
, NULL
);
383 s
->curw
->flags
&= ~WINLINK_ALERTFLAGS
;
384 s
->curw
->window
->latest
= c
;
385 alerts_check_session(s
);
386 tty_update_client_offset(c
);
387 status_timer_start(c
);
388 notify_client("client-session-changed", c
);
389 server_redraw_client(c
);
392 server_check_unattached();
393 server_update_socket();
398 server_client_lost(struct client
*c
)
400 struct client_file
*cf
, *cf1
;
401 struct client_window
*cw
, *cw1
;
403 c
->flags
|= CLIENT_DEAD
;
405 server_client_clear_overlay(c
);
406 status_prompt_clear(c
);
407 status_message_clear(c
);
409 RB_FOREACH_SAFE(cf
, client_files
, &c
->files
, cf1
) {
413 RB_FOREACH_SAFE(cw
, client_windows
, &c
->windows
, cw1
) {
414 RB_REMOVE(client_windows
, &c
->windows
, cw
);
418 TAILQ_REMOVE(&clients
, c
, entry
);
419 log_debug("lost client %p", c
);
421 if (c
->flags
& CLIENT_ATTACHED
) {
422 server_client_attached_lost(c
);
423 notify_client("client-detached", c
);
426 if (c
->flags
& CLIENT_CONTROL
)
428 if (c
->flags
& CLIENT_TERMINAL
)
431 free(c
->clipboard_panes
);
435 tty_term_free_list(c
->term_caps
, c
->term_ncaps
);
440 free((void *)c
->cwd
);
442 evtimer_del(&c
->repeat_timer
);
443 evtimer_del(&c
->click_timer
);
445 key_bindings_unref_table(c
->keytable
);
447 free(c
->message_string
);
448 if (event_initialized(&c
->message_timer
))
449 evtimer_del(&c
->message_timer
);
451 free(c
->prompt_saved
);
452 free(c
->prompt_string
);
453 free(c
->prompt_buffer
);
455 format_lost_client(c
);
456 environ_free(c
->environ
);
458 proc_remove_peer(c
->peer
);
467 server_client_unref(c
);
469 server_add_accept(0); /* may be more file descriptors now */
472 server_check_unattached();
473 server_update_socket();
476 /* Remove reference from a client. */
478 server_client_unref(struct client
*c
)
480 log_debug("unref client %p (%d references)", c
, c
->references
);
483 if (c
->references
== 0)
484 event_once(-1, EV_TIMEOUT
, server_client_free
, c
, NULL
);
487 /* Free dead client. */
489 server_client_free(__unused
int fd
, __unused
short events
, void *arg
)
491 struct client
*c
= arg
;
493 log_debug("free client %p (%d references)", c
, c
->references
);
497 if (c
->references
== 0) {
498 free((void *)c
->name
);
503 /* Suspend a client. */
505 server_client_suspend(struct client
*c
)
507 struct session
*s
= c
->session
;
509 if (s
== NULL
|| (c
->flags
& CLIENT_UNATTACHEDFLAGS
))
512 tty_stop_tty(&c
->tty
);
513 c
->flags
|= CLIENT_SUSPENDED
;
514 proc_send(c
->peer
, MSG_SUSPEND
, -1, NULL
, 0);
517 /* Detach a client. */
519 server_client_detach(struct client
*c
, enum msgtype msgtype
)
521 struct session
*s
= c
->session
;
523 if (s
== NULL
|| (c
->flags
& CLIENT_NODETACHFLAGS
))
526 c
->flags
|= CLIENT_EXIT
;
528 c
->exit_type
= CLIENT_EXIT_DETACH
;
529 c
->exit_msgtype
= msgtype
;
530 c
->exit_session
= xstrdup(s
->name
);
533 /* Execute command to replace a client. */
535 server_client_exec(struct client
*c
, const char *cmd
)
537 struct session
*s
= c
->session
;
540 size_t cmdsize
, shellsize
;
544 cmdsize
= strlen(cmd
) + 1;
547 shell
= options_get_string(s
->options
, "default-shell");
549 shell
= options_get_string(global_s_options
, "default-shell");
550 if (!checkshell(shell
))
551 shell
= _PATH_BSHELL
;
552 shellsize
= strlen(shell
) + 1;
554 msg
= xmalloc(cmdsize
+ shellsize
);
555 memcpy(msg
, cmd
, cmdsize
);
556 memcpy(msg
+ cmdsize
, shell
, shellsize
);
558 proc_send(c
->peer
, MSG_EXEC
, -1, msg
, cmdsize
+ shellsize
);
562 /* Check for mouse keys. */
564 server_client_check_mouse(struct client
*c
, struct key_event
*event
)
566 struct mouse_event
*m
= &event
->m
;
567 struct session
*s
= c
->session
, *fs
;
569 struct window_pane
*wp
, *fwp
;
570 u_int x
, y
, b
, sx
, sy
, px
, py
;
574 struct style_range
*sr
;
583 TRIPLE
} type
= NOTYPE
;
590 BORDER
} where
= NOWHERE
;
592 log_debug("%s mouse %02x at %u,%u (last %u,%u) (%d)", c
->name
, m
->b
,
593 m
->x
, m
->y
, m
->lx
, m
->ly
, c
->tty
.mouse_drag_flag
);
595 /* What type of event is this? */
596 if (event
->key
== KEYC_DOUBLECLICK
) {
598 x
= m
->x
, y
= m
->y
, b
= m
->b
;
600 log_debug("double-click at %u,%u", x
, y
);
601 } else if ((m
->sgr_type
!= ' ' &&
602 MOUSE_DRAG(m
->sgr_b
) &&
603 MOUSE_RELEASE(m
->sgr_b
)) ||
604 (m
->sgr_type
== ' ' &&
606 MOUSE_RELEASE(m
->b
) &&
607 MOUSE_RELEASE(m
->lb
))) {
609 x
= m
->x
, y
= m
->y
, b
= 0;
610 log_debug("move at %u,%u", x
, y
);
611 } else if (MOUSE_DRAG(m
->b
)) {
613 if (c
->tty
.mouse_drag_flag
) {
614 x
= m
->x
, y
= m
->y
, b
= m
->b
;
615 if (x
== m
->lx
&& y
== m
->ly
)
616 return (KEYC_UNKNOWN
);
617 log_debug("drag update at %u,%u", x
, y
);
619 x
= m
->lx
, y
= m
->ly
, b
= m
->lb
;
620 log_debug("drag start at %u,%u", x
, y
);
622 } else if (MOUSE_WHEEL(m
->b
)) {
624 x
= m
->x
, y
= m
->y
, b
= m
->b
;
625 log_debug("wheel at %u,%u", x
, y
);
626 } else if (MOUSE_RELEASE(m
->b
)) {
628 x
= m
->x
, y
= m
->y
, b
= m
->lb
;
629 log_debug("up at %u,%u", x
, y
);
631 if (c
->flags
& CLIENT_DOUBLECLICK
) {
632 evtimer_del(&c
->click_timer
);
633 c
->flags
&= ~CLIENT_DOUBLECLICK
;
634 if (m
->b
== c
->click_button
) {
636 x
= m
->x
, y
= m
->y
, b
= m
->b
;
637 log_debug("second-click at %u,%u", x
, y
);
638 c
->flags
|= CLIENT_TRIPLECLICK
;
640 } else if (c
->flags
& CLIENT_TRIPLECLICK
) {
641 evtimer_del(&c
->click_timer
);
642 c
->flags
&= ~CLIENT_TRIPLECLICK
;
643 if (m
->b
== c
->click_button
) {
645 x
= m
->x
, y
= m
->y
, b
= m
->b
;
646 log_debug("triple-click at %u,%u", x
, y
);
651 x
= m
->x
, y
= m
->y
, b
= m
->b
;
652 log_debug("down at %u,%u", x
, y
);
653 c
->flags
|= CLIENT_DOUBLECLICK
;
656 if (KEYC_CLICK_TIMEOUT
!= 0) {
657 memcpy(&c
->click_event
, m
, sizeof c
->click_event
);
658 c
->click_button
= m
->b
;
660 log_debug("click timer started");
661 tv
.tv_sec
= KEYC_CLICK_TIMEOUT
/ 1000;
662 tv
.tv_usec
= (KEYC_CLICK_TIMEOUT
% 1000) * 1000L;
663 evtimer_del(&c
->click_timer
);
664 evtimer_add(&c
->click_timer
, &tv
);
670 return (KEYC_UNKNOWN
);
672 /* Save the session. */
678 /* Is this on the status line? */
679 m
->statusat
= status_at_line(c
);
680 m
->statuslines
= status_line_size(c
);
681 if (m
->statusat
!= -1 &&
682 y
>= (u_int
)m
->statusat
&&
683 y
< m
->statusat
+ m
->statuslines
) {
684 sr
= status_get_range(c
, x
, y
- m
->statusat
);
686 where
= STATUS_DEFAULT
;
689 case STYLE_RANGE_NONE
:
690 return (KEYC_UNKNOWN
);
691 case STYLE_RANGE_LEFT
:
692 log_debug("mouse range: left");
695 case STYLE_RANGE_RIGHT
:
696 log_debug("mouse range: right");
697 where
= STATUS_RIGHT
;
699 case STYLE_RANGE_PANE
:
700 fwp
= window_pane_find_by_id(sr
->argument
);
702 return (KEYC_UNKNOWN
);
703 m
->wp
= sr
->argument
;
705 log_debug("mouse range: pane %%%u", m
->wp
);
708 case STYLE_RANGE_WINDOW
:
709 fwl
= winlink_find_by_index(&s
->windows
,
712 return (KEYC_UNKNOWN
);
713 m
->w
= fwl
->window
->id
;
715 log_debug("mouse range: window @%u", m
->w
);
718 case STYLE_RANGE_SESSION
:
719 fs
= session_find_by_id(sr
->argument
);
721 return (KEYC_UNKNOWN
);
724 log_debug("mouse range: session $%u", m
->s
);
727 case STYLE_RANGE_USER
:
734 /* Not on status line. Adjust position and check for border or pane. */
735 if (where
== NOWHERE
) {
737 if (m
->statusat
== 0 && y
>= m
->statuslines
)
738 py
= y
- m
->statuslines
;
739 else if (m
->statusat
> 0 && y
>= (u_int
)m
->statusat
)
740 py
= m
->statusat
- 1;
744 tty_window_offset(&c
->tty
, &m
->ox
, &m
->oy
, &sx
, &sy
);
745 log_debug("mouse window @%u at %u,%u (%ux%u)",
746 s
->curw
->window
->id
, m
->ox
, m
->oy
, sx
, sy
);
747 if (px
> sx
|| py
> sy
)
748 return (KEYC_UNKNOWN
);
752 /* Try the pane borders if not zoomed. */
753 if (~s
->curw
->window
->flags
& WINDOW_ZOOMED
) {
754 TAILQ_FOREACH(wp
, &s
->curw
->window
->panes
, entry
) {
755 if ((wp
->xoff
+ wp
->sx
== px
&&
756 wp
->yoff
<= 1 + py
&&
757 wp
->yoff
+ wp
->sy
>= py
) ||
758 (wp
->yoff
+ wp
->sy
== py
&&
759 wp
->xoff
<= 1 + px
&&
760 wp
->xoff
+ wp
->sx
>= px
))
767 /* Otherwise try inside the pane. */
768 if (where
== NOWHERE
) {
769 wp
= window_get_active_at(s
->curw
->window
, px
, py
);
773 return (KEYC_UNKNOWN
);
776 log_debug("mouse %u,%u on pane %%%u", x
, y
, wp
->id
);
777 else if (where
== BORDER
)
778 log_debug("mouse on pane %%%u border", wp
->id
);
780 m
->w
= wp
->window
->id
;
784 /* Stop dragging if needed. */
785 if (type
!= DRAG
&& type
!= WHEEL
&& c
->tty
.mouse_drag_flag
!= 0) {
786 if (c
->tty
.mouse_drag_release
!= NULL
)
787 c
->tty
.mouse_drag_release(c
, m
);
789 c
->tty
.mouse_drag_update
= NULL
;
790 c
->tty
.mouse_drag_release
= NULL
;
793 * End a mouse drag by passing a MouseDragEnd key corresponding
794 * to the button that started the drag.
796 switch (c
->tty
.mouse_drag_flag
- 1) {
799 key
= KEYC_MOUSEDRAGEND1_PANE
;
801 key
= KEYC_MOUSEDRAGEND1_STATUS
;
802 if (where
== STATUS_LEFT
)
803 key
= KEYC_MOUSEDRAGEND1_STATUS_LEFT
;
804 if (where
== STATUS_RIGHT
)
805 key
= KEYC_MOUSEDRAGEND1_STATUS_RIGHT
;
806 if (where
== STATUS_DEFAULT
)
807 key
= KEYC_MOUSEDRAGEND1_STATUS_DEFAULT
;
809 key
= KEYC_MOUSEDRAGEND1_BORDER
;
813 key
= KEYC_MOUSEDRAGEND2_PANE
;
815 key
= KEYC_MOUSEDRAGEND2_STATUS
;
816 if (where
== STATUS_LEFT
)
817 key
= KEYC_MOUSEDRAGEND2_STATUS_LEFT
;
818 if (where
== STATUS_RIGHT
)
819 key
= KEYC_MOUSEDRAGEND2_STATUS_RIGHT
;
820 if (where
== STATUS_DEFAULT
)
821 key
= KEYC_MOUSEDRAGEND2_STATUS_DEFAULT
;
823 key
= KEYC_MOUSEDRAGEND2_BORDER
;
827 key
= KEYC_MOUSEDRAGEND3_PANE
;
829 key
= KEYC_MOUSEDRAGEND3_STATUS
;
830 if (where
== STATUS_LEFT
)
831 key
= KEYC_MOUSEDRAGEND3_STATUS_LEFT
;
832 if (where
== STATUS_RIGHT
)
833 key
= KEYC_MOUSEDRAGEND3_STATUS_RIGHT
;
834 if (where
== STATUS_DEFAULT
)
835 key
= KEYC_MOUSEDRAGEND3_STATUS_DEFAULT
;
837 key
= KEYC_MOUSEDRAGEND3_BORDER
;
841 key
= KEYC_MOUSEDRAGEND6_PANE
;
843 key
= KEYC_MOUSEDRAGEND6_STATUS
;
844 if (where
== STATUS_LEFT
)
845 key
= KEYC_MOUSEDRAGEND6_STATUS_LEFT
;
846 if (where
== STATUS_RIGHT
)
847 key
= KEYC_MOUSEDRAGEND6_STATUS_RIGHT
;
848 if (where
== STATUS_DEFAULT
)
849 key
= KEYC_MOUSEDRAGEND6_STATUS_DEFAULT
;
851 key
= KEYC_MOUSEDRAGEND6_BORDER
;
855 key
= KEYC_MOUSEDRAGEND7_PANE
;
857 key
= KEYC_MOUSEDRAGEND7_STATUS
;
858 if (where
== STATUS_LEFT
)
859 key
= KEYC_MOUSEDRAGEND7_STATUS_LEFT
;
860 if (where
== STATUS_RIGHT
)
861 key
= KEYC_MOUSEDRAGEND7_STATUS_RIGHT
;
862 if (where
== STATUS_DEFAULT
)
863 key
= KEYC_MOUSEDRAGEND7_STATUS_DEFAULT
;
865 key
= KEYC_MOUSEDRAGEND7_BORDER
;
869 key
= KEYC_MOUSEDRAGEND8_PANE
;
871 key
= KEYC_MOUSEDRAGEND8_STATUS
;
872 if (where
== STATUS_LEFT
)
873 key
= KEYC_MOUSEDRAGEND8_STATUS_LEFT
;
874 if (where
== STATUS_RIGHT
)
875 key
= KEYC_MOUSEDRAGEND8_STATUS_RIGHT
;
876 if (where
== STATUS_DEFAULT
)
877 key
= KEYC_MOUSEDRAGEND8_STATUS_DEFAULT
;
879 key
= KEYC_MOUSEDRAGEND8_BORDER
;
883 key
= KEYC_MOUSEDRAGEND9_PANE
;
885 key
= KEYC_MOUSEDRAGEND9_STATUS
;
886 if (where
== STATUS_LEFT
)
887 key
= KEYC_MOUSEDRAGEND9_STATUS_LEFT
;
888 if (where
== STATUS_RIGHT
)
889 key
= KEYC_MOUSEDRAGEND9_STATUS_RIGHT
;
890 if (where
== STATUS_DEFAULT
)
891 key
= KEYC_MOUSEDRAGEND9_STATUS_DEFAULT
;
893 key
= KEYC_MOUSEDRAGEND9_BORDER
;
895 case MOUSE_BUTTON_10
:
897 key
= KEYC_MOUSEDRAGEND10_PANE
;
899 key
= KEYC_MOUSEDRAGEND10_STATUS
;
900 if (where
== STATUS_LEFT
)
901 key
= KEYC_MOUSEDRAGEND10_STATUS_LEFT
;
902 if (where
== STATUS_RIGHT
)
903 key
= KEYC_MOUSEDRAGEND10_STATUS_RIGHT
;
904 if (where
== STATUS_DEFAULT
)
905 key
= KEYC_MOUSEDRAGEND10_STATUS_DEFAULT
;
907 key
= KEYC_MOUSEDRAGEND10_BORDER
;
909 case MOUSE_BUTTON_11
:
911 key
= KEYC_MOUSEDRAGEND11_PANE
;
913 key
= KEYC_MOUSEDRAGEND11_STATUS
;
914 if (where
== STATUS_LEFT
)
915 key
= KEYC_MOUSEDRAGEND11_STATUS_LEFT
;
916 if (where
== STATUS_RIGHT
)
917 key
= KEYC_MOUSEDRAGEND11_STATUS_RIGHT
;
918 if (where
== STATUS_DEFAULT
)
919 key
= KEYC_MOUSEDRAGEND11_STATUS_DEFAULT
;
921 key
= KEYC_MOUSEDRAGEND11_BORDER
;
927 c
->tty
.mouse_drag_flag
= 0;
931 /* Convert to a key binding. */
938 key
= KEYC_MOUSEMOVE_PANE
;
940 key
= KEYC_MOUSEMOVE_STATUS
;
941 if (where
== STATUS_LEFT
)
942 key
= KEYC_MOUSEMOVE_STATUS_LEFT
;
943 if (where
== STATUS_RIGHT
)
944 key
= KEYC_MOUSEMOVE_STATUS_RIGHT
;
945 if (where
== STATUS_DEFAULT
)
946 key
= KEYC_MOUSEMOVE_STATUS_DEFAULT
;
948 key
= KEYC_MOUSEMOVE_BORDER
;
951 if (c
->tty
.mouse_drag_update
!= NULL
)
954 switch (MOUSE_BUTTONS(b
)) {
957 key
= KEYC_MOUSEDRAG1_PANE
;
959 key
= KEYC_MOUSEDRAG1_STATUS
;
960 if (where
== STATUS_LEFT
)
961 key
= KEYC_MOUSEDRAG1_STATUS_LEFT
;
962 if (where
== STATUS_RIGHT
)
963 key
= KEYC_MOUSEDRAG1_STATUS_RIGHT
;
964 if (where
== STATUS_DEFAULT
)
965 key
= KEYC_MOUSEDRAG1_STATUS_DEFAULT
;
967 key
= KEYC_MOUSEDRAG1_BORDER
;
971 key
= KEYC_MOUSEDRAG2_PANE
;
973 key
= KEYC_MOUSEDRAG2_STATUS
;
974 if (where
== STATUS_LEFT
)
975 key
= KEYC_MOUSEDRAG2_STATUS_LEFT
;
976 if (where
== STATUS_RIGHT
)
977 key
= KEYC_MOUSEDRAG2_STATUS_RIGHT
;
978 if (where
== STATUS_DEFAULT
)
979 key
= KEYC_MOUSEDRAG2_STATUS_DEFAULT
;
981 key
= KEYC_MOUSEDRAG2_BORDER
;
985 key
= KEYC_MOUSEDRAG3_PANE
;
987 key
= KEYC_MOUSEDRAG3_STATUS
;
988 if (where
== STATUS_LEFT
)
989 key
= KEYC_MOUSEDRAG3_STATUS_LEFT
;
990 if (where
== STATUS_RIGHT
)
991 key
= KEYC_MOUSEDRAG3_STATUS_RIGHT
;
992 if (where
== STATUS_DEFAULT
)
993 key
= KEYC_MOUSEDRAG3_STATUS_DEFAULT
;
995 key
= KEYC_MOUSEDRAG3_BORDER
;
999 key
= KEYC_MOUSEDRAG6_PANE
;
1000 if (where
== STATUS
)
1001 key
= KEYC_MOUSEDRAG6_STATUS
;
1002 if (where
== STATUS_LEFT
)
1003 key
= KEYC_MOUSEDRAG6_STATUS_LEFT
;
1004 if (where
== STATUS_RIGHT
)
1005 key
= KEYC_MOUSEDRAG6_STATUS_RIGHT
;
1006 if (where
== STATUS_DEFAULT
)
1007 key
= KEYC_MOUSEDRAG6_STATUS_DEFAULT
;
1008 if (where
== BORDER
)
1009 key
= KEYC_MOUSEDRAG6_BORDER
;
1011 case MOUSE_BUTTON_7
:
1013 key
= KEYC_MOUSEDRAG7_PANE
;
1014 if (where
== STATUS
)
1015 key
= KEYC_MOUSEDRAG7_STATUS
;
1016 if (where
== STATUS_LEFT
)
1017 key
= KEYC_MOUSEDRAG7_STATUS_LEFT
;
1018 if (where
== STATUS_RIGHT
)
1019 key
= KEYC_MOUSEDRAG7_STATUS_RIGHT
;
1020 if (where
== STATUS_DEFAULT
)
1021 key
= KEYC_MOUSEDRAG7_STATUS_DEFAULT
;
1022 if (where
== BORDER
)
1023 key
= KEYC_MOUSEDRAG7_BORDER
;
1025 case MOUSE_BUTTON_8
:
1027 key
= KEYC_MOUSEDRAG8_PANE
;
1028 if (where
== STATUS
)
1029 key
= KEYC_MOUSEDRAG8_STATUS
;
1030 if (where
== STATUS_LEFT
)
1031 key
= KEYC_MOUSEDRAG8_STATUS_LEFT
;
1032 if (where
== STATUS_RIGHT
)
1033 key
= KEYC_MOUSEDRAG8_STATUS_RIGHT
;
1034 if (where
== STATUS_DEFAULT
)
1035 key
= KEYC_MOUSEDRAG8_STATUS_DEFAULT
;
1036 if (where
== BORDER
)
1037 key
= KEYC_MOUSEDRAG8_BORDER
;
1039 case MOUSE_BUTTON_9
:
1041 key
= KEYC_MOUSEDRAG9_PANE
;
1042 if (where
== STATUS
)
1043 key
= KEYC_MOUSEDRAG9_STATUS
;
1044 if (where
== STATUS_LEFT
)
1045 key
= KEYC_MOUSEDRAG9_STATUS_LEFT
;
1046 if (where
== STATUS_RIGHT
)
1047 key
= KEYC_MOUSEDRAG9_STATUS_RIGHT
;
1048 if (where
== STATUS_DEFAULT
)
1049 key
= KEYC_MOUSEDRAG9_STATUS_DEFAULT
;
1050 if (where
== BORDER
)
1051 key
= KEYC_MOUSEDRAG9_BORDER
;
1053 case MOUSE_BUTTON_10
:
1055 key
= KEYC_MOUSEDRAG10_PANE
;
1056 if (where
== STATUS
)
1057 key
= KEYC_MOUSEDRAG10_STATUS
;
1058 if (where
== STATUS_LEFT
)
1059 key
= KEYC_MOUSEDRAG10_STATUS_LEFT
;
1060 if (where
== STATUS_RIGHT
)
1061 key
= KEYC_MOUSEDRAG10_STATUS_RIGHT
;
1062 if (where
== STATUS_DEFAULT
)
1063 key
= KEYC_MOUSEDRAG10_STATUS_DEFAULT
;
1064 if (where
== BORDER
)
1065 key
= KEYC_MOUSEDRAG10_BORDER
;
1067 case MOUSE_BUTTON_11
:
1069 key
= KEYC_MOUSEDRAG11_PANE
;
1070 if (where
== STATUS
)
1071 key
= KEYC_MOUSEDRAG11_STATUS
;
1072 if (where
== STATUS_LEFT
)
1073 key
= KEYC_MOUSEDRAG11_STATUS_LEFT
;
1074 if (where
== STATUS_RIGHT
)
1075 key
= KEYC_MOUSEDRAG11_STATUS_RIGHT
;
1076 if (where
== STATUS_DEFAULT
)
1077 key
= KEYC_MOUSEDRAG11_STATUS_DEFAULT
;
1078 if (where
== BORDER
)
1079 key
= KEYC_MOUSEDRAG11_BORDER
;
1085 * Begin a drag by setting the flag to a non-zero value that
1086 * corresponds to the mouse button in use.
1088 c
->tty
.mouse_drag_flag
= MOUSE_BUTTONS(b
) + 1;
1091 if (MOUSE_BUTTONS(b
) == MOUSE_WHEEL_UP
) {
1093 key
= KEYC_WHEELUP_PANE
;
1094 if (where
== STATUS
)
1095 key
= KEYC_WHEELUP_STATUS
;
1096 if (where
== STATUS_LEFT
)
1097 key
= KEYC_WHEELUP_STATUS_LEFT
;
1098 if (where
== STATUS_RIGHT
)
1099 key
= KEYC_WHEELUP_STATUS_RIGHT
;
1100 if (where
== STATUS_DEFAULT
)
1101 key
= KEYC_WHEELUP_STATUS_DEFAULT
;
1102 if (where
== BORDER
)
1103 key
= KEYC_WHEELUP_BORDER
;
1106 key
= KEYC_WHEELDOWN_PANE
;
1107 if (where
== STATUS
)
1108 key
= KEYC_WHEELDOWN_STATUS
;
1109 if (where
== STATUS_LEFT
)
1110 key
= KEYC_WHEELDOWN_STATUS_LEFT
;
1111 if (where
== STATUS_RIGHT
)
1112 key
= KEYC_WHEELDOWN_STATUS_RIGHT
;
1113 if (where
== STATUS_DEFAULT
)
1114 key
= KEYC_WHEELDOWN_STATUS_DEFAULT
;
1115 if (where
== BORDER
)
1116 key
= KEYC_WHEELDOWN_BORDER
;
1120 switch (MOUSE_BUTTONS(b
)) {
1121 case MOUSE_BUTTON_1
:
1123 key
= KEYC_MOUSEUP1_PANE
;
1124 if (where
== STATUS
)
1125 key
= KEYC_MOUSEUP1_STATUS
;
1126 if (where
== STATUS_LEFT
)
1127 key
= KEYC_MOUSEUP1_STATUS_LEFT
;
1128 if (where
== STATUS_RIGHT
)
1129 key
= KEYC_MOUSEUP1_STATUS_RIGHT
;
1130 if (where
== STATUS_DEFAULT
)
1131 key
= KEYC_MOUSEUP1_STATUS_DEFAULT
;
1132 if (where
== BORDER
)
1133 key
= KEYC_MOUSEUP1_BORDER
;
1135 case MOUSE_BUTTON_2
:
1137 key
= KEYC_MOUSEUP2_PANE
;
1138 if (where
== STATUS
)
1139 key
= KEYC_MOUSEUP2_STATUS
;
1140 if (where
== STATUS_LEFT
)
1141 key
= KEYC_MOUSEUP2_STATUS_LEFT
;
1142 if (where
== STATUS_RIGHT
)
1143 key
= KEYC_MOUSEUP2_STATUS_RIGHT
;
1144 if (where
== STATUS_DEFAULT
)
1145 key
= KEYC_MOUSEUP2_STATUS_DEFAULT
;
1146 if (where
== BORDER
)
1147 key
= KEYC_MOUSEUP2_BORDER
;
1149 case MOUSE_BUTTON_3
:
1151 key
= KEYC_MOUSEUP3_PANE
;
1152 if (where
== STATUS
)
1153 key
= KEYC_MOUSEUP3_STATUS
;
1154 if (where
== STATUS_LEFT
)
1155 key
= KEYC_MOUSEUP3_STATUS_LEFT
;
1156 if (where
== STATUS_RIGHT
)
1157 key
= KEYC_MOUSEUP3_STATUS_RIGHT
;
1158 if (where
== STATUS_DEFAULT
)
1159 key
= KEYC_MOUSEUP3_STATUS_DEFAULT
;
1160 if (where
== BORDER
)
1161 key
= KEYC_MOUSEUP3_BORDER
;
1163 case MOUSE_BUTTON_6
:
1165 key
= KEYC_MOUSEUP6_PANE
;
1166 if (where
== STATUS
)
1167 key
= KEYC_MOUSEUP6_STATUS
;
1168 if (where
== STATUS_LEFT
)
1169 key
= KEYC_MOUSEUP6_STATUS_LEFT
;
1170 if (where
== STATUS_RIGHT
)
1171 key
= KEYC_MOUSEUP6_STATUS_RIGHT
;
1172 if (where
== STATUS_DEFAULT
)
1173 key
= KEYC_MOUSEUP6_STATUS_DEFAULT
;
1174 if (where
== BORDER
)
1175 key
= KEYC_MOUSEUP6_BORDER
;
1177 case MOUSE_BUTTON_7
:
1179 key
= KEYC_MOUSEUP7_PANE
;
1180 if (where
== STATUS
)
1181 key
= KEYC_MOUSEUP7_STATUS
;
1182 if (where
== STATUS_LEFT
)
1183 key
= KEYC_MOUSEUP7_STATUS_LEFT
;
1184 if (where
== STATUS_RIGHT
)
1185 key
= KEYC_MOUSEUP7_STATUS_RIGHT
;
1186 if (where
== STATUS_DEFAULT
)
1187 key
= KEYC_MOUSEUP7_STATUS_DEFAULT
;
1188 if (where
== BORDER
)
1189 key
= KEYC_MOUSEUP7_BORDER
;
1191 case MOUSE_BUTTON_8
:
1193 key
= KEYC_MOUSEUP8_PANE
;
1194 if (where
== STATUS
)
1195 key
= KEYC_MOUSEUP8_STATUS
;
1196 if (where
== STATUS_LEFT
)
1197 key
= KEYC_MOUSEUP8_STATUS_LEFT
;
1198 if (where
== STATUS_RIGHT
)
1199 key
= KEYC_MOUSEUP8_STATUS_RIGHT
;
1200 if (where
== STATUS_DEFAULT
)
1201 key
= KEYC_MOUSEUP8_STATUS_DEFAULT
;
1202 if (where
== BORDER
)
1203 key
= KEYC_MOUSEUP8_BORDER
;
1205 case MOUSE_BUTTON_9
:
1207 key
= KEYC_MOUSEUP9_PANE
;
1208 if (where
== STATUS
)
1209 key
= KEYC_MOUSEUP9_STATUS
;
1210 if (where
== STATUS_LEFT
)
1211 key
= KEYC_MOUSEUP9_STATUS_LEFT
;
1212 if (where
== STATUS_RIGHT
)
1213 key
= KEYC_MOUSEUP9_STATUS_RIGHT
;
1214 if (where
== STATUS_DEFAULT
)
1215 key
= KEYC_MOUSEUP9_STATUS_DEFAULT
;
1216 if (where
== BORDER
)
1217 key
= KEYC_MOUSEUP9_BORDER
;
1219 case MOUSE_BUTTON_10
:
1221 key
= KEYC_MOUSEUP1_PANE
;
1222 if (where
== STATUS
)
1223 key
= KEYC_MOUSEUP1_STATUS
;
1224 if (where
== STATUS_LEFT
)
1225 key
= KEYC_MOUSEUP1_STATUS_LEFT
;
1226 if (where
== STATUS_RIGHT
)
1227 key
= KEYC_MOUSEUP1_STATUS_RIGHT
;
1228 if (where
== STATUS_DEFAULT
)
1229 key
= KEYC_MOUSEUP1_STATUS_DEFAULT
;
1230 if (where
== BORDER
)
1231 key
= KEYC_MOUSEUP1_BORDER
;
1233 case MOUSE_BUTTON_11
:
1235 key
= KEYC_MOUSEUP11_PANE
;
1236 if (where
== STATUS
)
1237 key
= KEYC_MOUSEUP11_STATUS
;
1238 if (where
== STATUS_LEFT
)
1239 key
= KEYC_MOUSEUP11_STATUS_LEFT
;
1240 if (where
== STATUS_RIGHT
)
1241 key
= KEYC_MOUSEUP11_STATUS_RIGHT
;
1242 if (where
== STATUS_DEFAULT
)
1243 key
= KEYC_MOUSEUP11_STATUS_DEFAULT
;
1244 if (where
== BORDER
)
1245 key
= KEYC_MOUSEUP11_BORDER
;
1250 switch (MOUSE_BUTTONS(b
)) {
1251 case MOUSE_BUTTON_1
:
1253 key
= KEYC_MOUSEDOWN1_PANE
;
1254 if (where
== STATUS
)
1255 key
= KEYC_MOUSEDOWN1_STATUS
;
1256 if (where
== STATUS_LEFT
)
1257 key
= KEYC_MOUSEDOWN1_STATUS_LEFT
;
1258 if (where
== STATUS_RIGHT
)
1259 key
= KEYC_MOUSEDOWN1_STATUS_RIGHT
;
1260 if (where
== STATUS_DEFAULT
)
1261 key
= KEYC_MOUSEDOWN1_STATUS_DEFAULT
;
1262 if (where
== BORDER
)
1263 key
= KEYC_MOUSEDOWN1_BORDER
;
1265 case MOUSE_BUTTON_2
:
1267 key
= KEYC_MOUSEDOWN2_PANE
;
1268 if (where
== STATUS
)
1269 key
= KEYC_MOUSEDOWN2_STATUS
;
1270 if (where
== STATUS_LEFT
)
1271 key
= KEYC_MOUSEDOWN2_STATUS_LEFT
;
1272 if (where
== STATUS_RIGHT
)
1273 key
= KEYC_MOUSEDOWN2_STATUS_RIGHT
;
1274 if (where
== STATUS_DEFAULT
)
1275 key
= KEYC_MOUSEDOWN2_STATUS_DEFAULT
;
1276 if (where
== BORDER
)
1277 key
= KEYC_MOUSEDOWN2_BORDER
;
1279 case MOUSE_BUTTON_3
:
1281 key
= KEYC_MOUSEDOWN3_PANE
;
1282 if (where
== STATUS
)
1283 key
= KEYC_MOUSEDOWN3_STATUS
;
1284 if (where
== STATUS_LEFT
)
1285 key
= KEYC_MOUSEDOWN3_STATUS_LEFT
;
1286 if (where
== STATUS_RIGHT
)
1287 key
= KEYC_MOUSEDOWN3_STATUS_RIGHT
;
1288 if (where
== STATUS_DEFAULT
)
1289 key
= KEYC_MOUSEDOWN3_STATUS_DEFAULT
;
1290 if (where
== BORDER
)
1291 key
= KEYC_MOUSEDOWN3_BORDER
;
1293 case MOUSE_BUTTON_6
:
1295 key
= KEYC_MOUSEDOWN6_PANE
;
1296 if (where
== STATUS
)
1297 key
= KEYC_MOUSEDOWN6_STATUS
;
1298 if (where
== STATUS_LEFT
)
1299 key
= KEYC_MOUSEDOWN6_STATUS_LEFT
;
1300 if (where
== STATUS_RIGHT
)
1301 key
= KEYC_MOUSEDOWN6_STATUS_RIGHT
;
1302 if (where
== STATUS_DEFAULT
)
1303 key
= KEYC_MOUSEDOWN6_STATUS_DEFAULT
;
1304 if (where
== BORDER
)
1305 key
= KEYC_MOUSEDOWN6_BORDER
;
1307 case MOUSE_BUTTON_7
:
1309 key
= KEYC_MOUSEDOWN7_PANE
;
1310 if (where
== STATUS
)
1311 key
= KEYC_MOUSEDOWN7_STATUS
;
1312 if (where
== STATUS_LEFT
)
1313 key
= KEYC_MOUSEDOWN7_STATUS_LEFT
;
1314 if (where
== STATUS_RIGHT
)
1315 key
= KEYC_MOUSEDOWN7_STATUS_RIGHT
;
1316 if (where
== STATUS_DEFAULT
)
1317 key
= KEYC_MOUSEDOWN7_STATUS_DEFAULT
;
1318 if (where
== BORDER
)
1319 key
= KEYC_MOUSEDOWN7_BORDER
;
1321 case MOUSE_BUTTON_8
:
1323 key
= KEYC_MOUSEDOWN8_PANE
;
1324 if (where
== STATUS
)
1325 key
= KEYC_MOUSEDOWN8_STATUS
;
1326 if (where
== STATUS_LEFT
)
1327 key
= KEYC_MOUSEDOWN8_STATUS_LEFT
;
1328 if (where
== STATUS_RIGHT
)
1329 key
= KEYC_MOUSEDOWN8_STATUS_RIGHT
;
1330 if (where
== STATUS_DEFAULT
)
1331 key
= KEYC_MOUSEDOWN8_STATUS_DEFAULT
;
1332 if (where
== BORDER
)
1333 key
= KEYC_MOUSEDOWN8_BORDER
;
1335 case MOUSE_BUTTON_9
:
1337 key
= KEYC_MOUSEDOWN9_PANE
;
1338 if (where
== STATUS
)
1339 key
= KEYC_MOUSEDOWN9_STATUS
;
1340 if (where
== STATUS_LEFT
)
1341 key
= KEYC_MOUSEDOWN9_STATUS_LEFT
;
1342 if (where
== STATUS_RIGHT
)
1343 key
= KEYC_MOUSEDOWN9_STATUS_RIGHT
;
1344 if (where
== STATUS_DEFAULT
)
1345 key
= KEYC_MOUSEDOWN9_STATUS_DEFAULT
;
1346 if (where
== BORDER
)
1347 key
= KEYC_MOUSEDOWN9_BORDER
;
1349 case MOUSE_BUTTON_10
:
1351 key
= KEYC_MOUSEDOWN10_PANE
;
1352 if (where
== STATUS
)
1353 key
= KEYC_MOUSEDOWN10_STATUS
;
1354 if (where
== STATUS_LEFT
)
1355 key
= KEYC_MOUSEDOWN10_STATUS_LEFT
;
1356 if (where
== STATUS_RIGHT
)
1357 key
= KEYC_MOUSEDOWN10_STATUS_RIGHT
;
1358 if (where
== STATUS_DEFAULT
)
1359 key
= KEYC_MOUSEDOWN10_STATUS_DEFAULT
;
1360 if (where
== BORDER
)
1361 key
= KEYC_MOUSEDOWN10_BORDER
;
1363 case MOUSE_BUTTON_11
:
1365 key
= KEYC_MOUSEDOWN11_PANE
;
1366 if (where
== STATUS
)
1367 key
= KEYC_MOUSEDOWN11_STATUS
;
1368 if (where
== STATUS_LEFT
)
1369 key
= KEYC_MOUSEDOWN11_STATUS_LEFT
;
1370 if (where
== STATUS_RIGHT
)
1371 key
= KEYC_MOUSEDOWN11_STATUS_RIGHT
;
1372 if (where
== STATUS_DEFAULT
)
1373 key
= KEYC_MOUSEDOWN11_STATUS_DEFAULT
;
1374 if (where
== BORDER
)
1375 key
= KEYC_MOUSEDOWN11_BORDER
;
1380 switch (MOUSE_BUTTONS(b
)) {
1381 case MOUSE_BUTTON_1
:
1383 key
= KEYC_SECONDCLICK1_PANE
;
1384 if (where
== STATUS
)
1385 key
= KEYC_SECONDCLICK1_STATUS
;
1386 if (where
== STATUS_LEFT
)
1387 key
= KEYC_SECONDCLICK1_STATUS_LEFT
;
1388 if (where
== STATUS_RIGHT
)
1389 key
= KEYC_SECONDCLICK1_STATUS_RIGHT
;
1390 if (where
== STATUS_DEFAULT
)
1391 key
= KEYC_SECONDCLICK1_STATUS_DEFAULT
;
1392 if (where
== BORDER
)
1393 key
= KEYC_SECONDCLICK1_BORDER
;
1395 case MOUSE_BUTTON_2
:
1397 key
= KEYC_SECONDCLICK2_PANE
;
1398 if (where
== STATUS
)
1399 key
= KEYC_SECONDCLICK2_STATUS
;
1400 if (where
== STATUS_LEFT
)
1401 key
= KEYC_SECONDCLICK2_STATUS_LEFT
;
1402 if (where
== STATUS_RIGHT
)
1403 key
= KEYC_SECONDCLICK2_STATUS_RIGHT
;
1404 if (where
== STATUS_DEFAULT
)
1405 key
= KEYC_SECONDCLICK2_STATUS_DEFAULT
;
1406 if (where
== BORDER
)
1407 key
= KEYC_SECONDCLICK2_BORDER
;
1409 case MOUSE_BUTTON_3
:
1411 key
= KEYC_SECONDCLICK3_PANE
;
1412 if (where
== STATUS
)
1413 key
= KEYC_SECONDCLICK3_STATUS
;
1414 if (where
== STATUS_LEFT
)
1415 key
= KEYC_SECONDCLICK3_STATUS_LEFT
;
1416 if (where
== STATUS_RIGHT
)
1417 key
= KEYC_SECONDCLICK3_STATUS_RIGHT
;
1418 if (where
== STATUS_DEFAULT
)
1419 key
= KEYC_SECONDCLICK3_STATUS_DEFAULT
;
1420 if (where
== BORDER
)
1421 key
= KEYC_SECONDCLICK3_BORDER
;
1423 case MOUSE_BUTTON_6
:
1425 key
= KEYC_SECONDCLICK6_PANE
;
1426 if (where
== STATUS
)
1427 key
= KEYC_SECONDCLICK6_STATUS
;
1428 if (where
== STATUS_LEFT
)
1429 key
= KEYC_SECONDCLICK6_STATUS_LEFT
;
1430 if (where
== STATUS_RIGHT
)
1431 key
= KEYC_SECONDCLICK6_STATUS_RIGHT
;
1432 if (where
== STATUS_DEFAULT
)
1433 key
= KEYC_SECONDCLICK6_STATUS_DEFAULT
;
1434 if (where
== BORDER
)
1435 key
= KEYC_SECONDCLICK6_BORDER
;
1437 case MOUSE_BUTTON_7
:
1439 key
= KEYC_SECONDCLICK7_PANE
;
1440 if (where
== STATUS
)
1441 key
= KEYC_SECONDCLICK7_STATUS
;
1442 if (where
== STATUS_LEFT
)
1443 key
= KEYC_SECONDCLICK7_STATUS_LEFT
;
1444 if (where
== STATUS_RIGHT
)
1445 key
= KEYC_SECONDCLICK7_STATUS_RIGHT
;
1446 if (where
== STATUS_DEFAULT
)
1447 key
= KEYC_SECONDCLICK7_STATUS_DEFAULT
;
1448 if (where
== BORDER
)
1449 key
= KEYC_SECONDCLICK7_BORDER
;
1451 case MOUSE_BUTTON_8
:
1453 key
= KEYC_SECONDCLICK8_PANE
;
1454 if (where
== STATUS
)
1455 key
= KEYC_SECONDCLICK8_STATUS
;
1456 if (where
== STATUS_LEFT
)
1457 key
= KEYC_SECONDCLICK8_STATUS_LEFT
;
1458 if (where
== STATUS_RIGHT
)
1459 key
= KEYC_SECONDCLICK8_STATUS_RIGHT
;
1460 if (where
== STATUS_DEFAULT
)
1461 key
= KEYC_SECONDCLICK8_STATUS_DEFAULT
;
1462 if (where
== BORDER
)
1463 key
= KEYC_SECONDCLICK8_BORDER
;
1465 case MOUSE_BUTTON_9
:
1467 key
= KEYC_SECONDCLICK9_PANE
;
1468 if (where
== STATUS
)
1469 key
= KEYC_SECONDCLICK9_STATUS
;
1470 if (where
== STATUS_LEFT
)
1471 key
= KEYC_SECONDCLICK9_STATUS_LEFT
;
1472 if (where
== STATUS_RIGHT
)
1473 key
= KEYC_SECONDCLICK9_STATUS_RIGHT
;
1474 if (where
== STATUS_DEFAULT
)
1475 key
= KEYC_SECONDCLICK9_STATUS_DEFAULT
;
1476 if (where
== BORDER
)
1477 key
= KEYC_SECONDCLICK9_BORDER
;
1479 case MOUSE_BUTTON_10
:
1481 key
= KEYC_SECONDCLICK10_PANE
;
1482 if (where
== STATUS
)
1483 key
= KEYC_SECONDCLICK10_STATUS
;
1484 if (where
== STATUS_LEFT
)
1485 key
= KEYC_SECONDCLICK10_STATUS_LEFT
;
1486 if (where
== STATUS_RIGHT
)
1487 key
= KEYC_SECONDCLICK10_STATUS_RIGHT
;
1488 if (where
== STATUS_DEFAULT
)
1489 key
= KEYC_SECONDCLICK10_STATUS_DEFAULT
;
1490 if (where
== BORDER
)
1491 key
= KEYC_SECONDCLICK10_BORDER
;
1493 case MOUSE_BUTTON_11
:
1495 key
= KEYC_SECONDCLICK11_PANE
;
1496 if (where
== STATUS
)
1497 key
= KEYC_SECONDCLICK11_STATUS
;
1498 if (where
== STATUS_LEFT
)
1499 key
= KEYC_SECONDCLICK11_STATUS_LEFT
;
1500 if (where
== STATUS_RIGHT
)
1501 key
= KEYC_SECONDCLICK11_STATUS_RIGHT
;
1502 if (where
== STATUS_DEFAULT
)
1503 key
= KEYC_SECONDCLICK11_STATUS_DEFAULT
;
1504 if (where
== BORDER
)
1505 key
= KEYC_SECONDCLICK11_BORDER
;
1510 switch (MOUSE_BUTTONS(b
)) {
1511 case MOUSE_BUTTON_1
:
1513 key
= KEYC_DOUBLECLICK1_PANE
;
1514 if (where
== STATUS
)
1515 key
= KEYC_DOUBLECLICK1_STATUS
;
1516 if (where
== STATUS_LEFT
)
1517 key
= KEYC_DOUBLECLICK1_STATUS_LEFT
;
1518 if (where
== STATUS_RIGHT
)
1519 key
= KEYC_DOUBLECLICK1_STATUS_RIGHT
;
1520 if (where
== STATUS_DEFAULT
)
1521 key
= KEYC_DOUBLECLICK1_STATUS_DEFAULT
;
1522 if (where
== BORDER
)
1523 key
= KEYC_DOUBLECLICK1_BORDER
;
1525 case MOUSE_BUTTON_2
:
1527 key
= KEYC_DOUBLECLICK2_PANE
;
1528 if (where
== STATUS
)
1529 key
= KEYC_DOUBLECLICK2_STATUS
;
1530 if (where
== STATUS_LEFT
)
1531 key
= KEYC_DOUBLECLICK2_STATUS_LEFT
;
1532 if (where
== STATUS_RIGHT
)
1533 key
= KEYC_DOUBLECLICK2_STATUS_RIGHT
;
1534 if (where
== STATUS_DEFAULT
)
1535 key
= KEYC_DOUBLECLICK2_STATUS_DEFAULT
;
1536 if (where
== BORDER
)
1537 key
= KEYC_DOUBLECLICK2_BORDER
;
1539 case MOUSE_BUTTON_3
:
1541 key
= KEYC_DOUBLECLICK3_PANE
;
1542 if (where
== STATUS
)
1543 key
= KEYC_DOUBLECLICK3_STATUS
;
1544 if (where
== STATUS_LEFT
)
1545 key
= KEYC_DOUBLECLICK3_STATUS_LEFT
;
1546 if (where
== STATUS_RIGHT
)
1547 key
= KEYC_DOUBLECLICK3_STATUS_RIGHT
;
1548 if (where
== STATUS_DEFAULT
)
1549 key
= KEYC_DOUBLECLICK3_STATUS_DEFAULT
;
1550 if (where
== BORDER
)
1551 key
= KEYC_DOUBLECLICK3_BORDER
;
1553 case MOUSE_BUTTON_6
:
1555 key
= KEYC_DOUBLECLICK6_PANE
;
1556 if (where
== STATUS
)
1557 key
= KEYC_DOUBLECLICK6_STATUS
;
1558 if (where
== STATUS_LEFT
)
1559 key
= KEYC_DOUBLECLICK6_STATUS_LEFT
;
1560 if (where
== STATUS_RIGHT
)
1561 key
= KEYC_DOUBLECLICK6_STATUS_RIGHT
;
1562 if (where
== STATUS_DEFAULT
)
1563 key
= KEYC_DOUBLECLICK6_STATUS_DEFAULT
;
1564 if (where
== BORDER
)
1565 key
= KEYC_DOUBLECLICK6_BORDER
;
1567 case MOUSE_BUTTON_7
:
1569 key
= KEYC_DOUBLECLICK7_PANE
;
1570 if (where
== STATUS
)
1571 key
= KEYC_DOUBLECLICK7_STATUS
;
1572 if (where
== STATUS_LEFT
)
1573 key
= KEYC_DOUBLECLICK7_STATUS_LEFT
;
1574 if (where
== STATUS_RIGHT
)
1575 key
= KEYC_DOUBLECLICK7_STATUS_RIGHT
;
1576 if (where
== STATUS_DEFAULT
)
1577 key
= KEYC_DOUBLECLICK7_STATUS_DEFAULT
;
1578 if (where
== BORDER
)
1579 key
= KEYC_DOUBLECLICK7_BORDER
;
1581 case MOUSE_BUTTON_8
:
1583 key
= KEYC_DOUBLECLICK8_PANE
;
1584 if (where
== STATUS
)
1585 key
= KEYC_DOUBLECLICK8_STATUS
;
1586 if (where
== STATUS_LEFT
)
1587 key
= KEYC_DOUBLECLICK8_STATUS_LEFT
;
1588 if (where
== STATUS_RIGHT
)
1589 key
= KEYC_DOUBLECLICK8_STATUS_RIGHT
;
1590 if (where
== STATUS_DEFAULT
)
1591 key
= KEYC_DOUBLECLICK8_STATUS_DEFAULT
;
1592 if (where
== BORDER
)
1593 key
= KEYC_DOUBLECLICK8_BORDER
;
1595 case MOUSE_BUTTON_9
:
1597 key
= KEYC_DOUBLECLICK9_PANE
;
1598 if (where
== STATUS
)
1599 key
= KEYC_DOUBLECLICK9_STATUS
;
1600 if (where
== STATUS_LEFT
)
1601 key
= KEYC_DOUBLECLICK9_STATUS_LEFT
;
1602 if (where
== STATUS_RIGHT
)
1603 key
= KEYC_DOUBLECLICK9_STATUS_RIGHT
;
1604 if (where
== STATUS_DEFAULT
)
1605 key
= KEYC_DOUBLECLICK9_STATUS_DEFAULT
;
1606 if (where
== BORDER
)
1607 key
= KEYC_DOUBLECLICK9_BORDER
;
1609 case MOUSE_BUTTON_10
:
1611 key
= KEYC_DOUBLECLICK10_PANE
;
1612 if (where
== STATUS
)
1613 key
= KEYC_DOUBLECLICK10_STATUS
;
1614 if (where
== STATUS_LEFT
)
1615 key
= KEYC_DOUBLECLICK10_STATUS_LEFT
;
1616 if (where
== STATUS_RIGHT
)
1617 key
= KEYC_DOUBLECLICK10_STATUS_RIGHT
;
1618 if (where
== STATUS_DEFAULT
)
1619 key
= KEYC_DOUBLECLICK10_STATUS_DEFAULT
;
1620 if (where
== BORDER
)
1621 key
= KEYC_DOUBLECLICK10_BORDER
;
1623 case MOUSE_BUTTON_11
:
1625 key
= KEYC_DOUBLECLICK11_PANE
;
1626 if (where
== STATUS
)
1627 key
= KEYC_DOUBLECLICK11_STATUS
;
1628 if (where
== STATUS_LEFT
)
1629 key
= KEYC_DOUBLECLICK11_STATUS_LEFT
;
1630 if (where
== STATUS_RIGHT
)
1631 key
= KEYC_DOUBLECLICK11_STATUS_RIGHT
;
1632 if (where
== STATUS_DEFAULT
)
1633 key
= KEYC_DOUBLECLICK11_STATUS_DEFAULT
;
1634 if (where
== BORDER
)
1635 key
= KEYC_DOUBLECLICK11_BORDER
;
1640 switch (MOUSE_BUTTONS(b
)) {
1641 case MOUSE_BUTTON_1
:
1643 key
= KEYC_TRIPLECLICK1_PANE
;
1644 if (where
== STATUS
)
1645 key
= KEYC_TRIPLECLICK1_STATUS
;
1646 if (where
== STATUS_LEFT
)
1647 key
= KEYC_TRIPLECLICK1_STATUS_LEFT
;
1648 if (where
== STATUS_RIGHT
)
1649 key
= KEYC_TRIPLECLICK1_STATUS_RIGHT
;
1650 if (where
== STATUS_DEFAULT
)
1651 key
= KEYC_TRIPLECLICK1_STATUS_DEFAULT
;
1652 if (where
== BORDER
)
1653 key
= KEYC_TRIPLECLICK1_BORDER
;
1655 case MOUSE_BUTTON_2
:
1657 key
= KEYC_TRIPLECLICK2_PANE
;
1658 if (where
== STATUS
)
1659 key
= KEYC_TRIPLECLICK2_STATUS
;
1660 if (where
== STATUS_LEFT
)
1661 key
= KEYC_TRIPLECLICK2_STATUS_LEFT
;
1662 if (where
== STATUS_RIGHT
)
1663 key
= KEYC_TRIPLECLICK2_STATUS_RIGHT
;
1664 if (where
== STATUS_DEFAULT
)
1665 key
= KEYC_TRIPLECLICK2_STATUS_DEFAULT
;
1666 if (where
== BORDER
)
1667 key
= KEYC_TRIPLECLICK2_BORDER
;
1669 case MOUSE_BUTTON_3
:
1671 key
= KEYC_TRIPLECLICK3_PANE
;
1672 if (where
== STATUS
)
1673 key
= KEYC_TRIPLECLICK3_STATUS
;
1674 if (where
== STATUS_LEFT
)
1675 key
= KEYC_TRIPLECLICK3_STATUS_LEFT
;
1676 if (where
== STATUS_RIGHT
)
1677 key
= KEYC_TRIPLECLICK3_STATUS_RIGHT
;
1678 if (where
== STATUS_DEFAULT
)
1679 key
= KEYC_TRIPLECLICK3_STATUS_DEFAULT
;
1680 if (where
== BORDER
)
1681 key
= KEYC_TRIPLECLICK3_BORDER
;
1683 case MOUSE_BUTTON_6
:
1685 key
= KEYC_TRIPLECLICK6_PANE
;
1686 if (where
== STATUS
)
1687 key
= KEYC_TRIPLECLICK6_STATUS
;
1688 if (where
== STATUS_LEFT
)
1689 key
= KEYC_TRIPLECLICK6_STATUS_LEFT
;
1690 if (where
== STATUS_RIGHT
)
1691 key
= KEYC_TRIPLECLICK6_STATUS_RIGHT
;
1692 if (where
== STATUS_DEFAULT
)
1693 key
= KEYC_TRIPLECLICK6_STATUS_DEFAULT
;
1694 if (where
== BORDER
)
1695 key
= KEYC_TRIPLECLICK6_BORDER
;
1697 case MOUSE_BUTTON_7
:
1699 key
= KEYC_TRIPLECLICK7_PANE
;
1700 if (where
== STATUS
)
1701 key
= KEYC_TRIPLECLICK7_STATUS
;
1702 if (where
== STATUS_LEFT
)
1703 key
= KEYC_TRIPLECLICK7_STATUS_LEFT
;
1704 if (where
== STATUS_RIGHT
)
1705 key
= KEYC_TRIPLECLICK7_STATUS_RIGHT
;
1706 if (where
== STATUS_DEFAULT
)
1707 key
= KEYC_TRIPLECLICK7_STATUS_DEFAULT
;
1708 if (where
== BORDER
)
1709 key
= KEYC_TRIPLECLICK7_BORDER
;
1711 case MOUSE_BUTTON_8
:
1713 key
= KEYC_TRIPLECLICK8_PANE
;
1714 if (where
== STATUS
)
1715 key
= KEYC_TRIPLECLICK8_STATUS
;
1716 if (where
== STATUS_LEFT
)
1717 key
= KEYC_TRIPLECLICK8_STATUS_LEFT
;
1718 if (where
== STATUS_RIGHT
)
1719 key
= KEYC_TRIPLECLICK8_STATUS_RIGHT
;
1720 if (where
== STATUS_DEFAULT
)
1721 key
= KEYC_TRIPLECLICK8_STATUS_DEFAULT
;
1722 if (where
== BORDER
)
1723 key
= KEYC_TRIPLECLICK8_BORDER
;
1725 case MOUSE_BUTTON_9
:
1727 key
= KEYC_TRIPLECLICK9_PANE
;
1728 if (where
== STATUS
)
1729 key
= KEYC_TRIPLECLICK9_STATUS
;
1730 if (where
== STATUS_LEFT
)
1731 key
= KEYC_TRIPLECLICK9_STATUS_LEFT
;
1732 if (where
== STATUS_RIGHT
)
1733 key
= KEYC_TRIPLECLICK9_STATUS_RIGHT
;
1734 if (where
== STATUS_DEFAULT
)
1735 key
= KEYC_TRIPLECLICK9_STATUS_DEFAULT
;
1736 if (where
== BORDER
)
1737 key
= KEYC_TRIPLECLICK9_BORDER
;
1739 case MOUSE_BUTTON_10
:
1741 key
= KEYC_TRIPLECLICK10_PANE
;
1742 if (where
== STATUS
)
1743 key
= KEYC_TRIPLECLICK10_STATUS
;
1744 if (where
== STATUS_LEFT
)
1745 key
= KEYC_TRIPLECLICK10_STATUS_LEFT
;
1746 if (where
== STATUS_RIGHT
)
1747 key
= KEYC_TRIPLECLICK10_STATUS_RIGHT
;
1748 if (where
== STATUS_DEFAULT
)
1749 key
= KEYC_TRIPLECLICK10_STATUS_DEFAULT
;
1750 if (where
== BORDER
)
1751 key
= KEYC_TRIPLECLICK10_BORDER
;
1753 case MOUSE_BUTTON_11
:
1755 key
= KEYC_TRIPLECLICK11_PANE
;
1756 if (where
== STATUS
)
1757 key
= KEYC_TRIPLECLICK11_STATUS
;
1758 if (where
== STATUS_LEFT
)
1759 key
= KEYC_TRIPLECLICK11_STATUS_LEFT
;
1760 if (where
== STATUS_RIGHT
)
1761 key
= KEYC_TRIPLECLICK11_STATUS_RIGHT
;
1762 if (where
== STATUS_DEFAULT
)
1763 key
= KEYC_TRIPLECLICK11_STATUS_DEFAULT
;
1764 if (where
== BORDER
)
1765 key
= KEYC_TRIPLECLICK11_BORDER
;
1770 if (key
== KEYC_UNKNOWN
)
1771 return (KEYC_UNKNOWN
);
1774 /* Apply modifiers if any. */
1775 if (b
& MOUSE_MASK_META
)
1777 if (b
& MOUSE_MASK_CTRL
)
1779 if (b
& MOUSE_MASK_SHIFT
)
1782 if (log_get_level() != 0)
1783 log_debug("mouse key is %s", key_string_lookup_key (key
, 1));
1787 /* Is this a bracket paste key? */
1789 server_client_is_bracket_pasting(struct client
*c
, key_code key
)
1791 if (key
== KEYC_PASTE_START
) {
1792 c
->flags
|= CLIENT_BRACKETPASTING
;
1793 log_debug("%s: bracket paste on", c
->name
);
1797 if (key
== KEYC_PASTE_END
) {
1798 c
->flags
&= ~CLIENT_BRACKETPASTING
;
1799 log_debug("%s: bracket paste off", c
->name
);
1803 return !!(c
->flags
& CLIENT_BRACKETPASTING
);
1806 /* Is this fast enough to probably be a paste? */
1808 server_client_assume_paste(struct session
*s
)
1813 if ((t
= options_get_number(s
->options
, "assume-paste-time")) == 0)
1816 timersub(&s
->activity_time
, &s
->last_activity_time
, &tv
);
1817 if (tv
.tv_sec
== 0 && tv
.tv_usec
< t
* 1000) {
1818 log_debug("session %s pasting (flag %d)", s
->name
,
1819 !!(s
->flags
& SESSION_PASTING
));
1820 if (s
->flags
& SESSION_PASTING
)
1822 s
->flags
|= SESSION_PASTING
;
1825 log_debug("session %s not pasting", s
->name
);
1826 s
->flags
&= ~SESSION_PASTING
;
1830 /* Has the latest client changed? */
1832 server_client_update_latest(struct client
*c
)
1836 if (c
->session
== NULL
)
1838 w
= c
->session
->curw
->window
;
1844 if (options_get_number(w
->options
, "window-size") == WINDOW_SIZE_LATEST
)
1845 recalculate_size(w
, 0);
1847 notify_client("client-active", c
);
1851 * Handle data key input from client. This owns and can modify the key event it
1852 * is given and is responsible for freeing it.
1854 static enum cmd_retval
1855 server_client_key_callback(struct cmdq_item
*item
, void *data
)
1857 struct client
*c
= cmdq_get_client(item
);
1858 struct key_event
*event
= data
;
1859 key_code key
= event
->key
;
1860 struct mouse_event
*m
= &event
->m
;
1861 struct session
*s
= c
->session
;
1863 struct window_pane
*wp
;
1864 struct window_mode_entry
*wme
;
1866 struct key_table
*table
, *first
;
1867 struct key_binding
*bd
;
1868 int xtimeout
, flags
;
1869 struct cmd_find_state fs
;
1872 /* Check the client is good to accept input. */
1873 if (s
== NULL
|| (c
->flags
& CLIENT_UNATTACHEDFLAGS
))
1877 /* Update the activity timer. */
1878 if (gettimeofday(&c
->activity_time
, NULL
) != 0)
1879 fatal("gettimeofday failed");
1880 session_update_activity(s
, &c
->activity_time
);
1882 /* Check for mouse keys. */
1884 if (key
== KEYC_MOUSE
|| key
== KEYC_DOUBLECLICK
) {
1885 if (c
->flags
& CLIENT_READONLY
)
1887 key
= server_client_check_mouse(c
, event
);
1888 if (key
== KEYC_UNKNOWN
)
1895 * Mouse drag is in progress, so fire the callback (now that
1896 * the mouse event is valid).
1898 if ((key
& KEYC_MASK_KEY
) == KEYC_DRAGGING
) {
1899 c
->tty
.mouse_drag_update(c
, m
);
1905 /* Find affected pane. */
1906 if (!KEYC_IS_MOUSE(key
) || cmd_find_from_mouse(&fs
, m
, 0) != 0)
1907 cmd_find_from_client(&fs
, c
, 0);
1910 /* Forward mouse keys if disabled. */
1911 if (KEYC_IS_MOUSE(key
) && !options_get_number(s
->options
, "mouse"))
1914 /* Forward if bracket pasting. */
1915 if (server_client_is_bracket_pasting(c
, key
))
1918 /* Treat everything as a regular key when pasting is detected. */
1919 if (!KEYC_IS_MOUSE(key
) &&
1920 (~key
& KEYC_SENT
) &&
1921 server_client_assume_paste(s
))
1925 * Work out the current key table. If the pane is in a mode, use
1926 * the mode table instead of the default key table.
1928 if (server_client_is_default_key_table(c
, c
->keytable
) &&
1930 (wme
= TAILQ_FIRST(&wp
->modes
)) != NULL
&&
1931 wme
->mode
->key_table
!= NULL
)
1932 table
= key_bindings_get_table(wme
->mode
->key_table(wme
), 1);
1934 table
= c
->keytable
;
1939 * The prefix always takes precedence and forces a switch to the prefix
1940 * table, unless we are already there.
1942 key0
= (key
& (KEYC_MASK_KEY
|KEYC_MASK_MODIFIERS
));
1943 if ((key0
== (key_code
)options_get_number(s
->options
, "prefix") ||
1944 key0
== (key_code
)options_get_number(s
->options
, "prefix2")) &&
1945 strcmp(table
->name
, "prefix") != 0) {
1946 server_client_set_key_table(c
, "prefix");
1947 server_status_client(c
);
1953 /* Log key table. */
1955 log_debug("key table %s (no pane)", table
->name
);
1957 log_debug("key table %s (pane %%%u)", table
->name
, wp
->id
);
1958 if (c
->flags
& CLIENT_REPEAT
)
1959 log_debug("currently repeating");
1961 /* Try to see if there is a key binding in the current table. */
1962 bd
= key_bindings_get(table
, key0
);
1965 * Key was matched in this table. If currently repeating but a
1966 * non-repeating binding was found, stop repeating and try
1967 * again in the root table.
1969 if ((c
->flags
& CLIENT_REPEAT
) &&
1970 (~bd
->flags
& KEY_BINDING_REPEAT
)) {
1971 log_debug("found in key table %s (not repeating)",
1973 server_client_set_key_table(c
, NULL
);
1974 first
= table
= c
->keytable
;
1975 c
->flags
&= ~CLIENT_REPEAT
;
1976 server_status_client(c
);
1979 log_debug("found in key table %s", table
->name
);
1982 * Take a reference to this table to make sure the key binding
1983 * doesn't disappear.
1985 table
->references
++;
1988 * If this is a repeating key, start the timer. Otherwise reset
1989 * the client back to the root table.
1991 xtimeout
= options_get_number(s
->options
, "repeat-time");
1992 if (xtimeout
!= 0 && (bd
->flags
& KEY_BINDING_REPEAT
)) {
1993 c
->flags
|= CLIENT_REPEAT
;
1995 tv
.tv_sec
= xtimeout
/ 1000;
1996 tv
.tv_usec
= (xtimeout
% 1000) * 1000L;
1997 evtimer_del(&c
->repeat_timer
);
1998 evtimer_add(&c
->repeat_timer
, &tv
);
2000 c
->flags
&= ~CLIENT_REPEAT
;
2001 server_client_set_key_table(c
, NULL
);
2003 server_status_client(c
);
2005 /* Execute the key binding. */
2006 key_bindings_dispatch(bd
, item
, c
, event
, &fs
);
2007 key_bindings_unref_table(table
);
2012 * No match, try the ANY key.
2014 if (key0
!= KEYC_ANY
) {
2020 * No match in this table. If not in the root table or if repeating,
2021 * switch the client back to the root table and try again.
2023 log_debug("not found in key table %s", table
->name
);
2024 if (!server_client_is_default_key_table(c
, table
) ||
2025 (c
->flags
& CLIENT_REPEAT
)) {
2026 log_debug("trying in root table");
2027 server_client_set_key_table(c
, NULL
);
2028 table
= c
->keytable
;
2029 if (c
->flags
& CLIENT_REPEAT
)
2031 c
->flags
&= ~CLIENT_REPEAT
;
2032 server_status_client(c
);
2037 * No match in the root table either. If this wasn't the first table
2038 * tried, don't pass the key to the pane.
2040 if (first
!= table
&& (~flags
& CLIENT_REPEAT
)) {
2041 server_client_set_key_table(c
, NULL
);
2042 server_status_client(c
);
2047 if (c
->flags
& CLIENT_READONLY
)
2050 window_pane_key(wp
, c
, s
, wl
, key
, m
);
2053 if (s
!= NULL
&& key
!= KEYC_FOCUS_OUT
)
2054 server_client_update_latest(c
);
2056 return (CMD_RETURN_NORMAL
);
2059 /* Handle a key event. */
2061 server_client_handle_key(struct client
*c
, struct key_event
*event
)
2063 struct session
*s
= c
->session
;
2064 struct cmdq_item
*item
;
2066 /* Check the client is good to accept input. */
2067 if (s
== NULL
|| (c
->flags
& CLIENT_UNATTACHEDFLAGS
))
2071 * Key presses in overlay mode and the command prompt are a special
2072 * case. The queue might be blocked so they need to be processed
2073 * immediately rather than queued.
2075 if (~c
->flags
& CLIENT_READONLY
) {
2076 if (c
->message_string
!= NULL
) {
2077 if (c
->message_ignore_keys
)
2079 status_message_clear(c
);
2081 if (c
->overlay_key
!= NULL
) {
2082 switch (c
->overlay_key(c
, c
->overlay_data
, event
)) {
2086 server_client_clear_overlay(c
);
2090 server_client_clear_overlay(c
);
2091 if (c
->prompt_string
!= NULL
) {
2092 if (status_prompt_key(c
, event
->key
) == 0)
2098 * Add the key to the queue so it happens after any commands queued by
2101 item
= cmdq_get_callback(server_client_key_callback
, event
);
2102 cmdq_append(c
, item
);
2106 /* Client functions that need to happen every loop. */
2108 server_client_loop(void)
2112 struct window_pane
*wp
;
2114 /* Check for window resize. This is done before redrawing. */
2115 RB_FOREACH(w
, windows
, &windows
)
2116 server_client_check_window_resize(w
);
2118 /* Check clients. */
2119 TAILQ_FOREACH(c
, &clients
, entry
) {
2120 server_client_check_exit(c
);
2121 if (c
->session
!= NULL
) {
2122 server_client_check_modes(c
);
2123 server_client_check_redraw(c
);
2124 server_client_reset_state(c
);
2129 * Any windows will have been redrawn as part of clients, so clear
2132 RB_FOREACH(w
, windows
, &windows
) {
2133 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
2135 server_client_check_pane_resize(wp
);
2136 server_client_check_pane_buffer(wp
);
2138 wp
->flags
&= ~PANE_REDRAW
;
2140 check_window_name(w
);
2144 /* Check if window needs to be resized. */
2146 server_client_check_window_resize(struct window
*w
)
2150 if (~w
->flags
& WINDOW_RESIZE
)
2153 TAILQ_FOREACH(wl
, &w
->winlinks
, wentry
) {
2154 if (wl
->session
->attached
!= 0 && wl
->session
->curw
== wl
)
2160 log_debug("%s: resizing window @%u", __func__
, w
->id
);
2161 resize_window(w
, w
->new_sx
, w
->new_sy
, w
->new_xpixel
, w
->new_ypixel
);
2164 /* Resize timer event. */
2166 server_client_resize_timer(__unused
int fd
, __unused
short events
, void *data
)
2168 struct window_pane
*wp
= data
;
2170 log_debug("%s: %%%u resize timer expired", __func__
, wp
->id
);
2171 evtimer_del(&wp
->resize_timer
);
2174 /* Check if pane should be resized. */
2176 server_client_check_pane_resize(struct window_pane
*wp
)
2178 struct window_pane_resize
*r
;
2179 struct window_pane_resize
*r1
;
2180 struct window_pane_resize
*first
;
2181 struct window_pane_resize
*last
;
2182 struct timeval tv
= { .tv_usec
= 250000 };
2184 if (TAILQ_EMPTY(&wp
->resize_queue
))
2187 if (!event_initialized(&wp
->resize_timer
))
2188 evtimer_set(&wp
->resize_timer
, server_client_resize_timer
, wp
);
2189 if (evtimer_pending(&wp
->resize_timer
, NULL
))
2192 log_debug("%s: %%%u needs to be resized", __func__
, wp
->id
);
2193 TAILQ_FOREACH(r
, &wp
->resize_queue
, entry
) {
2194 log_debug("queued resize: %ux%u -> %ux%u", r
->osx
, r
->osy
,
2199 * There are three cases that matter:
2201 * - Only one resize. It can just be applied.
2203 * - Multiple resizes and the ending size is different from the
2204 * starting size. We can discard all resizes except the most recent.
2206 * - Multiple resizes and the ending size is the same as the starting
2207 * size. We must resize at least twice to force the application to
2208 * redraw. So apply the first and leave the last on the queue for
2211 first
= TAILQ_FIRST(&wp
->resize_queue
);
2212 last
= TAILQ_LAST(&wp
->resize_queue
, window_pane_resizes
);
2213 if (first
== last
) {
2214 /* Only one resize. */
2215 window_pane_send_resize(wp
, first
->sx
, first
->sy
);
2216 TAILQ_REMOVE(&wp
->resize_queue
, first
, entry
);
2218 } else if (last
->sx
!= first
->osx
|| last
->sy
!= first
->osy
) {
2219 /* Multiple resizes ending up with a different size. */
2220 window_pane_send_resize(wp
, last
->sx
, last
->sy
);
2221 TAILQ_FOREACH_SAFE(r
, &wp
->resize_queue
, entry
, r1
) {
2222 TAILQ_REMOVE(&wp
->resize_queue
, r
, entry
);
2227 * Multiple resizes ending up with the same size. There will
2228 * not be more than one to the same size in succession so we
2229 * can just use the last-but-one on the list and leave the last
2230 * for later. We reduce the time until the next check to avoid
2231 * a long delay between the resizes.
2233 r
= TAILQ_PREV(last
, window_pane_resizes
, entry
);
2234 window_pane_send_resize(wp
, r
->sx
, r
->sy
);
2235 TAILQ_FOREACH_SAFE(r
, &wp
->resize_queue
, entry
, r1
) {
2238 TAILQ_REMOVE(&wp
->resize_queue
, r
, entry
);
2243 evtimer_add(&wp
->resize_timer
, &tv
);
2246 /* Check pane buffer size. */
2248 server_client_check_pane_buffer(struct window_pane
*wp
)
2250 struct evbuffer
*evb
= wp
->event
->input
;
2253 struct window_pane_offset
*wpo
;
2255 u_int attached_clients
= 0;
2259 * Work out the minimum used size. This is the most that can be removed
2262 minimum
= wp
->offset
.used
;
2263 if (wp
->pipe_fd
!= -1 && wp
->pipe_offset
.used
< minimum
)
2264 minimum
= wp
->pipe_offset
.used
;
2265 TAILQ_FOREACH(c
, &clients
, entry
) {
2266 if (c
->session
== NULL
)
2270 if (~c
->flags
& CLIENT_CONTROL
) {
2274 wpo
= control_pane_offset(c
, wp
, &flag
);
2283 window_pane_get_new_data(wp
, wpo
, &new_size
);
2284 log_debug("%s: %s has %zu bytes used and %zu left for %%%u",
2285 __func__
, c
->name
, wpo
->used
- wp
->base_offset
, new_size
,
2287 if (wpo
->used
< minimum
)
2288 minimum
= wpo
->used
;
2290 if (attached_clients
== 0)
2292 minimum
-= wp
->base_offset
;
2296 /* Drain the buffer. */
2297 log_debug("%s: %%%u has %zu minimum (of %zu) bytes used", __func__
,
2298 wp
->id
, minimum
, EVBUFFER_LENGTH(evb
));
2299 evbuffer_drain(evb
, minimum
);
2302 * Adjust the base offset. If it would roll over, all the offsets into
2303 * the buffer need to be adjusted.
2305 if (wp
->base_offset
> SIZE_MAX
- minimum
) {
2306 log_debug("%s: %%%u base offset has wrapped", __func__
, wp
->id
);
2307 wp
->offset
.used
-= wp
->base_offset
;
2308 if (wp
->pipe_fd
!= -1)
2309 wp
->pipe_offset
.used
-= wp
->base_offset
;
2310 TAILQ_FOREACH(c
, &clients
, entry
) {
2311 if (c
->session
== NULL
|| (~c
->flags
& CLIENT_CONTROL
))
2313 wpo
= control_pane_offset(c
, wp
, &flag
);
2314 if (wpo
!= NULL
&& !flag
)
2315 wpo
->used
-= wp
->base_offset
;
2317 wp
->base_offset
= minimum
;
2319 wp
->base_offset
+= minimum
;
2323 * If there is data remaining, and there are no clients able to consume
2324 * it, do not read any more. This is true when there are attached
2325 * clients, all of which are control clients which are not able to
2326 * accept any more data.
2328 log_debug("%s: pane %%%u is %s", __func__
, wp
->id
, off
? "off" : "on");
2330 bufferevent_disable(wp
->event
, EV_READ
);
2332 bufferevent_enable(wp
->event
, EV_READ
);
2336 * Update cursor position and mode settings. The scroll region and attributes
2337 * are cleared when idle (waiting for an event) as this is the most likely time
2338 * a user may interrupt tmux, for example with ~^Z in ssh(1). This is a
2339 * compromise between excessive resets and likelihood of an interrupt.
2341 * tty_region/tty_reset/tty_update_mode already take care of not resetting
2342 * things that are already in their default state.
2345 server_client_reset_state(struct client
*c
)
2347 struct tty
*tty
= &c
->tty
;
2348 struct window
*w
= c
->session
->curw
->window
;
2349 struct window_pane
*wp
= server_client_get_pane(c
), *loop
;
2350 struct screen
*s
= NULL
;
2351 struct options
*oo
= c
->session
->options
;
2352 int mode
= 0, cursor
, flags
, n
;
2353 u_int cx
= 0, cy
= 0, ox
, oy
, sx
, sy
;
2355 if (c
->flags
& (CLIENT_CONTROL
|CLIENT_SUSPENDED
))
2358 /* Disable the block flag. */
2359 flags
= (tty
->flags
& TTY_BLOCK
);
2360 tty
->flags
&= ~TTY_BLOCK
;
2362 /* Get mode from overlay if any, else from screen. */
2363 if (c
->overlay_draw
!= NULL
) {
2364 if (c
->overlay_mode
!= NULL
)
2365 s
= c
->overlay_mode(c
, c
->overlay_data
, &cx
, &cy
);
2370 if (log_get_level() != 0) {
2371 log_debug("%s: client %s mode %s", __func__
, c
->name
,
2372 screen_mode_to_string(mode
));
2375 /* Reset region and margin. */
2376 tty_region_off(tty
);
2377 tty_margin_off(tty
);
2379 /* Move cursor to pane cursor and offset. */
2380 if (c
->prompt_string
!= NULL
) {
2381 n
= options_get_number(c
->session
->options
, "status-position");
2385 n
= status_line_size(c
);
2391 cx
= c
->prompt_cursor
;
2392 mode
&= ~MODE_CURSOR
;
2393 } else if (c
->overlay_draw
== NULL
) {
2395 tty_window_offset(tty
, &ox
, &oy
, &sx
, &sy
);
2396 if (wp
->xoff
+ s
->cx
>= ox
&& wp
->xoff
+ s
->cx
<= ox
+ sx
&&
2397 wp
->yoff
+ s
->cy
>= oy
&& wp
->yoff
+ s
->cy
<= oy
+ sy
) {
2400 cx
= wp
->xoff
+ s
->cx
- ox
;
2401 cy
= wp
->yoff
+ s
->cy
- oy
;
2403 if (status_at_line(c
) == 0)
2404 cy
+= status_line_size(c
);
2407 mode
&= ~MODE_CURSOR
;
2409 log_debug("%s: cursor to %u,%u", __func__
, cx
, cy
);
2410 tty_cursor(tty
, cx
, cy
);
2413 * Set mouse mode if requested. To support dragging, always use button
2416 if (options_get_number(oo
, "mouse")) {
2417 if (c
->overlay_draw
== NULL
) {
2418 mode
&= ~ALL_MOUSE_MODES
;
2419 TAILQ_FOREACH(loop
, &w
->panes
, entry
) {
2420 if (loop
->screen
->mode
& MODE_MOUSE_ALL
)
2421 mode
|= MODE_MOUSE_ALL
;
2424 if (~mode
& MODE_MOUSE_ALL
)
2425 mode
|= MODE_MOUSE_BUTTON
;
2428 /* Clear bracketed paste mode if at the prompt. */
2429 if (c
->overlay_draw
== NULL
&& c
->prompt_string
!= NULL
)
2430 mode
&= ~MODE_BRACKETPASTE
;
2432 /* Set the terminal mode and reset attributes. */
2433 tty_update_mode(tty
, mode
, s
);
2436 /* All writing must be done, send a sync end (if it was started). */
2438 tty
->flags
|= flags
;
2441 /* Repeat time callback. */
2443 server_client_repeat_timer(__unused
int fd
, __unused
short events
, void *data
)
2445 struct client
*c
= data
;
2447 if (c
->flags
& CLIENT_REPEAT
) {
2448 server_client_set_key_table(c
, NULL
);
2449 c
->flags
&= ~CLIENT_REPEAT
;
2450 server_status_client(c
);
2454 /* Double-click callback. */
2456 server_client_click_timer(__unused
int fd
, __unused
short events
, void *data
)
2458 struct client
*c
= data
;
2459 struct key_event
*event
;
2461 log_debug("click timer expired");
2463 if (c
->flags
& CLIENT_TRIPLECLICK
) {
2465 * Waiting for a third click that hasn't happened, so this must
2466 * have been a double click.
2468 event
= xmalloc(sizeof *event
);
2469 event
->key
= KEYC_DOUBLECLICK
;
2470 memcpy(&event
->m
, &c
->click_event
, sizeof event
->m
);
2471 if (!server_client_handle_key(c
, event
))
2474 c
->flags
&= ~(CLIENT_DOUBLECLICK
|CLIENT_TRIPLECLICK
);
2477 /* Check if client should be exited. */
2479 server_client_check_exit(struct client
*c
)
2481 struct client_file
*cf
;
2482 const char *name
= c
->exit_session
;
2486 if (c
->flags
& (CLIENT_DEAD
|CLIENT_EXITED
))
2488 if (~c
->flags
& CLIENT_EXIT
)
2491 if (c
->flags
& CLIENT_CONTROL
) {
2493 if (!control_all_done(c
))
2496 RB_FOREACH(cf
, client_files
, &c
->files
) {
2497 if (EVBUFFER_LENGTH(cf
->buffer
) != 0)
2500 c
->flags
|= CLIENT_EXITED
;
2502 switch (c
->exit_type
) {
2503 case CLIENT_EXIT_RETURN
:
2504 if (c
->exit_message
!= NULL
)
2505 msize
= strlen(c
->exit_message
) + 1;
2508 size
= (sizeof c
->retval
) + msize
;
2509 data
= xmalloc(size
);
2510 memcpy(data
, &c
->retval
, sizeof c
->retval
);
2511 if (c
->exit_message
!= NULL
)
2512 memcpy(data
+ sizeof c
->retval
, c
->exit_message
, msize
);
2513 proc_send(c
->peer
, MSG_EXIT
, -1, data
, size
);
2516 case CLIENT_EXIT_SHUTDOWN
:
2517 proc_send(c
->peer
, MSG_SHUTDOWN
, -1, NULL
, 0);
2519 case CLIENT_EXIT_DETACH
:
2520 proc_send(c
->peer
, c
->exit_msgtype
, -1, name
, strlen(name
) + 1);
2523 free(c
->exit_session
);
2524 free(c
->exit_message
);
2527 /* Redraw timer callback. */
2529 server_client_redraw_timer(__unused
int fd
, __unused
short events
,
2530 __unused
void *data
)
2532 log_debug("redraw timer fired");
2536 * Check if modes need to be updated. Only modes in the current window are
2537 * updated and it is done when the status line is redrawn.
2540 server_client_check_modes(struct client
*c
)
2542 struct window
*w
= c
->session
->curw
->window
;
2543 struct window_pane
*wp
;
2544 struct window_mode_entry
*wme
;
2546 if (c
->flags
& (CLIENT_CONTROL
|CLIENT_SUSPENDED
))
2548 if (~c
->flags
& CLIENT_REDRAWSTATUS
)
2550 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
2551 wme
= TAILQ_FIRST(&wp
->modes
);
2552 if (wme
!= NULL
&& wme
->mode
->update
!= NULL
)
2553 wme
->mode
->update(wme
);
2557 /* Check for client redraws. */
2559 server_client_check_redraw(struct client
*c
)
2561 struct session
*s
= c
->session
;
2562 struct tty
*tty
= &c
->tty
;
2563 struct window
*w
= c
->session
->curw
->window
;
2564 struct window_pane
*wp
;
2565 int needed
, flags
, mode
= tty
->mode
, new_flags
= 0;
2568 struct timeval tv
= { .tv_usec
= 1000 };
2569 static struct event ev
;
2572 if (c
->flags
& (CLIENT_CONTROL
|CLIENT_SUSPENDED
))
2574 if (c
->flags
& CLIENT_ALLREDRAWFLAGS
) {
2575 log_debug("%s: redraw%s%s%s%s%s", c
->name
,
2576 (c
->flags
& CLIENT_REDRAWWINDOW
) ? " window" : "",
2577 (c
->flags
& CLIENT_REDRAWSTATUS
) ? " status" : "",
2578 (c
->flags
& CLIENT_REDRAWBORDERS
) ? " borders" : "",
2579 (c
->flags
& CLIENT_REDRAWOVERLAY
) ? " overlay" : "",
2580 (c
->flags
& CLIENT_REDRAWPANES
) ? " panes" : "");
2584 * If there is outstanding data, defer the redraw until it has been
2585 * consumed. We can just add a timer to get out of the event loop and
2589 if (c
->flags
& CLIENT_ALLREDRAWFLAGS
)
2592 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
2593 if (wp
->flags
& PANE_REDRAW
) {
2599 new_flags
|= CLIENT_REDRAWPANES
;
2601 if (needed
&& (left
= EVBUFFER_LENGTH(tty
->out
)) != 0) {
2602 log_debug("%s: redraw deferred (%zu left)", c
->name
, left
);
2603 if (!evtimer_initialized(&ev
))
2604 evtimer_set(&ev
, server_client_redraw_timer
, NULL
);
2605 if (!evtimer_pending(&ev
, NULL
)) {
2606 log_debug("redraw timer started");
2607 evtimer_add(&ev
, &tv
);
2610 if (~c
->flags
& CLIENT_REDRAWWINDOW
) {
2611 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
2612 if (wp
->flags
& PANE_REDRAW
) {
2613 log_debug("%s: pane %%%u needs redraw",
2615 c
->redraw_panes
|= (1 << bit
);
2619 * If more that 64 panes, give up and
2620 * just redraw the window.
2622 new_flags
&= CLIENT_REDRAWPANES
;
2623 new_flags
|= CLIENT_REDRAWWINDOW
;
2627 if (c
->redraw_panes
!= 0)
2628 c
->flags
|= CLIENT_REDRAWPANES
;
2630 c
->flags
|= new_flags
;
2633 log_debug("%s: redraw needed", c
->name
);
2635 flags
= tty
->flags
& (TTY_BLOCK
|TTY_FREEZE
|TTY_NOCURSOR
);
2636 tty
->flags
= (tty
->flags
& ~(TTY_BLOCK
|TTY_FREEZE
))|TTY_NOCURSOR
;
2638 if (~c
->flags
& CLIENT_REDRAWWINDOW
) {
2640 * If not redrawing the entire window, check whether each pane
2641 * needs to be redrawn.
2643 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
2645 if (wp
->flags
& PANE_REDRAW
)
2647 else if (c
->flags
& CLIENT_REDRAWPANES
)
2648 redraw
= !!(c
->redraw_panes
& (1 << bit
));
2652 log_debug("%s: redrawing pane %%%u", __func__
, wp
->id
);
2653 screen_redraw_pane(c
, wp
);
2655 c
->redraw_panes
= 0;
2656 c
->flags
&= ~CLIENT_REDRAWPANES
;
2659 if (c
->flags
& CLIENT_ALLREDRAWFLAGS
) {
2660 if (options_get_number(s
->options
, "set-titles")) {
2661 server_client_set_title(c
);
2662 server_client_set_path(c
);
2664 screen_redraw_screen(c
);
2667 tty
->flags
= (tty
->flags
& ~TTY_NOCURSOR
)|(flags
& TTY_NOCURSOR
);
2668 tty_update_mode(tty
, mode
, NULL
);
2669 tty
->flags
= (tty
->flags
& ~(TTY_BLOCK
|TTY_FREEZE
|TTY_NOCURSOR
))|flags
;
2671 c
->flags
&= ~(CLIENT_ALLREDRAWFLAGS
|CLIENT_STATUSFORCE
);
2675 * We would have deferred the redraw unless the output buffer
2676 * was empty, so we can record how many bytes the redraw
2679 c
->redraw
= EVBUFFER_LENGTH(tty
->out
);
2680 log_debug("%s: redraw added %zu bytes", c
->name
, c
->redraw
);
2684 /* Set client title. */
2686 server_client_set_title(struct client
*c
)
2688 struct session
*s
= c
->session
;
2689 const char *template;
2691 struct format_tree
*ft
;
2693 template = options_get_string(s
->options
, "set-titles-string");
2695 ft
= format_create(c
, NULL
, FORMAT_NONE
, 0);
2696 format_defaults(ft
, c
, NULL
, NULL
, NULL
);
2698 title
= format_expand_time(ft
, template);
2699 if (c
->title
== NULL
|| strcmp(title
, c
->title
) != 0) {
2701 c
->title
= xstrdup(title
);
2702 tty_set_title(&c
->tty
, c
->title
);
2709 /* Set client path. */
2711 server_client_set_path(struct client
*c
)
2713 struct session
*s
= c
->session
;
2716 if (s
->curw
== NULL
)
2718 if (s
->curw
->window
->active
->base
.path
== NULL
)
2721 path
= s
->curw
->window
->active
->base
.path
;
2722 if (c
->path
== NULL
|| strcmp(path
, c
->path
) != 0) {
2724 c
->path
= xstrdup(path
);
2725 tty_set_path(&c
->tty
, c
->path
);
2729 /* Dispatch message from client. */
2731 server_client_dispatch(struct imsg
*imsg
, void *arg
)
2733 struct client
*c
= arg
;
2737 if (c
->flags
& CLIENT_DEAD
)
2741 server_client_lost(c
);
2745 datalen
= imsg
->hdr
.len
- IMSG_HEADER_SIZE
;
2747 switch (imsg
->hdr
.type
) {
2748 case MSG_IDENTIFY_CLIENTPID
:
2749 case MSG_IDENTIFY_CWD
:
2750 case MSG_IDENTIFY_ENVIRON
:
2751 case MSG_IDENTIFY_FEATURES
:
2752 case MSG_IDENTIFY_FLAGS
:
2753 case MSG_IDENTIFY_LONGFLAGS
:
2754 case MSG_IDENTIFY_STDIN
:
2755 case MSG_IDENTIFY_STDOUT
:
2756 case MSG_IDENTIFY_TERM
:
2757 case MSG_IDENTIFY_TERMINFO
:
2758 case MSG_IDENTIFY_TTYNAME
:
2759 case MSG_IDENTIFY_DONE
:
2760 server_client_dispatch_identify(c
, imsg
);
2763 server_client_dispatch_command(c
, imsg
);
2767 fatalx("bad MSG_RESIZE size");
2769 if (c
->flags
& CLIENT_CONTROL
)
2771 server_client_update_latest(c
);
2772 tty_resize(&c
->tty
);
2773 tty_repeat_requests(&c
->tty
);
2774 recalculate_sizes();
2775 if (c
->overlay_resize
== NULL
)
2776 server_client_clear_overlay(c
);
2778 c
->overlay_resize(c
, c
->overlay_data
);
2779 server_redraw_client(c
);
2780 if (c
->session
!= NULL
)
2781 notify_client("client-resized", c
);
2785 fatalx("bad MSG_EXITING size");
2786 server_client_set_session(c
, NULL
);
2787 recalculate_sizes();
2789 proc_send(c
->peer
, MSG_EXITED
, -1, NULL
, 0);
2794 fatalx("bad MSG_WAKEUP size");
2796 if (!(c
->flags
& CLIENT_SUSPENDED
))
2798 c
->flags
&= ~CLIENT_SUSPENDED
;
2800 if (c
->fd
== -1 || c
->session
== NULL
) /* exited already */
2804 if (gettimeofday(&c
->activity_time
, NULL
) != 0)
2805 fatal("gettimeofday failed");
2807 tty_start_tty(&c
->tty
);
2808 server_redraw_client(c
);
2809 recalculate_sizes();
2812 session_update_activity(s
, &c
->activity_time
);
2816 fatalx("bad MSG_SHELL size");
2818 server_client_dispatch_shell(c
);
2820 case MSG_WRITE_READY
:
2821 file_write_ready(&c
->files
, imsg
);
2824 file_read_data(&c
->files
, imsg
);
2827 file_read_done(&c
->files
, imsg
);
2832 /* Callback when command is not allowed. */
2833 static enum cmd_retval
2834 server_client_read_only(struct cmdq_item
*item
, __unused
void *data
)
2836 cmdq_error(item
, "client is read-only");
2837 return (CMD_RETURN_ERROR
);
2840 /* Callback when command is done. */
2841 static enum cmd_retval
2842 server_client_command_done(struct cmdq_item
*item
, __unused
void *data
)
2844 struct client
*c
= cmdq_get_client(item
);
2846 if (~c
->flags
& CLIENT_ATTACHED
)
2847 c
->flags
|= CLIENT_EXIT
;
2848 else if (~c
->flags
& CLIENT_EXIT
) {
2849 if (c
->flags
& CLIENT_CONTROL
)
2851 tty_send_requests(&c
->tty
);
2853 return (CMD_RETURN_NORMAL
);
2856 /* Handle command message. */
2858 server_client_dispatch_command(struct client
*c
, struct imsg
*imsg
)
2860 struct msg_command data
;
2864 char **argv
, *cause
;
2865 struct cmd_parse_result
*pr
;
2866 struct args_value
*values
;
2867 struct cmdq_item
*new_item
;
2869 if (c
->flags
& CLIENT_EXIT
)
2872 if (imsg
->hdr
.len
- IMSG_HEADER_SIZE
< sizeof data
)
2873 fatalx("bad MSG_COMMAND size");
2874 memcpy(&data
, imsg
->data
, sizeof data
);
2876 buf
= (char *)imsg
->data
+ sizeof data
;
2877 len
= imsg
->hdr
.len
- IMSG_HEADER_SIZE
- sizeof data
;
2878 if (len
> 0 && buf
[len
- 1] != '\0')
2879 fatalx("bad MSG_COMMAND string");
2882 if (cmd_unpack_argv(buf
, len
, argc
, &argv
) != 0) {
2883 cause
= xstrdup("command too long");
2889 argv
= xcalloc(1, sizeof *argv
);
2890 *argv
= xstrdup("new-session");
2893 values
= args_from_vector(argc
, argv
);
2894 pr
= cmd_parse_from_arguments(values
, argc
, NULL
);
2895 switch (pr
->status
) {
2896 case CMD_PARSE_ERROR
:
2899 case CMD_PARSE_SUCCESS
:
2902 args_free_values(values
, argc
);
2904 cmd_free_argv(argc
, argv
);
2906 if ((c
->flags
& CLIENT_READONLY
) &&
2907 !cmd_list_all_have(pr
->cmdlist
, CMD_READONLY
))
2908 new_item
= cmdq_get_callback(server_client_read_only
, NULL
);
2910 new_item
= cmdq_get_command(pr
->cmdlist
, NULL
);
2911 cmdq_append(c
, new_item
);
2912 cmdq_append(c
, cmdq_get_callback(server_client_command_done
, NULL
));
2914 cmd_list_free(pr
->cmdlist
);
2918 cmd_free_argv(argc
, argv
);
2920 cmdq_append(c
, cmdq_get_error(cause
));
2923 c
->flags
|= CLIENT_EXIT
;
2926 /* Handle identify message. */
2928 server_client_dispatch_identify(struct client
*c
, struct imsg
*imsg
)
2930 const char *data
, *home
;
2936 if (c
->flags
& CLIENT_IDENTIFIED
)
2937 fatalx("out-of-order identify message");
2940 datalen
= imsg
->hdr
.len
- IMSG_HEADER_SIZE
;
2942 switch (imsg
->hdr
.type
) {
2943 case MSG_IDENTIFY_FEATURES
:
2944 if (datalen
!= sizeof feat
)
2945 fatalx("bad MSG_IDENTIFY_FEATURES size");
2946 memcpy(&feat
, data
, sizeof feat
);
2947 c
->term_features
|= feat
;
2948 log_debug("client %p IDENTIFY_FEATURES %s", c
,
2949 tty_get_features(feat
));
2951 case MSG_IDENTIFY_FLAGS
:
2952 if (datalen
!= sizeof flags
)
2953 fatalx("bad MSG_IDENTIFY_FLAGS size");
2954 memcpy(&flags
, data
, sizeof flags
);
2956 log_debug("client %p IDENTIFY_FLAGS %#x", c
, flags
);
2958 case MSG_IDENTIFY_LONGFLAGS
:
2959 if (datalen
!= sizeof longflags
)
2960 fatalx("bad MSG_IDENTIFY_LONGFLAGS size");
2961 memcpy(&longflags
, data
, sizeof longflags
);
2962 c
->flags
|= longflags
;
2963 log_debug("client %p IDENTIFY_LONGFLAGS %#llx", c
,
2964 (unsigned long long)longflags
);
2966 case MSG_IDENTIFY_TERM
:
2967 if (datalen
== 0 || data
[datalen
- 1] != '\0')
2968 fatalx("bad MSG_IDENTIFY_TERM string");
2970 c
->term_name
= xstrdup("unknown");
2972 c
->term_name
= xstrdup(data
);
2973 log_debug("client %p IDENTIFY_TERM %s", c
, data
);
2975 case MSG_IDENTIFY_TERMINFO
:
2976 if (datalen
== 0 || data
[datalen
- 1] != '\0')
2977 fatalx("bad MSG_IDENTIFY_TERMINFO string");
2978 c
->term_caps
= xreallocarray(c
->term_caps
, c
->term_ncaps
+ 1,
2979 sizeof *c
->term_caps
);
2980 c
->term_caps
[c
->term_ncaps
++] = xstrdup(data
);
2981 log_debug("client %p IDENTIFY_TERMINFO %s", c
, data
);
2983 case MSG_IDENTIFY_TTYNAME
:
2984 if (datalen
== 0 || data
[datalen
- 1] != '\0')
2985 fatalx("bad MSG_IDENTIFY_TTYNAME string");
2986 c
->ttyname
= xstrdup(data
);
2987 log_debug("client %p IDENTIFY_TTYNAME %s", c
, data
);
2989 case MSG_IDENTIFY_CWD
:
2990 if (datalen
== 0 || data
[datalen
- 1] != '\0')
2991 fatalx("bad MSG_IDENTIFY_CWD string");
2992 if (access(data
, X_OK
) == 0)
2993 c
->cwd
= xstrdup(data
);
2994 else if ((home
= find_home()) != NULL
)
2995 c
->cwd
= xstrdup(home
);
2997 c
->cwd
= xstrdup("/");
2998 log_debug("client %p IDENTIFY_CWD %s", c
, data
);
3000 case MSG_IDENTIFY_STDIN
:
3002 fatalx("bad MSG_IDENTIFY_STDIN size");
3004 log_debug("client %p IDENTIFY_STDIN %d", c
, imsg
->fd
);
3006 case MSG_IDENTIFY_STDOUT
:
3008 fatalx("bad MSG_IDENTIFY_STDOUT size");
3009 c
->out_fd
= imsg
->fd
;
3010 log_debug("client %p IDENTIFY_STDOUT %d", c
, imsg
->fd
);
3012 case MSG_IDENTIFY_ENVIRON
:
3013 if (datalen
== 0 || data
[datalen
- 1] != '\0')
3014 fatalx("bad MSG_IDENTIFY_ENVIRON string");
3015 if (strchr(data
, '=') != NULL
)
3016 environ_put(c
->environ
, data
, 0);
3017 log_debug("client %p IDENTIFY_ENVIRON %s", c
, data
);
3019 case MSG_IDENTIFY_CLIENTPID
:
3020 if (datalen
!= sizeof c
->pid
)
3021 fatalx("bad MSG_IDENTIFY_CLIENTPID size");
3022 memcpy(&c
->pid
, data
, sizeof c
->pid
);
3023 log_debug("client %p IDENTIFY_CLIENTPID %ld", c
, (long)c
->pid
);
3029 if (imsg
->hdr
.type
!= MSG_IDENTIFY_DONE
)
3031 c
->flags
|= CLIENT_IDENTIFIED
;
3033 if (*c
->ttyname
!= '\0')
3034 name
= xstrdup(c
->ttyname
);
3036 xasprintf(&name
, "client-%ld", (long)c
->pid
);
3038 log_debug("client %p name is %s", c
, c
->name
);
3040 if (c
->flags
& CLIENT_CONTROL
)
3042 else if (c
->fd
!= -1) {
3043 if (tty_init(&c
->tty
, c
) != 0) {
3047 tty_resize(&c
->tty
);
3048 c
->flags
|= CLIENT_TERMINAL
;
3055 * If this is the first client, load configuration files. Any later
3056 * clients are allowed to continue with their command even if the
3057 * config has not been loaded - they might have been run from inside it
3059 if ((~c
->flags
& CLIENT_EXIT
) &&
3061 c
== TAILQ_FIRST(&clients
))
3065 /* Handle shell message. */
3067 server_client_dispatch_shell(struct client
*c
)
3071 shell
= options_get_string(global_s_options
, "default-shell");
3072 if (!checkshell(shell
))
3073 shell
= _PATH_BSHELL
;
3074 proc_send(c
->peer
, MSG_SHELL
, -1, shell
, strlen(shell
) + 1);
3076 proc_kill_peer(c
->peer
);
3079 /* Get client working directory. */
3081 server_client_get_cwd(struct client
*c
, struct session
*s
)
3085 if (!cfg_finished
&& cfg_client
!= NULL
)
3086 return (cfg_client
->cwd
);
3087 if (c
!= NULL
&& c
->session
== NULL
&& c
->cwd
!= NULL
)
3089 if (s
!= NULL
&& s
->cwd
!= NULL
)
3091 if (c
!= NULL
&& (s
= c
->session
) != NULL
&& s
->cwd
!= NULL
)
3093 if ((home
= find_home()) != NULL
)
3098 /* Get control client flags. */
3100 server_client_control_flags(struct client
*c
, const char *next
)
3102 if (strcmp(next
, "pause-after") == 0) {
3104 return (CLIENT_CONTROL_PAUSEAFTER
);
3106 if (sscanf(next
, "pause-after=%u", &c
->pause_age
) == 1) {
3107 c
->pause_age
*= 1000;
3108 return (CLIENT_CONTROL_PAUSEAFTER
);
3110 if (strcmp(next
, "no-output") == 0)
3111 return (CLIENT_CONTROL_NOOUTPUT
);
3112 if (strcmp(next
, "wait-exit") == 0)
3113 return (CLIENT_CONTROL_WAITEXIT
);
3117 /* Set client flags. */
3119 server_client_set_flags(struct client
*c
, const char *flags
)
3121 char *s
, *copy
, *next
;
3125 s
= copy
= xstrdup(flags
);
3126 while ((next
= strsep(&s
, ",")) != NULL
) {
3127 not = (*next
== '!');
3131 if (c
->flags
& CLIENT_CONTROL
)
3132 flag
= server_client_control_flags(c
, next
);
3135 if (strcmp(next
, "read-only") == 0)
3136 flag
= CLIENT_READONLY
;
3137 else if (strcmp(next
, "ignore-size") == 0)
3138 flag
= CLIENT_IGNORESIZE
;
3139 else if (strcmp(next
, "active-pane") == 0)
3140 flag
= CLIENT_ACTIVEPANE
;
3144 log_debug("client %s set flag %s", c
->name
, next
);
3146 if (c
->flags
& CLIENT_READONLY
)
3147 flag
&= ~CLIENT_READONLY
;
3151 if (flag
== CLIENT_CONTROL_NOOUTPUT
)
3152 control_reset_offsets(c
);
3155 proc_send(c
->peer
, MSG_FLAGS
, -1, &c
->flags
, sizeof c
->flags
);
3158 /* Get client flags. This is only flags useful to show to users. */
3160 server_client_get_flags(struct client
*c
)
3166 if (c
->flags
& CLIENT_ATTACHED
)
3167 strlcat(s
, "attached,", sizeof s
);
3168 if (c
->flags
& CLIENT_FOCUSED
)
3169 strlcat(s
, "focused,", sizeof s
);
3170 if (c
->flags
& CLIENT_CONTROL
)
3171 strlcat(s
, "control-mode,", sizeof s
);
3172 if (c
->flags
& CLIENT_IGNORESIZE
)
3173 strlcat(s
, "ignore-size,", sizeof s
);
3174 if (c
->flags
& CLIENT_CONTROL_NOOUTPUT
)
3175 strlcat(s
, "no-output,", sizeof s
);
3176 if (c
->flags
& CLIENT_CONTROL_WAITEXIT
)
3177 strlcat(s
, "wait-exit,", sizeof s
);
3178 if (c
->flags
& CLIENT_CONTROL_PAUSEAFTER
) {
3179 xsnprintf(tmp
, sizeof tmp
, "pause-after=%u,",
3180 c
->pause_age
/ 1000);
3181 strlcat(s
, tmp
, sizeof s
);
3183 if (c
->flags
& CLIENT_READONLY
)
3184 strlcat(s
, "read-only,", sizeof s
);
3185 if (c
->flags
& CLIENT_ACTIVEPANE
)
3186 strlcat(s
, "active-pane,", sizeof s
);
3187 if (c
->flags
& CLIENT_SUSPENDED
)
3188 strlcat(s
, "suspended,", sizeof s
);
3189 if (c
->flags
& CLIENT_UTF8
)
3190 strlcat(s
, "UTF-8,", sizeof s
);
3192 s
[strlen(s
) - 1] = '\0';
3196 /* Get client window. */
3197 struct client_window
*
3198 server_client_get_client_window(struct client
*c
, u_int id
)
3200 struct client_window cw
= { .window
= id
};
3202 return (RB_FIND(client_windows
, &c
->windows
, &cw
));
3205 /* Add client window. */
3206 struct client_window
*
3207 server_client_add_client_window(struct client
*c
, u_int id
)
3209 struct client_window
*cw
;
3211 cw
= server_client_get_client_window(c
, id
);
3213 cw
= xcalloc(1, sizeof *cw
);
3215 RB_INSERT(client_windows
, &c
->windows
, cw
);
3220 /* Get client active pane. */
3221 struct window_pane
*
3222 server_client_get_pane(struct client
*c
)
3224 struct session
*s
= c
->session
;
3225 struct client_window
*cw
;
3230 if (~c
->flags
& CLIENT_ACTIVEPANE
)
3231 return (s
->curw
->window
->active
);
3232 cw
= server_client_get_client_window(c
, s
->curw
->window
->id
);
3234 return (s
->curw
->window
->active
);
3238 /* Set client active pane. */
3240 server_client_set_pane(struct client
*c
, struct window_pane
*wp
)
3242 struct session
*s
= c
->session
;
3243 struct client_window
*cw
;
3248 cw
= server_client_add_client_window(c
, s
->curw
->window
->id
);
3250 log_debug("%s pane now %%%u", c
->name
, wp
->id
);
3253 /* Remove pane from client lists. */
3255 server_client_remove_pane(struct window_pane
*wp
)
3258 struct window
*w
= wp
->window
;
3259 struct client_window
*cw
;
3261 TAILQ_FOREACH(c
, &clients
, entry
) {
3262 cw
= server_client_get_client_window(c
, w
->id
);
3263 if (cw
!= NULL
&& cw
->pane
== wp
) {
3264 RB_REMOVE(client_windows
, &c
->windows
, cw
);
3270 /* Print to a client. */
3272 server_client_print(struct client
*c
, int parse
, struct evbuffer
*evb
)
3274 void *data
= EVBUFFER_DATA(evb
);
3275 size_t size
= EVBUFFER_LENGTH(evb
);
3276 struct window_pane
*wp
;
3277 struct window_mode_entry
*wme
;
3278 char *sanitized
, *msg
, *line
;
3281 utf8_stravisx(&msg
, data
, size
,
3282 VIS_OCTAL
|VIS_CSTYLE
|VIS_NOSLASH
);
3283 log_debug("%s: %s", __func__
, msg
);
3285 msg
= EVBUFFER_DATA(evb
);
3286 if (msg
[size
- 1] != '\0')
3287 evbuffer_add(evb
, "", 1);
3293 if (c
->session
== NULL
|| (c
->flags
& CLIENT_CONTROL
)) {
3294 if (~c
->flags
& CLIENT_UTF8
) {
3295 sanitized
= utf8_sanitize(msg
);
3296 if (c
->flags
& CLIENT_CONTROL
)
3297 control_write(c
, "%s", sanitized
);
3299 file_print(c
, "%s\n", sanitized
);
3302 if (c
->flags
& CLIENT_CONTROL
)
3303 control_write(c
, "%s", msg
);
3305 file_print(c
, "%s\n", msg
);
3310 wp
= server_client_get_pane(c
);
3311 wme
= TAILQ_FIRST(&wp
->modes
);
3312 if (wme
== NULL
|| wme
->mode
!= &window_view_mode
)
3313 window_pane_set_mode(wp
, NULL
, &window_view_mode
, NULL
, NULL
);
3316 line
= evbuffer_readln(evb
, NULL
, EVBUFFER_EOL_LF
);
3318 window_copy_add(wp
, 1, "%s", line
);
3321 } while (line
!= NULL
);
3323 size
= EVBUFFER_LENGTH(evb
);
3325 line
= EVBUFFER_DATA(evb
);
3326 window_copy_add(wp
, 1, "%.*s", (int)size
, line
);
3329 window_copy_add(wp
, 0, "%s", msg
);