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

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