Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

osc.lua: extract icon_font and icons variables #15645

Merged
merged 1 commit into from
Jan 5, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 62 additions & 42 deletions player/lua/osc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,35 @@ for i = 1, 99 do
user_opts["custom_button_" .. i .. "_mbtn_right_command"] = ""
end

local icon_font = "mpv-osd-symbols"

-- Running this in Lua 5.3+ or LuaJIT converts a hexadecimal Unicode code point
-- to the decimal value of every byte for Lua 5.1 and 5.2 compatibility:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we remove support for puc lua?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we remove support for puc lua?

I think we really shouldn't.

luajit is a project with a bus-factor of 1, highly non-hackable, and way way more complex code than puc-lua (5.1 or 5.2).

Such a core part of mpv (lua scripting) cannot depend on one person to release sources which build correctly on every platform which mpv supports.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would be in favor of dropping support for Lua 5.1 because it makes you waste a lot of time converting unicode code points (I have the same issue in my LRC script) and the performance improvement of LuaJIT is nice for fuzzy finding, it will matter even more once history search is merged since that can grow infinitely. If LuaJIT is abandoned we should switch to Lua 5.4.

Copy link
Member

@avih avih Jan 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would be in favor of dropping support for Lua 5.1 because it makes you waste a lot of time converting unicode code points

Surely you don't need to do that more than once, because at least this comment was about literal codepoints embedded in literal strings as part of the script soure code, and that wouldn't change with each iteration of any loop. And if you do and can't avoid it and have to do whatever a lot, then write the slow part in C.

Using luajit is not a magic solution. because even if it's 5x faster with luajit, you run it on a RPI and it becomes x5 slower again.

It's a script, it can't be expected to perform like C code. But before that, do ensure your whatever algorithm is efficient, because typically it should be enough to gain the speed you prefer.

Also, IIRC 5.1 and 5.2 perform similarly across different use cases, from my own local tests in the past (sometimes one is faster than the other, but it's not necessarily always 5.2 which is faster, and largely they're similar).

And it's better to support more than once version, so I think we should keep both.

it will matter even more once history search is merged since that can grow infinitely.

Something which can grow indefinitely doesn't sound like a great idea to me. Surely you can drop some years old historical commands which you once issued accidentally. So pick a reasonable limit and a useful search algorithm, and it should be OK, and if not, again, write it in C, or drop this feature.

It's a script. Don't push it beyond reasonable limits.

And one can't expect minimum absolute performance (e.g. "this must finish in under 5ms"), because it can run on a wide range of processors, and an inefficient algorithm will remain slow regardess of the processor (or scripting VM), especially as the input grows.

So keep the runtime linear as much as possible with the input size, or at most log-linear, ensure each iteration is reasonably efficient, and then ensure the input size is also within reasonable limits.

That's how code works. When the input becomes big, the algorithm and implementation matter more than the absolute performance of each line of code, and limits should be put in place.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was talking about the watch history. It is up to users when to clear that. I did suggest integrating the C version of fuzzy search iniitally but it was rejected in favor of a Lua script because that was smaller.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, these were my measurements last time I tested it for calling fzy when typing 1 letter in the g-r selector (10k properties):

lua 5.1: 0.166s
luajit: 0.08s
C: 0.005s

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lua 5.1: 0.166s
luajit: 0.08s

If I got that right, luajit ended up 2x faster than 5.1? IMO that's a pretty small diffrence in the grand scheme of things. Definitely not enough to consider dropping 5.1. (yes, I know luajit can be much faster than 5.1 in some cases, but you always need to look at the overall gains, and not through the needle hole, and in the big pictures there are typically many more factors than how fast one function runs).

Also, I didn't look at the code, but there might still be room for further improvements. Usually there is, but not always enough of it to warrant the additional effort and possible complexity, but sometimes a small change is enough to make a big difference which is worth it.

-- glyph='\u{e000}' output=''
-- for i = 1, #glyph do output = output .. '\\' .. string.byte(glyph, i) end
-- print(output)
local icons = {
prev = "\238\132\144", -- E110
next = "\238\132\129", -- E101
pause = "\238\128\130", -- E002
play = "\238\132\129", -- E101
play_backward = "\238\132\144", -- E110
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we align = vertically?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought of aligning them but assignments in other tables in the file are not aligned, do you want to align only this one?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would look nice with constant value length, but whatever. avih also don't like aligning

skip_backward = "\238\128\132", -- E004
skip_forward = "\238\128\133", -- E005
chapter_prev = "\238\132\132", -- E104
chapter_next = "\238\132\133", -- E105
audio = "\238\132\134", -- E106
subtitle = "\238\132\135", -- E107
mute = "\238\132\138", -- E10A
volume = {"\238\132\139", "\238\132\140", "\238\132\141", "\238\132\142"},-- E10B E10C E10D E10E
fullscreen = "\238\132\136", -- E108
exit_fullscreen = "\238\132\137",-- E109
close = "\238\132\149", -- E115
minimize = "\238\132\146", -- E112
maximize = "\238\132\147", -- E113
unmaximize = "\238\132\148", -- E114
}

local osc_param = { -- calculated by osc_init()
playresy = 0, -- canvas size Y
playresx = 0, -- canvas size X
Expand Down Expand Up @@ -163,24 +192,24 @@ local osc_styles

local function set_osc_styles()
osc_styles = {
bigButtons = "{\\blur0\\bord0\\1c&H" .. osc_color_convert(user_opts.buttons_color) .. "\\3c&HFFFFFF\\fs50\\fnmpv-osd-symbols}",
smallButtonsL = "{\\blur0\\bord0\\1c&H" .. osc_color_convert(user_opts.small_buttonsL_color) .. "\\3c&HFFFFFF\\fs19\\fnmpv-osd-symbols}",
bigButtons = "{\\blur0\\bord0\\1c&H" .. osc_color_convert(user_opts.buttons_color) .. "\\3c&HFFFFFF\\fs50\\fn" .. icon_font .. "}",
smallButtonsL = "{\\blur0\\bord0\\1c&H" .. osc_color_convert(user_opts.small_buttonsL_color) .. "\\3c&HFFFFFF\\fs19\\fn" .. icon_font .. "}",
smallButtonsLlabel = "{\\fscx105\\fscy105\\fn" .. mp.get_property("options/osd-font") .. "}",
smallButtonsR = "{\\blur0\\bord0\\1c&H" .. osc_color_convert(user_opts.small_buttonsR_color) .. "\\3c&HFFFFFF\\fs30\\fnmpv-osd-symbols}",
topButtons = "{\\blur0\\bord0\\1c&H" .. osc_color_convert(user_opts.top_buttons_color) .. "\\3c&HFFFFFF\\fs12\\fnmpv-osd-symbols}",
smallButtonsR = "{\\blur0\\bord0\\1c&H" .. osc_color_convert(user_opts.small_buttonsR_color) .. "\\3c&HFFFFFF\\fs30\\fn" .. icon_font .. "}",
topButtons = "{\\blur0\\bord0\\1c&H" .. osc_color_convert(user_opts.top_buttons_color) .. "\\3c&HFFFFFF\\fs12\\fn" .. icon_font .. "}",

elementDown = "{\\1c&H" .. osc_color_convert(user_opts.held_element_color) .."}",
timecodes = "{\\blur0\\bord0\\1c&H" .. osc_color_convert(user_opts.timecode_color) .. "\\3c&HFFFFFF\\fs20}",
vidtitle = "{\\blur0\\bord0\\1c&H" .. osc_color_convert(user_opts.title_color) .. "\\3c&HFFFFFF\\fs14\\q2}",
box = "{\\rDefault\\blur0\\bord1\\1c&H" .. osc_color_convert(user_opts.background_color) .. "\\3c&HFFFFFF}",

topButtonsBar = "{\\blur0\\bord0\\1c&H" .. osc_color_convert(user_opts.top_buttons_color) .. "\\3c&HFFFFFF\\fs18\\fnmpv-osd-symbols}",
smallButtonsBar = "{\\blur0\\bord0\\1c&H" .. osc_color_convert(user_opts.buttons_color) .. "\\3c&HFFFFFF\\fs28\\fnmpv-osd-symbols}",
topButtonsBar = "{\\blur0\\bord0\\1c&H" .. osc_color_convert(user_opts.top_buttons_color) .. "\\3c&HFFFFFF\\fs18\\fn" .. icon_font .. "}",
smallButtonsBar = "{\\blur0\\bord0\\1c&H" .. osc_color_convert(user_opts.buttons_color) .. "\\3c&HFFFFFF\\fs28\\fn" .. icon_font .. "}",
timecodesBar = "{\\blur0\\bord0\\1c&H" .. osc_color_convert(user_opts.timecode_color) .."\\3c&HFFFFFF\\fs27}",
timePosBar = "{\\blur0\\bord".. user_opts.tooltipborder .."\\1c&H" .. osc_color_convert(user_opts.time_pos_color) .. "\\3c&H" .. osc_color_convert(user_opts.time_pos_outline_color) .. "\\fs30}",
vidtitleBar = "{\\blur0\\bord0\\1c&H" .. osc_color_convert(user_opts.title_color) .. "\\3c&HFFFFFF\\fs18\\q2}",

wcButtons = "{\\1c&H" .. osc_color_convert(user_opts.buttons_color) .. "\\fs24\\fnmpv-osd-symbols}",
wcButtons = "{\\1c&H" .. osc_color_convert(user_opts.buttons_color) .. "\\fs24\\fn" .. icon_font .. "}",
wcTitle = "{\\1c&H" .. osc_color_convert(user_opts.title_color) .. "\\fs24\\q2}",
wcBar = "{\\1c&H" .. osc_color_convert(user_opts.background_color) .. "}",
}
Expand Down Expand Up @@ -1127,7 +1156,7 @@ local function window_controls(topbar)

-- Close: 🗙
local ne = new_element("close", "button")
ne.content = "\238\132\149"
ne.content = icons.close
ne.eventresponder["mbtn_left_up"] =
function () mp.commandv("quit") end
lo = add_layout("close")
Expand All @@ -1136,7 +1165,7 @@ local function window_controls(topbar)

-- Minimize: 🗕
ne = new_element("minimize", "button")
ne.content = "\238\132\146"
ne.content = icons.minimize
ne.eventresponder["mbtn_left_up"] =
function () mp.commandv("cycle", "window-minimized") end
lo = add_layout("minimize")
Expand All @@ -1146,9 +1175,9 @@ local function window_controls(topbar)
-- Maximize: 🗖 /🗗
ne = new_element("maximize", "button")
if state.maximized or state.fullscreen then
ne.content = "\238\132\148"
ne.content = icons.unmaximize
else
ne.content = "\238\132\147"
ne.content = icons.maximize
end
ne.eventresponder["mbtn_left_up"] =
function ()
Expand Down Expand Up @@ -1824,14 +1853,14 @@ local function osc_init()
-- prev
ne = new_element("playlist_prev", "button")

ne.content = "\238\132\144"
ne.content = icons.prev
ne.enabled = (pl_pos > 1) or (loop ~= "no")
bind_mouse_buttons("playlist_prev")

--next
ne = new_element("playlist_next", "button")

ne.content = "\238\132\129"
ne.content = icons.next
ne.enabled = (have_pl and (pl_pos < pl_count)) or (loop ~= "no")
bind_mouse_buttons("playlist_next")

Expand All @@ -1842,23 +1871,21 @@ local function osc_init()
ne = new_element("play_pause", "button")

ne.content = function ()
if mp.get_property("pause") == "yes" then
if mp.get_property("play-direction", "forward") ~= "backward" then
return ("\238\132\129")
else
return ("\238\132\144")
end
else
return ("\238\128\130")
if not mp.get_property_native("pause") then
return icons.pause
end

return mp.get_property("play-direction") == "forward"
and icons.play
or icons.play_backward
end
bind_mouse_buttons("play_pause")

--skip_backward
ne = new_element("skip_backward", "button")

ne.softrepeat = true
ne.content = "\238\128\132"
ne.content = icons.skip_backward
ne.eventresponder["mbtn_left_down"] =
function () mp.commandv("seek", -5) end
ne.eventresponder["mbtn_mid"] =
Expand All @@ -1870,7 +1897,7 @@ local function osc_init()
ne = new_element("skip_forward", "button")

ne.softrepeat = true
ne.content = "\238\128\133"
ne.content = icons.skip_forward
ne.eventresponder["mbtn_left_down"] =
function () mp.commandv("seek", 10) end
ne.eventresponder["mbtn_mid"] =
Expand All @@ -1882,14 +1909,14 @@ local function osc_init()
ne = new_element("chapter_prev", "button")

ne.enabled = have_ch
ne.content = "\238\132\132"
ne.content = icons.chapter_prev
bind_mouse_buttons("chapter_prev")

--chapter_next
ne = new_element("chapter_next", "button")

ne.enabled = have_ch
ne.content = "\238\132\133"
ne.content = icons.chapter_next
bind_mouse_buttons("chapter_next")

--
Expand All @@ -1900,8 +1927,8 @@ local function osc_init()

ne.enabled = audio_track_count > 0
ne.content = function ()
return ("\238\132\134" .. osc_styles.smallButtonsLlabel .. " " ..
mp.get_property_number("aid", "-") .. "/" .. audio_track_count)
return icons.audio .. osc_styles.smallButtonsLlabel .. " " ..
mp.get_property_number("aid", "-") .. "/" .. audio_track_count
end
bind_mouse_buttons("audio_track")

Expand All @@ -1910,19 +1937,15 @@ local function osc_init()

ne.enabled = sub_track_count > 0
ne.content = function ()
return ("\238\132\135" .. osc_styles.smallButtonsLlabel .. " " ..
mp.get_property_number("sid", "-") .. "/" .. sub_track_count)
return icons.subtitle .. osc_styles.smallButtonsLlabel .. " " ..
mp.get_property_number("sid", "-") .. "/" .. sub_track_count
end
bind_mouse_buttons("sub_track")

--fullscreen
ne = new_element("fullscreen", "button")
ne.content = function ()
if state.fullscreen then
return ("\238\132\137")
else
return ("\238\132\136")
end
return state.fullscreen and icons.exit_fullscreen or icons.fullscreen
end
bind_mouse_buttons("fullscreen")

Expand Down Expand Up @@ -2094,15 +2117,12 @@ local function osc_init()
ne = new_element("volume", "button")

ne.content = function()
local volume = mp.get_property_number("volume", 0)
local mute = mp.get_property_native("mute")
local volicon = {"\238\132\139", "\238\132\140",
"\238\132\141", "\238\132\142"}
if volume == 0 or mute then
return "\238\132\138"
else
return volicon[math.min(4,math.ceil(volume / (100/3)))]
local volume = mp.get_property_number("volume")
if volume == 0 or mp.get_property_native("mute") then
return icons.mute
end

return icons.volume[math.min(4, math.ceil(volume / (100/3)))]
end
bind_mouse_buttons("volume")

Expand Down
Loading