Mention XParseColor(3) for the cursor colour escape sequence.
[tmux-openbsd.git] / server-fn.c
blobb29bc15c9621b3fdf39477c027cb11d76e1ab34c
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 struct session *server_next_session(struct session *);
33 static void server_destroy_session_group(struct session *);
35 void
36 server_redraw_client(struct client *c)
38 c->flags |= CLIENT_ALLREDRAWFLAGS;
41 void
42 server_status_client(struct client *c)
44 c->flags |= CLIENT_REDRAWSTATUS;
47 void
48 server_redraw_session(struct session *s)
50 struct client *c;
52 TAILQ_FOREACH(c, &clients, entry) {
53 if (c->session == s)
54 server_redraw_client(c);
58 void
59 server_redraw_session_group(struct session *s)
61 struct session_group *sg;
63 if ((sg = session_group_contains(s)) == NULL)
64 server_redraw_session(s);
65 else {
66 TAILQ_FOREACH(s, &sg->sessions, gentry)
67 server_redraw_session(s);
71 void
72 server_status_session(struct session *s)
74 struct client *c;
76 TAILQ_FOREACH(c, &clients, entry) {
77 if (c->session == s)
78 server_status_client(c);
82 void
83 server_status_session_group(struct session *s)
85 struct session_group *sg;
87 if ((sg = session_group_contains(s)) == NULL)
88 server_status_session(s);
89 else {
90 TAILQ_FOREACH(s, &sg->sessions, gentry)
91 server_status_session(s);
95 void
96 server_redraw_window(struct window *w)
98 struct client *c;
100 TAILQ_FOREACH(c, &clients, entry) {
101 if (c->session != NULL && c->session->curw->window == w)
102 server_redraw_client(c);
106 void
107 server_redraw_window_borders(struct window *w)
109 struct client *c;
111 TAILQ_FOREACH(c, &clients, entry) {
112 if (c->session != NULL && c->session->curw->window == w)
113 c->flags |= CLIENT_REDRAWBORDERS;
117 void
118 server_status_window(struct window *w)
120 struct session *s;
123 * This is slightly different. We want to redraw the status line of any
124 * clients containing this window rather than anywhere it is the
125 * current window.
128 RB_FOREACH(s, sessions, &sessions) {
129 if (session_has(s, w))
130 server_status_session(s);
134 void
135 server_lock(void)
137 struct client *c;
139 TAILQ_FOREACH(c, &clients, entry) {
140 if (c->session != NULL)
141 server_lock_client(c);
145 void
146 server_lock_session(struct session *s)
148 struct client *c;
150 TAILQ_FOREACH(c, &clients, entry) {
151 if (c->session == s)
152 server_lock_client(c);
156 void
157 server_lock_client(struct client *c)
159 const char *cmd;
161 if (c->flags & CLIENT_CONTROL)
162 return;
164 if (c->flags & CLIENT_SUSPENDED)
165 return;
167 cmd = options_get_string(c->session->options, "lock-command");
168 if (*cmd == '\0' || strlen(cmd) + 1 > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
169 return;
171 tty_stop_tty(&c->tty);
172 tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_SMCUP));
173 tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_CLEAR));
174 tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_E3));
176 c->flags |= CLIENT_SUSPENDED;
177 proc_send(c->peer, MSG_LOCK, -1, cmd, strlen(cmd) + 1);
180 void
181 server_kill_pane(struct window_pane *wp)
183 struct window *w = wp->window;
185 if (window_count_panes(w) == 1) {
186 server_kill_window(w, 1);
187 recalculate_sizes();
188 } else {
189 server_unzoom_window(w);
190 server_client_remove_pane(wp);
191 layout_close_pane(wp);
192 window_remove_pane(w, wp);
193 server_redraw_window(w);
197 void
198 server_kill_window(struct window *w, int renumber)
200 struct session *s, *s1;
201 struct winlink *wl;
203 RB_FOREACH_SAFE(s, sessions, &sessions, s1) {
204 if (!session_has(s, w))
205 continue;
207 server_unzoom_window(w);
208 while ((wl = winlink_find_by_window(&s->windows, w)) != NULL) {
209 if (session_detach(s, wl)) {
210 server_destroy_session_group(s);
211 break;
212 } else
213 server_redraw_session_group(s);
216 if (renumber)
217 server_renumber_session(s);
219 recalculate_sizes();
222 void
223 server_renumber_session(struct session *s)
225 struct session_group *sg;
227 if (options_get_number(s->options, "renumber-windows")) {
228 if ((sg = session_group_contains(s)) != NULL) {
229 TAILQ_FOREACH(s, &sg->sessions, gentry)
230 session_renumber_windows(s);
231 } else
232 session_renumber_windows(s);
236 void
237 server_renumber_all(void)
239 struct session *s;
241 RB_FOREACH(s, sessions, &sessions)
242 server_renumber_session(s);
246 server_link_window(struct session *src, struct winlink *srcwl,
247 struct session *dst, int dstidx, int killflag, int selectflag,
248 char **cause)
250 struct winlink *dstwl;
251 struct session_group *srcsg, *dstsg;
253 srcsg = session_group_contains(src);
254 dstsg = session_group_contains(dst);
255 if (src != dst && srcsg != NULL && dstsg != NULL && srcsg == dstsg) {
256 xasprintf(cause, "sessions are grouped");
257 return (-1);
260 dstwl = NULL;
261 if (dstidx != -1)
262 dstwl = winlink_find_by_index(&dst->windows, dstidx);
263 if (dstwl != NULL) {
264 if (dstwl->window == srcwl->window) {
265 xasprintf(cause, "same index: %d", dstidx);
266 return (-1);
268 if (killflag) {
270 * Can't use session_detach as it will destroy session
271 * if this makes it empty.
273 notify_session_window("window-unlinked", dst,
274 dstwl->window);
275 dstwl->flags &= ~WINLINK_ALERTFLAGS;
276 winlink_stack_remove(&dst->lastw, dstwl);
277 winlink_remove(&dst->windows, dstwl);
279 /* Force select/redraw if current. */
280 if (dstwl == dst->curw) {
281 selectflag = 1;
282 dst->curw = NULL;
287 if (dstidx == -1)
288 dstidx = -1 - options_get_number(dst->options, "base-index");
289 dstwl = session_attach(dst, srcwl->window, dstidx, cause);
290 if (dstwl == NULL)
291 return (-1);
293 if (selectflag)
294 session_select(dst, dstwl->idx);
295 server_redraw_session_group(dst);
297 return (0);
300 void
301 server_unlink_window(struct session *s, struct winlink *wl)
303 if (session_detach(s, wl))
304 server_destroy_session_group(s);
305 else
306 server_redraw_session_group(s);
309 void
310 server_destroy_pane(struct window_pane *wp, int notify)
312 struct window *w = wp->window;
313 struct screen_write_ctx ctx;
314 struct grid_cell gc;
315 time_t t;
316 char tim[26];
317 int remain_on_exit;
319 if (wp->fd != -1) {
320 bufferevent_free(wp->event);
321 wp->event = NULL;
322 close(wp->fd);
323 wp->fd = -1;
326 remain_on_exit = options_get_number(wp->options, "remain-on-exit");
327 if (remain_on_exit != 0 && (~wp->flags & PANE_STATUSREADY))
328 return;
329 switch (remain_on_exit) {
330 case 0:
331 break;
332 case 2:
333 if (WIFEXITED(wp->status) && WEXITSTATUS(wp->status) == 0)
334 break;
335 /* FALLTHROUGH */
336 case 1:
337 if (wp->flags & PANE_STATUSDRAWN)
338 return;
339 wp->flags |= PANE_STATUSDRAWN;
341 if (notify)
342 notify_pane("pane-died", wp);
344 screen_write_start_pane(&ctx, wp, &wp->base);
345 screen_write_scrollregion(&ctx, 0, screen_size_y(ctx.s) - 1);
346 screen_write_cursormove(&ctx, 0, screen_size_y(ctx.s) - 1, 0);
347 screen_write_linefeed(&ctx, 1, 8);
348 memcpy(&gc, &grid_default_cell, sizeof gc);
350 time(&t);
351 ctime_r(&t, tim);
352 tim[strcspn(tim, "\n")] = '\0';
354 if (WIFEXITED(wp->status)) {
355 screen_write_nputs(&ctx, -1, &gc,
356 "Pane is dead (status %d, %s)",
357 WEXITSTATUS(wp->status),
358 tim);
359 } else if (WIFSIGNALED(wp->status)) {
360 screen_write_nputs(&ctx, -1, &gc,
361 "Pane is dead (signal %s, %s)",
362 sig2name(WTERMSIG(wp->status)),
363 tim);
366 screen_write_stop(&ctx);
367 wp->flags |= PANE_REDRAW;
368 return;
371 if (notify)
372 notify_pane("pane-exited", wp);
374 server_unzoom_window(w);
375 server_client_remove_pane(wp);
376 layout_close_pane(wp);
377 window_remove_pane(w, wp);
379 if (TAILQ_EMPTY(&w->panes))
380 server_kill_window(w, 1);
381 else
382 server_redraw_window(w);
385 static void
386 server_destroy_session_group(struct session *s)
388 struct session_group *sg;
389 struct session *s1;
391 if ((sg = session_group_contains(s)) == NULL)
392 server_destroy_session(s);
393 else {
394 TAILQ_FOREACH_SAFE(s, &sg->sessions, gentry, s1) {
395 server_destroy_session(s);
396 session_destroy(s, 1, __func__);
401 static struct session *
402 server_next_session(struct session *s)
404 struct session *s_loop, *s_out = NULL;
406 RB_FOREACH(s_loop, sessions, &sessions) {
407 if (s_loop == s)
408 continue;
409 if (s_out == NULL ||
410 timercmp(&s_loop->activity_time, &s_out->activity_time, <))
411 s_out = s_loop;
413 return (s_out);
416 static struct session *
417 server_next_detached_session(struct session *s)
419 struct session *s_loop, *s_out = NULL;
421 RB_FOREACH(s_loop, sessions, &sessions) {
422 if (s_loop == s || s_loop->attached)
423 continue;
424 if (s_out == NULL ||
425 timercmp(&s_loop->activity_time, &s_out->activity_time, <))
426 s_out = s_loop;
428 return (s_out);
431 void
432 server_destroy_session(struct session *s)
434 struct client *c;
435 struct session *s_new;
436 int detach_on_destroy;
438 detach_on_destroy = options_get_number(s->options, "detach-on-destroy");
439 if (detach_on_destroy == 0)
440 s_new = server_next_session(s);
441 else if (detach_on_destroy == 2)
442 s_new = server_next_detached_session(s);
443 else
444 s_new = NULL;
445 TAILQ_FOREACH(c, &clients, entry) {
446 if (c->session != s)
447 continue;
448 server_client_set_session(c, s_new);
449 if (s_new == NULL)
450 c->flags |= CLIENT_EXIT;
452 recalculate_sizes();
455 void
456 server_check_unattached(void)
458 struct session *s;
461 * If any sessions are no longer attached and have destroy-unattached
462 * set, collect them.
464 RB_FOREACH(s, sessions, &sessions) {
465 if (s->attached != 0)
466 continue;
467 if (options_get_number (s->options, "destroy-unattached"))
468 session_destroy(s, 1, __func__);
472 void
473 server_unzoom_window(struct window *w)
475 if (window_unzoom(w) == 0)
476 server_redraw_window(w);