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