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).
Unified · Split
| @@ -1288,6 +1288,10 @@ All events can have the following fields: · 1287 unmodified lines | |||
| 1288 | ``event`` | 1288 | ``event`` |
| 1289 | Name as the event (as returned by ``mpv_event_name()``). | 1289 | Name as the event (as returned by ``mpv_event_name()``). |
| 1290 | 1290 | ||
| 1291 | ``id`` | ||
| 1292 | The ``reply_userdata`` field (opaque user value). If ``reply_userdata`` is 0, | ||
| 1293 | the field is not added. | ||
| 1294 | |||
| 1291 | ``error`` | 1295 | ``error`` |
| 1292 | Set to an error string (as returned by ``mpv_error_string()``). This field | 1296 | Set to an error string (as returned by ``mpv_error_string()``). This field |
| 1293 | is missing if no error happened, or the event type does not report error. | 1297 | 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: · 109 unmodified lines | |||
| 1403 | Undocumented. | 1407 | Undocumented. |
| 1404 | 1408 | ||
| 1405 | ``command-reply`` (``MPV_EVENT_COMMAND_REPLY``) | 1409 | ``command-reply`` (``MPV_EVENT_COMMAND_REPLY``) |
| 1406 | Undocumented. | 1410 | This is one of the commands for which the ```error`` field is meaningful. |
| 1411 | |||
| 1412 | JSON IPC and Lua and possibly other backends treat this specially and may | ||
| 1413 | not pass the actual event to the user. | ||
| 1414 | |||
| 1415 | The event has the following fields: | ||
| 1416 | |||
| 1417 | ``result`` | ||
| 1418 | The result (on success) of any ``mpv_node`` type, if any. | ||
| 1407 | 1419 | ||
| 1408 | ``client-message`` (``MPV_EVENT_CLIENT_MESSAGE``) | 1420 | ``client-message`` (``MPV_EVENT_CLIENT_MESSAGE``) |
| 1409 | Undocumented. | 1421 | Lua and possibly other backends treat this specially and may not pass the |
| 1422 | actual event to the user. | ||
| 1423 | |||
| 1424 | The event has the following fields: | ||
| 1425 | |||
| 1426 | ``args`` | ||
| 1427 | Array of strings with the message data. | ||
| 1410 | 1428 | ||
| 1411 | ``video-reconfig`` (``MPV_EVENT_VIDEO_RECONFIG``) | 1429 | ``video-reconfig`` (``MPV_EVENT_VIDEO_RECONFIG``) |
| 1412 | Happens on video output or filter reconfig. | 1430 | Happens on video output or filter reconfig. |
| @@ -1923,6 +1923,9 @@ int mpv_event_to_node(mpv_node *dst, mpv_event *event) · 1922 unmodified lines | |||
| 1923 | if (event->error < 0) | 1923 | if (event->error < 0) |
| 1924 | node_map_add_string(dst, "error", mpv_error_string(event->error)); | 1924 | node_map_add_string(dst, "error", mpv_error_string(event->error)); |
| 1925 | 1925 | ||
| 1926 | if (event->reply_userdata) | ||
| 1927 | node_map_add_int64(dst, "id", event->reply_userdata); | ||
| 1928 | |||
| 1926 | switch (event->event_id) { | 1929 | switch (event->event_id) { |
| 1927 | 1930 | ||
| 1928 | case MPV_EVENT_START_FILE: { | 1931 | case MPV_EVENT_START_FILE: { |
| @@ -1999,6 +2002,13 @@ int mpv_event_to_node(mpv_node *dst, mpv_event *event) · 70 unmodified lines | |||
| 1999 | break; | 2002 | break; |
| 2000 | } | 2003 | } |
| 2001 | 2004 | ||
| 2005 | case MPV_EVENT_COMMAND_REPLY: { | ||
| 2006 | mpv_event_command *cmd = event->data; | ||
| 2007 | |||
| 2008 | *node_map_add(dst, "result", MPV_FORMAT_NONE) = cmd->result; | ||
| 2009 | break; | ||
| 2010 | } | ||
| 2011 | |||
| 2002 | case MPV_EVENT_HOOK: { | 2012 | case MPV_EVENT_HOOK: { |
| 2003 | mpv_event_hook *hook = event->data; | 2013 | mpv_event_hook *hook = event->data; |
| 2004 | 2014 | ||
| @@ -505,62 +505,18 @@ static int script_wait_event(lua_State *L) · 504 unmodified lines | |||
| 505 | mpv_event *event = mpv_wait_event(ctx->client, luaL_optnumber(L, 1, 1e20)); | 505 | mpv_event *event = mpv_wait_event(ctx->client, luaL_optnumber(L, 1, 1e20)); |
| 506 | 506 | ||
| 507 | lua_newtable(L); // event | 507 | lua_newtable(L); // event |
| 508 | lua_pushstring(L, mpv_event_name(event->event_id)); // event name | ||
| 509 | lua_setfield(L, -2, "event"); // event | ||
| 510 | 508 | ||
| 511 | if (event->reply_userdata) { | 509 | struct mpv_node rn; |
| 512 | lua_pushnumber(L, event->reply_userdata); | 510 | mpv_event_to_node(&rn, event); |
| 513 | lua_setfield(L, -2, "id"); | ||
| 514 | } | ||
| 515 | 511 | ||
| 516 | if (event->error < 0) { | 512 | assert(rn.format == MPV_FORMAT_NODE_MAP); |
| 517 | lua_pushstring(L, mpv_error_string(event->error)); // event err | 513 | mpv_node_list *list = rn.u.list; |
| 518 | lua_setfield(L, -2, "error"); // event | 514 | for (int n = 0; n < list->num; n++) { |
| 515 | pushnode(L, &list->values[n]); | ||
| 516 | lua_setfield(L, -2, list->keys[n]); | ||
| 519 | } | 517 | } |
| 520 | 518 | ||
| 521 | switch (event->event_id) { | 519 | mpv_free_node_contents(&rn); |
| 522 | case MPV_EVENT_PROPERTY_CHANGE: { | ||
| 523 | mpv_event_property *prop = event->data; | ||
| 524 | lua_pushstring(L, prop->name); | ||
| 525 | lua_setfield(L, -2, "name"); | ||
| 526 | switch (prop->format) { | ||
| 527 | case MPV_FORMAT_NODE: | ||
| 528 | pushnode(L, prop->data); | ||
| 529 | break; | ||
| 530 | case MPV_FORMAT_DOUBLE: | ||
| 531 | lua_pushnumber(L, *(double *)prop->data); | ||
| 532 | break; | ||
| 533 | case MPV_FORMAT_FLAG: | ||
| 534 | lua_pushboolean(L, *(int *)prop->data); | ||
| 535 | break; | ||
| 536 | case MPV_FORMAT_STRING: | ||
| 537 | lua_pushstring(L, *(char **)prop->data); | ||
| 538 | break; | ||
| 539 | default: | ||
| 540 | lua_pushnil(L); | ||
| 541 | } | ||
| 542 | lua_setfield(L, -2, "data"); | ||
| 543 | break; | ||
| 544 | } | ||
| 545 | case MPV_EVENT_COMMAND_REPLY: { | ||
| 546 | mpv_event_command *cmd = event->data; | ||
| 547 | pushnode(L, &cmd->result); | ||
| 548 | lua_setfield(L, -2, "result"); | ||
| 549 | break; | ||
| 550 | } | ||
| 551 | default: ; | ||
| 552 | struct mpv_node rn; | ||
| 553 | mpv_event_to_node(&rn, event); | ||
| 554 | |||
| 555 | assert(rn.format == MPV_FORMAT_NODE_MAP); | ||
| 556 | mpv_node_list *list = rn.u.list; | ||
| 557 | for (int n = 0; n < list->num; n++) { | ||
| 558 | pushnode(L, &list->values[n]); | ||
| 559 | lua_setfield(L, -2, list->keys[n]); | ||
| 560 | } | ||
| 561 | |||
| 562 | mpv_free_node_contents(&rn); | ||
| 563 | } | ||
| 564 | 520 | ||
| 565 | // return event | 521 | // return event |
| 566 | return 1; | 522 | return 1; |