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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
#include <assert.h>
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include "nvim/vim.h"
#include "nvim/ui.h"
#include "nvim/memory.h"
#include "nvim/map.h"
#include "nvim/msgpack_rpc/remote_ui.h"
#include "nvim/msgpack_rpc/channel.h"
#include "nvim/api/private/defs.h"
#include "nvim/api/private/helpers.h"
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "msgpack_rpc/remote_ui.c.generated.h"
#endif
typedef struct {
uint64_t channel_id;
Array buffer;
} UIData;
static PMap(uint64_t) *connected_uis = NULL;
void remote_ui_init(void)
{
connected_uis = pmap_new(uint64_t)();
}
Object remote_ui_attach(uint64_t channel_id, uint64_t request_id, Array args,
Error *error)
{
if (pmap_has(uint64_t)(connected_uis, channel_id)) {
api_set_error(error, Exception, _("UI already attached for channel"));
return NIL;
}
if (args.size != 2 || args.items[0].type != kObjectTypeInteger
|| args.items[1].type != kObjectTypeInteger
|| args.items[0].data.integer <= 0 || args.items[1].data.integer <= 0) {
api_set_error(error, Validation,
_("Arguments must be a pair of positive integers "
"representing the remote screen width/height"));
return NIL;
}
UIData *data = xmalloc(sizeof(UIData));
data->channel_id = channel_id;
data->buffer = (Array)ARRAY_DICT_INIT;
UI *ui = xcalloc(1, sizeof(UI));
ui->width = (int)args.items[0].data.integer;
ui->height = (int)args.items[1].data.integer;
ui->data = data;
ui->resize = remote_ui_resize;
ui->clear = remote_ui_clear;
ui->eol_clear = remote_ui_eol_clear;
ui->cursor_goto = remote_ui_cursor_goto;
ui->cursor_on = remote_ui_cursor_on;
ui->cursor_off = remote_ui_cursor_off;
ui->mouse_on = remote_ui_mouse_on;
ui->mouse_off = remote_ui_mouse_off;
ui->insert_mode = remote_ui_insert_mode;
ui->normal_mode = remote_ui_normal_mode;
ui->set_scroll_region = remote_ui_set_scroll_region;
ui->scroll = remote_ui_scroll;
ui->highlight_set = remote_ui_highlight_set;
ui->put = remote_ui_put;
ui->bell = remote_ui_bell;
ui->visual_bell = remote_ui_visual_bell;
ui->flush = remote_ui_flush;
ui->suspend = remote_ui_suspend;
pmap_put(uint64_t)(connected_uis, channel_id, ui);
ui_attach(ui);
return NIL;
}
Object remote_ui_detach(uint64_t channel_id, uint64_t request_id, Array args,
Error *error)
{
if (!pmap_has(uint64_t)(connected_uis, channel_id)) {
api_set_error(error, Exception, _("UI is not attached for channel"));
}
remote_ui_disconnect(channel_id);
return NIL;
}
void remote_ui_disconnect(uint64_t channel_id)
{
UI *ui = pmap_get(uint64_t)(connected_uis, channel_id);
if (!ui) {
return;
}
UIData *data = ui->data;
// destroy pending screen updates
api_free_array(data->buffer);
pmap_del(uint64_t)(connected_uis, channel_id);
free(ui->data);
ui_detach(ui);
free(ui);
}
static void push_call(UI *ui, char *name, Array args)
{
Array call = ARRAY_DICT_INIT;
UIData *data = ui->data;
// To optimize data transfer(especially for "put"), we bundle adjacent
// calls to same method together, so only add a new call entry if the last
// method call is different from "name"
if (kv_size(data->buffer)) {
call = kv_A(data->buffer, kv_size(data->buffer) - 1).data.array;
}
if (!kv_size(call) || strcmp(kv_A(call, 0).data.string.data, name)) {
call = (Array)ARRAY_DICT_INIT;
ADD(data->buffer, ARRAY_OBJ(call));
ADD(call, STRING_OBJ(cstr_to_string(name)));
}
ADD(call, ARRAY_OBJ(args));
kv_A(data->buffer, kv_size(data->buffer) - 1).data.array = call;
}
static void remote_ui_resize(UI *ui, int width, int height)
{
Array args = ARRAY_DICT_INIT;
ADD(args, INTEGER_OBJ(width));
ADD(args, INTEGER_OBJ(height));
push_call(ui, "resize", args);
}
static void remote_ui_clear(UI *ui)
{
Array args = ARRAY_DICT_INIT;
push_call(ui, "clear", args);
}
static void remote_ui_eol_clear(UI *ui)
{
Array args = ARRAY_DICT_INIT;
push_call(ui, "eol_clear", args);
}
static void remote_ui_cursor_goto(UI *ui, int row, int col)
{
Array args = ARRAY_DICT_INIT;
ADD(args, INTEGER_OBJ(row));
ADD(args, INTEGER_OBJ(col));
push_call(ui, "cursor_goto", args);
}
static void remote_ui_cursor_on(UI *ui)
{
Array args = ARRAY_DICT_INIT;
push_call(ui, "cursor_on", args);
}
static void remote_ui_cursor_off(UI *ui)
{
Array args = ARRAY_DICT_INIT;
push_call(ui, "cursor_off", args);
}
static void remote_ui_mouse_on(UI *ui)
{
Array args = ARRAY_DICT_INIT;
push_call(ui, "mouse_on", args);
}
static void remote_ui_mouse_off(UI *ui)
{
Array args = ARRAY_DICT_INIT;
push_call(ui, "mouse_off", args);
}
static void remote_ui_insert_mode(UI *ui)
{
Array args = ARRAY_DICT_INIT;
push_call(ui, "insert_mode", args);
}
static void remote_ui_normal_mode(UI *ui)
{
Array args = ARRAY_DICT_INIT;
push_call(ui, "normal_mode", args);
}
static void remote_ui_set_scroll_region(UI *ui, int top, int bot, int left,
int right)
{
Array args = ARRAY_DICT_INIT;
ADD(args, INTEGER_OBJ(top));
ADD(args, INTEGER_OBJ(bot));
ADD(args, INTEGER_OBJ(left));
ADD(args, INTEGER_OBJ(right));
push_call(ui, "set_scroll_region", args);
}
static void remote_ui_scroll(UI *ui, int count)
{
Array args = ARRAY_DICT_INIT;
ADD(args, INTEGER_OBJ(count));
push_call(ui, "scroll", args);
}
static void remote_ui_highlight_set(UI *ui, HlAttrs attrs)
{
Array args = ARRAY_DICT_INIT;
Dictionary hl = ARRAY_DICT_INIT;
if (attrs.bold) {
PUT(hl, "bold", BOOLEAN_OBJ(true));
}
if (attrs.standout) {
PUT(hl, "standout", BOOLEAN_OBJ(true));
}
if (attrs.underline) {
PUT(hl, "underline", BOOLEAN_OBJ(true));
}
if (attrs.undercurl) {
PUT(hl, "undercurl", BOOLEAN_OBJ(true));
}
if (attrs.italic) {
PUT(hl, "italic", BOOLEAN_OBJ(true));
}
if (attrs.reverse) {
PUT(hl, "reverse", BOOLEAN_OBJ(true));
}
if (attrs.foreground != -1) {
PUT(hl, "foreground", INTEGER_OBJ(attrs.foreground));
}
if (attrs.background != -1) {
PUT(hl, "background", INTEGER_OBJ(attrs.background));
}
ADD(args, DICTIONARY_OBJ(hl));
push_call(ui, "highlight_set", args);
}
static void remote_ui_put(UI *ui, uint8_t *data, size_t size)
{
Array args = ARRAY_DICT_INIT;
String str = {.data = xmemdupz(data, size), .size = size};
ADD(args, STRING_OBJ(str));
push_call(ui, "put", args);
}
static void remote_ui_bell(UI *ui)
{
Array args = ARRAY_DICT_INIT;
push_call(ui, "bell", args);
}
static void remote_ui_visual_bell(UI *ui)
{
Array args = ARRAY_DICT_INIT;
push_call(ui, "visual_bell", args);
}
static void remote_ui_flush(UI *ui)
{
UIData *data = ui->data;
channel_send_event(data->channel_id, "redraw", data->buffer);
data->buffer = (Array)ARRAY_DICT_INIT;
}
static void remote_ui_suspend(UI *ui)
{
UIData *data = ui->data;
remote_ui_disconnect(data->channel_id);
}
|