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>
27 struct session
*server_next_session(struct session
*);
28 void server_callback_identify(int, short, void *);
31 server_fill_environ(struct session
*s
, struct environ
*env
)
33 char var
[MAXPATHLEN
], *term
;
38 term
= options_get_string(&s
->options
, "default-terminal");
39 environ_set(env
, "TERM", term
);
45 xsnprintf(var
, sizeof var
, "%s,%ld,%d", socket_path
, pid
, idx
);
46 environ_set(env
, "TMUX", var
);
51 struct client
*c
, enum msgtype type
, const void *buf
, size_t len
)
53 struct imsgbuf
*ibuf
= &c
->ibuf
;
55 if (c
->flags
& CLIENT_BAD
)
57 log_debug("writing %d to client %d", type
, c
->ibuf
.fd
);
58 imsg_compose(ibuf
, type
, PROTOCOL_VERSION
, -1, -1, (void *) buf
, len
);
59 server_update_event(c
);
64 struct session
*s
, enum msgtype type
, const void *buf
, size_t len
)
69 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
70 c
= ARRAY_ITEM(&clients
, i
);
71 if (c
== NULL
|| c
->session
== NULL
)
74 server_write_client(c
, type
, buf
, len
);
79 server_redraw_client(struct client
*c
)
81 c
->flags
|= CLIENT_REDRAW
;
85 server_status_client(struct client
*c
)
87 c
->flags
|= CLIENT_STATUS
;
91 server_redraw_session(struct session
*s
)
96 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
97 c
= ARRAY_ITEM(&clients
, i
);
98 if (c
== NULL
|| c
->session
== NULL
)
101 server_redraw_client(c
);
106 server_redraw_session_group(struct session
*s
)
108 struct session_group
*sg
;
110 if ((sg
= session_group_find(s
)) == NULL
)
111 server_redraw_session(s
);
113 TAILQ_FOREACH(s
, &sg
->sessions
, gentry
)
114 server_redraw_session(s
);
119 server_status_session(struct session
*s
)
124 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
125 c
= ARRAY_ITEM(&clients
, i
);
126 if (c
== NULL
|| c
->session
== NULL
)
129 server_status_client(c
);
134 server_status_session_group(struct session
*s
)
136 struct session_group
*sg
;
138 if ((sg
= session_group_find(s
)) == NULL
)
139 server_status_session(s
);
141 TAILQ_FOREACH(s
, &sg
->sessions
, gentry
)
142 server_status_session(s
);
147 server_redraw_window(struct window
*w
)
152 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
153 c
= ARRAY_ITEM(&clients
, i
);
154 if (c
== NULL
|| c
->session
== NULL
)
156 if (c
->session
->curw
->window
== w
)
157 server_redraw_client(c
);
159 w
->flags
|= WINDOW_REDRAW
;
163 server_redraw_window_borders(struct window
*w
)
168 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
169 c
= ARRAY_ITEM(&clients
, i
);
170 if (c
== NULL
|| c
->session
== NULL
)
172 if (c
->session
->curw
->window
== w
)
173 c
->flags
|= CLIENT_BORDERS
;
178 server_status_window(struct window
*w
)
183 * This is slightly different. We want to redraw the status line of any
184 * clients containing this window rather than any where it is the
188 RB_FOREACH(s
, sessions
, &sessions
) {
189 if (session_has(s
, w
) != NULL
)
190 server_status_session(s
);
200 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
201 c
= ARRAY_ITEM(&clients
, i
);
202 if (c
== NULL
|| c
->session
== NULL
)
204 server_lock_client(c
);
209 server_lock_session(struct session
*s
)
214 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
215 c
= ARRAY_ITEM(&clients
, i
);
216 if (c
== NULL
|| c
->session
== NULL
|| c
->session
!= s
)
218 server_lock_client(c
);
223 server_lock_client(struct client
*c
)
227 struct msg_lock_data lockdata
;
229 if (c
->flags
& CLIENT_SUSPENDED
)
232 cmd
= options_get_string(&c
->session
->options
, "lock-command");
233 cmdlen
= strlcpy(lockdata
.cmd
, cmd
, sizeof lockdata
.cmd
);
234 if (cmdlen
>= sizeof lockdata
.cmd
)
237 tty_stop_tty(&c
->tty
);
238 tty_raw(&c
->tty
, tty_term_string(c
->tty
.term
, TTYC_SMCUP
));
239 tty_raw(&c
->tty
, tty_term_string(c
->tty
.term
, TTYC_CLEAR
));
240 tty_raw(&c
->tty
, tty_term_string(c
->tty
.term
, TTYC_E3
));
242 c
->flags
|= CLIENT_SUSPENDED
;
243 server_write_client(c
, MSG_LOCK
, &lockdata
, sizeof lockdata
);
247 server_kill_window(struct window
*w
)
249 struct session
*s
, *next_s
;
252 next_s
= RB_MIN(sessions
, &sessions
);
253 while (next_s
!= NULL
) {
255 next_s
= RB_NEXT(sessions
, &sessions
, s
);
257 if (session_has(s
, w
) == NULL
)
259 while ((wl
= winlink_find_by_window(&s
->windows
, w
)) != NULL
) {
260 if (session_detach(s
, wl
)) {
261 server_destroy_session_group(s
);
264 server_redraw_session_group(s
);
270 server_link_window(struct session
*src
, struct winlink
*srcwl
,
271 struct session
*dst
, int dstidx
, int killflag
, int selectflag
, char **cause
)
273 struct winlink
*dstwl
;
274 struct session_group
*srcsg
, *dstsg
;
276 srcsg
= session_group_find(src
);
277 dstsg
= session_group_find(dst
);
278 if (src
!= dst
&& srcsg
!= NULL
&& dstsg
!= NULL
&& srcsg
== dstsg
) {
279 xasprintf(cause
, "sessions are grouped");
285 dstwl
= winlink_find_by_index(&dst
->windows
, dstidx
);
287 if (dstwl
->window
== srcwl
->window
) {
288 xasprintf(cause
, "same index: %d", dstidx
);
293 * Can't use session_detach as it will destroy session
294 * if this makes it empty.
296 notify_window_unlinked(dst
, dstwl
->window
);
297 dstwl
->flags
&= ~WINLINK_ALERTFLAGS
;
298 winlink_stack_remove(&dst
->lastw
, dstwl
);
299 winlink_remove(&dst
->windows
, dstwl
);
301 /* Force select/redraw if current. */
302 if (dstwl
== dst
->curw
) {
310 dstidx
= -1 - options_get_number(&dst
->options
, "base-index");
311 dstwl
= session_attach(dst
, srcwl
->window
, dstidx
, cause
);
316 session_select(dst
, dstwl
->idx
);
317 server_redraw_session_group(dst
);
323 server_unlink_window(struct session
*s
, struct winlink
*wl
)
325 if (session_detach(s
, wl
))
326 server_destroy_session_group(s
);
328 server_redraw_session_group(s
);
332 server_destroy_pane(struct window_pane
*wp
)
334 struct window
*w
= wp
->window
;
336 struct screen_write_ctx ctx
;
341 bufferevent_free(wp
->event
);
346 if (options_get_number(&w
->options
, "remain-on-exit")) {
349 screen_write_start(&ctx
, wp
, &wp
->base
);
350 screen_write_scrollregion(&ctx
, 0, screen_size_y(ctx
.s
) - 1);
351 screen_write_cursormove(&ctx
, 0, screen_size_y(ctx
.s
) - 1);
352 screen_write_linefeed(&ctx
, 1);
353 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
354 gc
.attr
|= GRID_ATTR_BRIGHT
;
355 screen_write_puts(&ctx
, &gc
, "Pane is dead");
356 screen_write_stop(&ctx
);
357 wp
->flags
|= PANE_REDRAW
;
361 layout_close_pane(wp
);
362 window_remove_pane(w
, wp
);
364 if (TAILQ_EMPTY(&w
->panes
))
365 server_kill_window(w
);
367 server_redraw_window(w
);
371 server_destroy_session_group(struct session
*s
)
373 struct session_group
*sg
;
375 if ((sg
= session_group_find(s
)) == NULL
)
376 server_destroy_session(s
);
378 TAILQ_FOREACH(s
, &sg
->sessions
, gentry
)
379 server_destroy_session(s
);
380 TAILQ_REMOVE(&session_groups
, sg
, entry
);
386 server_next_session(struct session
*s
)
388 struct session
*s_loop
, *s_out
;
391 RB_FOREACH(s_loop
, sessions
, &sessions
) {
395 timercmp(&s_loop
->activity_time
, &s_out
->activity_time
, <))
402 server_destroy_session(struct session
*s
)
405 struct session
*s_new
;
408 if (!options_get_number(&s
->options
, "detach-on-destroy"))
409 s_new
= server_next_session(s
);
413 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
414 c
= ARRAY_ITEM(&clients
, i
);
415 if (c
== NULL
|| c
->session
!= s
)
419 c
->flags
|= CLIENT_EXIT
;
421 c
->last_session
= NULL
;
423 notify_attached_session_changed(c
);
424 session_update_activity(s_new
);
425 server_redraw_client(c
);
432 server_check_unattached (void)
437 * If any sessions are no longer attached and have destroy-unattached
440 RB_FOREACH(s
, sessions
, &sessions
) {
441 if (!(s
->flags
& SESSION_UNATTACHED
))
443 if (options_get_number (&s
->options
, "destroy-unattached"))
449 server_set_identify(struct client
*c
)
454 delay
= options_get_number(&c
->session
->options
, "display-panes-time");
455 tv
.tv_sec
= delay
/ 1000;
456 tv
.tv_usec
= (delay
% 1000) * 1000L;
458 if (event_initialized (&c
->identify_timer
))
459 evtimer_del(&c
->identify_timer
);
460 evtimer_set(&c
->identify_timer
, server_callback_identify
, c
);
461 evtimer_add(&c
->identify_timer
, &tv
);
463 c
->flags
|= CLIENT_IDENTIFY
;
464 c
->tty
.flags
|= (TTY_FREEZE
|TTY_NOCURSOR
);
465 server_redraw_client(c
);
469 server_clear_identify(struct client
*c
)
471 if (c
->flags
& CLIENT_IDENTIFY
) {
472 c
->flags
&= ~CLIENT_IDENTIFY
;
473 c
->tty
.flags
&= ~(TTY_FREEZE
|TTY_NOCURSOR
);
474 server_redraw_client(c
);
480 server_callback_identify(unused
int fd
, unused
short events
, void *data
)
482 struct client
*c
= data
;
484 server_clear_identify(c
);
488 server_update_event(struct client
*c
)
493 if (!(c
->flags
& CLIENT_BAD
))
495 if (c
->ibuf
.w
.queued
> 0)
497 if (event_initialized(&c
->event
))
498 event_del(&c
->event
);
499 event_set(&c
->event
, c
->ibuf
.fd
, events
, server_client_callback
, c
);
500 event_add(&c
->event
, NULL
);