Annotation of src/usr.bin/tmux/server-fn.c, Revision 1.93
1.93 ! nicm 1: /* $OpenBSD: server-fn.c,v 1.92 2015/11/14 09:41:07 nicm Exp $ */
1.1 nicm 2:
3: /*
4: * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
5: *
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.
9: *
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.
17: */
18:
19: #include <sys/types.h>
1.90 nicm 20: #include <sys/queue.h>
21: #include <sys/uio.h>
1.1 nicm 22:
1.90 nicm 23: #include <imsg.h>
1.60 nicm 24: #include <stdlib.h>
1.1 nicm 25: #include <string.h>
1.5 nicm 26: #include <time.h>
1.1 nicm 27: #include <unistd.h>
28:
29: #include "tmux.h"
30:
1.39 nicm 31: struct session *server_next_session(struct session *);
32: void server_callback_identify(int, short, void *);
1.28 nicm 33:
1.13 nicm 34: void
35: server_fill_environ(struct session *s, struct environ *env)
1.1 nicm 36: {
1.77 nicm 37: char var[PATH_MAX], *term;
1.50 nicm 38: u_int idx;
39: long pid;
40:
41: if (s != NULL) {
1.91 nicm 42: term = options_get_string(global_options, "default-terminal");
1.50 nicm 43: environ_set(env, "TERM", term);
44:
1.67 nicm 45: idx = s->id;
1.50 nicm 46: } else
1.79 nicm 47: idx = (u_int)-1;
1.50 nicm 48: pid = getpid();
1.79 nicm 49: xsnprintf(var, sizeof var, "%s,%ld,%u", socket_path, pid, idx);
1.50 nicm 50: environ_set(env, "TMUX", var);
1.58 nicm 51: }
52:
53: void
1.1 nicm 54: server_redraw_client(struct client *c)
55: {
56: c->flags |= CLIENT_REDRAW;
57: }
58:
59: void
60: server_status_client(struct client *c)
61: {
62: c->flags |= CLIENT_STATUS;
63: }
64:
65: void
66: server_redraw_session(struct session *s)
67: {
68: struct client *c;
69:
1.84 nicm 70: TAILQ_FOREACH(c, &clients, entry) {
1.1 nicm 71: if (c->session == s)
72: server_redraw_client(c);
73: }
74: }
75:
76: void
1.25 nicm 77: server_redraw_session_group(struct session *s)
78: {
79: struct session_group *sg;
80:
81: if ((sg = session_group_find(s)) == NULL)
82: server_redraw_session(s);
83: else {
84: TAILQ_FOREACH(s, &sg->sessions, gentry)
85: server_redraw_session(s);
86: }
87: }
88:
89: void
1.1 nicm 90: server_status_session(struct session *s)
91: {
92: struct client *c;
93:
1.84 nicm 94: TAILQ_FOREACH(c, &clients, entry) {
1.1 nicm 95: if (c->session == s)
96: server_status_client(c);
97: }
98: }
99:
100: void
1.25 nicm 101: server_status_session_group(struct session *s)
102: {
103: struct session_group *sg;
104:
105: if ((sg = session_group_find(s)) == NULL)
106: server_status_session(s);
107: else {
108: TAILQ_FOREACH(s, &sg->sessions, gentry)
109: server_status_session(s);
110: }
111: }
112:
113: void
1.1 nicm 114: server_redraw_window(struct window *w)
115: {
116: struct client *c;
117:
1.84 nicm 118: TAILQ_FOREACH(c, &clients, entry) {
119: if (c->session != NULL && c->session->curw->window == w)
1.1 nicm 120: server_redraw_client(c);
121: }
122: w->flags |= WINDOW_REDRAW;
1.33 nicm 123: }
124:
125: void
126: server_redraw_window_borders(struct window *w)
127: {
128: struct client *c;
129:
1.84 nicm 130: TAILQ_FOREACH(c, &clients, entry) {
131: if (c->session != NULL && c->session->curw->window == w)
1.33 nicm 132: c->flags |= CLIENT_BORDERS;
133: }
1.1 nicm 134: }
135:
136: void
137: server_status_window(struct window *w)
138: {
139: struct session *s;
140:
141: /*
142: * This is slightly different. We want to redraw the status line of any
1.65 nicm 143: * clients containing this window rather than anywhere it is the
1.1 nicm 144: * current window.
145: */
146:
1.47 nicm 147: RB_FOREACH(s, sessions, &sessions) {
1.83 nicm 148: if (session_has(s, w))
1.1 nicm 149: server_status_session(s);
150: }
151: }
152:
153: void
154: server_lock(void)
155: {
1.23 nicm 156: struct client *c;
1.17 nicm 157:
1.84 nicm 158: TAILQ_FOREACH(c, &clients, entry) {
159: if (c->session != NULL)
160: server_lock_client(c);
1.23 nicm 161: }
162: }
1.1 nicm 163:
1.23 nicm 164: void
165: server_lock_session(struct session *s)
166: {
167: struct client *c;
168:
1.84 nicm 169: TAILQ_FOREACH(c, &clients, entry) {
170: if (c->session == s)
171: server_lock_client(c);
1.31 nicm 172: }
1.23 nicm 173: }
1.1 nicm 174:
1.23 nicm 175: void
176: server_lock_client(struct client *c)
177: {
1.72 nicm 178: const char *cmd;
1.62 nicm 179:
1.64 nicm 180: if (c->flags & CLIENT_CONTROL)
1.62 nicm 181: return;
1.24 nicm 182:
183: if (c->flags & CLIENT_SUSPENDED)
184: return;
1.1 nicm 185:
1.91 nicm 186: cmd = options_get_string(c->session->options, "lock-command");
1.72 nicm 187: if (strlen(cmd) + 1 > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
1.23 nicm 188: return;
1.31 nicm 189:
1.23 nicm 190: tty_stop_tty(&c->tty);
191: tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_SMCUP));
192: tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_CLEAR));
1.52 nicm 193: tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_E3));
1.23 nicm 194:
195: c->flags |= CLIENT_SUSPENDED;
1.90 nicm 196: proc_send_s(c->peer, MSG_LOCK, cmd);
1.8 nicm 197: }
198:
199: void
200: server_kill_window(struct window *w)
201: {
1.70 nicm 202: struct session *s, *next_s, *target_s;
203: struct session_group *sg;
204: struct winlink *wl;
1.31 nicm 205:
1.48 nicm 206: next_s = RB_MIN(sessions, &sessions);
207: while (next_s != NULL) {
208: s = next_s;
209: next_s = RB_NEXT(sessions, &sessions, s);
210:
1.83 nicm 211: if (!session_has(s, w))
1.8 nicm 212: continue;
1.78 nicm 213: server_unzoom_window(w);
1.34 nicm 214: while ((wl = winlink_find_by_window(&s->windows, w)) != NULL) {
215: if (session_detach(s, wl)) {
216: server_destroy_session_group(s);
217: break;
218: } else
219: server_redraw_session_group(s);
220: }
1.56 nicm 221:
1.91 nicm 222: if (options_get_number(s->options, "renumber-windows")) {
1.70 nicm 223: if ((sg = session_group_find(s)) != NULL) {
224: TAILQ_FOREACH(target_s, &sg->sessions, gentry)
225: session_renumber_windows(target_s);
226: } else
227: session_renumber_windows(s);
228: }
1.8 nicm 229: }
1.69 nicm 230: recalculate_sizes();
1.21 nicm 231: }
232:
233: int
1.25 nicm 234: server_link_window(struct session *src, struct winlink *srcwl,
1.76 nicm 235: struct session *dst, int dstidx, int killflag, int selectflag,
236: char **cause)
1.21 nicm 237: {
1.25 nicm 238: struct winlink *dstwl;
239: struct session_group *srcsg, *dstsg;
240:
241: srcsg = session_group_find(src);
242: dstsg = session_group_find(dst);
243: if (src != dst && srcsg != NULL && dstsg != NULL && srcsg == dstsg) {
244: xasprintf(cause, "sessions are grouped");
245: return (-1);
246: }
1.21 nicm 247:
248: dstwl = NULL;
249: if (dstidx != -1)
250: dstwl = winlink_find_by_index(&dst->windows, dstidx);
251: if (dstwl != NULL) {
1.44 nicm 252: if (dstwl->window == srcwl->window) {
253: xasprintf(cause, "same index: %d", dstidx);
1.41 nicm 254: return (-1);
1.44 nicm 255: }
1.21 nicm 256: if (killflag) {
257: /*
258: * Can't use session_detach as it will destroy session
259: * if this makes it empty.
260: */
1.55 nicm 261: notify_window_unlinked(dst, dstwl->window);
1.37 nicm 262: dstwl->flags &= ~WINLINK_ALERTFLAGS;
1.21 nicm 263: winlink_stack_remove(&dst->lastw, dstwl);
264: winlink_remove(&dst->windows, dstwl);
265:
266: /* Force select/redraw if current. */
1.26 nicm 267: if (dstwl == dst->curw) {
1.21 nicm 268: selectflag = 1;
1.26 nicm 269: dst->curw = NULL;
270: }
1.21 nicm 271: }
272: }
273:
274: if (dstidx == -1)
1.91 nicm 275: dstidx = -1 - options_get_number(dst->options, "base-index");
1.21 nicm 276: dstwl = session_attach(dst, srcwl->window, dstidx, cause);
277: if (dstwl == NULL)
278: return (-1);
279:
1.25 nicm 280: if (selectflag)
1.21 nicm 281: session_select(dst, dstwl->idx);
1.25 nicm 282: server_redraw_session_group(dst);
1.21 nicm 283:
284: return (0);
285: }
286:
287: void
288: server_unlink_window(struct session *s, struct winlink *wl)
289: {
290: if (session_detach(s, wl))
1.25 nicm 291: server_destroy_session_group(s);
292: else
293: server_redraw_session_group(s);
1.29 nicm 294: }
295:
296: void
297: server_destroy_pane(struct window_pane *wp)
298: {
1.51 nicm 299: struct window *w = wp->window;
300: int old_fd;
301: struct screen_write_ctx ctx;
302: struct grid_cell gc;
1.29 nicm 303:
1.51 nicm 304: old_fd = wp->fd;
1.36 nicm 305: if (wp->fd != -1) {
1.53 nicm 306: bufferevent_free(wp->event);
1.36 nicm 307: close(wp->fd);
308: wp->fd = -1;
309: }
1.29 nicm 310:
1.91 nicm 311: if (options_get_number(w->options, "remain-on-exit")) {
1.51 nicm 312: if (old_fd == -1)
313: return;
314: screen_write_start(&ctx, wp, &wp->base);
315: screen_write_scrollregion(&ctx, 0, screen_size_y(ctx.s) - 1);
316: screen_write_cursormove(&ctx, 0, screen_size_y(ctx.s) - 1);
317: screen_write_linefeed(&ctx, 1);
318: memcpy(&gc, &grid_default_cell, sizeof gc);
319: gc.attr |= GRID_ATTR_BRIGHT;
320: screen_write_puts(&ctx, &gc, "Pane is dead");
321: screen_write_stop(&ctx);
322: wp->flags |= PANE_REDRAW;
1.29 nicm 323: return;
1.51 nicm 324: }
1.29 nicm 325:
1.66 nicm 326: server_unzoom_window(w);
1.29 nicm 327: layout_close_pane(wp);
328: window_remove_pane(w, wp);
329:
330: if (TAILQ_EMPTY(&w->panes))
331: server_kill_window(w);
332: else
333: server_redraw_window(w);
1.25 nicm 334: }
335:
336: void
337: server_destroy_session_group(struct session *s)
338: {
339: struct session_group *sg;
1.71 nicm 340: struct session *s1;
1.25 nicm 341:
342: if ((sg = session_group_find(s)) == NULL)
1.21 nicm 343: server_destroy_session(s);
1.25 nicm 344: else {
1.71 nicm 345: TAILQ_FOREACH_SAFE(s, &sg->sessions, gentry, s1) {
1.25 nicm 346: server_destroy_session(s);
1.71 nicm 347: session_destroy(s);
348: }
1.25 nicm 349: }
1.19 nicm 350: }
351:
1.39 nicm 352: struct session *
353: server_next_session(struct session *s)
354: {
355: struct session *s_loop, *s_out;
356:
357: s_out = NULL;
1.47 nicm 358: RB_FOREACH(s_loop, sessions, &sessions) {
359: if (s_loop == s)
1.39 nicm 360: continue;
361: if (s_out == NULL ||
362: timercmp(&s_loop->activity_time, &s_out->activity_time, <))
363: s_out = s_loop;
364: }
365: return (s_out);
366: }
367:
1.19 nicm 368: void
369: server_destroy_session(struct session *s)
370: {
371: struct client *c;
1.39 nicm 372: struct session *s_new;
1.31 nicm 373:
1.91 nicm 374: if (!options_get_number(s->options, "detach-on-destroy"))
1.39 nicm 375: s_new = server_next_session(s);
376: else
377: s_new = NULL;
378:
1.84 nicm 379: TAILQ_FOREACH(c, &clients, entry) {
380: if (c->session != s)
1.19 nicm 381: continue;
1.39 nicm 382: if (s_new == NULL) {
383: c->session = NULL;
1.40 nicm 384: c->flags |= CLIENT_EXIT;
1.39 nicm 385: } else {
1.46 nicm 386: c->last_session = NULL;
1.39 nicm 387: c->session = s_new;
1.86 nicm 388: status_timer_start(c);
1.55 nicm 389: notify_attached_session_changed(c);
1.87 nicm 390: session_update_activity(s_new, NULL);
1.88 nicm 391: gettimeofday(&s_new->last_attached_time, NULL);
1.39 nicm 392: server_redraw_client(c);
393: }
1.19 nicm 394: }
1.38 nicm 395: recalculate_sizes();
1.42 nicm 396: }
397:
398: void
1.75 nicm 399: server_check_unattached(void)
1.42 nicm 400: {
401: struct session *s;
402:
403: /*
404: * If any sessions are no longer attached and have destroy-unattached
405: * set, collect them.
406: */
1.47 nicm 407: RB_FOREACH(s, sessions, &sessions) {
408: if (!(s->flags & SESSION_UNATTACHED))
1.42 nicm 409: continue;
1.91 nicm 410: if (options_get_number (s->options, "destroy-unattached"))
1.42 nicm 411: session_destroy(s);
412: }
1.15 nicm 413: }
414:
415: void
416: server_set_identify(struct client *c)
417: {
418: struct timeval tv;
419: int delay;
420:
1.91 nicm 421: delay = options_get_number(c->session->options, "display-panes-time");
1.15 nicm 422: tv.tv_sec = delay / 1000;
423: tv.tv_usec = (delay % 1000) * 1000L;
1.31 nicm 424:
1.75 nicm 425: if (event_initialized(&c->identify_timer))
1.54 nicm 426: evtimer_del(&c->identify_timer);
1.28 nicm 427: evtimer_set(&c->identify_timer, server_callback_identify, c);
428: evtimer_add(&c->identify_timer, &tv);
1.15 nicm 429:
430: c->flags |= CLIENT_IDENTIFY;
431: c->tty.flags |= (TTY_FREEZE|TTY_NOCURSOR);
432: server_redraw_client(c);
433: }
434:
435: void
436: server_clear_identify(struct client *c)
437: {
438: if (c->flags & CLIENT_IDENTIFY) {
439: c->flags &= ~CLIENT_IDENTIFY;
440: c->tty.flags &= ~(TTY_FREEZE|TTY_NOCURSOR);
441: server_redraw_client(c);
442: }
1.28 nicm 443: }
444:
445: void
1.93 ! nicm 446: server_callback_identify(__unused int fd, __unused short events, void *data)
1.28 nicm 447: {
448: struct client *c = data;
449:
450: server_clear_identify(c);
1.57 nicm 451: }
452:
453: /* Set stdin callback. */
454: int
455: server_set_stdin_callback(struct client *c, void (*cb)(struct client *, int,
456: void *), void *cb_data, char **cause)
457: {
1.65 nicm 458: if (c == NULL || c->session != NULL) {
1.57 nicm 459: *cause = xstrdup("no client with stdin");
460: return (-1);
461: }
462: if (c->flags & CLIENT_TERMINAL) {
463: *cause = xstrdup("stdin is a tty");
464: return (-1);
465: }
466: if (c->stdin_callback != NULL) {
467: *cause = xstrdup("stdin in use");
468: return (-1);
469: }
470:
471: c->stdin_callback_data = cb_data;
472: c->stdin_callback = cb;
473:
474: c->references++;
475:
476: if (c->stdin_closed)
1.75 nicm 477: c->stdin_callback(c, 1, c->stdin_callback_data);
1.61 nicm 478:
1.90 nicm 479: proc_send(c->peer, MSG_STDIN, -1, NULL, 0);
1.61 nicm 480:
1.57 nicm 481: return (0);
1.66 nicm 482: }
483:
484: void
485: server_unzoom_window(struct window *w)
486: {
1.80 nicm 487: if (window_unzoom(w) == 0) {
488: server_redraw_window(w);
489: server_status_window(w);
490: }
1.1 nicm 491: }