aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Avi Halachmi (:avih) <avihpit@yahoo.com>2018-03-27 15:29:57 +0300
committerGravatar Kevin Mitchell <kevmitch@gmail.com>2018-04-07 16:02:20 -0700
commitec625266c8f2b2c06355f31b5d58ec030bbc8e5d (patch)
tree5bc683dd8e845f9ab4a7ac662b2d7a9037818644
parent84aa9e71469f666c552aa9f797da16d3c9676f53 (diff)
js: use new hooks API (match f60826c3)
-rw-r--r--player/javascript.c42
-rw-r--r--player/javascript/defaults.js18
2 files changed, 50 insertions, 10 deletions
diff --git a/player/javascript.c b/player/javascript.c
index 9afc5f10da..9ffc9de4c8 100644
--- a/player/javascript.c
+++ b/player/javascript.c
@@ -503,6 +503,8 @@ error_out:
*********************************************************************/
static void pushnode(js_State *J, mpv_node *node);
static void makenode(void *ta_ctx, mpv_node *dst, js_State *J, int idx);
+static int jsL_checkint(js_State *J, int idx);
+static int64_t jsL_checkint64(js_State *J, int idx);
// Return the index in opts of stack[idx] (or of def if undefined), else throws.
static int checkopt(js_State *J, int idx, const char *def, const char *opts[],
@@ -792,6 +794,21 @@ static void script_get_wakeup_pipe(js_State *J)
js_pushnumber(J, mpv_get_wakeup_pipe(jclient(J)));
}
+// args: name (str), priority (int), id (uint)
+static void script__hook_add(js_State *J)
+{
+ const char *name = js_tostring(J, 1);
+ int pri = jsL_checkint(J, 2);
+ uint64_t id = jsL_checkint64(J, 3);
+ push_status(J, mpv_hook_add(jclient(J), id, name, pri));
+}
+
+// args: id (uint)
+static void script__hook_continue(js_State *J)
+{
+ push_status(J, mpv_hook_continue(jclient(J), jsL_checkint64(J, 1)));
+}
+
/**********************************************************************
* mp.utils
*********************************************************************/
@@ -1103,6 +1120,22 @@ static bool same_as_int64(double d)
return d >= INT64_MIN && d <= INT64_MAX && d == (int64_t)d;
}
+static int jsL_checkint(js_State *J, int idx)
+{
+ double d = js_tonumber(J, idx);
+ if (!(d >= INT_MIN && d <= INT_MAX))
+ js_error(J, "integer out of range at index %d", idx);
+ return d;
+}
+
+static int64_t jsL_checkint64(js_State *J, int idx)
+{
+ double d = js_tonumber(J, idx);
+ if (!(d >= INT64_MIN && d <= INT64_MAX))
+ js_error(J, "integer out of range at index %d", idx);
+ return d;
+}
+
// From the js stack value/array/object at index idx
static void makenode(void *ta_ctx, mpv_node *dst, js_State *J, int idx)
{
@@ -1243,6 +1276,13 @@ static void script_wait_event(js_State *J)
js_setproperty(J, -2, "data"); // reply.data (value as observed type)
break;
}
+
+ case MPV_EVENT_HOOK: {
+ mpv_event_hook *hook = event->data;
+ js_pushnumber(J, hook->id);
+ js_setproperty(J, -2, "hook_id"); // reply.hook_id (is a number)
+ break;
+ }
} // switch (event->event_id)
assert(top == js_gettop(J) - 1);
@@ -1285,6 +1325,8 @@ static const struct fn_entry main_fns[] = {
AF_ENTRY(format_time, 2),
FN_ENTRY(enable_messages, 1),
FN_ENTRY(get_wakeup_pipe, 0),
+ FN_ENTRY(_hook_add, 3),
+ FN_ENTRY(_hook_continue, 1),
FN_ENTRY(set_osd_ass, 3),
FN_ENTRY(get_osd_size, 0),
FN_ENTRY(get_osd_margins, 0),
diff --git a/player/javascript/defaults.js b/player/javascript/defaults.js
index 96bac2126f..6e5686ff3d 100644
--- a/player/javascript/defaults.js
+++ b/player/javascript/defaults.js
@@ -123,22 +123,19 @@ function dispatch_message(ev) {
}
// ----- hooks -----
-var next_hid = 1,
- hooks = new_cache(); // items of id: fn
+var hooks = []; // array of callbacks, id is index+1
-function hook_run(id, cont) {
- var cb = hooks[id];
+function run_hook(ev) {
+ var cb = ev.id > 0 && hooks[ev.id - 1];
if (cb)
cb();
- mp.commandv("hook-ack", cont);
+ mp._hook_continue(ev.hook_id);
}
mp.add_hook = function add_hook(name, pri, fn) {
- if (next_hid == 1) // doesn't really matter if we do it once or always
- mp.register_script_message("hook_run", hook_run);
- var id = next_hid++;
- hooks[id] = fn;
- return mp.commandv("hook-add", name, id, pri);
+ hooks.push(fn);
+ // 50 (scripting docs default priority) maps to 0 (default in C API docs)
+ return mp._hook_add(name, pri - 50, hooks.length);
}
/**********************************************************************
@@ -559,6 +556,7 @@ mp.keep_running = true;
g.exit = function() { mp.keep_running = false }; // user-facing too
mp.register_event("shutdown", g.exit);
mp.register_event("property-change", notify_observer);
+mp.register_event("hook", run_hook);
mp.register_event("client-message", dispatch_message);
mp.register_script_message("key-binding", dispatch_key_binding);