History

client API, lua: add new API for setting OSD overlays

Lua scripting has an undocumented mp.set_osd_ass() function, which is used by osc.lua and console.lua. Apparently, 3rd party scripts also use this. It's probably time to make this a public API. The Lua implementation just bypassed the libmpv API. To make it usable by any type of client, turn it into a command, "osd-overlay". There's already a "overlay-add". Ignore it (although the manpage admits guiltiness). I don't really want to deal with that old command. Its main problem is that it uses global IDs, while I'd like to avoid that scripts mess with each others overlays (whether that is accidentally or intentionally). Maybe "overlay-add" can eventually be merged into "osd-overlay", but I'm too lazy to do that now. Scripting now uses the commands. There is a helper to manage OSD overlays. The helper is very "thin"; I only want to force script authors to use the ID allocation, which may help with putting multiple scripts into a single .lua file without causing conflicts (basically, avoiding singletons within a script's environment). The old set_osd_ass() is emulated with the new API. The JS scripting wrapper also provides a set_osd_ass() function, which calls internal mpv API. Comment that part (to keep it compiling), but I'm leaving it to @avih to finish the change.

wm4 committed 6 years ago

Commit 07287261 parent 96932fe11 files · +292 −68Browse files

Showing the changes to player/lua/defaults.lua (1 of 11 files). Show all

Unified · Split

player/lua/defaults.lua+59−0
@@ -589,6 +589,65 @@ function mp.abort_async_command(t) · 588 unmodified lines
589 end589 end
590end590end
591 591
592local overlay_mt = {}
593overlay_mt.__index = overlay_mt
594local overlay_new_id = 0
595
596function mp.create_osd_overlay(format)
597 overlay_new_id = overlay_new_id + 1
598 local overlay = {
599 format = format,
600 id = overlay_new_id,
601 data = "",
602 res_x = 0,
603 res_y = 720,
604 }
605 setmetatable(overlay, overlay_mt)
606 return overlay
607end
608
609function overlay_mt.update(ov)
610 local cmd = {}
611 for k, v in pairs(ov) do
612 cmd[k] = v
613 end
614 cmd.name = "osd-overlay"
615 mp.command_native(cmd)
616end
617
618function overlay_mt.remove(ov)
619 mp.command_native {
620 name = "osd-overlay",
621 id = ov.id,
622 format = "none",
623 data = "",
624 }
625end
626
627-- legacy API
628function mp.set_osd_ass(res_x, res_y, data)
629 if not mp._legacy_overlay then
630 mp._legacy_overlay = mp.create_osd_overlay("ass-events")
631 end
632 mp._legacy_overlay.res_x = res_x
633 mp._legacy_overlay.res_y = res_y
634 mp._legacy_overlay.data = data
635 mp._legacy_overlay:update()
636end
637
638function mp.get_osd_size()
639 local w = mp.get_property_number("osd-width", 0)
640 local h = mp.get_property_number("osd-height", 0)
641 local par = mp.get_property_number("osd-par", 0)
642 if par == 0 then
643 par = 1
644 end
645
646 local aspect = 1.0 * w / math.max(h) / par
647 return w, h, aspect
648end
649
650
592local mp_utils = package.loaded["mp.utils"]651local mp_utils = package.loaded["mp.utils"]
593 652
594function mp_utils.format_table(t, set)653function mp_utils.format_table(t, set)

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