Merge branch 'obsd-master'
[tmux.git] / server-fn.c
blob2a79f3e3d76653592238e1f9817320f1fdb56336
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/wait.h>
21 #include <sys/uio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <time.h>
26 #include <unistd.h>
28 #include "tmux.h"
30 static struct session *server_next_session(struct session *);
31 static void server_destroy_session_group(struct session *);
33 void
34 server_redraw_client(struct client *c)
36 c->flags |= CLIENT_ALLREDRAWFLAGS;
39 void
40 server_status_client(struct client *c)
42 c->flags |= CLIENT_REDRAWSTATUS;
45 void
46 server_redraw_session(struct session *s)
48 struct client *c;
50 TAILQ_FOREACH(c, &clients, entry) {
51 if (c->session == s)
52 server_redraw_client(c);
56 void
57 server_redraw_session_group(struct session *s)
59 struct session_group *sg;
61 if ((sg = session_group_contains(s)) == NULL)
62 server_redraw_session(s);
63 else {
64 TAILQ_FOREACH(s, &sg->sessions, gentry)
65 server_redraw_session(s);
69 void
70 server_status_session(struct session *s)
72 struct client *c;
74 TAILQ_FOREACH(c, &clients, entry) {
75 if (c->session == s)
76 server_status_client(c);
80 void
81 server_status_session_group(struct session *s)
83 struct session_group *sg;
85 if ((sg = session_group_contains(s)) == NULL)
86 server_status_session(s);
87 else {
88 TAILQ_FOREACH(s, &sg->sessions, gentry)
89 server_status_session(s);
93 void
94 server_redraw_window(struct window *w)
96 struct client *c;
98 TAILQ_FOREACH(c, &clients, entry) {
99 if (c->session != NULL && c->session->curw->window == w)
100 server_redraw_client(c);
104 void
105 server_redraw_window_borders(struct window *w)
107 struct client *c;
109 TAILQ_FOREACH(c, &clients, entry) {
110 if (c->session != NULL && c->session->curw->window == w)
111 c->flags |= CLIENT_REDRAWBORDERS;
115 void
116 server_status_window(struct window *w)
118 struct session *s;
121 * This is slightly different. We want to redraw the status line of any
122 * clients containing this window rather than anywhere it is the
123 * current window.
126 RB_FOREACH(s, sessions, &sessions) {
127 if (session_has(s, w))
128 server_status_session(s);
132 void
133 server_lock(void)
135 struct client *c;
137 TAILQ_FOREACH(c, &clients, entry) {
138 if (c->session != NULL)
139 server_lock_client(c);
143 void
144 server_lock_session(struct session *s)
146 struct client *c;
148 TAILQ_FOREACH(c, &clients, entry) {
149 if (c->session == s)
150 server_lock_client(c);
154 void
155 server_lock_client(struct client *c)
157 const char *cmd;
159 if (c->flags & CLIENT_CONTROL)
160 return;
162 if (c->flags & CLIENT_SUSPENDED)
163 return;
165 cmd = options_get_string(c->session->options, "lock-command");
166 if (*cmd == '\0' || strlen(cmd) + 1 > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
167 return;
169 tty_stop_tty(&c->tty);
170 tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_SMCUP));
171 tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_CLEAR));
172 tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_E3));
174 c->flags |= CLIENT_SUSPENDED;
175 proc_send(c->peer, MSG_LOCK, -1, cmd, strlen(cmd) + 1);
178 void
179 server_kill_pane(struct window_pane *wp)
181 struct window *w = wp->window;
183 if (window_count_panes(w) == 1) {
184 server_kill_window(w, 1);
185 recalculate_sizes();
186 } else {
187 server_unzoom_window(w);
188 server_client_remove_pane(wp);
189 layout_close_pane(wp);
190 window_remove_pane(w, wp);
191 server_redraw_window(w);
195 void
196 server_kill_window(struct window *w, int renumber)
198 struct session *s, *s1;
199 struct winlink *wl;
201 RB_FOREACH_SAFE(s, sessions, &sessions, s1) {
202 if (!session_has(s, w))
203 continue;
205 server_unzoom_window(w);
206 while ((wl = winlink_find_by_window(&s->windows, w)) != NULL) {
207 if (session_detach(s, wl)) {
208 server_destroy_session_group(s);
209 break;
210 } else
211 server_redraw_session_group(s);
214 if (renumber)
215 server_renumber_session(s);
217 recalculate_sizes();
220 void
221 server_renumber_session(struct session *s)
223 struct session_group *sg;
225 if (options_get_number(s->options, "renumber-windows")) {
226 if ((sg = session_group_contains(s)) != NULL) {
227 TAILQ_FOREACH(s, &sg->sessions, gentry)
228 session_renumber_windows(s);
229 } else
230 session_renumber_windows(s);
234 void
235 server_renumber_all(void)
237 struct session *s;
239 RB_FOREACH(s, sessions, &sessions)
240 server_renumber_session(s);
244 server_link_window(struct session *src, struct winlink *srcwl,
245 struct session *dst, int dstidx, int killflag, int selectflag,
246 char **cause)
248 struct winlink *dstwl;
249 struct session_group *srcsg, *dstsg;
251 srcsg = session_group_contains(src);
252 dstsg = session_group_contains(dst);
253 if (src != dst && srcsg != NULL && dstsg != NULL && srcsg == dstsg) {
254 xasprintf(cause, "sessions are grouped");
255 return (-1);
258 dstwl = NULL;
259 if (dstidx != -1)
260 dstwl = winlink_find_by_index(&dst->windows, dstidx);
261 if (dstwl != NULL) {
262 if (dstwl->window == srcwl->window) {
263 xasprintf(cause, "same index: %d", dstidx);
264 return (-1);
266 if (killflag) {
268 * Can't use session_detach as it will destroy session
269 * if this makes it empty.
271 notify_session_window("window-unlinked", dst,
272 dstwl->window);
273 dstwl->flags &= ~WINLINK_ALERTFLAGS;
274 winlink_stack_remove(&dst->lastw, dstwl);
275 winlink_remove(&dst->windows, dstwl);
277 /* Force select/redraw if current. */
278 if (dstwl == dst->curw) {
279 selectflag = 1;
280 dst->curw = NULL;
285 if (dstidx == -1)
286 dstidx = -1 - options_get_number(dst->options, "base-index");
287 dstwl = session_attach(dst, srcwl->window, dstidx, cause);
288 if (dstwl == NULL)
289 return (-1);
291 if (selectflag)
292 session_select(dst, dstwl->idx);
293 server_redraw_session_group(dst);
295 return (0);
298 void
299 server_unlink_window(struct session *s, struct winlink *wl)
301 if (session_detach(s, wl))
302 server_destroy_session_group(s);
303 else
304 server_redraw_session_group(s);
307 void
308 server_destroy_pane(struct window_pane *wp, int notify)
310 struct window *w = wp->window;
311 struct screen_write_ctx ctx;
312 struct grid_cell gc;
313 int remain_on_exit;
314 const char *s;
315 char *expanded;
316 u_int sx = screen_size_x(&wp->base);
317 u_int sy = screen_size_y(&wp->base);
319 if (wp->fd != -1) {
320 #ifdef HAVE_UTEMPTER
321 utempter_remove_record(wp->fd);
322 #endif
323 bufferevent_free(wp->event);
324 wp->event = NULL;
325 close(wp->fd);
326 wp->fd = -1;
329 remain_on_exit = options_get_number(wp->options, "remain-on-exit");
330 if (remain_on_exit != 0 && (~wp->flags & PANE_STATUSREADY))
331 return;
332 switch (remain_on_exit) {
333 case 0:
334 break;
335 case 2:
336 if (WIFEXITED(wp->status) && WEXITSTATUS(wp->status) == 0)
337 break;
338 /* FALLTHROUGH */
339 case 1:
340 if (wp->flags & PANE_STATUSDRAWN)
341 return;
342 wp->flags |= PANE_STATUSDRAWN;
344 gettimeofday(&wp->dead_time, NULL);
345 if (notify)
346 notify_pane("pane-died", wp);
348 s = options_get_string(wp->options, "remain-on-exit-format");
349 if (*s != '\0') {
350 screen_write_start_pane(&ctx, wp, &wp->base);
351 screen_write_scrollregion(&ctx, 0, sy - 1);
352 screen_write_cursormove(&ctx, 0, sy - 1, 0);
353 screen_write_linefeed(&ctx, 1, 8);
354 memcpy(&gc, &grid_default_cell, sizeof gc);
356 expanded = format_single(NULL, s, NULL, NULL, NULL, wp);
357 format_draw(&ctx, &gc, sx, expanded, NULL, 0);
358 free(expanded);
360 screen_write_stop(&ctx);
362 wp->base.mode &= ~MODE_CURSOR;
364 wp->flags |= PANE_REDRAW;
365 return;
368 if (notify)
369 notify_pane("pane-exited", wp);
371 server_unzoom_window(w);
372 server_client_remove_pane(wp);
373 layout_close_pane(wp);
374 window_remove_pane(w, wp);
376 if (TAILQ_EMPTY(&w->panes))
377 server_kill_window(w, 1);
378 else
379 server_redraw_window(w);
382 static void
383 server_destroy_session_group(struct session *s)
385 struct session_group *sg;
386 struct session *s1;
388 if ((sg = session_group_contains(s)) == NULL)
389 server_destroy_session(s);
390 else {
391 TAILQ_FOREACH_SAFE(s, &sg->sessions, gentry, s1) {
392 server_destroy_session(s);
393 session_destroy(s, 1, __func__);
398 static struct session *
399 server_next_session(struct session *s)
401 struct session *s_loop, *s_out = NULL;
403 RB_FOREACH(s_loop, sessions, &sessions) {
404 if (s_loop == s)
405 continue;
406 if (s_out == NULL ||
407 timercmp(&s_loop->activity_time, &s_out->activity_time, <))
408 s_out = s_loop;
410 return (s_out);
413 static struct session *
414 server_next_detached_session(struct session *s)
416 struct session *s_loop, *s_out = NULL;
418 RB_FOREACH(s_loop, sessions, &sessions) {
419 if (s_loop == s || s_loop->attached)
420 continue;
421 if (s_out == NULL ||
422 timercmp(&s_loop->activity_time, &s_out->activity_time, <))
423 s_out = s_loop;
425 return (s_out);
428 void
429 server_destroy_session(struct session *s)
431 struct client *c;
432 struct session *s_new;
433 int detach_on_destroy;
435 detach_on_destroy = options_get_number(s->options, "detach-on-destroy");
436 if (detach_on_destroy == 0)
437 s_new = server_next_session(s);
438 else if (detach_on_destroy == 2)
439 s_new = server_next_detached_session(s);
440 else
441 s_new = NULL;
442 TAILQ_FOREACH(c, &clients, entry) {
443 if (c->session != s)
444 continue;
445 server_client_set_session(c, s_new);
446 if (s_new == NULL)
447 c->flags |= CLIENT_EXIT;
449 recalculate_sizes();
452 void
453 server_check_unattached(void)
455 struct session *s;
458 * If any sessions are no longer attached and have destroy-unattached
459 * set, collect them.
461 RB_FOREACH(s, sessions, &sessions) {
462 if (s->attached != 0)
463 continue;
464 if (options_get_number (s->options, "destroy-unattached"))
465 session_destroy(s, 1, __func__);
469 void
470 server_unzoom_window(struct window *w)
472 if (window_unzoom(w) == 0)
473 server_redraw_window(w);