[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.51

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