input.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. package door
  2. import (
  3. "log"
  4. "strconv"
  5. "strings"
  6. "time"
  7. )
  8. // This is the current list of Extended keys we support:
  9. const (
  10. XKEY_UP_ARROW = 0x1001
  11. XKEY_DOWN_ARROW = 0x1002
  12. XKEY_RIGHT_ARROW = 0x1003
  13. XKEY_LEFT_ARROW = 0x1004
  14. XKEY_HOME = 0x1010
  15. XKEY_END = 0x1011
  16. XKEY_PGUP = 0x1012
  17. XKEY_PGDN = 0x1023
  18. XKEY_INSERT = 0x1024
  19. XKEY_DELETE = 0x7f
  20. XKEY_F1 = 0x1021
  21. XKEY_F2 = 0x1022
  22. XKEY_F3 = 0x1023
  23. XKEY_F4 = 0x1024
  24. XKEY_F5 = 0x1025
  25. XKEY_F6 = 0x1026
  26. XKEY_F7 = 0x1027
  27. XKEY_F8 = 0x1028
  28. XKEY_F9 = 0x1029
  29. XKEY_F10 = 0x102a
  30. XKEY_F11 = 0x102b
  31. XKEY_F12 = 0x102c
  32. XKEY_UNKNOWN = 0x1111
  33. )
  34. var readerChannel chan byte
  35. // Low level read key function.
  36. // This gets the raw keys from the client, it doesn't handle extended keys,
  37. // functions, arrows.
  38. // Return key, or -1 (Timeout/No key available), -2 hangup
  39. func (d *Door) getch() int {
  40. select {
  41. case res, ok := <-readerChannel:
  42. if ok {
  43. return int(res)
  44. } else {
  45. d.Disconnected = true
  46. return -2
  47. }
  48. case <-time.After(time.Duration(100) * time.Millisecond):
  49. return -1
  50. }
  51. }
  52. func (d *Door) getkey_or_pushback() int {
  53. if !d.Pushback.Empty() {
  54. return d.Pushback.Pop()
  55. }
  56. if false {
  57. key := d.getch()
  58. log.Printf("%d / %X\n", key, key)
  59. return key
  60. } else {
  61. return d.getch()
  62. }
  63. }
  64. // Return key received, or XKEY_* values.
  65. // -1 timeout/no key
  66. // -2 hangup
  67. // -3 out of time
  68. func (d *Door) GetKey() int {
  69. var c, c2 int
  70. if d.Disconnected {
  71. return -2
  72. }
  73. if d.TimeLeft() < 0 {
  74. return -3
  75. }
  76. c = d.getkey_or_pushback()
  77. if c < 0 {
  78. return c
  79. }
  80. // We get 0x0d 0x00, or 0x0d 0x0a from syncterm.
  81. if c == 0x0d {
  82. c2 = d.getkey_or_pushback()
  83. if c2 > 0 {
  84. // wasn't an error
  85. if c2 != 0x00 && c2 != 0x0a {
  86. // wasn't 0x00 or 0x0a
  87. d.Pushback.Push(c2)
  88. // log.Printf("Push 0x0d trailer %d / %x\n", c2, c2)
  89. }
  90. }
  91. return c
  92. }
  93. if c == 0 {
  94. // possibly doorway mode
  95. tries := 0
  96. c2 = d.getkey_or_pushback()
  97. for c2 < 0 {
  98. if tries > 7 {
  99. return c
  100. }
  101. c2 = d.getkey_or_pushback()
  102. tries++
  103. }
  104. switch c2 {
  105. case 0x50:
  106. return XKEY_DOWN_ARROW
  107. case 0x48:
  108. return XKEY_UP_ARROW
  109. case 0x4b:
  110. return XKEY_LEFT_ARROW
  111. case 0x4d:
  112. return XKEY_RIGHT_ARROW
  113. case 0x47:
  114. return XKEY_HOME
  115. case 0x4f:
  116. return XKEY_END
  117. case 0x49:
  118. return XKEY_PGUP
  119. case 0x51:
  120. return XKEY_PGDN
  121. case 0x3b:
  122. return XKEY_F1
  123. case 0x3c:
  124. return XKEY_F2
  125. case 0x3d:
  126. return XKEY_F3
  127. case 0x3e:
  128. return XKEY_F4
  129. case 0x3f:
  130. return XKEY_F5
  131. case 0x40:
  132. return XKEY_F6
  133. case 0x41:
  134. return XKEY_F7
  135. case 0x42:
  136. return XKEY_F8
  137. case 0x43:
  138. return XKEY_F9
  139. case 0x44:
  140. return XKEY_F10
  141. /*
  142. case 0x45:
  143. return XKEY_F11
  144. case 0x46:
  145. return XKEY_F12
  146. */
  147. case 0x52:
  148. return XKEY_INSERT
  149. case 0x53:
  150. return XKEY_DELETE
  151. default:
  152. log.Printf("ERROR Doorway mode: 0x00 %x\n", c2)
  153. return XKEY_UNKNOWN
  154. }
  155. }
  156. if c == 0x1b {
  157. // Escape key?
  158. c2 = d.getkey_or_pushback()
  159. if c2 < 0 {
  160. // Just escape key
  161. return c
  162. }
  163. var extended string = string(byte(c2))
  164. c2 = d.getkey_or_pushback()
  165. for c2 > 0 {
  166. if c2 == 0x1b {
  167. d.Pushback.Push(c2)
  168. break
  169. }
  170. extended += string(byte(c2))
  171. c2 = d.getkey_or_pushback()
  172. }
  173. switch extended {
  174. case "[A":
  175. return XKEY_UP_ARROW
  176. case "[B":
  177. return XKEY_DOWN_ARROW
  178. case "[C":
  179. return XKEY_RIGHT_ARROW
  180. case "[D":
  181. return XKEY_LEFT_ARROW
  182. case "[H":
  183. return XKEY_HOME
  184. case "[F":
  185. return XKEY_END // terminal
  186. case "[K":
  187. return XKEY_END
  188. case "[V":
  189. return XKEY_PGUP
  190. case "[U":
  191. return XKEY_PGDN
  192. case "[@":
  193. return XKEY_INSERT
  194. case "[1":
  195. // Syncterm is lost, could be F1..F5?
  196. log.Printf("ERROR (Syncterm) Extended %#v\n", extended)
  197. return XKEY_UNKNOWN
  198. case "[2~":
  199. return XKEY_INSERT // terminal
  200. case "[3~":
  201. return XKEY_DELETE // terminal
  202. case "[5~":
  203. return XKEY_PGUP // terminal
  204. case "[6~":
  205. return XKEY_PGDN // terminal
  206. case "[15~":
  207. return XKEY_F5 // terminal
  208. case "[17~":
  209. return XKEY_F6 // terminal
  210. case "[18~":
  211. return XKEY_F7 // terminal
  212. case "[19~":
  213. return XKEY_F8 // terminal
  214. case "[20~":
  215. return XKEY_F9 // terminal
  216. case "[21~":
  217. return XKEY_F10 // terminal
  218. case "[23~":
  219. return XKEY_F11
  220. case "[24~":
  221. return XKEY_F12 // terminal
  222. case "OP":
  223. return XKEY_F1
  224. case "OQ":
  225. return XKEY_F2
  226. case "OR":
  227. return XKEY_F3
  228. case "OS":
  229. return XKEY_F4
  230. case "Ot":
  231. return XKEY_F5 // syncterm
  232. default:
  233. log.Printf("ERROR Extended %#v\n", extended)
  234. return XKEY_UNKNOWN
  235. }
  236. }
  237. return c
  238. }
  239. func (d *Door) Key() int {
  240. return d.WaitKey(Inactivity, 0)
  241. }
  242. func (d *Door) WaitKey(secs int64, usecs int64) int {
  243. if d.Disconnected {
  244. return -2
  245. }
  246. if !d.Pushback.Empty() {
  247. return d.GetKey()
  248. }
  249. timeout := time.Duration(secs)*time.Second + time.Duration(usecs)*time.Millisecond
  250. select {
  251. case res, ok := <-readerChannel:
  252. if ok {
  253. d.Pushback.Push(int(res))
  254. return d.GetKey()
  255. } else {
  256. d.Disconnected = true
  257. return -2
  258. }
  259. case <-time.After(timeout):
  260. return -1
  261. }
  262. }
  263. // Outputs spaces and backspaces
  264. // If you have set a background color, this shows the input area.
  265. func DisplayInput(max int) string {
  266. return strings.Repeat(" ", max) + strings.Repeat("\x08", max)
  267. }
  268. // Input a string of max length.
  269. // This displays the input area if a bg color was set.
  270. // This handles timeout, input, backspace, and enter.
  271. func (d *Door) Input(max int) string {
  272. var line string
  273. // draw input area
  274. d.Write(DisplayInput(max))
  275. var c int
  276. for {
  277. c = d.WaitKey(Inactivity, 0)
  278. if c < 0 {
  279. // timeout/hangup
  280. return ""
  281. }
  282. if c > 1000 {
  283. continue
  284. }
  285. if strconv.IsPrint(rune(c)) {
  286. if len(line) < max {
  287. d.Write(string(byte(c)))
  288. line += string(byte(c))
  289. } else {
  290. d.Write("\x07")
  291. }
  292. } else {
  293. // Non-print
  294. switch c {
  295. case 0x7f, 0x08:
  296. if len(line) > 0 {
  297. d.Write("\x08 \x08")
  298. line = line[:len(line)-1]
  299. }
  300. case 0x0d:
  301. return line
  302. }
  303. }
  304. }
  305. // this is never reached
  306. return line
  307. }