hidconv.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. const (
  80. //Frontend Mouse Opr Types
  81. FRONTEND_MOUSE_CLICK = "mc"
  82. FRONTEND_MOUSE_PRESS = "md"
  83. FRONTEND_MOUSE_RELEASE = "mu"
  84. FRONTEND_MOUSE_BTN_LEFT = "0"
  85. FRONTEND_MOUSE_BTN_MIDDLE = "1"
  86. FRONTEND_MOUSE_BTN_RIGHT = "2"
  87. // Mouse Subtypes
  88. SUBTYPE_MOUSE_CLICK = 0x01 // Mouse button click
  89. SUBTYPE_MOUSE_PRESS = 0x02 // Mouse button press
  90. SUBTYPE_MOUSE_RELEASE = 0x03 // Mouse button release
  91. SUBTYPE_MOUSE_SETPOS = 0x04 // Mouse presets position
  92. SUBTYPE_MOUSE_RESET = 0x05 // Reset all mouse button states
  93. // Mouse Buttons IDs
  94. PAYLOAD_MOUSE_BTN_LEFT = 0x01
  95. PAYLOAD_MOUSE_BTN_RIGHT = 0x02
  96. PAYLOAD_MOUSE_BTN_MID = 0x03
  97. )
  98. // Control Code
  99. const (
  100. FRONT_END_OPR_RESET = "reset" // Reset all the mouse and keyboard states
  101. )
  102. // Response Codes
  103. const (
  104. RESP_OK = 0x00
  105. RESP_UNKNOWN_OPR = 0x01
  106. RESP_INVALID_OPR_TYPE = 0x02
  107. RESP_INVALID_KEY_VALUE = 0x03
  108. RESP_NOT_IMPLEMENTED = 0x04
  109. )
  110. //isModifierKey checks if a key is a modifier key
  111. func isModifierKey(key string) bool {
  112. switch key {
  113. case "LEFT_Shift", "RIGHT_Shift", "LEFT_Control", "RIGHT_Control", "LEFT_Alt", "RIGHT_Alt", "Meta", "ContextMenu":
  114. return true
  115. default:
  116. return false
  117. }
  118. }
  119. //Convert modifier key string to byte
  120. func modifierKeyToByte(key string) byte {
  121. switch key {
  122. case "LEFT_Shift":
  123. return PAYLOAD_KEY_LEFT_SHIFT
  124. case "RIGHT_Shift":
  125. return PAYLOAD_KEY_RIGHT_SHIFT
  126. case "LEFT_Control":
  127. return PAYLOAD_KEY_LEFT_CTRL
  128. case "RIGHT_Control":
  129. return PAYLOAD_KEY_RIGHT_CTRL
  130. case "LEFT_Alt":
  131. return PAYLOAD_KEY_LEFT_ALT
  132. case "RIGHT_Alt":
  133. return PAYLOAD_KEY_RIGHT_ALT
  134. case "Meta":
  135. return PAYLOAD_KEY_LEFT_GUI
  136. case "ContextMenu":
  137. return PAYLOAD_KEY_RIGHT_GUI
  138. default:
  139. return 0xFF
  140. }
  141. }
  142. //Is a key a function key
  143. func isFuncKey(key string) bool {
  144. switch key {
  145. case "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12",
  146. "F13", "F14", "F15", "F16", "F17", "F18", "F19", "F20", "F21", "F22", "F23", "F24":
  147. return true
  148. default:
  149. return false
  150. }
  151. }
  152. //Convert function key string to byte
  153. func funcKeyToByte(key string) byte {
  154. switch key {
  155. case "F1":
  156. return 0xC2
  157. case "F2":
  158. return 0xC3
  159. case "F3":
  160. return 0xC4
  161. case "F4":
  162. return 0xC5
  163. case "F5":
  164. return 0xC6
  165. case "F6":
  166. return 0xC7
  167. case "F7":
  168. return 0xC8
  169. case "F8":
  170. return 0xC9
  171. case "F9":
  172. return 0xCA
  173. case "F10":
  174. return 0xCB
  175. case "F11":
  176. return 0xCC
  177. case "F12":
  178. return 0xCD
  179. case "F13":
  180. return 0xF0
  181. case "F14":
  182. return 0xF1
  183. case "F15":
  184. return 0xF2
  185. case "F16":
  186. return 0xF3
  187. case "F17":
  188. return 0xF4
  189. case "F18":
  190. return 0xF5
  191. case "F19":
  192. return 0xF6
  193. case "F20":
  194. return 0xF7
  195. case "F21":
  196. return 0xF8
  197. case "F22":
  198. return 0xF9
  199. case "F23":
  200. return 0xFA
  201. case "F24":
  202. return 0xFB
  203. default:
  204. return 0xFF
  205. }
  206. }
  207. /* Check for other keys */
  208. func isOtherKeys(key string) bool {
  209. return nonAsciiKeysToBytes(key)[0] != 0xFF
  210. }
  211. func nonAsciiKeysToBytes(key string) []byte {
  212. switch key {
  213. case "ArrowUp":
  214. return []byte{0xDA}
  215. case "ArrowDown":
  216. return []byte{0xD9}
  217. case "ArrowLeft":
  218. return []byte{0xD8}
  219. case "ArrowRight":
  220. return []byte{0xD7}
  221. case "Backspace":
  222. return []byte{0xB2}
  223. case "Tab":
  224. return []byte{0xB3}
  225. case "Enter":
  226. return []byte{0xB0}
  227. case "Escape":
  228. return []byte{0xB1}
  229. case "Insert":
  230. return []byte{0xD1}
  231. case "Delete":
  232. return []byte{0xD4}
  233. case "PageUp":
  234. return []byte{0xD3}
  235. case "PageDown":
  236. return []byte{0xD6}
  237. case "Home":
  238. return []byte{0xD2}
  239. case "End":
  240. return []byte{0xD5}
  241. case "CapsLock":
  242. return []byte{0xC1}
  243. default:
  244. return []byte{0xFF}
  245. }
  246. }
  247. /* Numpad keys */
  248. func isNumpadKey(key string) bool {
  249. return len(key) > 7 && key[:7] == "NUMPAD_"
  250. }
  251. func numpadKeyToByte(key string) byte {
  252. switch key {
  253. case "NUMPAD_0":
  254. return PAYLOAD_KEYBOARD_NUMPAD_0
  255. case "NUMPAD_1":
  256. return PAYLOAD_KEYBOARD_NUMPAD_1
  257. case "NUMPAD_2":
  258. return PAYLOAD_KEYBOARD_NUMPAD_2
  259. case "NUMPAD_3":
  260. return PAYLOAD_KEYBOARD_NUMPAD_3
  261. case "NUMPAD_4":
  262. return PAYLOAD_KEYBOARD_NUMPAD_4
  263. case "NUMPAD_5":
  264. return PAYLOAD_KEYBOARD_NUMPAD_5
  265. case "NUMPAD_6":
  266. return PAYLOAD_KEYBOARD_NUMPAD_6
  267. case "NUMPAD_7":
  268. return PAYLOAD_KEYBOARD_NUMPAD_7
  269. case "NUMPAD_8":
  270. return PAYLOAD_KEYBOARD_NUMPAD_8
  271. case "NUMPAD_9":
  272. return PAYLOAD_KEYBOARD_NUMPAD_9
  273. case "NUMPAD_.":
  274. return PAYLOAD_KEYBOARD_NUMPAD_DOT
  275. case "NUMPAD_*":
  276. return PAYLOAD_KEYBOARD_NUMPAD_TIMES
  277. case "NUMPAD_/":
  278. return PAYLOAD_KEYBOARD_NUMPAD_DIV
  279. case "NUMPAD_+":
  280. return PAYLOAD_KEYBOARD_NUMPAD_PLUS
  281. case "NUMPAD_-":
  282. return PAYLOAD_KEYBOARD_NUMPAD_MINUS
  283. case "NUMPAD_Enter":
  284. return PAYLOAD_KEYBOARD_NUMPAD_ENTER
  285. default:
  286. return 0xFF
  287. }
  288. }