History

js: use new hooks API (match f60826c3)

Avi Halachmi (:avih) committed 8 years ago

Commit ec625261 parent 84aa9e72 files · +50 −10Browse files

Unified · Split

2 files
  1. player/javascript.c +42 −0
  2. player/javascript/defaults.js +8 −10
player/javascript.c+42−0
@@ -503,6 +503,8 @@ error_out: · 502 unmodified lines
503503 *********************************************************************/
504504 static void pushnode(js_State *J, mpv_node *node);
505505 static void makenode(void *ta_ctx, mpv_node *dst, js_State *J, int idx);
506+static int jsL_checkint(js_State *J, int idx);
507+static int64_t jsL_checkint64(js_State *J, int idx);
506508
507509 // Return the index in opts of stack[idx] (or of def if undefined), else throws.
508510 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) · 283 unmodified lines
792794 js_pushnumber(J, mpv_get_wakeup_pipe(jclient(J)));
793795 }
794796
797+// args: name (str), priority (int), id (uint)
798+static void script__hook_add(js_State *J)
799+{
800+ const char *name = js_tostring(J, 1);
801+ int pri = jsL_checkint(J, 2);
802+ uint64_t id = jsL_checkint64(J, 3);
803+ push_status(J, mpv_hook_add(jclient(J), id, name, pri));
804+}
805+
806+// args: id (uint)
807+static void script__hook_continue(js_State *J)
808+{
809+ push_status(J, mpv_hook_continue(jclient(J), jsL_checkint64(J, 1)));
810+}
811+
795812 /**********************************************************************
796813 * mp.utils
797814 *********************************************************************/
@@ -1103,6 +1120,22 @@ static bool same_as_int64(double d) · 305 unmodified lines
11031120 return d >= INT64_MIN && d <= INT64_MAX && d == (int64_t)d;
11041121 }
11051122
1123+static int jsL_checkint(js_State *J, int idx)
1124+{
1125+ double d = js_tonumber(J, idx);
1126+ if (!(d >= INT_MIN && d <= INT_MAX))
1127+ js_error(J, "integer out of range at index %d", idx);
1128+ return d;
1129+}
1130+
1131+static int64_t jsL_checkint64(js_State *J, int idx)
1132+{
1133+ double d = js_tonumber(J, idx);
1134+ if (!(d >= INT64_MIN && d <= INT64_MAX))
1135+ js_error(J, "integer out of range at index %d", idx);
1136+ return d;
1137+}
1138+
11061139 // From the js stack value/array/object at index idx
11071140 static void makenode(void *ta_ctx, mpv_node *dst, js_State *J, int idx)
11081141 {
@@ -1243,6 +1276,13 @@ static void script_wait_event(js_State *J) · 134 unmodified lines
12431276 js_setproperty(J, -2, "data"); // reply.data (value as observed type)
12441277 break;
12451278 }
1279+
1280+ case MPV_EVENT_HOOK: {
1281+ mpv_event_hook *hook = event->data;
1282+ js_pushnumber(J, hook->id);
1283+ js_setproperty(J, -2, "hook_id"); // reply.hook_id (is a number)
1284+ break;
1285+ }
12461286 } // switch (event->event_id)
12471287
12481288 assert(top == js_gettop(J) - 1);
@@ -1285,6 +1325,8 @@ static const struct fn_entry main_fns[] = { · 36 unmodified lines
12851325 AF_ENTRY(format_time, 2),
12861326 FN_ENTRY(enable_messages, 1),
12871327 FN_ENTRY(get_wakeup_pipe, 0),
1328+ FN_ENTRY(_hook_add, 3),
1329+ FN_ENTRY(_hook_continue, 1),
12881330 FN_ENTRY(set_osd_ass, 3),
12891331 FN_ENTRY(get_osd_size, 0),
12901332 FN_ENTRY(get_osd_margins, 0),
player/javascript/defaults.js+8−10
@@ -123,22 +123,19 @@ function dispatch_message(ev) { · 122 unmodified lines
123123 }
124124
125125 // ----- hooks -----
126−var next_hid = 1,
127− hooks = new_cache(); // items of id: fn
126+var hooks = []; // array of callbacks, id is index+1
128127
129−function hook_run(id, cont) {
130− var cb = hooks[id];
128+function run_hook(ev) {
129+ var cb = ev.id > 0 && hooks[ev.id - 1];
131130 if (cb)
132131 cb();
133− mp.commandv("hook-ack", cont);
132+ mp._hook_continue(ev.hook_id);
134133 }
135134
136135 mp.add_hook = function add_hook(name, pri, fn) {
137− if (next_hid == 1) // doesn't really matter if we do it once or always
138− mp.register_script_message("hook_run", hook_run);
139− var id = next_hid++;
140− hooks[id] = fn;
141− return mp.commandv("hook-add", name, id, pri);
136+ hooks.push(fn);
137+ // 50 (scripting docs default priority) maps to 0 (default in C API docs)
138+ return mp._hook_add(name, pri - 50, hooks.length);
142139 }
143140
144141 /**********************************************************************
@@ -559,6 +556,7 @@ mp.keep_running = true; · 414 unmodified lines
559556 g.exit = function() { mp.keep_running = false }; // user-facing too
560557 mp.register_event("shutdown", g.exit);
561558 mp.register_event("property-change", notify_observer);
559+mp.register_event("hook", run_hook);
562560 mp.register_event("client-message", dispatch_message);
563561 mp.register_script_message("key-binding", dispatch_key_binding);
564562

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