aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/ui_client.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/nvim/ui_client.c')
-rw-r--r--src/nvim/ui_client.c144
1 files changed, 106 insertions, 38 deletions
diff --git a/src/nvim/ui_client.c b/src/nvim/ui_client.c
index a586fec3bf..378c0e4831 100644
--- a/src/nvim/ui_client.c
+++ b/src/nvim/ui_client.c
@@ -1,44 +1,126 @@
// This is an open source non-commercial project. Dear PVS-Studio, please check
// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
-#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
+#include <stdlib.h>
-#include "nvim/api/private/dispatch.h"
#include "nvim/api/private/helpers.h"
+#include "nvim/channel.h"
+#include "nvim/eval.h"
+#include "nvim/eval/typval_defs.h"
+#include "nvim/event/loop.h"
+#include "nvim/globals.h"
#include "nvim/highlight.h"
#include "nvim/log.h"
-#include "nvim/map.h"
+#include "nvim/main.h"
+#include "nvim/memory.h"
#include "nvim/msgpack_rpc/channel.h"
-#include "nvim/screen.h"
+#include "nvim/msgpack_rpc/channel_defs.h"
+#include "nvim/os/os_defs.h"
+#include "nvim/tui/tui.h"
#include "nvim/ui.h"
#include "nvim/ui_client.h"
-#include "nvim/vim.h"
+static TUIData *tui = NULL;
+
+// uncrustify:off
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "ui_client.c.generated.h"
-
# include "ui_events_client.generated.h"
#endif
+// uncrustify:on
+//
-void ui_client_init(uint64_t chan)
+uint64_t ui_client_start_server(int argc, char **argv)
{
- Array args = ARRAY_DICT_INIT;
- int width = Columns;
- int height = Rows;
- Dictionary opts = ARRAY_DICT_INIT;
+ varnumber_T exit_status;
+ char **args = xmalloc(((size_t)(2 + argc)) * sizeof(char *));
+ int args_idx = 0;
+ args[args_idx++] = xstrdup((const char *)get_vim_var_str(VV_PROGPATH));
+ args[args_idx++] = xstrdup("--embed");
+ for (int i = 1; i < argc; i++) {
+ args[args_idx++] = xstrdup(argv[i]);
+ }
+ args[args_idx++] = NULL;
+
+ CallbackReader on_err = CALLBACK_READER_INIT;
+ on_err.fwd_err = true;
+
+ Channel *channel = channel_job_start(args, CALLBACK_READER_INIT,
+ on_err, CALLBACK_NONE,
+ false, true, true, false, kChannelStdinPipe,
+ NULL, 0, 0, NULL, &exit_status);
+
+ // If stdin is not a pty, it is forwarded to the client.
+ // Replace stdin in the TUI process with the tty fd.
+ if (ui_client_forward_stdin) {
+ close(0);
+#ifdef MSWIN
+ os_open_conin_fd();
+#else
+ dup(stderr_isatty ? STDERR_FILENO : STDOUT_FILENO);
+#endif
+ }
- PUT(opts, "rgb", BOOLEAN_OBJ(true));
- PUT(opts, "ext_linegrid", BOOLEAN_OBJ(true));
- PUT(opts, "ext_termcolors", BOOLEAN_OBJ(true));
+ return channel->id;
+}
- ADD(args, INTEGER_OBJ((int)width));
- ADD(args, INTEGER_OBJ((int)height));
- ADD(args, DICTIONARY_OBJ(opts));
+void ui_client_run(bool remote_ui)
+ FUNC_ATTR_NORETURN
+{
+ int width, height;
+ char *term;
+ tui = tui_start(&width, &height, &term);
+
+ MAXSIZE_TEMP_ARRAY(args, 3);
+ ADD_C(args, INTEGER_OBJ(width));
+ ADD_C(args, INTEGER_OBJ(height));
+
+ MAXSIZE_TEMP_DICT(opts, 9);
+ PUT_C(opts, "rgb", BOOLEAN_OBJ(true));
+ PUT_C(opts, "ext_linegrid", BOOLEAN_OBJ(true));
+ PUT_C(opts, "ext_termcolors", BOOLEAN_OBJ(true));
+ if (term) {
+ PUT(opts, "term_name", STRING_OBJ(cstr_to_string(term)));
+ }
+ if (ui_client_bg_response != kNone) {
+ bool is_dark = (ui_client_bg_response == kTrue);
+ PUT_C(opts, "term_background", STRING_OBJ(cstr_as_string(is_dark ? "dark" : "light")));
+ }
+ PUT_C(opts, "term_colors", INTEGER_OBJ(t_colors));
+ if (!remote_ui) {
+ PUT_C(opts, "stdin_tty", BOOLEAN_OBJ(stdin_isatty));
+ PUT_C(opts, "stdout_tty", BOOLEAN_OBJ(stdout_isatty));
+ if (ui_client_forward_stdin) {
+ PUT_C(opts, "stdin_fd", INTEGER_OBJ(UI_CLIENT_STDIN_FD));
+ }
+ }
+ ADD_C(args, DICTIONARY_OBJ(opts));
+
+ rpc_send_event(ui_client_channel_id, "nvim_ui_attach", args);
+ ui_client_attached = true;
- rpc_send_event(chan, "nvim_ui_attach", args);
- ui_client_channel_id = chan;
+ // os_exit() will be invoked when the client channel detaches
+ while (true) {
+ LOOP_PROCESS_EVENTS(&main_loop, resize_events, -1);
+ }
+}
+
+void ui_client_stop(void)
+{
+ tui_stop(tui);
+}
+
+void ui_client_set_size(int width, int height)
+{
+ // The currently known size will be sent when attaching
+ if (ui_client_attached) {
+ MAXSIZE_TEMP_ARRAY(args, 2);
+ ADD_C(args, INTEGER_OBJ((int)width));
+ ADD_C(args, INTEGER_OBJ((int)height));
+ rpc_send_event(ui_client_channel_id, "nvim_ui_try_resize", args);
+ }
}
UIClientHandler ui_client_get_redraw_handler(const char *name, size_t name_len, Error *error)
@@ -55,26 +137,12 @@ UIClientHandler ui_client_get_redraw_handler(const char *name, size_t name_len,
/// async 'redraw' events, which are expected when nvim acts as an ui client.
/// get handled in msgpack_rpc/unpacker.c and directly dispatched to handlers
/// of specific ui events, like ui_client_event_grid_resize and so on.
-Object handle_ui_client_redraw(uint64_t channel_id, Array args, Error *error)
+Object handle_ui_client_redraw(uint64_t channel_id, Array args, Arena *arena, Error *error)
{
api_set_error(error, kErrorTypeValidation, "'redraw' cannot be sent as a request");
return NIL;
}
-/// run the main thread in ui client mode
-///
-/// This is just a stub. the full version will handle input, resizing, etc
-void ui_client_execute(uint64_t chan)
- FUNC_ATTR_NORETURN
-{
- while (true) {
- loop_poll_events(&main_loop, -1);
- multiqueue_process_events(resize_events);
- }
-
- getout(0);
-}
-
static HlAttrs ui_client_dict2hlattrs(Dictionary d, bool rgb)
{
Error err = ERROR_INIT;
@@ -83,7 +151,7 @@ static HlAttrs ui_client_dict2hlattrs(Dictionary d, bool rgb)
// TODO(bfredl): log "err"
return HLATTRS_INIT;
}
- return dict2hlattrs(&dict, true, NULL, &err);
+ return dict2hlattrs(&dict, rgb, NULL, &err);
}
void ui_client_event_grid_resize(Array args)
@@ -99,7 +167,7 @@ void ui_client_event_grid_resize(Array args)
Integer grid = args.items[0].data.integer;
Integer width = args.items[1].data.integer;
Integer height = args.items[2].data.integer;
- ui_call_grid_resize(grid, width, height);
+ tui_grid_resize(tui, grid, width, height);
if (grid_line_buf_size < (size_t)width) {
xfree(grid_line_buf_char);
@@ -125,6 +193,6 @@ void ui_client_event_raw_line(GridLineEvent *g)
// TODO(hlpr98): Accommodate other LineFlags when included in grid_line
LineFlags lineflags = 0;
- ui_call_raw_line(grid, row, startcol, endcol, clearcol, g->cur_attr, lineflags,
- (const schar_T *)grid_line_buf_char, grid_line_buf_attr);
+ tui_raw_line(tui, grid, row, startcol, endcol, clearcol, g->cur_attr, lineflags,
+ (const schar_T *)grid_line_buf_char, grid_line_buf_attr);
}