natural-harmonia-gropius / input-event

InputEvent.lua for mpv-player, enhanced input.conf with better, conflict-free, low-latency event mechanism.
MIT License
39 stars 5 forks source link

Conditional auto keybind #20

Closed zhongfly closed 1 year ago

zhongfly commented 1 year ago

Just like Conditional auto profiles for input key: bind keys only when conditions are met. Example: When only pause and the file is played to the end, the → button is bound to "next file"

natural-harmonia-gropius commented 1 year ago

忽略掉默认input的报错 https://github.com/Natural-Harmonia-Gropius/InputEvent/pull/21 能满足需求吗?

zhongfly commented 1 year ago

我觉得不是同一个东西 我自己设想的方案,增加语法|或者别的符号来分割按键动作和触发条件 例如:

RIGHT                playlist-next                #@click|pause and eof-reached
RIGHT                seek  5                      #@click

只有满足暂停且播放结束,点击右方向键会跳到下一个文件

natural-harmonia-gropius commented 1 year ago

mpv 的 auto profile 在 C 的部分做了很多 workaround,有些键 lua 的 observer 是无法监听的。

https://github.com/hooke007/MPV_lazy/discussions/220 等等看这个问题有没有解吧

natural-harmonia-gropius commented 1 year ago

临时解决方案的话我可以另外写一个脚本给你,不过要过两天

zhongfly commented 1 year ago

不是条件满足时触发绑定,而是执行按键动作前判断是否需要满足条件、条件是否满足

natural-harmonia-gropius commented 1 year ago

好像是我想复杂了,我试试看,但也要过两天👻

zhongfly commented 1 year ago

我觉得不是同一个东西 我自己设想的方案,增加语法|或者别的符号来分割按键动作和触发条件 例如:

RIGHT                playlist-next                #@click|pause and eof-reached
RIGHT                seek  5                      #@click

只有满足暂停且播放结束,点击右方向键会跳到下一个文件

一些细节的想法: 这会导致一种按键事件可能有多种不同的情况,所以需要考虑优先级:1)排在下面的优先(按照mpv原有的逻辑) 2)所有 有条件的情况都优先于无条件的。

zhongfly commented 1 year ago

https://github.com/zhongfly/InputEvent/tree/conditional-keybind

大概整出来了,简单测试了下能用

模仿auto-profile.lua增加了条件,理论上和profile-cond一样的条件结果

测试 默认开启了使用外部配置 外部配置默认为script-opts\inputevent_key.conf,内容如下

RIGHT           playlist-next                       #@click|pause and time_remaining < 2
i               show-text "double_click|pause"      #@double_click|pause
i               show-text "double_click"            #@double_click
SPACE           no-osd set speed 4; set pause no    #@press

暂停且剩余时间小于2,点击右方向键会跳到下一个文件 暂停时双击i,显示文字"double_click|pause"

natural-harmonia-gropius commented 1 year ago

我电脑现在出问题还在修,ssh key 没有备份。等修好了再看

natural-harmonia-gropius commented 1 year ago

https://github.com/natural-harmonia-gropius/input-event#how-to-integrate-with-other-scripts

你能不能用这个做一个扩展性的脚本,我不太想塞一段很长而又针对性的东西进来 为了避免冲突可以换个前缀,像是 key command #cond@

zhongfly commented 1 year ago

我想不到通过这样实现的方法,因为需要在按下按键时进行判断使用什么命令,然而脚本只提供了绑定按键的接口。

natural-harmonia-gropius commented 1 year ago

一开始的input.conf

SPACE           no-osd set speed 1; set pause no         #@press
SPACE           no-osd add speed 0.1; show-text ${speed} #cond@repeat|speed<4
SPACE           no-osd add speed 0; show-text ${speed}   #cond@repeat|speed>=4

通过接口把它变成这样(我没记错的话单按键绑定行为是合并),在你的脚本里再做分支

SPACE           no-osd set speed 1; set pause no              #@press
SPACE           script-binding inputeventcond/SPACE_repeat    #@repeat
zhongfly commented 1 year ago

还有一个问题就是release-auto和press的处理 假如把press命令是发给扩展脚本,就不能享受release-auto的便利。 如果要恢复这种便利,那就得把release也绑定给扩展脚本,并且需要在扩展脚本里实现release-auto。这么一来,我觉得扩展脚本除了多了“条件判断”、少了“按键处理”之外,和目前的脚本几乎一样了…

当然,这种使用究竟有没有必要还是问题

zhongfly commented 1 year ago

我觉得可以做成一个完全独立的脚本,不局限于与input-event搭配使用。 作用就是对于一个事件会根据不同条件执行不同的命令

想问一下,假如写成这样:

<按键> script-message-to <脚本名> run <event> #@repeat #cond|<real command>|speed<4

你的脚本能正常绑定吗?

natural-harmonia-gropius commented 1 year ago

这一行能正常绑定(如果我没记错)

zhongfly commented 1 year ago

一开始的input.conf

SPACE           no-osd set speed 1; set pause no         #@press
SPACE           no-osd add speed 0.1; show-text ${speed} #cond@repeat|speed<4
SPACE           no-osd add speed 0; show-text ${speed}   #cond@repeat|speed>=4

通过接口把它变成这样(我没记错的话单按键绑定行为是合并),在你的脚本里再做分支

SPACE           no-osd set speed 1; set pause no              #@press
SPACE           script-binding inputeventcond/SPACE_repeat    #@repeat

这个方法似乎挺好的……还是先按这个来

zhongfly commented 1 year ago

绑定可能出问题。 如果使用扩展脚本的话,会不会出现多个脚本同时调用bind,这2行 https://github.com/natural-harmonia-gropius/input-event/blob/04a65782b2604acb54ad26cf2910bd4f016d3da8/inputevent.lua#L331 https://github.com/natural-harmonia-gropius/input-event/blob/04a65782b2604acb54ad26cf2910bd4f016d3da8/inputevent.lua#L335 就有可能使最后的结果产生意外?

例如,脚本1和脚本2同时想对同一个键添加新绑定,结果最后的结果可能是

然后来到主脚本 扩展脚本 ,它们读取了配置文件,都想要进行绑定,先后顺序可能还不一定。为了实现本issue的目标,肯定希望使主脚本先,扩展脚本覆盖。

natural-harmonia-gropius commented 1 year ago

理论上不会吧……单线程也没异步的脚本 按照我的设想如果a绑了a @click,b绑了b @press, c绑了 c@click, 最终是 {click:c, press:b}

zhongfly commented 1 year ago

问题在于主脚本 与 扩展脚本 ,它们都读取了配置文件,都想要进行绑定,先后顺序可能还不一定。为了实现本issue的目标,肯定希望使配置更新时,无论谁先处理完配置,都是主脚本先绑定,然后扩展脚本覆盖。

zhongfly commented 1 year ago

可能需要主脚本用script-message来广播已经完成了配置文件绑定?不了解client怎么接受处理这个

natural-harmonia-gropius commented 1 year ago

脚本1和脚本2同时想对同一个键添加新绑定

按照现在的设计 conf 会长成这样, 应该没有这样用的…… key command #@click #a #b

运行时的冲突我现在没什么想法,有空我试试这个 https://github.com/mpv-player/mpv/pull/11225

另外这脚本自身就支持热重载,在刚开始的一段时间以外其他脚本绑定的可靠性也难以保证 https://github.com/natural-harmonia-gropius/input-event#support-for-multiple-configuration-files

感觉确实需要弄个优先级……头大

zhongfly commented 1 year ago

总的来说,用扩展脚本来做感觉复杂了很多,还不如直接改

zhongfly commented 1 year ago

https://github.com/zhongfly/command_cond 先这么做着吧……独立的脚本已经能覆盖绝大多数情况了

SPACE      no-osd set speed 1; set pause no          #@press
SPACE      script-message-to command_cond speedup    #@repeat
SPACE      ignore                                    #@release
SPACE      cycle pause                               #@click
speedup           no-osd add speed 0.1; show-text ${speed} #|speed<4
speedup           no-osd add speed 0; show-text ${speed}   #|speed>=4