aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbfredl <bjorn.linse@gmail.com>2022-03-12 13:47:50 +0100
committerbfredl <bjorn.linse@gmail.com>2022-03-12 19:24:46 +0100
commita4400bf8cda8ace4c4aab67bc73a1820478f46f1 (patch)
tree1b63176d66c1e09a030499971591cf20c172c390
parent3a12737e6c13e9be774483f34655e7ac96e36c09 (diff)
downloadrneovim-a4400bf8cda8ace4c4aab67bc73a1820478f46f1.tar.gz
rneovim-a4400bf8cda8ace4c4aab67bc73a1820478f46f1.tar.bz2
rneovim-a4400bf8cda8ace4c4aab67bc73a1820478f46f1.zip
feat(ui): connect to remote ui (only debug messages for now)
co-authored-by: hlpr98 <hlpr98@gmail.com>
-rw-r--r--src/nvim/api/private/dispatch.c8
-rw-r--r--src/nvim/globals.h3
-rw-r--r--src/nvim/main.c18
-rw-r--r--src/nvim/msgpack_rpc/channel.c5
-rw-r--r--src/nvim/ui_client.c70
-rw-r--r--src/nvim/ui_client.h9
6 files changed, 109 insertions, 4 deletions
diff --git a/src/nvim/api/private/dispatch.c b/src/nvim/api/private/dispatch.c
index f670f06357..ba2e560d63 100644
--- a/src/nvim/api/private/dispatch.c
+++ b/src/nvim/api/private/dispatch.c
@@ -30,6 +30,7 @@
#include "nvim/api/vimscript.h"
#include "nvim/api/win_config.h"
#include "nvim/api/window.h"
+#include "nvim/ui_client.h"
static Map(String, MsgpackRpcRequestHandler) methods = MAP_INIT;
@@ -38,6 +39,13 @@ static void msgpack_rpc_add_method_handler(String method, MsgpackRpcRequestHandl
map_put(String, MsgpackRpcRequestHandler)(&methods, method, handler);
}
+void msgpack_rpc_add_redraw(void)
+{
+ msgpack_rpc_add_method_handler(STATIC_CSTR_AS_STRING("redraw"),
+ (MsgpackRpcRequestHandler) { .fn = ui_client_handle_redraw,
+ .fast = true });
+}
+
/// @param name API method name
/// @param name_len name size (includes terminating NUL)
MsgpackRpcRequestHandler msgpack_rpc_get_handler_for(const char *name, size_t name_len,
diff --git a/src/nvim/globals.h b/src/nvim/globals.h
index 35ad57906b..b64ed7c758 100644
--- a/src/nvim/globals.h
+++ b/src/nvim/globals.h
@@ -342,6 +342,9 @@ EXTERN sctx_T current_sctx INIT(= { 0 COMMA 0 COMMA 0 });
// ID of the current channel making a client API call
EXTERN uint64_t current_channel_id INIT(= 0);
+// ID of the client channel. Used by ui client
+EXTERN uint64_t ui_client_channel_id INIT(= 0);
+
EXTERN bool did_source_packages INIT(= false);
// Scope information for the code that indirectly triggered the current
diff --git a/src/nvim/main.c b/src/nvim/main.c
index f762160c05..95ef306745 100644
--- a/src/nvim/main.c
+++ b/src/nvim/main.c
@@ -29,6 +29,7 @@
#include "nvim/if_cscope.h"
#include "nvim/lua/executor.h"
#include "nvim/main.h"
+#include "nvim/ui_client.h"
#include "nvim/vim.h"
#ifdef HAVE_LOCALE_H
# include <locale.h>
@@ -269,8 +270,7 @@ int main(int argc, char **argv)
server_init(params.listen_addr);
if (params.remote) {
- remote_request(&params, params.remote,
- params.server_addr, argc, argv);
+ remote_request(&params, params.remote, params.server_addr, argc, argv);
}
if (GARGCOUNT > 0) {
@@ -834,10 +834,20 @@ static void remote_request(mparm_T *params, int remote_args,
uint64_t chan = server_connect(server_addr, &connect_error);
Object rvobj = OBJECT_INIT;
- int t_argc = remote_args;
+ if (strequal(argv[remote_args], "--remote-ui-test")) {
+ if (!chan) {
+ emsg(connect_error);
+ exit(1);
+ }
+
+ ui_client_init(chan);
+ ui_client_execute(chan);
+ abort(); // unreachable
+ }
+
Array args = ARRAY_DICT_INIT;
String arg_s;
- for (; t_argc < argc; t_argc++) {
+ for (int t_argc = remote_args; t_argc < argc; t_argc++) {
arg_s = cstr_to_string(argv[t_argc]);
ADD(args, STRING_OBJ(arg_s));
}
diff --git a/src/nvim/msgpack_rpc/channel.c b/src/nvim/msgpack_rpc/channel.c
index 299651ee97..f4e836fa81 100644
--- a/src/nvim/msgpack_rpc/channel.c
+++ b/src/nvim/msgpack_rpc/channel.c
@@ -547,6 +547,11 @@ void rpc_close(Channel *channel)
channel->rpc.closed = true;
channel_decref(channel);
+ if (channel->id == ui_client_channel_id) {
+ // TODO(bfredl): handle this in ui_client, where os_exit() is safe
+ exit(0);
+ }
+
if (channel->streamtype == kChannelStreamStdio) {
multiqueue_put(main_loop.fast_events, exit_event, 0);
}
diff --git a/src/nvim/ui_client.c b/src/nvim/ui_client.c
new file mode 100644
index 0000000000..4a435aac4d
--- /dev/null
+++ b/src/nvim/ui_client.c
@@ -0,0 +1,70 @@
+// 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 <stdbool.h>
+#include <stdint.h>
+#include <assert.h>
+
+#include "nvim/vim.h"
+#include "nvim/ui_client.h"
+#include "nvim/api/private/helpers.h"
+#include "nvim/msgpack_rpc/channel.h"
+#include "nvim/api/private/dispatch.h"
+#include "nvim/ui.h"
+
+void ui_client_init(uint64_t chan)
+{
+ Array args = ARRAY_DICT_INIT;
+ int width = 80;
+ int height = 25;
+ Dictionary opts = ARRAY_DICT_INIT;
+
+ PUT(opts, "rgb", BOOLEAN_OBJ(true));
+ PUT(opts, "ext_linegrid", BOOLEAN_OBJ(true));
+ PUT(opts, "ext_termcolors", BOOLEAN_OBJ(true));
+
+ // TODO(bfredl): use the size of the client UI
+ ADD(args, INTEGER_OBJ((int)width));
+ ADD(args, INTEGER_OBJ((int)height));
+ ADD(args, DICTIONARY_OBJ(opts));
+
+ rpc_send_event(chan, "nvim_ui_attach", args);
+ msgpack_rpc_add_redraw(); // GAME!
+ ui_client_channel_id = chan;
+}
+
+/// Handler for "redraw" events sent by the NVIM server
+///
+/// This is just a stub. The mentioned functionality will be implemented.
+///
+/// This function will be called by handle_request (in msgpack_rpc/channle.c)
+/// The individual ui_events sent by the server are individually handled
+/// by their respective handlers defined in ui_events_redraw.generated.h
+///
+/// @note The "flush" event is called only once and only after handling all
+/// the other events
+/// @param channel_id: The id of the rpc channel
+/// @param uidata: The dense array containing the ui_events sent by the server
+/// @param[out] err Error details, if any
+Object ui_client_handle_redraw(uint64_t channel_id, Array args, Error *error)
+{
+ for (size_t i = 0; i < args.size; i++) {
+ Array call = args.items[i].data.array;
+ char *method_name = call.items[0].data.string.data;
+
+ fprintf(stderr, "%s: %zu\n", method_name, call.size-1);
+ }
+ 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)
+{
+ while (true) {
+ loop_poll_events(&main_loop, -1);
+ }
+
+ getout(0);
+}
diff --git a/src/nvim/ui_client.h b/src/nvim/ui_client.h
new file mode 100644
index 0000000000..067f78d5c5
--- /dev/null
+++ b/src/nvim/ui_client.h
@@ -0,0 +1,9 @@
+#ifndef NVIM_UI_CLIENT_H
+#define NVIM_UI_CLIENT_H
+
+#include "nvim/api/private/defs.h"
+
+#ifdef INCLUDE_GENERATED_DECLARATIONS
+#include "ui_client.h.generated.h"
+#endif
+#endif // NVIM_UI_CLIENT_H