Przeglądaj źródła

Added cursor position in mouse move

TC 4 dni temu
rodzic
commit
7e88e24cfc

+ 1 - 2
remdeskd/main.go

@@ -66,8 +66,7 @@ func main() {
 
 		time.Sleep(2 * time.Second) // Wait for the controller to initialize
 		//Configure the HID controller
-		usbKVM.ConfigureChipTo115200()
-
+		usbKVM.ConfigureChipTo19200()
 	case "usbkvm":
 		log.Println("Starting in USB KVM mode...")
 

+ 29 - 5
remdeskd/mod/remdeshid/ch9329.go

@@ -6,7 +6,7 @@ import (
 	"time"
 )
 
-func (c *Controller) ConfigureChipTo115200() error {
+func (c *Controller) ConfigureChipTo19200() error {
 	// Send the command to get chip configuration and info
 	currentConfig, err := c.GetChipCurrentConfiguration()
 	if err != nil {
@@ -43,7 +43,7 @@ func (c *Controller) ConfigureChipTo115200() error {
 		fmt.Printf("0x%02X ", b)
 	}
 	fmt.Println()
-	fmt.Println("Baudrate updated to 115200 successfully")
+	fmt.Println("Baudrate updated to 19200 successfully")
 	return nil
 }
 
@@ -117,10 +117,34 @@ func (c *Controller) ConstructAndSendCmd(HIDCommand *HIDCommand) ([]byte, error)
 		}
 		return c.SendKeyboardRelease(uint8(HIDCommand.Keycode))
 	case EventTypeMouseMove:
-		if time.Now().UnixMilli()-c.lastCursorEventTime < MinCusorEventInterval {
-			// Ignore mouse move events that are too close together
-			return []byte{}, nil
+		//if time.Now().UnixMilli()-c.lastCursorEventTime < MinCusorEventInterval {
+		//	// Ignore mouse move events that are too close together
+		//	return []byte{}, nil
+		//}
+
+		//Map mouse button state to HID state
+		leftPressed := (HIDCommand.MouseMoveButtonState & 0x01) != 0
+		middlePressed := (HIDCommand.MouseMoveButtonState & 0x02) != 0
+		rightPressed := (HIDCommand.MouseMoveButtonState & 0x04) != 0
+		if leftPressed {
+			c.hidState.MouseButtons |= 0x01
+		} else {
+			c.hidState.MouseButtons &^= 0x01
 		}
+
+		if middlePressed {
+			c.hidState.MouseButtons |= 0x04
+		} else {
+			c.hidState.MouseButtons &^= 0x04
+		}
+
+		if rightPressed {
+			c.hidState.MouseButtons |= 0x02
+		} else {
+			c.hidState.MouseButtons &^= 0x02
+		}
+
+		// Update mouse position
 		c.lastCursorEventTime = time.Now().UnixMilli()
 		if HIDCommand.MouseAbsX != 0 || HIDCommand.MouseAbsY != 0 {
 			xLSB := byte(HIDCommand.MouseAbsX & 0xFF)        // Extract LSB of X

+ 1 - 1
remdeskd/mod/remdeshid/handler.go

@@ -33,7 +33,7 @@ func (c *Controller) HIDWebSocketHandler(w http.ResponseWriter, r *http.Request)
 			log.Println("Error reading message:", err)
 			break
 		}
-		log.Printf("Received: %s", message)
+		//log.Printf("Received: %s", message)
 
 		//Try parsing the message as a HIDCommand
 		var hidCmd HIDCommand

+ 1 - 2
remdeskd/mod/remdeshid/mouse.go

@@ -4,7 +4,6 @@ import (
 	"bytes"
 	"encoding/binary"
 	"errors"
-	"fmt"
 )
 
 // calcChecksum calculates the checksum for a given data slice.
@@ -126,6 +125,6 @@ func (c *Controller) MouseScroll(tilt int) ([]byte, error) {
 		wheel = uint8(0xFF - c.Config.ScrollSensitivity)
 	}
 
-	fmt.Println(tilt, "-->", wheel)
+	//fmt.Println(tilt, "-->", wheel)
 	return c.MouseMoveRelative(0, 0, wheel)
 }

+ 0 - 1
remdeskd/mod/remdeshid/remdeshid.go

@@ -59,7 +59,6 @@ func (c *Controller) Connect() error {
 				//for i := 0; i < n; i++ {
 				//	fmt.Printf("0x%02X ", buf[i])
 				//}
-				fmt.Println()
 			}
 		}
 	}()

+ 11 - 10
remdeskd/mod/remdeshid/typedef.go

@@ -14,7 +14,7 @@ const (
 	EventTypeHIDCommand
 )
 
-const MinCusorEventInterval = 40 // Minimum interval between cursor events in milliseconds
+const MinCusorEventInterval = 30 // Minimum interval between cursor events in milliseconds
 
 type Config struct {
 	/* Serial port configs */
@@ -47,13 +47,14 @@ type Controller struct {
 }
 
 type HIDCommand struct {
-	Event         EventType `json:"event"`
-	Keycode       int       `json:"keycode,omitempty"`
-	IsRightModKey bool      `json:"is_right_modifier_key,omitempty"` // true if the key is a right modifier key (Ctrl, Shift, Alt, GUI)
-	MouseAbsX     int       `json:"mouse_x,omitempty"`               // Absolute mouse position in X direction
-	MouseAbsY     int       `json:"mouse_y,omitempty"`               // Absolute mouse position in Y direction
-	MouseRelX     int       `json:"mouse_rel_x,omitempty"`           // Relative mouse movement in X direction
-	MouseRelY     int       `json:"mouse_rel_y,omitempty"`           // Relative mouse movement in Y direction
-	MouseButton   int       `json:"mouse_button,omitempty"`          //0x01 for left click, 0x02 for right click, 0x03 for middle clicks
-	MouseScroll   int       `json:"mouse_scroll,omitempty"`          // Positive for scroll up, negative for scroll down, max 127
+	Event                EventType `json:"event"`
+	Keycode              int       `json:"keycode,omitempty"`
+	IsRightModKey        bool      `json:"is_right_modifier_key,omitempty"`   // true if the key is a right modifier key (Ctrl, Shift, Alt, GUI)
+	MouseAbsX            int       `json:"mouse_x,omitempty"`                 // Absolute mouse position in X direction
+	MouseAbsY            int       `json:"mouse_y,omitempty"`                 // Absolute mouse position in Y direction
+	MouseRelX            int       `json:"mouse_rel_x,omitempty"`             // Relative mouse movement in X direction
+	MouseRelY            int       `json:"mouse_rel_y,omitempty"`             // Relative mouse movement in Y direction
+	MouseMoveButtonState int       `json:"mouse_move_button_state,omitempty"` // Mouse button state during move,
+	MouseButton          int       `json:"mouse_button,omitempty"`            //0x01 for left click, 0x02 for right click, 0x03 for middle clicks
+	MouseScroll          int       `json:"mouse_scroll,omitempty"`            // Positive for scroll up, negative for scroll down, max 127
 }

+ 19 - 2
remdeskd/www/index.html

@@ -36,16 +36,22 @@
         let socketURL = `${protocol}://${window.location.hostname}:${port}/hid`;
         let mouseMoveAbsolte = true; // Set to true for absolute mouse coordinates, false for relativeZ
         let mouseIsOutside = false;
+        let mouseButtonState = [0, 0, 0]; // Array to track mouse button states, left, middle, right
 
         /* Mouse events */
         function handleMouseMove(event) {
+            const mouseButtonBits = mouseButtonState.reduce((acc, state, index) => {
+                return acc | (state << index);
+            }, 0);
             const hidCommand = {
                 event: 2,
                 mouse_x: event.clientX,
-                mouse_y: event.clientY
+                mouse_y: event.clientY,
+                mouse_move_button_state: mouseButtonBits // Combine mouse button states into a single bitmask
             };
 
 
+
             const rect = event.target.getBoundingClientRect();
             const relativeX = event.clientX - rect.left;
             const relativeY = event.clientY - rect.top;
@@ -91,6 +97,12 @@
                 mouse_button: buttonMap[event.button] || 0
             };
 
+            // Update mouse button state
+            if (event.button >= 0 && event.button < mouseButtonState.length) {
+                mouseButtonState[event.button] = 1; // Set button state to pressed
+            }
+            // Log the mouse button state
+
             console.log(`Mouse down: ${hidCommand.mouse_button}`);
 
             if (socket && socket.readyState === WebSocket.OPEN) {
@@ -112,12 +124,17 @@
                 1: 3,
                 2: 2
             }; //Map javascript mouse buttons to HID buttons
-
+            
             const hidCommand = {
                 event: 4,
                 mouse_button: buttonMap[event.button] || 0
             };
 
+            // Update mouse button state
+            if (event.button >= 0 && event.button < mouseButtonState.length) {
+                mouseButtonState[event.button] = 0; // Set button state to released
+            }
+
             console.log(`Mouse release: ${hidCommand.mouse_button}`);
 
             if (socket && socket.readyState === WebSocket.OPEN) {