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>
20 #include <sys/queue.h>
32 static struct session
*server_next_session(struct session
*);
33 static void server_destroy_session_group(struct session
*);
36 server_redraw_client(struct client
*c
)
38 c
->flags
|= CLIENT_ALLREDRAWFLAGS
;
42 server_status_client(struct client
*c
)
44 c
->flags
|= CLIENT_REDRAWSTATUS
;
48 server_redraw_session(struct session
*s
)
52 TAILQ_FOREACH(c
, &clients
, entry
) {
54 server_redraw_client(c
);
59 server_redraw_session_group(struct session
*s
)
61 struct session_group
*sg
;
63 if ((sg
= session_group_contains(s
)) == NULL
)
64 server_redraw_session(s
);
66 TAILQ_FOREACH(s
, &sg
->sessions
, gentry
)
67 server_redraw_session(s
);
72 server_status_session(struct session
*s
)
76 TAILQ_FOREACH(c
, &clients
, entry
) {
78 server_status_client(c
);
83 server_status_session_group(struct session
*s
)
85 struct session_group
*sg
;
87 if ((sg
= session_group_contains(s
)) == NULL
)
88 server_status_session(s
);
90 TAILQ_FOREACH(s
, &sg
->sessions
, gentry
)
91 server_status_session(s
);
96 server_redraw_window(struct window
*w
)
100 TAILQ_FOREACH(c
, &clients
, entry
) {
101 if (c
->session
!= NULL
&& c
->session
->curw
->window
== w
)
102 server_redraw_client(c
);
107 server_redraw_window_borders(struct window
*w
)
111 TAILQ_FOREACH(c
, &clients
, entry
) {
112 if (c
->session
!= NULL
&& c
->session
->curw
->window
== w
)
113 c
->flags
|= CLIENT_REDRAWBORDERS
;
118 server_status_window(struct window
*w
)
123 * This is slightly different. We want to redraw the status line of any
124 * clients containing this window rather than anywhere it is the
128 RB_FOREACH(s
, sessions
, &sessions
) {
129 if (session_has(s
, w
))
130 server_status_session(s
);
139 TAILQ_FOREACH(c
, &clients
, entry
) {
140 if (c
->session
!= NULL
)
141 server_lock_client(c
);
146 server_lock_session(struct session
*s
)
150 TAILQ_FOREACH(c
, &clients
, entry
) {
152 server_lock_client(c
);
157 server_lock_client(struct client
*c
)
161 if (c
->flags
& CLIENT_CONTROL
)
164 if (c
->flags
& CLIENT_SUSPENDED
)
167 cmd
= options_get_string(c
->session
->options
, "lock-command");
168 if (*cmd
== '\0' || strlen(cmd
) + 1 > MAX_IMSGSIZE
- IMSG_HEADER_SIZE
)
171 tty_stop_tty(&c
->tty
);
172 tty_raw(&c
->tty
, tty_term_string(c
->tty
.term
, TTYC_SMCUP
));
173 tty_raw(&c
->tty
, tty_term_string(c
->tty
.term
, TTYC_CLEAR
));
174 tty_raw(&c
->tty
, tty_term_string(c
->tty
.term
, TTYC_E3
));
176 c
->flags
|= CLIENT_SUSPENDED
;
177 proc_send(c
->peer
, MSG_LOCK
, -1, cmd
, strlen(cmd
) + 1);
181 server_kill_pane(struct window_pane
*wp
)
183 struct window
*w
= wp
->window
;
185 if (window_count_panes(w
) == 1) {
186 server_kill_window(w
, 1);
189 server_unzoom_window(w
);
190 server_client_remove_pane(wp
);
191 layout_close_pane(wp
);
192 window_remove_pane(w
, wp
);
193 server_redraw_window(w
);
198 server_kill_window(struct window
*w
, int renumber
)
200 struct session
*s
, *s1
;
203 RB_FOREACH_SAFE(s
, sessions
, &sessions
, s1
) {
204 if (!session_has(s
, w
))
207 server_unzoom_window(w
);
208 while ((wl
= winlink_find_by_window(&s
->windows
, w
)) != NULL
) {
209 if (session_detach(s
, wl
)) {
210 server_destroy_session_group(s
);
213 server_redraw_session_group(s
);
217 server_renumber_session(s
);
223 server_renumber_session(struct session
*s
)
225 struct session_group
*sg
;
227 if (options_get_number(s
->options
, "renumber-windows")) {
228 if ((sg
= session_group_contains(s
)) != NULL
) {
229 TAILQ_FOREACH(s
, &sg
->sessions
, gentry
)
230 session_renumber_windows(s
);
232 session_renumber_windows(s
);
237 server_renumber_all(void)
241 RB_FOREACH(s
, sessions
, &sessions
)
242 server_renumber_session(s
);
246 server_link_window(struct session
*src
, struct winlink
*srcwl
,
247 struct session
*dst
, int dstidx
, int killflag
, int selectflag
,
250 struct winlink
*dstwl
;
251 struct session_group
*srcsg
, *dstsg
;
253 srcsg
= session_group_contains(src
);
254 dstsg
= session_group_contains(dst
);
255 if (src
!= dst
&& srcsg
!= NULL
&& dstsg
!= NULL
&& srcsg
== dstsg
) {
256 xasprintf(cause
, "sessions are grouped");
262 dstwl
= winlink_find_by_index(&dst
->windows
, dstidx
);
264 if (dstwl
->window
== srcwl
->window
) {
265 xasprintf(cause
, "same index: %d", dstidx
);
270 * Can't use session_detach as it will destroy session
271 * if this makes it empty.
273 notify_session_window("window-unlinked", dst
,
275 dstwl
->flags
&= ~WINLINK_ALERTFLAGS
;
276 winlink_stack_remove(&dst
->lastw
, dstwl
);
277 winlink_remove(&dst
->windows
, dstwl
);
279 /* Force select/redraw if current. */
280 if (dstwl
== dst
->curw
) {
288 dstidx
= -1 - options_get_number(dst
->options
, "base-index");
289 dstwl
= session_attach(dst
, srcwl
->window
, dstidx
, cause
);
294 session_select(dst
, dstwl
->idx
);
295 server_redraw_session_group(dst
);
301 server_unlink_window(struct session
*s
, struct winlink
*wl
)
303 if (session_detach(s
, wl
))
304 server_destroy_session_group(s
);
306 server_redraw_session_group(s
);
310 server_destroy_pane(struct window_pane
*wp
, int notify
)
312 struct window
*w
= wp
->window
;
313 struct screen_write_ctx ctx
;
318 u_int sx
= screen_size_x(&wp
->base
);
319 u_int sy
= screen_size_y(&wp
->base
);
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
);
390 TAILQ_FOREACH_SAFE(s
, &sg
->sessions
, gentry
, s1
) {
391 server_destroy_session(s
);
392 session_destroy(s
, 1, __func__
);
397 static struct session
*
398 server_next_session(struct session
*s
)
400 struct session
*s_loop
, *s_out
= NULL
;
402 RB_FOREACH(s_loop
, sessions
, &sessions
) {
406 timercmp(&s_loop
->activity_time
, &s_out
->activity_time
, <))
412 static struct session
*
413 server_next_detached_session(struct session
*s
)
415 struct session
*s_loop
, *s_out
= NULL
;
417 RB_FOREACH(s_loop
, sessions
, &sessions
) {
418 if (s_loop
== s
|| s_loop
->attached
)
421 timercmp(&s_loop
->activity_time
, &s_out
->activity_time
, <))
428 server_destroy_session(struct session
*s
)
431 struct session
*s_new
;
432 int detach_on_destroy
;
434 detach_on_destroy
= options_get_number(s
->options
, "detach-on-destroy");
435 if (detach_on_destroy
== 0)
436 s_new
= server_next_session(s
);
437 else if (detach_on_destroy
== 2)
438 s_new
= server_next_detached_session(s
);
441 TAILQ_FOREACH(c
, &clients
, entry
) {
444 server_client_set_session(c
, s_new
);
446 c
->flags
|= CLIENT_EXIT
;
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
->attached
!= 0)
463 if (options_get_number (s
->options
, "destroy-unattached"))
464 session_destroy(s
, 1, __func__
);
469 server_unzoom_window(struct window
*w
)
471 if (window_unzoom(w
) == 0)
472 server_redraw_window(w
);