Annotation of src/usr.bin/tmux/server-fn.c, Revision 1.28
1.28 ! nicm 1: /* $OpenBSD: server-fn.c,v 1.27 2009/11/04 23:12:43 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.28 ! nicm 27: void server_callback_identify(int, short, void *);
! 28:
1.13 nicm 29: void
30: server_fill_environ(struct session *s, struct environ *env)
1.1 nicm 31: {
1.13 nicm 32: char tmuxvar[MAXPATHLEN], *term;
33: u_int idx;
1.1 nicm 34:
35: if (session_index(s, &idx) != 0)
36: fatalx("session not found");
37: xsnprintf(tmuxvar, sizeof tmuxvar,
1.13 nicm 38: "%s,%ld,%u", socket_path, (long) getpid(), idx);
39: environ_set(env, "TMUX", tmuxvar);
1.1 nicm 40:
1.13 nicm 41: term = options_get_string(&s->options, "default-terminal");
42: environ_set(env, "TERM", term);
1.1 nicm 43: }
44:
45: void
1.10 nicm 46: server_write_error(struct client *c, const char *msg)
47: {
48: struct msg_print_data printdata;
49:
50: strlcpy(printdata.msg, msg, sizeof printdata.msg);
51: server_write_client(c, MSG_ERROR, &printdata, sizeof printdata);
52: }
53:
54: void
1.1 nicm 55: server_write_client(
1.11 nicm 56: struct client *c, enum msgtype type, const void *buf, size_t len)
1.1 nicm 57: {
1.14 nicm 58: struct imsgbuf *ibuf = &c->ibuf;
1.1 nicm 59:
1.12 nicm 60: if (c->flags & CLIENT_BAD)
61: return;
1.14 nicm 62: log_debug("writing %d to client %d", type, c->ibuf.fd);
63: imsg_compose(ibuf, type, PROTOCOL_VERSION, -1, -1, (void *) buf, len);
1.27 nicm 64: server_update_event(c);
1.1 nicm 65: }
66:
67: void
68: server_write_session(
1.11 nicm 69: struct session *s, enum msgtype type, const void *buf, size_t len)
1.1 nicm 70: {
71: struct client *c;
72: u_int i;
73:
74: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
75: c = ARRAY_ITEM(&clients, i);
76: if (c == NULL || c->session == NULL)
77: continue;
78: if (c->session == s)
79: server_write_client(c, type, buf, len);
80: }
81: }
82:
83: void
84: server_redraw_client(struct client *c)
85: {
86: c->flags |= CLIENT_REDRAW;
87: }
88:
89: void
90: server_status_client(struct client *c)
91: {
92: c->flags |= CLIENT_STATUS;
93: }
94:
95: void
96: server_redraw_session(struct session *s)
97: {
98: struct client *c;
99: u_int i;
100:
101: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
102: c = ARRAY_ITEM(&clients, i);
103: if (c == NULL || c->session == NULL)
104: continue;
105: if (c->session == s)
106: server_redraw_client(c);
107: }
108: }
109:
110: void
1.25 nicm 111: server_redraw_session_group(struct session *s)
112: {
113: struct session_group *sg;
114:
115: if ((sg = session_group_find(s)) == NULL)
116: server_redraw_session(s);
117: else {
118: TAILQ_FOREACH(s, &sg->sessions, gentry)
119: server_redraw_session(s);
120: }
121: }
122:
123: void
1.1 nicm 124: server_status_session(struct session *s)
125: {
126: struct client *c;
127: u_int i;
128:
129: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
130: c = ARRAY_ITEM(&clients, i);
131: if (c == NULL || c->session == NULL)
132: continue;
133: if (c->session == s)
134: server_status_client(c);
135: }
136: }
137:
138: void
1.25 nicm 139: server_status_session_group(struct session *s)
140: {
141: struct session_group *sg;
142:
143: if ((sg = session_group_find(s)) == NULL)
144: server_status_session(s);
145: else {
146: TAILQ_FOREACH(s, &sg->sessions, gentry)
147: server_status_session(s);
148: }
149: }
150:
151: void
1.1 nicm 152: server_redraw_window(struct window *w)
153: {
154: struct client *c;
155: u_int i;
156:
157: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
158: c = ARRAY_ITEM(&clients, i);
159: if (c == NULL || c->session == NULL)
160: continue;
161: if (c->session->curw->window == w)
162: server_redraw_client(c);
163: }
164: w->flags |= WINDOW_REDRAW;
165: }
166:
167: void
168: server_status_window(struct window *w)
169: {
170: struct session *s;
171: u_int i;
172:
173: /*
174: * This is slightly different. We want to redraw the status line of any
175: * clients containing this window rather than any where it is the
176: * current window.
177: */
178:
179: for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
180: s = ARRAY_ITEM(&sessions, i);
181: if (s != NULL && session_has(s, w))
182: server_status_session(s);
183: }
184: }
185:
186: void
187: server_lock(void)
188: {
1.23 nicm 189: struct client *c;
190: u_int i;
1.17 nicm 191:
1.1 nicm 192: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
193: c = ARRAY_ITEM(&clients, i);
194: if (c == NULL || c->session == NULL)
195: continue;
1.23 nicm 196: server_lock_client(c);
197: }
198: }
1.1 nicm 199:
1.23 nicm 200: void
201: server_lock_session(struct session *s)
202: {
203: struct client *c;
204: u_int i;
205:
206: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
207: c = ARRAY_ITEM(&clients, i);
208: if (c == NULL || c->session == NULL || c->session != s)
209: continue;
210: server_lock_client(c);
211: }
212: }
1.1 nicm 213:
1.23 nicm 214: void
215: server_lock_client(struct client *c)
216: {
217: const char *cmd;
218: size_t cmdlen;
219: struct msg_lock_data lockdata;
1.24 nicm 220:
221: if (c->flags & CLIENT_SUSPENDED)
222: return;
1.1 nicm 223:
1.23 nicm 224: cmd = options_get_string(&c->session->options, "lock-command");
225: cmdlen = strlcpy(lockdata.cmd, cmd, sizeof lockdata.cmd);
226: if (cmdlen >= sizeof lockdata.cmd)
227: return;
228:
229: tty_stop_tty(&c->tty);
230: tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_SMCUP));
231: tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_CLEAR));
232:
233: c->flags |= CLIENT_SUSPENDED;
234: server_write_client(c, MSG_LOCK, &lockdata, sizeof lockdata);
1.8 nicm 235: }
236:
237: void
238: server_kill_window(struct window *w)
239: {
240: struct session *s;
241: struct winlink *wl;
1.19 nicm 242: u_int i;
1.8 nicm 243:
244: for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
245: s = ARRAY_ITEM(&sessions, i);
246: if (s == NULL || !session_has(s, w))
247: continue;
248: if ((wl = winlink_find_by_window(&s->windows, w)) == NULL)
249: continue;
250:
1.19 nicm 251: if (session_detach(s, wl))
1.25 nicm 252: server_destroy_session_group(s);
253: else {
1.19 nicm 254: server_redraw_session(s);
1.25 nicm 255: server_status_session_group(s);
256: }
1.8 nicm 257: }
1.21 nicm 258: }
259:
260: int
1.25 nicm 261: server_link_window(struct session *src, struct winlink *srcwl,
262: struct session *dst, int dstidx, int killflag, int selectflag, char **cause)
1.21 nicm 263: {
1.25 nicm 264: struct winlink *dstwl;
265: struct session_group *srcsg, *dstsg;
266:
267: srcsg = session_group_find(src);
268: dstsg = session_group_find(dst);
269: if (src != dst && srcsg != NULL && dstsg != NULL && srcsg == dstsg) {
270: xasprintf(cause, "sessions are grouped");
271: return (-1);
272: }
1.21 nicm 273:
274: dstwl = NULL;
275: if (dstidx != -1)
276: dstwl = winlink_find_by_index(&dst->windows, dstidx);
277: if (dstwl != NULL) {
278: if (dstwl->window == srcwl->window)
279: return (0);
280: if (killflag) {
281: /*
282: * Can't use session_detach as it will destroy session
283: * if this makes it empty.
284: */
285: session_alert_cancel(dst, dstwl);
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);
317: }
318:
319: void
320: server_destroy_session_group(struct session *s)
321: {
322: struct session_group *sg;
323:
324: if ((sg = session_group_find(s)) == NULL)
1.21 nicm 325: server_destroy_session(s);
1.25 nicm 326: else {
327: TAILQ_FOREACH(s, &sg->sessions, gentry)
328: server_destroy_session(s);
329: TAILQ_REMOVE(&session_groups, sg, entry);
330: xfree(sg);
331: }
1.19 nicm 332: }
333:
334: void
335: server_destroy_session(struct session *s)
336: {
337: struct client *c;
338: u_int i;
339:
340: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
341: c = ARRAY_ITEM(&clients, i);
342: if (c == NULL || c->session != s)
343: continue;
344: c->session = NULL;
345: server_write_client(c, MSG_EXIT, NULL, 0);
346: }
1.15 nicm 347: }
348:
349: void
350: server_set_identify(struct client *c)
351: {
352: struct timeval tv;
353: int delay;
354:
355: delay = options_get_number(&c->session->options, "display-panes-time");
356: tv.tv_sec = delay / 1000;
357: tv.tv_usec = (delay % 1000) * 1000L;
1.28 ! nicm 358:
! 359: evtimer_del(&c->identify_timer);
! 360: evtimer_set(&c->identify_timer, server_callback_identify, c);
! 361: evtimer_add(&c->identify_timer, &tv);
1.15 nicm 362:
363: c->flags |= CLIENT_IDENTIFY;
364: c->tty.flags |= (TTY_FREEZE|TTY_NOCURSOR);
365: server_redraw_client(c);
366: }
367:
368: void
369: server_clear_identify(struct client *c)
370: {
371: if (c->flags & CLIENT_IDENTIFY) {
372: c->flags &= ~CLIENT_IDENTIFY;
373: c->tty.flags &= ~(TTY_FREEZE|TTY_NOCURSOR);
374: server_redraw_client(c);
375: }
1.28 ! nicm 376: }
! 377:
! 378: void
! 379: server_callback_identify(unused int fd, unused short events, void *data)
! 380: {
! 381: struct client *c = data;
! 382:
! 383: server_clear_identify(c);
1.27 nicm 384: }
385:
386: void
387: server_update_event(struct client *c)
388: {
389: short events;
390:
391: events = 0;
392: if (!(c->flags & CLIENT_BAD))
393: events |= EV_READ;
394: if (c->ibuf.w.queued > 0)
395: events |= EV_WRITE;
396: event_del(&c->event);
397: event_set(&c->event, c->ibuf.fd, events, server_client_callback, c);
398: event_add(&c->event, NULL);
1.1 nicm 399: }