Lines Matching defs:s

61 session_alive(struct session *s)
66 if (s_loop == s)
76 struct session s;
78 s.name = (char *) name;
79 return (RB_FIND(sessions, &sessions, &s));
84 session_find_by_id_str(const char *s)
89 if (*s != '$')
92 id = strtonum(s + 1, 0, UINT_MAX, &errstr);
102 struct session *s;
104 RB_FOREACH(s, sessions, &sessions) {
105 if (s->id == id)
106 return (s);
116 struct session *s;
118 s = xcalloc(1, sizeof *s);
119 s->references = 1;
120 s->flags = 0;
122 s->cwd = xstrdup(cwd);
124 TAILQ_INIT(&s->lastw);
125 RB_INIT(&s->windows);
127 s->environ = env;
128 s->options = oo;
130 status_update_cache(s);
132 s->tio = NULL;
134 s->tio = xmalloc(sizeof *s->tio);
135 memcpy(s->tio, tio, sizeof *s->tio);
139 s->name = xstrdup(name);
140 s->id = next_session_id++;
143 s->id = next_session_id++;
144 free(s->name);
146 xasprintf(&s->name, "%s-%u", prefix, s->id);
148 xasprintf(&s->name, "%u", s->id);
149 } while (RB_FIND(sessions, &sessions, s) != NULL);
151 RB_INSERT(sessions, &sessions, s);
153 log_debug("new session %s $%u", s->name, s->id);
155 if (gettimeofday(&s->creation_time, NULL) != 0)
157 session_update_activity(s, &s->creation_time);
159 return (s);
164 session_add_ref(struct session *s, const char *from)
166 s->references++;
167 log_debug("%s: %s %s, now %d", __func__, s->name, from, s->references);
172 session_remove_ref(struct session *s, const char *from)
174 s->references--;
175 log_debug("%s: %s %s, now %d", __func__, s->name, from, s->references);
177 if (s->references == 0)
178 event_once(-1, EV_TIMEOUT, session_free, s, NULL);
185 struct session *s = arg;
187 log_debug("session %s freed (%d references)", s->name, s->references);
189 if (s->references == 0) {
190 environ_free(s->environ);
191 options_free(s->options);
193 free(s->name);
194 free(s);
200 session_destroy(struct session *s, int notify, const char *from)
204 log_debug("session %s destroyed (%s)", s->name, from);
206 if (s->curw == NULL)
208 s->curw = NULL;
210 RB_REMOVE(sessions, &sessions, s);
212 notify_session("session-closed", s);
214 free(s->tio);
216 if (event_initialized(&s->lock_timer))
217 event_del(&s->lock_timer);
219 session_group_remove(s);
221 while (!TAILQ_EMPTY(&s->lastw))
222 winlink_stack_remove(&s->lastw, TAILQ_FIRST(&s->lastw));
223 while (!RB_EMPTY(&s->windows)) {
224 wl = RB_ROOT(&s->windows);
225 notify_session_window("window-unlinked", s, wl->window);
226 winlink_remove(&s->windows, wl);
229 free((void *)s->cwd);
231 session_remove_ref(s, __func__);
256 struct session *s = arg;
258 if (s->attached == 0)
261 log_debug("session %s locked, activity time %lld", s->name,
262 (long long)s->activity_time.tv_sec);
264 server_lock_session(s);
270 session_update_activity(struct session *s, struct timeval *from)
275 gettimeofday(&s->activity_time, NULL);
277 memcpy(&s->activity_time, from, sizeof s->activity_time);
279 log_debug("session $%u %s activity %lld.%06d", s->id,
280 s->name, (long long)s->activity_time.tv_sec,
281 (int)s->activity_time.tv_usec);
283 if (evtimer_initialized(&s->lock_timer))
284 evtimer_del(&s->lock_timer);
286 evtimer_set(&s->lock_timer, session_lock_timer, s);
288 if (s->attached != 0) {
290 tv.tv_sec = options_get_number(s->options, "lock-after-time");
292 evtimer_add(&s->lock_timer, &tv);
298 session_next_session(struct session *s)
302 if (RB_EMPTY(&sessions) || !session_alive(s))
305 s2 = RB_NEXT(sessions, &sessions, s);
308 if (s2 == s)
315 session_previous_session(struct session *s)
319 if (RB_EMPTY(&sessions) || !session_alive(s))
322 s2 = RB_PREV(sessions, &sessions, s);
325 if (s2 == s)
332 session_attach(struct session *s, struct window *w, int idx, char **cause)
336 if ((wl = winlink_add(&s->windows, idx)) == NULL) {
340 wl->session = s;
342 notify_session_window("window-linked", s, w);
344 session_group_synchronize_from(s);
350 session_detach(struct session *s, struct winlink *wl)
352 if (s->curw == wl &&
353 session_last(s) != 0 &&
354 session_previous(s, 0) != 0)
355 session_next(s, 0);
358 notify_session_window("window-unlinked", s, wl->window);
359 winlink_stack_remove(&s->lastw, wl);
360 winlink_remove(&s->windows, wl);
362 session_group_synchronize_from(s);
364 if (RB_EMPTY(&s->windows))
371 session_has(struct session *s, struct window *w)
376 if (wl->session == s)
387 session_is_linked(struct session *s, struct window *w)
391 if ((sg = session_group_contains(s)) != NULL)
409 session_next(struct session *s, int alert)
413 if (s->curw == NULL)
416 wl = winlink_next(s->curw);
420 wl = RB_MIN(winlinks, &s->windows);
424 return (session_set_current(s, wl));
440 session_previous(struct session *s, int alert)
444 if (s->curw == NULL)
447 wl = winlink_previous(s->curw);
451 wl = RB_MAX(winlinks, &s->windows);
455 return (session_set_current(s, wl));
460 session_select(struct session *s, int idx)
464 wl = winlink_find_by_index(&s->windows, idx);
465 return (session_set_current(s, wl));
470 session_last(struct session *s)
474 wl = TAILQ_FIRST(&s->lastw);
477 if (wl == s->curw)
480 return (session_set_current(s, wl));
485 session_set_current(struct session *s, struct winlink *wl)
487 struct winlink *old = s->curw;
491 if (wl == s->curw)
494 winlink_stack_remove(&s->lastw, wl);
495 winlink_stack_push(&s->lastw, s->curw);
496 s->curw = wl;
505 notify_session("session-window-changed", s);
514 struct session *s;
517 TAILQ_FOREACH(s, &sg->sessions, gentry) {
518 if (s == target)
554 session_group_add(struct session_group *sg, struct session *s)
556 if (session_group_contains(s) == NULL)
557 TAILQ_INSERT_TAIL(&sg->sessions, s, gentry);
562 session_group_remove(struct session *s)
566 if ((sg = session_group_contains(s)) == NULL)
568 TAILQ_REMOVE(&sg->sessions, s, gentry);
580 struct session *s;
584 TAILQ_FOREACH(s, &sg->sessions, gentry)
593 struct session *s;
597 TAILQ_FOREACH(s, &sg->sessions, gentry)
598 n += s->attached;
604 session_group_synchronize_to(struct session *s)
609 if ((sg = session_group_contains(s)) == NULL)
614 if (target != s)
618 session_group_synchronize1(target, s);
626 struct session *s;
631 TAILQ_FOREACH(s, &sg->sessions, gentry) {
632 if (s != target)
633 session_group_synchronize1(target, s);
643 session_group_synchronize1(struct session *target, struct session *s)
655 if (s->curw != NULL &&
656 winlink_find_by_index(ww, s->curw->idx) == NULL &&
657 session_last(s) != 0 && session_previous(s, 0) != 0)
658 session_next(s, 0);
661 memcpy(&old_windows, &s->windows, sizeof old_windows);
662 RB_INIT(&s->windows);
666 wl2 = winlink_add(&s->windows, wl->idx);
667 wl2->session = s;
669 notify_session_window("window-linked", s, wl2->window);
674 if (s->curw != NULL)
675 s->curw = winlink_find_by_index(&s->windows, s->curw->idx);
677 s->curw = winlink_find_by_index(&s->windows, target->curw->idx);
680 memcpy(&old_lastw, &s->lastw, sizeof old_lastw);
681 TAILQ_INIT(&s->lastw);
683 wl2 = winlink_find_by_index(&s->windows, wl->idx);
685 TAILQ_INSERT_TAIL(&s->lastw, wl2, sentry);
693 wl2 = winlink_find_by_window_id(&s->windows, wl->window->id);
695 notify_session_window("window-unlinked", s, wl->window);
702 session_renumber_windows(struct session *s)
710 memcpy(&old_wins, &s->windows, sizeof old_wins);
711 RB_INIT(&s->windows);
713 /* Start renumbering from the base-index if it's set. */
714 new_idx = options_get_number(s->options, "base-index");
719 wl_new = winlink_add(&s->windows, new_idx);
720 wl_new->session = s;
726 if (wl == s->curw)
733 memcpy(&old_lastw, &s->lastw, sizeof old_lastw);
734 TAILQ_INIT(&s->lastw);
737 wl_new = winlink_find_by_window(&s->windows, wl->window);
739 TAILQ_INSERT_TAIL(&s->lastw, wl_new, sentry);
746 marked_pane.wl = winlink_find_by_index(&s->windows, marked_idx);
750 s->curw = winlink_find_by_index(&s->windows, new_curw_idx);