hidconv.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. package remdeshid
  2. /*
  3. hidconv.go
  4. This file contains functions to convert HID commands to bytes
  5. that can be sent over the USBKVM device
  6. */
  7. // Operation Types
  8. const (
  9. // Frontend Opr Types
  10. FRONTEND_OPR_TYPE_KEYBOARD_WRITE = "kw"
  11. FRONTEND_OPR_TYPE_MOUSE_WRITE = "mw"
  12. FRONTEND_OPR_TYPE_MOUSE_MOVE = "mm"
  13. FRONTEND_OPR_TYPE_MOUSE_SCROLL = "ms"
  14. // USBKVM Operation Types
  15. OPR_TYPE_RESERVED = 0x00
  16. OPR_TYPE_KEYBOARD_WRITE = 0x01
  17. OPR_TYPE_MOUSE_WRITE = 0x02
  18. OPR_TYPE_MOUSE_MOVE = 0x03
  19. OPR_TYPE_MOUSE_SCROLL = 0x04
  20. OPR_TYPE_DATA_RESET = 0xFF
  21. )
  22. // Operation Sub-types
  23. const (
  24. SUBTYPE_RESERVED = 0x00
  25. )
  26. // Keyboard Subtypes
  27. const (
  28. // Frontend Keyboard Opr Types
  29. FRONTEND_SUBTYPE_KEYBOARD_KEY_DOWN = "kd"
  30. FRONTEND_SUBTYPE_KEYBOARD_KEY_UP = "ku"
  31. FRONTEND_SUBTYPE_KEYBOARD_KEY_CLICK = "kc"
  32. // USBKVM Keyboard Subtypes
  33. SUBTYPE_KEYBOARD_ASCII_WRITE = 0x01
  34. SUBTYPE_KEYBOARD_ASCII_PRESS = 0x02
  35. SUBTYPE_KEYBOARD_ASCII_RELEASE = 0x03
  36. SUBTYPE_KEYBOARD_MODIFIER_SET = 0x04
  37. SUBTYPE_KEYBOARD_MODIFIER_CLEAR = 0x05
  38. SUBTYPE_KEYBOARD_FUNCTKEY_PRESS = 0x06
  39. SUBTYPE_KEYBOARD_FUNCTKEY_RELEASE = 0x07
  40. SUBTYPE_KEYBOARD_OTHERKEY_PRESS = 0x08
  41. SUBTYPE_KEYBOARD_OTHERKEY_RELEASE = 0x09
  42. SUBTYPE_KEYBOARD_NUMPAD_PRESS = 0x0A
  43. SUBTYPE_KEYBOARD_NUMPAD_RELEASE = 0x0B
  44. SUBTYPE_KEYBOARD_SPECIAL_CTRLALTDEL = 0xFD
  45. SUBTYPE_KEYBOARD_SPECIAL_RESET = 0xFE
  46. SUBTYPE_KEYBOARD_SPECIAL_RESERVED = 0xFF
  47. // Numpad Buttons IDs
  48. PAYLOAD_KEYBOARD_NUMPAD_0 = 0x00
  49. PAYLOAD_KEYBOARD_NUMPAD_1 = 0x01
  50. PAYLOAD_KEYBOARD_NUMPAD_2 = 0x02
  51. PAYLOAD_KEYBOARD_NUMPAD_3 = 0x03
  52. PAYLOAD_KEYBOARD_NUMPAD_4 = 0x04
  53. PAYLOAD_KEYBOARD_NUMPAD_5 = 0x05
  54. PAYLOAD_KEYBOARD_NUMPAD_6 = 0x06
  55. PAYLOAD_KEYBOARD_NUMPAD_7 = 0x07
  56. PAYLOAD_KEYBOARD_NUMPAD_8 = 0x08
  57. PAYLOAD_KEYBOARD_NUMPAD_9 = 0x09
  58. PAYLOAD_KEYBOARD_NUMPAD_DOT = 0x0A
  59. PAYLOAD_KEYBOARD_NUMPAD_TIMES = 0x0B
  60. PAYLOAD_KEYBOARD_NUMPAD_DIV = 0x0C
  61. PAYLOAD_KEYBOARD_NUMPAD_PLUS = 0x0D
  62. PAYLOAD_KEYBOARD_NUMPAD_MINUS = 0x0E
  63. PAYLOAD_KEYBOARD_NUMPAD_ENTER = 0x0F
  64. PAYLOAD_KEYBOARD_NUMPAD_NUMLOCK = 0x10
  65. )
  66. // Response Codes
  67. const (
  68. RESP_OK = 0x00
  69. RESP_UNKNOWN_OPR = 0x01
  70. RESP_INVALID_OPR_TYPE = 0x02
  71. RESP_INVALID_KEY_VALUE = 0x03
  72. RESP_NOT_IMPLEMENTED = 0x04
  73. )
  74. //Is a key a function key
  75. func isFuncKey(key string) bool {
  76. switch key {
  77. case "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12",
  78. "F13", "F14", "F15", "F16", "F17", "F18", "F19", "F20", "F21", "F22", "F23", "F24":
  79. return true
  80. default:
  81. return false
  82. }
  83. }
  84. //Convert function key string to byte
  85. func funcKeyToByte(key string) byte {
  86. switch key {
  87. case "F1":
  88. return 0xC2
  89. case "F2":
  90. return 0xC3
  91. case "F3":
  92. return 0xC4
  93. case "F4":
  94. return 0xC5
  95. case "F5":
  96. return 0xC6
  97. case "F6":
  98. return 0xC7
  99. case "F7":
  100. return 0xC8
  101. case "F8":
  102. return 0xC9
  103. case "F9":
  104. return 0xCA
  105. case "F10":
  106. return 0xCB
  107. case "F11":
  108. return 0xCC
  109. case "F12":
  110. return 0xCD
  111. case "F13":
  112. return 0xF0
  113. case "F14":
  114. return 0xF1
  115. case "F15":
  116. return 0xF2
  117. case "F16":
  118. return 0xF3
  119. case "F17":
  120. return 0xF4
  121. case "F18":
  122. return 0xF5
  123. case "F19":
  124. return 0xF6
  125. case "F20":
  126. return 0xF7
  127. case "F21":
  128. return 0xF8
  129. case "F22":
  130. return 0xF9
  131. case "F23":
  132. return 0xFA
  133. case "F24":
  134. return 0xFB
  135. default:
  136. return 0xFF
  137. }
  138. }
  139. /* Check for other keys */
  140. func isOtherKeys(key string) bool {
  141. return nonAsciiKeysToBytes(key)[0] != 0xFF
  142. }
  143. func nonAsciiKeysToBytes(key string) []byte {
  144. switch key {
  145. case "ArrowUp":
  146. return []byte{0xDA}
  147. case "ArrowDown":
  148. return []byte{0xD9}
  149. case "ArrowLeft":
  150. return []byte{0xD8}
  151. case "ArrowRight":
  152. return []byte{0xD7}
  153. case "Backspace":
  154. return []byte{0xB2}
  155. case "Tab":
  156. return []byte{0xB3}
  157. case "Enter":
  158. return []byte{0xB0}
  159. case "Escape":
  160. return []byte{0xB1}
  161. case "Insert":
  162. return []byte{0xD1}
  163. case "Delete":
  164. return []byte{0xD4}
  165. case "PageUp":
  166. return []byte{0xD3}
  167. case "PageDown":
  168. return []byte{0xD6}
  169. case "Home":
  170. return []byte{0xD2}
  171. case "End":
  172. return []byte{0xD5}
  173. case "CapsLock":
  174. return []byte{0xC1}
  175. default:
  176. return []byte{0xFF}
  177. }
  178. }
  179. /* Numpad keys */
  180. func isNumpadKey(key string) bool {
  181. return len(key) > 7 && key[:7] == "NUMPAD_"
  182. }
  183. func numpadKeyToByte(key string) byte {
  184. switch key {
  185. case "NUMPAD_0":
  186. return PAYLOAD_KEYBOARD_NUMPAD_0
  187. case "NUMPAD_1":
  188. return PAYLOAD_KEYBOARD_NUMPAD_1
  189. case "NUMPAD_2":
  190. return PAYLOAD_KEYBOARD_NUMPAD_2
  191. case "NUMPAD_3":
  192. return PAYLOAD_KEYBOARD_NUMPAD_3
  193. case "NUMPAD_4":
  194. return PAYLOAD_KEYBOARD_NUMPAD_4
  195. case "NUMPAD_5":
  196. return PAYLOAD_KEYBOARD_NUMPAD_5
  197. case "NUMPAD_6":
  198. return PAYLOAD_KEYBOARD_NUMPAD_6
  199. case "NUMPAD_7":
  200. return PAYLOAD_KEYBOARD_NUMPAD_7
  201. case "NUMPAD_8":
  202. return PAYLOAD_KEYBOARD_NUMPAD_8
  203. case "NUMPAD_9":
  204. return PAYLOAD_KEYBOARD_NUMPAD_9
  205. case "NUMPAD_.":
  206. return PAYLOAD_KEYBOARD_NUMPAD_DOT
  207. case "NUMPAD_*":
  208. return PAYLOAD_KEYBOARD_NUMPAD_TIMES
  209. case "NUMPAD_/":
  210. return PAYLOAD_KEYBOARD_NUMPAD_DIV
  211. case "NUMPAD_+":
  212. return PAYLOAD_KEYBOARD_NUMPAD_PLUS
  213. case "NUMPAD_-":
  214. return PAYLOAD_KEYBOARD_NUMPAD_MINUS
  215. case "NUMPAD_Enter":
  216. return PAYLOAD_KEYBOARD_NUMPAD_ENTER
  217. default:
  218. return 0xFF
  219. }
  220. }