aboutsummaryrefslogtreecommitdiff
path: root/server-client.c
diff options
context:
space:
mode:
authorThomas Adam <thomas.adam@smoothwall.net>2013-03-25 14:59:29 +0000
committerThomas Adam <thomas.adam@smoothwall.net>2013-03-25 14:59:29 +0000
commitf90eb43fcb12720711ea01b110c5b474111e6600 (patch)
tree43b2e85bcf1626e3810ade10578ac18399931772 /server-client.c
parent418ba99078a2712ece398e17a5a9bc1f6600126b (diff)
parent58bb6f8c5650d496fb3b872766c0278aa024631d (diff)
downloadrtmux-f90eb43fcb12720711ea01b110c5b474111e6600.tar.gz
rtmux-f90eb43fcb12720711ea01b110c5b474111e6600.tar.bz2
rtmux-f90eb43fcb12720711ea01b110c5b474111e6600.zip
Merge branch 'obsd-master'
Diffstat (limited to 'server-client.c')
-rw-r--r--server-client.c205
1 files changed, 124 insertions, 81 deletions
diff --git a/server-client.c b/server-client.c
index 796390f7..377f9ae9 100644
--- a/server-client.c
+++ b/server-client.c
@@ -17,9 +17,11 @@
*/
#include <sys/types.h>
+#include <sys/ioctl.h>
#include <event.h>
#include <fcntl.h>
+#include <paths.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
@@ -27,6 +29,8 @@
#include "tmux.h"
+void server_client_check_focus(struct window_pane *);
+void server_client_check_resize(struct window_pane *);
void server_client_check_mouse(struct client *, struct window_pane *);
void server_client_repeat_timer(int, short, void *);
void server_client_check_exit(struct client *);
@@ -41,10 +45,6 @@ void server_client_msg_identify(
struct client *, struct msg_identify_data *, int);
void server_client_msg_shell(struct client *);
-void printflike2 server_client_msg_error(struct cmd_ctx *, const char *, ...);
-void printflike2 server_client_msg_print(struct cmd_ctx *, const char *, ...);
-void printflike2 server_client_msg_info(struct cmd_ctx *, const char *, ...);
-
/* Create a new client. */
void
server_client_create(int fd)
@@ -63,6 +63,9 @@ server_client_create(int fd)
fatal("gettimeofday failed");
memcpy(&c->activity_time, &c->creation_time, sizeof c->activity_time);
+ c->cmdq = cmdq_new(c);
+ c->cmdq->client_exit = 1;
+
c->stdin_data = evbuffer_new ();
c->stdout_data = evbuffer_new ();
c->stderr_data = evbuffer_new ();
@@ -93,6 +96,8 @@ server_client_create(int fd)
c->tty.mouse.event = MOUSE_EVENT_UP;
c->tty.mouse.flags = 0;
+ c->flags |= CLIENT_FOCUSED;
+
evtimer_set(&c->repeat_timer, server_client_repeat_timer, c);
for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
@@ -149,7 +154,8 @@ server_client_lost(struct client *c)
evbuffer_free (c->stdin_data);
evbuffer_free (c->stdout_data);
- evbuffer_free (c->stderr_data);
+ if (c->stderr_data != c->stdout_data)
+ evbuffer_free (c->stderr_data);
status_free_jobs(&c->status_new);
status_free_jobs(&c->status_old);
@@ -175,6 +181,10 @@ server_client_lost(struct client *c)
free(c->prompt_buffer);
free(c->cwd);
+ c->cmdq->dead = 1;
+ cmdq_free(c->cmdq);
+ c->cmdq = NULL;
+
environ_free(&c->environ);
close(c->ibuf.fd);
@@ -355,6 +365,7 @@ server_client_handle_key(struct client *c, int key)
/* Check the client is good to accept input. */
if ((c->flags & (CLIENT_DEAD|CLIENT_SUSPENDED)) != 0)
return;
+
if (c->session == NULL)
return;
s = c->session;
@@ -374,6 +385,7 @@ server_client_handle_key(struct client *c, int key)
if (c->flags & CLIENT_IDENTIFY && key >= '0' && key <= '9') {
if (c->flags & CLIENT_READONLY)
return;
+ window_unzoom(w);
wp = window_pane_at_index(w, key - '0');
if (wp != NULL && window_pane_visible(wp))
window_set_active_pane(w, wp);
@@ -417,6 +429,7 @@ server_client_handle_key(struct client *c, int key)
if (!(c->flags & CLIENT_PREFIX)) {
if (isprefix) {
c->flags |= CLIENT_PREFIX;
+ server_status_client(c);
return;
}
@@ -431,6 +444,7 @@ server_client_handle_key(struct client *c, int key)
/* Prefix key already pressed. Reset prefix and lookup key. */
c->flags &= ~CLIENT_PREFIX;
+ server_status_client(c);
if ((bd = key_bindings_lookup(key | KEYC_PREFIX)) == NULL) {
/* If repeating, treat this as a key, else ignore. */
if (c->flags & CLIENT_REPEAT) {
@@ -491,7 +505,7 @@ server_client_loop(void)
/*
* Any windows will have been redrawn as part of clients, so clear
- * their flags now.
+ * their flags now. Also check pane focus and resize.
*/
for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
w = ARRAY_ITEM(&windows, i);
@@ -499,9 +513,90 @@ server_client_loop(void)
continue;
w->flags &= ~WINDOW_REDRAW;
- TAILQ_FOREACH(wp, &w->panes, entry)
+ TAILQ_FOREACH(wp, &w->panes, entry) {
+ server_client_check_focus(wp);
+ server_client_check_resize(wp);
wp->flags &= ~PANE_REDRAW;
+ }
+ }
+}
+
+/* Check if pane should be resized. */
+void
+server_client_check_resize(struct window_pane *wp)
+{
+ struct winsize ws;
+
+ if (wp->fd == -1 || !(wp->flags & PANE_RESIZE))
+ return;
+
+ memset(&ws, 0, sizeof ws);
+ ws.ws_col = wp->sx;
+ ws.ws_row = wp->sy;
+
+ if (ioctl(wp->fd, TIOCSWINSZ, &ws) == -1) {
+#ifdef __sun
+ /*
+ * Some versions of Solaris apparently can return an error when
+ * resizing; don't know why this happens, can't reproduce on
+ * other platforms and ignoring it doesn't seem to cause any
+ * issues.
+ */
+ if (errno != EINVAL)
+#endif
+ fatal("ioctl failed");
+ }
+
+ wp->flags &= ~PANE_RESIZE;
+}
+
+/* Check whether pane should be focused. */
+void
+server_client_check_focus(struct window_pane *wp)
+{
+ u_int i;
+ struct client *c;
+
+ /* If we don't care about focus, forget it. */
+ if (!(wp->base.mode & MODE_FOCUSON))
+ return;
+
+ /* If we're not the active pane in our window, we're not focused. */
+ if (wp->window->active != wp)
+ goto not_focused;
+
+ /* If we're in a mode, we're not focused. */
+ if (wp->screen != &wp->base)
+ goto not_focused;
+
+ /*
+ * If our window is the current window in any focused clients with an
+ * attached session, we're focused.
+ */
+ for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
+ c = ARRAY_ITEM(&clients, i);
+ if (c == NULL || c->session == NULL)
+ continue;
+
+ if (!(c->flags & CLIENT_FOCUSED))
+ continue;
+ if (c->session->flags & SESSION_UNATTACHED)
+ continue;
+
+ if (c->session->curw->window == wp->window)
+ goto focused;
}
+
+not_focused:
+ if (wp->flags & PANE_FOCUSED)
+ bufferevent_write(wp->event, "\033[O", 3);
+ wp->flags &= ~PANE_FOCUSED;
+ return;
+
+focused:
+ if (!(wp->flags & PANE_FOCUSED))
+ bufferevent_write(wp->event, "\033[I", 3);
+ wp->flags |= PANE_FOCUSED;
}
/*
@@ -526,6 +621,9 @@ server_client_reset_state(struct client *c)
if (c->flags & CLIENT_SUSPENDED)
return;
+ if (c->flags & CLIENT_CONTROL)
+ return;
+
tty_region(&c->tty, 0, c->tty.sy - 1);
status = options_get_number(oo, "status");
@@ -580,14 +678,16 @@ server_client_reset_state(struct client *c)
}
/* Repeat time callback. */
-/* ARGSUSED */
void
server_client_repeat_timer(unused int fd, unused short events, void *data)
{
struct client *c = data;
- if (c->flags & CLIENT_REPEAT)
+ if (c->flags & CLIENT_REPEAT) {
+ if (c->flags & CLIENT_PREFIX)
+ server_status_client(c);
c->flags &= ~(CLIENT_PREFIX|CLIENT_REPEAT);
+ }
}
/* Check if client should be exited. */
@@ -620,7 +720,7 @@ server_client_check_redraw(struct client *c)
struct window_pane *wp;
int flags, redraw;
- if (c->flags & CLIENT_SUSPENDED)
+ if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
return;
flags = c->tty.flags & TTY_FREEZE;
@@ -750,6 +850,8 @@ server_client_msg_dispatch(struct client *c)
if (datalen != 0)
fatalx("bad MSG_RESIZE size");
+ if (c->flags & CLIENT_CONTROL)
+ break;
if (tty_resize(&c->tty)) {
recalculate_sizes();
server_redraw_client(c);
@@ -804,74 +906,18 @@ server_client_msg_dispatch(struct client *c)
}
}
-/* Callback to send error message to client. */
-void printflike2
-server_client_msg_error(struct cmd_ctx *ctx, const char *fmt, ...)
-{
- va_list ap;
-
- va_start(ap, fmt);
- evbuffer_add_vprintf(ctx->cmdclient->stderr_data, fmt, ap);
- va_end(ap);
-
- evbuffer_add(ctx->cmdclient->stderr_data, "\n", 1);
- server_push_stderr(ctx->cmdclient);
- ctx->cmdclient->retcode = 1;
-}
-
-/* Callback to send print message to client. */
-void printflike2
-server_client_msg_print(struct cmd_ctx *ctx, const char *fmt, ...)
-{
- va_list ap;
-
- va_start(ap, fmt);
- evbuffer_add_vprintf(ctx->cmdclient->stdout_data, fmt, ap);
- va_end(ap);
-
- evbuffer_add(ctx->cmdclient->stdout_data, "\n", 1);
- server_push_stdout(ctx->cmdclient);
-}
-
-/* Callback to send print message to client, if not quiet. */
-void printflike2
-server_client_msg_info(struct cmd_ctx *ctx, const char *fmt, ...)
-{
- va_list ap;
-
- if (options_get_number(&global_options, "quiet"))
- return;
-
- va_start(ap, fmt);
- evbuffer_add_vprintf(ctx->cmdclient->stdout_data, fmt, ap);
- va_end(ap);
-
- evbuffer_add(ctx->cmdclient->stdout_data, "\n", 1);
- server_push_stdout(ctx->cmdclient);
-}
-
/* Handle command message. */
void
server_client_msg_command(struct client *c, struct msg_command_data *data)
{
- struct cmd_ctx ctx;
struct cmd_list *cmdlist = NULL;
int argc;
char **argv, *cause;
- ctx.error = server_client_msg_error;
- ctx.print = server_client_msg_print;
- ctx.info = server_client_msg_info;
-
- ctx.msgdata = data;
- ctx.curclient = NULL;
-
- ctx.cmdclient = c;
-
argc = data->argc;
data->argv[(sizeof data->argv) - 1] = '\0';
if (cmd_unpack_argv(data->argv, sizeof data->argv, argc, &argv) != 0) {
- server_client_msg_error(&ctx, "command too long");
+ cmdq_error(c->cmdq, "command too long");
goto error;
}
@@ -881,29 +927,21 @@ server_client_msg_command(struct client *c, struct msg_command_data *data)
*argv = xstrdup("new-session");
}
- if ((cmdlist = cmd_list_parse(argc, argv, &cause)) == NULL) {
- server_client_msg_error(&ctx, "%s", cause);
+ if ((cmdlist = cmd_list_parse(argc, argv, NULL, 0, &cause)) == NULL) {
+ cmdq_error(c->cmdq, "%s", cause);
cmd_free_argv(argc, argv);
goto error;
}
cmd_free_argv(argc, argv);
- switch (cmd_list_exec(cmdlist, &ctx))
- {
- case CMD_RETURN_ERROR:
- case CMD_RETURN_NORMAL:
- c->flags |= CLIENT_EXIT;
- break;
- case CMD_RETURN_ATTACH:
- case CMD_RETURN_YIELD:
- break;
- }
+ cmdq_run(c->cmdq, cmdlist);
cmd_list_free(cmdlist);
return;
error:
if (cmdlist != NULL)
cmd_list_free(cmdlist);
+
c->flags |= CLIENT_EXIT;
}
@@ -919,7 +957,11 @@ server_client_msg_identify(
if (data->flags & IDENTIFY_CONTROL) {
c->stdin_callback = control_callback;
- c->flags |= (CLIENT_CONTROL|CLIENT_SUSPENDED);
+ evbuffer_free(c->stderr_data);
+ c->stderr_data = c->stdout_data;
+ c->flags |= CLIENT_CONTROL;
+ if (data->flags & IDENTIFY_TERMIOS)
+ evbuffer_add_printf(c->stdout_data, "\033P1000p");
server_write_client(c, MSG_STDIN, NULL, 0);
c->tty.fd = -1;
@@ -944,7 +986,8 @@ server_client_msg_identify(
tty_resize(&c->tty);
- c->flags |= CLIENT_TERMINAL;
+ if (!(data->flags & IDENTIFY_CONTROL))
+ c->flags |= CLIENT_TERMINAL;
}
/* Handle shell message. */