[BACK]Return to session.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/session.c, Revision 1.71

1.71    ! nicm        1: /* $OpenBSD: session.c,v 1.70 2016/10/19 09:22:07 nicm Exp $ */
1.1       nicm        2:
                      3: /*
1.62      nicm        4:  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1       nicm        5:  *
                      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.
                      9:  *
                     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.
                     17:  */
                     18:
                     19: #include <sys/types.h>
                     20: #include <sys/time.h>
                     21:
1.6       nicm       22: #include <paths.h>
1.1       nicm       23: #include <string.h>
                     24: #include <stdlib.h>
                     25: #include <unistd.h>
1.10      nicm       26: #include <time.h>
1.1       nicm       27:
                     28: #include "tmux.h"
                     29:
1.63      nicm       30: struct sessions                sessions;
                     31: static u_int           next_session_id;
                     32: struct session_groups  session_groups;
1.1       nicm       33:
1.63      nicm       34: static void    session_free(int, short, void *);
1.50      nicm       35:
1.63      nicm       36: static void    session_lock_timer(int, short, void *);
1.52      nicm       37:
1.63      nicm       38: static struct winlink *session_next_alert(struct winlink *);
                     39: static struct winlink *session_previous_alert(struct winlink *);
1.21      nicm       40:
1.64      nicm       41: static void    session_group_remove(struct session *);
                     42: static u_int   session_group_count(struct session_group *);
                     43: static void    session_group_synchronize1(struct session *, struct session *);
                     44:
1.25      nicm       45: RB_GENERATE(sessions, session, entry, session_cmp);
                     46:
                     47: int
                     48: session_cmp(struct session *s1, struct session *s2)
                     49: {
                     50:        return (strcmp(s1->name, s2->name));
                     51: }
                     52:
1.21      nicm       53: /*
                     54:  * Find if session is still alive. This is true if it is still on the global
                     55:  * sessions list.
                     56:  */
                     57: int
                     58: session_alive(struct session *s)
                     59: {
1.25      nicm       60:        struct session *s_loop;
1.21      nicm       61:
1.25      nicm       62:        RB_FOREACH(s_loop, sessions, &sessions) {
                     63:                if (s_loop == s)
                     64:                        return (1);
                     65:        }
                     66:        return (0);
1.21      nicm       67: }
1.1       nicm       68:
                     69: /* Find session by name. */
                     70: struct session *
                     71: session_find(const char *name)
                     72: {
1.25      nicm       73:        struct session  s;
                     74:
                     75:        s.name = (char *) name;
                     76:        return (RB_FIND(sessions, &sessions, &s));
1.48      nicm       77: }
                     78:
                     79: /* Find session by id parsed from a string. */
                     80: struct session *
                     81: session_find_by_id_str(const char *s)
                     82: {
                     83:        const char      *errstr;
                     84:        u_int            id;
                     85:
                     86:        if (*s != '$')
                     87:                return (NULL);
                     88:
                     89:        id = strtonum(s + 1, 0, UINT_MAX, &errstr);
                     90:        if (errstr != NULL)
                     91:                return (NULL);
                     92:        return (session_find_by_id(id));
1.25      nicm       93: }
                     94:
1.38      nicm       95: /* Find session by id. */
1.25      nicm       96: struct session *
1.38      nicm       97: session_find_by_id(u_int id)
1.25      nicm       98: {
1.1       nicm       99:        struct session  *s;
                    100:
1.25      nicm      101:        RB_FOREACH(s, sessions, &sessions) {
1.38      nicm      102:                if (s->id == id)
1.1       nicm      103:                        return (s);
                    104:        }
                    105:        return (NULL);
                    106: }
                    107:
                    108: /* Create a new session. */
                    109: struct session *
1.44      nicm      110: session_create(const char *name, int argc, char **argv, const char *path,
1.58      nicm      111:     const char *cwd, struct environ *env, struct termios *tio, int idx,
                    112:     u_int sx, u_int sy, char **cause)
1.1       nicm      113: {
                    114:        struct session  *s;
1.44      nicm      115:        struct winlink  *wl;
1.1       nicm      116:
1.52      nicm      117:        s = xcalloc(1, sizeof *s);
1.50      nicm      118:        s->references = 1;
1.1       nicm      119:        s->flags = 0;
1.7       nicm      120:
1.58      nicm      121:        s->cwd = xstrdup(cwd);
1.19      nicm      122:
1.1       nicm      123:        s->curw = NULL;
1.11      nicm      124:        TAILQ_INIT(&s->lastw);
1.1       nicm      125:        RB_INIT(&s->windows);
1.7       nicm      126:
1.57      nicm      127:        s->environ = environ_create();
1.3       nicm      128:        if (env != NULL)
1.57      nicm      129:                environ_copy(env, s->environ);
1.61      nicm      130:
1.56      nicm      131:        s->options = options_create(global_s_options);
1.61      nicm      132:        s->hooks = hooks_create(global_hooks);
1.71    ! nicm      133:
        !           134:        status_update_saved(s);
1.8       nicm      135:
                    136:        s->tio = NULL;
                    137:        if (tio != NULL) {
                    138:                s->tio = xmalloc(sizeof *s->tio);
                    139:                memcpy(s->tio, tio, sizeof *s->tio);
                    140:        }
1.1       nicm      141:
                    142:        s->sx = sx;
                    143:        s->sy = sy;
                    144:
1.32      nicm      145:        if (name != NULL) {
1.1       nicm      146:                s->name = xstrdup(name);
1.38      nicm      147:                s->id = next_session_id++;
1.32      nicm      148:        } else {
                    149:                s->name = NULL;
                    150:                do {
1.38      nicm      151:                        s->id = next_session_id++;
1.46      nicm      152:                        free(s->name);
1.38      nicm      153:                        xasprintf(&s->name, "%u", s->id);
1.32      nicm      154:                } while (RB_FIND(sessions, &sessions, s) != NULL);
                    155:        }
1.25      nicm      156:        RB_INSERT(sessions, &sessions, s);
1.15      nicm      157:
1.59      nicm      158:        log_debug("new session %s $%u", s->name, s->id);
                    159:
1.52      nicm      160:        if (gettimeofday(&s->creation_time, NULL) != 0)
                    161:                fatal("gettimeofday failed");
                    162:        session_update_activity(s, &s->creation_time);
                    163:
1.44      nicm      164:        if (argc >= 0) {
                    165:                wl = session_new(s, NULL, argc, argv, path, cwd, idx, cause);
                    166:                if (wl == NULL) {
1.11      nicm      167:                        session_destroy(s);
                    168:                        return (NULL);
                    169:                }
                    170:                session_select(s, RB_ROOT(&s->windows)->idx);
1.1       nicm      171:        }
                    172:
                    173:        log_debug("session %s created", s->name);
                    174:
                    175:        return (s);
                    176: }
                    177:
1.50      nicm      178: /* Remove a reference from a session. */
                    179: void
                    180: session_unref(struct session *s)
                    181: {
                    182:        log_debug("session %s has %d references", s->name, s->references);
                    183:
                    184:        s->references--;
                    185:        if (s->references == 0)
                    186:                event_once(-1, EV_TIMEOUT, session_free, s, NULL);
                    187: }
                    188:
                    189: /* Free session. */
1.63      nicm      190: static void
1.60      nicm      191: session_free(__unused int fd, __unused short events, void *arg)
1.50      nicm      192: {
                    193:        struct session  *s = arg;
                    194:
1.55      nicm      195:        log_debug("session %s freed (%d references)", s->name, s->references);
1.50      nicm      196:
1.53      nicm      197:        if (s->references == 0) {
1.57      nicm      198:                environ_free(s->environ);
1.61      nicm      199:
1.56      nicm      200:                options_free(s->options);
1.61      nicm      201:                hooks_free(s->hooks);
1.56      nicm      202:
1.53      nicm      203:                free(s->name);
1.50      nicm      204:                free(s);
1.53      nicm      205:        }
1.50      nicm      206: }
                    207:
1.1       nicm      208: /* Destroy a session. */
                    209: void
                    210: session_destroy(struct session *s)
                    211: {
1.33      nicm      212:        struct winlink  *wl;
1.39      nicm      213:
1.1       nicm      214:        log_debug("session %s destroyed", s->name);
1.66      nicm      215:        s->curw = NULL;
1.1       nicm      216:
1.25      nicm      217:        RB_REMOVE(sessions, &sessions, s);
1.68      nicm      218:        notify_session("session-closed", s);
1.1       nicm      219:
1.36      nicm      220:        free(s->tio);
1.8       nicm      221:
1.52      nicm      222:        if (event_initialized(&s->lock_timer))
                    223:                event_del(&s->lock_timer);
                    224:
1.11      nicm      225:        session_group_remove(s);
1.1       nicm      226:
1.11      nicm      227:        while (!TAILQ_EMPTY(&s->lastw))
                    228:                winlink_stack_remove(&s->lastw, TAILQ_FIRST(&s->lastw));
1.33      nicm      229:        while (!RB_EMPTY(&s->windows)) {
                    230:                wl = RB_ROOT(&s->windows);
1.68      nicm      231:                notify_session_window("window-unlinked", s, wl->window);
1.33      nicm      232:                winlink_remove(&s->windows, wl);
                    233:        }
1.1       nicm      234:
1.58      nicm      235:        free((void *)s->cwd);
1.15      nicm      236:
1.50      nicm      237:        session_unref(s);
1.31      nicm      238: }
                    239:
1.42      nicm      240: /* Check a session name is valid: not empty and no colons or periods. */
1.31      nicm      241: int
                    242: session_check_name(const char *name)
                    243: {
1.42      nicm      244:        return (*name != '\0' && name[strcspn(name, ":.")] == '\0');
1.27      nicm      245: }
                    246:
1.52      nicm      247: /* Lock session if it has timed out. */
1.63      nicm      248: static void
1.60      nicm      249: session_lock_timer(__unused int fd, __unused short events, void *arg)
1.52      nicm      250: {
                    251:        struct session  *s = arg;
                    252:
                    253:        if (s->flags & SESSION_UNATTACHED)
                    254:                return;
                    255:
                    256:        log_debug("session %s locked, activity time %lld", s->name,
                    257:            (long long)s->activity_time.tv_sec);
                    258:
                    259:        server_lock_session(s);
                    260:        recalculate_sizes();
                    261: }
                    262:
1.51      nicm      263: /* Update activity time. */
1.27      nicm      264: void
1.51      nicm      265: session_update_activity(struct session *s, struct timeval *from)
1.27      nicm      266: {
1.51      nicm      267:        struct timeval  *last = &s->last_activity_time;
1.52      nicm      268:        struct timeval   tv;
1.51      nicm      269:
                    270:        memcpy(last, &s->activity_time, sizeof *last);
                    271:        if (from == NULL)
                    272:                gettimeofday(&s->activity_time, NULL);
                    273:        else
                    274:                memcpy(&s->activity_time, from, sizeof s->activity_time);
1.59      nicm      275:
                    276:        log_debug("session %s activity %lld.%06d (last %lld.%06d)", s->name,
                    277:            (long long)s->activity_time.tv_sec, (int)s->activity_time.tv_usec,
                    278:            (long long)last->tv_sec, (int)last->tv_usec);
1.52      nicm      279:
                    280:        if (evtimer_initialized(&s->lock_timer))
                    281:                evtimer_del(&s->lock_timer);
                    282:        else
                    283:                evtimer_set(&s->lock_timer, session_lock_timer, s);
                    284:
                    285:        if (~s->flags & SESSION_UNATTACHED) {
                    286:                timerclear(&tv);
1.56      nicm      287:                tv.tv_sec = options_get_number(s->options, "lock-after-time");
1.52      nicm      288:                if (tv.tv_sec != 0)
                    289:                        evtimer_add(&s->lock_timer, &tv);
                    290:        }
1.20      nicm      291: }
                    292:
                    293: /* Find the next usable session. */
                    294: struct session *
                    295: session_next_session(struct session *s)
                    296: {
                    297:        struct session *s2;
                    298:
1.25      nicm      299:        if (RB_EMPTY(&sessions) || !session_alive(s))
1.20      nicm      300:                return (NULL);
                    301:
1.29      nicm      302:        s2 = RB_NEXT(sessions, &sessions, s);
1.28      nicm      303:        if (s2 == NULL)
                    304:                s2 = RB_MIN(sessions, &sessions);
1.25      nicm      305:        if (s2 == s)
                    306:                return (NULL);
1.20      nicm      307:        return (s2);
                    308: }
                    309:
                    310: /* Find the previous usable session. */
                    311: struct session *
                    312: session_previous_session(struct session *s)
                    313: {
                    314:        struct session *s2;
                    315:
1.25      nicm      316:        if (RB_EMPTY(&sessions) || !session_alive(s))
1.20      nicm      317:                return (NULL);
                    318:
1.29      nicm      319:        s2 = RB_PREV(sessions, &sessions, s);
1.28      nicm      320:        if (s2 == NULL)
                    321:                s2 = RB_MAX(sessions, &sessions);
1.25      nicm      322:        if (s2 == s)
                    323:                return (NULL);
1.20      nicm      324:        return (s2);
1.1       nicm      325: }
                    326:
                    327: /* Create a new window on a session. */
                    328: struct winlink *
1.44      nicm      329: session_new(struct session *s, const char *name, int argc, char **argv,
1.58      nicm      330:     const char *path, const char *cwd, int idx, char **cause)
1.1       nicm      331: {
                    332:        struct window   *w;
1.30      nicm      333:        struct winlink  *wl;
1.57      nicm      334:        struct environ  *env;
1.6       nicm      335:        const char      *shell;
1.1       nicm      336:        u_int            hlimit;
                    337:
1.30      nicm      338:        if ((wl = winlink_add(&s->windows, idx)) == NULL) {
                    339:                xasprintf(cause, "index in use: %d", idx);
                    340:                return (NULL);
                    341:        }
1.70      nicm      342:        wl->session = s;
1.30      nicm      343:
1.57      nicm      344:        env = environ_create();
                    345:        environ_copy(global_environ, env);
                    346:        environ_copy(s->environ, env);
                    347:        server_fill_environ(s, env);
1.1       nicm      348:
1.56      nicm      349:        shell = options_get_string(s->options, "default-shell");
1.6       nicm      350:        if (*shell == '\0' || areshell(shell))
                    351:                shell = _PATH_BSHELL;
                    352:
1.56      nicm      353:        hlimit = options_get_number(s->options, "history-limit");
1.67      nicm      354:        w = window_create_spawn(name, argc, argv, path, shell, cwd, env, s->tio,
1.44      nicm      355:            s->sx, s->sy, hlimit, cause);
1.3       nicm      356:        if (w == NULL) {
1.30      nicm      357:                winlink_remove(&s->windows, wl);
1.57      nicm      358:                environ_free(env);
1.1       nicm      359:                return (NULL);
1.3       nicm      360:        }
1.30      nicm      361:        winlink_set_window(wl, w);
1.68      nicm      362:        notify_session_window("window-linked", s, w);
1.57      nicm      363:        environ_free(env);
1.1       nicm      364:
1.30      nicm      365:        session_group_synchronize_from(s);
                    366:        return (wl);
1.1       nicm      367: }
                    368:
                    369: /* Attach a window to a session. */
                    370: struct winlink *
                    371: session_attach(struct session *s, struct window *w, int idx, char **cause)
                    372: {
                    373:        struct winlink  *wl;
                    374:
1.30      nicm      375:        if ((wl = winlink_add(&s->windows, idx)) == NULL) {
1.1       nicm      376:                xasprintf(cause, "index in use: %d", idx);
1.30      nicm      377:                return (NULL);
                    378:        }
1.70      nicm      379:        wl->session = s;
1.30      nicm      380:        winlink_set_window(wl, w);
1.68      nicm      381:        notify_session_window("window-linked", s, w);
1.30      nicm      382:
1.11      nicm      383:        session_group_synchronize_from(s);
1.1       nicm      384:        return (wl);
                    385: }
                    386:
                    387: /* Detach a window from a session. */
                    388: int
                    389: session_detach(struct session *s, struct winlink *wl)
                    390: {
                    391:        if (s->curw == wl &&
1.66      nicm      392:            session_last(s) != 0 &&
                    393:            session_previous(s, 0) != 0)
1.1       nicm      394:                session_next(s, 0);
                    395:
1.18      nicm      396:        wl->flags &= ~WINLINK_ALERTFLAGS;
1.68      nicm      397:        notify_session_window("window-unlinked", s, wl->window);
1.1       nicm      398:        winlink_stack_remove(&s->lastw, wl);
                    399:        winlink_remove(&s->windows, wl);
1.66      nicm      400:
1.11      nicm      401:        session_group_synchronize_from(s);
1.66      nicm      402:
1.1       nicm      403:        if (RB_EMPTY(&s->windows)) {
                    404:                session_destroy(s);
                    405:                return (1);
                    406:        }
                    407:        return (0);
                    408: }
                    409:
                    410: /* Return if session has window. */
1.47      nicm      411: int
1.1       nicm      412: session_has(struct session *s, struct window *w)
                    413: {
                    414:        struct winlink  *wl;
                    415:
1.70      nicm      416:        TAILQ_FOREACH(wl, &w->winlinks, wentry) {
                    417:                if (wl->session == s)
1.47      nicm      418:                        return (1);
1.1       nicm      419:        }
1.47      nicm      420:        return (0);
1.49      nicm      421: }
                    422:
                    423: /*
                    424:  * Return 1 if a window is linked outside this session (not including session
                    425:  * groups). The window must be in this session!
                    426:  */
                    427: int
                    428: session_is_linked(struct session *s, struct window *w)
                    429: {
                    430:        struct session_group    *sg;
                    431:
                    432:        if ((sg = session_group_find(s)) != NULL)
                    433:                return (w->references != session_group_count(sg));
                    434:        return (w->references != 1);
1.1       nicm      435: }
                    436:
1.63      nicm      437: static struct winlink *
1.18      nicm      438: session_next_alert(struct winlink *wl)
1.1       nicm      439: {
                    440:        while (wl != NULL) {
1.18      nicm      441:                if (wl->flags & WINLINK_ALERTFLAGS)
1.1       nicm      442:                        break;
1.14      nicm      443:                wl = winlink_next(wl);
1.1       nicm      444:        }
                    445:        return (wl);
                    446: }
                    447:
                    448: /* Move session to next window. */
                    449: int
1.17      nicm      450: session_next(struct session *s, int alert)
1.1       nicm      451: {
                    452:        struct winlink  *wl;
                    453:
                    454:        if (s->curw == NULL)
                    455:                return (-1);
                    456:
1.14      nicm      457:        wl = winlink_next(s->curw);
1.17      nicm      458:        if (alert)
1.18      nicm      459:                wl = session_next_alert(wl);
1.1       nicm      460:        if (wl == NULL) {
                    461:                wl = RB_MIN(winlinks, &s->windows);
1.18      nicm      462:                if (alert && ((wl = session_next_alert(wl)) == NULL))
1.1       nicm      463:                        return (-1);
                    464:        }
1.37      nicm      465:        return (session_set_current(s, wl));
1.1       nicm      466: }
                    467:
1.63      nicm      468: static struct winlink *
1.18      nicm      469: session_previous_alert(struct winlink *wl)
1.1       nicm      470: {
                    471:        while (wl != NULL) {
1.18      nicm      472:                if (wl->flags & WINLINK_ALERTFLAGS)
1.1       nicm      473:                        break;
1.14      nicm      474:                wl = winlink_previous(wl);
1.1       nicm      475:        }
                    476:        return (wl);
                    477: }
                    478:
                    479: /* Move session to previous window. */
                    480: int
1.17      nicm      481: session_previous(struct session *s, int alert)
1.1       nicm      482: {
                    483:        struct winlink  *wl;
                    484:
                    485:        if (s->curw == NULL)
                    486:                return (-1);
                    487:
1.14      nicm      488:        wl = winlink_previous(s->curw);
1.17      nicm      489:        if (alert)
1.18      nicm      490:                wl = session_previous_alert(wl);
1.1       nicm      491:        if (wl == NULL) {
                    492:                wl = RB_MAX(winlinks, &s->windows);
1.18      nicm      493:                if (alert && (wl = session_previous_alert(wl)) == NULL)
1.1       nicm      494:                        return (-1);
                    495:        }
1.37      nicm      496:        return (session_set_current(s, wl));
1.1       nicm      497: }
                    498:
                    499: /* Move session to specific window. */
                    500: int
                    501: session_select(struct session *s, int idx)
                    502: {
                    503:        struct winlink  *wl;
                    504:
                    505:        wl = winlink_find_by_index(&s->windows, idx);
1.37      nicm      506:        return (session_set_current(s, wl));
1.1       nicm      507: }
                    508:
                    509: /* Move session to last used window. */
                    510: int
                    511: session_last(struct session *s)
                    512: {
                    513:        struct winlink  *wl;
                    514:
1.11      nicm      515:        wl = TAILQ_FIRST(&s->lastw);
1.37      nicm      516:        if (wl == NULL)
                    517:                return (-1);
                    518:        if (wl == s->curw)
                    519:                return (1);
                    520:
                    521:        return (session_set_current(s, wl));
                    522: }
                    523:
                    524: /* Set current winlink to wl .*/
                    525: int
                    526: session_set_current(struct session *s, struct winlink *wl)
                    527: {
1.1       nicm      528:        if (wl == NULL)
                    529:                return (-1);
                    530:        if (wl == s->curw)
                    531:                return (1);
                    532:
                    533:        winlink_stack_remove(&s->lastw, wl);
                    534:        winlink_stack_push(&s->lastw, s->curw);
                    535:        s->curw = wl;
1.35      nicm      536:        winlink_clear_flags(wl);
1.54      nicm      537:        window_update_activity(wl->window);
1.1       nicm      538:        return (0);
1.11      nicm      539: }
                    540:
                    541: /* Find the session group containing a session. */
                    542: struct session_group *
                    543: session_group_find(struct session *target)
                    544: {
                    545:        struct session_group    *sg;
                    546:        struct session          *s;
                    547:
                    548:        TAILQ_FOREACH(sg, &session_groups, entry) {
                    549:                TAILQ_FOREACH(s, &sg->sessions, gentry) {
                    550:                        if (s == target)
                    551:                                return (sg);
                    552:                }
                    553:        }
                    554:        return (NULL);
                    555: }
                    556:
                    557: /* Find session group index. */
                    558: u_int
                    559: session_group_index(struct session_group *sg)
                    560: {
                    561:        struct session_group   *sg2;
                    562:        u_int                   i;
                    563:
                    564:        i = 0;
                    565:        TAILQ_FOREACH(sg2, &session_groups, entry) {
                    566:                if (sg == sg2)
                    567:                        return (i);
                    568:                i++;
                    569:        }
                    570:
                    571:        fatalx("session group not found");
                    572: }
                    573:
                    574: /*
                    575:  * Add a session to the session group containing target, creating it if
1.15      nicm      576:  * necessary.
1.11      nicm      577:  */
                    578: void
                    579: session_group_add(struct session *target, struct session *s)
                    580: {
                    581:        struct session_group    *sg;
                    582:
                    583:        if ((sg = session_group_find(target)) == NULL) {
                    584:                sg = xmalloc(sizeof *sg);
                    585:                TAILQ_INSERT_TAIL(&session_groups, sg, entry);
                    586:                TAILQ_INIT(&sg->sessions);
                    587:                TAILQ_INSERT_TAIL(&sg->sessions, target, gentry);
                    588:        }
                    589:        TAILQ_INSERT_TAIL(&sg->sessions, s, gentry);
                    590: }
                    591:
                    592: /* Remove a session from its group and destroy the group if empty. */
1.64      nicm      593: static void
1.11      nicm      594: session_group_remove(struct session *s)
                    595: {
                    596:        struct session_group    *sg;
                    597:
                    598:        if ((sg = session_group_find(s)) == NULL)
                    599:                return;
                    600:        TAILQ_REMOVE(&sg->sessions, s, gentry);
                    601:        if (TAILQ_NEXT(TAILQ_FIRST(&sg->sessions), gentry) == NULL)
                    602:                TAILQ_REMOVE(&sg->sessions, TAILQ_FIRST(&sg->sessions), gentry);
                    603:        if (TAILQ_EMPTY(&sg->sessions)) {
                    604:                TAILQ_REMOVE(&session_groups, sg, entry);
1.36      nicm      605:                free(sg);
1.11      nicm      606:        }
                    607: }
                    608:
1.45      nicm      609: /* Count number of sessions in session group. */
1.64      nicm      610: static u_int
1.45      nicm      611: session_group_count(struct session_group *sg)
                    612: {
                    613:        struct session  *s;
                    614:        u_int            n;
                    615:
                    616:        n = 0;
                    617:        TAILQ_FOREACH(s, &sg->sessions, gentry)
                    618:            n++;
                    619:        return (n);
                    620: }
                    621:
1.11      nicm      622: /* Synchronize a session to its session group. */
                    623: void
                    624: session_group_synchronize_to(struct session *s)
                    625: {
                    626:        struct session_group    *sg;
                    627:        struct session          *target;
                    628:
                    629:        if ((sg = session_group_find(s)) == NULL)
                    630:                return;
                    631:
                    632:        target = NULL;
                    633:        TAILQ_FOREACH(target, &sg->sessions, gentry) {
                    634:                if (target != s)
                    635:                        break;
                    636:        }
                    637:        session_group_synchronize1(target, s);
                    638: }
                    639:
                    640: /* Synchronize a session group to a session. */
                    641: void
                    642: session_group_synchronize_from(struct session *target)
                    643: {
                    644:        struct session_group    *sg;
                    645:        struct session          *s;
                    646:
                    647:        if ((sg = session_group_find(target)) == NULL)
                    648:                return;
                    649:
                    650:        TAILQ_FOREACH(s, &sg->sessions, gentry) {
                    651:                if (s != target)
                    652:                        session_group_synchronize1(target, s);
                    653:        }
                    654: }
                    655:
                    656: /*
                    657:  * Synchronize a session with a target session. This means destroying all
                    658:  * winlinks then recreating them, then updating the current window, last window
                    659:  * stack and alerts.
                    660:  */
1.64      nicm      661: static void
1.11      nicm      662: session_group_synchronize1(struct session *target, struct session *s)
                    663: {
                    664:        struct winlinks          old_windows, *ww;
                    665:        struct winlink_stack     old_lastw;
                    666:        struct winlink          *wl, *wl2;
                    667:
                    668:        /* Don't do anything if the session is empty (it'll be destroyed). */
                    669:        ww = &target->windows;
                    670:        if (RB_EMPTY(ww))
                    671:                return;
                    672:
                    673:        /* If the current window has vanished, move to the next now. */
1.16      nicm      674:        if (s->curw != NULL &&
                    675:            winlink_find_by_index(ww, s->curw->idx) == NULL &&
                    676:            session_last(s) != 0 && session_previous(s, 0) != 0)
                    677:                session_next(s, 0);
1.11      nicm      678:
                    679:        /* Save the old pointer and reset it. */
                    680:        memcpy(&old_windows, &s->windows, sizeof old_windows);
                    681:        RB_INIT(&s->windows);
                    682:
                    683:        /* Link all the windows from the target. */
1.18      nicm      684:        RB_FOREACH(wl, winlinks, ww) {
1.30      nicm      685:                wl2 = winlink_add(&s->windows, wl->idx);
1.70      nicm      686:                wl2->session = s;
1.30      nicm      687:                winlink_set_window(wl2, wl->window);
1.68      nicm      688:                notify_session_window("window-linked", s, wl2->window);
1.18      nicm      689:                wl2->flags |= wl->flags & WINLINK_ALERTFLAGS;
                    690:        }
1.11      nicm      691:
                    692:        /* Fix up the current window. */
                    693:        if (s->curw != NULL)
                    694:                s->curw = winlink_find_by_index(&s->windows, s->curw->idx);
                    695:        else
                    696:                s->curw = winlink_find_by_index(&s->windows, target->curw->idx);
                    697:
                    698:        /* Fix up the last window stack. */
                    699:        memcpy(&old_lastw, &s->lastw, sizeof old_lastw);
                    700:        TAILQ_INIT(&s->lastw);
                    701:        TAILQ_FOREACH(wl, &old_lastw, sentry) {
                    702:                wl2 = winlink_find_by_index(&s->windows, wl->idx);
                    703:                if (wl2 != NULL)
                    704:                        TAILQ_INSERT_TAIL(&s->lastw, wl2, sentry);
                    705:        }
                    706:
                    707:        /* Then free the old winlinks list. */
                    708:        while (!RB_EMPTY(&old_windows)) {
                    709:                wl = RB_ROOT(&old_windows);
1.45      nicm      710:                wl2 = winlink_find_by_window_id(&s->windows, wl->window->id);
                    711:                if (wl2 == NULL)
1.68      nicm      712:                        notify_session_window("window-unlinked", s, wl->window);
1.13      nicm      713:                winlink_remove(&old_windows, wl);
1.11      nicm      714:        }
1.34      nicm      715: }
                    716:
                    717: /* Renumber the windows across winlinks attached to a specific session. */
                    718: void
                    719: session_renumber_windows(struct session *s)
                    720: {
                    721:        struct winlink          *wl, *wl1, *wl_new;
                    722:        struct winlinks          old_wins;
                    723:        struct winlink_stack     old_lastw;
                    724:        int                      new_idx, new_curw_idx;
                    725:
                    726:        /* Save and replace old window list. */
                    727:        memcpy(&old_wins, &s->windows, sizeof old_wins);
                    728:        RB_INIT(&s->windows);
                    729:
                    730:        /* Start renumbering from the base-index if it's set. */
1.56      nicm      731:        new_idx = options_get_number(s->options, "base-index");
1.34      nicm      732:        new_curw_idx = 0;
                    733:
                    734:        /* Go through the winlinks and assign new indexes. */
                    735:        RB_FOREACH(wl, winlinks, &old_wins) {
                    736:                wl_new = winlink_add(&s->windows, new_idx);
1.70      nicm      737:                wl_new->session = s;
1.34      nicm      738:                winlink_set_window(wl_new, wl->window);
                    739:                wl_new->flags |= wl->flags & WINLINK_ALERTFLAGS;
                    740:
                    741:                if (wl == s->curw)
                    742:                        new_curw_idx = wl_new->idx;
                    743:
                    744:                new_idx++;
                    745:        }
                    746:
                    747:        /* Fix the stack of last windows now. */
                    748:        memcpy(&old_lastw, &s->lastw, sizeof old_lastw);
                    749:        TAILQ_INIT(&s->lastw);
                    750:        TAILQ_FOREACH(wl, &old_lastw, sentry) {
1.40      nicm      751:                wl_new = winlink_find_by_window(&s->windows, wl->window);
1.34      nicm      752:                if (wl_new != NULL)
                    753:                        TAILQ_INSERT_TAIL(&s->lastw, wl_new, sentry);
                    754:        }
                    755:
                    756:        /* Set the current window. */
                    757:        s->curw = winlink_find_by_index(&s->windows, new_curw_idx);
                    758:
                    759:        /* Free the old winlinks (reducing window references too). */
                    760:        RB_FOREACH_SAFE(wl, winlinks, &old_wins, wl1)
                    761:                winlink_remove(&old_wins, wl);
1.1       nicm      762: }