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

1.86    ! nicm        1: /* $OpenBSD: session.c,v 1.85 2019/04/26 11:38:51 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.71      nicm      132:
1.83      nicm      133:        status_update_cache(s);
1.8       nicm      134:
                    135:        s->tio = NULL;
                    136:        if (tio != NULL) {
                    137:                s->tio = xmalloc(sizeof *s->tio);
                    138:                memcpy(s->tio, tio, sizeof *s->tio);
                    139:        }
1.1       nicm      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.72      nicm      149:                        if (prefix != NULL)
                    150:                                xasprintf(&s->name, "%s-%u", prefix, s->id);
                    151:                        else
                    152:                                xasprintf(&s->name, "%u", s->id);
1.32      nicm      153:                } while (RB_FIND(sessions, &sessions, s) != NULL);
                    154:        }
1.25      nicm      155:        RB_INSERT(sessions, &sessions, s);
1.15      nicm      156:
1.59      nicm      157:        log_debug("new session %s $%u", s->name, s->id);
                    158:
1.52      nicm      159:        if (gettimeofday(&s->creation_time, NULL) != 0)
                    160:                fatal("gettimeofday failed");
                    161:        session_update_activity(s, &s->creation_time);
                    162:
1.1       nicm      163:        return (s);
                    164: }
                    165:
1.75      nicm      166: /* Add a reference to a session. */
                    167: void
                    168: session_add_ref(struct session *s, const char *from)
                    169: {
                    170:        s->references++;
                    171:        log_debug("%s: %s %s, now %d", __func__, s->name, from, s->references);
                    172: }
                    173:
1.50      nicm      174: /* Remove a reference from a session. */
                    175: void
1.75      nicm      176: session_remove_ref(struct session *s, const char *from)
1.50      nicm      177: {
1.75      nicm      178:        s->references--;
                    179:        log_debug("%s: %s %s, now %d", __func__, s->name, from, s->references);
1.50      nicm      180:
                    181:        if (s->references == 0)
                    182:                event_once(-1, EV_TIMEOUT, session_free, s, NULL);
                    183: }
                    184:
                    185: /* Free session. */
1.63      nicm      186: static void
1.60      nicm      187: session_free(__unused int fd, __unused short events, void *arg)
1.50      nicm      188: {
                    189:        struct session  *s = arg;
                    190:
1.55      nicm      191:        log_debug("session %s freed (%d references)", s->name, s->references);
1.50      nicm      192:
1.53      nicm      193:        if (s->references == 0) {
1.57      nicm      194:                environ_free(s->environ);
1.56      nicm      195:                options_free(s->options);
                    196:
1.53      nicm      197:                free(s->name);
1.50      nicm      198:                free(s);
1.53      nicm      199:        }
1.50      nicm      200: }
                    201:
1.1       nicm      202: /* Destroy a session. */
                    203: void
1.84      nicm      204: session_destroy(struct session *s, int notify, const char *from)
1.1       nicm      205: {
1.33      nicm      206:        struct winlink  *wl;
1.39      nicm      207:
1.77      nicm      208:        log_debug("session %s destroyed (%s)", s->name, from);
1.66      nicm      209:        s->curw = NULL;
1.1       nicm      210:
1.25      nicm      211:        RB_REMOVE(sessions, &sessions, s);
1.84      nicm      212:        if (notify)
                    213:                notify_session("session-closed", s);
1.1       nicm      214:
1.36      nicm      215:        free(s->tio);
1.8       nicm      216:
1.52      nicm      217:        if (event_initialized(&s->lock_timer))
                    218:                event_del(&s->lock_timer);
                    219:
1.11      nicm      220:        session_group_remove(s);
1.1       nicm      221:
1.11      nicm      222:        while (!TAILQ_EMPTY(&s->lastw))
                    223:                winlink_stack_remove(&s->lastw, TAILQ_FIRST(&s->lastw));
1.33      nicm      224:        while (!RB_EMPTY(&s->windows)) {
                    225:                wl = RB_ROOT(&s->windows);
1.68      nicm      226:                notify_session_window("window-unlinked", s, wl->window);
1.33      nicm      227:                winlink_remove(&s->windows, wl);
                    228:        }
1.1       nicm      229:
1.58      nicm      230:        free((void *)s->cwd);
1.15      nicm      231:
1.75      nicm      232:        session_remove_ref(s, __func__);
1.31      nicm      233: }
                    234:
1.42      nicm      235: /* Check a session name is valid: not empty and no colons or periods. */
1.31      nicm      236: int
                    237: session_check_name(const char *name)
                    238: {
1.42      nicm      239:        return (*name != '\0' && name[strcspn(name, ":.")] == '\0');
1.27      nicm      240: }
                    241:
1.52      nicm      242: /* Lock session if it has timed out. */
1.63      nicm      243: static void
1.60      nicm      244: session_lock_timer(__unused int fd, __unused short events, void *arg)
1.52      nicm      245: {
                    246:        struct session  *s = arg;
                    247:
1.81      nicm      248:        if (s->attached == 0)
1.52      nicm      249:                return;
                    250:
                    251:        log_debug("session %s locked, activity time %lld", s->name,
                    252:            (long long)s->activity_time.tv_sec);
                    253:
                    254:        server_lock_session(s);
                    255:        recalculate_sizes();
                    256: }
                    257:
1.51      nicm      258: /* Update activity time. */
1.27      nicm      259: void
1.51      nicm      260: session_update_activity(struct session *s, struct timeval *from)
1.27      nicm      261: {
1.51      nicm      262:        struct timeval  *last = &s->last_activity_time;
1.52      nicm      263:        struct timeval   tv;
1.51      nicm      264:
                    265:        memcpy(last, &s->activity_time, sizeof *last);
                    266:        if (from == NULL)
                    267:                gettimeofday(&s->activity_time, NULL);
                    268:        else
                    269:                memcpy(&s->activity_time, from, sizeof s->activity_time);
1.59      nicm      270:
1.79      nicm      271:        log_debug("session $%u %s activity %lld.%06d (last %lld.%06d)", s->id,
                    272:            s->name, (long long)s->activity_time.tv_sec,
                    273:            (int)s->activity_time.tv_usec, (long long)last->tv_sec,
                    274:            (int)last->tv_usec);
1.52      nicm      275:
                    276:        if (evtimer_initialized(&s->lock_timer))
                    277:                evtimer_del(&s->lock_timer);
                    278:        else
                    279:                evtimer_set(&s->lock_timer, session_lock_timer, s);
                    280:
1.81      nicm      281:        if (s->attached != 0) {
1.52      nicm      282:                timerclear(&tv);
1.56      nicm      283:                tv.tv_sec = options_get_number(s->options, "lock-after-time");
1.52      nicm      284:                if (tv.tv_sec != 0)
                    285:                        evtimer_add(&s->lock_timer, &tv);
                    286:        }
1.20      nicm      287: }
                    288:
                    289: /* Find the next usable session. */
                    290: struct session *
                    291: session_next_session(struct session *s)
                    292: {
                    293:        struct session *s2;
                    294:
1.25      nicm      295:        if (RB_EMPTY(&sessions) || !session_alive(s))
1.20      nicm      296:                return (NULL);
                    297:
1.29      nicm      298:        s2 = RB_NEXT(sessions, &sessions, s);
1.28      nicm      299:        if (s2 == NULL)
                    300:                s2 = RB_MIN(sessions, &sessions);
1.25      nicm      301:        if (s2 == s)
                    302:                return (NULL);
1.20      nicm      303:        return (s2);
                    304: }
                    305:
                    306: /* Find the previous usable session. */
                    307: struct session *
                    308: session_previous_session(struct session *s)
                    309: {
                    310:        struct session *s2;
                    311:
1.25      nicm      312:        if (RB_EMPTY(&sessions) || !session_alive(s))
1.20      nicm      313:                return (NULL);
                    314:
1.29      nicm      315:        s2 = RB_PREV(sessions, &sessions, s);
1.28      nicm      316:        if (s2 == NULL)
                    317:                s2 = RB_MAX(sessions, &sessions);
1.25      nicm      318:        if (s2 == s)
                    319:                return (NULL);
1.20      nicm      320:        return (s2);
1.1       nicm      321: }
                    322:
                    323: /* Attach a window to a session. */
                    324: struct winlink *
                    325: session_attach(struct session *s, struct window *w, int idx, char **cause)
                    326: {
                    327:        struct winlink  *wl;
                    328:
1.30      nicm      329:        if ((wl = winlink_add(&s->windows, idx)) == NULL) {
1.1       nicm      330:                xasprintf(cause, "index in use: %d", idx);
1.30      nicm      331:                return (NULL);
                    332:        }
1.70      nicm      333:        wl->session = s;
1.30      nicm      334:        winlink_set_window(wl, w);
1.68      nicm      335:        notify_session_window("window-linked", s, w);
1.30      nicm      336:
1.11      nicm      337:        session_group_synchronize_from(s);
1.1       nicm      338:        return (wl);
                    339: }
                    340:
                    341: /* Detach a window from a session. */
                    342: int
                    343: session_detach(struct session *s, struct winlink *wl)
                    344: {
                    345:        if (s->curw == wl &&
1.66      nicm      346:            session_last(s) != 0 &&
                    347:            session_previous(s, 0) != 0)
1.1       nicm      348:                session_next(s, 0);
                    349:
1.18      nicm      350:        wl->flags &= ~WINLINK_ALERTFLAGS;
1.68      nicm      351:        notify_session_window("window-unlinked", s, wl->window);
1.1       nicm      352:        winlink_stack_remove(&s->lastw, wl);
                    353:        winlink_remove(&s->windows, wl);
1.66      nicm      354:
1.11      nicm      355:        session_group_synchronize_from(s);
1.66      nicm      356:
1.1       nicm      357:        if (RB_EMPTY(&s->windows)) {
1.84      nicm      358:                session_destroy(s, 1, __func__);
1.1       nicm      359:                return (1);
                    360:        }
                    361:        return (0);
                    362: }
                    363:
                    364: /* Return if session has window. */
1.47      nicm      365: int
1.1       nicm      366: session_has(struct session *s, struct window *w)
                    367: {
                    368:        struct winlink  *wl;
                    369:
1.70      nicm      370:        TAILQ_FOREACH(wl, &w->winlinks, wentry) {
                    371:                if (wl->session == s)
1.47      nicm      372:                        return (1);
1.1       nicm      373:        }
1.47      nicm      374:        return (0);
1.49      nicm      375: }
                    376:
                    377: /*
                    378:  * Return 1 if a window is linked outside this session (not including session
                    379:  * groups). The window must be in this session!
                    380:  */
                    381: int
                    382: session_is_linked(struct session *s, struct window *w)
                    383: {
                    384:        struct session_group    *sg;
                    385:
1.72      nicm      386:        if ((sg = session_group_contains(s)) != NULL)
1.49      nicm      387:                return (w->references != session_group_count(sg));
                    388:        return (w->references != 1);
1.1       nicm      389: }
                    390:
1.63      nicm      391: static struct winlink *
1.18      nicm      392: session_next_alert(struct winlink *wl)
1.1       nicm      393: {
                    394:        while (wl != NULL) {
1.18      nicm      395:                if (wl->flags & WINLINK_ALERTFLAGS)
1.1       nicm      396:                        break;
1.14      nicm      397:                wl = winlink_next(wl);
1.1       nicm      398:        }
                    399:        return (wl);
                    400: }
                    401:
                    402: /* Move session to next window. */
                    403: int
1.17      nicm      404: session_next(struct session *s, int alert)
1.1       nicm      405: {
                    406:        struct winlink  *wl;
                    407:
                    408:        if (s->curw == NULL)
                    409:                return (-1);
                    410:
1.14      nicm      411:        wl = winlink_next(s->curw);
1.17      nicm      412:        if (alert)
1.18      nicm      413:                wl = session_next_alert(wl);
1.1       nicm      414:        if (wl == NULL) {
                    415:                wl = RB_MIN(winlinks, &s->windows);
1.18      nicm      416:                if (alert && ((wl = session_next_alert(wl)) == NULL))
1.1       nicm      417:                        return (-1);
                    418:        }
1.37      nicm      419:        return (session_set_current(s, wl));
1.1       nicm      420: }
                    421:
1.63      nicm      422: static struct winlink *
1.18      nicm      423: session_previous_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_previous(wl);
1.1       nicm      429:        }
                    430:        return (wl);
                    431: }
                    432:
                    433: /* Move session to previous window. */
                    434: int
1.17      nicm      435: session_previous(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_previous(s->curw);
1.17      nicm      443:        if (alert)
1.18      nicm      444:                wl = session_previous_alert(wl);
1.1       nicm      445:        if (wl == NULL) {
                    446:                wl = RB_MAX(winlinks, &s->windows);
1.18      nicm      447:                if (alert && (wl = session_previous_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: /* Move session to specific window. */
                    454: int
                    455: session_select(struct session *s, int idx)
                    456: {
                    457:        struct winlink  *wl;
                    458:
                    459:        wl = winlink_find_by_index(&s->windows, idx);
1.37      nicm      460:        return (session_set_current(s, wl));
1.1       nicm      461: }
                    462:
                    463: /* Move session to last used window. */
                    464: int
                    465: session_last(struct session *s)
                    466: {
                    467:        struct winlink  *wl;
                    468:
1.11      nicm      469:        wl = TAILQ_FIRST(&s->lastw);
1.37      nicm      470:        if (wl == NULL)
                    471:                return (-1);
                    472:        if (wl == s->curw)
                    473:                return (1);
                    474:
                    475:        return (session_set_current(s, wl));
                    476: }
                    477:
                    478: /* Set current winlink to wl .*/
                    479: int
                    480: session_set_current(struct session *s, struct winlink *wl)
                    481: {
1.1       nicm      482:        if (wl == NULL)
                    483:                return (-1);
                    484:        if (wl == s->curw)
                    485:                return (1);
                    486:
                    487:        winlink_stack_remove(&s->lastw, wl);
                    488:        winlink_stack_push(&s->lastw, s->curw);
                    489:        s->curw = wl;
1.35      nicm      490:        winlink_clear_flags(wl);
1.54      nicm      491:        window_update_activity(wl->window);
1.82      nicm      492:        tty_update_window_offset(wl->window);
1.76      nicm      493:        notify_session("session-window-changed", s);
1.1       nicm      494:        return (0);
1.11      nicm      495: }
                    496:
                    497: /* Find the session group containing a session. */
                    498: struct session_group *
1.72      nicm      499: session_group_contains(struct session *target)
1.11      nicm      500: {
                    501:        struct session_group    *sg;
                    502:        struct session          *s;
                    503:
1.72      nicm      504:        RB_FOREACH(sg, session_groups, &session_groups) {
1.11      nicm      505:                TAILQ_FOREACH(s, &sg->sessions, gentry) {
                    506:                        if (s == target)
                    507:                                return (sg);
                    508:                }
                    509:        }
                    510:        return (NULL);
                    511: }
                    512:
1.72      nicm      513: /* Find session group by name. */
                    514: struct session_group *
                    515: session_group_find(const char *name)
1.11      nicm      516: {
1.72      nicm      517:        struct session_group    sg;
                    518:
                    519:        sg.name = name;
                    520:        return (RB_FIND(session_groups, &session_groups, &sg));
                    521: }
1.11      nicm      522:
1.72      nicm      523: /* Create a new session group. */
                    524: struct session_group *
                    525: session_group_new(const char *name)
                    526: {
                    527:        struct session_group    *sg;
                    528:
                    529:        if ((sg = session_group_find(name)) != NULL)
                    530:                return (sg);
                    531:
                    532:        sg = xcalloc(1, sizeof *sg);
                    533:        sg->name = xstrdup(name);
                    534:        TAILQ_INIT(&sg->sessions);
1.11      nicm      535:
1.72      nicm      536:        RB_INSERT(session_groups, &session_groups, sg);
                    537:        return (sg);
1.11      nicm      538: }
                    539:
1.72      nicm      540: /* Add a session to a session group. */
1.11      nicm      541: void
1.72      nicm      542: session_group_add(struct session_group *sg, struct session *s)
1.11      nicm      543: {
1.72      nicm      544:        if (session_group_contains(s) == NULL)
                    545:                TAILQ_INSERT_TAIL(&sg->sessions, s, gentry);
1.11      nicm      546: }
                    547:
                    548: /* Remove a session from its group and destroy the group if empty. */
1.64      nicm      549: static void
1.11      nicm      550: session_group_remove(struct session *s)
                    551: {
                    552:        struct session_group    *sg;
                    553:
1.72      nicm      554:        if ((sg = session_group_contains(s)) == NULL)
1.11      nicm      555:                return;
                    556:        TAILQ_REMOVE(&sg->sessions, s, gentry);
                    557:        if (TAILQ_EMPTY(&sg->sessions)) {
1.72      nicm      558:                RB_REMOVE(session_groups, &session_groups, sg);
1.36      nicm      559:                free(sg);
1.11      nicm      560:        }
                    561: }
                    562:
1.45      nicm      563: /* Count number of sessions in session group. */
1.78      nicm      564: u_int
1.45      nicm      565: session_group_count(struct session_group *sg)
                    566: {
                    567:        struct session  *s;
                    568:        u_int            n;
                    569:
                    570:        n = 0;
                    571:        TAILQ_FOREACH(s, &sg->sessions, gentry)
1.86    ! nicm      572:                n++;
        !           573:        return (n);
        !           574: }
        !           575:
        !           576: /* Count number of clients attached to sessions in session group. */
        !           577: u_int
        !           578: session_group_attached_count(struct session_group *sg)
        !           579: {
        !           580:        struct session  *s;
        !           581:        u_int            n;
        !           582:
        !           583:        n = 0;
        !           584:        TAILQ_FOREACH(s, &sg->sessions, gentry)
        !           585:                n += s->attached;
1.45      nicm      586:        return (n);
                    587: }
                    588:
1.11      nicm      589: /* Synchronize a session to its session group. */
                    590: void
                    591: session_group_synchronize_to(struct session *s)
                    592: {
                    593:        struct session_group    *sg;
                    594:        struct session          *target;
                    595:
1.72      nicm      596:        if ((sg = session_group_contains(s)) == NULL)
1.11      nicm      597:                return;
                    598:
                    599:        target = NULL;
                    600:        TAILQ_FOREACH(target, &sg->sessions, gentry) {
                    601:                if (target != s)
                    602:                        break;
                    603:        }
1.72      nicm      604:        if (target != NULL)
                    605:                session_group_synchronize1(target, s);
1.11      nicm      606: }
                    607:
                    608: /* Synchronize a session group to a session. */
                    609: void
                    610: session_group_synchronize_from(struct session *target)
                    611: {
                    612:        struct session_group    *sg;
                    613:        struct session          *s;
                    614:
1.72      nicm      615:        if ((sg = session_group_contains(target)) == NULL)
1.11      nicm      616:                return;
                    617:
                    618:        TAILQ_FOREACH(s, &sg->sessions, gentry) {
                    619:                if (s != target)
                    620:                        session_group_synchronize1(target, s);
                    621:        }
                    622: }
                    623:
                    624: /*
                    625:  * Synchronize a session with a target session. This means destroying all
                    626:  * winlinks then recreating them, then updating the current window, last window
                    627:  * stack and alerts.
                    628:  */
1.64      nicm      629: static void
1.11      nicm      630: session_group_synchronize1(struct session *target, struct session *s)
                    631: {
                    632:        struct winlinks          old_windows, *ww;
                    633:        struct winlink_stack     old_lastw;
                    634:        struct winlink          *wl, *wl2;
                    635:
                    636:        /* Don't do anything if the session is empty (it'll be destroyed). */
                    637:        ww = &target->windows;
                    638:        if (RB_EMPTY(ww))
                    639:                return;
                    640:
                    641:        /* If the current window has vanished, move to the next now. */
1.16      nicm      642:        if (s->curw != NULL &&
                    643:            winlink_find_by_index(ww, s->curw->idx) == NULL &&
                    644:            session_last(s) != 0 && session_previous(s, 0) != 0)
                    645:                session_next(s, 0);
1.11      nicm      646:
                    647:        /* Save the old pointer and reset it. */
                    648:        memcpy(&old_windows, &s->windows, sizeof old_windows);
                    649:        RB_INIT(&s->windows);
                    650:
                    651:        /* Link all the windows from the target. */
1.18      nicm      652:        RB_FOREACH(wl, winlinks, ww) {
1.30      nicm      653:                wl2 = winlink_add(&s->windows, wl->idx);
1.70      nicm      654:                wl2->session = s;
1.30      nicm      655:                winlink_set_window(wl2, wl->window);
1.68      nicm      656:                notify_session_window("window-linked", s, wl2->window);
1.18      nicm      657:                wl2->flags |= wl->flags & WINLINK_ALERTFLAGS;
                    658:        }
1.11      nicm      659:
                    660:        /* Fix up the current window. */
                    661:        if (s->curw != NULL)
                    662:                s->curw = winlink_find_by_index(&s->windows, s->curw->idx);
                    663:        else
                    664:                s->curw = winlink_find_by_index(&s->windows, target->curw->idx);
                    665:
                    666:        /* Fix up the last window stack. */
                    667:        memcpy(&old_lastw, &s->lastw, sizeof old_lastw);
                    668:        TAILQ_INIT(&s->lastw);
                    669:        TAILQ_FOREACH(wl, &old_lastw, sentry) {
                    670:                wl2 = winlink_find_by_index(&s->windows, wl->idx);
                    671:                if (wl2 != NULL)
                    672:                        TAILQ_INSERT_TAIL(&s->lastw, wl2, sentry);
                    673:        }
                    674:
                    675:        /* Then free the old winlinks list. */
                    676:        while (!RB_EMPTY(&old_windows)) {
                    677:                wl = RB_ROOT(&old_windows);
1.45      nicm      678:                wl2 = winlink_find_by_window_id(&s->windows, wl->window->id);
                    679:                if (wl2 == NULL)
1.68      nicm      680:                        notify_session_window("window-unlinked", s, wl->window);
1.13      nicm      681:                winlink_remove(&old_windows, wl);
1.11      nicm      682:        }
1.34      nicm      683: }
                    684:
                    685: /* Renumber the windows across winlinks attached to a specific session. */
                    686: void
                    687: session_renumber_windows(struct session *s)
                    688: {
                    689:        struct winlink          *wl, *wl1, *wl_new;
                    690:        struct winlinks          old_wins;
                    691:        struct winlink_stack     old_lastw;
                    692:        int                      new_idx, new_curw_idx;
                    693:
                    694:        /* Save and replace old window list. */
                    695:        memcpy(&old_wins, &s->windows, sizeof old_wins);
                    696:        RB_INIT(&s->windows);
                    697:
                    698:        /* Start renumbering from the base-index if it's set. */
1.56      nicm      699:        new_idx = options_get_number(s->options, "base-index");
1.34      nicm      700:        new_curw_idx = 0;
                    701:
                    702:        /* Go through the winlinks and assign new indexes. */
                    703:        RB_FOREACH(wl, winlinks, &old_wins) {
                    704:                wl_new = winlink_add(&s->windows, new_idx);
1.70      nicm      705:                wl_new->session = s;
1.34      nicm      706:                winlink_set_window(wl_new, wl->window);
                    707:                wl_new->flags |= wl->flags & WINLINK_ALERTFLAGS;
                    708:
                    709:                if (wl == s->curw)
                    710:                        new_curw_idx = wl_new->idx;
                    711:
                    712:                new_idx++;
                    713:        }
                    714:
                    715:        /* Fix the stack of last windows now. */
                    716:        memcpy(&old_lastw, &s->lastw, sizeof old_lastw);
                    717:        TAILQ_INIT(&s->lastw);
                    718:        TAILQ_FOREACH(wl, &old_lastw, sentry) {
1.40      nicm      719:                wl_new = winlink_find_by_window(&s->windows, wl->window);
1.34      nicm      720:                if (wl_new != NULL)
                    721:                        TAILQ_INSERT_TAIL(&s->lastw, wl_new, sentry);
                    722:        }
                    723:
                    724:        /* Set the current window. */
                    725:        s->curw = winlink_find_by_index(&s->windows, new_curw_idx);
                    726:
                    727:        /* Free the old winlinks (reducing window references too). */
                    728:        RB_FOREACH_SAFE(wl, winlinks, &old_wins, wl1)
                    729:                winlink_remove(&old_wins, wl);
1.1       nicm      730: }