input.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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.", 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.Len() != 0 {
  101. e := d.pushback.Front()
  102. d.pushback.Remove(e)
  103. return e.Value.(int)
  104. }
  105. return d.getch()
  106. }
  107. // Return key received, or XKEY_* values.
  108. // -1 timeout/no key
  109. // -2 hangup
  110. // -3 out of time
  111. func (d *Door) GetKey() int {
  112. var c, c2 int
  113. if d.Disconnected {
  114. return -2
  115. }
  116. if d.TimeLeft() < 0 {
  117. return -3
  118. }
  119. c = d.getkey_or_pushback()
  120. if c < 0 {
  121. return c
  122. }
  123. // We get 0x0d 0x00, or 0x0d 0x0a from syncterm.
  124. if c == 0x0d {
  125. c2 = d.getkey_or_pushback()
  126. if c2 > 0 {
  127. // wasn't an error
  128. if c2 != 0x00 && c2 != 0x0a {
  129. // wasn't 0x00 or 0x0a
  130. d.pushback.PushFront(c2)
  131. }
  132. }
  133. return c
  134. }
  135. if c == 0 {
  136. // possibly doorway mode
  137. tries := 0
  138. c2 = d.getkey_or_pushback()
  139. for c2 < 0 {
  140. if tries > 7 {
  141. return c
  142. }
  143. c2 = d.getkey_or_pushback()
  144. tries++
  145. }
  146. switch c2 {
  147. case 0x50:
  148. return XKEY_DOWN_ARROW
  149. case 0x48:
  150. return XKEY_UP_ARROW
  151. case 0x4b:
  152. return XKEY_LEFT_ARROW
  153. case 0x4d:
  154. return XKEY_RIGHT_ARROW
  155. case 0x47:
  156. return XKEY_HOME
  157. case 0x4f:
  158. return XKEY_END
  159. case 0x49:
  160. return XKEY_PGUP
  161. case 0x51:
  162. return XKEY_PGDN
  163. case 0x3b:
  164. return XKEY_F1
  165. case 0x3c:
  166. return XKEY_F2
  167. case 0x3d:
  168. return XKEY_F3
  169. case 0x3e:
  170. return XKEY_F4
  171. case 0x3f:
  172. return XKEY_F5
  173. case 0x40:
  174. return XKEY_F6
  175. case 0x41:
  176. return XKEY_F7
  177. case 0x42:
  178. return XKEY_F8
  179. case 0x43:
  180. return XKEY_F9
  181. case 0x44:
  182. return XKEY_F10
  183. /*
  184. case 0x45:
  185. return XKEY_F11
  186. case 0x46:
  187. return XKEY_F12
  188. */
  189. case 0x52:
  190. return XKEY_INSERT
  191. case 0x53:
  192. return XKEY_DELETE
  193. default:
  194. fmt.Printf("ERROR Doorway mode: 0x00 %x\n", c2)
  195. return XKEY_UNKNOWN
  196. }
  197. }
  198. if c == 0x1b {
  199. // Escape key?
  200. c2 = d.getkey_or_pushback()
  201. if c2 < 0 {
  202. // Just escape key
  203. return c
  204. }
  205. var extended string = string(c2)
  206. c2 = d.getkey_or_pushback()
  207. for c2 > 0 {
  208. if c2 == 0x1b {
  209. d.pushback.PushBack(c2)
  210. break
  211. }
  212. extended += string(c2)
  213. c2 = d.getkey_or_pushback()
  214. }
  215. switch extended {
  216. case "[A":
  217. return XKEY_UP_ARROW
  218. case "[B":
  219. return XKEY_DOWN_ARROW
  220. case "[C":
  221. return XKEY_RIGHT_ARROW
  222. case "[D":
  223. return XKEY_LEFT_ARROW
  224. case "[H":
  225. return XKEY_HOME
  226. case "[F":
  227. return XKEY_END // terminal
  228. case "[K":
  229. return XKEY_END
  230. case "[V":
  231. return XKEY_PGUP
  232. case "[U":
  233. return XKEY_PGDN
  234. case "[@":
  235. return XKEY_INSERT
  236. case "[1":
  237. // Syncterm is lost, could be F1..F5?
  238. fmt.Printf("ERROR (Syncterm) Extended %#v\n", extended)
  239. return XKEY_UNKNOWN
  240. case "[2~":
  241. return XKEY_INSERT // terminal
  242. case "[3~":
  243. return XKEY_DELETE // terminal
  244. case "[5~":
  245. return XKEY_PGUP // terminal
  246. case "[6~":
  247. return XKEY_PGDN // terminal
  248. case "[15~":
  249. return XKEY_F5 // terminal
  250. case "[17~":
  251. return XKEY_F6 // terminal
  252. case "[18~":
  253. return XKEY_F7 // terminal
  254. case "[19~":
  255. return XKEY_F8 // terminal
  256. case "[20~":
  257. return XKEY_F9 // terminal
  258. case "[21~":
  259. return XKEY_F10 // terminal
  260. case "[23~":
  261. return XKEY_F11
  262. case "[24~":
  263. return XKEY_F12 // terminal
  264. case "OP":
  265. return XKEY_F1
  266. case "OQ":
  267. return XKEY_F2
  268. case "OR":
  269. return XKEY_F3
  270. case "OS":
  271. return XKEY_F4
  272. case "Ot":
  273. return XKEY_F5 // syncterm
  274. default:
  275. fmt.Printf("ERROR Extended %#v\n", extended)
  276. return XKEY_UNKNOWN
  277. }
  278. }
  279. return c
  280. }
  281. func (d *Door) Key() int {
  282. return d.WaitKey(Inactivity, 0)
  283. }
  284. func (d *Door) WaitKey(secs int64, usecs int64) int {
  285. if d.Disconnected {
  286. return -2
  287. }
  288. var fdsetRead syscall.FdSet
  289. retry:
  290. clearAll(&fdsetRead)
  291. set(&fdsetRead, d.READFD)
  292. timeout := syscall.Timeval{Sec: secs, Usec: usecs}
  293. v, err := syscall.Select(d.READFD+1, &fdsetRead, nil, nil, &timeout)
  294. if v == -1 {
  295. errno, ok := err.(syscall.Errno)
  296. if ok && errno == syscall.EINTR {
  297. // Should I retry my syscall.Select here? Yes! -1 is not return value.
  298. fmt.Printf("WaitKey: EINTR\n")
  299. goto retry
  300. // return -1
  301. }
  302. /*
  303. Interrupted system call ??? What does this mean?
  304. This isn't hangup. I'm still connected.
  305. -1 : interrupted system call
  306. This seemed to happen when I called WaitKey many times with usecs.
  307. */
  308. fmt.Println("-1 : ", err)
  309. // hangup ?!
  310. return -2
  311. }
  312. if v == 0 {
  313. // timeout
  314. return -1
  315. }
  316. return d.GetKey()
  317. // var buffer []byte -- 0 byte buffer. doh!
  318. buffer := make([]byte, 1)
  319. r, err := syscall.Read(d.READFD, buffer)
  320. if r != 1 {
  321. // I'm getting write error here... (when disconnected)
  322. fmt.Printf("Read said ready, but didn't read a character %d %v.", r, err)
  323. // hangup
  324. d.Disconnected = true
  325. return -2
  326. }
  327. return int(buffer[0])
  328. }
  329. // Outputs spaces and backspaces
  330. // If you have set a background color, this shows the input area.
  331. func (d *Door) DisplayInput(max int) {
  332. d.Write(strings.Repeat(" ", max) + strings.Repeat("\x08", max))
  333. }
  334. // Input a string of max length.
  335. // This displays the input area if a bg color was set.
  336. // This handles timeout, input, backspace, and enter.
  337. func (d *Door) Input(max int) string {
  338. var line string
  339. // draw input area
  340. d.DisplayInput(max)
  341. var c int
  342. for true {
  343. c = d.WaitKey(Inactivity, 0)
  344. if c < 0 {
  345. // timeout/hangup
  346. return ""
  347. }
  348. if c > 1000 {
  349. continue
  350. }
  351. if strconv.IsPrint(rune(c)) {
  352. if len(line) < max {
  353. d.Write(string(c))
  354. line += string(c)
  355. } else {
  356. d.Write("\x07")
  357. }
  358. } else {
  359. // Non-print
  360. switch c {
  361. case 0x7f, 0x08:
  362. if len(line) > 0 {
  363. d.Write("\x08 \x08")
  364. line = line[:len(line)-1]
  365. }
  366. case 0x0d:
  367. return line
  368. }
  369. }
  370. }
  371. // this is never reached
  372. return line
  373. }