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