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