RIS should reset focus reporting, from Hayaki Saito.
[tmux-openbsd.git] / server-fn.c
blobd92754e1015a08e1f69335d4ace390bcf21a60a8
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
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>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <time.h>
24 #include <unistd.h>
26 #include "tmux.h"
28 struct session *server_next_session(struct session *);
29 void server_callback_identify(int, short, void *);
31 void
32 server_fill_environ(struct session *s, struct environ *env)
34 char var[MAXPATHLEN], *term;
35 u_int idx;
36 long pid;
38 if (s != NULL) {
39 term = options_get_string(&s->options, "default-terminal");
40 environ_set(env, "TERM", term);
42 idx = s->id;
43 } else
44 idx = -1;
45 pid = getpid();
46 xsnprintf(var, sizeof var, "%s,%ld,%d", socket_path, pid, idx);
47 environ_set(env, "TMUX", var);
50 void
51 server_write_ready(struct client *c)
53 if (c->flags & CLIENT_CONTROL)
54 return;
55 server_write_client(c, MSG_READY, NULL, 0);
58 int
59 server_write_client(
60 struct client *c, enum msgtype type, const void *buf, size_t len)
62 struct imsgbuf *ibuf = &c->ibuf;
63 int error;
65 if (c->flags & CLIENT_BAD)
66 return (-1);
67 log_debug("writing %d to client %d", type, c->ibuf.fd);
68 error = imsg_compose(ibuf, type, PROTOCOL_VERSION, -1, -1,
69 (void *) buf, len);
70 if (error == 1)
71 server_update_event(c);
72 return (error == 1 ? 0 : -1);
75 void
76 server_write_session(
77 struct session *s, enum msgtype type, const void *buf, size_t len)
79 struct client *c;
80 u_int i;
82 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
83 c = ARRAY_ITEM(&clients, i);
84 if (c == NULL || c->session == NULL)
85 continue;
86 if (c->session == s)
87 server_write_client(c, type, buf, len);
91 void
92 server_redraw_client(struct client *c)
94 c->flags |= CLIENT_REDRAW;
97 void
98 server_status_client(struct client *c)
100 c->flags |= CLIENT_STATUS;
103 void
104 server_redraw_session(struct session *s)
106 struct client *c;
107 u_int i;
109 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
110 c = ARRAY_ITEM(&clients, i);
111 if (c == NULL || c->session == NULL)
112 continue;
113 if (c->session == s)
114 server_redraw_client(c);
118 void
119 server_redraw_session_group(struct session *s)
121 struct session_group *sg;
123 if ((sg = session_group_find(s)) == NULL)
124 server_redraw_session(s);
125 else {
126 TAILQ_FOREACH(s, &sg->sessions, gentry)
127 server_redraw_session(s);
131 void
132 server_status_session(struct session *s)
134 struct client *c;
135 u_int i;
137 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
138 c = ARRAY_ITEM(&clients, i);
139 if (c == NULL || c->session == NULL)
140 continue;
141 if (c->session == s)
142 server_status_client(c);
146 void
147 server_status_session_group(struct session *s)
149 struct session_group *sg;
151 if ((sg = session_group_find(s)) == NULL)
152 server_status_session(s);
153 else {
154 TAILQ_FOREACH(s, &sg->sessions, gentry)
155 server_status_session(s);
159 void
160 server_redraw_window(struct window *w)
162 struct client *c;
163 u_int i;
165 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
166 c = ARRAY_ITEM(&clients, i);
167 if (c == NULL || c->session == NULL)
168 continue;
169 if (c->session->curw->window == w)
170 server_redraw_client(c);
172 w->flags |= WINDOW_REDRAW;
175 void
176 server_redraw_window_borders(struct window *w)
178 struct client *c;
179 u_int i;
181 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
182 c = ARRAY_ITEM(&clients, i);
183 if (c == NULL || c->session == NULL)
184 continue;
185 if (c->session->curw->window == w)
186 c->flags |= CLIENT_BORDERS;
190 void
191 server_status_window(struct window *w)
193 struct session *s;
196 * This is slightly different. We want to redraw the status line of any
197 * clients containing this window rather than anywhere it is the
198 * current window.
201 RB_FOREACH(s, sessions, &sessions) {
202 if (session_has(s, w) != NULL)
203 server_status_session(s);
207 void
208 server_lock(void)
210 struct client *c;
211 u_int i;
213 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
214 c = ARRAY_ITEM(&clients, i);
215 if (c == NULL || c->session == NULL)
216 continue;
217 server_lock_client(c);
221 void
222 server_lock_session(struct session *s)
224 struct client *c;
225 u_int i;
227 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
228 c = ARRAY_ITEM(&clients, i);
229 if (c == NULL || c->session == NULL || c->session != s)
230 continue;
231 server_lock_client(c);
235 void
236 server_lock_client(struct client *c)
238 const char *cmd;
239 size_t cmdlen;
240 struct msg_lock_data lockdata;
242 if (c->flags & CLIENT_CONTROL)
243 return;
245 if (c->flags & CLIENT_SUSPENDED)
246 return;
248 cmd = options_get_string(&c->session->options, "lock-command");
249 cmdlen = strlcpy(lockdata.cmd, cmd, sizeof lockdata.cmd);
250 if (cmdlen >= sizeof lockdata.cmd)
251 return;
253 tty_stop_tty(&c->tty);
254 tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_SMCUP));
255 tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_CLEAR));
256 tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_E3));
258 c->flags |= CLIENT_SUSPENDED;
259 server_write_client(c, MSG_LOCK, &lockdata, sizeof lockdata);
262 void
263 server_kill_window(struct window *w)
265 struct session *s, *next_s;
266 struct winlink *wl;
268 next_s = RB_MIN(sessions, &sessions);
269 while (next_s != NULL) {
270 s = next_s;
271 next_s = RB_NEXT(sessions, &sessions, s);
273 if (session_has(s, w) == NULL)
274 continue;
275 while ((wl = winlink_find_by_window(&s->windows, w)) != NULL) {
276 if (session_detach(s, wl)) {
277 server_destroy_session_group(s);
278 break;
279 } else
280 server_redraw_session_group(s);
283 if (options_get_number(&s->options, "renumber-windows"))
284 session_renumber_windows(s);
286 recalculate_sizes();
290 server_link_window(struct session *src, struct winlink *srcwl,
291 struct session *dst, int dstidx, int killflag, int selectflag, char **cause)
293 struct winlink *dstwl;
294 struct session_group *srcsg, *dstsg;
296 srcsg = session_group_find(src);
297 dstsg = session_group_find(dst);
298 if (src != dst && srcsg != NULL && dstsg != NULL && srcsg == dstsg) {
299 xasprintf(cause, "sessions are grouped");
300 return (-1);
303 dstwl = NULL;
304 if (dstidx != -1)
305 dstwl = winlink_find_by_index(&dst->windows, dstidx);
306 if (dstwl != NULL) {
307 if (dstwl->window == srcwl->window) {
308 xasprintf(cause, "same index: %d", dstidx);
309 return (-1);
311 if (killflag) {
313 * Can't use session_detach as it will destroy session
314 * if this makes it empty.
316 notify_window_unlinked(dst, dstwl->window);
317 dstwl->flags &= ~WINLINK_ALERTFLAGS;
318 winlink_stack_remove(&dst->lastw, dstwl);
319 winlink_remove(&dst->windows, dstwl);
321 /* Force select/redraw if current. */
322 if (dstwl == dst->curw) {
323 selectflag = 1;
324 dst->curw = NULL;
329 if (dstidx == -1)
330 dstidx = -1 - options_get_number(&dst->options, "base-index");
331 dstwl = session_attach(dst, srcwl->window, dstidx, cause);
332 if (dstwl == NULL)
333 return (-1);
335 if (selectflag)
336 session_select(dst, dstwl->idx);
337 server_redraw_session_group(dst);
339 return (0);
342 void
343 server_unlink_window(struct session *s, struct winlink *wl)
345 if (session_detach(s, wl))
346 server_destroy_session_group(s);
347 else
348 server_redraw_session_group(s);
351 void
352 server_destroy_pane(struct window_pane *wp)
354 struct window *w = wp->window;
355 int old_fd;
356 struct screen_write_ctx ctx;
357 struct grid_cell gc;
359 old_fd = wp->fd;
360 if (wp->fd != -1) {
361 bufferevent_free(wp->event);
362 close(wp->fd);
363 wp->fd = -1;
366 if (options_get_number(&w->options, "remain-on-exit")) {
367 if (old_fd == -1)
368 return;
369 screen_write_start(&ctx, wp, &wp->base);
370 screen_write_scrollregion(&ctx, 0, screen_size_y(ctx.s) - 1);
371 screen_write_cursormove(&ctx, 0, screen_size_y(ctx.s) - 1);
372 screen_write_linefeed(&ctx, 1);
373 memcpy(&gc, &grid_default_cell, sizeof gc);
374 gc.attr |= GRID_ATTR_BRIGHT;
375 screen_write_puts(&ctx, &gc, "Pane is dead");
376 screen_write_stop(&ctx);
377 wp->flags |= PANE_REDRAW;
378 return;
381 server_unzoom_window(w);
382 layout_close_pane(wp);
383 window_remove_pane(w, wp);
385 if (TAILQ_EMPTY(&w->panes))
386 server_kill_window(w);
387 else
388 server_redraw_window(w);
391 void
392 server_destroy_session_group(struct session *s)
394 struct session_group *sg;
396 if ((sg = session_group_find(s)) == NULL)
397 server_destroy_session(s);
398 else {
399 TAILQ_FOREACH(s, &sg->sessions, gentry)
400 server_destroy_session(s);
401 TAILQ_REMOVE(&session_groups, sg, entry);
402 free(sg);
406 struct session *
407 server_next_session(struct session *s)
409 struct session *s_loop, *s_out;
411 s_out = NULL;
412 RB_FOREACH(s_loop, sessions, &sessions) {
413 if (s_loop == s)
414 continue;
415 if (s_out == NULL ||
416 timercmp(&s_loop->activity_time, &s_out->activity_time, <))
417 s_out = s_loop;
419 return (s_out);
422 void
423 server_destroy_session(struct session *s)
425 struct client *c;
426 struct session *s_new;
427 u_int i;
429 if (!options_get_number(&s->options, "detach-on-destroy"))
430 s_new = server_next_session(s);
431 else
432 s_new = NULL;
434 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
435 c = ARRAY_ITEM(&clients, i);
436 if (c == NULL || c->session != s)
437 continue;
438 if (s_new == NULL) {
439 c->session = NULL;
440 c->flags |= CLIENT_EXIT;
441 } else {
442 c->last_session = NULL;
443 c->session = s_new;
444 notify_attached_session_changed(c);
445 session_update_activity(s_new);
446 server_redraw_client(c);
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->flags & SESSION_UNATTACHED))
463 continue;
464 if (options_get_number (&s->options, "destroy-unattached"))
465 session_destroy(s);
469 void
470 server_set_identify(struct client *c)
472 struct timeval tv;
473 int delay;
475 delay = options_get_number(&c->session->options, "display-panes-time");
476 tv.tv_sec = delay / 1000;
477 tv.tv_usec = (delay % 1000) * 1000L;
479 if (event_initialized (&c->identify_timer))
480 evtimer_del(&c->identify_timer);
481 evtimer_set(&c->identify_timer, server_callback_identify, c);
482 evtimer_add(&c->identify_timer, &tv);
484 c->flags |= CLIENT_IDENTIFY;
485 c->tty.flags |= (TTY_FREEZE|TTY_NOCURSOR);
486 server_redraw_client(c);
489 void
490 server_clear_identify(struct client *c)
492 if (c->flags & CLIENT_IDENTIFY) {
493 c->flags &= ~CLIENT_IDENTIFY;
494 c->tty.flags &= ~(TTY_FREEZE|TTY_NOCURSOR);
495 server_redraw_client(c);
499 void
500 server_callback_identify(unused int fd, unused short events, void *data)
502 struct client *c = data;
504 server_clear_identify(c);
507 void
508 server_update_event(struct client *c)
510 short events;
512 events = 0;
513 if (!(c->flags & CLIENT_BAD))
514 events |= EV_READ;
515 if (c->ibuf.w.queued > 0)
516 events |= EV_WRITE;
517 if (event_initialized(&c->event))
518 event_del(&c->event);
519 event_set(&c->event, c->ibuf.fd, events, server_client_callback, c);
520 event_add(&c->event, NULL);
523 /* Push stdout to client if possible. */
524 void
525 server_push_stdout(struct client *c)
527 struct msg_stdout_data data;
528 size_t size;
530 size = EVBUFFER_LENGTH(c->stdout_data);
531 if (size == 0)
532 return;
533 if (size > sizeof data.data)
534 size = sizeof data.data;
536 memcpy(data.data, EVBUFFER_DATA(c->stdout_data), size);
537 data.size = size;
539 if (server_write_client(c, MSG_STDOUT, &data, sizeof data) == 0)
540 evbuffer_drain(c->stdout_data, size);
543 /* Push stderr to client if possible. */
544 void
545 server_push_stderr(struct client *c)
547 struct msg_stderr_data data;
548 size_t size;
550 if (c->stderr_data == c->stdout_data) {
551 server_push_stdout(c);
552 return;
554 size = EVBUFFER_LENGTH(c->stderr_data);
555 if (size == 0)
556 return;
557 if (size > sizeof data.data)
558 size = sizeof data.data;
560 memcpy(data.data, EVBUFFER_DATA(c->stderr_data), size);
561 data.size = size;
563 if (server_write_client(c, MSG_STDERR, &data, sizeof data) == 0)
564 evbuffer_drain(c->stderr_data, size);
567 /* Set stdin callback. */
569 server_set_stdin_callback(struct client *c, void (*cb)(struct client *, int,
570 void *), void *cb_data, char **cause)
572 if (c == NULL || c->session != NULL) {
573 *cause = xstrdup("no client with stdin");
574 return (-1);
576 if (c->flags & CLIENT_TERMINAL) {
577 *cause = xstrdup("stdin is a tty");
578 return (-1);
580 if (c->stdin_callback != NULL) {
581 *cause = xstrdup("stdin in use");
582 return (-1);
585 c->stdin_callback_data = cb_data;
586 c->stdin_callback = cb;
588 c->references++;
590 if (c->stdin_closed)
591 c->stdin_callback (c, 1, c->stdin_callback_data);
593 server_write_client(c, MSG_STDIN, NULL, 0);
595 return (0);
598 void
599 server_unzoom_window(struct window *w)
601 window_unzoom(w);
602 server_redraw_window(w);
603 server_status_window(w);