4 * Copyright (c) 2007 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>
30 static void server_destroy_session_group(struct session
*);
33 server_redraw_client(struct client
*c
)
35 c
->flags
|= CLIENT_ALLREDRAWFLAGS
;
39 server_status_client(struct client
*c
)
41 c
->flags
|= CLIENT_REDRAWSTATUS
;
45 server_redraw_session(struct session
*s
)
49 TAILQ_FOREACH(c
, &clients
, entry
) {
51 server_redraw_client(c
);
56 server_redraw_session_group(struct session
*s
)
58 struct session_group
*sg
;
60 if ((sg
= session_group_contains(s
)) == NULL
)
61 server_redraw_session(s
);
63 TAILQ_FOREACH(s
, &sg
->sessions
, gentry
)
64 server_redraw_session(s
);
69 server_status_session(struct session
*s
)
73 TAILQ_FOREACH(c
, &clients
, entry
) {
75 server_status_client(c
);
80 server_status_session_group(struct session
*s
)
82 struct session_group
*sg
;
84 if ((sg
= session_group_contains(s
)) == NULL
)
85 server_status_session(s
);
87 TAILQ_FOREACH(s
, &sg
->sessions
, gentry
)
88 server_status_session(s
);
93 server_redraw_window(struct window
*w
)
97 TAILQ_FOREACH(c
, &clients
, entry
) {
98 if (c
->session
!= NULL
&& c
->session
->curw
->window
== w
)
99 server_redraw_client(c
);
104 server_redraw_window_borders(struct window
*w
)
108 TAILQ_FOREACH(c
, &clients
, entry
) {
109 if (c
->session
!= NULL
&& c
->session
->curw
->window
== w
)
110 c
->flags
|= CLIENT_REDRAWBORDERS
;
115 server_status_window(struct window
*w
)
120 * This is slightly different. We want to redraw the status line of any
121 * clients containing this window rather than anywhere it is the
125 RB_FOREACH(s
, sessions
, &sessions
) {
126 if (session_has(s
, w
))
127 server_status_session(s
);
136 TAILQ_FOREACH(c
, &clients
, entry
) {
137 if (c
->session
!= NULL
)
138 server_lock_client(c
);
143 server_lock_session(struct session
*s
)
147 TAILQ_FOREACH(c
, &clients
, entry
) {
149 server_lock_client(c
);
154 server_lock_client(struct client
*c
)
158 if (c
->flags
& CLIENT_CONTROL
)
161 if (c
->flags
& CLIENT_SUSPENDED
)
164 cmd
= options_get_string(c
->session
->options
, "lock-command");
165 if (*cmd
== '\0' || strlen(cmd
) + 1 > MAX_IMSGSIZE
- IMSG_HEADER_SIZE
)
168 tty_stop_tty(&c
->tty
);
169 tty_raw(&c
->tty
, tty_term_string(c
->tty
.term
, TTYC_SMCUP
));
170 tty_raw(&c
->tty
, tty_term_string(c
->tty
.term
, TTYC_CLEAR
));
171 tty_raw(&c
->tty
, tty_term_string(c
->tty
.term
, TTYC_E3
));
173 c
->flags
|= CLIENT_SUSPENDED
;
174 proc_send(c
->peer
, MSG_LOCK
, -1, cmd
, strlen(cmd
) + 1);
178 server_kill_pane(struct window_pane
*wp
)
180 struct window
*w
= wp
->window
;
182 if (window_count_panes(w
) == 1) {
183 server_kill_window(w
, 1);
186 server_unzoom_window(w
);
187 server_client_remove_pane(wp
);
188 layout_close_pane(wp
);
189 window_remove_pane(w
, wp
);
190 server_redraw_window(w
);
195 server_kill_window(struct window
*w
, int renumber
)
197 struct session
*s
, *s1
;
200 RB_FOREACH_SAFE(s
, sessions
, &sessions
, s1
) {
201 if (!session_has(s
, w
))
204 server_unzoom_window(w
);
205 while ((wl
= winlink_find_by_window(&s
->windows
, w
)) != NULL
) {
206 if (session_detach(s
, wl
)) {
207 server_destroy_session_group(s
);
210 server_redraw_session_group(s
);
214 server_renumber_session(s
);
220 server_renumber_session(struct session
*s
)
222 struct session_group
*sg
;
224 if (options_get_number(s
->options
, "renumber-windows")) {
225 if ((sg
= session_group_contains(s
)) != NULL
) {
226 TAILQ_FOREACH(s
, &sg
->sessions
, gentry
)
227 session_renumber_windows(s
);
229 session_renumber_windows(s
);
234 server_renumber_all(void)
238 RB_FOREACH(s
, sessions
, &sessions
)
239 server_renumber_session(s
);
243 server_link_window(struct session
*src
, struct winlink
*srcwl
,
244 struct session
*dst
, int dstidx
, int killflag
, int selectflag
,
247 struct winlink
*dstwl
;
248 struct session_group
*srcsg
, *dstsg
;
250 srcsg
= session_group_contains(src
);
251 dstsg
= session_group_contains(dst
);
252 if (src
!= dst
&& srcsg
!= NULL
&& dstsg
!= NULL
&& srcsg
== dstsg
) {
253 xasprintf(cause
, "sessions are grouped");
259 dstwl
= winlink_find_by_index(&dst
->windows
, dstidx
);
261 if (dstwl
->window
== srcwl
->window
) {
262 xasprintf(cause
, "same index: %d", dstidx
);
267 * Can't use session_detach as it will destroy session
268 * if this makes it empty.
270 notify_session_window("window-unlinked", dst
,
272 dstwl
->flags
&= ~WINLINK_ALERTFLAGS
;
273 winlink_stack_remove(&dst
->lastw
, dstwl
);
274 winlink_remove(&dst
->windows
, dstwl
);
276 /* Force select/redraw if current. */
277 if (dstwl
== dst
->curw
) {
285 dstidx
= -1 - options_get_number(dst
->options
, "base-index");
286 dstwl
= session_attach(dst
, srcwl
->window
, dstidx
, cause
);
291 session_select(dst
, dstwl
->idx
);
292 server_redraw_session_group(dst
);
298 server_unlink_window(struct session
*s
, struct winlink
*wl
)
300 if (session_detach(s
, wl
))
301 server_destroy_session_group(s
);
303 server_redraw_session_group(s
);
307 server_destroy_pane(struct window_pane
*wp
, int notify
)
309 struct window
*w
= wp
->window
;
310 struct screen_write_ctx ctx
;
315 u_int sx
= screen_size_x(&wp
->base
);
316 u_int sy
= screen_size_y(&wp
->base
);
320 utempter_remove_record(wp
->fd
);
322 bufferevent_free(wp
->event
);
328 remain_on_exit
= options_get_number(wp
->options
, "remain-on-exit");
329 if (remain_on_exit
!= 0 && (~wp
->flags
& PANE_STATUSREADY
))
331 switch (remain_on_exit
) {
335 if (WIFEXITED(wp
->status
) && WEXITSTATUS(wp
->status
) == 0)
339 if (wp
->flags
& PANE_STATUSDRAWN
)
341 wp
->flags
|= PANE_STATUSDRAWN
;
343 gettimeofday(&wp
->dead_time
, NULL
);
345 notify_pane("pane-died", wp
);
347 s
= options_get_string(wp
->options
, "remain-on-exit-format");
349 screen_write_start_pane(&ctx
, wp
, &wp
->base
);
350 screen_write_scrollregion(&ctx
, 0, sy
- 1);
351 screen_write_cursormove(&ctx
, 0, sy
- 1, 0);
352 screen_write_linefeed(&ctx
, 1, 8);
353 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
355 expanded
= format_single(NULL
, s
, NULL
, NULL
, NULL
, wp
);
356 format_draw(&ctx
, &gc
, sx
, expanded
, NULL
, 0);
359 screen_write_stop(&ctx
);
361 wp
->base
.mode
&= ~MODE_CURSOR
;
363 wp
->flags
|= PANE_REDRAW
;
368 notify_pane("pane-exited", wp
);
370 server_unzoom_window(w
);
371 server_client_remove_pane(wp
);
372 layout_close_pane(wp
);
373 window_remove_pane(w
, wp
);
375 if (TAILQ_EMPTY(&w
->panes
))
376 server_kill_window(w
, 1);
378 server_redraw_window(w
);
382 server_destroy_session_group(struct session
*s
)
384 struct session_group
*sg
;
387 if ((sg
= session_group_contains(s
)) == NULL
) {
388 server_destroy_session(s
);
389 session_destroy(s
, 1, __func__
);
391 TAILQ_FOREACH_SAFE(s
, &sg
->sessions
, gentry
, s1
) {
392 server_destroy_session(s
);
393 session_destroy(s
, 1, __func__
);
398 static struct session
*
399 server_find_session(struct session
*s
,
400 int (*f
)(struct session
*, struct session
*))
402 struct session
*s_loop
, *s_out
= NULL
;
404 RB_FOREACH(s_loop
, sessions
, &sessions
) {
405 if (s_loop
!= s
&& (s_out
== NULL
|| f(s_loop
, s_out
)))
412 server_newer_session(struct session
*s_loop
, struct session
*s_out
)
414 return (timercmp(&s_loop
->activity_time
, &s_out
->activity_time
, >));
418 server_newer_detached_session(struct session
*s_loop
, struct session
*s_out
)
420 if (s_loop
->attached
)
422 return (server_newer_session(s_loop
, s_out
));
426 server_destroy_session(struct session
*s
)
429 struct session
*s_new
= NULL
;
430 int detach_on_destroy
;
432 detach_on_destroy
= options_get_number(s
->options
, "detach-on-destroy");
433 if (detach_on_destroy
== 0)
434 s_new
= server_find_session(s
, server_newer_session
);
435 else if (detach_on_destroy
== 2)
436 s_new
= server_find_session(s
, server_newer_detached_session
);
437 else if (detach_on_destroy
== 3)
438 s_new
= session_previous_session(s
);
439 else if (detach_on_destroy
== 4)
440 s_new
= session_next_session(s
);
443 TAILQ_FOREACH(c
, &clients
, entry
) {
447 c
->last_session
= NULL
;
448 server_client_set_session(c
, s_new
);
450 c
->flags
|= CLIENT_EXIT
;
456 server_check_unattached(void)
459 struct session_group
*sg
;
462 * If any sessions are no longer attached and have destroy-unattached
465 RB_FOREACH(s
, sessions
, &sessions
) {
466 if (s
->attached
!= 0)
468 switch (options_get_number(s
->options
, "destroy-unattached")) {
473 case 2: /* keep-last */
474 sg
= session_group_contains(s
);
475 if (sg
== NULL
|| session_group_count(sg
) <= 1)
478 case 3: /* keep-group */
479 sg
= session_group_contains(s
);
480 if (sg
!= NULL
&& session_group_count(sg
) == 1)
484 session_destroy(s
, 1, __func__
);
489 server_unzoom_window(struct window
*w
)
491 if (window_unzoom(w
, 1) == 0)
492 server_redraw_window(w
);