input.go 6.7 KB

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