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>
31 struct sessions sessions
;
32 u_int next_session_id
;
33 struct session_groups session_groups
= RB_INITIALIZER(&session_groups
);
35 static void session_free(int, short, void *);
37 static void session_lock_timer(int, short, void *);
39 static struct winlink
*session_next_alert(struct winlink
*);
40 static struct winlink
*session_previous_alert(struct winlink
*);
42 static void session_group_remove(struct session
*);
43 static void session_group_synchronize1(struct session
*, struct session
*);
46 session_cmp(struct session
*s1
, struct session
*s2
)
48 return (strcmp(s1
->name
, s2
->name
));
50 RB_GENERATE(sessions
, session
, entry
, session_cmp
);
53 session_group_cmp(struct session_group
*s1
, struct session_group
*s2
)
55 return (strcmp(s1
->name
, s2
->name
));
57 RB_GENERATE_STATIC(session_groups
, session_group
, entry
, session_group_cmp
);
60 * Find if session is still alive. This is true if it is still on the global
64 session_alive(struct session
*s
)
66 struct session
*s_loop
;
68 RB_FOREACH(s_loop
, sessions
, &sessions
) {
75 /* Find session by name. */
77 session_find(const char *name
)
81 s
.name
= (char *) name
;
82 return (RB_FIND(sessions
, &sessions
, &s
));
85 /* Find session by id parsed from a string. */
87 session_find_by_id_str(const char *s
)
95 id
= strtonum(s
+ 1, 0, UINT_MAX
, &errstr
);
98 return (session_find_by_id(id
));
101 /* Find session by id. */
103 session_find_by_id(u_int id
)
107 RB_FOREACH(s
, sessions
, &sessions
) {
114 /* Create a new session. */
116 session_create(const char *prefix
, const char *name
, const char *cwd
,
117 struct environ
*env
, struct options
*oo
, struct termios
*tio
)
121 s
= xcalloc(1, sizeof *s
);
125 s
->cwd
= xstrdup(cwd
);
127 TAILQ_INIT(&s
->lastw
);
128 RB_INIT(&s
->windows
);
133 status_update_cache(s
);
137 s
->tio
= xmalloc(sizeof *s
->tio
);
138 memcpy(s
->tio
, tio
, sizeof *s
->tio
);
142 s
->name
= xstrdup(name
);
143 s
->id
= next_session_id
++;
146 s
->id
= next_session_id
++;
149 xasprintf(&s
->name
, "%s-%u", prefix
, s
->id
);
151 xasprintf(&s
->name
, "%u", s
->id
);
152 } while (RB_FIND(sessions
, &sessions
, s
) != NULL
);
154 RB_INSERT(sessions
, &sessions
, s
);
156 log_debug("new session %s $%u", s
->name
, s
->id
);
158 if (gettimeofday(&s
->creation_time
, NULL
) != 0)
159 fatal("gettimeofday failed");
160 session_update_activity(s
, &s
->creation_time
);
165 /* Add a reference to a session. */
167 session_add_ref(struct session
*s
, const char *from
)
170 log_debug("%s: %s %s, now %d", __func__
, s
->name
, from
, s
->references
);
173 /* Remove a reference from a session. */
175 session_remove_ref(struct session
*s
, const char *from
)
178 log_debug("%s: %s %s, now %d", __func__
, s
->name
, from
, s
->references
);
180 if (s
->references
== 0)
181 event_once(-1, EV_TIMEOUT
, session_free
, s
, NULL
);
186 session_free(__unused
int fd
, __unused
short events
, void *arg
)
188 struct session
*s
= arg
;
190 log_debug("session %s freed (%d references)", s
->name
, s
->references
);
192 if (s
->references
== 0) {
193 environ_free(s
->environ
);
194 options_free(s
->options
);
201 /* Destroy a session. */
203 session_destroy(struct session
*s
, int notify
, const char *from
)
207 log_debug("session %s destroyed (%s)", s
->name
, from
);
213 RB_REMOVE(sessions
, &sessions
, s
);
215 notify_session("session-closed", s
);
219 if (event_initialized(&s
->lock_timer
))
220 event_del(&s
->lock_timer
);
222 session_group_remove(s
);
224 while (!TAILQ_EMPTY(&s
->lastw
))
225 winlink_stack_remove(&s
->lastw
, TAILQ_FIRST(&s
->lastw
));
226 while (!RB_EMPTY(&s
->windows
)) {
227 wl
= RB_ROOT(&s
->windows
);
228 notify_session_window("window-unlinked", s
, wl
->window
);
229 winlink_remove(&s
->windows
, wl
);
232 free((void *)s
->cwd
);
234 session_remove_ref(s
, __func__
);
237 /* Sanitize session name. */
239 session_check_name(const char *name
)
241 char *copy
, *cp
, *new_name
;
245 copy
= xstrdup(name
);
246 for (cp
= copy
; *cp
!= '\0'; cp
++) {
247 if (*cp
== ':' || *cp
== '.')
250 utf8_stravis(&new_name
, copy
, VIS_OCTAL
|VIS_CSTYLE
|VIS_TAB
|VIS_NL
);
255 /* Lock session if it has timed out. */
257 session_lock_timer(__unused
int fd
, __unused
short events
, void *arg
)
259 struct session
*s
= arg
;
261 if (s
->attached
== 0)
264 log_debug("session %s locked, activity time %lld", s
->name
,
265 (long long)s
->activity_time
.tv_sec
);
267 server_lock_session(s
);
271 /* Update activity time. */
273 session_update_activity(struct session
*s
, struct timeval
*from
)
278 gettimeofday(&s
->activity_time
, NULL
);
280 memcpy(&s
->activity_time
, from
, sizeof s
->activity_time
);
282 log_debug("session $%u %s activity %lld.%06d", s
->id
,
283 s
->name
, (long long)s
->activity_time
.tv_sec
,
284 (int)s
->activity_time
.tv_usec
);
286 if (evtimer_initialized(&s
->lock_timer
))
287 evtimer_del(&s
->lock_timer
);
289 evtimer_set(&s
->lock_timer
, session_lock_timer
, s
);
291 if (s
->attached
!= 0) {
293 tv
.tv_sec
= options_get_number(s
->options
, "lock-after-time");
295 evtimer_add(&s
->lock_timer
, &tv
);
299 /* Find the next usable session. */
301 session_next_session(struct session
*s
)
305 if (RB_EMPTY(&sessions
) || !session_alive(s
))
308 s2
= RB_NEXT(sessions
, &sessions
, s
);
310 s2
= RB_MIN(sessions
, &sessions
);
316 /* Find the previous usable session. */
318 session_previous_session(struct session
*s
)
322 if (RB_EMPTY(&sessions
) || !session_alive(s
))
325 s2
= RB_PREV(sessions
, &sessions
, s
);
327 s2
= RB_MAX(sessions
, &sessions
);
333 /* Attach a window to a session. */
335 session_attach(struct session
*s
, struct window
*w
, int idx
, char **cause
)
339 if ((wl
= winlink_add(&s
->windows
, idx
)) == NULL
) {
340 xasprintf(cause
, "index in use: %d", idx
);
344 winlink_set_window(wl
, w
);
345 notify_session_window("window-linked", s
, w
);
347 session_group_synchronize_from(s
);
351 /* Detach a window from a session. */
353 session_detach(struct session
*s
, struct winlink
*wl
)
356 session_last(s
) != 0 &&
357 session_previous(s
, 0) != 0)
360 wl
->flags
&= ~WINLINK_ALERTFLAGS
;
361 notify_session_window("window-unlinked", s
, wl
->window
);
362 winlink_stack_remove(&s
->lastw
, wl
);
363 winlink_remove(&s
->windows
, wl
);
365 session_group_synchronize_from(s
);
367 if (RB_EMPTY(&s
->windows
))
372 /* Return if session has window. */
374 session_has(struct session
*s
, struct window
*w
)
378 TAILQ_FOREACH(wl
, &w
->winlinks
, wentry
) {
379 if (wl
->session
== s
)
386 * Return 1 if a window is linked outside this session (not including session
387 * groups). The window must be in this session!
390 session_is_linked(struct session
*s
, struct window
*w
)
392 struct session_group
*sg
;
394 if ((sg
= session_group_contains(s
)) != NULL
)
395 return (w
->references
!= session_group_count(sg
));
396 return (w
->references
!= 1);
399 static struct winlink
*
400 session_next_alert(struct winlink
*wl
)
403 if (wl
->flags
& WINLINK_ALERTFLAGS
)
405 wl
= winlink_next(wl
);
410 /* Move session to next window. */
412 session_next(struct session
*s
, int alert
)
419 wl
= winlink_next(s
->curw
);
421 wl
= session_next_alert(wl
);
423 wl
= RB_MIN(winlinks
, &s
->windows
);
424 if (alert
&& ((wl
= session_next_alert(wl
)) == NULL
))
427 return (session_set_current(s
, wl
));
430 static struct winlink
*
431 session_previous_alert(struct winlink
*wl
)
434 if (wl
->flags
& WINLINK_ALERTFLAGS
)
436 wl
= winlink_previous(wl
);
441 /* Move session to previous window. */
443 session_previous(struct session
*s
, int alert
)
450 wl
= winlink_previous(s
->curw
);
452 wl
= session_previous_alert(wl
);
454 wl
= RB_MAX(winlinks
, &s
->windows
);
455 if (alert
&& (wl
= session_previous_alert(wl
)) == NULL
)
458 return (session_set_current(s
, wl
));
461 /* Move session to specific window. */
463 session_select(struct session
*s
, int idx
)
467 wl
= winlink_find_by_index(&s
->windows
, idx
);
468 return (session_set_current(s
, wl
));
471 /* Move session to last used window. */
473 session_last(struct session
*s
)
477 wl
= TAILQ_FIRST(&s
->lastw
);
483 return (session_set_current(s
, wl
));
486 /* Set current winlink to wl .*/
488 session_set_current(struct session
*s
, struct winlink
*wl
)
490 struct winlink
*old
= s
->curw
;
497 winlink_stack_remove(&s
->lastw
, wl
);
498 winlink_stack_push(&s
->lastw
, s
->curw
);
500 if (options_get_number(global_options
, "focus-events")) {
502 window_update_focus(old
->window
);
503 window_update_focus(wl
->window
);
505 winlink_clear_flags(wl
);
506 window_update_activity(wl
->window
);
507 tty_update_window_offset(wl
->window
);
508 notify_session("session-window-changed", s
);
512 /* Find the session group containing a session. */
513 struct session_group
*
514 session_group_contains(struct session
*target
)
516 struct session_group
*sg
;
519 RB_FOREACH(sg
, session_groups
, &session_groups
) {
520 TAILQ_FOREACH(s
, &sg
->sessions
, gentry
) {
528 /* Find session group by name. */
529 struct session_group
*
530 session_group_find(const char *name
)
532 struct session_group sg
;
535 return (RB_FIND(session_groups
, &session_groups
, &sg
));
538 /* Create a new session group. */
539 struct session_group
*
540 session_group_new(const char *name
)
542 struct session_group
*sg
;
544 if ((sg
= session_group_find(name
)) != NULL
)
547 sg
= xcalloc(1, sizeof *sg
);
548 sg
->name
= xstrdup(name
);
549 TAILQ_INIT(&sg
->sessions
);
551 RB_INSERT(session_groups
, &session_groups
, sg
);
555 /* Add a session to a session group. */
557 session_group_add(struct session_group
*sg
, struct session
*s
)
559 if (session_group_contains(s
) == NULL
)
560 TAILQ_INSERT_TAIL(&sg
->sessions
, s
, gentry
);
563 /* Remove a session from its group and destroy the group if empty. */
565 session_group_remove(struct session
*s
)
567 struct session_group
*sg
;
569 if ((sg
= session_group_contains(s
)) == NULL
)
571 TAILQ_REMOVE(&sg
->sessions
, s
, gentry
);
572 if (TAILQ_EMPTY(&sg
->sessions
)) {
573 RB_REMOVE(session_groups
, &session_groups
, sg
);
574 free((void *)sg
->name
);
579 /* Count number of sessions in session group. */
581 session_group_count(struct session_group
*sg
)
587 TAILQ_FOREACH(s
, &sg
->sessions
, gentry
)
592 /* Count number of clients attached to sessions in session group. */
594 session_group_attached_count(struct session_group
*sg
)
600 TAILQ_FOREACH(s
, &sg
->sessions
, gentry
)
605 /* Synchronize a session to its session group. */
607 session_group_synchronize_to(struct session
*s
)
609 struct session_group
*sg
;
610 struct session
*target
;
612 if ((sg
= session_group_contains(s
)) == NULL
)
616 TAILQ_FOREACH(target
, &sg
->sessions
, gentry
) {
621 session_group_synchronize1(target
, s
);
624 /* Synchronize a session group to a session. */
626 session_group_synchronize_from(struct session
*target
)
628 struct session_group
*sg
;
631 if ((sg
= session_group_contains(target
)) == NULL
)
634 TAILQ_FOREACH(s
, &sg
->sessions
, gentry
) {
636 session_group_synchronize1(target
, s
);
641 * Synchronize a session with a target session. This means destroying all
642 * winlinks then recreating them, then updating the current window, last window
646 session_group_synchronize1(struct session
*target
, struct session
*s
)
648 struct winlinks old_windows
, *ww
;
649 struct winlink_stack old_lastw
;
650 struct winlink
*wl
, *wl2
;
652 /* Don't do anything if the session is empty (it'll be destroyed). */
653 ww
= &target
->windows
;
657 /* If the current window has vanished, move to the next now. */
658 if (s
->curw
!= NULL
&&
659 winlink_find_by_index(ww
, s
->curw
->idx
) == NULL
&&
660 session_last(s
) != 0 && session_previous(s
, 0) != 0)
663 /* Save the old pointer and reset it. */
664 memcpy(&old_windows
, &s
->windows
, sizeof old_windows
);
665 RB_INIT(&s
->windows
);
667 /* Link all the windows from the target. */
668 RB_FOREACH(wl
, winlinks
, ww
) {
669 wl2
= winlink_add(&s
->windows
, wl
->idx
);
671 winlink_set_window(wl2
, wl
->window
);
672 notify_session_window("window-linked", s
, wl2
->window
);
673 wl2
->flags
|= wl
->flags
& WINLINK_ALERTFLAGS
;
676 /* Fix up the current window. */
678 s
->curw
= winlink_find_by_index(&s
->windows
, s
->curw
->idx
);
680 s
->curw
= winlink_find_by_index(&s
->windows
, target
->curw
->idx
);
682 /* Fix up the last window stack. */
683 memcpy(&old_lastw
, &s
->lastw
, sizeof old_lastw
);
684 TAILQ_INIT(&s
->lastw
);
685 TAILQ_FOREACH(wl
, &old_lastw
, sentry
) {
686 wl2
= winlink_find_by_index(&s
->windows
, wl
->idx
);
688 TAILQ_INSERT_TAIL(&s
->lastw
, wl2
, sentry
);
689 wl2
->flags
|= WINLINK_VISITED
;
693 /* Then free the old winlinks list. */
694 while (!RB_EMPTY(&old_windows
)) {
695 wl
= RB_ROOT(&old_windows
);
696 wl2
= winlink_find_by_window_id(&s
->windows
, wl
->window
->id
);
698 notify_session_window("window-unlinked", s
, wl
->window
);
699 winlink_remove(&old_windows
, wl
);
703 /* Renumber the windows across winlinks attached to a specific session. */
705 session_renumber_windows(struct session
*s
)
707 struct winlink
*wl
, *wl1
, *wl_new
;
708 struct winlinks old_wins
;
709 struct winlink_stack old_lastw
;
710 int new_idx
, new_curw_idx
, marked_idx
= -1;
712 /* Save and replace old window list. */
713 memcpy(&old_wins
, &s
->windows
, sizeof old_wins
);
714 RB_INIT(&s
->windows
);
716 /* Start renumbering from the base-index if it's set. */
717 new_idx
= options_get_number(s
->options
, "base-index");
720 /* Go through the winlinks and assign new indexes. */
721 RB_FOREACH(wl
, winlinks
, &old_wins
) {
722 wl_new
= winlink_add(&s
->windows
, new_idx
);
724 winlink_set_window(wl_new
, wl
->window
);
725 wl_new
->flags
|= wl
->flags
& WINLINK_ALERTFLAGS
;
727 if (wl
== marked_pane
.wl
)
728 marked_idx
= wl_new
->idx
;
730 new_curw_idx
= wl_new
->idx
;
735 /* Fix the stack of last windows now. */
736 memcpy(&old_lastw
, &s
->lastw
, sizeof old_lastw
);
737 TAILQ_INIT(&s
->lastw
);
738 TAILQ_FOREACH(wl
, &old_lastw
, sentry
) {
739 wl
->flags
&= ~WINLINK_VISITED
;
740 wl_new
= winlink_find_by_window(&s
->windows
, wl
->window
);
741 if (wl_new
!= NULL
) {
742 TAILQ_INSERT_TAIL(&s
->lastw
, wl_new
, sentry
);
743 wl_new
->flags
|= WINLINK_VISITED
;
747 /* Set the current window. */
748 if (marked_idx
!= -1) {
749 marked_pane
.wl
= winlink_find_by_index(&s
->windows
, marked_idx
);
750 if (marked_pane
.wl
== NULL
)
751 server_clear_marked();
753 s
->curw
= winlink_find_by_index(&s
->windows
, new_curw_idx
);
755 /* Free the old winlinks (reducing window references too). */
756 RB_FOREACH_SAFE(wl
, winlinks
, &old_wins
, wl1
)
757 winlink_remove(&old_wins
, wl
);