Store sessions in an RB tree by name rather than a list, this is tidier

and allows them to easily be shown sorted in various lists
(list-sessions/choose-sessions).

Keep a session index which is used in a couple of places internally but
make it an ever-increasing number rather than filling in gaps with new
sessions.
This commit is contained in:
Nicholas Marriott
2010-12-21 22:37:59 +00:00
parent 1b8488ee75
commit acf13ce978
12 changed files with 143 additions and 183 deletions

View File

@ -30,13 +30,10 @@ void server_callback_identify(int, short, void *);
void
server_fill_environ(struct session *s, struct environ *env)
{
char tmuxvar[MAXPATHLEN], *term;
u_int idx;
char tmuxvar[MAXPATHLEN], *term;
if (session_index(s, &idx) != 0)
fatalx("session not found");
xsnprintf(tmuxvar, sizeof tmuxvar,
"%s,%ld,%u", socket_path, (long) getpid(), idx);
"%s,%ld,%u", socket_path, (long) getpid(), s->idx);
environ_set(env, "TMUX", tmuxvar);
term = options_get_string(&s->options, "default-terminal");
@ -175,7 +172,6 @@ void
server_status_window(struct window *w)
{
struct session *s;
u_int i;
/*
* This is slightly different. We want to redraw the status line of any
@ -183,9 +179,8 @@ server_status_window(struct window *w)
* current window.
*/
for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
s = ARRAY_ITEM(&sessions, i);
if (s != NULL && session_has(s, w) != NULL)
RB_FOREACH(s, sessions, &sessions) {
if (session_has(s, w) != NULL)
server_status_session(s);
}
}
@ -246,11 +241,9 @@ server_kill_window(struct window *w)
{
struct session *s;
struct winlink *wl;
u_int i;
for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
s = ARRAY_ITEM(&sessions, i);
if (s == NULL || session_has(s, w) == NULL)
RB_FOREACH(s, sessions, &sessions) {
if (session_has(s, w) == NULL)
continue;
while ((wl = winlink_find_by_window(&s->windows, w)) != NULL) {
if (session_detach(s, wl)) {
@ -365,12 +358,10 @@ struct session *
server_next_session(struct session *s)
{
struct session *s_loop, *s_out;
u_int i;
s_out = NULL;
for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
s_loop = ARRAY_ITEM(&sessions, i);
if (s_loop == NULL || s_loop == s)
RB_FOREACH(s_loop, sessions, &sessions) {
if (s_loop == s)
continue;
if (s_out == NULL ||
timercmp(&s_loop->activity_time, &s_out->activity_time, <))
@ -411,15 +402,13 @@ void
server_check_unattached (void)
{
struct session *s;
u_int i;
/*
* If any sessions are no longer attached and have destroy-unattached
* set, collect them.
*/
for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
s = ARRAY_ITEM(&sessions, i);
if (s == NULL || !(s->flags & SESSION_UNATTACHED))
RB_FOREACH(s, sessions, &sessions) {
if (!(s->flags & SESSION_UNATTACHED))
continue;
if (options_get_number (&s->options, "destroy-unattached"))
session_destroy(s);