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