History

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).

wm4 committed 6 years ago

Commit d375cc31 parent 0fbe7f93 files · +38 −54Browse files

Unified · Split

3 files
  1. DOCS/man/input.rst +20 −2
  2. player/client.c +10 −0
  3. player/lua.c +8 −52
DOCS/man/input.rst+20−2
@@ -1288,6 +1288,10 @@ All events can have the following fields: · 1287 unmodified lines
12881288 ``event``
12891289 Name as the event (as returned by ``mpv_event_name()``).
12901290
1291+``id``
1292+ The ``reply_userdata`` field (opaque user value). If ``reply_userdata`` is 0,
1293+ the field is not added.
1294+
12911295 ``error``
12921296 Set to an error string (as returned by ``mpv_error_string()``). This field
12931297 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
14031407 Undocumented.
14041408
14051409 ``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.
14071419
14081420 ``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.
14101428
14111429 ``video-reconfig`` (``MPV_EVENT_VIDEO_RECONFIG``)
14121430 Happens on video output or filter reconfig.
player/client.c+10−0
@@ -1923,6 +1923,9 @@ int mpv_event_to_node(mpv_node *dst, mpv_event *event) · 1922 unmodified lines
19231923 if (event->error < 0)
19241924 node_map_add_string(dst, "error", mpv_error_string(event->error));
19251925
1926+ if (event->reply_userdata)
1927+ node_map_add_int64(dst, "id", event->reply_userdata);
1928+
19261929 switch (event->event_id) {
19271930
19281931 case MPV_EVENT_START_FILE: {
@@ -1999,6 +2002,13 @@ int mpv_event_to_node(mpv_node *dst, mpv_event *event) · 70 unmodified lines
19992002 break;
20002003 }
20012004
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+
20022012 case MPV_EVENT_HOOK: {
20032013 mpv_event_hook *hook = event->data;
20042014
player/lua.c+8−52
@@ -505,62 +505,18 @@ static int script_wait_event(lua_State *L) · 504 unmodified lines
505505 mpv_event *event = mpv_wait_event(ctx->client, luaL_optnumber(L, 1, 1e20));
506506
507507 lua_newtable(L); // event
508− lua_pushstring(L, mpv_event_name(event->event_id)); // event name
509− lua_setfield(L, -2, "event"); // event
510508
511− if (event->reply_userdata) {
512− lua_pushnumber(L, event->reply_userdata);
513− lua_setfield(L, -2, "id");
514− }
509+ struct mpv_node rn;
510+ mpv_event_to_node(&rn, event);
515511
516− if (event->error < 0) {
517− lua_pushstring(L, mpv_error_string(event->error)); // event err
518− lua_setfield(L, -2, "error"); // event
512+ assert(rn.format == MPV_FORMAT_NODE_MAP);
513+ mpv_node_list *list = rn.u.list;
514+ for (int n = 0; n < list->num; n++) {
515+ pushnode(L, &list->values[n]);
516+ lua_setfield(L, -2, list->keys[n]);
519517 }
520518
521− switch (event->event_id) {
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− }
519+ mpv_free_node_contents(&rn);
564520
565521 // return event
566522 return 1;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t