Annotation of src/usr.bin/tmux/server-fn.c, Revision 1.23
1.23 ! nicm 1: /* $OpenBSD: server-fn.c,v 1.22 2009/09/23 06:18:47 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.23 ! nicm 160: struct client *c;
! 161: u_int i;
1.17 nicm 162:
1.1 nicm 163: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
164: c = ARRAY_ITEM(&clients, i);
165: if (c == NULL || c->session == NULL)
166: continue;
1.22 nicm 167: if (c->flags & CLIENT_SUSPENDED)
1.1 nicm 168: continue;
1.23 ! nicm 169: server_lock_client(c);
! 170: }
! 171: }
1.1 nicm 172:
1.23 ! nicm 173: void
! 174: server_lock_session(struct session *s)
! 175: {
! 176: struct client *c;
! 177: u_int i;
! 178:
! 179: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
! 180: c = ARRAY_ITEM(&clients, i);
! 181: if (c == NULL || c->session == NULL || c->session != s)
! 182: continue;
! 183: if (c->flags & CLIENT_SUSPENDED)
1.1 nicm 184: continue;
1.23 ! nicm 185: server_lock_client(c);
! 186: }
! 187: }
1.1 nicm 188:
1.23 ! nicm 189: void
! 190: server_lock_client(struct client *c)
! 191: {
! 192: const char *cmd;
! 193: size_t cmdlen;
! 194: struct msg_lock_data lockdata;
1.1 nicm 195:
1.23 ! nicm 196: cmd = options_get_string(&c->session->options, "lock-command");
! 197: cmdlen = strlcpy(lockdata.cmd, cmd, sizeof lockdata.cmd);
! 198: if (cmdlen >= sizeof lockdata.cmd)
! 199: return;
! 200:
! 201: tty_stop_tty(&c->tty);
! 202: tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_SMCUP));
! 203: tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_CLEAR));
! 204:
! 205: c->flags |= CLIENT_SUSPENDED;
! 206: server_write_client(c, MSG_LOCK, &lockdata, sizeof lockdata);
1.8 nicm 207: }
208:
209: void
210: server_kill_window(struct window *w)
211: {
212: struct session *s;
213: struct winlink *wl;
1.19 nicm 214: u_int i;
1.8 nicm 215:
216: for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
217: s = ARRAY_ITEM(&sessions, i);
218: if (s == NULL || !session_has(s, w))
219: continue;
220: if ((wl = winlink_find_by_window(&s->windows, w)) == NULL)
221: continue;
222:
1.19 nicm 223: if (session_detach(s, wl))
224: server_destroy_session(s);
225: else
226: server_redraw_session(s);
1.8 nicm 227: }
1.21 nicm 228: }
229:
230: int
231: server_link_window(
232: struct winlink *srcwl, struct session *dst, int dstidx,
233: int killflag, int selectflag, char **cause)
234: {
235: struct winlink *dstwl;
236:
237: dstwl = NULL;
238: if (dstidx != -1)
239: dstwl = winlink_find_by_index(&dst->windows, dstidx);
240: if (dstwl != NULL) {
241: if (dstwl->window == srcwl->window)
242: return (0);
243: if (killflag) {
244: /*
245: * Can't use session_detach as it will destroy session
246: * if this makes it empty.
247: */
248: session_alert_cancel(dst, dstwl);
249: winlink_stack_remove(&dst->lastw, dstwl);
250: winlink_remove(&dst->windows, dstwl);
251:
252: /* Force select/redraw if current. */
253: if (dstwl == dst->curw)
254: selectflag = 1;
255: }
256: }
257:
258: if (dstidx == -1)
259: dstidx = -1 - options_get_number(&dst->options, "base-index");
260: dstwl = session_attach(dst, srcwl->window, dstidx, cause);
261: if (dstwl == NULL)
262: return (-1);
263:
264: if (!selectflag)
265: server_status_session(dst);
266: else {
267: session_select(dst, dstwl->idx);
268: server_redraw_session(dst);
269: }
270:
271: return (0);
272: }
273:
274: void
275: server_unlink_window(struct session *s, struct winlink *wl)
276: {
277: if (session_detach(s, wl))
278: server_destroy_session(s);
279: else
280: server_redraw_session(s);
1.19 nicm 281: }
282:
283: void
284: server_destroy_session(struct session *s)
285: {
286: struct client *c;
287: u_int i;
288:
289: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
290: c = ARRAY_ITEM(&clients, i);
291: if (c == NULL || c->session != s)
292: continue;
293: c->session = NULL;
294: server_write_client(c, MSG_EXIT, NULL, 0);
295: }
1.15 nicm 296: }
297:
298: void
299: server_set_identify(struct client *c)
300: {
301: struct timeval tv;
302: int delay;
303:
304: delay = options_get_number(&c->session->options, "display-panes-time");
305: tv.tv_sec = delay / 1000;
306: tv.tv_usec = (delay % 1000) * 1000L;
307:
308: if (gettimeofday(&c->identify_timer, NULL) != 0)
1.20 nicm 309: fatal("gettimeofday failed");
1.15 nicm 310: timeradd(&c->identify_timer, &tv, &c->identify_timer);
311:
312: c->flags |= CLIENT_IDENTIFY;
313: c->tty.flags |= (TTY_FREEZE|TTY_NOCURSOR);
314: server_redraw_client(c);
315: }
316:
317: void
318: server_clear_identify(struct client *c)
319: {
320: if (c->flags & CLIENT_IDENTIFY) {
321: c->flags &= ~CLIENT_IDENTIFY;
322: c->tty.flags &= ~(TTY_FREEZE|TTY_NOCURSOR);
323: server_redraw_client(c);
324: }
1.1 nicm 325: }