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