hidconv.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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_PRESS = 0x04
  37. SUBTYPE_KEYBOARD_MODIFIER_RELEASE = 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_PAUSE = 0xF9
  45. SUBTYPE_KEYBOARD_SPECIAL_PRINT_SCREEN = 0xFA
  46. SUBTYPE_KEYBOARD_SPECIAL_SCROLL_LOCK = 0xFB
  47. SUBTYPE_KEYBOARD_SPECIAL_NUMLOCK = 0xFC
  48. SUBTYPE_KEYBOARD_SPECIAL_CTRLALTDEL = 0xFD
  49. SUBTYPE_KEYBOARD_SPECIAL_RESET = 0xFE
  50. SUBTYPE_KEYBOARD_SPECIAL_RESERVED = 0xFF
  51. // Numpad Buttons IDs
  52. PAYLOAD_KEYBOARD_NUMPAD_0 = 0x00
  53. PAYLOAD_KEYBOARD_NUMPAD_1 = 0x01
  54. PAYLOAD_KEYBOARD_NUMPAD_2 = 0x02
  55. PAYLOAD_KEYBOARD_NUMPAD_3 = 0x03
  56. PAYLOAD_KEYBOARD_NUMPAD_4 = 0x04
  57. PAYLOAD_KEYBOARD_NUMPAD_5 = 0x05
  58. PAYLOAD_KEYBOARD_NUMPAD_6 = 0x06
  59. PAYLOAD_KEYBOARD_NUMPAD_7 = 0x07
  60. PAYLOAD_KEYBOARD_NUMPAD_8 = 0x08
  61. PAYLOAD_KEYBOARD_NUMPAD_9 = 0x09
  62. PAYLOAD_KEYBOARD_NUMPAD_DOT = 0x0A
  63. PAYLOAD_KEYBOARD_NUMPAD_TIMES = 0x0B
  64. PAYLOAD_KEYBOARD_NUMPAD_DIV = 0x0C
  65. PAYLOAD_KEYBOARD_NUMPAD_PLUS = 0x0D
  66. PAYLOAD_KEYBOARD_NUMPAD_MINUS = 0x0E
  67. PAYLOAD_KEYBOARD_NUMPAD_ENTER = 0x0F
  68. PAYLOAD_KEYBOARD_NUMPAD_NUMLOCK = 0x10
  69. // Modifier Keys IDs
  70. PAYLOAD_KEY_LEFT_CTRL = 0x00
  71. PAYLOAD_KEY_LEFT_SHIFT = 0x01
  72. PAYLOAD_KEY_LEFT_ALT = 0x02
  73. PAYLOAD_KEY_LEFT_GUI = 0x03
  74. PAYLOAD_KEY_RIGHT_CTRL = 0x04
  75. PAYLOAD_KEY_RIGHT_SHIFT = 0x05
  76. PAYLOAD_KEY_RIGHT_ALT = 0x06
  77. PAYLOAD_KEY_RIGHT_GUI = 0x07
  78. )
  79. // Response Codes
  80. const (
  81. RESP_OK = 0x00
  82. RESP_UNKNOWN_OPR = 0x01
  83. RESP_INVALID_OPR_TYPE = 0x02
  84. RESP_INVALID_KEY_VALUE = 0x03
  85. RESP_NOT_IMPLEMENTED = 0x04
  86. )
  87. //isModifierKey checks if a key is a modifier key
  88. func isModifierKey(key string) bool {
  89. switch key {
  90. case "LEFT_Shift", "RIGHT_Shift", "LEFT_Control", "RIGHT_Control", "LEFT_Alt", "RIGHT_Alt", "Meta", "ContextMenu":
  91. return true
  92. default:
  93. return false
  94. }
  95. }
  96. //Convert modifier key string to byte
  97. func modifierKeyToByte(key string) byte {
  98. switch key {
  99. case "LEFT_Shift":
  100. return PAYLOAD_KEY_LEFT_SHIFT
  101. case "RIGHT_Shift":
  102. return PAYLOAD_KEY_RIGHT_SHIFT
  103. case "LEFT_Control":
  104. return PAYLOAD_KEY_LEFT_CTRL
  105. case "RIGHT_Control":
  106. return PAYLOAD_KEY_RIGHT_CTRL
  107. case "LEFT_Alt":
  108. return PAYLOAD_KEY_LEFT_ALT
  109. case "RIGHT_Alt":
  110. return PAYLOAD_KEY_RIGHT_ALT
  111. case "Meta":
  112. return PAYLOAD_KEY_LEFT_GUI
  113. case "ContextMenu":
  114. return PAYLOAD_KEY_RIGHT_GUI
  115. default:
  116. return 0xFF
  117. }
  118. }
  119. //Is a key a function key
  120. func isFuncKey(key string) bool {
  121. switch key {
  122. case "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12",
  123. "F13", "F14", "F15", "F16", "F17", "F18", "F19", "F20", "F21", "F22", "F23", "F24":
  124. return true
  125. default:
  126. return false
  127. }
  128. }
  129. //Convert function key string to byte
  130. func funcKeyToByte(key string) byte {
  131. switch key {
  132. case "F1":
  133. return 0xC2
  134. case "F2":
  135. return 0xC3
  136. case "F3":
  137. return 0xC4
  138. case "F4":
  139. return 0xC5
  140. case "F5":
  141. return 0xC6
  142. case "F6":
  143. return 0xC7
  144. case "F7":
  145. return 0xC8
  146. case "F8":
  147. return 0xC9
  148. case "F9":
  149. return 0xCA
  150. case "F10":
  151. return 0xCB
  152. case "F11":
  153. return 0xCC
  154. case "F12":
  155. return 0xCD
  156. case "F13":
  157. return 0xF0
  158. case "F14":
  159. return 0xF1
  160. case "F15":
  161. return 0xF2
  162. case "F16":
  163. return 0xF3
  164. case "F17":
  165. return 0xF4
  166. case "F18":
  167. return 0xF5
  168. case "F19":
  169. return 0xF6
  170. case "F20":
  171. return 0xF7
  172. case "F21":
  173. return 0xF8
  174. case "F22":
  175. return 0xF9
  176. case "F23":
  177. return 0xFA
  178. case "F24":
  179. return 0xFB
  180. default:
  181. return 0xFF
  182. }
  183. }
  184. /* Check for other keys */
  185. func isOtherKeys(key string) bool {
  186. return nonAsciiKeysToBytes(key)[0] != 0xFF
  187. }
  188. func nonAsciiKeysToBytes(key string) []byte {
  189. switch key {
  190. case "ArrowUp":
  191. return []byte{0xDA}
  192. case "ArrowDown":
  193. return []byte{0xD9}
  194. case "ArrowLeft":
  195. return []byte{0xD8}
  196. case "ArrowRight":
  197. return []byte{0xD7}
  198. case "Backspace":
  199. return []byte{0xB2}
  200. case "Tab":
  201. return []byte{0xB3}
  202. case "Enter":
  203. return []byte{0xB0}
  204. case "Escape":
  205. return []byte{0xB1}
  206. case "Insert":
  207. return []byte{0xD1}
  208. case "Delete":
  209. return []byte{0xD4}
  210. case "PageUp":
  211. return []byte{0xD3}
  212. case "PageDown":
  213. return []byte{0xD6}
  214. case "Home":
  215. return []byte{0xD2}
  216. case "End":
  217. return []byte{0xD5}
  218. case "CapsLock":
  219. return []byte{0xC1}
  220. default:
  221. return []byte{0xFF}
  222. }
  223. }
  224. /* Numpad keys */
  225. func isNumpadKey(key string) bool {
  226. return len(key) > 7 && key[:7] == "NUMPAD_"
  227. }
  228. func numpadKeyToByte(key string) byte {
  229. switch key {
  230. case "NUMPAD_0":
  231. return PAYLOAD_KEYBOARD_NUMPAD_0
  232. case "NUMPAD_1":
  233. return PAYLOAD_KEYBOARD_NUMPAD_1
  234. case "NUMPAD_2":
  235. return PAYLOAD_KEYBOARD_NUMPAD_2
  236. case "NUMPAD_3":
  237. return PAYLOAD_KEYBOARD_NUMPAD_3
  238. case "NUMPAD_4":
  239. return PAYLOAD_KEYBOARD_NUMPAD_4
  240. case "NUMPAD_5":
  241. return PAYLOAD_KEYBOARD_NUMPAD_5
  242. case "NUMPAD_6":
  243. return PAYLOAD_KEYBOARD_NUMPAD_6
  244. case "NUMPAD_7":
  245. return PAYLOAD_KEYBOARD_NUMPAD_7
  246. case "NUMPAD_8":
  247. return PAYLOAD_KEYBOARD_NUMPAD_8
  248. case "NUMPAD_9":
  249. return PAYLOAD_KEYBOARD_NUMPAD_9
  250. case "NUMPAD_.":
  251. return PAYLOAD_KEYBOARD_NUMPAD_DOT
  252. case "NUMPAD_*":
  253. return PAYLOAD_KEYBOARD_NUMPAD_TIMES
  254. case "NUMPAD_/":
  255. return PAYLOAD_KEYBOARD_NUMPAD_DIV
  256. case "NUMPAD_+":
  257. return PAYLOAD_KEYBOARD_NUMPAD_PLUS
  258. case "NUMPAD_-":
  259. return PAYLOAD_KEYBOARD_NUMPAD_MINUS
  260. case "NUMPAD_Enter":
  261. return PAYLOAD_KEYBOARD_NUMPAD_ENTER
  262. default:
  263. return 0xFF
  264. }
  265. }