从硬件选择到实现kodi的完美播放(十)
资讯
2024-01-28
397
10.添加脚本让kodi能直接遥控器启动
上次我们给kodi配置好了遥控器,在开着kodi的情况下可以很容易的进行各种操作了,但是在kodi没启动的时候是没有办法通过遥控器的开关键来实现kodi的开关,今天我们就来设置一下,使得遥控器可以直接开关kodi,这样使用起来就像使用电视盒子一样方便了。
用遥控器操控PVE中直装的Kodi,实现打开和关闭Kodi,这个场景需要在无桌面环境下,劫持遥控器的电源键,让它的作用变更为打开/关闭Kodi,所以只是将电源键映射为其他键是不行的,而是需要劫持遥控器的电源键,让它变更为运行某个脚本,然后由脚本实现打开/关闭Kodi。
# 安装evtest
apt install evtest
# 运行evtest
evtest --grab
# evtest会列出所有可用的输入设备,可以看到我的遥控器有event5-event8共4个设备
# 你可以选择具体的编号来进行测试,找到每个设备分别能控制哪些按键
# 经过测试,我发现我遥控器的电源键在event8,也就是2.4G Composite Devic System Control这个设备
No device specified, trying to scan all of /dev/input/event*
Available devices:
/dev/input/event0: Power Button
/dev/input/event1: AT Translated Set 2 keyboard
/dev/input/event2: VirtualPS/2 VMware VMMouse
/dev/input/event3: VirtualPS/2 VMware VMMouse
/dev/input/event4: QEMU QEMU USB Tablet
/dev/input/event5: 2.4G Composite Devic
/dev/input/event6: 2.4G Composite Devic Mouse
/dev/input/event7: 2.4G Composite Devic Consumer Control
/dev/input/event8: 2.4G Composite Devic System Control
/dev/input/event9: PC Speaker
Select the device event number [0-9]:8 # 手动输入要测试的设备编号,如果不清楚电源键在什么设备上,你可以一个一个试出来
Input device ID: bus 0x3 vendor 0x627 product 0x697d version 0x110
Input device name: "2.4G Composite Devic System Control"
Supported events:
Event type 0 (EV_SYN)
Event type 1 (EV_KEY)
Event code 116 (KEY_POWER)
Event code 142 (KEY_SLEEP)
Event code 143 (KEY_WAKEUP)
Event type 4 (EV_MSC)
Event code 4 (MSC_SCAN)
Properties:
Testing ... (interrupt to exit) # 按下电源键,evtest输出以下结果
Event: time 1669209045.421999, type 4 (EV_MSC), code 4 (MSC_SCAN), value 10081
Event: time 1669209045.421999, type 1 (EV_KEY), code 116 (KEY_POWER), value 1
Event: time 1669209045.421999, -------------- SYN_REPORT ------------
Event: time 1669209045.422021, type 1 (EV_KEY), code 116 (KEY_POWER), value 0
Event: time 1669209045.422021, -------------- SYN_REPORT ------------
# Ctrl+C退出
通过测试,我知道了我遥控器的电源键的键码为116,键值为KEY_POWER,去掉KEY_,键值power这个值在后面会用到。
现在来找一下event8这个设备在/dev/input/by-id下的软连接叫什么,运行ls -l /dev/input/by-id/:
total 0
lrwxrwxrwx 1 root root 9 Nov 23 22:40 usb-0627_2.4G_Composite_Devic-event-if01 -> ../event7
lrwxrwxrwx 1 root root 9 Nov 23 22:40 usb-0627_2.4G_Composite_Devic-event-kbd -> ../event5
lrwxrwxrwx 1 root root 9 Nov 23 22:40 usb-0627_2.4G_Composite_Devic-if01-event-mouse -> ../event6
lrwxrwxrwx 1 root root 9 Nov 23 22:40 usb-0627_2.4G_Composite_Devic-if01-mouse -> ../mouse3
lrwxrwxrwx 1 root root 9 Nov 23 22:40 usb-QEMU_QEMU_USB_Tablet_28754-0000:00:01.2-1-event-mouse -> ../event4
lrwxrwxrwx 1 root root 9 Nov 23 22:40 usb-QEMU_QEMU_USB_Tablet_28754-0000:00:01.2-1-mouse -> ../mouse2
这...udev没有识别出来...准确的讲,是因为udev的默认规则无法区分2.4G Composite Devic Consumer Control和2.4G Composite Devic System Control这两个设备,现象是每次关机再开机或每次卸载再插入接收器后,usb-0627_2.4G_Composite_Devic-event-if01这个设备一会儿指向2.4G Composite Devic Consumer Control,一会儿指向2.4G Composite Devic System Control,所以我需要修改一下udev的规则,让它能够准确识别出2.4G Composite Devic System Control。可能其他遥控器不存在此问题,需要你自行测试一下此问题存在不存在。
现在来让udev能准确识别2.4G Composite Devic System Control,运行udevadm info -a -p $(udevadm info -q path -n /dev/input/event8)(其中event8是根据上面evtest测试后知道的),输出内容:
Udevadm info starts with the device specified by the devpath and then
walks up the chain of parent devices. It prints for every device
found, all possible attributes in the udev rules key format.
A rule to match, can be composed by the attributes of the device
and the attributes from one single parent device.
looking at device '/devices/pci0000:00/0000:00:1e.0/0000:01:1b.0/usb2/2-1/2-1:1.1/0003:0627:697D.0003/input/input9/event8':
KERNEL=="event8"
SUBSYSTEM=="input"
DRIVER==""
ATTR{power/async}=="disabled"
ATTR{power/control}=="auto"
ATTR{power/runtime_active_kids}=="0"
ATTR{power/runtime_active_time}=="0"
ATTR{power/runtime_enabled}=="disabled"
ATTR{power/runtime_status}=="unsupported"
ATTR{power/runtime_suspended_time}=="0"
ATTR{power/runtime_usage}=="0"
looking at parent device '/devices/pci0000:00/0000:00:1e.0/0000:01:1b.0/usb2/2-1/2-1:1.1/0003:0627:697D.0003/input/input9':
KERNELS=="input9"
SUBSYSTEMS=="input"
DRIVERS==""
ATTRS{capabilities/abs}=="0"
ATTRS{capabilities/ev}=="13"
ATTRS{capabilities/ff}=="0"
ATTRS{capabilities/key}=="c000 10000000000000 0"
ATTRS{capabilities/led}=="0"
ATTRS{capabilities/msc}=="10"
ATTRS{capabilities/rel}=="0"
ATTRS{capabilities/snd}=="0"
ATTRS{capabilities/sw}=="0"
ATTRS{id/bustype}=="0003"
ATTRS{id/product}=="697d"
ATTRS{id/vendor}=="0627"
ATTRS{id/version}=="0110"
ATTRS{inhibited}=="0"
ATTRS{name}=="2.4G Composite Devic System Control"
ATTRS{phys}=="usb-0000:01:1b.0-1/input1"
ATTRS{power/async}=="disabled"
ATTRS{power/control}=="auto"
ATTRS{power/runtime_active_kids}=="0"
ATTRS{power/runtime_active_time}=="0"
ATTRS{power/runtime_enabled}=="disabled"
ATTRS{power/runtime_status}=="unsupported"
ATTRS{power/runtime_suspended_time}=="0"
ATTRS{power/runtime_usage}=="0"
ATTRS{properties}=="0"
ATTRS{uniq}==""
通过多次运行udevadm info -a -p $(udevadm info -q path -n /dev/input/eventX)(X更换为不同的设备编号)可知,我的设备2.4G Composite Devic System Control可以通过ATTRS{name}这个属性即可以进行唯一识别。参考 udev帮助文档,新建/etc/udev/rules.d/01-system-control.rules,内容如下:
ATTRS{name}=="2.4G Composite Devic System Control", SYMLINK+="input/by-id/usb-0627_2.4G_Composite_Devic-System-Control"
其中SYMLINK+="input/by-id/usb-0627_2.4G_Composite_Devic-System-Control"是指让udev在/dev/input/by-id下自动创建usb-0627_2.4G_Composite_Devic-System-Control,其指向2.4G Composite Devic System Control这个设备。软连接名称你可以自行定义。现在重启下或者重插一下接收器就可以识别出来了
下载预编译好的evsieve:
wget https://raw.githubusercontent.com/devome/files/master/evsieve/evsieve -O /usr/local/bin/evsieve
chmod +x /usr/local/bin/evsieve
apt install libevdev2
新建一个用来自动判断是启动Kodi还是关闭Kodi的脚本/usr/local/bin/kodi-power.sh,后续由evsieve来使用,内容如下:
#!/usr/bin/env bash
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
if [[ -z $(ps -ef grep "kodi.bin" grep -v "grep") ]]; then ## 如果kodi还没有启动,那么就启动它
systemctl start kodi.service
else ## 如果kodi已经启动了,那么就停止它
systemctl stop kodi.service
fi
注意增加可执行权限:chmod +x /usr/local/bin/kodi-power.sh。
根据evsieve的说明,在运行evsieve时,它所要劫持的设备必须已经插入了,但是我又希望evsieve能够开机自动运行,所以我需要在正式运行前先确保2.4G Composite Devic System Control这个设备已经插入。这项工作就交给脚本来判断吧,新建/usr/local/bin/run-evsieve.sh,内容如下(注意按照注释修改target_hijack_input_device和power):
#!/usr/bin/env bash
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
## 要劫持的目标设备的绝对路径,请注意修改成自己的
## 不要直接写/dev/input/eventX,而要写/dev/input/by-id下的设备
target_hijack_input_device="/dev/input/by-id/usb-0627_2.4G_Composite_Devic-System-Control"
## 先检测 target_hijack_input_device 是否已经存在
while :; do
if [[ -L "$target_hijack_input_device" ]]; then
break
else
echo "The '$target_hijack_input_device' is not inserted, wait 60 seconds..."
sleep 60
fi
done
## 执行电源键劫持,当按下KEY_POWER时,转换为运行脚本/usr/local/bin/kodi-power.sh,详细用法请见:https://github.com/KarsMulder/evsieve
## 注意将下方key:后的 power 修改为你通过evtest获取到的键值
exec evsieve \
--input "$target_hijack_input_device" grab persist=reopen \
--hook key:power exec-shell="/usr/local/bin/kodi-power.sh" \
--block key:power
该脚本同样需要可执行权限:chmod +x /usr/local/bin/run-evsieve.sh。
新建/etc/systemd/system/evsieve.service,内容如下:
[Unit]
Description = Run evsieve
Requires = systemd-udevd.service
After = systemd-udevd.service
Wants = systemd-udevd.service
[Service]
User = root
Group = root
ExecStart = /usr/local/bin/run-evsieve.sh
ExecStop = /usr/bin/killall --user root --exact --wait evsieve
TimeoutStartSec = infinity
Restart = on-abort
[Install]
WantedBy = multi-user.target
让evsieve.service开机启动启动。这个必须得开机自动启动,要不然劫持不了。
## 设置开机自动启动,并立即运行起来
systemctl enable --now evsieve.service
## 再看看是不是evsieve.service是不是启动好了
systemctl status evsieve.service
## 如果像这样就表示启动好啦
evsieve.service - Run evsieve
Loaded: loaded (/etc/systemd/system/evsieve.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2022-11-24 18:11:27 CST; 36min ago
Main PID: 1249 (evsieve)
Tasks: 2 (limit: 72123)
Memory: 1.8M
CPU: 59ms
CGroup: /system.slice/evsieve.service
└─1249 evsieve --input /dev/input/by-id/usb-0627_2.4G_Composite_Devic-System-Control grab persist=reopen --hook key:power exec-shell=/usr/local/bin/kodi-power.sh --block key:power
11月 24 18:11:27 pve systemd[1]: Started Run evsieve.
本站涵盖的内容、图片、视频等数据系网络收集,部分未能与原作者取得联系。若涉及版权问题,请联系我们删除!联系邮箱:ynstorm@foxmail.com 谢谢支持!
10.添加脚本让kodi能直接遥控器启动
上次我们给kodi配置好了遥控器,在开着kodi的情况下可以很容易的进行各种操作了,但是在kodi没启动的时候是没有办法通过遥控器的开关键来实现kodi的开关,今天我们就来设置一下,使得遥控器可以直接开关kodi,这样使用起来就像使用电视盒子一样方便了。
用遥控器操控PVE中直装的Kodi,实现打开和关闭Kodi,这个场景需要在无桌面环境下,劫持遥控器的电源键,让它的作用变更为打开/关闭Kodi,所以只是将电源键映射为其他键是不行的,而是需要劫持遥控器的电源键,让它变更为运行某个脚本,然后由脚本实现打开/关闭Kodi。
# 安装evtest
apt install evtest
# 运行evtest
evtest --grab
# evtest会列出所有可用的输入设备,可以看到我的遥控器有event5-event8共4个设备
# 你可以选择具体的编号来进行测试,找到每个设备分别能控制哪些按键
# 经过测试,我发现我遥控器的电源键在event8,也就是2.4G Composite Devic System Control这个设备
No device specified, trying to scan all of /dev/input/event*
Available devices:
/dev/input/event0: Power Button
/dev/input/event1: AT Translated Set 2 keyboard
/dev/input/event2: VirtualPS/2 VMware VMMouse
/dev/input/event3: VirtualPS/2 VMware VMMouse
/dev/input/event4: QEMU QEMU USB Tablet
/dev/input/event5: 2.4G Composite Devic
/dev/input/event6: 2.4G Composite Devic Mouse
/dev/input/event7: 2.4G Composite Devic Consumer Control
/dev/input/event8: 2.4G Composite Devic System Control
/dev/input/event9: PC Speaker
Select the device event number [0-9]:8 # 手动输入要测试的设备编号,如果不清楚电源键在什么设备上,你可以一个一个试出来
Input device ID: bus 0x3 vendor 0x627 product 0x697d version 0x110
Input device name: "2.4G Composite Devic System Control"
Supported events:
Event type 0 (EV_SYN)
Event type 1 (EV_KEY)
Event code 116 (KEY_POWER)
Event code 142 (KEY_SLEEP)
Event code 143 (KEY_WAKEUP)
Event type 4 (EV_MSC)
Event code 4 (MSC_SCAN)
Properties:
Testing ... (interrupt to exit) # 按下电源键,evtest输出以下结果
Event: time 1669209045.421999, type 4 (EV_MSC), code 4 (MSC_SCAN), value 10081
Event: time 1669209045.421999, type 1 (EV_KEY), code 116 (KEY_POWER), value 1
Event: time 1669209045.421999, -------------- SYN_REPORT ------------
Event: time 1669209045.422021, type 1 (EV_KEY), code 116 (KEY_POWER), value 0
Event: time 1669209045.422021, -------------- SYN_REPORT ------------
# Ctrl+C退出
通过测试,我知道了我遥控器的电源键的键码为116,键值为KEY_POWER,去掉KEY_,键值power这个值在后面会用到。
现在来找一下event8这个设备在/dev/input/by-id下的软连接叫什么,运行ls -l /dev/input/by-id/:
total 0
lrwxrwxrwx 1 root root 9 Nov 23 22:40 usb-0627_2.4G_Composite_Devic-event-if01 -> ../event7
lrwxrwxrwx 1 root root 9 Nov 23 22:40 usb-0627_2.4G_Composite_Devic-event-kbd -> ../event5
lrwxrwxrwx 1 root root 9 Nov 23 22:40 usb-0627_2.4G_Composite_Devic-if01-event-mouse -> ../event6
lrwxrwxrwx 1 root root 9 Nov 23 22:40 usb-0627_2.4G_Composite_Devic-if01-mouse -> ../mouse3
lrwxrwxrwx 1 root root 9 Nov 23 22:40 usb-QEMU_QEMU_USB_Tablet_28754-0000:00:01.2-1-event-mouse -> ../event4
lrwxrwxrwx 1 root root 9 Nov 23 22:40 usb-QEMU_QEMU_USB_Tablet_28754-0000:00:01.2-1-mouse -> ../mouse2
这...udev没有识别出来...准确的讲,是因为udev的默认规则无法区分2.4G Composite Devic Consumer Control和2.4G Composite Devic System Control这两个设备,现象是每次关机再开机或每次卸载再插入接收器后,usb-0627_2.4G_Composite_Devic-event-if01这个设备一会儿指向2.4G Composite Devic Consumer Control,一会儿指向2.4G Composite Devic System Control,所以我需要修改一下udev的规则,让它能够准确识别出2.4G Composite Devic System Control。可能其他遥控器不存在此问题,需要你自行测试一下此问题存在不存在。
现在来让udev能准确识别2.4G Composite Devic System Control,运行udevadm info -a -p $(udevadm info -q path -n /dev/input/event8)(其中event8是根据上面evtest测试后知道的),输出内容:
Udevadm info starts with the device specified by the devpath and then
walks up the chain of parent devices. It prints for every device
found, all possible attributes in the udev rules key format.
A rule to match, can be composed by the attributes of the device
and the attributes from one single parent device.
looking at device '/devices/pci0000:00/0000:00:1e.0/0000:01:1b.0/usb2/2-1/2-1:1.1/0003:0627:697D.0003/input/input9/event8':
KERNEL=="event8"
SUBSYSTEM=="input"
DRIVER==""
ATTR{power/async}=="disabled"
ATTR{power/control}=="auto"
ATTR{power/runtime_active_kids}=="0"
ATTR{power/runtime_active_time}=="0"
ATTR{power/runtime_enabled}=="disabled"
ATTR{power/runtime_status}=="unsupported"
ATTR{power/runtime_suspended_time}=="0"
ATTR{power/runtime_usage}=="0"
looking at parent device '/devices/pci0000:00/0000:00:1e.0/0000:01:1b.0/usb2/2-1/2-1:1.1/0003:0627:697D.0003/input/input9':
KERNELS=="input9"
SUBSYSTEMS=="input"
DRIVERS==""
ATTRS{capabilities/abs}=="0"
ATTRS{capabilities/ev}=="13"
ATTRS{capabilities/ff}=="0"
ATTRS{capabilities/key}=="c000 10000000000000 0"
ATTRS{capabilities/led}=="0"
ATTRS{capabilities/msc}=="10"
ATTRS{capabilities/rel}=="0"
ATTRS{capabilities/snd}=="0"
ATTRS{capabilities/sw}=="0"
ATTRS{id/bustype}=="0003"
ATTRS{id/product}=="697d"
ATTRS{id/vendor}=="0627"
ATTRS{id/version}=="0110"
ATTRS{inhibited}=="0"
ATTRS{name}=="2.4G Composite Devic System Control"
ATTRS{phys}=="usb-0000:01:1b.0-1/input1"
ATTRS{power/async}=="disabled"
ATTRS{power/control}=="auto"
ATTRS{power/runtime_active_kids}=="0"
ATTRS{power/runtime_active_time}=="0"
ATTRS{power/runtime_enabled}=="disabled"
ATTRS{power/runtime_status}=="unsupported"
ATTRS{power/runtime_suspended_time}=="0"
ATTRS{power/runtime_usage}=="0"
ATTRS{properties}=="0"
ATTRS{uniq}==""
通过多次运行udevadm info -a -p $(udevadm info -q path -n /dev/input/eventX)(X更换为不同的设备编号)可知,我的设备2.4G Composite Devic System Control可以通过ATTRS{name}这个属性即可以进行唯一识别。参考 udev帮助文档,新建/etc/udev/rules.d/01-system-control.rules,内容如下:
ATTRS{name}=="2.4G Composite Devic System Control", SYMLINK+="input/by-id/usb-0627_2.4G_Composite_Devic-System-Control"
其中SYMLINK+="input/by-id/usb-0627_2.4G_Composite_Devic-System-Control"是指让udev在/dev/input/by-id下自动创建usb-0627_2.4G_Composite_Devic-System-Control,其指向2.4G Composite Devic System Control这个设备。软连接名称你可以自行定义。现在重启下或者重插一下接收器就可以识别出来了
下载预编译好的evsieve:
wget https://raw.githubusercontent.com/devome/files/master/evsieve/evsieve -O /usr/local/bin/evsieve
chmod +x /usr/local/bin/evsieve
apt install libevdev2
新建一个用来自动判断是启动Kodi还是关闭Kodi的脚本/usr/local/bin/kodi-power.sh,后续由evsieve来使用,内容如下:
#!/usr/bin/env bash
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
if [[ -z $(ps -ef grep "kodi.bin" grep -v "grep") ]]; then ## 如果kodi还没有启动,那么就启动它
systemctl start kodi.service
else ## 如果kodi已经启动了,那么就停止它
systemctl stop kodi.service
fi
注意增加可执行权限:chmod +x /usr/local/bin/kodi-power.sh。
根据evsieve的说明,在运行evsieve时,它所要劫持的设备必须已经插入了,但是我又希望evsieve能够开机自动运行,所以我需要在正式运行前先确保2.4G Composite Devic System Control这个设备已经插入。这项工作就交给脚本来判断吧,新建/usr/local/bin/run-evsieve.sh,内容如下(注意按照注释修改target_hijack_input_device和power):
#!/usr/bin/env bash
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
## 要劫持的目标设备的绝对路径,请注意修改成自己的
## 不要直接写/dev/input/eventX,而要写/dev/input/by-id下的设备
target_hijack_input_device="/dev/input/by-id/usb-0627_2.4G_Composite_Devic-System-Control"
## 先检测 target_hijack_input_device 是否已经存在
while :; do
if [[ -L "$target_hijack_input_device" ]]; then
break
else
echo "The '$target_hijack_input_device' is not inserted, wait 60 seconds..."
sleep 60
fi
done
## 执行电源键劫持,当按下KEY_POWER时,转换为运行脚本/usr/local/bin/kodi-power.sh,详细用法请见:https://github.com/KarsMulder/evsieve
## 注意将下方key:后的 power 修改为你通过evtest获取到的键值
exec evsieve \
--input "$target_hijack_input_device" grab persist=reopen \
--hook key:power exec-shell="/usr/local/bin/kodi-power.sh" \
--block key:power
该脚本同样需要可执行权限:chmod +x /usr/local/bin/run-evsieve.sh。
新建/etc/systemd/system/evsieve.service,内容如下:
[Unit]
Description = Run evsieve
Requires = systemd-udevd.service
After = systemd-udevd.service
Wants = systemd-udevd.service
[Service]
User = root
Group = root
ExecStart = /usr/local/bin/run-evsieve.sh
ExecStop = /usr/bin/killall --user root --exact --wait evsieve
TimeoutStartSec = infinity
Restart = on-abort
[Install]
WantedBy = multi-user.target
让evsieve.service开机启动启动。这个必须得开机自动启动,要不然劫持不了。
## 设置开机自动启动,并立即运行起来
systemctl enable --now evsieve.service
## 再看看是不是evsieve.service是不是启动好了
systemctl status evsieve.service
## 如果像这样就表示启动好啦
evsieve.service - Run evsieve
Loaded: loaded (/etc/systemd/system/evsieve.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2022-11-24 18:11:27 CST; 36min ago
Main PID: 1249 (evsieve)
Tasks: 2 (limit: 72123)
Memory: 1.8M
CPU: 59ms
CGroup: /system.slice/evsieve.service
└─1249 evsieve --input /dev/input/by-id/usb-0627_2.4G_Composite_Devic-System-Control grab persist=reopen --hook key:power exec-shell=/usr/local/bin/kodi-power.sh --block key:power
11月 24 18:11:27 pve systemd[1]: Started Run evsieve.
本站涵盖的内容、图片、视频等数据系网络收集,部分未能与原作者取得联系。若涉及版权问题,请联系我们删除!联系邮箱:ynstorm@foxmail.com 谢谢支持!