input.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. package door
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. "syscall"
  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. // from: https://github.com/yubo/dea_ng
  35. // https://github.com/yubo/dea_ng/blob/master/go/src/directoryserver/streaming.go
  36. func set(fdSetPtr *syscall.FdSet, fd int) {
  37. (*fdSetPtr).Bits[fd/64] |= 1 << uint64(fd%64)
  38. }
  39. func isSet(fdSetPtr *syscall.FdSet, fd int) bool {
  40. return ((*fdSetPtr).Bits[fd/64] & (1 << uint64(fd%64))) != 0
  41. }
  42. func clearAll(fdSetPtr *syscall.FdSet) {
  43. for index, _ := range (*fdSetPtr).Bits {
  44. (*fdSetPtr).Bits[index] = 0
  45. }
  46. }
  47. // Is there a key waiting?
  48. // Returns true on key, or if hungup/time out
  49. func (d *Door) HasKey() bool {
  50. if d.Disconnected {
  51. return true
  52. }
  53. if d.TimeLeft() < 0 {
  54. return true
  55. }
  56. var fdsetRead = syscall.FdSet{}
  57. clearAll(&fdsetRead)
  58. set(&fdsetRead, d.READFD)
  59. timeout := syscall.Timeval{Sec: 0, Usec: 1}
  60. v, _ := syscall.Select(d.READFD+1, &fdsetRead, nil, nil, &timeout)
  61. if v == -1 {
  62. return false
  63. }
  64. if v == 0 {
  65. return false
  66. }
  67. return true
  68. }
  69. // Low level read key function.
  70. // This gets the raw keys from the client, it doesn't handle extended keys,
  71. // functions, arrows.
  72. // Return key, or -1 (Timeout/No key available), -2 hangup
  73. func (d *Door) getch() int {
  74. var fdsetRead syscall.FdSet
  75. clearAll(&fdsetRead)
  76. set(&fdsetRead, d.READFD)
  77. // 100 Usec seems like a good value, works with QModem in dosbox.
  78. timeout := syscall.Timeval{Sec: 0, Usec: 100}
  79. v, err := syscall.Select(d.READFD+1, &fdsetRead, nil, nil, &timeout)
  80. if v == -1 {
  81. // hangup
  82. return -2
  83. }
  84. if v == 0 {
  85. // timeout
  86. return -1
  87. }
  88. buffer := make([]byte, 1)
  89. r, err := syscall.Read(d.READFD, buffer)
  90. if r != 1 {
  91. // I'm getting write error here... (when disconnected)
  92. fmt.Printf("Read said ready, but didn't read a character %d %v.\n", r, err)
  93. // hangup
  94. d.Disconnected = true
  95. return -2
  96. }
  97. return int(buffer[0])
  98. }
  99. func (d *Door) getkey_or_pushback() int {
  100. if !d.Pushback.Empty() {
  101. return d.Pushback.Pop()
  102. }
  103. if false {
  104. key := d.getch()
  105. fmt.Printf("%d / %X\n", key, key)
  106. return key
  107. } else {
  108. return d.getch()
  109. }
  110. }
  111. // Return key received, or XKEY_* values.
  112. // -1 timeout/no key
  113. // -2 hangup
  114. // -3 out of time
  115. func (d *Door) GetKey() int {
  116. var c, c2 int
  117. if d.Disconnected {
  118. return -2
  119. }
  120. if d.TimeLeft() < 0 {
  121. return -3
  122. }
  123. c = d.getkey_or_pushback()
  124. if c < 0 {
  125. return c
  126. }
  127. // We get 0x0d 0x00, or 0x0d 0x0a from syncterm.
  128. if c == 0x0d {
  129. c2 = d.getkey_or_pushback()
  130. if c2 > 0 {
  131. // wasn't an error
  132. if c2 != 0x00 && c2 != 0x0a {
  133. // wasn't 0x00 or 0x0a
  134. d.Pushback.Push(c2)
  135. // fmt.Printf("Push 0x0d trailer %d / %x\n", c2, c2)
  136. }
  137. }
  138. return c
  139. }
  140. if c == 0 {
  141. // possibly doorway mode
  142. tries := 0
  143. c2 = d.getkey_or_pushback()
  144. for c2 < 0 {
  145. if tries > 7 {
  146. return c
  147. }
  148. c2 = d.getkey_or_pushback()
  149. tries++
  150. }
  151. switch c2 {
  152. case 0x50:
  153. return XKEY_DOWN_ARROW
  154. case 0x48:
  155. return XKEY_UP_ARROW
  156. case 0x4b:
  157. return XKEY_LEFT_ARROW
  158. case 0x4d:
  159. return XKEY_RIGHT_ARROW
  160. case 0x47:
  161. return XKEY_HOME
  162. case 0x4f:
  163. return XKEY_END
  164. case 0x49:
  165. return XKEY_PGUP
  166. case 0x51:
  167. return XKEY_PGDN
  168. case 0x3b:
  169. return XKEY_F1
  170. case 0x3c:
  171. return XKEY_F2
  172. case 0x3d:
  173. return XKEY_F3
  174. case 0x3e:
  175. return XKEY_F4
  176. case 0x3f:
  177. return XKEY_F5
  178. case 0x40:
  179. return XKEY_F6
  180. case 0x41:
  181. return XKEY_F7
  182. case 0x42:
  183. return XKEY_F8
  184. case 0x43:
  185. return XKEY_F9
  186. case 0x44:
  187. return XKEY_F10
  188. /*
  189. case 0x45:
  190. return XKEY_F11
  191. case 0x46:
  192. return XKEY_F12
  193. */
  194. case 0x52:
  195. return XKEY_INSERT
  196. case 0x53:
  197. return XKEY_DELETE
  198. default:
  199. fmt.Printf("ERROR Doorway mode: 0x00 %x\n", c2)
  200. return XKEY_UNKNOWN
  201. }
  202. }
  203. if c == 0x1b {
  204. // Escape key?
  205. c2 = d.getkey_or_pushback()
  206. if c2 < 0 {
  207. // Just escape key
  208. return c
  209. }
  210. var extended string = string(byte(c2))
  211. c2 = d.getkey_or_pushback()
  212. for c2 > 0 {
  213. if c2 == 0x1b {
  214. d.Pushback.Push(c2)
  215. break
  216. }
  217. extended += string(byte(c2))
  218. c2 = d.getkey_or_pushback()
  219. }
  220. switch extended {
  221. case "[A":
  222. return XKEY_UP_ARROW
  223. case "[B":
  224. return XKEY_DOWN_ARROW
  225. case "[C":
  226. return XKEY_RIGHT_ARROW
  227. case "[D":
  228. return XKEY_LEFT_ARROW
  229. case "[H":
  230. return XKEY_HOME
  231. case "[F":
  232. return XKEY_END // terminal
  233. case "[K":
  234. return XKEY_END
  235. case "[V":
  236. return XKEY_PGUP
  237. case "[U":
  238. return XKEY_PGDN
  239. case "[@":
  240. return XKEY_INSERT
  241. case "[1":
  242. // Syncterm is lost, could be F1..F5?
  243. fmt.Printf("ERROR (Syncterm) Extended %#v\n", extended)
  244. return XKEY_UNKNOWN
  245. case "[2~":
  246. return XKEY_INSERT // terminal
  247. case "[3~":
  248. return XKEY_DELETE // terminal
  249. case "[5~":
  250. return XKEY_PGUP // terminal
  251. case "[6~":
  252. return XKEY_PGDN // terminal
  253. case "[15~":
  254. return XKEY_F5 // terminal
  255. case "[17~":
  256. return XKEY_F6 // terminal
  257. case "[18~":
  258. return XKEY_F7 // terminal
  259. case "[19~":
  260. return XKEY_F8 // terminal
  261. case "[20~":
  262. return XKEY_F9 // terminal
  263. case "[21~":
  264. return XKEY_F10 // terminal
  265. case "[23~":
  266. return XKEY_F11
  267. case "[24~":
  268. return XKEY_F12 // terminal
  269. case "OP":
  270. return XKEY_F1
  271. case "OQ":
  272. return XKEY_F2
  273. case "OR":
  274. return XKEY_F3
  275. case "OS":
  276. return XKEY_F4
  277. case "Ot":
  278. return XKEY_F5 // syncterm
  279. default:
  280. fmt.Printf("ERROR Extended %#v\n", extended)
  281. return XKEY_UNKNOWN
  282. }
  283. }
  284. return c
  285. }
  286. func (d *Door) Key() int {
  287. return d.WaitKey(Inactivity, 0)
  288. }
  289. func (d *Door) WaitKey(secs int64, usecs int64) int {
  290. if d.Disconnected {
  291. return -2
  292. }
  293. if !d.Pushback.Empty() {
  294. return d.GetKey()
  295. }
  296. var fdsetRead syscall.FdSet
  297. retry:
  298. clearAll(&fdsetRead)
  299. set(&fdsetRead, d.READFD)
  300. timeout := syscall.Timeval{Sec: secs, Usec: usecs}
  301. v, err := syscall.Select(d.READFD+1, &fdsetRead, nil, nil, &timeout)
  302. if v == -1 {
  303. errno, ok := err.(syscall.Errno)
  304. if ok && errno == syscall.EINTR {
  305. // Should I retry my syscall.Select here? Yes! -1 is not return value.
  306. fmt.Printf("WaitKey: EINTR\n")
  307. goto retry
  308. // return -1
  309. }
  310. /*
  311. Interrupted system call ??? What does this mean?
  312. This isn't hangup. I'm still connected.
  313. -1 : interrupted system call
  314. This seemed to happen when I called WaitKey many times with usecs.
  315. */
  316. fmt.Println("-1 : ", err)
  317. // hangup ?!
  318. return -2
  319. }
  320. if v == 0 {
  321. // timeout
  322. return -1
  323. }
  324. return d.GetKey()
  325. }
  326. // Outputs spaces and backspaces
  327. // If you have set a background color, this shows the input area.
  328. func DisplayInput(max int) string {
  329. return strings.Repeat(" ", max) + strings.Repeat("\x08", max)
  330. }
  331. // Input a string of max length.
  332. // This displays the input area if a bg color was set.
  333. // This handles timeout, input, backspace, and enter.
  334. func (d *Door) Input(max int) string {
  335. var line string
  336. // draw input area
  337. d.Write(DisplayInput(max))
  338. var c int
  339. for true {
  340. c = d.WaitKey(Inactivity, 0)
  341. if c < 0 {
  342. // timeout/hangup
  343. return ""
  344. }
  345. if c > 1000 {
  346. continue
  347. }
  348. if strconv.IsPrint(rune(c)) {
  349. if len(line) < max {
  350. d.Write(string(byte(c)))
  351. line += string(byte(c))
  352. } else {
  353. d.Write("\x07")
  354. }
  355. } else {
  356. // Non-print
  357. switch c {
  358. case 0x7f, 0x08:
  359. if len(line) > 0 {
  360. d.Write("\x08 \x08")
  361. line = line[:len(line)-1]
  362. }
  363. case 0x0d:
  364. return line
  365. }
  366. }
  367. }
  368. // this is never reached
  369. return line
  370. }