input_linux.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. package door
  2. import (
  3. "bufio"
  4. "bytes"
  5. "io"
  6. "log"
  7. "strings"
  8. "syscall"
  9. "time"
  10. "unicode"
  11. )
  12. var ReaderInterval = time.Duration(200) * time.Millisecond
  13. var ReaderTimeval syscall.Timeval = syscall.Timeval{Sec: 0, Usec: 200}
  14. // Output a nice string representation of the rune buffer.
  15. func extended_output(buffer []rune) string {
  16. var output string = string(buffer)
  17. output = strings.Replace(output, "\x1b", "^[", -1)
  18. return output
  19. }
  20. // syscall.FdSet clear all
  21. func clearAll(fdSetPtr *syscall.FdSet) {
  22. for index := range (*fdSetPtr).Bits {
  23. (*fdSetPtr).Bits[index] = 0
  24. }
  25. }
  26. // syscall.FdSet set fd
  27. func set(fdSetPtr *syscall.FdSet, fd int) {
  28. (*fdSetPtr).Bits[fd/64] |= 1 << uint64(fd%64)
  29. }
  30. func RuneToInt8(r rune) int8 {
  31. return int8(r)
  32. }
  33. const READ_SIZE = 16 // Size of read buffer
  34. var readerBuffer []rune
  35. // Translate these extended codes into these Extended values.
  36. var extendedKeys map[string]Extended = map[string]Extended{
  37. "[A": UP_ARROW,
  38. "[B": DOWN_ARROW,
  39. "[C": RIGHT_ARROW,
  40. "[D": LEFT_ARROW,
  41. "[H": HOME,
  42. "[F": END, // terminal
  43. "[K": END,
  44. "[V": PAGE_UP,
  45. "[U": PAGE_DOWN,
  46. "[@": INSERT,
  47. "[2~": INSERT, // terminal
  48. "[3~": DELETE, // terminal
  49. "[5~": PAGE_UP, // terminal
  50. "[6~": PAGE_DOWN, // terminal
  51. "[15~": F5, // terminal
  52. "[17~": F6, // terminal
  53. "[18~": F7, // terminal
  54. "[19~": F8, // terminal
  55. "[20~": F9, // terminal
  56. "[21~": F10, // terminal
  57. "[23~": F11,
  58. "[24~": F12, // terminal
  59. "OP": F1,
  60. "OQ": F2,
  61. "OR": F3,
  62. "OS": F4, // syncterm "[1" = F1,F2,F3, and F4)
  63. "Ot": F5, // syncterm
  64. }
  65. func process(d *Door, newRune bool) {
  66. // This is like GetKey
  67. var rlen int
  68. var r, r2 rune
  69. var has2 bool
  70. var done bool = false
  71. for !done {
  72. rlen = len(readerBuffer)
  73. if rlen == 0 {
  74. // Nothing to do here
  75. return
  76. }
  77. if DEBUG_INPUT {
  78. log.Println("rlen:", rlen, "readerBuffer:", readerBuffer, "newRune:", newRune)
  79. }
  80. r = readerBuffer[0]
  81. if rlen >= 2 {
  82. r2 = readerBuffer[1]
  83. has2 = true
  84. } else {
  85. r2 = unicode.ReplacementChar
  86. has2 = false
  87. }
  88. // if !has2 and !newRune, then we're "done" (received everything,
  89. // and there's nothing else coming...)
  90. // fyneterm CR
  91. if r == '\x0a' {
  92. if !has2 && !newRune {
  93. ArrayPop(&readerBuffer, 1)
  94. d.readerChannel <- ReaderData{R: '\x0d', Ex: NOP, Err: nil}
  95. return
  96. }
  97. if has2 {
  98. ArrayPop(&readerBuffer, 1)
  99. if r2 == '\x00' || r2 == '\x0a' {
  100. ArrayPop(&readerBuffer, 1)
  101. }
  102. d.readerChannel <- ReaderData{R: '\x0d', Ex: NOP, Err: nil}
  103. } else {
  104. // We don't have a 2nd rune, and we haven't timed out.
  105. return
  106. }
  107. continue
  108. }
  109. // We get 0x0d, 0x00, or 0x0d 0x0a from syncterm
  110. if r == '\x0d' {
  111. if !has2 && !newRune {
  112. ArrayPop(&readerBuffer, 1)
  113. d.readerChannel <- ReaderData{R: r, Ex: NOP, Err: nil}
  114. return
  115. }
  116. if has2 {
  117. ArrayPop(&readerBuffer, 1)
  118. if r2 == '\x00' || r2 == '\x0a' {
  119. ArrayPop(&readerBuffer, 1)
  120. }
  121. d.readerChannel <- ReaderData{R: r, Ex: NOP, Err: nil}
  122. } else {
  123. return
  124. }
  125. continue
  126. }
  127. if r == '\x00' {
  128. // Possibly doorway mode - deprecated?
  129. // syncterm does support this.
  130. if !has2 && !newRune {
  131. // timeout
  132. ArrayPop(&readerBuffer, 1)
  133. d.readerChannel <- ReaderData{R: r, Ex: NOP, Err: nil}
  134. return
  135. }
  136. if has2 {
  137. ArrayPop(&readerBuffer, 2)
  138. switch r2 {
  139. case '\x50':
  140. d.readerChannel <- ReaderData{0, DOWN_ARROW, nil}
  141. case '\x48':
  142. d.readerChannel <- ReaderData{0, UP_ARROW, nil}
  143. case '\x4b':
  144. d.readerChannel <- ReaderData{0, LEFT_ARROW, nil}
  145. case 0x4d:
  146. d.readerChannel <- ReaderData{0, RIGHT_ARROW, nil}
  147. case 0x47:
  148. d.readerChannel <- ReaderData{0, HOME, nil}
  149. case 0x4f:
  150. d.readerChannel <- ReaderData{0, END, nil}
  151. case 0x49:
  152. d.readerChannel <- ReaderData{0, PAGE_UP, nil}
  153. case 0x51:
  154. d.readerChannel <- ReaderData{0, PAGE_DOWN, nil}
  155. case 0x3b:
  156. d.readerChannel <- ReaderData{0, F1, nil}
  157. case 0x3c:
  158. d.readerChannel <- ReaderData{0, F2, nil}
  159. case 0x3d:
  160. d.readerChannel <- ReaderData{0, F3, nil}
  161. case 0x3e:
  162. d.readerChannel <- ReaderData{0, F4, nil}
  163. case 0x3f:
  164. d.readerChannel <- ReaderData{0, F5, nil}
  165. case 0x40:
  166. d.readerChannel <- ReaderData{0, F6, nil}
  167. case 0x41:
  168. d.readerChannel <- ReaderData{0, F7, nil}
  169. case 0x42:
  170. d.readerChannel <- ReaderData{0, F8, nil}
  171. case 0x43:
  172. d.readerChannel <- ReaderData{0, F9, nil}
  173. case 0x44:
  174. d.readerChannel <- ReaderData{0, F10, nil}
  175. case 0x45:
  176. d.readerChannel <- ReaderData{0, F11, nil}
  177. case 0x46:
  178. d.readerChannel <- ReaderData{0, F12, nil}
  179. case 0x52:
  180. d.readerChannel <- ReaderData{0, INSERT, nil}
  181. case 0x53:
  182. d.readerChannel <- ReaderData{0, DELETE, nil}
  183. default:
  184. log.Printf("ERROR Doorway mode: 0x00 %x\n", r2)
  185. d.readerChannel <- ReaderData{0, UNKNOWN, nil}
  186. }
  187. } else {
  188. return
  189. }
  190. continue
  191. } // end doorway mode
  192. if r == '\x1b' {
  193. // Escape key, or ?
  194. // This is a little harder, since we don't know how many we need.
  195. if !has2 && !newRune {
  196. ArrayPop(&readerBuffer, 1)
  197. d.readerChannel <- ReaderData{r, NOP, nil}
  198. return
  199. }
  200. if has2 {
  201. // We at least have the 2nd one...
  202. if r2 == '\x1b' {
  203. ArrayPop(&readerBuffer, 1)
  204. d.readerChannel <- ReaderData{r, NOP, nil}
  205. continue
  206. }
  207. // Can't distinguish between \x1bO and \x1bOP (F1)
  208. /*
  209. if unicode.IsLetter(r2) {
  210. // ALT-KEY
  211. if unicode.IsLower(r2) {
  212. ArrayPop(&readerBuffer, 2)
  213. ex := Extended(int(ALT_a) + int(r2-'a'))
  214. d.readerChannel <- ReaderData{0, ex, nil}
  215. } else {
  216. // Must be upper
  217. ArrayPop(&readerBuffer, 2)
  218. ex := Extended(int(ALT_A) + int(r2-'A'))
  219. d.readerChannel <- ReaderData{0, ex, nil}
  220. }
  221. continue
  222. }
  223. */
  224. var extended []rune = make([]rune, 0, 10)
  225. extended = append(extended, r2)
  226. var extlen = 2 // Length of codes to remove on successful match.
  227. var pos = 2 // Where we get the next rune from
  228. var isMouse bool = false
  229. var found bool = false
  230. if DEBUG_INPUT {
  231. log.Println(pos, readerBuffer)
  232. }
  233. for pos < rlen {
  234. r2 = readerBuffer[pos]
  235. pos++
  236. extlen++
  237. extended = append(extended, r2)
  238. if DEBUG_INPUT {
  239. log.Println("0x1b LOOP pos:", pos, "extlen:", extlen, "extended:", string(extended))
  240. }
  241. ext, has := extendedKeys[string(extended)]
  242. if has {
  243. // Found it!
  244. if DEBUG_INPUT {
  245. log.Println("Found Extended Match:", ext.String())
  246. }
  247. ArrayPop(&readerBuffer, extlen)
  248. d.readerChannel <- ReaderData{0, ext, nil}
  249. found = true
  250. break
  251. }
  252. // Mouse codes can also contain letters.
  253. if !isMouse && unicode.IsLetter(r2) {
  254. // end of extended code, unless mouse!
  255. if DEBUG_INPUT {
  256. log.Println("not mouse, is letter...")
  257. }
  258. if string(extended) == "[M" {
  259. isMouse = true
  260. } else {
  261. break
  262. }
  263. }
  264. if isMouse && len(extended) == 5 {
  265. break
  266. }
  267. }
  268. if found {
  269. continue
  270. }
  271. if DEBUG_INPUT {
  272. log.Println("POS:", pos, "EXLEN:", extlen, "Extended:", extended, "readerBuffer:", readerBuffer)
  273. log.Printf("(possible) Extended Code: [%s]", extended_output(extended))
  274. }
  275. var exString string = string(extended)
  276. if strings.HasPrefix(exString, "[M") && len(extended) == 5 {
  277. // Yes, "valid"
  278. ArrayPop(&readerBuffer, extlen)
  279. // Mouse Extended - input zero based (I add +1 to X, Y, and button)
  280. var mouse Mouse = Mouse{Button: RuneToInt8(extended[2]) - ' ' + 1,
  281. X: RuneToInt8(extended[3]) - '!' + 1,
  282. Y: RuneToInt8(extended[4]) - '!' + 1}
  283. d.AddMouse(mouse)
  284. log.Println("Mouse:", mouse)
  285. d.readerChannel <- ReaderData{0, MOUSE, nil}
  286. continue
  287. }
  288. if strings.HasSuffix(exString, "R") {
  289. // yes, "valid"
  290. ArrayPop(&readerBuffer, extlen)
  291. // Cursor Position information (or Shift-F3)
  292. var cursor CursorPos
  293. // ^[[1;1R^[[2;3r^[[41;173R
  294. // Y;X
  295. exString = exString[1 : len(exString)-1] // Remove [ and R
  296. pos := SplitToInt(exString, ";")
  297. if len(pos) == 2 {
  298. cursor.X = pos[1]
  299. cursor.Y = pos[0]
  300. d.AddCursorPos(cursor)
  301. /*
  302. d.mcMutex.Lock()
  303. d.LastCursor = append(d.LastCursor, cursor)
  304. d.mcMutex.Unlock()
  305. */
  306. log.Println("Cursor Pos:", cursor)
  307. d.readerChannel <- ReaderData{0, CURSOR, nil}
  308. continue
  309. } else {
  310. log.Println("ERROR Cursor Pos:", extended)
  311. d.readerChannel <- ReaderData{0, UNKNOWN, nil}
  312. continue
  313. }
  314. }
  315. // Ok, this LOOKS like something invalid...
  316. if !newRune {
  317. // Yes, this is invalid.
  318. ArrayPop(&readerBuffer, extlen)
  319. log.Println("ERROR Extended:", extended)
  320. d.readerChannel <- ReaderData{0, UNKNOWN, nil}
  321. } else {
  322. if DEBUG_INPUT {
  323. log.Println("(Possibly) invalid extended:", extended)
  324. }
  325. return
  326. }
  327. } else {
  328. return
  329. }
  330. continue
  331. }
  332. ArrayPop(&readerBuffer, 1)
  333. d.readerChannel <- ReaderData{r, NOP, nil}
  334. continue
  335. }
  336. }
  337. // go routine Reader for input
  338. // This "times out" every ReaderTimeval
  339. func Reader(d *Door) {
  340. // I need non-blocking reads here.
  341. readerBuffer = make([]rune, 0, READ_SIZE*2)
  342. defer func() {
  343. log.Printf("~Reader\n")
  344. if d.ReaderCanClose {
  345. d.wg.Done()
  346. }
  347. }()
  348. defer func() {
  349. if err := recover(); err != nil {
  350. log.Printf("Reader: %#v\n", err)
  351. }
  352. }()
  353. var fdset syscall.FdSet
  354. var readbuffer [READ_SIZE]byte
  355. var runebuffer bytes.Buffer
  356. var runeread = bufio.NewReaderSize(&runebuffer, 1)
  357. for {
  358. clearAll(&fdset)
  359. set(&fdset, d.READFD)
  360. v, err := syscall.Select(d.READFD+1, &fdset, nil, nil, &ReaderTimeval)
  361. if err == syscall.EINTR {
  362. continue
  363. }
  364. // log.Printf("Select: %#v / %#v\n", v, err)
  365. if v == -1 {
  366. log.Printf("Reader ERR: %#v\n", err)
  367. d.readerMutex.Lock()
  368. if !d.ReaderClosed {
  369. d.ReaderClosed = true
  370. close(d.readerChannel)
  371. }
  372. return
  373. }
  374. if v == 0 {
  375. // timeout
  376. process(d, false)
  377. continue
  378. }
  379. // The buffer used here must have len & cap to size you want to read.
  380. // use [BUFF_SIZE]byte for readone
  381. r, err := syscall.Read(d.READFD, readbuffer[:])
  382. if r == -1 {
  383. log.Println("syscall.Read -1 (closed)")
  384. d.readerMutex.Lock()
  385. defer d.readerMutex.Unlock()
  386. if !d.ReaderClosed {
  387. d.ReaderClosed = true
  388. d.Disconnected = true
  389. close(d.readerChannel)
  390. }
  391. return
  392. }
  393. if r == 0 {
  394. log.Printf("Select said ready, but: %#v %#v\n", r, err)
  395. d.readerMutex.Lock()
  396. defer d.readerMutex.Unlock()
  397. if !d.ReaderClosed {
  398. d.ReaderClosed = true
  399. d.Disconnected = true
  400. close(d.readerChannel)
  401. }
  402. return
  403. }
  404. if DEBUG_INPUT {
  405. log.Printf("Reader << %d, %#v\n", r, readbuffer[:r])
  406. }
  407. runebuffer.Write(readbuffer[:r])
  408. var input rune
  409. // Is this unicode?
  410. err = nil
  411. for err == nil {
  412. //RuneRead:
  413. input, _, err = runeread.ReadRune()
  414. if err == io.EOF {
  415. break // continue
  416. }
  417. if err != nil {
  418. if DEBUG_INPUT {
  419. log.Printf("ReadRune: %#v\n", err)
  420. }
  421. // errors EOF
  422. break // continue // break for loop
  423. }
  424. if input == unicode.ReplacementChar {
  425. runeread.UnreadRune()
  426. b, _ := runeread.ReadByte()
  427. if DEBUG_INPUT {
  428. log.Printf("Reader (byte) >> %x\n", b)
  429. }
  430. readerBuffer = append(readerBuffer, rune(b))
  431. // d.readerChannel <- rune(b)
  432. } else {
  433. if DEBUG_INPUT {
  434. log.Printf("Reader >> %x\n", input)
  435. }
  436. readerBuffer = append(readerBuffer, input)
  437. // d.readerChannel <- input
  438. }
  439. // process(d, true)
  440. } // goto RuneRead
  441. process(d, true)
  442. // buffer = append(buffer, readone[0])
  443. /*
  444. Take2:
  445. input, size = utf8.DecodeRune(buffer)
  446. if input != utf8.RuneError {
  447. d.readerChannel <- input
  448. for size > 0 {
  449. ArrayDelete(&buffer, 0)
  450. size--
  451. }
  452. if len(buffer) > 0 {
  453. goto Take2
  454. }
  455. timeoutCount = 0
  456. } else {
  457. // Not a valid rune
  458. continue
  459. }
  460. */
  461. // d.readerChannel <- rune(buffer[0])
  462. }
  463. }