Annotation of src/usr.bin/tmux/server-fn.c, Revision 1.47
1.47 ! nicm 1: /* $OpenBSD: server-fn.c,v 1.46 2010/12/20 00:17:22 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>
20:
21: #include <string.h>
1.5 nicm 22: #include <time.h>
1.1 nicm 23: #include <unistd.h>
24:
25: #include "tmux.h"
26:
1.39 nicm 27: struct session *server_next_session(struct session *);
28: void server_callback_identify(int, short, void *);
1.28 nicm 29:
1.13 nicm 30: void
31: server_fill_environ(struct session *s, struct environ *env)
1.1 nicm 32: {
1.47 ! nicm 33: char tmuxvar[MAXPATHLEN], *term;
1.1 nicm 34:
35: xsnprintf(tmuxvar, sizeof tmuxvar,
1.47 ! nicm 36: "%s,%ld,%u", socket_path, (long) getpid(), s->idx);
1.13 nicm 37: environ_set(env, "TMUX", tmuxvar);
1.1 nicm 38:
1.13 nicm 39: term = options_get_string(&s->options, "default-terminal");
40: environ_set(env, "TERM", term);
1.10 nicm 41: }
42:
43: void
1.1 nicm 44: server_write_client(
1.11 nicm 45: struct client *c, enum msgtype type, const void *buf, size_t len)
1.1 nicm 46: {
1.14 nicm 47: struct imsgbuf *ibuf = &c->ibuf;
1.1 nicm 48:
1.12 nicm 49: if (c->flags & CLIENT_BAD)
50: return;
1.14 nicm 51: log_debug("writing %d to client %d", type, c->ibuf.fd);
52: imsg_compose(ibuf, type, PROTOCOL_VERSION, -1, -1, (void *) buf, len);
1.31 nicm 53: server_update_event(c);
1.1 nicm 54: }
55:
56: void
57: server_write_session(
1.11 nicm 58: struct session *s, enum msgtype type, const void *buf, size_t len)
1.1 nicm 59: {
60: struct client *c;
61: u_int i;
62:
63: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
64: c = ARRAY_ITEM(&clients, i);
65: if (c == NULL || c->session == NULL)
66: continue;
67: if (c->session == s)
68: server_write_client(c, type, buf, len);
69: }
70: }
71:
72: void
73: server_redraw_client(struct client *c)
74: {
75: c->flags |= CLIENT_REDRAW;
76: }
77:
78: void
79: server_status_client(struct client *c)
80: {
81: c->flags |= CLIENT_STATUS;
82: }
83:
84: void
85: server_redraw_session(struct session *s)
86: {
87: struct client *c;
88: u_int i;
89:
90: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
91: c = ARRAY_ITEM(&clients, i);
92: if (c == NULL || c->session == NULL)
93: continue;
94: if (c->session == s)
95: server_redraw_client(c);
96: }
97: }
98:
99: void
1.25 nicm 100: server_redraw_session_group(struct session *s)
101: {
102: struct session_group *sg;
103:
104: if ((sg = session_group_find(s)) == NULL)
105: server_redraw_session(s);
106: else {
107: TAILQ_FOREACH(s, &sg->sessions, gentry)
108: server_redraw_session(s);
109: }
110: }
111:
112: void
1.1 nicm 113: server_status_session(struct session *s)
114: {
115: struct client *c;
116: u_int i;
117:
118: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
119: c = ARRAY_ITEM(&clients, i);
120: if (c == NULL || c->session == NULL)
121: continue;
122: if (c->session == s)
123: server_status_client(c);
124: }
125: }
126:
127: void
1.25 nicm 128: server_status_session_group(struct session *s)
129: {
130: struct session_group *sg;
131:
132: if ((sg = session_group_find(s)) == NULL)
133: server_status_session(s);
134: else {
135: TAILQ_FOREACH(s, &sg->sessions, gentry)
136: server_status_session(s);
137: }
138: }
139:
140: void
1.1 nicm 141: server_redraw_window(struct window *w)
142: {
143: struct client *c;
144: u_int i;
145:
146: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
147: c = ARRAY_ITEM(&clients, i);
148: if (c == NULL || c->session == NULL)
149: continue;
150: if (c->session->curw->window == w)
151: server_redraw_client(c);
152: }
153: w->flags |= WINDOW_REDRAW;
1.33 nicm 154: }
155:
156: void
157: server_redraw_window_borders(struct window *w)
158: {
159: struct client *c;
160: u_int i;
161:
162: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
163: c = ARRAY_ITEM(&clients, i);
164: if (c == NULL || c->session == NULL)
165: continue;
166: if (c->session->curw->window == w)
167: c->flags |= CLIENT_BORDERS;
168: }
1.1 nicm 169: }
170:
171: void
172: server_status_window(struct window *w)
173: {
174: struct session *s;
175:
176: /*
177: * This is slightly different. We want to redraw the status line of any
178: * clients containing this window rather than any where it is the
179: * current window.
180: */
181:
1.47 ! nicm 182: RB_FOREACH(s, sessions, &sessions) {
! 183: if (session_has(s, w) != NULL)
1.1 nicm 184: server_status_session(s);
185: }
186: }
187:
188: void
189: server_lock(void)
190: {
1.23 nicm 191: struct client *c;
192: u_int i;
1.17 nicm 193:
1.1 nicm 194: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
195: c = ARRAY_ITEM(&clients, i);
196: if (c == NULL || c->session == NULL)
197: continue;
1.23 nicm 198: server_lock_client(c);
199: }
200: }
1.1 nicm 201:
1.23 nicm 202: void
203: server_lock_session(struct session *s)
204: {
205: struct client *c;
206: u_int i;
207:
208: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
209: c = ARRAY_ITEM(&clients, i);
210: if (c == NULL || c->session == NULL || c->session != s)
211: continue;
212: server_lock_client(c);
1.31 nicm 213: }
1.23 nicm 214: }
1.1 nicm 215:
1.23 nicm 216: void
217: server_lock_client(struct client *c)
218: {
219: const char *cmd;
220: size_t cmdlen;
221: struct msg_lock_data lockdata;
1.24 nicm 222:
223: if (c->flags & CLIENT_SUSPENDED)
224: return;
1.1 nicm 225:
1.23 nicm 226: cmd = options_get_string(&c->session->options, "lock-command");
227: cmdlen = strlcpy(lockdata.cmd, cmd, sizeof lockdata.cmd);
228: if (cmdlen >= sizeof lockdata.cmd)
229: return;
1.31 nicm 230:
1.23 nicm 231: tty_stop_tty(&c->tty);
232: tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_SMCUP));
233: tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_CLEAR));
234:
235: c->flags |= CLIENT_SUSPENDED;
236: server_write_client(c, MSG_LOCK, &lockdata, sizeof lockdata);
1.8 nicm 237: }
238:
239: void
240: server_kill_window(struct window *w)
241: {
242: struct session *s;
243: struct winlink *wl;
1.31 nicm 244:
1.47 ! nicm 245: RB_FOREACH(s, sessions, &sessions) {
! 246: if (session_has(s, w) == NULL)
1.8 nicm 247: continue;
1.34 nicm 248: while ((wl = winlink_find_by_window(&s->windows, w)) != NULL) {
249: if (session_detach(s, wl)) {
250: server_destroy_session_group(s);
251: break;
252: } else
253: server_redraw_session_group(s);
254: }
1.8 nicm 255: }
1.21 nicm 256: }
257:
258: int
1.25 nicm 259: server_link_window(struct session *src, struct winlink *srcwl,
260: struct session *dst, int dstidx, int killflag, int selectflag, char **cause)
1.21 nicm 261: {
1.25 nicm 262: struct winlink *dstwl;
263: struct session_group *srcsg, *dstsg;
264:
265: srcsg = session_group_find(src);
266: dstsg = session_group_find(dst);
267: if (src != dst && srcsg != NULL && dstsg != NULL && srcsg == dstsg) {
268: xasprintf(cause, "sessions are grouped");
269: return (-1);
270: }
1.21 nicm 271:
272: dstwl = NULL;
273: if (dstidx != -1)
274: dstwl = winlink_find_by_index(&dst->windows, dstidx);
275: if (dstwl != NULL) {
1.44 nicm 276: if (dstwl->window == srcwl->window) {
277: xasprintf(cause, "same index: %d", dstidx);
1.41 nicm 278: return (-1);
1.44 nicm 279: }
1.21 nicm 280: if (killflag) {
281: /*
282: * Can't use session_detach as it will destroy session
283: * if this makes it empty.
284: */
1.37 nicm 285: dstwl->flags &= ~WINLINK_ALERTFLAGS;
1.21 nicm 286: winlink_stack_remove(&dst->lastw, dstwl);
287: winlink_remove(&dst->windows, dstwl);
288:
289: /* Force select/redraw if current. */
1.26 nicm 290: if (dstwl == dst->curw) {
1.21 nicm 291: selectflag = 1;
1.26 nicm 292: dst->curw = NULL;
293: }
1.21 nicm 294: }
295: }
296:
297: if (dstidx == -1)
298: dstidx = -1 - options_get_number(&dst->options, "base-index");
299: dstwl = session_attach(dst, srcwl->window, dstidx, cause);
300: if (dstwl == NULL)
301: return (-1);
302:
1.25 nicm 303: if (selectflag)
1.21 nicm 304: session_select(dst, dstwl->idx);
1.25 nicm 305: server_redraw_session_group(dst);
1.21 nicm 306:
307: return (0);
308: }
309:
310: void
311: server_unlink_window(struct session *s, struct winlink *wl)
312: {
313: if (session_detach(s, wl))
1.25 nicm 314: server_destroy_session_group(s);
315: else
316: server_redraw_session_group(s);
1.29 nicm 317: }
318:
319: void
320: server_destroy_pane(struct window_pane *wp)
321: {
322: struct window *w = wp->window;
323:
1.36 nicm 324: if (wp->fd != -1) {
325: close(wp->fd);
326: bufferevent_free(wp->event);
327: wp->fd = -1;
328: }
1.29 nicm 329:
330: if (options_get_number(&w->options, "remain-on-exit"))
331: return;
332:
333: layout_close_pane(wp);
334: window_remove_pane(w, wp);
335:
336: if (TAILQ_EMPTY(&w->panes))
337: server_kill_window(w);
338: else
339: server_redraw_window(w);
1.25 nicm 340: }
341:
342: void
343: server_destroy_session_group(struct session *s)
344: {
345: struct session_group *sg;
346:
347: if ((sg = session_group_find(s)) == NULL)
1.21 nicm 348: server_destroy_session(s);
1.25 nicm 349: else {
350: TAILQ_FOREACH(s, &sg->sessions, gentry)
351: server_destroy_session(s);
352: TAILQ_REMOVE(&session_groups, sg, entry);
353: xfree(sg);
354: }
1.19 nicm 355: }
356:
1.39 nicm 357: struct session *
358: server_next_session(struct session *s)
359: {
360: struct session *s_loop, *s_out;
361:
362: s_out = NULL;
1.47 ! nicm 363: RB_FOREACH(s_loop, sessions, &sessions) {
! 364: if (s_loop == s)
1.39 nicm 365: continue;
366: if (s_out == NULL ||
367: timercmp(&s_loop->activity_time, &s_out->activity_time, <))
368: s_out = s_loop;
369: }
370: return (s_out);
371: }
372:
1.19 nicm 373: void
374: server_destroy_session(struct session *s)
375: {
376: struct client *c;
1.39 nicm 377: struct session *s_new;
1.19 nicm 378: u_int i;
1.31 nicm 379:
1.39 nicm 380: if (!options_get_number(&s->options, "detach-on-destroy"))
381: s_new = server_next_session(s);
382: else
383: s_new = NULL;
384:
1.19 nicm 385: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
386: c = ARRAY_ITEM(&clients, i);
387: if (c == NULL || c->session != s)
388: continue;
1.39 nicm 389: if (s_new == NULL) {
390: c->session = NULL;
1.40 nicm 391: c->flags |= CLIENT_EXIT;
1.39 nicm 392: } else {
1.46 nicm 393: c->last_session = NULL;
1.39 nicm 394: c->session = s_new;
395: server_redraw_client(c);
396: }
1.19 nicm 397: }
1.38 nicm 398: recalculate_sizes();
1.42 nicm 399: }
400:
401: void
402: server_check_unattached (void)
403: {
404: struct session *s;
405:
406: /*
407: * If any sessions are no longer attached and have destroy-unattached
408: * set, collect them.
409: */
1.47 ! nicm 410: RB_FOREACH(s, sessions, &sessions) {
! 411: if (!(s->flags & SESSION_UNATTACHED))
1.42 nicm 412: continue;
413: if (options_get_number (&s->options, "destroy-unattached"))
414: session_destroy(s);
415: }
1.15 nicm 416: }
417:
418: void
419: server_set_identify(struct client *c)
420: {
421: struct timeval tv;
422: int delay;
423:
424: delay = options_get_number(&c->session->options, "display-panes-time");
425: tv.tv_sec = delay / 1000;
426: tv.tv_usec = (delay % 1000) * 1000L;
1.31 nicm 427:
1.28 nicm 428: evtimer_del(&c->identify_timer);
429: evtimer_set(&c->identify_timer, server_callback_identify, c);
430: evtimer_add(&c->identify_timer, &tv);
1.15 nicm 431:
432: c->flags |= CLIENT_IDENTIFY;
433: c->tty.flags |= (TTY_FREEZE|TTY_NOCURSOR);
434: server_redraw_client(c);
435: }
436:
437: void
438: server_clear_identify(struct client *c)
439: {
440: if (c->flags & CLIENT_IDENTIFY) {
441: c->flags &= ~CLIENT_IDENTIFY;
442: c->tty.flags &= ~(TTY_FREEZE|TTY_NOCURSOR);
443: server_redraw_client(c);
444: }
1.28 nicm 445: }
446:
1.30 nicm 447: /* ARGSUSED */
1.28 nicm 448: void
449: server_callback_identify(unused int fd, unused short events, void *data)
450: {
451: struct client *c = data;
452:
453: server_clear_identify(c);
1.27 nicm 454: }
455:
456: void
457: server_update_event(struct client *c)
458: {
459: short events;
460:
461: events = 0;
462: if (!(c->flags & CLIENT_BAD))
463: events |= EV_READ;
464: if (c->ibuf.w.queued > 0)
465: events |= EV_WRITE;
466: event_del(&c->event);
467: event_set(&c->event, c->ibuf.fd, events, server_client_callback, c);
1.31 nicm 468: event_add(&c->event, NULL);
1.1 nicm 469: }