hidcomm.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package remdeshid
  2. import "fmt"
  3. //Convert a HIDCommand to bytes that can be sent over the USBKVM device
  4. func ConvHIDCommandToBytes(cmd HIDCommand) ([]byte, error) {
  5. // Convert the HID command to bytes
  6. var data []byte
  7. if cmd.EventType == FRONTEND_OPR_TYPE_KEYBOARD_WRITE {
  8. data = []byte{OPR_TYPE_KEYBOARD_WRITE}
  9. /* Keyboard Subtypes */
  10. if len(cmd.Data) == 1 && cmd.Data[0] >= 32 && cmd.Data[0] <= 127 {
  11. //Valid ASCII character
  12. if cmd.EventSubType == FRONTEND_SUBTYPE_KEYBOARD_KEY_DOWN {
  13. data = append(data, SUBTYPE_KEYBOARD_ASCII_PRESS)
  14. } else if cmd.EventSubType == FRONTEND_SUBTYPE_KEYBOARD_KEY_UP {
  15. data = append(data, SUBTYPE_KEYBOARD_ASCII_RELEASE)
  16. } else {
  17. //Key Click
  18. data = append(data, SUBTYPE_KEYBOARD_ASCII_WRITE)
  19. }
  20. data = append(data, cmd.Data[0])
  21. return data, nil
  22. } else if isFuncKey(cmd.Data) {
  23. //Function Key
  24. if cmd.EventSubType == FRONTEND_SUBTYPE_KEYBOARD_KEY_DOWN {
  25. data = append(data, SUBTYPE_KEYBOARD_FUNCTKEY_PRESS)
  26. } else {
  27. data = append(data, SUBTYPE_KEYBOARD_FUNCTKEY_RELEASE)
  28. }
  29. data = append(data, funcKeyToByte(cmd.Data))
  30. if data[0] == 0xFF {
  31. return nil, fmt.Errorf("invalid function key: %v", cmd.Data)
  32. }
  33. return data, nil
  34. } else if isModifierKey(cmd.Data) {
  35. //Modifier Key
  36. if cmd.EventSubType == FRONTEND_SUBTYPE_KEYBOARD_KEY_DOWN {
  37. data = append(data, SUBTYPE_KEYBOARD_MODIFIER_PRESS)
  38. } else {
  39. data = append(data, SUBTYPE_KEYBOARD_MODIFIER_RELEASE)
  40. }
  41. data = append(data, modifierKeyToByte(cmd.Data))
  42. if data[0] == 0xFF {
  43. return nil, fmt.Errorf("invalid modifier key: %v", cmd.Data)
  44. }
  45. return data, nil
  46. } else if isOtherKeys(cmd.Data) {
  47. //Other Keys
  48. if cmd.EventSubType == FRONTEND_SUBTYPE_KEYBOARD_KEY_DOWN {
  49. data = append(data, SUBTYPE_KEYBOARD_OTHERKEY_PRESS)
  50. } else if cmd.EventSubType == FRONTEND_SUBTYPE_KEYBOARD_KEY_UP {
  51. data = append(data, SUBTYPE_KEYBOARD_OTHERKEY_RELEASE)
  52. } else {
  53. return nil, fmt.Errorf("invalid HID command subtype: %v", cmd.Data)
  54. }
  55. data = append(data, nonAsciiKeysToBytes(cmd.Data)...)
  56. return data, nil
  57. } else if isNumpadKey(cmd.Data) {
  58. //Numpad Keys
  59. if cmd.EventSubType == FRONTEND_SUBTYPE_KEYBOARD_KEY_DOWN {
  60. data = append(data, SUBTYPE_KEYBOARD_NUMPAD_PRESS)
  61. } else if cmd.EventSubType == FRONTEND_SUBTYPE_KEYBOARD_KEY_UP {
  62. data = append(data, SUBTYPE_KEYBOARD_NUMPAD_RELEASE)
  63. } else {
  64. return nil, fmt.Errorf("invalid HID command subtype: %v", cmd.Data)
  65. }
  66. data = append(data, numpadKeyToByte(string(cmd.Data)))
  67. return data, nil
  68. } else if cmd.Data == "NumLock" {
  69. if cmd.EventSubType == FRONTEND_SUBTYPE_KEYBOARD_KEY_DOWN {
  70. data = append(data, SUBTYPE_KEYBOARD_SPECIAL_NUMLOCK)
  71. data = append(data, 0x00)
  72. return data, nil
  73. }
  74. return nil, fmt.Errorf("numLock do not support key up")
  75. } else if cmd.Data == "Pause" {
  76. if cmd.EventSubType == FRONTEND_SUBTYPE_KEYBOARD_KEY_DOWN {
  77. data = append(data, SUBTYPE_KEYBOARD_SPECIAL_PAUSE)
  78. data = append(data, 0x00)
  79. return data, nil
  80. }
  81. return nil, fmt.Errorf("pause do not support key up")
  82. } else if cmd.Data == "PrintScreen" {
  83. if cmd.EventSubType == FRONTEND_SUBTYPE_KEYBOARD_KEY_UP {
  84. data = append(data, SUBTYPE_KEYBOARD_SPECIAL_PRINT_SCREEN)
  85. data = append(data, 0x00)
  86. return data, nil
  87. }
  88. return nil, fmt.Errorf("printScreen do not support key down")
  89. } else if cmd.Data == "ScrollLock" {
  90. if cmd.EventSubType == FRONTEND_SUBTYPE_KEYBOARD_KEY_DOWN {
  91. data = append(data, SUBTYPE_KEYBOARD_SPECIAL_SCROLL_LOCK)
  92. data = append(data, 0x00)
  93. return data, nil
  94. }
  95. return nil, fmt.Errorf("scrollLock do not support key up")
  96. } else if cmd.Data == "ContextMenu" {
  97. //Special Key: ContextMenu
  98. //TODO: Implement ContextMenu
  99. return nil, fmt.Errorf("ContextMenu not implemented")
  100. } else if cmd.Data == "Ctrl+Alt+Del" {
  101. //Special Key: Ctrl+Alt+Del
  102. data = append(data, SUBTYPE_KEYBOARD_SPECIAL_CTRLALTDEL)
  103. data = append(data, 0x00)
  104. return data, nil
  105. } else {
  106. return nil, fmt.Errorf("invalid HID command subtype: %v", cmd.Data)
  107. }
  108. } else if cmd.EventType == FRONTEND_OPR_TYPE_MOUSE_WRITE {
  109. /* Mouse Write Event */
  110. data = []byte{OPR_TYPE_MOUSE_WRITE}
  111. } else {
  112. return nil, fmt.Errorf("invalid HID command type: %s", cmd.EventType)
  113. }
  114. return data, nil
  115. }