4 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
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.
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.
19 #include <sys/types.h>
27 struct session
*server_next_session(struct session
*);
28 void server_callback_identify(int, short, void *);
31 server_fill_environ(struct session
*s
, struct environ
*env
)
33 char tmuxvar
[MAXPATHLEN
], *term
;
36 if (session_index(s
, &idx
) != 0)
37 fatalx("session not found");
38 xsnprintf(tmuxvar
, sizeof tmuxvar
,
39 "%s,%ld,%u", socket_path
, (long) getpid(), idx
);
40 environ_set(env
, "TMUX", tmuxvar
);
42 term
= options_get_string(&s
->options
, "default-terminal");
43 environ_set(env
, "TERM", term
);
48 struct client
*c
, enum msgtype type
, const void *buf
, size_t len
)
50 struct imsgbuf
*ibuf
= &c
->ibuf
;
52 if (c
->flags
& CLIENT_BAD
)
54 log_debug("writing %d to client %d", type
, c
->ibuf
.fd
);
55 imsg_compose(ibuf
, type
, PROTOCOL_VERSION
, -1, -1, (void *) buf
, len
);
56 server_update_event(c
);
61 struct session
*s
, enum msgtype type
, const void *buf
, size_t len
)
66 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
67 c
= ARRAY_ITEM(&clients
, i
);
68 if (c
== NULL
|| c
->session
== NULL
)
71 server_write_client(c
, type
, buf
, len
);
76 server_redraw_client(struct client
*c
)
78 c
->flags
|= CLIENT_REDRAW
;
82 server_status_client(struct client
*c
)
84 c
->flags
|= CLIENT_STATUS
;
88 server_redraw_session(struct session
*s
)
93 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
94 c
= ARRAY_ITEM(&clients
, i
);
95 if (c
== NULL
|| c
->session
== NULL
)
98 server_redraw_client(c
);
103 server_redraw_session_group(struct session
*s
)
105 struct session_group
*sg
;
107 if ((sg
= session_group_find(s
)) == NULL
)
108 server_redraw_session(s
);
110 TAILQ_FOREACH(s
, &sg
->sessions
, gentry
)
111 server_redraw_session(s
);
116 server_status_session(struct session
*s
)
121 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
122 c
= ARRAY_ITEM(&clients
, i
);
123 if (c
== NULL
|| c
->session
== NULL
)
126 server_status_client(c
);
131 server_status_session_group(struct session
*s
)
133 struct session_group
*sg
;
135 if ((sg
= session_group_find(s
)) == NULL
)
136 server_status_session(s
);
138 TAILQ_FOREACH(s
, &sg
->sessions
, gentry
)
139 server_status_session(s
);
144 server_redraw_window(struct window
*w
)
149 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
150 c
= ARRAY_ITEM(&clients
, i
);
151 if (c
== NULL
|| c
->session
== NULL
)
153 if (c
->session
->curw
->window
== w
)
154 server_redraw_client(c
);
156 w
->flags
|= WINDOW_REDRAW
;
160 server_redraw_window_borders(struct window
*w
)
165 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
166 c
= ARRAY_ITEM(&clients
, i
);
167 if (c
== NULL
|| c
->session
== NULL
)
169 if (c
->session
->curw
->window
== w
)
170 c
->flags
|= CLIENT_BORDERS
;
175 server_status_window(struct window
*w
)
181 * This is slightly different. We want to redraw the status line of any
182 * clients containing this window rather than any where it is the
186 for (i
= 0; i
< ARRAY_LENGTH(&sessions
); i
++) {
187 s
= ARRAY_ITEM(&sessions
, i
);
188 if (s
!= NULL
&& session_has(s
, w
) != NULL
)
189 server_status_session(s
);
199 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
200 c
= ARRAY_ITEM(&clients
, i
);
201 if (c
== NULL
|| c
->session
== NULL
)
203 server_lock_client(c
);
208 server_lock_session(struct session
*s
)
213 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
214 c
= ARRAY_ITEM(&clients
, i
);
215 if (c
== NULL
|| c
->session
== NULL
|| c
->session
!= s
)
217 server_lock_client(c
);
222 server_lock_client(struct client
*c
)
226 struct msg_lock_data lockdata
;
228 if (c
->flags
& CLIENT_SUSPENDED
)
231 cmd
= options_get_string(&c
->session
->options
, "lock-command");
232 cmdlen
= strlcpy(lockdata
.cmd
, cmd
, sizeof lockdata
.cmd
);
233 if (cmdlen
>= sizeof lockdata
.cmd
)
236 tty_stop_tty(&c
->tty
);
237 tty_raw(&c
->tty
, tty_term_string(c
->tty
.term
, TTYC_SMCUP
));
238 tty_raw(&c
->tty
, tty_term_string(c
->tty
.term
, TTYC_CLEAR
));
240 c
->flags
|= CLIENT_SUSPENDED
;
241 server_write_client(c
, MSG_LOCK
, &lockdata
, sizeof lockdata
);
245 server_kill_window(struct window
*w
)
251 for (i
= 0; i
< ARRAY_LENGTH(&sessions
); i
++) {
252 s
= ARRAY_ITEM(&sessions
, i
);
253 if (s
== NULL
|| session_has(s
, w
) == NULL
)
255 while ((wl
= winlink_find_by_window(&s
->windows
, w
)) != NULL
) {
256 if (session_detach(s
, wl
)) {
257 server_destroy_session_group(s
);
260 server_redraw_session_group(s
);
266 server_link_window(struct session
*src
, struct winlink
*srcwl
,
267 struct session
*dst
, int dstidx
, int killflag
, int selectflag
, char **cause
)
269 struct winlink
*dstwl
;
270 struct session_group
*srcsg
, *dstsg
;
272 srcsg
= session_group_find(src
);
273 dstsg
= session_group_find(dst
);
274 if (src
!= dst
&& srcsg
!= NULL
&& dstsg
!= NULL
&& srcsg
== dstsg
) {
275 xasprintf(cause
, "sessions are grouped");
281 dstwl
= winlink_find_by_index(&dst
->windows
, dstidx
);
283 if (dstwl
->window
== srcwl
->window
)
287 * Can't use session_detach as it will destroy session
288 * if this makes it empty.
290 dstwl
->flags
&= ~WINLINK_ALERTFLAGS
;
291 winlink_stack_remove(&dst
->lastw
, dstwl
);
292 winlink_remove(&dst
->windows
, dstwl
);
294 /* Force select/redraw if current. */
295 if (dstwl
== dst
->curw
) {
303 dstidx
= -1 - options_get_number(&dst
->options
, "base-index");
304 dstwl
= session_attach(dst
, srcwl
->window
, dstidx
, cause
);
309 session_select(dst
, dstwl
->idx
);
310 server_redraw_session_group(dst
);
316 server_unlink_window(struct session
*s
, struct winlink
*wl
)
318 if (session_detach(s
, wl
))
319 server_destroy_session_group(s
);
321 server_redraw_session_group(s
);
325 server_destroy_pane(struct window_pane
*wp
)
327 struct window
*w
= wp
->window
;
331 bufferevent_free(wp
->event
);
335 if (options_get_number(&w
->options
, "remain-on-exit"))
338 layout_close_pane(wp
);
339 window_remove_pane(w
, wp
);
341 if (TAILQ_EMPTY(&w
->panes
))
342 server_kill_window(w
);
344 server_redraw_window(w
);
348 server_destroy_session_group(struct session
*s
)
350 struct session_group
*sg
;
352 if ((sg
= session_group_find(s
)) == NULL
)
353 server_destroy_session(s
);
355 TAILQ_FOREACH(s
, &sg
->sessions
, gentry
)
356 server_destroy_session(s
);
357 TAILQ_REMOVE(&session_groups
, sg
, entry
);
363 server_next_session(struct session
*s
)
365 struct session
*s_loop
, *s_out
;
369 for (i
= 0; i
< ARRAY_LENGTH(&sessions
); i
++) {
370 s_loop
= ARRAY_ITEM(&sessions
, i
);
374 timercmp(&s_loop
->activity_time
, &s_out
->activity_time
, <))
381 server_destroy_session(struct session
*s
)
384 struct session
*s_new
;
387 if (!options_get_number(&s
->options
, "detach-on-destroy"))
388 s_new
= server_next_session(s
);
392 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
393 c
= ARRAY_ITEM(&clients
, i
);
394 if (c
== NULL
|| c
->session
!= s
)
398 c
->flags
|= CLIENT_EXIT
;
401 server_redraw_client(c
);
408 server_set_identify(struct client
*c
)
413 delay
= options_get_number(&c
->session
->options
, "display-panes-time");
414 tv
.tv_sec
= delay
/ 1000;
415 tv
.tv_usec
= (delay
% 1000) * 1000L;
417 evtimer_del(&c
->identify_timer
);
418 evtimer_set(&c
->identify_timer
, server_callback_identify
, c
);
419 evtimer_add(&c
->identify_timer
, &tv
);
421 c
->flags
|= CLIENT_IDENTIFY
;
422 c
->tty
.flags
|= (TTY_FREEZE
|TTY_NOCURSOR
);
423 server_redraw_client(c
);
427 server_clear_identify(struct client
*c
)
429 if (c
->flags
& CLIENT_IDENTIFY
) {
430 c
->flags
&= ~CLIENT_IDENTIFY
;
431 c
->tty
.flags
&= ~(TTY_FREEZE
|TTY_NOCURSOR
);
432 server_redraw_client(c
);
438 server_callback_identify(unused
int fd
, unused
short events
, void *data
)
440 struct client
*c
= data
;
442 server_clear_identify(c
);
446 server_update_event(struct client
*c
)
451 if (!(c
->flags
& CLIENT_BAD
))
453 if (c
->ibuf
.w
.queued
> 0)
455 event_del(&c
->event
);
456 event_set(&c
->event
, c
->ibuf
.fd
, events
, server_client_callback
, c
);
457 event_add(&c
->event
, NULL
);