4 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
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>
28 struct session
*server_next_session(struct session
*);
29 void server_callback_identify(int, short, void *);
32 server_fill_environ(struct session
*s
, struct environ
*env
)
34 char var
[MAXPATHLEN
], *term
;
39 term
= options_get_string(&s
->options
, "default-terminal");
40 environ_set(env
, "TERM", term
);
46 xsnprintf(var
, sizeof var
, "%s,%ld,%d", socket_path
, pid
, idx
);
47 environ_set(env
, "TMUX", var
);
51 server_write_ready(struct client
*c
)
53 if (c
->flags
& CLIENT_CONTROL
)
55 server_write_client(c
, MSG_READY
, NULL
, 0);
60 struct client
*c
, enum msgtype type
, const void *buf
, size_t len
)
62 struct imsgbuf
*ibuf
= &c
->ibuf
;
65 if (c
->flags
& CLIENT_BAD
)
67 log_debug("writing %d to client %d", type
, c
->ibuf
.fd
);
68 error
= imsg_compose(ibuf
, type
, PROTOCOL_VERSION
, -1, -1,
71 server_update_event(c
);
72 return (error
== 1 ? 0 : -1);
77 struct session
*s
, enum msgtype type
, const void *buf
, size_t len
)
82 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
83 c
= ARRAY_ITEM(&clients
, i
);
84 if (c
== NULL
|| c
->session
== NULL
)
87 server_write_client(c
, type
, buf
, len
);
92 server_redraw_client(struct client
*c
)
94 c
->flags
|= CLIENT_REDRAW
;
98 server_status_client(struct client
*c
)
100 c
->flags
|= CLIENT_STATUS
;
104 server_redraw_session(struct session
*s
)
109 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
110 c
= ARRAY_ITEM(&clients
, i
);
111 if (c
== NULL
|| c
->session
== NULL
)
114 server_redraw_client(c
);
119 server_redraw_session_group(struct session
*s
)
121 struct session_group
*sg
;
123 if ((sg
= session_group_find(s
)) == NULL
)
124 server_redraw_session(s
);
126 TAILQ_FOREACH(s
, &sg
->sessions
, gentry
)
127 server_redraw_session(s
);
132 server_status_session(struct session
*s
)
137 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
138 c
= ARRAY_ITEM(&clients
, i
);
139 if (c
== NULL
|| c
->session
== NULL
)
142 server_status_client(c
);
147 server_status_session_group(struct session
*s
)
149 struct session_group
*sg
;
151 if ((sg
= session_group_find(s
)) == NULL
)
152 server_status_session(s
);
154 TAILQ_FOREACH(s
, &sg
->sessions
, gentry
)
155 server_status_session(s
);
160 server_redraw_window(struct window
*w
)
165 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
166 c
= ARRAY_ITEM(&clients
, i
);
167 if (c
== NULL
|| c
->session
== NULL
)
169 if (c
->session
->curw
->window
== w
)
170 server_redraw_client(c
);
172 w
->flags
|= WINDOW_REDRAW
;
176 server_redraw_window_borders(struct window
*w
)
181 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
182 c
= ARRAY_ITEM(&clients
, i
);
183 if (c
== NULL
|| c
->session
== NULL
)
185 if (c
->session
->curw
->window
== w
)
186 c
->flags
|= CLIENT_BORDERS
;
191 server_status_window(struct window
*w
)
196 * This is slightly different. We want to redraw the status line of any
197 * clients containing this window rather than anywhere it is the
201 RB_FOREACH(s
, sessions
, &sessions
) {
202 if (session_has(s
, w
) != NULL
)
203 server_status_session(s
);
213 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
214 c
= ARRAY_ITEM(&clients
, i
);
215 if (c
== NULL
|| c
->session
== NULL
)
217 server_lock_client(c
);
222 server_lock_session(struct session
*s
)
227 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
228 c
= ARRAY_ITEM(&clients
, i
);
229 if (c
== NULL
|| c
->session
== NULL
|| c
->session
!= s
)
231 server_lock_client(c
);
236 server_lock_client(struct client
*c
)
240 struct msg_lock_data lockdata
;
242 if (c
->flags
& CLIENT_CONTROL
)
245 if (c
->flags
& CLIENT_SUSPENDED
)
248 cmd
= options_get_string(&c
->session
->options
, "lock-command");
249 cmdlen
= strlcpy(lockdata
.cmd
, cmd
, sizeof lockdata
.cmd
);
250 if (cmdlen
>= sizeof lockdata
.cmd
)
253 tty_stop_tty(&c
->tty
);
254 tty_raw(&c
->tty
, tty_term_string(c
->tty
.term
, TTYC_SMCUP
));
255 tty_raw(&c
->tty
, tty_term_string(c
->tty
.term
, TTYC_CLEAR
));
256 tty_raw(&c
->tty
, tty_term_string(c
->tty
.term
, TTYC_E3
));
258 c
->flags
|= CLIENT_SUSPENDED
;
259 server_write_client(c
, MSG_LOCK
, &lockdata
, sizeof lockdata
);
263 server_kill_window(struct window
*w
)
265 struct session
*s
, *next_s
;
268 next_s
= RB_MIN(sessions
, &sessions
);
269 while (next_s
!= NULL
) {
271 next_s
= RB_NEXT(sessions
, &sessions
, s
);
273 if (session_has(s
, w
) == NULL
)
275 while ((wl
= winlink_find_by_window(&s
->windows
, w
)) != NULL
) {
276 if (session_detach(s
, wl
)) {
277 server_destroy_session_group(s
);
280 server_redraw_session_group(s
);
283 if (options_get_number(&s
->options
, "renumber-windows"))
284 session_renumber_windows(s
);
289 server_link_window(struct session
*src
, struct winlink
*srcwl
,
290 struct session
*dst
, int dstidx
, int killflag
, int selectflag
, char **cause
)
292 struct winlink
*dstwl
;
293 struct session_group
*srcsg
, *dstsg
;
295 srcsg
= session_group_find(src
);
296 dstsg
= session_group_find(dst
);
297 if (src
!= dst
&& srcsg
!= NULL
&& dstsg
!= NULL
&& srcsg
== dstsg
) {
298 xasprintf(cause
, "sessions are grouped");
304 dstwl
= winlink_find_by_index(&dst
->windows
, dstidx
);
306 if (dstwl
->window
== srcwl
->window
) {
307 xasprintf(cause
, "same index: %d", dstidx
);
312 * Can't use session_detach as it will destroy session
313 * if this makes it empty.
315 notify_window_unlinked(dst
, dstwl
->window
);
316 dstwl
->flags
&= ~WINLINK_ALERTFLAGS
;
317 winlink_stack_remove(&dst
->lastw
, dstwl
);
318 winlink_remove(&dst
->windows
, dstwl
);
320 /* Force select/redraw if current. */
321 if (dstwl
== dst
->curw
) {
329 dstidx
= -1 - options_get_number(&dst
->options
, "base-index");
330 dstwl
= session_attach(dst
, srcwl
->window
, dstidx
, cause
);
335 session_select(dst
, dstwl
->idx
);
336 server_redraw_session_group(dst
);
342 server_unlink_window(struct session
*s
, struct winlink
*wl
)
344 if (session_detach(s
, wl
))
345 server_destroy_session_group(s
);
347 server_redraw_session_group(s
);
351 server_destroy_pane(struct window_pane
*wp
)
353 struct window
*w
= wp
->window
;
355 struct screen_write_ctx ctx
;
360 bufferevent_free(wp
->event
);
365 if (options_get_number(&w
->options
, "remain-on-exit")) {
368 screen_write_start(&ctx
, wp
, &wp
->base
);
369 screen_write_scrollregion(&ctx
, 0, screen_size_y(ctx
.s
) - 1);
370 screen_write_cursormove(&ctx
, 0, screen_size_y(ctx
.s
) - 1);
371 screen_write_linefeed(&ctx
, 1);
372 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
373 gc
.attr
|= GRID_ATTR_BRIGHT
;
374 screen_write_puts(&ctx
, &gc
, "Pane is dead");
375 screen_write_stop(&ctx
);
376 wp
->flags
|= PANE_REDRAW
;
380 server_unzoom_window(w
);
381 layout_close_pane(wp
);
382 window_remove_pane(w
, wp
);
384 if (TAILQ_EMPTY(&w
->panes
))
385 server_kill_window(w
);
387 server_redraw_window(w
);
391 server_destroy_session_group(struct session
*s
)
393 struct session_group
*sg
;
395 if ((sg
= session_group_find(s
)) == NULL
)
396 server_destroy_session(s
);
398 TAILQ_FOREACH(s
, &sg
->sessions
, gentry
)
399 server_destroy_session(s
);
400 TAILQ_REMOVE(&session_groups
, sg
, entry
);
406 server_next_session(struct session
*s
)
408 struct session
*s_loop
, *s_out
;
411 RB_FOREACH(s_loop
, sessions
, &sessions
) {
415 timercmp(&s_loop
->activity_time
, &s_out
->activity_time
, <))
422 server_destroy_session(struct session
*s
)
425 struct session
*s_new
;
428 if (!options_get_number(&s
->options
, "detach-on-destroy"))
429 s_new
= server_next_session(s
);
433 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
434 c
= ARRAY_ITEM(&clients
, i
);
435 if (c
== NULL
|| c
->session
!= s
)
439 c
->flags
|= CLIENT_EXIT
;
441 c
->last_session
= NULL
;
443 notify_attached_session_changed(c
);
444 session_update_activity(s_new
);
445 server_redraw_client(c
);
452 server_check_unattached (void)
457 * If any sessions are no longer attached and have destroy-unattached
460 RB_FOREACH(s
, sessions
, &sessions
) {
461 if (!(s
->flags
& SESSION_UNATTACHED
))
463 if (options_get_number (&s
->options
, "destroy-unattached"))
469 server_set_identify(struct client
*c
)
474 delay
= options_get_number(&c
->session
->options
, "display-panes-time");
475 tv
.tv_sec
= delay
/ 1000;
476 tv
.tv_usec
= (delay
% 1000) * 1000L;
478 if (event_initialized (&c
->identify_timer
))
479 evtimer_del(&c
->identify_timer
);
480 evtimer_set(&c
->identify_timer
, server_callback_identify
, c
);
481 evtimer_add(&c
->identify_timer
, &tv
);
483 c
->flags
|= CLIENT_IDENTIFY
;
484 c
->tty
.flags
|= (TTY_FREEZE
|TTY_NOCURSOR
);
485 server_redraw_client(c
);
489 server_clear_identify(struct client
*c
)
491 if (c
->flags
& CLIENT_IDENTIFY
) {
492 c
->flags
&= ~CLIENT_IDENTIFY
;
493 c
->tty
.flags
&= ~(TTY_FREEZE
|TTY_NOCURSOR
);
494 server_redraw_client(c
);
499 server_callback_identify(unused
int fd
, unused
short events
, void *data
)
501 struct client
*c
= data
;
503 server_clear_identify(c
);
507 server_update_event(struct client
*c
)
512 if (!(c
->flags
& CLIENT_BAD
))
514 if (c
->ibuf
.w
.queued
> 0)
516 if (event_initialized(&c
->event
))
517 event_del(&c
->event
);
518 event_set(&c
->event
, c
->ibuf
.fd
, events
, server_client_callback
, c
);
519 event_add(&c
->event
, NULL
);
522 /* Push stdout to client if possible. */
524 server_push_stdout(struct client
*c
)
526 struct msg_stdout_data data
;
529 size
= EVBUFFER_LENGTH(c
->stdout_data
);
532 if (size
> sizeof data
.data
)
533 size
= sizeof data
.data
;
535 memcpy(data
.data
, EVBUFFER_DATA(c
->stdout_data
), size
);
538 if (server_write_client(c
, MSG_STDOUT
, &data
, sizeof data
) == 0)
539 evbuffer_drain(c
->stdout_data
, size
);
542 /* Push stderr to client if possible. */
544 server_push_stderr(struct client
*c
)
546 struct msg_stderr_data data
;
549 size
= EVBUFFER_LENGTH(c
->stderr_data
);
552 if (size
> sizeof data
.data
)
553 size
= sizeof data
.data
;
555 memcpy(data
.data
, EVBUFFER_DATA(c
->stderr_data
), size
);
558 if (server_write_client(c
, MSG_STDERR
, &data
, sizeof data
) == 0)
559 evbuffer_drain(c
->stderr_data
, size
);
562 /* Set stdin callback. */
564 server_set_stdin_callback(struct client
*c
, void (*cb
)(struct client
*, int,
565 void *), void *cb_data
, char **cause
)
567 if (c
== NULL
|| c
->session
!= NULL
) {
568 *cause
= xstrdup("no client with stdin");
571 if (c
->flags
& CLIENT_TERMINAL
) {
572 *cause
= xstrdup("stdin is a tty");
575 if (c
->stdin_callback
!= NULL
) {
576 *cause
= xstrdup("stdin in use");
580 c
->stdin_callback_data
= cb_data
;
581 c
->stdin_callback
= cb
;
586 c
->stdin_callback (c
, 1, c
->stdin_callback_data
);
588 server_write_client(c
, MSG_STDIN
, NULL
, 0);
594 server_unzoom_window(struct window
*w
)
597 server_redraw_window(w
);
598 server_status_window(w
);