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>
30 /* Global session list. */
31 struct sessions sessions
;
32 struct sessions dead_sessions
;
34 struct session_groups session_groups
;
36 struct winlink
*session_next_alert(struct winlink
*);
37 struct winlink
*session_previous_alert(struct winlink
*);
39 RB_GENERATE(sessions
, session
, entry
, session_cmp
);
42 session_cmp(struct session
*s1
, struct session
*s2
)
44 return (strcmp(s1
->name
, s2
->name
));
48 * Find if session is still alive. This is true if it is still on the global
52 session_alive(struct session
*s
)
54 struct session
*s_loop
;
56 RB_FOREACH(s_loop
, sessions
, &sessions
) {
63 /* Find session by name. */
65 session_find(const char *name
)
69 s
.name
= (char *) name
;
70 return (RB_FIND(sessions
, &sessions
, &s
));
73 /* Find session by index. */
75 session_find_by_index(u_int idx
)
79 RB_FOREACH(s
, sessions
, &sessions
) {
86 /* Create a new session. */
88 session_create(const char *name
, const char *cmd
, const char *cwd
,
89 struct environ
*env
, struct termios
*tio
, int idx
, u_int sx
, u_int sy
,
94 s
= xmalloc(sizeof *s
);
98 if (gettimeofday(&s
->creation_time
, NULL
) != 0)
99 fatal("gettimeofday failed");
100 session_update_activity(s
);
102 s
->cwd
= xstrdup(cwd
);
105 TAILQ_INIT(&s
->lastw
);
106 RB_INIT(&s
->windows
);
108 options_init(&s
->options
, &global_s_options
);
109 environ_init(&s
->environ
);
111 environ_copy(env
, &s
->environ
);
115 s
->tio
= xmalloc(sizeof *s
->tio
);
116 memcpy(s
->tio
, tio
, sizeof *s
->tio
);
122 s
->idx
= next_session
++;
124 s
->name
= xstrdup(name
);
126 xasprintf(&s
->name
, "%u", s
->idx
);
127 RB_INSERT(sessions
, &sessions
, s
);
130 if (session_new(s
, NULL
, cmd
, cwd
, idx
, cause
) == NULL
) {
134 session_select(s
, RB_ROOT(&s
->windows
)->idx
);
137 log_debug("session %s created", s
->name
);
142 /* Destroy a session. */
144 session_destroy(struct session
*s
)
146 log_debug("session %s destroyed", s
->name
);
148 RB_REMOVE(sessions
, &sessions
, s
);
153 session_group_remove(s
);
154 environ_free(&s
->environ
);
155 options_free(&s
->options
);
157 while (!TAILQ_EMPTY(&s
->lastw
))
158 winlink_stack_remove(&s
->lastw
, TAILQ_FIRST(&s
->lastw
));
159 while (!RB_EMPTY(&s
->windows
))
160 winlink_remove(&s
->windows
, RB_ROOT(&s
->windows
));
164 RB_INSERT(sessions
, &dead_sessions
, s
);
167 /* Update session active time. */
169 session_update_activity(struct session
*s
)
171 if (gettimeofday(&s
->activity_time
, NULL
) != 0)
172 fatal("gettimeofday");
175 /* Find the next usable session. */
177 session_next_session(struct session
*s
)
181 if (RB_EMPTY(&sessions
) || !session_alive(s
))
184 s2
= RB_NEXT(sessions
, &sessions
, s
);
186 s2
= RB_MIN(sessions
, &sessions
);
192 /* Find the previous usable session. */
194 session_previous_session(struct session
*s
)
198 if (RB_EMPTY(&sessions
) || !session_alive(s
))
201 s2
= RB_PREV(sessions
, &sessions
, s
);
203 s2
= RB_MAX(sessions
, &sessions
);
209 /* Create a new window on a session. */
211 session_new(struct session
*s
,
212 const char *name
, const char *cmd
, const char *cwd
, int idx
, char **cause
)
220 if ((wl
= winlink_add(&s
->windows
, idx
)) == NULL
) {
221 xasprintf(cause
, "index in use: %d", idx
);
226 environ_copy(&global_environ
, &env
);
227 environ_copy(&s
->environ
, &env
);
228 server_fill_environ(s
, &env
);
230 shell
= options_get_string(&s
->options
, "default-shell");
231 if (*shell
== '\0' || areshell(shell
))
232 shell
= _PATH_BSHELL
;
234 hlimit
= options_get_number(&s
->options
, "history-limit");
236 name
, cmd
, shell
, cwd
, &env
, s
->tio
, s
->sx
, s
->sy
, hlimit
, cause
);
238 winlink_remove(&s
->windows
, wl
);
242 winlink_set_window(wl
, w
);
245 if (options_get_number(&s
->options
, "set-remain-on-exit"))
246 options_set_number(&w
->options
, "remain-on-exit", 1);
248 session_group_synchronize_from(s
);
252 /* Attach a window to a session. */
254 session_attach(struct session
*s
, struct window
*w
, int idx
, char **cause
)
258 if ((wl
= winlink_add(&s
->windows
, idx
)) == NULL
) {
259 xasprintf(cause
, "index in use: %d", idx
);
262 winlink_set_window(wl
, w
);
264 session_group_synchronize_from(s
);
268 /* Detach a window from a session. */
270 session_detach(struct session
*s
, struct winlink
*wl
)
273 session_last(s
) != 0 && session_previous(s
, 0) != 0)
276 wl
->flags
&= ~WINLINK_ALERTFLAGS
;
277 winlink_stack_remove(&s
->lastw
, wl
);
278 winlink_remove(&s
->windows
, wl
);
279 session_group_synchronize_from(s
);
280 if (RB_EMPTY(&s
->windows
)) {
287 /* Return if session has window. */
289 session_has(struct session
*s
, struct window
*w
)
293 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
301 session_next_alert(struct winlink
*wl
)
304 if (wl
->flags
& WINLINK_ALERTFLAGS
)
306 wl
= winlink_next(wl
);
311 /* Move session to next window. */
313 session_next(struct session
*s
, int alert
)
320 wl
= winlink_next(s
->curw
);
322 wl
= session_next_alert(wl
);
324 wl
= RB_MIN(winlinks
, &s
->windows
);
325 if (alert
&& ((wl
= session_next_alert(wl
)) == NULL
))
330 winlink_stack_remove(&s
->lastw
, wl
);
331 winlink_stack_push(&s
->lastw
, s
->curw
);
333 wl
->flags
&= ~WINLINK_ALERTFLAGS
;
338 session_previous_alert(struct winlink
*wl
)
341 if (wl
->flags
& WINLINK_ALERTFLAGS
)
343 wl
= winlink_previous(wl
);
348 /* Move session to previous window. */
350 session_previous(struct session
*s
, int alert
)
357 wl
= winlink_previous(s
->curw
);
359 wl
= session_previous_alert(wl
);
361 wl
= RB_MAX(winlinks
, &s
->windows
);
362 if (alert
&& (wl
= session_previous_alert(wl
)) == NULL
)
367 winlink_stack_remove(&s
->lastw
, wl
);
368 winlink_stack_push(&s
->lastw
, s
->curw
);
370 wl
->flags
&= ~WINLINK_ALERTFLAGS
;
374 /* Move session to specific window. */
376 session_select(struct session
*s
, int idx
)
380 wl
= winlink_find_by_index(&s
->windows
, idx
);
385 winlink_stack_remove(&s
->lastw
, wl
);
386 winlink_stack_push(&s
->lastw
, s
->curw
);
388 wl
->flags
&= ~WINLINK_ALERTFLAGS
;
392 /* Move session to last used window. */
394 session_last(struct session
*s
)
398 wl
= TAILQ_FIRST(&s
->lastw
);
404 winlink_stack_remove(&s
->lastw
, wl
);
405 winlink_stack_push(&s
->lastw
, s
->curw
);
407 wl
->flags
&= ~WINLINK_ALERTFLAGS
;
411 /* Find the session group containing a session. */
412 struct session_group
*
413 session_group_find(struct session
*target
)
415 struct session_group
*sg
;
418 TAILQ_FOREACH(sg
, &session_groups
, entry
) {
419 TAILQ_FOREACH(s
, &sg
->sessions
, gentry
) {
427 /* Find session group index. */
429 session_group_index(struct session_group
*sg
)
431 struct session_group
*sg2
;
435 TAILQ_FOREACH(sg2
, &session_groups
, entry
) {
441 fatalx("session group not found");
445 * Add a session to the session group containing target, creating it if
449 session_group_add(struct session
*target
, struct session
*s
)
451 struct session_group
*sg
;
453 if ((sg
= session_group_find(target
)) == NULL
) {
454 sg
= xmalloc(sizeof *sg
);
455 TAILQ_INSERT_TAIL(&session_groups
, sg
, entry
);
456 TAILQ_INIT(&sg
->sessions
);
457 TAILQ_INSERT_TAIL(&sg
->sessions
, target
, gentry
);
459 TAILQ_INSERT_TAIL(&sg
->sessions
, s
, gentry
);
462 /* Remove a session from its group and destroy the group if empty. */
464 session_group_remove(struct session
*s
)
466 struct session_group
*sg
;
468 if ((sg
= session_group_find(s
)) == NULL
)
470 TAILQ_REMOVE(&sg
->sessions
, s
, gentry
);
471 if (TAILQ_NEXT(TAILQ_FIRST(&sg
->sessions
), gentry
) == NULL
)
472 TAILQ_REMOVE(&sg
->sessions
, TAILQ_FIRST(&sg
->sessions
), gentry
);
473 if (TAILQ_EMPTY(&sg
->sessions
)) {
474 TAILQ_REMOVE(&session_groups
, sg
, entry
);
479 /* Synchronize a session to its session group. */
481 session_group_synchronize_to(struct session
*s
)
483 struct session_group
*sg
;
484 struct session
*target
;
486 if ((sg
= session_group_find(s
)) == NULL
)
490 TAILQ_FOREACH(target
, &sg
->sessions
, gentry
) {
494 session_group_synchronize1(target
, s
);
497 /* Synchronize a session group to a session. */
499 session_group_synchronize_from(struct session
*target
)
501 struct session_group
*sg
;
504 if ((sg
= session_group_find(target
)) == NULL
)
507 TAILQ_FOREACH(s
, &sg
->sessions
, gentry
) {
509 session_group_synchronize1(target
, s
);
514 * Synchronize a session with a target session. This means destroying all
515 * winlinks then recreating them, then updating the current window, last window
519 session_group_synchronize1(struct session
*target
, struct session
*s
)
521 struct winlinks old_windows
, *ww
;
522 struct winlink_stack old_lastw
;
523 struct winlink
*wl
, *wl2
;
525 /* Don't do anything if the session is empty (it'll be destroyed). */
526 ww
= &target
->windows
;
530 /* If the current window has vanished, move to the next now. */
531 if (s
->curw
!= NULL
&&
532 winlink_find_by_index(ww
, s
->curw
->idx
) == NULL
&&
533 session_last(s
) != 0 && session_previous(s
, 0) != 0)
536 /* Save the old pointer and reset it. */
537 memcpy(&old_windows
, &s
->windows
, sizeof old_windows
);
538 RB_INIT(&s
->windows
);
540 /* Link all the windows from the target. */
541 RB_FOREACH(wl
, winlinks
, ww
) {
542 wl2
= winlink_add(&s
->windows
, wl
->idx
);
543 winlink_set_window(wl2
, wl
->window
);
544 wl2
->flags
|= wl
->flags
& WINLINK_ALERTFLAGS
;
547 /* Fix up the current window. */
549 s
->curw
= winlink_find_by_index(&s
->windows
, s
->curw
->idx
);
551 s
->curw
= winlink_find_by_index(&s
->windows
, target
->curw
->idx
);
553 /* Fix up the last window stack. */
554 memcpy(&old_lastw
, &s
->lastw
, sizeof old_lastw
);
555 TAILQ_INIT(&s
->lastw
);
556 TAILQ_FOREACH(wl
, &old_lastw
, sentry
) {
557 wl2
= winlink_find_by_index(&s
->windows
, wl
->idx
);
559 TAILQ_INSERT_TAIL(&s
->lastw
, wl2
, sentry
);
562 /* Then free the old winlinks list. */
563 while (!RB_EMPTY(&old_windows
)) {
564 wl
= RB_ROOT(&old_windows
);
565 winlink_remove(&old_windows
, wl
);