Annotation of src/usr.bin/tmux/server-fn.c, Revision 1.22
1.22 ! nicm 1: /* $OpenBSD: server-fn.c,v 1.21 2009/09/20 17:27:18 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.13 nicm 27: void
28: server_fill_environ(struct session *s, struct environ *env)
1.1 nicm 29: {
1.13 nicm 30: char tmuxvar[MAXPATHLEN], *term;
31: u_int idx;
1.1 nicm 32:
33: if (session_index(s, &idx) != 0)
34: fatalx("session not found");
35: xsnprintf(tmuxvar, sizeof tmuxvar,
1.13 nicm 36: "%s,%ld,%u", socket_path, (long) getpid(), idx);
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.1 nicm 41: }
42:
43: void
1.10 nicm 44: server_write_error(struct client *c, const char *msg)
45: {
46: struct msg_print_data printdata;
47:
48: strlcpy(printdata.msg, msg, sizeof printdata.msg);
49: server_write_client(c, MSG_ERROR, &printdata, sizeof printdata);
50: }
51:
52: void
1.1 nicm 53: server_write_client(
1.11 nicm 54: struct client *c, enum msgtype type, const void *buf, size_t len)
1.1 nicm 55: {
1.14 nicm 56: struct imsgbuf *ibuf = &c->ibuf;
1.1 nicm 57:
1.12 nicm 58: if (c->flags & CLIENT_BAD)
59: return;
1.14 nicm 60: log_debug("writing %d to client %d", type, c->ibuf.fd);
61: imsg_compose(ibuf, type, PROTOCOL_VERSION, -1, -1, (void *) buf, len);
1.1 nicm 62: }
63:
64: void
65: server_write_session(
1.11 nicm 66: struct session *s, enum msgtype type, const void *buf, size_t len)
1.1 nicm 67: {
68: struct client *c;
69: u_int i;
70:
71: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
72: c = ARRAY_ITEM(&clients, i);
73: if (c == NULL || c->session == NULL)
74: continue;
75: if (c->session == s)
76: server_write_client(c, type, buf, len);
77: }
78: }
79:
80: void
81: server_redraw_client(struct client *c)
82: {
83: c->flags |= CLIENT_REDRAW;
84: }
85:
86: void
87: server_status_client(struct client *c)
88: {
89: c->flags |= CLIENT_STATUS;
90: }
91:
92: void
93: server_redraw_session(struct session *s)
94: {
95: struct client *c;
96: u_int i;
97:
98: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
99: c = ARRAY_ITEM(&clients, i);
100: if (c == NULL || c->session == NULL)
101: continue;
102: if (c->session == s)
103: server_redraw_client(c);
104: }
105: }
106:
107: void
108: server_status_session(struct session *s)
109: {
110: struct client *c;
111: u_int i;
112:
113: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
114: c = ARRAY_ITEM(&clients, i);
115: if (c == NULL || c->session == NULL)
116: continue;
117: if (c->session == s)
118: server_status_client(c);
119: }
120: }
121:
122: void
123: server_redraw_window(struct window *w)
124: {
125: struct client *c;
126: u_int i;
127:
128: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
129: c = ARRAY_ITEM(&clients, i);
130: if (c == NULL || c->session == NULL)
131: continue;
132: if (c->session->curw->window == w)
133: server_redraw_client(c);
134: }
135: w->flags |= WINDOW_REDRAW;
136: }
137:
138: void
139: server_status_window(struct window *w)
140: {
141: struct session *s;
142: u_int i;
143:
144: /*
145: * This is slightly different. We want to redraw the status line of any
146: * clients containing this window rather than any where it is the
147: * current window.
148: */
149:
150: for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
151: s = ARRAY_ITEM(&sessions, i);
152: if (s != NULL && session_has(s, w))
153: server_status_session(s);
154: }
155: }
156:
157: void
158: server_lock(void)
159: {
1.22 ! nicm 160: struct client *c;
! 161: const char *cmd;
! 162: struct msg_lock_data lockdata;
! 163: u_int i;
1.17 nicm 164:
1.1 nicm 165: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
166: c = ARRAY_ITEM(&clients, i);
167: if (c == NULL || c->session == NULL)
168: continue;
1.22 ! nicm 169: if (c->flags & CLIENT_SUSPENDED)
1.1 nicm 170: continue;
171:
1.22 ! nicm 172: cmd = options_get_string(&c->session->options, "lock-command");
! 173: if (strlcpy(lockdata.cmd,
! 174: cmd, sizeof lockdata.cmd) >= sizeof lockdata.cmd)
1.1 nicm 175: continue;
176:
1.22 ! nicm 177: tty_stop_tty(&c->tty);
! 178: tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_SMCUP));
! 179: tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_CLEAR));
1.1 nicm 180:
1.22 ! nicm 181: c->flags |= CLIENT_SUSPENDED;
! 182: server_write_client(c, MSG_LOCK, &lockdata, sizeof lockdata);
1.17 nicm 183: }
1.8 nicm 184: }
185:
186: void
187: server_kill_window(struct window *w)
188: {
189: struct session *s;
190: struct winlink *wl;
1.19 nicm 191: u_int i;
1.8 nicm 192:
193: for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
194: s = ARRAY_ITEM(&sessions, i);
195: if (s == NULL || !session_has(s, w))
196: continue;
197: if ((wl = winlink_find_by_window(&s->windows, w)) == NULL)
198: continue;
199:
1.19 nicm 200: if (session_detach(s, wl))
201: server_destroy_session(s);
202: else
203: server_redraw_session(s);
1.8 nicm 204: }
1.21 nicm 205: }
206:
207: int
208: server_link_window(
209: struct winlink *srcwl, struct session *dst, int dstidx,
210: int killflag, int selectflag, char **cause)
211: {
212: struct winlink *dstwl;
213:
214: dstwl = NULL;
215: if (dstidx != -1)
216: dstwl = winlink_find_by_index(&dst->windows, dstidx);
217: if (dstwl != NULL) {
218: if (dstwl->window == srcwl->window)
219: return (0);
220: if (killflag) {
221: /*
222: * Can't use session_detach as it will destroy session
223: * if this makes it empty.
224: */
225: session_alert_cancel(dst, dstwl);
226: winlink_stack_remove(&dst->lastw, dstwl);
227: winlink_remove(&dst->windows, dstwl);
228:
229: /* Force select/redraw if current. */
230: if (dstwl == dst->curw)
231: selectflag = 1;
232: }
233: }
234:
235: if (dstidx == -1)
236: dstidx = -1 - options_get_number(&dst->options, "base-index");
237: dstwl = session_attach(dst, srcwl->window, dstidx, cause);
238: if (dstwl == NULL)
239: return (-1);
240:
241: if (!selectflag)
242: server_status_session(dst);
243: else {
244: session_select(dst, dstwl->idx);
245: server_redraw_session(dst);
246: }
247:
248: return (0);
249: }
250:
251: void
252: server_unlink_window(struct session *s, struct winlink *wl)
253: {
254: if (session_detach(s, wl))
255: server_destroy_session(s);
256: else
257: server_redraw_session(s);
1.19 nicm 258: }
259:
260: void
261: server_destroy_session(struct session *s)
262: {
263: struct client *c;
264: u_int i;
265:
266: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
267: c = ARRAY_ITEM(&clients, i);
268: if (c == NULL || c->session != s)
269: continue;
270: c->session = NULL;
271: server_write_client(c, MSG_EXIT, NULL, 0);
272: }
1.15 nicm 273: }
274:
275: void
276: server_set_identify(struct client *c)
277: {
278: struct timeval tv;
279: int delay;
280:
281: delay = options_get_number(&c->session->options, "display-panes-time");
282: tv.tv_sec = delay / 1000;
283: tv.tv_usec = (delay % 1000) * 1000L;
284:
285: if (gettimeofday(&c->identify_timer, NULL) != 0)
1.20 nicm 286: fatal("gettimeofday failed");
1.15 nicm 287: timeradd(&c->identify_timer, &tv, &c->identify_timer);
288:
289: c->flags |= CLIENT_IDENTIFY;
290: c->tty.flags |= (TTY_FREEZE|TTY_NOCURSOR);
291: server_redraw_client(c);
292: }
293:
294: void
295: server_clear_identify(struct client *c)
296: {
297: if (c->flags & CLIENT_IDENTIFY) {
298: c->flags &= ~CLIENT_IDENTIFY;
299: c->tty.flags &= ~(TTY_FREEZE|TTY_NOCURSOR);
300: server_redraw_client(c);
301: }
1.1 nicm 302: }