1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
// 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 "nvim/api/private/dispatch.h"
#include "nvim/api/private/helpers.h"
#include "nvim/highlight.h"
#include "nvim/log.h"
#include "nvim/map.h"
#include "nvim/msgpack_rpc/channel.h"
#include "nvim/screen.h"
#include "nvim/ui.h"
#include "nvim/ui_client.h"
#include "nvim/vim.h"
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "ui_client.c.generated.h"
# include "ui_events_client.generated.h"
#endif
// 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;
void ui_client_init(uint64_t chan)
{
Array args = ARRAY_DICT_INIT;
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));
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);
ui_client_channel_id = chan;
}
UIClientHandler ui_client_get_redraw_handler(const char *name, size_t name_len, Error *error)
{
int hash = ui_client_handler_hash(name, name_len);
if (hash < 0) {
return (UIClientHandler){ NULL, NULL };
}
return event_handlers[hash];
}
/// Placeholder for _sync_ requests with 'redraw' method name
///
/// async 'redraw' events, which are expected when nvim acts as an ui client.
/// get handled in msgpack_rpc/unpacker.c and directy 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)
{
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;
Dict(highlight) dict = { 0 };
if (!api_dict_to_keydict(&dict, KeyDict_highlight_get_field, d, &err)) {
// TODO(bfredl): log "err"
return HLATTRS_INIT;
}
return dict2hlattrs(&dict, true, NULL, &err);
}
void ui_client_event_grid_resize(Array args)
{
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;
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;
// TODO(hlpr98): Accommodate other LineFlags when included in grid_line
LineFlags lineflags = 0;
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;
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 (cell.size >= 3) {
if (cell.items[2].type != kObjectTypeInteger
|| cell.items[2].data.integer < 0) {
goto error;
}
repeat = (int)cell.items[2].data.integer;
}
if (i == cells.size - 1 && sstring.size == 1 && sstring.data[0] == ' ' && repeat > 1) {
clear_width = repeat;
break;
}
for (int r = 0; r < repeat; r++) {
if (j >= buf_size) {
goto error; // _YIKES_
}
STRLCPY(buf_char[j], schar, sizeof(schar_T));
buf_attr[j++] = cur_attr;
}
}
Integer endcol = startcol + (int)j;
Integer clearcol = endcol + clear_width;
clear_attr = cur_attr;
ui_call_raw_line(grid, row, startcol, endcol, clearcol, clear_attr, lineflags,
(const schar_T *)buf_char, (const sattr_T *)buf_attr);
return;
error:
ELOG("Error handling ui event 'grid_line'");
}
|