mouse.go 2.9 KB

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