hidcomm.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. //Special Key: NumLock
  70. //TODO: Implement NumLock
  71. return nil, fmt.Errorf("numLock not implemented")
  72. } else if cmd.Data == "ContextMenu" {
  73. //Special Key: ContextMenu
  74. //TODO: Implement ContextMenu
  75. return nil, fmt.Errorf("ContextMenu not implemented")
  76. } else if cmd.Data == "Ctrl+Alt+Del" {
  77. //Special Key: Ctrl+Alt+Del
  78. data = append(data, SUBTYPE_KEYBOARD_SPECIAL_CTRLALTDEL)
  79. data = append(data, 0x00)
  80. return data, nil
  81. } else {
  82. return nil, fmt.Errorf("invalid HID command subtype: %v", cmd.Data)
  83. }
  84. } else if cmd.EventType == FRONTEND_OPR_TYPE_MOUSE_WRITE {
  85. /* Mouse Write Event */
  86. data = []byte{OPR_TYPE_MOUSE_WRITE}
  87. } else {
  88. return nil, fmt.Errorf("invalid HID command type: %s", cmd.EventType)
  89. }
  90. return data, nil
  91. }