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

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