Revert "[WIP] "abstract_ui" fixes and improvements"

This commit is contained in:
Justin M. Keyes
2015-01-12 10:14:52 -05:00
parent 4c55c34efa
commit d7e18b5c95
18 changed files with 159 additions and 240 deletions

View File

@@ -45,7 +45,6 @@ typedef struct {
typedef struct {
uint64_t id;
size_t pending_requests;
PMap(cstr_t) *subscribed_events;
bool is_job, closed;
msgpack_unpacker *unpacker;
@@ -84,6 +83,7 @@ static uint64_t next_id = 1;
static PMap(uint64_t) *channels = NULL;
static PMap(cstr_t) *event_strings = NULL;
static msgpack_sbuffer out_buffer;
static size_t pending_requests = 0;
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "msgpack_rpc/channel.c.generated.h"
@@ -103,7 +103,14 @@ void channel_init(void)
}
if (abstract_ui) {
// Add handler for "attach_ui"
remote_ui_init();
String method = cstr_as_string("attach_ui");
MsgpackRpcRequestHandler handler = {.fn = remote_ui_attach, .defer = true};
msgpack_rpc_add_method_handler(method, handler);
method = cstr_as_string("detach_ui");
handler.fn = remote_ui_detach;
msgpack_rpc_add_method_handler(method, handler);
}
}
@@ -193,21 +200,20 @@ bool channel_send_event(uint64_t id, char *name, Array args)
return false;
}
if (channel) {
if (channel->pending_requests) {
DelayedNotification p = {
.channel = channel,
.method = cstr_to_string(name),
.args = args
};
// Pending request, queue the notification for sending later
*kl_pushp(DelayedNotification, delayed_notifications) = p;
} else {
if (pending_requests) {
DelayedNotification p = {
.channel = channel,
.method = cstr_to_string(name),
.args = args
};
// Pending request, queue the notification for sending later
*kl_pushp(DelayedNotification, delayed_notifications) = p;
} else {
if (channel) {
send_event(channel, name, args);
} else {
broadcast_event(name, args);
}
} else {
// TODO(tarruda): Implement event broadcasting in vimscript
broadcast_event(name, args);
}
return true;
@@ -240,10 +246,10 @@ Object channel_send_call(uint64_t id,
// Push the frame
ChannelCallFrame frame = {request_id, false, false, NIL};
kv_push(ChannelCallFrame *, channel->call_stack, &frame);
channel->pending_requests++;
pending_requests++;
event_poll_until(-1, frame.returned);
(void)kv_pop(channel->call_stack);
channel->pending_requests--;
pending_requests--;
if (frame.errored) {
api_set_error(err, Exception, "%s", frame.result.data.string.data);
@@ -255,7 +261,7 @@ Object channel_send_call(uint64_t id,
free_channel(channel);
}
if (!channel->pending_requests) {
if (!pending_requests) {
send_delayed_notifications();
}
@@ -486,7 +492,6 @@ static void on_request_event(Event event)
{
RequestEvent *e = event.data;
Channel *channel = e->channel;
last_message_source = channel->id;
MsgpackRpcRequestHandler handler = e->handler;
Array args = e->args;
uint64_t request_id = e->request_id;
@@ -639,7 +644,6 @@ static void close_channel(Channel *channel)
uv_handle_t *handle = (uv_handle_t *)channel->data.streams.uv;
if (handle) {
uv_close(handle, close_cb);
free_channel(channel);
} else {
event_push((Event) { .handler = on_stdio_close }, false);
}
@@ -683,7 +687,6 @@ static Channel *register_channel(void)
rv->closed = false;
rv->unpacker = msgpack_unpacker_new(MSGPACK_UNPACKER_INIT_BUFFER_SIZE);
rv->id = next_id++;
rv->pending_requests = 0;
rv->subscribed_events = pmap_new(cstr_t)();
rv->next_request_id = 1;
kv_init(rv->call_stack);

View File

@@ -26,44 +26,18 @@ static PMap(uint64_t) *connected_uis = NULL;
void remote_ui_init(void)
{
connected_uis = pmap_new(uint64_t)();
// Add handler for "attach_ui"
String method = cstr_as_string("ui_attach");
MsgpackRpcRequestHandler handler = {.fn = remote_ui_attach, .defer = false};
msgpack_rpc_add_method_handler(method, handler);
method = cstr_as_string("ui_detach");
handler.fn = remote_ui_detach;
msgpack_rpc_add_method_handler(method, handler);
method = cstr_as_string("ui_try_resize");
handler.fn = remote_ui_try_resize;
msgpack_rpc_add_method_handler(method, handler);
}
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 Object remote_ui_attach(uint64_t channel_id, uint64_t request_id,
Array args, Error *error)
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 != 3 || args.items[0].type != kObjectTypeInteger
if (args.size != 2 || args.items[0].type != kObjectTypeInteger
|| args.items[1].type != kObjectTypeInteger
|| args.items[2].type != kObjectTypeBoolean
|| 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 "
@@ -76,7 +50,6 @@ static Object remote_ui_attach(uint64_t channel_id, uint64_t request_id,
UI *ui = xcalloc(1, sizeof(UI));
ui->width = (int)args.items[0].data.integer;
ui->height = (int)args.items[1].data.integer;
ui->rgb = args.items[2].data.boolean;
ui->data = data;
ui->resize = remote_ui_resize;
ui->clear = remote_ui_clear;
@@ -94,16 +67,16 @@ static Object remote_ui_attach(uint64_t channel_id, uint64_t request_id,
ui->put = remote_ui_put;
ui->bell = remote_ui_bell;
ui->visual_bell = remote_ui_visual_bell;
ui->update_fg = remote_ui_update_fg;
ui->update_bg = remote_ui_update_bg;
ui->flush = remote_ui_flush;
ui->suspend = remote_ui_suspend;
pmap_put(uint64_t)(connected_uis, channel_id, ui);
ui_attach(ui);
return NIL;
}
static Object remote_ui_detach(uint64_t channel_id, uint64_t request_id,
Array args, Error *error)
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"));
@@ -113,30 +86,21 @@ static Object remote_ui_detach(uint64_t channel_id, uint64_t request_id,
return NIL;
}
static Object remote_ui_try_resize(uint64_t channel_id, uint64_t request_id,
Array args, Error *error)
void remote_ui_disconnect(uint64_t channel_id)
{
if (!pmap_has(uint64_t)(connected_uis, channel_id)) {
api_set_error(error, Exception, _("UI is not attached for channel"));
}
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;
}
UI *ui = pmap_get(uint64_t)(connected_uis, channel_id);
ui->width = (int)args.items[0].data.integer;
ui->height = (int)args.items[1].data.integer;
ui_refresh();
return NIL;
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;
@@ -250,6 +214,10 @@ static void remote_ui_highlight_set(UI *ui, HlAttrs attrs)
PUT(hl, "bold", BOOLEAN_OBJ(true));
}
if (attrs.standout) {
PUT(hl, "standout", BOOLEAN_OBJ(true));
}
if (attrs.underline) {
PUT(hl, "underline", BOOLEAN_OBJ(true));
}
@@ -298,23 +266,15 @@ static void remote_ui_visual_bell(UI *ui)
push_call(ui, "visual_bell", args);
}
static void remote_ui_update_fg(UI *ui, int fg)
{
Array args = ARRAY_DICT_INIT;
ADD(args, INTEGER_OBJ(fg));
push_call(ui, "update_fg", args);
}
static void remote_ui_update_bg(UI *ui, int bg)
{
Array args = ARRAY_DICT_INIT;
ADD(args, INTEGER_OBJ(bg));
push_call(ui, "update_bg", 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);
}