diff --git a/cmd-paste-buffer.c b/cmd-paste-buffer.c index 1d92557f..e8e638ef 100644 --- a/cmd-paste-buffer.c +++ b/cmd-paste-buffer.c @@ -27,7 +27,7 @@ */ int cmd_paste_buffer_exec(struct cmd *, struct cmd_ctx *); -void cmd_paste_buffer_lf2cr(struct buffer *, const char *, size_t); +void cmd_paste_buffer_lf2cr(struct window_pane *, const char *, size_t); const struct cmd_entry cmd_paste_buffer_entry = { "paste-buffer", "pasteb", @@ -65,9 +65,9 @@ cmd_paste_buffer_exec(struct cmd *self, struct cmd_ctx *ctx) if (pb != NULL && *pb->data != '\0') { /* -r means raw data without LF->CR conversion. */ if (data->chflags & CMD_CHFLAG('r')) - buffer_write(wp->out, pb->data, pb->size); + bufferevent_write(wp->event, pb->data, pb->size); else - cmd_paste_buffer_lf2cr(wp->out, pb->data, pb->size); + cmd_paste_buffer_lf2cr(wp, pb->data, pb->size); } /* Delete the buffer if -d. */ @@ -83,18 +83,18 @@ cmd_paste_buffer_exec(struct cmd *self, struct cmd_ctx *ctx) /* Add bytes to a buffer but change every '\n' to '\r'. */ void -cmd_paste_buffer_lf2cr(struct buffer *b, const char *data, size_t size) +cmd_paste_buffer_lf2cr(struct window_pane *wp, const char *data, size_t size) { const char *end = data + size; const char *lf; while ((lf = memchr(data, '\n', end - data)) != NULL) { if (lf != data) - buffer_write(b, data, lf - data); - buffer_write8(b, '\r'); + bufferevent_write(wp->event, data, lf - data); + bufferevent_write(wp->event, "\r", 1); data = lf + 1; } if (end != data) - buffer_write(b, data, end - data); + bufferevent_write(wp->event, data, end - data); } diff --git a/cmd-pipe-pane.c b/cmd-pipe-pane.c index 4565174e..d0b0fcca 100644 --- a/cmd-pipe-pane.c +++ b/cmd-pipe-pane.c @@ -113,7 +113,7 @@ cmd_pipe_pane_exec(struct cmd *self, struct cmd_ctx *ctx) close(pipe_fd[1]); wp->pipe_fd = pipe_fd[0]; - wp->pipe_off = BUFFER_USED(wp->in); + wp->pipe_off = EVBUFFER_LENGTH(wp->event->input); wp->pipe_event = bufferevent_new(wp->pipe_fd, NULL, NULL, cmd_pipe_pane_error_callback, wp); diff --git a/input-keys.c b/input-keys.c index 627b22cf..e8a163a0 100644 --- a/input-keys.c +++ b/input-keys.c @@ -163,6 +163,7 @@ input_key(struct window_pane *wp, int key) u_int i; size_t dlen; char *out; + u_char ch; log_debug2("writing key 0x%x", key); @@ -172,8 +173,10 @@ input_key(struct window_pane *wp, int key) */ if (key != KEYC_NONE && (key & ~KEYC_ESCAPE) < 0x100) { if (key & KEYC_ESCAPE) - buffer_write8(wp->out, '\033'); - buffer_write8(wp->out, (uint8_t) (key & ~KEYC_ESCAPE)); + ch = '\033'; + else + ch = key & ~KEYC_ESCAPE; + bufferevent_write(wp->event, &ch, 1); return; } @@ -183,7 +186,7 @@ input_key(struct window_pane *wp, int key) */ if (options_get_number(&wp->window->options, "xterm-keys")) { if ((out = xterm_keys_lookup(key)) != NULL) { - buffer_write(wp->out, out, strlen(out)); + bufferevent_write(wp->event, out, strlen(out)); xfree(out); return; } @@ -214,18 +217,19 @@ input_key(struct window_pane *wp, int key) /* Prefix a \033 for escape. */ if (key & KEYC_ESCAPE) - buffer_write8(wp->out, '\033'); - buffer_write(wp->out, ike->data, dlen); + bufferevent_write(wp->event, "\033", 1); + bufferevent_write(wp->event, ike->data, dlen); } /* Translate mouse and output. */ void input_mouse(struct window_pane *wp, struct mouse_event *m) { + char out[8]; + if (wp->screen->mode & MODE_MOUSE) { - buffer_write(wp->out, "\033[M", 3); - buffer_write8(wp->out, m->b + 32); - buffer_write8(wp->out, m->x + 33); - buffer_write8(wp->out, m->y + 33); + xsnprintf(out, sizeof out, + "\033[M%c%c%c", m->b + 32, m->x + 33, m->y + 33); + bufferevent_write(wp->event, out, strlen(out)); } } diff --git a/input.c b/input.c index f0a566e5..b846e060 100644 --- a/input.c +++ b/input.c @@ -256,12 +256,12 @@ input_parse(struct window_pane *wp) struct input_ctx *ictx = &wp->ictx; u_char ch; - if (BUFFER_USED(wp->in) == ictx->was) + if (EVBUFFER_LENGTH(wp->event->input) == ictx->was) return; wp->window->flags |= WINDOW_ACTIVITY; - ictx->buf = BUFFER_OUT(wp->in); - ictx->len = BUFFER_USED(wp->in); + ictx->buf = EVBUFFER_DATA(wp->event->input); + ictx->len = EVBUFFER_LENGTH(wp->event->input); ictx->off = 0; ictx->wp = wp; @@ -278,8 +278,8 @@ input_parse(struct window_pane *wp) screen_write_stop(&ictx->ctx); - buffer_remove(wp->in, ictx->len); - ictx->was = BUFFER_USED(wp->in); + evbuffer_drain(wp->event->input, ictx->len); + ictx->was = EVBUFFER_LENGTH(wp->event->input); } void @@ -932,7 +932,8 @@ input_handle_sequence_cbt(struct input_ctx *ictx) void input_handle_sequence_da(struct input_ctx *ictx) { - uint16_t n; + struct window_pane *wp = ictx->wp; + uint16_t n; if (ictx->private != '\0') return; @@ -944,7 +945,7 @@ input_handle_sequence_da(struct input_ctx *ictx) if (n != 0) return; - buffer_write(ictx->wp->out, "\033[?1;2c", (sizeof "\033[?1;2c") - 1); + bufferevent_write(wp->event, "\033[?1;2c", (sizeof "\033[?1;2c") - 1); } void @@ -1314,9 +1315,10 @@ input_handle_sequence_rm(struct input_ctx *ictx) void input_handle_sequence_dsr(struct input_ctx *ictx) { - struct screen *s = ictx->ctx.s; - uint16_t n; - char reply[32]; + struct window_pane *wp = ictx->wp; + struct screen *s = ictx->ctx.s; + uint16_t n; + char reply[32]; if (ARRAY_LENGTH(&ictx->args) > 1) return; @@ -1329,7 +1331,7 @@ input_handle_sequence_dsr(struct input_ctx *ictx) xsnprintf(reply, sizeof reply, "\033[%u;%uR", s->cy + 1, s->cx + 1); log_debug("cursor request, reply: %s", reply); - buffer_write(ictx->wp->out, reply, strlen(reply)); + bufferevent_write(wp->event, reply, strlen(reply)); break; } } diff --git a/server-window.c b/server-window.c index bc6f0b4f..d0eb7960 100644 --- a/server-window.c +++ b/server-window.c @@ -30,35 +30,6 @@ int server_window_check_content( struct session *, struct window *, struct window_pane *); void server_window_check_alive(struct window *); -/* Register windows for poll. */ -void -server_window_prepare(void) -{ - struct window *w; - struct window_pane *wp; - u_int i; - int events; - - for (i = 0; i < ARRAY_LENGTH(&windows); i++) { - if ((w = ARRAY_ITEM(&windows, i)) == NULL) - continue; - - TAILQ_FOREACH(wp, &w->panes, entry) { - if (wp->fd == -1) - continue; - events = 0; - if (!server_window_backoff(wp)) - events |= EV_READ; - if (BUFFER_USED(wp->out) > 0) - events |= EV_WRITE; - event_del(&wp->event); - event_set(&wp->event, - wp->fd, events, server_window_callback, wp); - event_add(&wp->event, NULL); - } - } -} - /* Check if this window should suspend reading. */ int server_window_backoff(struct window_pane *wp) @@ -84,24 +55,6 @@ server_window_backoff(struct window_pane *wp) return (0); } -/* Process a single window pane event. */ -void -server_window_callback(int fd, short events, void *data) -{ - struct window_pane *wp = data; - - if (wp->fd == -1) - return; - - if (fd == wp->fd) { - if (buffer_poll(fd, events, wp->in, wp->out) != 0) { - close(wp->fd); - wp->fd = -1; - } else - window_pane_parse(wp); - } -} - /* Window functions that need to happen every loop. */ void server_window_loop(void) @@ -116,6 +69,13 @@ server_window_loop(void) if (w == NULL) continue; + TAILQ_FOREACH(wp, &w->panes, entry) { + if (server_window_backoff(wp)) + bufferevent_disable(wp->event, EV_READ); + else + bufferevent_enable(wp->event, EV_READ); + } + for (j = 0; j < ARRAY_LENGTH(&sessions); j++) { s = ARRAY_ITEM(&sessions, j); if (s == NULL || !session_has(s, w)) diff --git a/server.c b/server.c index bd6c28c3..b4c5a8d2 100644 --- a/server.c +++ b/server.c @@ -214,7 +214,6 @@ server_loop(void) while (!server_should_shutdown()) { server_update_socket(); - server_window_prepare(); server_client_prepare(); event_loopexit(&tv); diff --git a/tmux.h b/tmux.h index 3edc6ec8..8940d381 100644 --- a/tmux.h +++ b/tmux.h @@ -798,9 +798,7 @@ struct window_pane { char tty[TTY_NAME_MAX]; int fd; - struct event event; - struct buffer *in; - struct buffer *out; + struct bufferevent *event; struct input_ctx ictx; @@ -1586,8 +1584,6 @@ void server_client_callback(int, short, void *); void server_client_loop(void); /* server-window.c */ -void server_window_prepare(void); -void server_window_callback(int, short, void *); void server_window_loop(void); /* server-fn.c */ diff --git a/window.c b/window.c index 293a1c63..fca94df8 100644 --- a/window.c +++ b/window.c @@ -56,6 +56,9 @@ /* Global window list. */ struct windows windows; +void window_pane_read_callback(struct bufferevent *, void *); +void window_pane_error_callback(struct bufferevent *, short, void *); + RB_GENERATE(winlinks, winlink, entry, winlink_cmp); int @@ -412,8 +415,7 @@ window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit) wp->cwd = NULL; wp->fd = -1; - wp->in = buffer_create(BUFSIZ); - wp->out = buffer_create(BUFSIZ); + wp->event = NULL; wp->mode = NULL; @@ -442,8 +444,10 @@ window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit) void window_pane_destroy(struct window_pane *wp) { - if (wp->fd != -1) + if (wp->fd != -1) { close(wp->fd); + bufferevent_free(wp->event); + } input_free(wp); @@ -457,10 +461,6 @@ window_pane_destroy(struct window_pane *wp) bufferevent_free(wp->pipe_event); } - buffer_destroy(wp->in); - buffer_destroy(wp->out); - event_del(&wp->event); - if (wp->cwd != NULL) xfree(wp->cwd); if (wp->shell != NULL) @@ -484,8 +484,10 @@ window_pane_spawn(struct window_pane *wp, const char *cmd, const char *shell, struct termios tio2; u_int i; - if (wp->fd != -1) + if (wp->fd != -1) { close(wp->fd); + bufferevent_free(wp->event); + } if (cmd != NULL) { if (wp->cmd != NULL) xfree(wp->cmd); @@ -574,10 +576,32 @@ window_pane_spawn(struct window_pane *wp, const char *cmd, const char *shell, fatal("fcntl failed"); if (fcntl(wp->fd, F_SETFD, FD_CLOEXEC) == -1) fatal("fcntl failed"); + wp->event = bufferevent_new(wp->fd, + window_pane_read_callback, NULL, window_pane_error_callback, wp); + bufferevent_enable(wp->event, EV_READ|EV_WRITE); return (0); } +void +window_pane_read_callback(unused struct bufferevent *bufev, void *data) +{ + struct window_pane *wp = data; + + window_pane_parse(wp); +} + +void +window_pane_error_callback( + unused struct bufferevent *bufev, unused short what, void *data) +{ + struct window_pane *wp = data; + + close(wp->fd); + bufferevent_free(wp->event); + wp->fd = -1; +} + void window_pane_resize(struct window_pane *wp, u_int sx, u_int sy) { @@ -631,18 +655,21 @@ window_pane_reset_mode(struct window_pane *wp) void window_pane_parse(struct window_pane *wp) { + char *data; size_t new_size; if (wp->mode != NULL) return; - new_size = BUFFER_USED(wp->in) - wp->pipe_off; - if (wp->pipe_fd != -1 && new_size > 0) - bufferevent_write(wp->pipe_event, BUFFER_OUT(wp->in), new_size); + new_size = EVBUFFER_LENGTH(wp->event->input) - wp->pipe_off; + if (wp->pipe_fd != -1 && new_size > 0) { + data = EVBUFFER_DATA(wp->event->input); + bufferevent_write(wp->pipe_event, data, new_size); + } input_parse(wp); - wp->pipe_off = BUFFER_USED(wp->in); + wp->pipe_off = EVBUFFER_LENGTH(wp->event->input); } void