input.go 8.0 KB

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