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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
|
// UI wrapper that sends requests to the UI thread.
// Used by the built-in TUI and libnvim-based UIs.
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <limits.h>
#include "nvim/log.h"
#include "nvim/main.h"
#include "nvim/vim.h"
#include "nvim/ui.h"
#include "nvim/memory.h"
#include "nvim/ui_bridge.h"
#include "nvim/ugrid.h"
#include "nvim/api/private/helpers.h"
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "ui_bridge.c.generated.h"
#endif
#define UI(b) (((UIBridgeData *)b)->ui)
#if MIN_LOG_LEVEL <= DEBUG_LOG_LEVEL
static size_t uilog_seen = 0;
static argv_callback uilog_event = NULL;
#define UI_CALL(ui, name, argc, ...) \
do { \
if (uilog_event == ui_bridge_##name##_event) { \
uilog_seen++; \
} else { \
if (uilog_seen > 0) { \
DLOG("UI bridge: ...%zu times", uilog_seen); \
} \
DLOG("UI bridge: " STR(name)); \
uilog_seen = 0; \
uilog_event = ui_bridge_##name##_event; \
} \
((UIBridgeData *)ui)->scheduler( \
event_create(1, ui_bridge_##name##_event, argc, __VA_ARGS__), UI(ui)); \
} while (0)
#else
// Schedule a function call on the UI bridge thread.
#define UI_CALL(ui, name, argc, ...) \
((UIBridgeData *)ui)->scheduler( \
event_create(1, ui_bridge_##name##_event, argc, __VA_ARGS__), UI(ui))
#endif
#define INT2PTR(i) ((void *)(uintptr_t)i)
#define PTR2INT(p) ((int)(uintptr_t)p)
UI *ui_bridge_attach(UI *ui, ui_main_fn ui_main, event_scheduler scheduler)
{
UIBridgeData *rv = xcalloc(1, sizeof(UIBridgeData));
rv->ui = ui;
rv->bridge.rgb = ui->rgb;
rv->bridge.pum_external = ui->pum_external;
rv->bridge.stop = ui_bridge_stop;
rv->bridge.resize = ui_bridge_resize;
rv->bridge.clear = ui_bridge_clear;
rv->bridge.eol_clear = ui_bridge_eol_clear;
rv->bridge.cursor_goto = ui_bridge_cursor_goto;
rv->bridge.cursor_style_set = ui_bridge_cursor_styleset;
rv->bridge.update_menu = ui_bridge_update_menu;
rv->bridge.busy_start = ui_bridge_busy_start;
rv->bridge.busy_stop = ui_bridge_busy_stop;
rv->bridge.mouse_on = ui_bridge_mouse_on;
rv->bridge.mouse_off = ui_bridge_mouse_off;
rv->bridge.mode_change = ui_bridge_mode_change;
rv->bridge.set_scroll_region = ui_bridge_set_scroll_region;
rv->bridge.scroll = ui_bridge_scroll;
rv->bridge.highlight_set = ui_bridge_highlight_set;
rv->bridge.put = ui_bridge_put;
rv->bridge.bell = ui_bridge_bell;
rv->bridge.visual_bell = ui_bridge_visual_bell;
rv->bridge.update_fg = ui_bridge_update_fg;
rv->bridge.update_bg = ui_bridge_update_bg;
rv->bridge.update_sp = ui_bridge_update_sp;
rv->bridge.flush = ui_bridge_flush;
rv->bridge.suspend = ui_bridge_suspend;
rv->bridge.set_title = ui_bridge_set_title;
rv->bridge.set_icon = ui_bridge_set_icon;
rv->scheduler = scheduler;
rv->ui_main = ui_main;
uv_mutex_init(&rv->mutex);
uv_cond_init(&rv->cond);
uv_mutex_lock(&rv->mutex);
rv->ready = false;
if (uv_thread_create(&rv->ui_thread, ui_thread_run, rv)) {
abort();
}
while (!rv->ready) {
uv_cond_wait(&rv->cond, &rv->mutex);
}
uv_mutex_unlock(&rv->mutex);
ui_attach_impl(&rv->bridge);
return &rv->bridge;
}
void ui_bridge_stopped(UIBridgeData *bridge)
{
uv_mutex_lock(&bridge->mutex);
bridge->stopped = true;
uv_mutex_unlock(&bridge->mutex);
}
static void ui_thread_run(void *data)
{
UIBridgeData *bridge = data;
bridge->ui_main(bridge, bridge->ui);
}
static void ui_bridge_stop(UI *b)
{
UIBridgeData *bridge = (UIBridgeData *)b;
bool stopped = bridge->stopped = false;
UI_CALL(b, stop, 1, b);
for (;;) {
uv_mutex_lock(&bridge->mutex);
stopped = bridge->stopped;
uv_mutex_unlock(&bridge->mutex);
if (stopped) {
break;
}
loop_poll_events(&main_loop, 10);
}
uv_thread_join(&bridge->ui_thread);
uv_mutex_destroy(&bridge->mutex);
uv_cond_destroy(&bridge->cond);
ui_detach_impl(b);
xfree(b);
}
static void ui_bridge_stop_event(void **argv)
{
UI *ui = UI(argv[0]);
ui->stop(ui);
}
static void ui_bridge_resize(UI *b, int width, int height)
{
UI_CALL(b, resize, 3, b, INT2PTR(width), INT2PTR(height));
}
static void ui_bridge_resize_event(void **argv)
{
UI *ui = UI(argv[0]);
ui->resize(ui, PTR2INT(argv[1]), PTR2INT(argv[2]));
}
static void ui_bridge_clear(UI *b)
{
UI_CALL(b, clear, 1, b);
}
static void ui_bridge_clear_event(void **argv)
{
UI *ui = UI(argv[0]);
ui->clear(ui);
}
static void ui_bridge_eol_clear(UI *b)
{
UI_CALL(b, eol_clear, 1, b);
}
static void ui_bridge_eol_clear_event(void **argv)
{
UI *ui = UI(argv[0]);
ui->eol_clear(ui);
}
static void ui_bridge_cursor_goto(UI *b, int row, int col)
{
UI_CALL(b, cursor_goto, 3, b, INT2PTR(row), INT2PTR(col));
}
static void ui_bridge_cursor_goto_event(void **argv)
{
UI *ui = UI(argv[0]);
ui->cursor_goto(ui, PTR2INT(argv[1]), PTR2INT(argv[2]));
}
static void ui_bridge_cursor_styleset(UI *b, Dictionary style)
{
Object copy = copy_object(DICTIONARY_OBJ(style));
Object *pobj = xmalloc(sizeof(copy));
*pobj = copy;
UI_CALL(b, cursor_styleset, 2, b, pobj);
}
static void ui_bridge_cursor_styleset_event(void **argv)
{
UI *ui = UI(argv[0]);
Object *styles = (Object *)argv[1];
ui->cursor_style_set(ui, styles->data.dictionary);
api_free_object(*styles);
xfree(styles);
}
static void ui_bridge_update_menu(UI *b)
{
UI_CALL(b, update_menu, 1, b);
}
static void ui_bridge_update_menu_event(void **argv)
{
UI *ui = UI(argv[0]);
ui->update_menu(ui);
}
static void ui_bridge_busy_start(UI *b)
{
UI_CALL(b, busy_start, 1, b);
}
static void ui_bridge_busy_start_event(void **argv)
{
UI *ui = UI(argv[0]);
ui->busy_start(ui);
}
static void ui_bridge_busy_stop(UI *b)
{
UI_CALL(b, busy_stop, 1, b);
}
static void ui_bridge_busy_stop_event(void **argv)
{
UI *ui = UI(argv[0]);
ui->busy_stop(ui);
}
static void ui_bridge_mouse_on(UI *b)
{
UI_CALL(b, mouse_on, 1, b);
}
static void ui_bridge_mouse_on_event(void **argv)
{
UI *ui = UI(argv[0]);
ui->mouse_on(ui);
}
static void ui_bridge_mouse_off(UI *b)
{
UI_CALL(b, mouse_off, 1, b);
}
static void ui_bridge_mouse_off_event(void **argv)
{
UI *ui = UI(argv[0]);
ui->mouse_off(ui);
}
static void ui_bridge_mode_change(UI *b, int mode)
{
UI_CALL(b, mode_change, 2, b, INT2PTR(mode));
}
static void ui_bridge_mode_change_event(void **argv)
{
UI *ui = UI(argv[0]);
ui->mode_change(ui, PTR2INT(argv[1]));
}
static void ui_bridge_set_scroll_region(UI *b, int top, int bot, int left,
int right)
{
UI_CALL(b, set_scroll_region, 5, b, INT2PTR(top), INT2PTR(bot),
INT2PTR(left), INT2PTR(right));
}
static void ui_bridge_set_scroll_region_event(void **argv)
{
UI *ui = UI(argv[0]);
ui->set_scroll_region(ui, PTR2INT(argv[1]), PTR2INT(argv[2]),
PTR2INT(argv[3]), PTR2INT(argv[4]));
}
static void ui_bridge_scroll(UI *b, int count)
{
UI_CALL(b, scroll, 2, b, INT2PTR(count));
}
static void ui_bridge_scroll_event(void **argv)
{
UI *ui = UI(argv[0]);
ui->scroll(ui, PTR2INT(argv[1]));
}
static void ui_bridge_highlight_set(UI *b, HlAttrs attrs)
{
HlAttrs *a = xmalloc(sizeof(HlAttrs));
*a = attrs;
UI_CALL(b, highlight_set, 2, b, a);
}
static void ui_bridge_highlight_set_event(void **argv)
{
UI *ui = UI(argv[0]);
ui->highlight_set(ui, *((HlAttrs *)argv[1]));
xfree(argv[1]);
}
static void ui_bridge_put(UI *b, uint8_t *text, size_t size)
{
uint8_t *t = NULL;
if (text) {
t = xmalloc(sizeof(((UCell *)0)->data));
memcpy(t, text, size);
}
UI_CALL(b, put, 3, b, t, INT2PTR(size));
}
static void ui_bridge_put_event(void **argv)
{
UI *ui = UI(argv[0]);
ui->put(ui, (uint8_t *)argv[1], (size_t)(uintptr_t)argv[2]);
xfree(argv[1]);
}
static void ui_bridge_bell(UI *b)
{
UI_CALL(b, bell, 1, b);
}
static void ui_bridge_bell_event(void **argv)
{
UI *ui = UI(argv[0]);
ui->bell(ui);
}
static void ui_bridge_visual_bell(UI *b)
{
UI_CALL(b, visual_bell, 1, b);
}
static void ui_bridge_visual_bell_event(void **argv)
{
UI *ui = UI(argv[0]);
ui->visual_bell(ui);
}
static void ui_bridge_update_fg(UI *b, int fg)
{
UI_CALL(b, update_fg, 2, b, INT2PTR(fg));
}
static void ui_bridge_update_fg_event(void **argv)
{
UI *ui = UI(argv[0]);
ui->update_fg(ui, PTR2INT(argv[1]));
}
static void ui_bridge_update_bg(UI *b, int bg)
{
UI_CALL(b, update_bg, 2, b, INT2PTR(bg));
}
static void ui_bridge_update_bg_event(void **argv)
{
UI *ui = UI(argv[0]);
ui->update_bg(ui, PTR2INT(argv[1]));
}
static void ui_bridge_update_sp(UI *b, int sp)
{
UI_CALL(b, update_sp, 2, b, INT2PTR(sp));
}
static void ui_bridge_update_sp_event(void **argv)
{
UI *ui = UI(argv[0]);
ui->update_sp(ui, PTR2INT(argv[1]));
}
static void ui_bridge_flush(UI *b)
{
UI_CALL(b, flush, 1, b);
}
static void ui_bridge_flush_event(void **argv)
{
UI *ui = UI(argv[0]);
ui->flush(ui);
}
static void ui_bridge_suspend(UI *b)
{
UIBridgeData *data = (UIBridgeData *)b;
uv_mutex_lock(&data->mutex);
UI_CALL(b, suspend, 1, b);
data->ready = false;
// suspend the main thread until CONTINUE is called by the UI thread
while (!data->ready) {
uv_cond_wait(&data->cond, &data->mutex);
}
uv_mutex_unlock(&data->mutex);
}
static void ui_bridge_suspend_event(void **argv)
{
UI *ui = UI(argv[0]);
ui->suspend(ui);
}
static void ui_bridge_set_title(UI *b, char *title)
{
UI_CALL(b, set_title, 2, b, title ? xstrdup(title) : NULL);
}
static void ui_bridge_set_title_event(void **argv)
{
UI *ui = UI(argv[0]);
ui->set_title(ui, argv[1]);
xfree(argv[1]);
}
static void ui_bridge_set_icon(UI *b, char *icon)
{
UI_CALL(b, set_icon, 2, b, icon ? xstrdup(icon) : NULL);
}
static void ui_bridge_set_icon_event(void **argv)
{
UI *ui = UI(argv[0]);
ui->set_icon(ui, argv[1]);
xfree(argv[1]);
}
|