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

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