summaryrefslogtreecommitdiff
path: root/input.lua
diff options
context:
space:
mode:
Diffstat (limited to 'input.lua')
-rw-r--r--input.lua95
1 files changed, 51 insertions, 44 deletions
diff --git a/input.lua b/input.lua
index 62fe9b1..746e6c0 100644
--- a/input.lua
+++ b/input.lua
@@ -9,7 +9,7 @@ local mappings = {}
local active_mapping = {}
local mapping_bound = false
-local clk = {}
+local clicks = {}
local state
local osd
@@ -28,45 +28,45 @@ end
-- 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.
-local function handle_mbtn(ev, id)
- if ev.canceled then
- clk = {}
- return
- end
-
+--
+-- 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
- if not mpos then
- return
+ local k = ev.key_name
+ local c = clicks[k]
+ if not c then
+ c = {}
+ clicks[k] = c
end
- local time = mp.get_time()
-
if ev.event == 'down' then
- clk.ct, clk.ck = time, ev.key_name
- clk.cx, clk.cy, clk.ci = mpos.x, mpos.y, id
- return
+ 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 ev.event ~= 'up' or not clk.ct then
- return
+ if c.dbl then
+ ev.key_name = k .. '_DBL'
end
- if time - clk.ct > config.click_timeout or
- clk.ck ~= ev.key_name or clk.ci ~= id or
- mouse_has_drifted(mpos.x, mpos.y, clk.cx, clk.cy) then
- clk = {}
- return
+ 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
-
- local dbl = clk.pt and clk.ct - clk.pt <= config.click_dbl_time and
- clk.pk == ev.key_name and clk.pi == id and
- not mouse_has_drifted(clk.cx, clk.cy, clk.px, clk.py)
- clk = dbl and {} or
- {
- pt = clk.ct, pk = ev.key_name,
- px = clk.cx, py = clk.cy, pi = id,
- }
- return dbl and ev.key_name .. '_DBL' or ev.key_name
end
-- mpv does not process key-binding changes requested by script functions until
@@ -92,10 +92,12 @@ local function handle_key(ev)
return
end
- k = handle_mbtn(ev)
- if not k then
- return
- end
+ process_mbtn(ev)
+ k = ev.key_name
+ end
+
+ if ev.canceled then
+ return
end
local target_mapping = osd.mstate.target and
@@ -114,7 +116,7 @@ local function handle_key(ev)
end
elseif osd.mactive then
osd:set_mactive(false)
- clk = {}
+ clicks.k = nil
end
local f = t and t[1]
@@ -124,8 +126,11 @@ local function handle_key(ev)
if t[2] == 'complex' then
f(ev)
- elseif mbtn or ev.event == 'down' or
- (ev.event == 'repeat' and t[2] == 'repeat') then
+ elseif (mbtn and ev.event == 'up') or
+ (not mbtn and
+ (ev.event == 'down' or
+ (ev.event == 'repeat' and
+ t[2] == 'repeat'))) then
f()
end
@@ -154,7 +159,7 @@ function input.set_key_bindings()
return
end
- if not mapping_bound then
+ if not mapping_bound then
mp.command_native({'disable-section', 'default'})
mp.command_native({'enable-section', 'nodrag'})
mp.add_forced_key_binding('MOUSE_MOVE', 'mouse_move') -- noisy
@@ -168,7 +173,8 @@ local function bind_click(f)
mp.command_native({'enable-section', 'click-nodrag'})
mp.add_forced_key_binding(
'MBTN_LEFT', 'click', function(ev)
- if handle_mbtn(ev) then
+ process_mbtn(ev)
+ if not ev.canceled and ev.event == 'down' then
f()
end
end, {complex = true})
@@ -207,8 +213,9 @@ function input.update_mpos(mpos)
-- click of what mpv internally considers a double-click. this
-- replicates that drift detection behaviour, allowing for consistency
-- on double-clicks.
- if clk.ct and mouse_has_drifted(mpos.x, mpos.y, clk.cx, clk.cy) then
- clk = {}
+ 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
@@ -242,7 +249,7 @@ end
function input.on_resize()
osd:show_menu_btn(false)
osd:set_mpos(nil)
- clk = {}
+ clicks.k = nil
unbind_click()
btn_timer:kill()
end