-- Copyright 2025 David Vazgenovich Shakaryan local config = require('config') local input = {} local mappings = {} local active_mapping = {} local mapping_bound = false local clicks = {} local state local osd local menu_btn_f function input.init(_state, _osd, _menu_btn_f) state = _state osd = _osd menu_btn_f = _menu_btn_f end function input.define_mapping(name, m) mappings[name] = m end function input.set_mapping(name) active_mapping = mappings[name] end local function mouse_has_drifted(x1, y1, x2, y2) return math.abs(x1 - x2) > config.click_max_drift or math.abs(y1 - y2) > config.click_max_drift end -- when mpv registers a double-click, the second click triggers both -- double-click and click events. instead of identifying and ignoring this -- second click event, we can implement double-click detection ourselves for -- more control and to bypass some mpv inconsistencies. -- -- this modifies the mpv event table in-place. it may be marked cancelled or -- renamed, e.g. from MBTN_LEFT to MBTN_LEFT_DBL. local function process_mbtn(ev, id) local time = mp.get_time() local mpos = osd.mpos local k = ev.key_name local c = clicks[k] if not c then c = {} clicks[k] = c end if ev.event == 'down' then c.dbl = c.pt and time - c.pt <= config.click_dbl_time and id == c.pi and k == clicks.k and mpos and not mouse_has_drifted(mpos.x, mpos.y, c.px, c.py) end if c.dbl then ev.key_name = k .. '_DBL' end if ev.canceled or not mpos or (ev.event ~= 'down' and (k ~= clicks.k or not c.ct or id ~= c.ci or time - c.ct > config.click_timeout or mouse_has_drifted( mpos.x, mpos.y, c.cx, c.cy))) then ev.canceled = true clicks[k] = (ev.event == 'up') and {} or {dbl = c.dbl} clicks.k = nil elseif ev.event == 'down' then c.ct, c.cx, c.cy, c.ci = time, mpos.x, mpos.y, id clicks.k = k elseif c.dbl then clicks[k] = {} clicks.k = nil else clicks[k] = {pt = c.ct, px = c.cx, py = c.cy, pi = c.ci} end end -- mpv does not process key-binding changes requested by script functions until -- those functions return. in the meantime, while the function is running, mpv -- discards any key presses for keys that were not already bound prior to the -- start of function execution. similarly, any pending key presses (for keys -- that were bound) are discarded if the binding changes during function -- execution. -- -- in other words, we get an inconsistent result where pressing a key while a -- function is running triggers the bound function upon completion if that -- binding remains the same, but ignores such key presses when our function -- changes the corresponding binding or adds a new one. we can avoid this by -- leaving our keys bound to a common function and building our own logic to -- route keys per the current state. local function handle_key(ev) local k = ev.key_name local mbtn = k:find('MBTN_') if mbtn then -- ignore double-click events from mpv as we have our own -- detection logic if k:find('_DBL') then return end process_mbtn(ev) k = ev.key_name end if ev.canceled then return end local target_mapping = osd.mstate.target and active_mapping._targets and active_mapping._targets[osd.mstate.target] local t = target_mapping and (target_mapping[k] or ev.key_text and target_mapping['ANY_UNICODE']) or (active_mapping[k] or ev.key_text and active_mapping['ANY_UNICODE']) -- ev.is_mouse is false for some mouse events if mbtn or k:find('MOUSE_') or k:find('WHEEL_') then if not osd.mactive then osd:set_mactive(true) end elseif osd.mactive then osd:set_mactive(false) clicks.k = nil end local f = t and t[1] if not f then goto flush end if t[2] == 'complex' then f(ev) elseif (mbtn and ev.event == 'up') or (not mbtn and (ev.event == 'down' or (ev.event == 'repeat' and t[2] == 'repeat'))) then f() end ::flush:: osd:flush(state) end -- uses enable-section and disable-section to disable builtin key bindings -- while the OSD is visible. these commands are technically deprecated for -- non-internal use, but they still work, and there doesn't appear to be -- another way apart from setting an override for each individual key. -- -- nonexistent `nodrag' section is enabled to disable VO dragging while the -- menu is open. function input.activate(bool) if mapping_bound == bool then return end if bool then mp.command_native({'disable-section', 'default'}) mp.command_native({'enable-section', 'nodrag'}) mp.add_forced_key_binding('MOUSE_MOVE', 'mouse_move') -- noisy mp.add_forced_key_binding( 'UNMAPPED', 'unmapped', handle_key, {complex = true}) else mp.remove_key_binding('mouse_move') mp.remove_key_binding('unmapped') mp.command_native({'disable-section', 'nodrag'}) mp.command_native({ 'enable-section', 'default', 'allow-hide-cursor+allow-vo-dragging'}) end mapping_bound = bool end local function bind_click(f) mp.command_native({'enable-section', 'click-nodrag'}) mp.add_forced_key_binding( 'MBTN_LEFT', 'click', function(ev) process_mbtn(ev) if not ev.canceled and ev.event == 'down' then f() end end, {complex = true}) mp.add_forced_key_binding('MBTN_LEFT_DBL', 'click-dbl') end local function unbind_click() mp.remove_key_binding('click') mp.remove_key_binding('click-dbl') mp.command_native({'disable-section', 'click-nodrag'}) end local btn_timer; btn_timer = mp.add_periodic_timer(0.1, function() if osd.mpos and mp.get_time() - osd.mpos_time > config.btn_timeout then osd:show_menu_btn(false) btn_timer:kill() end end, true) -- disabled function input.update_mpos(mpos) if not mpos then return end local ps = osd.mstate or {} local ms = osd:set_mpos(mpos) if not ms then return end -- mpv normally sends a cancel event when the mouse drifts before a -- click is released, but only for a first click and not for the second -- click of what mpv internally considers a double-click. this -- replicates that drift detection behaviour, allowing for consistency -- on double-clicks. local c = clicks.k and clicks[clicks.k] if c and c.ct and mouse_has_drifted(mpos.x, mpos.y, c.cx, c.cy) then clicks.k = nil end if not ms.over_btn_area ~= not ps.over_btn_area then osd:show_menu_btn(ms.over_btn_area) elseif ms.over_btn_area and osd.menu_btn.hidden then osd:show_menu_btn(true) end if (ms.target == 'menu_btn') ~= (ps.target == 'menu_btn') then if ms.target == 'menu_btn' then bind_click(menu_btn_f) else unbind_click() end end if ms.over_btn_area and ms.target ~= 'menu_btn' then btn_timer:resume() else btn_timer:kill() end osd:flush(state) end -- after a resize, mpv does not update the mouse coordinates until the mouse is -- moved. if the mouse was previously over a clickable element and we do -- nothing, a click would trigger that element regardless of mouse position. -- since we cannot get the new position, we instead wipe mouse state on resize, -- clearing it until the next move. function input.on_resize() osd:show_menu_btn(false) osd:set_mpos(nil) clicks.k = nil unbind_click() btn_timer:kill() end return input