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