Request terminal colours again on SIGWINCH but at most once every 30
[tmux.git] / server-fn.c
blob3c14c73388a4c2e708cde9e063fac16cfee0764f
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/queue.h>
21 #include <sys/wait.h>
22 #include <sys/uio.h>
24 #include <imsg.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <time.h>
28 #include <unistd.h>
30 #include "tmux.h"
32 static void server_destroy_session_group(struct session *);
34 void
35 server_redraw_client(struct client *c)
37 c->flags |= CLIENT_ALLREDRAWFLAGS;
40 void
41 server_status_client(struct client *c)
43 c->flags |= CLIENT_REDRAWSTATUS;
46 void
47 server_redraw_session(struct session *s)
49 struct client *c;
51 TAILQ_FOREACH(c, &clients, entry) {
52 if (c->session == s)
53 server_redraw_client(c);
57 void
58 server_redraw_session_group(struct session *s)
60 struct session_group *sg;
62 if ((sg = session_group_contains(s)) == NULL)
63 server_redraw_session(s);
64 else {
65 TAILQ_FOREACH(s, &sg->sessions, gentry)
66 server_redraw_session(s);
70 void
71 server_status_session(struct session *s)
73 struct client *c;
75 TAILQ_FOREACH(c, &clients, entry) {
76 if (c->session == s)
77 server_status_client(c);
81 void
82 server_status_session_group(struct session *s)
84 struct session_group *sg;
86 if ((sg = session_group_contains(s)) == NULL)
87 server_status_session(s);
88 else {
89 TAILQ_FOREACH(s, &sg->sessions, gentry)
90 server_status_session(s);
94 void
95 server_redraw_window(struct window *w)
97 struct client *c;
99 TAILQ_FOREACH(c, &clients, entry) {
100 if (c->session != NULL && c->session->curw->window == w)
101 server_redraw_client(c);
105 void
106 server_redraw_window_borders(struct window *w)
108 struct client *c;
110 TAILQ_FOREACH(c, &clients, entry) {
111 if (c->session != NULL && c->session->curw->window == w)
112 c->flags |= CLIENT_REDRAWBORDERS;
116 void
117 server_status_window(struct window *w)
119 struct session *s;
122 * This is slightly different. We want to redraw the status line of any
123 * clients containing this window rather than anywhere it is the
124 * current window.
127 RB_FOREACH(s, sessions, &sessions) {
128 if (session_has(s, w))
129 server_status_session(s);
133 void
134 server_lock(void)
136 struct client *c;
138 TAILQ_FOREACH(c, &clients, entry) {
139 if (c->session != NULL)
140 server_lock_client(c);
144 void
145 server_lock_session(struct session *s)
147 struct client *c;
149 TAILQ_FOREACH(c, &clients, entry) {
150 if (c->session == s)
151 server_lock_client(c);
155 void
156 server_lock_client(struct client *c)
158 const char *cmd;
160 if (c->flags & CLIENT_CONTROL)
161 return;
163 if (c->flags & CLIENT_SUSPENDED)
164 return;
166 cmd = options_get_string(c->session->options, "lock-command");
167 if (*cmd == '\0' || strlen(cmd) + 1 > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
168 return;
170 tty_stop_tty(&c->tty);
171 tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_SMCUP));
172 tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_CLEAR));
173 tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_E3));
175 c->flags |= CLIENT_SUSPENDED;
176 proc_send(c->peer, MSG_LOCK, -1, cmd, strlen(cmd) + 1);
179 void
180 server_kill_pane(struct window_pane *wp)
182 struct window *w = wp->window;
184 if (window_count_panes(w) == 1) {
185 server_kill_window(w, 1);
186 recalculate_sizes();
187 } else {
188 server_unzoom_window(w);
189 server_client_remove_pane(wp);
190 layout_close_pane(wp);
191 window_remove_pane(w, wp);
192 server_redraw_window(w);
196 void
197 server_kill_window(struct window *w, int renumber)
199 struct session *s, *s1;
200 struct winlink *wl;
202 RB_FOREACH_SAFE(s, sessions, &sessions, s1) {
203 if (!session_has(s, w))
204 continue;
206 server_unzoom_window(w);
207 while ((wl = winlink_find_by_window(&s->windows, w)) != NULL) {
208 if (session_detach(s, wl)) {
209 server_destroy_session_group(s);
210 break;
212 server_redraw_session_group(s);
215 if (renumber)
216 server_renumber_session(s);
218 recalculate_sizes();
221 void
222 server_renumber_session(struct session *s)
224 struct session_group *sg;
226 if (options_get_number(s->options, "renumber-windows")) {
227 if ((sg = session_group_contains(s)) != NULL) {
228 TAILQ_FOREACH(s, &sg->sessions, gentry)
229 session_renumber_windows(s);
230 } else
231 session_renumber_windows(s);
235 void
236 server_renumber_all(void)
238 struct session *s;
240 RB_FOREACH(s, sessions, &sessions)
241 server_renumber_session(s);
245 server_link_window(struct session *src, struct winlink *srcwl,
246 struct session *dst, int dstidx, int killflag, int selectflag,
247 char **cause)
249 struct winlink *dstwl;
250 struct session_group *srcsg, *dstsg;
252 srcsg = session_group_contains(src);
253 dstsg = session_group_contains(dst);
254 if (src != dst && srcsg != NULL && dstsg != NULL && srcsg == dstsg) {
255 xasprintf(cause, "sessions are grouped");
256 return (-1);
259 dstwl = NULL;
260 if (dstidx != -1)
261 dstwl = winlink_find_by_index(&dst->windows, dstidx);
262 if (dstwl != NULL) {
263 if (dstwl->window == srcwl->window) {
264 xasprintf(cause, "same index: %d", dstidx);
265 return (-1);
267 if (killflag) {
269 * Can't use session_detach as it will destroy session
270 * if this makes it empty.
272 notify_session_window("window-unlinked", dst,
273 dstwl->window);
274 dstwl->flags &= ~WINLINK_ALERTFLAGS;
275 winlink_stack_remove(&dst->lastw, dstwl);
276 winlink_remove(&dst->windows, dstwl);
278 /* Force select/redraw if current. */
279 if (dstwl == dst->curw) {
280 selectflag = 1;
281 dst->curw = NULL;
286 if (dstidx == -1)
287 dstidx = -1 - options_get_number(dst->options, "base-index");
288 dstwl = session_attach(dst, srcwl->window, dstidx, cause);
289 if (dstwl == NULL)
290 return (-1);
292 if (selectflag)
293 session_select(dst, dstwl->idx);
294 server_redraw_session_group(dst);
296 return (0);
299 void
300 server_unlink_window(struct session *s, struct winlink *wl)
302 if (session_detach(s, wl))
303 server_destroy_session_group(s);
304 else
305 server_redraw_session_group(s);
308 void
309 server_destroy_pane(struct window_pane *wp, int notify)
311 struct window *w = wp->window;
312 struct screen_write_ctx ctx;
313 struct grid_cell gc;
314 int remain_on_exit;
315 const char *s;
316 char *expanded;
317 u_int sx = screen_size_x(&wp->base);
318 u_int sy = screen_size_y(&wp->base);
320 if (wp->fd != -1) {
321 bufferevent_free(wp->event);
322 wp->event = NULL;
323 close(wp->fd);
324 wp->fd = -1;
327 remain_on_exit = options_get_number(wp->options, "remain-on-exit");
328 if (remain_on_exit != 0 && (~wp->flags & PANE_STATUSREADY))
329 return;
330 switch (remain_on_exit) {
331 case 0:
332 break;
333 case 2:
334 if (WIFEXITED(wp->status) && WEXITSTATUS(wp->status) == 0)
335 break;
336 /* FALLTHROUGH */
337 case 1:
338 if (wp->flags & PANE_STATUSDRAWN)
339 return;
340 wp->flags |= PANE_STATUSDRAWN;
342 gettimeofday(&wp->dead_time, NULL);
343 if (notify)
344 notify_pane("pane-died", wp);
346 s = options_get_string(wp->options, "remain-on-exit-format");
347 if (*s != '\0') {
348 screen_write_start_pane(&ctx, wp, &wp->base);
349 screen_write_scrollregion(&ctx, 0, sy - 1);
350 screen_write_cursormove(&ctx, 0, sy - 1, 0);
351 screen_write_linefeed(&ctx, 1, 8);
352 memcpy(&gc, &grid_default_cell, sizeof gc);
354 expanded = format_single(NULL, s, NULL, NULL, NULL, wp);
355 format_draw(&ctx, &gc, sx, expanded, NULL, 0);
356 free(expanded);
358 screen_write_stop(&ctx);
360 wp->base.mode &= ~MODE_CURSOR;
362 wp->flags |= PANE_REDRAW;
363 return;
366 if (notify)
367 notify_pane("pane-exited", wp);
369 server_unzoom_window(w);
370 server_client_remove_pane(wp);
371 layout_close_pane(wp);
372 window_remove_pane(w, wp);
374 if (TAILQ_EMPTY(&w->panes))
375 server_kill_window(w, 1);
376 else
377 server_redraw_window(w);
380 static void
381 server_destroy_session_group(struct session *s)
383 struct session_group *sg;
384 struct session *s1;
386 if ((sg = session_group_contains(s)) == NULL) {
387 server_destroy_session(s);
388 session_destroy(s, 1, __func__);
389 } else {
390 TAILQ_FOREACH_SAFE(s, &sg->sessions, gentry, s1) {
391 server_destroy_session(s);
392 session_destroy(s, 1, __func__);
397 static struct session *
398 server_find_session(struct session *s,
399 int (*f)(struct session *, struct session *))
401 struct session *s_loop, *s_out = NULL;
403 RB_FOREACH(s_loop, sessions, &sessions) {
404 if (s_loop != s && (s_out == NULL || f(s_loop, s_out)))
405 s_out = s_loop;
407 return (s_out);
410 static int
411 server_newer_session(struct session *s_loop, struct session *s_out)
413 return (timercmp(&s_loop->activity_time, &s_out->activity_time, <));
416 static int
417 server_newer_detached_session(struct session *s_loop, struct session *s_out)
419 if (s_loop->attached)
420 return (0);
421 return (server_newer_session(s_loop, s_out));
424 void
425 server_destroy_session(struct session *s)
427 struct client *c;
428 struct session *s_new = NULL;
429 int detach_on_destroy;
431 detach_on_destroy = options_get_number(s->options, "detach-on-destroy");
432 if (detach_on_destroy == 0)
433 s_new = server_find_session(s, server_newer_session);
434 else if (detach_on_destroy == 2)
435 s_new = server_find_session(s, server_newer_detached_session);
436 else if (detach_on_destroy == 3)
437 s_new = session_previous_session(s);
438 else if (detach_on_destroy == 4)
439 s_new = session_next_session(s);
440 if (s_new == s)
441 s_new = NULL;
442 TAILQ_FOREACH(c, &clients, entry) {
443 if (c->session != s)
444 continue;
445 c->session = NULL;
446 c->last_session = NULL;
447 server_client_set_session(c, s_new);
448 if (s_new == NULL)
449 c->flags |= CLIENT_EXIT;
451 recalculate_sizes();
454 void
455 server_check_unattached(void)
457 struct session *s;
460 * If any sessions are no longer attached and have destroy-unattached
461 * set, collect them.
463 RB_FOREACH(s, sessions, &sessions) {
464 if (s->attached != 0)
465 continue;
466 if (options_get_number (s->options, "destroy-unattached"))
467 session_destroy(s, 1, __func__);
471 void
472 server_unzoom_window(struct window *w)
474 if (window_unzoom(w) == 0)
475 server_redraw_window(w);