1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
|
-- Copyright 2025 David Vazgenovich Shakaryan
local config = require('config')
local rt = require('rt')
local input = {}
local mappings = {}
local active_mapping = {}
local mapping_bound = false
local clk = {}
local state
local osd
function input.init(_state, _osd, _bs)
state = _state
osd = _osd
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.
local function handle_mbtn(ev, id)
if ev.canceled then
clk = {}
return
end
local mpos = osd.mpos
if not mpos then
return
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
end
if ev.event ~= 'up' or not clk.ct then
return
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
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
-- 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
k = handle_mbtn(ev)
if not k then
return
end
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)
clk = {}
end
local f = t and t[1]
if not f then
goto flush
end
if t[2] == 'complex' then
f(ev)
elseif mbtn or 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.set_key_bindings()
if osd:is_hidden() then
if mapping_bound then
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'})
mapping_bound = false
end
return
end
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
mp.add_forced_key_binding(
'UNMAPPED', 'unmapped', handle_key, {complex = true})
mapping_bound = true
end
end
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
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
function input.set_key_mapping(m)
active_mapping = mappings[m]
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.
if clk.ct and mouse_has_drifted(mpos.x, mpos.y, clk.cx, clk.cy) then
clk = {}
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(rt.toggle_menu)
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)
clk = {}
unbind_click()
btn_timer:kill()
end
mappings.MENU = {
['BS'] = {rt.prev_menu},
['/'] = {rt.start_search},
['Ctrl+s'] = {rt.toggle_menu_sort},
['Ctrl+R'] = {rt.reload_data},
['ENTER'] = {rt.select_option},
['Ctrl+f'] = {rt.favourite_option},
['g'] = {rt.goto_option},
['i'] = {rt.open_option_info},
['?'] = {rt.open_option_info},
['Ctrl+p'] = {rt.goto_playing},
['k'] = {rt.cursor_up, 'repeat'},
['j'] = {rt.cursor_down, 'repeat'},
['K'] = {rt.cursor_page_up, 'repeat'},
['J'] = {rt.cursor_page_down, 'repeat'},
['UP'] = {rt.cursor_up, 'repeat'},
['DOWN'] = {rt.cursor_down, 'repeat'},
['Shift+UP'] = {rt.cursor_page_up, 'repeat'},
['Shift+DOWN'] = {rt.cursor_page_down, 'repeat'},
['PGUP'] = {rt.cursor_page_up, 'repeat'},
['PGDWN'] = {rt.cursor_page_down, 'repeat'},
['HOME'] = {rt.cursor_start},
['END'] = {rt.cursor_end},
['WHEEL_UP'] = {rt.cursor_wheel_up, 'repeat'},
['WHEEL_DOWN'] = {rt.cursor_wheel_down, 'repeat'},
['Shift+WHEEL_UP'] = {rt.cursor_wheel_page_up, 'repeat'},
['Shift+WHEEL_DOWN'] = {rt.cursor_wheel_page_down, 'repeat'},
['Alt+k'] = {rt.move_option_up, 'repeat'},
['Alt+j'] = {rt.move_option_down, 'repeat'},
['Alt+K'] = {rt.move_option_page_up, 'repeat'},
['Alt+J'] = {rt.move_option_page_down, 'repeat'},
['Alt+UP'] = {rt.move_option_up, 'repeat'},
['Alt+DOWN'] = {rt.move_option_down, 'repeat'},
['Shift+Alt+UP'] = {rt.move_option_page_up, 'repeat'},
['Shift+Alt+DOWN'] = {rt.move_option_page_down, 'repeat'},
['Alt+PGUP'] = {rt.move_option_page_up, 'repeat'},
['Alt+PGDWN'] = {rt.move_option_page_down, 'repeat'},
['Alt+HOME'] = {rt.move_option_start},
['Alt+END'] = {rt.move_option_end},
['Alt+WHEEL_UP'] = {rt.move_option_wheel_up, 'repeat'},
['Alt+WHEEL_DOWN'] = {rt.move_option_wheel_down, 'repeat'},
['Shift+Alt+WHEEL_UP'] = {rt.move_option_wheel_page_up, 'repeat'},
['Shift+Alt+WHEEL_DOWN'] = {rt.move_option_wheel_page_down, 'repeat'},
_targets = {
scrollbar = {
['MBTN_LEFT_DBL'] = {rt.dbl_click_scrollbar},
},
menu = {
['MBTN_LEFT'] = {rt.click_menu},
['MBTN_LEFT_DBL'] = {rt.dbl_click_menu},
['MBTN_RIGHT_DBL'] = {rt.dbl_right_click_menu},
},
},
}
mappings.SEARCH = {
['ANY_UNICODE'] = {rt.search_input_char, 'complex'},
['BS'] = {rt.search_input_bs, 'repeat'},
['DEL'] = {rt.search_input_del, 'repeat'},
['ENTER'] = {rt.end_search},
['ESC'] = {rt.cancel_search},
['Ctrl+c'] = {rt.cancel_search},
['LEFT'] = {rt.search_cursor_left, 'repeat'},
['RIGHT'] = {rt.search_cursor_right, 'repeat'},
['Ctrl+a'] = {rt.search_cursor_start},
['Ctrl+e'] = {rt.search_cursor_end},
}
return input
|