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