From d375cc304aec11f09ed685ffd2f2e7af0abf8910 Mon Sep 17 00:00:00 2001 From: wm4 Date: Sat, 21 Mar 2020 22:09:07 +0100 Subject: 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). --- DOCS/man/input.rst | 22 ++++++++++++++++++-- player/client.c | 10 +++++++++ player/lua.c | 60 ++++++++---------------------------------------------- 3 files changed, 38 insertions(+), 54 deletions(-) diff --git a/DOCS/man/input.rst b/DOCS/man/input.rst index a4567be3aa..7a4c5dcdc2 100644 --- a/DOCS/man/input.rst +++ b/DOCS/man/input.rst @@ -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. diff --git a/player/client.c b/player/client.c index 396c623cea..648ff1173c 100644 --- a/player/client.c +++ b/player/client.c @@ -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; diff --git a/player/lua.c b/player/lua.c index aa0b071eb5..76a0d69e52 100644 --- a/player/lua.c +++ b/player/lua.c @@ -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); - if (event->error < 0) { - lua_pushstring(L, mpv_error_string(event->error)); // event err - lua_setfield(L, -2, "error"); // 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]); } - 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; -- cgit v1.2.3