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
;
33 u_int next_session_id
;
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 id. */
75 session_find_by_id(u_int id
)
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
);
123 s
->name
= xstrdup(name
);
124 s
->id
= next_session_id
++;
128 s
->id
= next_session_id
++;
130 xasprintf(&s
->name
, "%u", s
->id
);
131 } while (RB_FIND(sessions
, &sessions
, s
) != NULL
);
133 RB_INSERT(sessions
, &sessions
, s
);
136 if (session_new(s
, NULL
, cmd
, cwd
, idx
, cause
) == NULL
) {
140 session_select(s
, RB_ROOT(&s
->windows
)->idx
);
143 log_debug("session %s created", s
->name
);
144 notify_session_created(s
);
149 /* Destroy a session. */
151 session_destroy(struct session
*s
)
155 log_debug("session %s destroyed", s
->name
);
157 RB_REMOVE(sessions
, &sessions
, s
);
158 notify_session_closed(s
);
162 session_group_remove(s
);
163 environ_free(&s
->environ
);
164 options_free(&s
->options
);
166 while (!TAILQ_EMPTY(&s
->lastw
))
167 winlink_stack_remove(&s
->lastw
, TAILQ_FIRST(&s
->lastw
));
168 while (!RB_EMPTY(&s
->windows
)) {
169 wl
= RB_ROOT(&s
->windows
);
170 notify_window_unlinked(s
, wl
->window
);
171 winlink_remove(&s
->windows
, wl
);
176 RB_INSERT(sessions
, &dead_sessions
, s
);
179 /* Check a session name is valid: not empty and no colons. */
181 session_check_name(const char *name
)
183 return (*name
!= '\0' && strchr(name
, ':') == NULL
);
186 /* Update session active time. */
188 session_update_activity(struct session
*s
)
190 if (gettimeofday(&s
->activity_time
, NULL
) != 0)
191 fatal("gettimeofday");
194 /* Find the next usable session. */
196 session_next_session(struct session
*s
)
200 if (RB_EMPTY(&sessions
) || !session_alive(s
))
203 s2
= RB_NEXT(sessions
, &sessions
, s
);
205 s2
= RB_MIN(sessions
, &sessions
);
211 /* Find the previous usable session. */
213 session_previous_session(struct session
*s
)
217 if (RB_EMPTY(&sessions
) || !session_alive(s
))
220 s2
= RB_PREV(sessions
, &sessions
, s
);
222 s2
= RB_MAX(sessions
, &sessions
);
228 /* Create a new window on a session. */
230 session_new(struct session
*s
,
231 const char *name
, const char *cmd
, const char *cwd
, int idx
, char **cause
)
239 if ((wl
= winlink_add(&s
->windows
, idx
)) == NULL
) {
240 xasprintf(cause
, "index in use: %d", idx
);
245 environ_copy(&global_environ
, &env
);
246 environ_copy(&s
->environ
, &env
);
247 server_fill_environ(s
, &env
);
249 shell
= options_get_string(&s
->options
, "default-shell");
250 if (*shell
== '\0' || areshell(shell
))
251 shell
= _PATH_BSHELL
;
253 hlimit
= options_get_number(&s
->options
, "history-limit");
255 name
, cmd
, shell
, cwd
, &env
, s
->tio
, s
->sx
, s
->sy
, hlimit
, cause
);
257 winlink_remove(&s
->windows
, wl
);
261 winlink_set_window(wl
, w
);
262 notify_window_linked(s
, w
);
265 if (options_get_number(&s
->options
, "set-remain-on-exit"))
266 options_set_number(&w
->options
, "remain-on-exit", 1);
268 session_group_synchronize_from(s
);
272 /* Attach a window to a session. */
274 session_attach(struct session
*s
, struct window
*w
, int idx
, char **cause
)
278 if ((wl
= winlink_add(&s
->windows
, idx
)) == NULL
) {
279 xasprintf(cause
, "index in use: %d", idx
);
282 winlink_set_window(wl
, w
);
283 notify_window_linked(s
, w
);
285 session_group_synchronize_from(s
);
289 /* Detach a window from a session. */
291 session_detach(struct session
*s
, struct winlink
*wl
)
294 session_last(s
) != 0 && session_previous(s
, 0) != 0)
297 wl
->flags
&= ~WINLINK_ALERTFLAGS
;
298 notify_window_unlinked(s
, wl
->window
);
299 winlink_stack_remove(&s
->lastw
, wl
);
300 winlink_remove(&s
->windows
, wl
);
301 session_group_synchronize_from(s
);
302 if (RB_EMPTY(&s
->windows
)) {
309 /* Return if session has window. */
311 session_has(struct session
*s
, struct window
*w
)
315 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
323 session_next_alert(struct winlink
*wl
)
326 if (wl
->flags
& WINLINK_ALERTFLAGS
)
328 wl
= winlink_next(wl
);
333 /* Move session to next window. */
335 session_next(struct session
*s
, int alert
)
342 wl
= winlink_next(s
->curw
);
344 wl
= session_next_alert(wl
);
346 wl
= RB_MIN(winlinks
, &s
->windows
);
347 if (alert
&& ((wl
= session_next_alert(wl
)) == NULL
))
350 return (session_set_current(s
, wl
));
354 session_previous_alert(struct winlink
*wl
)
357 if (wl
->flags
& WINLINK_ALERTFLAGS
)
359 wl
= winlink_previous(wl
);
364 /* Move session to previous window. */
366 session_previous(struct session
*s
, int alert
)
373 wl
= winlink_previous(s
->curw
);
375 wl
= session_previous_alert(wl
);
377 wl
= RB_MAX(winlinks
, &s
->windows
);
378 if (alert
&& (wl
= session_previous_alert(wl
)) == NULL
)
381 return (session_set_current(s
, wl
));
384 /* Move session to specific window. */
386 session_select(struct session
*s
, int idx
)
390 wl
= winlink_find_by_index(&s
->windows
, idx
);
391 return (session_set_current(s
, wl
));
394 /* Move session to last used window. */
396 session_last(struct session
*s
)
400 wl
= TAILQ_FIRST(&s
->lastw
);
406 return (session_set_current(s
, wl
));
409 /* Set current winlink to wl .*/
411 session_set_current(struct session
*s
, struct winlink
*wl
)
418 winlink_stack_remove(&s
->lastw
, wl
);
419 winlink_stack_push(&s
->lastw
, s
->curw
);
421 winlink_clear_flags(wl
);
425 /* Find the session group containing a session. */
426 struct session_group
*
427 session_group_find(struct session
*target
)
429 struct session_group
*sg
;
432 TAILQ_FOREACH(sg
, &session_groups
, entry
) {
433 TAILQ_FOREACH(s
, &sg
->sessions
, gentry
) {
441 /* Find session group index. */
443 session_group_index(struct session_group
*sg
)
445 struct session_group
*sg2
;
449 TAILQ_FOREACH(sg2
, &session_groups
, entry
) {
455 fatalx("session group not found");
459 * Add a session to the session group containing target, creating it if
463 session_group_add(struct session
*target
, struct session
*s
)
465 struct session_group
*sg
;
467 if ((sg
= session_group_find(target
)) == NULL
) {
468 sg
= xmalloc(sizeof *sg
);
469 TAILQ_INSERT_TAIL(&session_groups
, sg
, entry
);
470 TAILQ_INIT(&sg
->sessions
);
471 TAILQ_INSERT_TAIL(&sg
->sessions
, target
, gentry
);
473 TAILQ_INSERT_TAIL(&sg
->sessions
, s
, gentry
);
476 /* Remove a session from its group and destroy the group if empty. */
478 session_group_remove(struct session
*s
)
480 struct session_group
*sg
;
482 if ((sg
= session_group_find(s
)) == NULL
)
484 TAILQ_REMOVE(&sg
->sessions
, s
, gentry
);
485 if (TAILQ_NEXT(TAILQ_FIRST(&sg
->sessions
), gentry
) == NULL
)
486 TAILQ_REMOVE(&sg
->sessions
, TAILQ_FIRST(&sg
->sessions
), gentry
);
487 if (TAILQ_EMPTY(&sg
->sessions
)) {
488 TAILQ_REMOVE(&session_groups
, sg
, entry
);
493 /* Synchronize a session to its session group. */
495 session_group_synchronize_to(struct session
*s
)
497 struct session_group
*sg
;
498 struct session
*target
;
500 if ((sg
= session_group_find(s
)) == NULL
)
504 TAILQ_FOREACH(target
, &sg
->sessions
, gentry
) {
508 session_group_synchronize1(target
, s
);
511 /* Synchronize a session group to a session. */
513 session_group_synchronize_from(struct session
*target
)
515 struct session_group
*sg
;
518 if ((sg
= session_group_find(target
)) == NULL
)
521 TAILQ_FOREACH(s
, &sg
->sessions
, gentry
) {
523 session_group_synchronize1(target
, s
);
528 * Synchronize a session with a target session. This means destroying all
529 * winlinks then recreating them, then updating the current window, last window
533 session_group_synchronize1(struct session
*target
, struct session
*s
)
535 struct winlinks old_windows
, *ww
;
536 struct winlink_stack old_lastw
;
537 struct winlink
*wl
, *wl2
;
539 /* Don't do anything if the session is empty (it'll be destroyed). */
540 ww
= &target
->windows
;
544 /* If the current window has vanished, move to the next now. */
545 if (s
->curw
!= NULL
&&
546 winlink_find_by_index(ww
, s
->curw
->idx
) == NULL
&&
547 session_last(s
) != 0 && session_previous(s
, 0) != 0)
550 /* Save the old pointer and reset it. */
551 memcpy(&old_windows
, &s
->windows
, sizeof old_windows
);
552 RB_INIT(&s
->windows
);
554 /* Link all the windows from the target. */
555 RB_FOREACH(wl
, winlinks
, ww
) {
556 wl2
= winlink_add(&s
->windows
, wl
->idx
);
557 winlink_set_window(wl2
, wl
->window
);
558 notify_window_linked(s
, wl2
->window
);
559 wl2
->flags
|= wl
->flags
& WINLINK_ALERTFLAGS
;
562 /* Fix up the current window. */
564 s
->curw
= winlink_find_by_index(&s
->windows
, s
->curw
->idx
);
566 s
->curw
= winlink_find_by_index(&s
->windows
, target
->curw
->idx
);
568 /* Fix up the last window stack. */
569 memcpy(&old_lastw
, &s
->lastw
, sizeof old_lastw
);
570 TAILQ_INIT(&s
->lastw
);
571 TAILQ_FOREACH(wl
, &old_lastw
, sentry
) {
572 wl2
= winlink_find_by_index(&s
->windows
, wl
->idx
);
574 TAILQ_INSERT_TAIL(&s
->lastw
, wl2
, sentry
);
577 /* Then free the old winlinks list. */
578 while (!RB_EMPTY(&old_windows
)) {
579 wl
= RB_ROOT(&old_windows
);
580 if (winlink_find_by_window_id(&s
->windows
, wl
->window
->id
) == NULL
)
581 notify_window_unlinked(s
, wl
->window
);
582 winlink_remove(&old_windows
, wl
);
586 /* Renumber the windows across winlinks attached to a specific session. */
588 session_renumber_windows(struct session
*s
)
590 struct winlink
*wl
, *wl1
, *wl_new
;
591 struct winlinks old_wins
;
592 struct winlink_stack old_lastw
;
593 int new_idx
, new_curw_idx
;
595 /* Save and replace old window list. */
596 memcpy(&old_wins
, &s
->windows
, sizeof old_wins
);
597 RB_INIT(&s
->windows
);
599 /* Start renumbering from the base-index if it's set. */
600 new_idx
= options_get_number(&s
->options
, "base-index");
603 /* Go through the winlinks and assign new indexes. */
604 RB_FOREACH(wl
, winlinks
, &old_wins
) {
605 wl_new
= winlink_add(&s
->windows
, new_idx
);
606 winlink_set_window(wl_new
, wl
->window
);
607 wl_new
->flags
|= wl
->flags
& WINLINK_ALERTFLAGS
;
610 new_curw_idx
= wl_new
->idx
;
615 /* Fix the stack of last windows now. */
616 memcpy(&old_lastw
, &s
->lastw
, sizeof old_lastw
);
617 TAILQ_INIT(&s
->lastw
);
618 TAILQ_FOREACH(wl
, &old_lastw
, sentry
) {
619 wl_new
= winlink_find_by_window(&s
->windows
, wl
->window
);
621 TAILQ_INSERT_TAIL(&s
->lastw
, wl_new
, sentry
);
624 /* Set the current window. */
625 s
->curw
= winlink_find_by_index(&s
->windows
, new_curw_idx
);
627 /* Free the old winlinks (reducing window references too). */
628 RB_FOREACH_SAFE(wl
, winlinks
, &old_wins
, wl1
)
629 winlink_remove(&old_wins
, wl
);