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

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