mouse.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package remdeshid
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "errors"
  6. )
  7. // calcChecksum calculates the checksum for a given data slice.
  8. func calcChecksum(data []uint8) uint8 {
  9. var sum uint8 = 0
  10. for _, value := range data {
  11. sum += value
  12. }
  13. return sum
  14. }
  15. func (c *Controller) MouseMoveAbsolute(xLSB, xMSB, yLSB, yMSB uint8) ([]byte, error) {
  16. packet := []uint8{
  17. 0x57, 0xAB, 0x00, 0x04, 0x07, 0x02,
  18. c.hidState.MouseButtons,
  19. xLSB, // X LSB
  20. xMSB, // X MSB
  21. yLSB, // Y LSB
  22. yMSB, // Y MSB
  23. 0x00, // Scroll
  24. 0x00, // Checksum placeholder
  25. }
  26. packet[12] = calcChecksum(packet[:12])
  27. buf := new(bytes.Buffer)
  28. if err := binary.Write(buf, binary.LittleEndian, packet); err != nil {
  29. return nil, errors.New("failed to write packet to buffer")
  30. }
  31. err := c.Send(buf.Bytes())
  32. if err != nil {
  33. return nil, errors.New("failed to send mouse move command: " + err.Error())
  34. }
  35. // Wait for a reply from the device
  36. return c.WaitForReply(0x04)
  37. }
  38. func (c *Controller) MouseMoveRelative(dx, dy, wheel uint8) ([]byte, error) {
  39. // Ensure 0x80 is not used
  40. if dx == 0x80 {
  41. dx = 0x81
  42. }
  43. if dy == 0x80 {
  44. dy = 0x81
  45. }
  46. packet := []uint8{
  47. 0x57, 0xAB, 0x00, 0x05, 0x05, 0x01,
  48. c.hidState.MouseButtons,
  49. dx, // Delta X
  50. dy, // Delta Y
  51. wheel, // Scroll wheel
  52. 0x00, // Checksum placeholder
  53. }
  54. packet[10] = calcChecksum(packet[:10])
  55. buf := new(bytes.Buffer)
  56. if err := binary.Write(buf, binary.LittleEndian, packet); err != nil {
  57. return nil, errors.New("failed to write packet to buffer")
  58. }
  59. err := c.Send(buf.Bytes())
  60. if err != nil {
  61. return nil, errors.New("failed to send mouse move relative command: " + err.Error())
  62. }
  63. return c.WaitForReply(0x05)
  64. }
  65. // Handle mouse button press events
  66. func (c *Controller) MouseButtonPress(button uint8) ([]byte, error) {
  67. switch button {
  68. case 0x01: // Left
  69. c.hidState.MouseButtons |= 0x01
  70. case 0x02: // Right
  71. c.hidState.MouseButtons |= 0x02
  72. case 0x03: // Middle
  73. c.hidState.MouseButtons |= 0x04
  74. default:
  75. return nil, errors.New("invalid opcode for mouse button press")
  76. }
  77. // Send updated button state with no movement
  78. return c.MouseMoveRelative(0, 0, 0)
  79. }
  80. // Handle mouse button release events
  81. func (c *Controller) MouseButtonRelease(button uint8) ([]byte, error) {
  82. switch button {
  83. case 0x00: // Release all
  84. c.hidState.MouseButtons = 0x00
  85. case 0x01: // Left
  86. c.hidState.MouseButtons &^= 0x01
  87. case 0x02: // Right
  88. c.hidState.MouseButtons &^= 0x02
  89. case 0x03: // Middle
  90. c.hidState.MouseButtons &^= 0x04
  91. default:
  92. return nil, errors.New("invalid opcode for mouse button release")
  93. }
  94. // Send updated button state with no movement
  95. return c.MouseMoveRelative(0, 0, 0)
  96. }
  97. func (c *Controller) MouseScroll(tilt int) ([]byte, error) {
  98. if tilt == 0 {
  99. // No need to scroll
  100. return nil, nil
  101. }
  102. var wheel uint8
  103. if tilt < 0 {
  104. wheel = uint8(c.Config.ScrollSensitivity)
  105. } else {
  106. wheel = uint8(0xFF - c.Config.ScrollSensitivity)
  107. }
  108. //fmt.Println(tilt, "-->", wheel)
  109. return c.MouseMoveRelative(0, 0, wheel)
  110. }