From 794d2744f33562326172801ddd729853e7135347 Mon Sep 17 00:00:00 2001 From: hlpr98 Date: Sat, 12 Mar 2022 21:08:29 +0100 Subject: feat(ui): implement ui_client event handlers --- src/nvim/ui_client.c | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) (limited to 'src/nvim/ui_client.c') diff --git a/src/nvim/ui_client.c b/src/nvim/ui_client.c index 4a435aac4d..b23ceefb6f 100644 --- a/src/nvim/ui_client.c +++ b/src/nvim/ui_client.c @@ -6,11 +6,22 @@ #include #include "nvim/vim.h" +#include "nvim/log.h" +#include "nvim/map.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" +#include "nvim/highlight.h" +#include "nvim/screen.h" + +static Map(String, ApiRedrawWrapper) redraw_methods = MAP_INIT; + +static void add_redraw_event_handler(String method, ApiRedrawWrapper handler) +{ + map_put(String, ApiRedrawWrapper)(&redraw_methods, method, handler); +} void ui_client_init(uint64_t chan) { @@ -68,3 +79,106 @@ void ui_client_execute(uint64_t chan) getout(0); } + +/// @param name Redraw method name +/// @param name_len name size (includes terminating NUL) +ApiRedrawWrapper get_redraw_event_handler(const char *name, size_t name_len, Error *error) +{ + String m = { .data = (char *)name, .size = name_len }; + ApiRedrawWrapper rv = + map_get(String, ApiRedrawWrapper)(&redraw_methods, m); + + if (!rv) { + api_set_error(error, kErrorTypeException, "Invalid method: %.*s", + m.size > 0 ? (int)m.size : (int)sizeof(""), + m.size > 0 ? m.data : ""); + } + return rv; +} + +static HlAttrs redraw_dict2hlattrs(Dictionary redraw_dict, bool rgb) +{ + Error err = ERROR_INIT; + Dict(highlight) dict = { 0 }; + if (!api_dict_to_keydict(&dict, KeyDict_highlight_get_field, redraw_dict, &err)) { + // TODO(bfredl): log "err" + return HLATTRS_INIT; + } + return dict2hlattrs(&dict, true, NULL, &err); +} + +#ifdef INCLUDE_GENERATED_DECLARATIONS +#include "ui_events_redraw.generated.h" +#endif + +void ui_redraw_event_grid_line(Array args) +{ + Integer grid = args.items[0].data.integer; + Integer row = args.items[1].data.integer; + Integer startcol = args.items[2].data.integer; + Array cells = args.items[3].data.array; + Integer endcol, clearcol, clearattr; + // TODO(hlpr98): Accomodate other LineFlags when included in grid_line + LineFlags lineflags = 0; + schar_T *chunk; + sattr_T *attrs; + size_t size_of_cells = cells.size; + size_t no_of_cells = size_of_cells; + endcol = startcol; + + // checking if clearcol > endcol + if (!STRCMP(cells.items[size_of_cells-1].data.array + .items[0].data.string.data, " ") + && cells.items[size_of_cells-1].data.array.size == 3) { + no_of_cells = size_of_cells - 1; + } + + // getting endcol + for (size_t i = 0; i < no_of_cells; i++) { + endcol++; + if (cells.items[i].data.array.size == 3) { + endcol += cells.items[i].data.array.items[2].data.integer - 1; + } + } + + if (!STRCMP(cells.items[size_of_cells-1].data.array + .items[0].data.string.data, " ") + && cells.items[size_of_cells-1].data.array.size == 3) { + clearattr = cells.items[size_of_cells-1].data.array.items[1].data.integer; + clearcol = endcol + cells.items[size_of_cells-1].data.array + .items[2].data.integer; + } else { + clearattr = 0; + clearcol = endcol; + } + + size_t ncells = (size_t)(endcol - startcol); + chunk = xmalloc(ncells * sizeof(schar_T) + 1); + attrs = xmalloc(ncells * sizeof(sattr_T) + 1); + + size_t j = 0; + size_t k = 0; + for (size_t i = 0; i < no_of_cells; i++) { + STRCPY(chunk[j++], cells.items[i].data.array.items[0].data.string.data); + if (cells.items[i].data.array.size == 3) { + // repeat present + for (size_t i_intr = 1; + i_intr < (size_t)cells.items[i].data.array.items[2].data.integer; + i_intr++) { + STRCPY(chunk[j++], cells.items[i].data.array.items[0].data.string.data); + attrs[k++] = (sattr_T)cells.items[i].data.array.items[1].data.integer; + } + } else if (cells.items[i].data.array.size == 2) { + // repeat = 1 but attrs != last_hl + attrs[k++] = (sattr_T)cells.items[i].data.array.items[1].data.integer; + } + if (j > k) { + // attrs == last_hl + attrs[k] = attrs[k-1]; + k++; + } + } + + ui_call_raw_line(grid, row, startcol, endcol, clearcol, clearattr, lineflags, + (const schar_T *)chunk, (const sattr_T *)attrs); +} -- cgit From 534edce3c4972d1c8da44fbcf60e7946c09a5612 Mon Sep 17 00:00:00 2001 From: bfredl Date: Sat, 12 Mar 2022 23:17:32 +0100 Subject: feat(ui): invoke ui client handlers --- src/nvim/ui_client.c | 39 ++++++++++++++++++--------------------- 1 file changed, 18 insertions(+), 21 deletions(-) (limited to 'src/nvim/ui_client.c') diff --git a/src/nvim/ui_client.c b/src/nvim/ui_client.c index b23ceefb6f..c13bfc2d07 100644 --- a/src/nvim/ui_client.c +++ b/src/nvim/ui_client.c @@ -26,21 +26,21 @@ static void add_redraw_event_handler(String method, ApiRedrawWrapper handler) void ui_client_init(uint64_t chan) { Array args = ARRAY_DICT_INIT; - int width = 80; - int height = 25; + int width = Columns; + int height = Rows; 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! + redraw_methods_table_init(); ui_client_channel_id = chan; } @@ -61,9 +61,22 @@ 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; + String name = call.items[0].data.string; + + ApiRedrawWrapper handler = map_get(String, ApiRedrawWrapper)(&redraw_methods, name); + if (!handler) { + ELOG("No redraw handler by name: %s", name.size ? name.data : ""); + continue; + } + + // fprintf(stderr, "%s: %zu\n", name.data, call.size-1); + + DLOG("Invoke redraw handler by name: %s", name.data); + for (size_t j = 1; j < call.size; j++) { + Array internal_call_args = call.items[j].data.array; + handler(internal_call_args); + } - fprintf(stderr, "%s: %zu\n", method_name, call.size-1); } return NIL; } @@ -80,22 +93,6 @@ void ui_client_execute(uint64_t chan) getout(0); } -/// @param name Redraw method name -/// @param name_len name size (includes terminating NUL) -ApiRedrawWrapper get_redraw_event_handler(const char *name, size_t name_len, Error *error) -{ - String m = { .data = (char *)name, .size = name_len }; - ApiRedrawWrapper rv = - map_get(String, ApiRedrawWrapper)(&redraw_methods, m); - - if (!rv) { - api_set_error(error, kErrorTypeException, "Invalid method: %.*s", - m.size > 0 ? (int)m.size : (int)sizeof(""), - m.size > 0 ? m.data : ""); - } - return rv; -} - static HlAttrs redraw_dict2hlattrs(Dictionary redraw_dict, bool rgb) { Error err = ERROR_INIT; -- cgit From ca23f2ed308b46665e5c8e677c4012cfa7453490 Mon Sep 17 00:00:00 2001 From: bfredl Date: Sun, 13 Mar 2022 14:57:57 +0100 Subject: refactor(ui): use "ui_client" instead of "redraw" as general prefix --- src/nvim/ui_client.c | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) (limited to 'src/nvim/ui_client.c') diff --git a/src/nvim/ui_client.c b/src/nvim/ui_client.c index c13bfc2d07..daae257027 100644 --- a/src/nvim/ui_client.c +++ b/src/nvim/ui_client.c @@ -16,11 +16,11 @@ #include "nvim/highlight.h" #include "nvim/screen.h" -static Map(String, ApiRedrawWrapper) redraw_methods = MAP_INIT; +static Map(String, UIClientHandler) ui_client_handlers = MAP_INIT; -static void add_redraw_event_handler(String method, ApiRedrawWrapper handler) +static void add_ui_client_event_handler(String method, UIClientHandler handler) { - map_put(String, ApiRedrawWrapper)(&redraw_methods, method, handler); + map_put(String, UIClientHandler)(&ui_client_handlers, method, handler); } void ui_client_init(uint64_t chan) @@ -40,17 +40,16 @@ void ui_client_init(uint64_t chan) rpc_send_event(chan, "nvim_ui_attach", args); msgpack_rpc_add_redraw(); // GAME! - redraw_methods_table_init(); + // TODO(bfredl): use a keyset instead + ui_client_methods_table_init(); 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) +/// This function will be called by handle_request (in msgpack_rpc/channel.c) /// The individual ui_events sent by the server are individually handled -/// by their respective handlers defined in ui_events_redraw.generated.h +/// by their respective handlers defined in ui_events_client.generated.h /// /// @note The "flush" event is called only once and only after handling all /// the other events @@ -63,21 +62,19 @@ Object ui_client_handle_redraw(uint64_t channel_id, Array args, Error *error) Array call = args.items[i].data.array; String name = call.items[0].data.string; - ApiRedrawWrapper handler = map_get(String, ApiRedrawWrapper)(&redraw_methods, name); + UIClientHandler handler = map_get(String, UIClientHandler)(&ui_client_handlers, name); if (!handler) { - ELOG("No redraw handler by name: %s", name.size ? name.data : ""); + ELOG("No ui client handler for %s", name.size ? name.data : ""); continue; } // fprintf(stderr, "%s: %zu\n", name.data, call.size-1); - - DLOG("Invoke redraw handler by name: %s", name.data); + DLOG("Invoke ui client handler for %s", name.data); for (size_t j = 1; j < call.size; j++) { - Array internal_call_args = call.items[j].data.array; - handler(internal_call_args); + handler(call.items[j].data.array); } - } + return NIL; } @@ -93,11 +90,11 @@ void ui_client_execute(uint64_t chan) getout(0); } -static HlAttrs redraw_dict2hlattrs(Dictionary redraw_dict, bool rgb) +static HlAttrs ui_client_dict2hlattrs(Dictionary d, bool rgb) { Error err = ERROR_INIT; Dict(highlight) dict = { 0 }; - if (!api_dict_to_keydict(&dict, KeyDict_highlight_get_field, redraw_dict, &err)) { + if (!api_dict_to_keydict(&dict, KeyDict_highlight_get_field, d, &err)) { // TODO(bfredl): log "err" return HLATTRS_INIT; } @@ -105,10 +102,10 @@ static HlAttrs redraw_dict2hlattrs(Dictionary redraw_dict, bool rgb) } #ifdef INCLUDE_GENERATED_DECLARATIONS -#include "ui_events_redraw.generated.h" +#include "ui_events_client.generated.h" #endif -void ui_redraw_event_grid_line(Array args) +void ui_client_event_grid_line(Array args) { Integer grid = args.items[0].data.integer; Integer row = args.items[1].data.integer; -- cgit From c0b4d931e12910f67cc3eade664247ea2d2bb913 Mon Sep 17 00:00:00 2001 From: bfredl Date: Sun, 13 Mar 2022 16:02:53 +0100 Subject: refactor(ui): make ui_client_event_grid_line typesafe --- src/nvim/ui_client.c | 131 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 80 insertions(+), 51 deletions(-) (limited to 'src/nvim/ui_client.c') diff --git a/src/nvim/ui_client.c b/src/nvim/ui_client.c index daae257027..3914a4e199 100644 --- a/src/nvim/ui_client.c +++ b/src/nvim/ui_client.c @@ -18,6 +18,11 @@ static Map(String, UIClientHandler) ui_client_handlers = MAP_INIT; +// Temporary buffer for converting a single grid_line event +static size_t buf_size = 0; +static schar_T *buf_char = NULL; +static sattr_T *buf_attr = NULL; + static void add_ui_client_event_handler(String method, UIClientHandler handler) { map_put(String, UIClientHandler)(&ui_client_handlers, method, handler); @@ -105,74 +110,98 @@ static HlAttrs ui_client_dict2hlattrs(Dictionary d, bool rgb) #include "ui_events_client.generated.h" #endif +void ui_client_event_grid_resize(Array args) +{ + // TODO: typesafe! + 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); + + if (buf_size < (size_t)width) { + xfree(buf_char); + xfree(buf_attr); + buf_size = (size_t)width; + buf_char = xmalloc(buf_size * sizeof(schar_T)); + buf_attr = xmalloc(buf_size * sizeof(sattr_T)); + } +} + void ui_client_event_grid_line(Array args) { + if (args.size < 4 + || args.items[0].type != kObjectTypeInteger + || args.items[1].type != kObjectTypeInteger + || args.items[2].type != kObjectTypeInteger + || args.items[3].type != kObjectTypeArray) { + goto error; + } + Integer grid = args.items[0].data.integer; Integer row = args.items[1].data.integer; Integer startcol = args.items[2].data.integer; Array cells = args.items[3].data.array; - Integer endcol, clearcol, clearattr; + + Integer endcol, clearcol; // TODO(hlpr98): Accomodate other LineFlags when included in grid_line LineFlags lineflags = 0; - schar_T *chunk; - sattr_T *attrs; - size_t size_of_cells = cells.size; - size_t no_of_cells = size_of_cells; endcol = startcol; - // checking if clearcol > endcol - if (!STRCMP(cells.items[size_of_cells-1].data.array - .items[0].data.string.data, " ") - && cells.items[size_of_cells-1].data.array.size == 3) { - no_of_cells = size_of_cells - 1; - } + size_t j = 0; + int cur_attr = 0; + int clear_attr = 0; + int clear_width = 0; + for (size_t i = 0; i < cells.size; i++) { + if (cells.items[i].type != kObjectTypeArray) { + goto error; + } + Array cell = cells.items[i].data.array; - // getting endcol - for (size_t i = 0; i < no_of_cells; i++) { - endcol++; - if (cells.items[i].data.array.size == 3) { - endcol += cells.items[i].data.array.items[2].data.integer - 1; + if (cell.size < 1 || cell.items[0].type != kObjectTypeString) { + goto error; + } + String sstring = cell.items[0].data.string; + + char *schar = sstring.data; + int repeat = 1; + if (cell.size >= 2) { + if (cell.items[1].type != kObjectTypeInteger + || cell.items[1].data.integer < 0) { + goto error; + } + cur_attr = (int)cell.items[1].data.integer; } - } - if (!STRCMP(cells.items[size_of_cells-1].data.array - .items[0].data.string.data, " ") - && cells.items[size_of_cells-1].data.array.size == 3) { - clearattr = cells.items[size_of_cells-1].data.array.items[1].data.integer; - clearcol = endcol + cells.items[size_of_cells-1].data.array - .items[2].data.integer; - } else { - clearattr = 0; - clearcol = endcol; - } + if (cell.size >= 3) { + if (cell.items[2].type != kObjectTypeInteger + || cell.items[2].data.integer < 0) { + goto error; + } + repeat = (int)cell.items[2].data.integer; + } - size_t ncells = (size_t)(endcol - startcol); - chunk = xmalloc(ncells * sizeof(schar_T) + 1); - attrs = xmalloc(ncells * sizeof(sattr_T) + 1); + if (i == cells.size - 1 && sstring.size == 1 && sstring.data[0] == ' ' && repeat > 1) { + clear_width = repeat; + break; + } - size_t j = 0; - size_t k = 0; - for (size_t i = 0; i < no_of_cells; i++) { - STRCPY(chunk[j++], cells.items[i].data.array.items[0].data.string.data); - if (cells.items[i].data.array.size == 3) { - // repeat present - for (size_t i_intr = 1; - i_intr < (size_t)cells.items[i].data.array.items[2].data.integer; - i_intr++) { - STRCPY(chunk[j++], cells.items[i].data.array.items[0].data.string.data); - attrs[k++] = (sattr_T)cells.items[i].data.array.items[1].data.integer; + for (int r = 0; r < repeat; r++) { + if (j >= buf_size) { + goto error; // _YIKES_ } - } else if (cells.items[i].data.array.size == 2) { - // repeat = 1 but attrs != last_hl - attrs[k++] = (sattr_T)cells.items[i].data.array.items[1].data.integer; - } - if (j > k) { - // attrs == last_hl - attrs[k] = attrs[k-1]; - k++; + STRCPY(buf_char[j], schar); + buf_attr[j++] = cur_attr; } } - ui_call_raw_line(grid, row, startcol, endcol, clearcol, clearattr, lineflags, - (const schar_T *)chunk, (const sattr_T *)attrs); + endcol = startcol + (int)j; + clearcol = endcol + clear_width; + clear_attr = cur_attr; + + ui_call_raw_line(grid, row, startcol, endcol, clearcol, clear_attr, lineflags, + buf_char, buf_attr); + return; + +error: + ELOG("malformatted 'grid_line' event"); } -- cgit From f01d203b70f426c1538813b3bacb4483e914ab44 Mon Sep 17 00:00:00 2001 From: bfredl Date: Tue, 15 Mar 2022 00:23:14 +0100 Subject: refactor(ui): make auto-generated ui client handlers typesafe --- src/nvim/ui_client.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'src/nvim/ui_client.c') diff --git a/src/nvim/ui_client.c b/src/nvim/ui_client.c index 3914a4e199..4fad3e0709 100644 --- a/src/nvim/ui_client.c +++ b/src/nvim/ui_client.c @@ -112,7 +112,14 @@ static HlAttrs ui_client_dict2hlattrs(Dictionary d, bool rgb) void ui_client_event_grid_resize(Array args) { - // TODO: typesafe! + if (args.size < 3 + || args.items[0].type != kObjectTypeInteger + || args.items[1].type != kObjectTypeInteger + || args.items[2].type != kObjectTypeInteger) { + ELOG("Error handling ui event 'grid_resize'"); + return; + } + Integer grid = args.items[0].data.integer; Integer width = args.items[1].data.integer; Integer height = args.items[2].data.integer; @@ -189,7 +196,7 @@ void ui_client_event_grid_line(Array args) if (j >= buf_size) { goto error; // _YIKES_ } - STRCPY(buf_char[j], schar); + STRLCPY(buf_char[j], schar, sizeof(schar_T)); buf_attr[j++] = cur_attr; } } @@ -199,9 +206,9 @@ void ui_client_event_grid_line(Array args) clear_attr = cur_attr; ui_call_raw_line(grid, row, startcol, endcol, clearcol, clear_attr, lineflags, - buf_char, buf_attr); + (const schar_T *)buf_char, (const sattr_T *)buf_attr); return; error: - ELOG("malformatted 'grid_line' event"); + ELOG("Error handling ui event 'grid_line'"); } -- cgit