mirror of https://github.com/jetkvm/kvm.git
refactor: use less data to transfer keyboard and mouse events [WIP DO NOT MERGE]
This commit is contained in:
parent
584768bacf
commit
ae775aeeee
|
@ -142,9 +142,11 @@ fi
|
||||||
msg_info "▶ Building go binary"
|
msg_info "▶ Building go binary"
|
||||||
make build_dev
|
make build_dev
|
||||||
|
|
||||||
|
msg_info "▶ Killing any existing instances of the application"
|
||||||
# Kill any existing instances of the application
|
# Kill any existing instances of the application
|
||||||
ssh "${REMOTE_USER}@${REMOTE_HOST}" "killall jetkvm_app_debug || true"
|
ssh "${REMOTE_USER}@${REMOTE_HOST}" "killall jetkvm_app_debug || true"
|
||||||
|
|
||||||
|
msg_info "▶ Copying binary to remote host"
|
||||||
# Copy the binary to the remote host
|
# Copy the binary to the remote host
|
||||||
ssh "${REMOTE_USER}@${REMOTE_HOST}" "cat > ${REMOTE_PATH}/jetkvm_app_debug" < bin/jetkvm_app
|
ssh "${REMOTE_USER}@${REMOTE_HOST}" "cat > ${REMOTE_PATH}/jetkvm_app_debug" < bin/jetkvm_app
|
||||||
|
|
||||||
|
@ -156,6 +158,7 @@ if [ "$RESET_USB_HID_DEVICE" = true ]; then
|
||||||
ssh "${REMOTE_USER}@${REMOTE_HOST}" "ls /sys/class/udc > /sys/kernel/config/usb_gadget/jetkvm/UDC"
|
ssh "${REMOTE_USER}@${REMOTE_HOST}" "ls /sys/class/udc > /sys/kernel/config/usb_gadget/jetkvm/UDC"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
msg_info "▶ Deploying and running the application on the remote host"
|
||||||
# Deploy and run the application on the remote host
|
# Deploy and run the application on the remote host
|
||||||
ssh "${REMOTE_USER}@${REMOTE_HOST}" ash << EOF
|
ssh "${REMOTE_USER}@${REMOTE_HOST}" ash << EOF
|
||||||
set -e
|
set -e
|
||||||
|
|
|
@ -0,0 +1,61 @@
|
||||||
|
package kvm
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/binary"
|
||||||
|
|
||||||
|
"github.com/pion/webrtc/v4"
|
||||||
|
)
|
||||||
|
|
||||||
|
type HidPayloadType uint8
|
||||||
|
|
||||||
|
const (
|
||||||
|
HidPayloadTypeKeyboardReport HidPayloadType = 1
|
||||||
|
HidPayloadTypeKeyboardReportNoModifier HidPayloadType = 2
|
||||||
|
HidPayloadTypeKeyboardReportNothing HidPayloadType = 3
|
||||||
|
HidPayloadTypeAbsMouseReport HidPayloadType = 8
|
||||||
|
HidPayloadTypeRelMouseReport HidPayloadType = 9
|
||||||
|
)
|
||||||
|
|
||||||
|
func handleHidMessage(msg webrtc.DataChannelMessage) {
|
||||||
|
if msg.IsString {
|
||||||
|
webrtcLogger.Info().Interface("msg", msg.Data).Msg("Hid message is a string, skipping")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
payloadType := HidPayloadType(msg.Data[0])
|
||||||
|
switch payloadType {
|
||||||
|
case HidPayloadTypeKeyboardReport:
|
||||||
|
if len(msg.Data) < 3 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
modifier := msg.Data[1]
|
||||||
|
keys := msg.Data[2:]
|
||||||
|
_ = gadget.KeyboardReport(modifier, keys)
|
||||||
|
case HidPayloadTypeKeyboardReportNoModifier:
|
||||||
|
if len(msg.Data) < 2 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
keys := msg.Data[1:]
|
||||||
|
_ = gadget.KeyboardReport(0, keys)
|
||||||
|
case HidPayloadTypeKeyboardReportNothing:
|
||||||
|
_ = gadget.KeyboardReport(0, []byte{})
|
||||||
|
case HidPayloadTypeAbsMouseReport:
|
||||||
|
if len(msg.Data) < 6 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
x := binary.LittleEndian.Uint16(msg.Data[1:3])
|
||||||
|
y := binary.LittleEndian.Uint16(msg.Data[3:5])
|
||||||
|
buttons := msg.Data[5]
|
||||||
|
webrtcLogger.Info().Uint16("x", x).Uint16("y", y).Uint8("buttons", buttons).Msg("Absolute mouse report")
|
||||||
|
_ = gadget.AbsMouseReport(int(x), int(y), buttons)
|
||||||
|
case HidPayloadTypeRelMouseReport:
|
||||||
|
if len(msg.Data) < 4 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
dx := int8(msg.Data[1])
|
||||||
|
dy := int8(msg.Data[2])
|
||||||
|
buttons := msg.Data[3]
|
||||||
|
webrtcLogger.Info().Int8("dx", dx).Int8("dy", dy).Uint8("buttons", buttons).Msg("Relative mouse report")
|
||||||
|
_ = gadget.RelMouseReport(dx, dy, buttons)
|
||||||
|
}
|
||||||
|
}
|
|
@ -214,6 +214,8 @@ func (u *UsbGadget) KeyboardReport(modifier uint8, keys []uint8) error {
|
||||||
u.keyboardLock.Lock()
|
u.keyboardLock.Lock()
|
||||||
defer u.keyboardLock.Unlock()
|
defer u.keyboardLock.Unlock()
|
||||||
|
|
||||||
|
u.log.Trace().Uint8("modifier", modifier).Bytes("keys", keys).Msg("KeyboardReport")
|
||||||
|
|
||||||
if len(keys) > 6 {
|
if len(keys) > 6 {
|
||||||
keys = keys[:6]
|
keys = keys[:6]
|
||||||
}
|
}
|
||||||
|
|
|
@ -88,6 +88,8 @@ func (u *UsbGadget) AbsMouseReport(x, y int, buttons uint8) error {
|
||||||
u.absMouseLock.Lock()
|
u.absMouseLock.Lock()
|
||||||
defer u.absMouseLock.Unlock()
|
defer u.absMouseLock.Unlock()
|
||||||
|
|
||||||
|
u.log.Trace().Int("x", x).Int("y", y).Uint8("buttons", buttons).Msg("AbsMouseReport")
|
||||||
|
|
||||||
err := u.absMouseWriteHidFile([]byte{
|
err := u.absMouseWriteHidFile([]byte{
|
||||||
1, // Report ID 1
|
1, // Report ID 1
|
||||||
buttons, // Buttons
|
buttons, // Buttons
|
||||||
|
@ -108,6 +110,8 @@ func (u *UsbGadget) AbsMouseWheelReport(wheelY int8) error {
|
||||||
u.absMouseLock.Lock()
|
u.absMouseLock.Lock()
|
||||||
defer u.absMouseLock.Unlock()
|
defer u.absMouseLock.Unlock()
|
||||||
|
|
||||||
|
u.log.Trace().Int8("wheelY", wheelY).Msg("AbsMouseWheelReport")
|
||||||
|
|
||||||
// Only send a report if the value is non-zero
|
// Only send a report if the value is non-zero
|
||||||
if wheelY == 0 {
|
if wheelY == 0 {
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -65,7 +65,7 @@ export default function WebRTCVideo() {
|
||||||
|
|
||||||
// RTC related states
|
// RTC related states
|
||||||
const peerConnection = useRTCStore(state => state.peerConnection);
|
const peerConnection = useRTCStore(state => state.peerConnection);
|
||||||
|
const hidDataChannel = useRTCStore(state => state.hidDataChannel);
|
||||||
// HDMI and UI states
|
// HDMI and UI states
|
||||||
const hdmiState = useVideoStore(state => state.hdmiState);
|
const hdmiState = useVideoStore(state => state.hdmiState);
|
||||||
const hdmiError = ["no_lock", "no_signal", "out_of_range"].includes(hdmiState);
|
const hdmiError = ["no_lock", "no_signal", "out_of_range"].includes(hdmiState);
|
||||||
|
@ -253,11 +253,21 @@ export default function WebRTCVideo() {
|
||||||
const sendAbsMouseMovement = useCallback(
|
const sendAbsMouseMovement = useCallback(
|
||||||
(x: number, y: number, buttons: number) => {
|
(x: number, y: number, buttons: number) => {
|
||||||
if (settings.mouseMode !== "absolute") return;
|
if (settings.mouseMode !== "absolute") return;
|
||||||
send("absMouseReport", { x, y, buttons });
|
if (hidDataChannel?.readyState === "open") {
|
||||||
|
const buffer = new ArrayBuffer(6);
|
||||||
|
const view = new DataView(buffer);
|
||||||
|
view.setUint8(0, 8); // type
|
||||||
|
view.setUint16(1, x, true); // x, little-endian
|
||||||
|
view.setUint16(3, y, true); // y, little-endian
|
||||||
|
view.setUint8(5, buttons); // buttons
|
||||||
|
hidDataChannel.send(buffer);
|
||||||
|
} else {
|
||||||
|
send("absMouseReport", { x, y, buttons });
|
||||||
|
}
|
||||||
// We set that for the debug info bar
|
// We set that for the debug info bar
|
||||||
setMousePosition(x, y);
|
setMousePosition(x, y);
|
||||||
},
|
},
|
||||||
[send, setMousePosition, settings.mouseMode],
|
[hidDataChannel?.readyState, send, setMousePosition, settings.mouseMode],
|
||||||
);
|
);
|
||||||
|
|
||||||
const absMouseMoveHandler = useCallback(
|
const absMouseMoveHandler = useCallback(
|
||||||
|
|
|
@ -145,6 +145,9 @@ interface RTCState {
|
||||||
|
|
||||||
terminalChannel: RTCDataChannel | null;
|
terminalChannel: RTCDataChannel | null;
|
||||||
setTerminalChannel: (channel: RTCDataChannel) => void;
|
setTerminalChannel: (channel: RTCDataChannel) => void;
|
||||||
|
|
||||||
|
hidDataChannel: RTCDataChannel | null;
|
||||||
|
setHidDataChannel: (channel: RTCDataChannel) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useRTCStore = create<RTCState>(set => ({
|
export const useRTCStore = create<RTCState>(set => ({
|
||||||
|
@ -154,6 +157,9 @@ export const useRTCStore = create<RTCState>(set => ({
|
||||||
rpcDataChannel: null,
|
rpcDataChannel: null,
|
||||||
setRpcDataChannel: channel => set({ rpcDataChannel: channel }),
|
setRpcDataChannel: channel => set({ rpcDataChannel: channel }),
|
||||||
|
|
||||||
|
hidDataChannel: null,
|
||||||
|
setHidDataChannel: channel => set({ hidDataChannel: channel }),
|
||||||
|
|
||||||
transceiver: null,
|
transceiver: null,
|
||||||
setTransceiver: transceiver => set({ transceiver }),
|
setTransceiver: transceiver => set({ transceiver }),
|
||||||
|
|
||||||
|
|
|
@ -8,21 +8,43 @@ export default function useKeyboard() {
|
||||||
const [send] = useJsonRpc();
|
const [send] = useJsonRpc();
|
||||||
|
|
||||||
const rpcDataChannel = useRTCStore(state => state.rpcDataChannel);
|
const rpcDataChannel = useRTCStore(state => state.rpcDataChannel);
|
||||||
|
const hidDataChannel = useRTCStore(state => state.hidDataChannel);
|
||||||
|
|
||||||
const updateActiveKeysAndModifiers = useHidStore(
|
const updateActiveKeysAndModifiers = useHidStore(
|
||||||
state => state.updateActiveKeysAndModifiers,
|
state => state.updateActiveKeysAndModifiers,
|
||||||
);
|
);
|
||||||
|
|
||||||
const sendKeyboardEvent = useCallback(
|
const sendKeyboardEvent = useCallback(
|
||||||
(keys: number[], modifiers: number[]) => {
|
(keys: number[], modifiers: number[]) => {
|
||||||
if (rpcDataChannel?.readyState !== "open") return;
|
const rpcChannelReady = rpcDataChannel?.readyState === "open";
|
||||||
|
const hidChannelReady = hidDataChannel?.readyState === "open";
|
||||||
|
if (!rpcChannelReady && !hidChannelReady) return;
|
||||||
|
|
||||||
const accModifier = modifiers.reduce((acc, val) => acc + val, 0);
|
const accModifier = modifiers.reduce((acc, val) => acc + val, 0);
|
||||||
|
|
||||||
send("keyboardReport", { keys, modifier: accModifier });
|
if (hidChannelReady) {
|
||||||
|
if (accModifier > 0) {
|
||||||
|
hidDataChannel?.send(new Uint8Array([1, accModifier, ...keys]));
|
||||||
|
} else {
|
||||||
|
if (keys.length > 0) {
|
||||||
|
hidDataChannel?.send(new Uint8Array([2, ...keys]));
|
||||||
|
} else {
|
||||||
|
hidDataChannel?.send(new Uint8Array([3]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
send("keyboardReport", { keys, modifier: accModifier });
|
||||||
|
}
|
||||||
|
|
||||||
// We do this for the info bar to display the currently pressed keys for the user
|
// We do this for the info bar to display the currently pressed keys for the user
|
||||||
updateActiveKeysAndModifiers({ keys: keys, modifiers: modifiers });
|
updateActiveKeysAndModifiers({ keys: keys, modifiers: modifiers });
|
||||||
},
|
},
|
||||||
[rpcDataChannel?.readyState, send, updateActiveKeysAndModifiers],
|
[
|
||||||
|
hidDataChannel?.readyState,
|
||||||
|
rpcDataChannel?.readyState,
|
||||||
|
send,
|
||||||
|
updateActiveKeysAndModifiers,
|
||||||
|
],
|
||||||
);
|
);
|
||||||
|
|
||||||
const resetKeyboardState = useCallback(() => {
|
const resetKeyboardState = useCallback(() => {
|
||||||
|
|
|
@ -138,6 +138,7 @@ export default function KvmIdRoute() {
|
||||||
const setPeerConnection = useRTCStore(state => state.setPeerConnection);
|
const setPeerConnection = useRTCStore(state => state.setPeerConnection);
|
||||||
const setDiskChannel = useRTCStore(state => state.setDiskChannel);
|
const setDiskChannel = useRTCStore(state => state.setDiskChannel);
|
||||||
const setRpcDataChannel = useRTCStore(state => state.setRpcDataChannel);
|
const setRpcDataChannel = useRTCStore(state => state.setRpcDataChannel);
|
||||||
|
const setHidDataChannel = useRTCStore(state => state.setHidDataChannel);
|
||||||
const setTransceiver = useRTCStore(state => state.setTransceiver);
|
const setTransceiver = useRTCStore(state => state.setTransceiver);
|
||||||
const location = useLocation();
|
const location = useLocation();
|
||||||
|
|
||||||
|
@ -485,6 +486,12 @@ export default function KvmIdRoute() {
|
||||||
setRpcDataChannel(rpcDataChannel);
|
setRpcDataChannel(rpcDataChannel);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const hidDataChannel = pc.createDataChannel("hid");
|
||||||
|
hidDataChannel.binaryType = "arraybuffer";
|
||||||
|
hidDataChannel.onopen = () => {
|
||||||
|
setHidDataChannel(hidDataChannel);
|
||||||
|
};
|
||||||
|
|
||||||
const diskDataChannel = pc.createDataChannel("disk");
|
const diskDataChannel = pc.createDataChannel("disk");
|
||||||
diskDataChannel.onopen = () => {
|
diskDataChannel.onopen = () => {
|
||||||
setDiskChannel(diskDataChannel);
|
setDiskChannel(diskDataChannel);
|
||||||
|
@ -501,6 +508,7 @@ export default function KvmIdRoute() {
|
||||||
setPeerConnection,
|
setPeerConnection,
|
||||||
setPeerConnectionState,
|
setPeerConnectionState,
|
||||||
setRpcDataChannel,
|
setRpcDataChannel,
|
||||||
|
setHidDataChannel,
|
||||||
setTransceiver,
|
setTransceiver,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
|
|
@ -117,6 +117,9 @@ func newSession(config SessionConfig) (*Session, error) {
|
||||||
triggerOTAStateUpdate()
|
triggerOTAStateUpdate()
|
||||||
triggerVideoStateUpdate()
|
triggerVideoStateUpdate()
|
||||||
triggerUSBStateUpdate()
|
triggerUSBStateUpdate()
|
||||||
|
case "hid":
|
||||||
|
session.HidChannel = d
|
||||||
|
d.OnMessage(handleHidMessage)
|
||||||
case "disk":
|
case "disk":
|
||||||
session.DiskChannel = d
|
session.DiskChannel = d
|
||||||
d.OnMessage(onDiskMessage)
|
d.OnMessage(onDiskMessage)
|
||||||
|
|
Loading…
Reference in New Issue