client API, lua: unify event code further

Move some parts that can be generic to the client API code. It turns out
lua.c doesn't need anything special.

This adds the "id" field. I think this was actually missing from the
JSON IPC code (i.e. it's a very recent regression that is fixed with
this commit).
This commit is contained in:
wm4 2020-03-21 22:09:07 +01:00
parent 0fbe7f9e54
commit d375cc304a
3 changed files with 39 additions and 55 deletions

View File

@ -1288,6 +1288,10 @@ All events can have the following fields:
``event``
Name as the event (as returned by ``mpv_event_name()``).
``id``
The ``reply_userdata`` field (opaque user value). If ``reply_userdata`` is 0,
the field is not added.
``error``
Set to an error string (as returned by ``mpv_error_string()``). This field
is missing if no error happened, or the event type does not report error.
@ -1403,10 +1407,24 @@ This list uses the event name field value, and the C API symbol in brackets:
Undocumented.
``command-reply`` (``MPV_EVENT_COMMAND_REPLY``)
Undocumented.
This is one of the commands for which the ```error`` field is meaningful.
JSON IPC and Lua and possibly other backends treat this specially and may
not pass the actual event to the user.
The event has the following fields:
``result``
The result (on success) of any ``mpv_node`` type, if any.
``client-message`` (``MPV_EVENT_CLIENT_MESSAGE``)
Undocumented.
Lua and possibly other backends treat this specially and may not pass the
actual event to the user.
The event has the following fields:
``args``
Array of strings with the message data.
``video-reconfig`` (``MPV_EVENT_VIDEO_RECONFIG``)
Happens on video output or filter reconfig.

View File

@ -1923,6 +1923,9 @@ int mpv_event_to_node(mpv_node *dst, mpv_event *event)
if (event->error < 0)
node_map_add_string(dst, "error", mpv_error_string(event->error));
if (event->reply_userdata)
node_map_add_int64(dst, "id", event->reply_userdata);
switch (event->event_id) {
case MPV_EVENT_START_FILE: {
@ -1999,6 +2002,13 @@ int mpv_event_to_node(mpv_node *dst, mpv_event *event)
break;
}
case MPV_EVENT_COMMAND_REPLY: {
mpv_event_command *cmd = event->data;
*node_map_add(dst, "result", MPV_FORMAT_NONE) = cmd->result;
break;
}
case MPV_EVENT_HOOK: {
mpv_event_hook *hook = event->data;

View File

@ -505,62 +505,18 @@ static int script_wait_event(lua_State *L)
mpv_event *event = mpv_wait_event(ctx->client, luaL_optnumber(L, 1, 1e20));
lua_newtable(L); // event
lua_pushstring(L, mpv_event_name(event->event_id)); // event name
lua_setfield(L, -2, "event"); // event
if (event->reply_userdata) {
lua_pushnumber(L, event->reply_userdata);
lua_setfield(L, -2, "id");
struct mpv_node rn;
mpv_event_to_node(&rn, event);
assert(rn.format == MPV_FORMAT_NODE_MAP);
mpv_node_list *list = rn.u.list;
for (int n = 0; n < list->num; n++) {
pushnode(L, &list->values[n]);
lua_setfield(L, -2, list->keys[n]);
}
if (event->error < 0) {
lua_pushstring(L, mpv_error_string(event->error)); // event err
lua_setfield(L, -2, "error"); // event
}
switch (event->event_id) {
case MPV_EVENT_PROPERTY_CHANGE: {
mpv_event_property *prop = event->data;
lua_pushstring(L, prop->name);
lua_setfield(L, -2, "name");
switch (prop->format) {
case MPV_FORMAT_NODE:
pushnode(L, prop->data);
break;
case MPV_FORMAT_DOUBLE:
lua_pushnumber(L, *(double *)prop->data);
break;
case MPV_FORMAT_FLAG:
lua_pushboolean(L, *(int *)prop->data);
break;
case MPV_FORMAT_STRING:
lua_pushstring(L, *(char **)prop->data);
break;
default:
lua_pushnil(L);
}
lua_setfield(L, -2, "data");
break;
}
case MPV_EVENT_COMMAND_REPLY: {
mpv_event_command *cmd = event->data;
pushnode(L, &cmd->result);
lua_setfield(L, -2, "result");
break;
}
default: ;
struct mpv_node rn;
mpv_event_to_node(&rn, event);
assert(rn.format == MPV_FORMAT_NODE_MAP);
mpv_node_list *list = rn.u.list;
for (int n = 0; n < list->num; n++) {
pushnode(L, &list->values[n]);
lua_setfield(L, -2, list->keys[n]);
}
mpv_free_node_contents(&rn);
}
mpv_free_node_contents(&rn);
// return event
return 1;