hidcomm.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 isOtherKeys(cmd.Data) {
  35. //Other Keys
  36. if cmd.EventSubType == FRONTEND_SUBTYPE_KEYBOARD_KEY_DOWN {
  37. data = append(data, SUBTYPE_KEYBOARD_OTHERKEY_PRESS)
  38. } else if cmd.EventSubType == FRONTEND_SUBTYPE_KEYBOARD_KEY_UP {
  39. data = append(data, SUBTYPE_KEYBOARD_OTHERKEY_RELEASE)
  40. } else {
  41. return nil, fmt.Errorf("invalid HID command subtype: %v", cmd.Data)
  42. }
  43. data = append(data, nonAsciiKeysToBytes(cmd.Data)...)
  44. return data, nil
  45. } else if isNumpadKey(cmd.Data) {
  46. //Numpad Keys
  47. if cmd.EventSubType == FRONTEND_SUBTYPE_KEYBOARD_KEY_DOWN {
  48. data = append(data, SUBTYPE_KEYBOARD_NUMPAD_PRESS)
  49. } else if cmd.EventSubType == FRONTEND_SUBTYPE_KEYBOARD_KEY_UP {
  50. data = append(data, SUBTYPE_KEYBOARD_NUMPAD_RELEASE)
  51. } else {
  52. return nil, fmt.Errorf("invalid HID command subtype: %v", cmd.Data)
  53. }
  54. data = append(data, numpadKeyToByte(string(cmd.Data)))
  55. return data, nil
  56. } else if cmd.Data == "NumLock" {
  57. //Special Key: NumLock
  58. //TODO: Implement NumLock
  59. return nil, fmt.Errorf("numLock not implemented")
  60. } else if cmd.Data == "Ctrl+Alt+Del" {
  61. //Special Key: Ctrl+Alt+Del
  62. data = append(data, SUBTYPE_KEYBOARD_SPECIAL_CTRLALTDEL)
  63. data = append(data, 0x00)
  64. return data, nil
  65. } else {
  66. return nil, fmt.Errorf("invalid HID command subtype: %v", cmd.Data)
  67. }
  68. } else if cmd.EventType == FRONTEND_OPR_TYPE_MOUSE_WRITE {
  69. data = []byte{OPR_TYPE_MOUSE_WRITE}
  70. } else {
  71. return nil, fmt.Errorf("invalid HID command type: %s", cmd.EventType)
  72. }
  73. return data, nil
  74. }