Merge branch 'obsd-master'
[tmux.git] / session.c
blob0d79c82b1e6dac571b04c425e166147fb0bb0720
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
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.
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.
19 #include <sys/types.h>
20 #include <sys/time.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <time.h>
27 #include "tmux.h"
29 struct sessions sessions;
30 u_int next_session_id;
31 struct session_groups session_groups = RB_INITIALIZER(&session_groups);
33 static void session_free(int, short, void *);
35 static void session_lock_timer(int, short, void *);
37 static struct winlink *session_next_alert(struct winlink *);
38 static struct winlink *session_previous_alert(struct winlink *);
40 static void session_group_remove(struct session *);
41 static void session_group_synchronize1(struct session *, struct session *);
43 int
44 session_cmp(struct session *s1, struct session *s2)
46 return (strcmp(s1->name, s2->name));
48 RB_GENERATE(sessions, session, entry, session_cmp);
50 static int
51 session_group_cmp(struct session_group *s1, struct session_group *s2)
53 return (strcmp(s1->name, s2->name));
55 RB_GENERATE_STATIC(session_groups, session_group, entry, session_group_cmp);
58 * Find if session is still alive. This is true if it is still on the global
59 * sessions list.
61 int
62 session_alive(struct session *s)
64 struct session *s_loop;
66 RB_FOREACH(s_loop, sessions, &sessions) {
67 if (s_loop == s)
68 return (1);
70 return (0);
73 /* Find session by name. */
74 struct session *
75 session_find(const char *name)
77 struct session s;
79 s.name = (char *) name;
80 return (RB_FIND(sessions, &sessions, &s));
83 /* Find session by id parsed from a string. */
84 struct session *
85 session_find_by_id_str(const char *s)
87 const char *errstr;
88 u_int id;
90 if (*s != '$')
91 return (NULL);
93 id = strtonum(s + 1, 0, UINT_MAX, &errstr);
94 if (errstr != NULL)
95 return (NULL);
96 return (session_find_by_id(id));
99 /* Find session by id. */
100 struct session *
101 session_find_by_id(u_int id)
103 struct session *s;
105 RB_FOREACH(s, sessions, &sessions) {
106 if (s->id == id)
107 return (s);
109 return (NULL);
112 /* Create a new session. */
113 struct session *
114 session_create(const char *prefix, const char *name, const char *cwd,
115 struct environ *env, struct options *oo, struct termios *tio)
117 struct session *s;
119 s = xcalloc(1, sizeof *s);
120 s->references = 1;
121 s->flags = 0;
123 s->cwd = xstrdup(cwd);
125 TAILQ_INIT(&s->lastw);
126 RB_INIT(&s->windows);
128 s->environ = env;
129 s->options = oo;
131 status_update_cache(s);
133 s->tio = NULL;
134 if (tio != NULL) {
135 s->tio = xmalloc(sizeof *s->tio);
136 memcpy(s->tio, tio, sizeof *s->tio);
139 if (name != NULL) {
140 s->name = xstrdup(name);
141 s->id = next_session_id++;
142 } else {
143 do {
144 s->id = next_session_id++;
145 free(s->name);
146 if (prefix != NULL)
147 xasprintf(&s->name, "%s-%u", prefix, s->id);
148 else
149 xasprintf(&s->name, "%u", s->id);
150 } while (RB_FIND(sessions, &sessions, s) != NULL);
152 RB_INSERT(sessions, &sessions, s);
154 log_debug("new session %s $%u", s->name, s->id);
156 if (gettimeofday(&s->creation_time, NULL) != 0)
157 fatal("gettimeofday failed");
158 session_update_activity(s, &s->creation_time);
160 return (s);
163 /* Add a reference to a session. */
164 void
165 session_add_ref(struct session *s, const char *from)
167 s->references++;
168 log_debug("%s: %s %s, now %d", __func__, s->name, from, s->references);
171 /* Remove a reference from a session. */
172 void
173 session_remove_ref(struct session *s, const char *from)
175 s->references--;
176 log_debug("%s: %s %s, now %d", __func__, s->name, from, s->references);
178 if (s->references == 0)
179 event_once(-1, EV_TIMEOUT, session_free, s, NULL);
182 /* Free session. */
183 static void
184 session_free(__unused int fd, __unused short events, void *arg)
186 struct session *s = arg;
188 log_debug("session %s freed (%d references)", s->name, s->references);
190 if (s->references == 0) {
191 environ_free(s->environ);
192 options_free(s->options);
194 free(s->name);
195 free(s);
199 /* Destroy a session. */
200 void
201 session_destroy(struct session *s, int notify, const char *from)
203 struct winlink *wl;
205 log_debug("session %s destroyed (%s)", s->name, from);
207 if (s->curw == NULL)
208 return;
209 s->curw = NULL;
211 RB_REMOVE(sessions, &sessions, s);
212 if (notify)
213 notify_session("session-closed", s);
215 free(s->tio);
217 if (event_initialized(&s->lock_timer))
218 event_del(&s->lock_timer);
220 session_group_remove(s);
222 while (!TAILQ_EMPTY(&s->lastw))
223 winlink_stack_remove(&s->lastw, TAILQ_FIRST(&s->lastw));
224 while (!RB_EMPTY(&s->windows)) {
225 wl = RB_ROOT(&s->windows);
226 notify_session_window("window-unlinked", s, wl->window);
227 winlink_remove(&s->windows, wl);
230 free((void *)s->cwd);
232 session_remove_ref(s, __func__);
235 /* Sanitize session name. */
236 char *
237 session_check_name(const char *name)
239 char *copy, *cp, *new_name;
241 if (*name == '\0')
242 return (NULL);
243 copy = xstrdup(name);
244 for (cp = copy; *cp != '\0'; cp++) {
245 if (*cp == ':' || *cp == '.')
246 *cp = '_';
248 utf8_stravis(&new_name, copy, VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL);
249 free(copy);
250 return (new_name);
253 /* Lock session if it has timed out. */
254 static void
255 session_lock_timer(__unused int fd, __unused short events, void *arg)
257 struct session *s = arg;
259 if (s->attached == 0)
260 return;
262 log_debug("session %s locked, activity time %lld", s->name,
263 (long long)s->activity_time.tv_sec);
265 server_lock_session(s);
266 recalculate_sizes();
269 /* Update activity time. */
270 void
271 session_update_activity(struct session *s, struct timeval *from)
273 struct timeval tv;
275 if (from == NULL)
276 gettimeofday(&s->activity_time, NULL);
277 else
278 memcpy(&s->activity_time, from, sizeof s->activity_time);
280 log_debug("session $%u %s activity %lld.%06d", s->id,
281 s->name, (long long)s->activity_time.tv_sec,
282 (int)s->activity_time.tv_usec);
284 if (evtimer_initialized(&s->lock_timer))
285 evtimer_del(&s->lock_timer);
286 else
287 evtimer_set(&s->lock_timer, session_lock_timer, s);
289 if (s->attached != 0) {
290 timerclear(&tv);
291 tv.tv_sec = options_get_number(s->options, "lock-after-time");
292 if (tv.tv_sec != 0)
293 evtimer_add(&s->lock_timer, &tv);
297 /* Find the next usable session. */
298 struct session *
299 session_next_session(struct session *s)
301 struct session *s2;
303 if (RB_EMPTY(&sessions) || !session_alive(s))
304 return (NULL);
306 s2 = RB_NEXT(sessions, &sessions, s);
307 if (s2 == NULL)
308 s2 = RB_MIN(sessions, &sessions);
309 if (s2 == s)
310 return (NULL);
311 return (s2);
314 /* Find the previous usable session. */
315 struct session *
316 session_previous_session(struct session *s)
318 struct session *s2;
320 if (RB_EMPTY(&sessions) || !session_alive(s))
321 return (NULL);
323 s2 = RB_PREV(sessions, &sessions, s);
324 if (s2 == NULL)
325 s2 = RB_MAX(sessions, &sessions);
326 if (s2 == s)
327 return (NULL);
328 return (s2);
331 /* Attach a window to a session. */
332 struct winlink *
333 session_attach(struct session *s, struct window *w, int idx, char **cause)
335 struct winlink *wl;
337 if ((wl = winlink_add(&s->windows, idx)) == NULL) {
338 xasprintf(cause, "index in use: %d", idx);
339 return (NULL);
341 wl->session = s;
342 winlink_set_window(wl, w);
343 notify_session_window("window-linked", s, w);
345 session_group_synchronize_from(s);
346 return (wl);
349 /* Detach a window from a session. */
351 session_detach(struct session *s, struct winlink *wl)
353 if (s->curw == wl &&
354 session_last(s) != 0 &&
355 session_previous(s, 0) != 0)
356 session_next(s, 0);
358 wl->flags &= ~WINLINK_ALERTFLAGS;
359 notify_session_window("window-unlinked", s, wl->window);
360 winlink_stack_remove(&s->lastw, wl);
361 winlink_remove(&s->windows, wl);
363 session_group_synchronize_from(s);
365 if (RB_EMPTY(&s->windows))
366 return (1);
367 return (0);
370 /* Return if session has window. */
372 session_has(struct session *s, struct window *w)
374 struct winlink *wl;
376 TAILQ_FOREACH(wl, &w->winlinks, wentry) {
377 if (wl->session == s)
378 return (1);
380 return (0);
384 * Return 1 if a window is linked outside this session (not including session
385 * groups). The window must be in this session!
388 session_is_linked(struct session *s, struct window *w)
390 struct session_group *sg;
392 if ((sg = session_group_contains(s)) != NULL)
393 return (w->references != session_group_count(sg));
394 return (w->references != 1);
397 static struct winlink *
398 session_next_alert(struct winlink *wl)
400 while (wl != NULL) {
401 if (wl->flags & WINLINK_ALERTFLAGS)
402 break;
403 wl = winlink_next(wl);
405 return (wl);
408 /* Move session to next window. */
410 session_next(struct session *s, int alert)
412 struct winlink *wl;
414 if (s->curw == NULL)
415 return (-1);
417 wl = winlink_next(s->curw);
418 if (alert)
419 wl = session_next_alert(wl);
420 if (wl == NULL) {
421 wl = RB_MIN(winlinks, &s->windows);
422 if (alert && ((wl = session_next_alert(wl)) == NULL))
423 return (-1);
425 return (session_set_current(s, wl));
428 static struct winlink *
429 session_previous_alert(struct winlink *wl)
431 while (wl != NULL) {
432 if (wl->flags & WINLINK_ALERTFLAGS)
433 break;
434 wl = winlink_previous(wl);
436 return (wl);
439 /* Move session to previous window. */
441 session_previous(struct session *s, int alert)
443 struct winlink *wl;
445 if (s->curw == NULL)
446 return (-1);
448 wl = winlink_previous(s->curw);
449 if (alert)
450 wl = session_previous_alert(wl);
451 if (wl == NULL) {
452 wl = RB_MAX(winlinks, &s->windows);
453 if (alert && (wl = session_previous_alert(wl)) == NULL)
454 return (-1);
456 return (session_set_current(s, wl));
459 /* Move session to specific window. */
461 session_select(struct session *s, int idx)
463 struct winlink *wl;
465 wl = winlink_find_by_index(&s->windows, idx);
466 return (session_set_current(s, wl));
469 /* Move session to last used window. */
471 session_last(struct session *s)
473 struct winlink *wl;
475 wl = TAILQ_FIRST(&s->lastw);
476 if (wl == NULL)
477 return (-1);
478 if (wl == s->curw)
479 return (1);
481 return (session_set_current(s, wl));
484 /* Set current winlink to wl .*/
486 session_set_current(struct session *s, struct winlink *wl)
488 struct winlink *old = s->curw;
490 if (wl == NULL)
491 return (-1);
492 if (wl == s->curw)
493 return (1);
495 winlink_stack_remove(&s->lastw, wl);
496 winlink_stack_push(&s->lastw, s->curw);
497 s->curw = wl;
498 if (options_get_number(global_options, "focus-events")) {
499 if (old != NULL)
500 window_update_focus(old->window);
501 window_update_focus(wl->window);
503 winlink_clear_flags(wl);
504 window_update_activity(wl->window);
505 tty_update_window_offset(wl->window);
506 notify_session("session-window-changed", s);
507 return (0);
510 /* Find the session group containing a session. */
511 struct session_group *
512 session_group_contains(struct session *target)
514 struct session_group *sg;
515 struct session *s;
517 RB_FOREACH(sg, session_groups, &session_groups) {
518 TAILQ_FOREACH(s, &sg->sessions, gentry) {
519 if (s == target)
520 return (sg);
523 return (NULL);
526 /* Find session group by name. */
527 struct session_group *
528 session_group_find(const char *name)
530 struct session_group sg;
532 sg.name = name;
533 return (RB_FIND(session_groups, &session_groups, &sg));
536 /* Create a new session group. */
537 struct session_group *
538 session_group_new(const char *name)
540 struct session_group *sg;
542 if ((sg = session_group_find(name)) != NULL)
543 return (sg);
545 sg = xcalloc(1, sizeof *sg);
546 sg->name = xstrdup(name);
547 TAILQ_INIT(&sg->sessions);
549 RB_INSERT(session_groups, &session_groups, sg);
550 return (sg);
553 /* Add a session to a session group. */
554 void
555 session_group_add(struct session_group *sg, struct session *s)
557 if (session_group_contains(s) == NULL)
558 TAILQ_INSERT_TAIL(&sg->sessions, s, gentry);
561 /* Remove a session from its group and destroy the group if empty. */
562 static void
563 session_group_remove(struct session *s)
565 struct session_group *sg;
567 if ((sg = session_group_contains(s)) == NULL)
568 return;
569 TAILQ_REMOVE(&sg->sessions, s, gentry);
570 if (TAILQ_EMPTY(&sg->sessions)) {
571 RB_REMOVE(session_groups, &session_groups, sg);
572 free((void *)sg->name);
573 free(sg);
577 /* Count number of sessions in session group. */
578 u_int
579 session_group_count(struct session_group *sg)
581 struct session *s;
582 u_int n;
584 n = 0;
585 TAILQ_FOREACH(s, &sg->sessions, gentry)
586 n++;
587 return (n);
590 /* Count number of clients attached to sessions in session group. */
591 u_int
592 session_group_attached_count(struct session_group *sg)
594 struct session *s;
595 u_int n;
597 n = 0;
598 TAILQ_FOREACH(s, &sg->sessions, gentry)
599 n += s->attached;
600 return (n);
603 /* Synchronize a session to its session group. */
604 void
605 session_group_synchronize_to(struct session *s)
607 struct session_group *sg;
608 struct session *target;
610 if ((sg = session_group_contains(s)) == NULL)
611 return;
613 target = NULL;
614 TAILQ_FOREACH(target, &sg->sessions, gentry) {
615 if (target != s)
616 break;
618 if (target != NULL)
619 session_group_synchronize1(target, s);
622 /* Synchronize a session group to a session. */
623 void
624 session_group_synchronize_from(struct session *target)
626 struct session_group *sg;
627 struct session *s;
629 if ((sg = session_group_contains(target)) == NULL)
630 return;
632 TAILQ_FOREACH(s, &sg->sessions, gentry) {
633 if (s != target)
634 session_group_synchronize1(target, s);
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.
643 static void
644 session_group_synchronize1(struct session *target, struct session *s)
646 struct winlinks old_windows, *ww;
647 struct winlink_stack old_lastw;
648 struct winlink *wl, *wl2;
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;
655 /* If the current window has vanished, move to the next now. */
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);
661 /* Save the old pointer and reset it. */
662 memcpy(&old_windows, &s->windows, sizeof old_windows);
663 RB_INIT(&s->windows);
665 /* Link all the windows from the target. */
666 RB_FOREACH(wl, winlinks, ww) {
667 wl2 = winlink_add(&s->windows, wl->idx);
668 wl2->session = s;
669 winlink_set_window(wl2, wl->window);
670 notify_session_window("window-linked", s, wl2->window);
671 wl2->flags |= wl->flags & WINLINK_ALERTFLAGS;
674 /* Fix up the current window. */
675 if (s->curw != NULL)
676 s->curw = winlink_find_by_index(&s->windows, s->curw->idx);
677 else
678 s->curw = winlink_find_by_index(&s->windows, target->curw->idx);
680 /* Fix up the last window stack. */
681 memcpy(&old_lastw, &s->lastw, sizeof old_lastw);
682 TAILQ_INIT(&s->lastw);
683 TAILQ_FOREACH(wl, &old_lastw, sentry) {
684 wl2 = winlink_find_by_index(&s->windows, wl->idx);
685 if (wl2 != NULL) {
686 TAILQ_INSERT_TAIL(&s->lastw, wl2, sentry);
687 wl2->flags |= WINLINK_VISITED;
691 /* Then free the old winlinks list. */
692 while (!RB_EMPTY(&old_windows)) {
693 wl = RB_ROOT(&old_windows);
694 wl2 = winlink_find_by_window_id(&s->windows, wl->window->id);
695 if (wl2 == NULL)
696 notify_session_window("window-unlinked", s, wl->window);
697 winlink_remove(&old_windows, wl);
701 /* Renumber the windows across winlinks attached to a specific session. */
702 void
703 session_renumber_windows(struct session *s)
705 struct winlink *wl, *wl1, *wl_new;
706 struct winlinks old_wins;
707 struct winlink_stack old_lastw;
708 int new_idx, new_curw_idx, marked_idx = -1;
710 /* Save and replace old window list. */
711 memcpy(&old_wins, &s->windows, sizeof old_wins);
712 RB_INIT(&s->windows);
714 /* Start renumbering from the base-index if it's set. */
715 new_idx = options_get_number(s->options, "base-index");
716 new_curw_idx = 0;
718 /* Go through the winlinks and assign new indexes. */
719 RB_FOREACH(wl, winlinks, &old_wins) {
720 wl_new = winlink_add(&s->windows, new_idx);
721 wl_new->session = s;
722 winlink_set_window(wl_new, wl->window);
723 wl_new->flags |= wl->flags & WINLINK_ALERTFLAGS;
725 if (wl == marked_pane.wl)
726 marked_idx = wl_new->idx;
727 if (wl == s->curw)
728 new_curw_idx = wl_new->idx;
730 new_idx++;
733 /* Fix the stack of last windows now. */
734 memcpy(&old_lastw, &s->lastw, sizeof old_lastw);
735 TAILQ_INIT(&s->lastw);
736 TAILQ_FOREACH(wl, &old_lastw, sentry) {
737 wl->flags &= ~WINLINK_VISITED;
738 wl_new = winlink_find_by_window(&s->windows, wl->window);
739 if (wl_new != NULL) {
740 TAILQ_INSERT_TAIL(&s->lastw, wl_new, sentry);
741 wl_new->flags |= WINLINK_VISITED;
745 /* Set the current window. */
746 if (marked_idx != -1) {
747 marked_pane.wl = winlink_find_by_index(&s->windows, marked_idx);
748 if (marked_pane.wl == NULL)
749 server_clear_marked();
751 s->curw = winlink_find_by_index(&s->windows, new_curw_idx);
753 /* Free the old winlinks (reducing window references too). */
754 RB_FOREACH_SAFE(wl, winlinks, &old_wins, wl1)
755 winlink_remove(&old_wins, wl);