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
503 *********************************************************************/503 *********************************************************************/
504static void pushnode(js_State *J, mpv_node *node);504static void pushnode(js_State *J, mpv_node *node);
505static void makenode(void *ta_ctx, mpv_node *dst, js_State *J, int idx);505static void makenode(void *ta_ctx, mpv_node *dst, js_State *J, int idx);
506static int jsL_checkint(js_State *J, int idx);
507static int64_t jsL_checkint64(js_State *J, int idx);
506 508
507// Return the index in opts of stack[idx] (or of def if undefined), else throws.509// Return the index in opts of stack[idx] (or of def if undefined), else throws.
508static int checkopt(js_State *J, int idx, const char *def, const char *opts[],510static 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
792 js_pushnumber(J, mpv_get_wakeup_pipe(jclient(J)));794 js_pushnumber(J, mpv_get_wakeup_pipe(jclient(J)));
793}795}
794 796
797// args: name (str), priority (int), id (uint)
798static 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)
807static void script__hook_continue(js_State *J)
808{
809 push_status(J, mpv_hook_continue(jclient(J), jsL_checkint64(J, 1)));
810}
811
795/**********************************************************************812/**********************************************************************
796 * mp.utils813 * mp.utils
797 *********************************************************************/814 *********************************************************************/
@@ -1103,6 +1120,22 @@ static bool same_as_int64(double d) · 305 unmodified lines
1103 return d >= INT64_MIN && d <= INT64_MAX && d == (int64_t)d;1120 return d >= INT64_MIN && d <= INT64_MAX && d == (int64_t)d;
1104}1121}
1105 1122
1123static 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
1131static 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
1106// From the js stack value/array/object at index idx1139// From the js stack value/array/object at index idx
1107static void makenode(void *ta_ctx, mpv_node *dst, js_State *J, int idx)1140static void makenode(void *ta_ctx, mpv_node *dst, js_State *J, int idx)
1108{1141{
@@ -1243,6 +1276,13 @@ static void script_wait_event(js_State *J) · 134 unmodified lines
1243 js_setproperty(J, -2, "data"); // reply.data (value as observed type)1276 js_setproperty(J, -2, "data"); // reply.data (value as observed type)
1244 break;1277 break;
1245 }1278 }
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 }
1246 } // switch (event->event_id)1286 } // switch (event->event_id)
1247 1287
1248 assert(top == js_gettop(J) - 1);1288 assert(top == js_gettop(J) - 1);
@@ -1285,6 +1325,8 @@ static const struct fn_entry main_fns[] = { · 36 unmodified lines
1285 AF_ENTRY(format_time, 2),1325 AF_ENTRY(format_time, 2),
1286 FN_ENTRY(enable_messages, 1),1326 FN_ENTRY(enable_messages, 1),
1287 FN_ENTRY(get_wakeup_pipe, 0),1327 FN_ENTRY(get_wakeup_pipe, 0),
1328 FN_ENTRY(_hook_add, 3),
1329 FN_ENTRY(_hook_continue, 1),
1288 FN_ENTRY(set_osd_ass, 3),1330 FN_ENTRY(set_osd_ass, 3),
1289 FN_ENTRY(get_osd_size, 0),1331 FN_ENTRY(get_osd_size, 0),
1290 FN_ENTRY(get_osd_margins, 0),1332 FN_ENTRY(get_osd_margins, 0),
player/javascript/defaults.js+8−10
@@ -123,22 +123,19 @@ function dispatch_message(ev) { · 122 unmodified lines
123}123}
124 124
125// ----- hooks -----125// ----- hooks -----
126var next_hid = 1,126var hooks = []; // array of callbacks, id is index+1
127 hooks = new_cache(); // items of id: fn
128 127
129function hook_run(id, cont) {128function run_hook(ev) {
130 var cb = hooks[id];129 var cb = ev.id > 0 && hooks[ev.id - 1];
131 if (cb)130 if (cb)
132 cb();131 cb();
133 mp.commandv("hook-ack", cont);132 mp._hook_continue(ev.hook_id);
134}133}
135 134
136mp.add_hook = function add_hook(name, pri, fn) {135mp.add_hook = function add_hook(name, pri, fn) {
137 if (next_hid == 1) // doesn't really matter if we do it once or always136 hooks.push(fn);
138 mp.register_script_message("hook_run", hook_run);137 // 50 (scripting docs default priority) maps to 0 (default in C API docs)
139 var id = next_hid++;138 return mp._hook_add(name, pri - 50, hooks.length);
140 hooks[id] = fn;
141 return mp.commandv("hook-add", name, id, pri);
142}139}
143 140
144/**********************************************************************141/**********************************************************************
@@ -559,6 +556,7 @@ mp.keep_running = true; · 414 unmodified lines
559g.exit = function() { mp.keep_running = false }; // user-facing too556g.exit = function() { mp.keep_running = false }; // user-facing too
560mp.register_event("shutdown", g.exit);557mp.register_event("shutdown", g.exit);
561mp.register_event("property-change", notify_observer);558mp.register_event("property-change", notify_observer);
559mp.register_event("hook", run_hook);
562mp.register_event("client-message", dispatch_message);560mp.register_event("client-message", dispatch_message);
563mp.register_script_message("key-binding", dispatch_key_binding);561mp.register_script_message("key-binding", dispatch_key_binding);
564 562

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