Browse Source

Added working cursor move capture

Toby Chui 1 tuần trước cách đây
mục cha
commit
0cb2c4d570

+ 56 - 4
remdeskd/mod/remdeshid/hidcomm.go

@@ -115,6 +115,30 @@ func appendKeyboardEventSubtypes(data []byte, cmd HIDCommand) ([]byte, error) {
 	}
 }
 
+// Append the mouse click event subtypes to the data
+func appendMouseClickEventSubtypes(data []byte, cmd HIDCommand) ([]byte, error) {
+	/* Mouse Click Subtypes */
+	isPress := cmd.EventSubType == FRONTEND_MOUSE_PRESS
+	if isPress {
+		//Mouse Button Press
+		data = append(data, 0x02)
+	} else {
+		//Mouse Button Release
+		data = append(data, 0x03)
+	}
+
+	if cmd.Data == FRONTEND_MOUSE_BTN_LEFT {
+		data = append(data, PAYLOAD_MOUSE_BTN_LEFT)
+	} else if cmd.Data == FRONTEND_MOUSE_BTN_MIDDLE {
+		data = append(data, PAYLOAD_MOUSE_BTN_MID)
+	} else if cmd.Data == FRONTEND_MOUSE_BTN_RIGHT {
+		data = append(data, PAYLOAD_MOUSE_BTN_RIGHT)
+	} else {
+		return nil, fmt.Errorf("invalid HID command subtype: %v", cmd.Data)
+	}
+	return data, nil
+}
+
 // Append the mouse move event subtypes to the data
 func appendMouseMoveEventSubtypes(data []byte, cmd HIDCommand) ([]byte, error) {
 	//The mouse move command requires x_pos, y_pos, x_sign, y_sign
@@ -148,16 +172,46 @@ func appendMouseMoveEventSubtypes(data []byte, cmd HIDCommand) ([]byte, error) {
 
 }
 
+// Append the mouse scroll event subtypes to the data
+// The sensitivityDivider is used to divide the scroll value to make it less sensitive
+func appendMouseScrollEventSubtypes(data []byte, cmd HIDCommand, sensitivityDivider int) ([]byte, error) {
+	//The mouse scroll command PosY contains the scroll value
+	//The scroll command require a direction byte and a scroll value byte
+	scrollValue := cmd.PosY
+	if scrollValue < 0 {
+		//Scroll up
+		scrollValue = -scrollValue
+		if scrollValue > 0x7F {
+			scrollValue = 0x7F
+		}
+		scrollValue /= sensitivityDivider
+		data = append(data, 0x00)
+		data = append(data, byte(scrollValue))
+	} else {
+		//SCroll down
+		if scrollValue > 0x7F {
+			scrollValue = 0x7F
+		}
+		scrollValue /= sensitivityDivider
+		data = append(data, 0x01)
+		data = append(data, byte(scrollValue))
+	}
+
+	return data, nil
+}
+
 //Entry function for converting a HIDCommand to bytes that can be sent over the USBKVM device
 func ConvHIDCommandToBytes(cmd HIDCommand) ([]byte, error) {
 	// Convert the HID command to bytes
 	var data []byte
 	if cmd.EventType == FRONTEND_OPR_TYPE_KEYBOARD_WRITE {
+		/* Keyboard Write Event */
 		data = []byte{OPR_TYPE_KEYBOARD_WRITE}
 		return appendKeyboardEventSubtypes(data, cmd)
 	} else if cmd.EventType == FRONTEND_OPR_TYPE_MOUSE_WRITE {
 		/* Mouse Write Event */
 		data = []byte{OPR_TYPE_MOUSE_WRITE}
+		return appendMouseClickEventSubtypes(data, cmd)
 	} else if cmd.EventType == FRONTEND_OPR_TYPE_MOUSE_MOVE {
 		/* Mouse Move Event */
 		data = []byte{OPR_TYPE_MOUSE_MOVE}
@@ -165,10 +219,8 @@ func ConvHIDCommandToBytes(cmd HIDCommand) ([]byte, error) {
 	} else if cmd.EventType == FRONTEND_OPR_TYPE_MOUSE_SCROLL {
 		/* Mouse Scroll Event */
 		data = []byte{OPR_TYPE_MOUSE_SCROLL}
-
-	} else {
-		return nil, fmt.Errorf("invalid HID command type: %s", cmd.EventType)
+		return appendMouseScrollEventSubtypes(data, cmd, 8)
 	}
 
-	return data, nil
+	return nil, fmt.Errorf("invalid HID command type: %s", cmd.EventType)
 }

+ 23 - 0
remdeskd/mod/remdeshid/hidconv.go

@@ -86,6 +86,29 @@ const (
 	PAYLOAD_KEY_RIGHT_GUI   = 0x07
 )
 
+const (
+	//Frontend Mouse Opr Types
+	FRONTEND_MOUSE_CLICK   = "mc"
+	FRONTEND_MOUSE_PRESS   = "md"
+	FRONTEND_MOUSE_RELEASE = "mu"
+
+	FRONTEND_MOUSE_BTN_LEFT   = "0"
+	FRONTEND_MOUSE_BTN_MIDDLE = "1"
+	FRONTEND_MOUSE_BTN_RIGHT  = "2"
+
+	// Mouse Subtypes
+	SUBTYPE_MOUSE_CLICK   = 0x01 // Mouse button click
+	SUBTYPE_MOUSE_PRESS   = 0x02 // Mouse button press
+	SUBTYPE_MOUSE_RELEASE = 0x03 // Mouse button release
+	SUBTYPE_MOUSE_SETPOS  = 0x04 // Mouse presets position
+	SUBTYPE_MOUSE_RESET   = 0x05 // Reset all mouse button states
+
+	// Mouse Buttons IDs
+	PAYLOAD_MOUSE_BTN_LEFT  = 0x01
+	PAYLOAD_MOUSE_BTN_RIGHT = 0x02
+	PAYLOAD_MOUSE_BTN_MID   = 0x03
+)
+
 // Response Codes
 const (
 	RESP_OK                = 0x00

+ 26 - 2
remdeskd/www/index.html

@@ -16,7 +16,7 @@
 </head>
 <body>
     <h1>Hello World</h1>
-    <p>Welcome to my website!</p>
+    <p>Click start to start connection to backend and start Capture to start KVM-ing</p>
     <button id="startButton">Start</button>
     <button id="stopButton">Stop</button>
     <button id="capture">Capture</button>
@@ -25,13 +25,37 @@
         let protocol = window.location.protocol === 'https:' ? 'wss' : 'ws';
         let port = window.location.port ? window.location.port : (protocol === 'wss' ? 443 : 80);
         let socketURL = `${protocol}://${window.location.hostname}:${port}/hid`;
-
+        
         /* Mouse */
         document.getElementById('capture').addEventListener('click', function() {
             document.body.requestPointerLock();
             document.addEventListener('mousemove', handleMouseMove);
+            document.addEventListener('mousedown', handleMouseDown);
+            document.addEventListener('mouseup', handleMouseUp);
+            document.addEventListener('wheel', handleScroll);
         });
 
+        function handleMouseDown(event) {
+            console.log(`Mouse button pressed: Button=${event.button}`);
+            if (socket) {
+                socket.send(JSON.stringify({ t: 'mw', s: 'md', d: event.button+"" }));
+            }
+        }
+
+        function handleMouseUp(event) {
+            console.log(`Mouse button released: Button=${event.button}`);
+            if (socket) {
+                socket.send(JSON.stringify({ t: 'mw', s: 'mu', d: event.button+"" }));
+            }
+        }
+
+        function handleScroll(event) {
+            console.log(`Mouse scrolled: DeltaX=${event.deltaX}, DeltaY=${event.deltaY}`);
+            if (socket) {
+                socket.send(JSON.stringify({ t: 'ms', y: event.deltaY }));
+            }
+        }
+
         document.addEventListener('pointerlockchange', function() {
             if (document.pointerLockElement === document.body) {
                 console.log('Pointer locked');

+ 2 - 2
usbkvm/debug_terminal/mouse_scroll.bat

@@ -1,6 +1,6 @@
 @echo off
-:: Scroll down 
+:: Scroll up (Tested on Windows)
 .\send.exe COM4 115200 0x04 0x00 0x02
 timeout /t 3 /nobreak >nul
-:: Scroll up
+:: Scroll down
 .\send.exe COM4 115200 0x04 0x01 0x02