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

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