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 void server_destroy_session_group(struct session
*);
35 server_redraw_client(struct client
*c
)
37 c
->flags
|= CLIENT_ALLREDRAWFLAGS
;
41 server_status_client(struct client
*c
)
43 c
->flags
|= CLIENT_REDRAWSTATUS
;
47 server_redraw_session(struct session
*s
)
51 TAILQ_FOREACH(c
, &clients
, entry
) {
53 server_redraw_client(c
);
58 server_redraw_session_group(struct session
*s
)
60 struct session_group
*sg
;
62 if ((sg
= session_group_contains(s
)) == NULL
)
63 server_redraw_session(s
);
65 TAILQ_FOREACH(s
, &sg
->sessions
, gentry
)
66 server_redraw_session(s
);
71 server_status_session(struct session
*s
)
75 TAILQ_FOREACH(c
, &clients
, entry
) {
77 server_status_client(c
);
82 server_status_session_group(struct session
*s
)
84 struct session_group
*sg
;
86 if ((sg
= session_group_contains(s
)) == NULL
)
87 server_status_session(s
);
89 TAILQ_FOREACH(s
, &sg
->sessions
, gentry
)
90 server_status_session(s
);
95 server_redraw_window(struct window
*w
)
99 TAILQ_FOREACH(c
, &clients
, entry
) {
100 if (c
->session
!= NULL
&& c
->session
->curw
->window
== w
)
101 server_redraw_client(c
);
106 server_redraw_window_borders(struct window
*w
)
110 TAILQ_FOREACH(c
, &clients
, entry
) {
111 if (c
->session
!= NULL
&& c
->session
->curw
->window
== w
)
112 c
->flags
|= CLIENT_REDRAWBORDERS
;
117 server_status_window(struct window
*w
)
122 * This is slightly different. We want to redraw the status line of any
123 * clients containing this window rather than anywhere it is the
127 RB_FOREACH(s
, sessions
, &sessions
) {
128 if (session_has(s
, w
))
129 server_status_session(s
);
138 TAILQ_FOREACH(c
, &clients
, entry
) {
139 if (c
->session
!= NULL
)
140 server_lock_client(c
);
145 server_lock_session(struct session
*s
)
149 TAILQ_FOREACH(c
, &clients
, entry
) {
151 server_lock_client(c
);
156 server_lock_client(struct client
*c
)
160 if (c
->flags
& CLIENT_CONTROL
)
163 if (c
->flags
& CLIENT_SUSPENDED
)
166 cmd
= options_get_string(c
->session
->options
, "lock-command");
167 if (*cmd
== '\0' || strlen(cmd
) + 1 > MAX_IMSGSIZE
- IMSG_HEADER_SIZE
)
170 tty_stop_tty(&c
->tty
);
171 tty_raw(&c
->tty
, tty_term_string(c
->tty
.term
, TTYC_SMCUP
));
172 tty_raw(&c
->tty
, tty_term_string(c
->tty
.term
, TTYC_CLEAR
));
173 tty_raw(&c
->tty
, tty_term_string(c
->tty
.term
, TTYC_E3
));
175 c
->flags
|= CLIENT_SUSPENDED
;
176 proc_send(c
->peer
, MSG_LOCK
, -1, cmd
, strlen(cmd
) + 1);
180 server_kill_pane(struct window_pane
*wp
)
182 struct window
*w
= wp
->window
;
184 if (window_count_panes(w
) == 1) {
185 server_kill_window(w
, 1);
188 server_unzoom_window(w
);
189 server_client_remove_pane(wp
);
190 layout_close_pane(wp
);
191 window_remove_pane(w
, wp
);
192 server_redraw_window(w
);
197 server_kill_window(struct window
*w
, int renumber
)
199 struct session
*s
, *s1
;
202 RB_FOREACH_SAFE(s
, sessions
, &sessions
, s1
) {
203 if (!session_has(s
, w
))
206 server_unzoom_window(w
);
207 while ((wl
= winlink_find_by_window(&s
->windows
, w
)) != NULL
) {
208 if (session_detach(s
, wl
)) {
209 server_destroy_session_group(s
);
212 server_redraw_session_group(s
);
216 server_renumber_session(s
);
222 server_renumber_session(struct session
*s
)
224 struct session_group
*sg
;
226 if (options_get_number(s
->options
, "renumber-windows")) {
227 if ((sg
= session_group_contains(s
)) != NULL
) {
228 TAILQ_FOREACH(s
, &sg
->sessions
, gentry
)
229 session_renumber_windows(s
);
231 session_renumber_windows(s
);
236 server_renumber_all(void)
240 RB_FOREACH(s
, sessions
, &sessions
)
241 server_renumber_session(s
);
245 server_link_window(struct session
*src
, struct winlink
*srcwl
,
246 struct session
*dst
, int dstidx
, int killflag
, int selectflag
,
249 struct winlink
*dstwl
;
250 struct session_group
*srcsg
, *dstsg
;
252 srcsg
= session_group_contains(src
);
253 dstsg
= session_group_contains(dst
);
254 if (src
!= dst
&& srcsg
!= NULL
&& dstsg
!= NULL
&& srcsg
== dstsg
) {
255 xasprintf(cause
, "sessions are grouped");
261 dstwl
= winlink_find_by_index(&dst
->windows
, dstidx
);
263 if (dstwl
->window
== srcwl
->window
) {
264 xasprintf(cause
, "same index: %d", dstidx
);
269 * Can't use session_detach as it will destroy session
270 * if this makes it empty.
272 notify_session_window("window-unlinked", dst
,
274 dstwl
->flags
&= ~WINLINK_ALERTFLAGS
;
275 winlink_stack_remove(&dst
->lastw
, dstwl
);
276 winlink_remove(&dst
->windows
, dstwl
);
278 /* Force select/redraw if current. */
279 if (dstwl
== dst
->curw
) {
287 dstidx
= -1 - options_get_number(dst
->options
, "base-index");
288 dstwl
= session_attach(dst
, srcwl
->window
, dstidx
, cause
);
293 session_select(dst
, dstwl
->idx
);
294 server_redraw_session_group(dst
);
300 server_unlink_window(struct session
*s
, struct winlink
*wl
)
302 if (session_detach(s
, wl
))
303 server_destroy_session_group(s
);
305 server_redraw_session_group(s
);
309 server_destroy_pane(struct window_pane
*wp
, int notify
)
311 struct window
*w
= wp
->window
;
312 struct screen_write_ctx ctx
;
317 u_int sx
= screen_size_x(&wp
->base
);
318 u_int sy
= screen_size_y(&wp
->base
);
321 bufferevent_free(wp
->event
);
327 remain_on_exit
= options_get_number(wp
->options
, "remain-on-exit");
328 if (remain_on_exit
!= 0 && (~wp
->flags
& PANE_STATUSREADY
))
330 switch (remain_on_exit
) {
334 if (WIFEXITED(wp
->status
) && WEXITSTATUS(wp
->status
) == 0)
338 if (wp
->flags
& PANE_STATUSDRAWN
)
340 wp
->flags
|= PANE_STATUSDRAWN
;
342 gettimeofday(&wp
->dead_time
, NULL
);
344 notify_pane("pane-died", wp
);
346 s
= options_get_string(wp
->options
, "remain-on-exit-format");
348 screen_write_start_pane(&ctx
, wp
, &wp
->base
);
349 screen_write_scrollregion(&ctx
, 0, sy
- 1);
350 screen_write_cursormove(&ctx
, 0, sy
- 1, 0);
351 screen_write_linefeed(&ctx
, 1, 8);
352 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
354 expanded
= format_single(NULL
, s
, NULL
, NULL
, NULL
, wp
);
355 format_draw(&ctx
, &gc
, sx
, expanded
, NULL
, 0);
358 screen_write_stop(&ctx
);
360 wp
->base
.mode
&= ~MODE_CURSOR
;
362 wp
->flags
|= PANE_REDRAW
;
367 notify_pane("pane-exited", wp
);
369 server_unzoom_window(w
);
370 server_client_remove_pane(wp
);
371 layout_close_pane(wp
);
372 window_remove_pane(w
, wp
);
374 if (TAILQ_EMPTY(&w
->panes
))
375 server_kill_window(w
, 1);
377 server_redraw_window(w
);
381 server_destroy_session_group(struct session
*s
)
383 struct session_group
*sg
;
386 if ((sg
= session_group_contains(s
)) == NULL
) {
387 server_destroy_session(s
);
388 session_destroy(s
, 1, __func__
);
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_find_session(struct session
*s
,
399 int (*f
)(struct session
*, struct session
*))
401 struct session
*s_loop
, *s_out
= NULL
;
403 RB_FOREACH(s_loop
, sessions
, &sessions
) {
404 if (s_loop
!= s
&& (s_out
== NULL
|| f(s_loop
, s_out
)))
411 server_newer_session(struct session
*s_loop
, struct session
*s_out
)
413 return (timercmp(&s_loop
->activity_time
, &s_out
->activity_time
, <));
417 server_newer_detached_session(struct session
*s_loop
, struct session
*s_out
)
419 if (s_loop
->attached
)
421 return (server_newer_session(s_loop
, s_out
));
425 server_destroy_session(struct session
*s
)
428 struct session
*s_new
= NULL
;
429 int detach_on_destroy
;
431 detach_on_destroy
= options_get_number(s
->options
, "detach-on-destroy");
432 if (detach_on_destroy
== 0)
433 s_new
= server_find_session(s
, server_newer_session
);
434 else if (detach_on_destroy
== 2)
435 s_new
= server_find_session(s
, server_newer_detached_session
);
436 else if (detach_on_destroy
== 3)
437 s_new
= session_previous_session(s
);
438 else if (detach_on_destroy
== 4)
439 s_new
= session_next_session(s
);
442 TAILQ_FOREACH(c
, &clients
, entry
) {
446 c
->last_session
= NULL
;
447 server_client_set_session(c
, s_new
);
449 c
->flags
|= CLIENT_EXIT
;
455 server_check_unattached(void)
460 * If any sessions are no longer attached and have destroy-unattached
463 RB_FOREACH(s
, sessions
, &sessions
) {
464 if (s
->attached
!= 0)
466 if (options_get_number (s
->options
, "destroy-unattached"))
467 session_destroy(s
, 1, __func__
);
472 server_unzoom_window(struct window
*w
)
474 if (window_unzoom(w
) == 0)
475 server_redraw_window(w
);