123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530 |
- package door
- import (
- "bufio"
- "bytes"
- "io"
- "log"
- "strings"
- "syscall"
- "time"
- "unicode"
- )
- var ReaderInterval = time.Duration(200) * time.Millisecond
- var ReaderTimeval syscall.Timeval = syscall.Timeval{Sec: 0, Usec: 200}
- // Output a nice string representation of the rune buffer.
- func extended_output(buffer []rune) string {
- var output string = string(buffer)
- output = strings.Replace(output, "\x1b", "^[", -1)
- return output
- }
- // syscall.FdSet clear all
- func clearAll(fdSetPtr *syscall.FdSet) {
- for index := range (*fdSetPtr).Bits {
- (*fdSetPtr).Bits[index] = 0
- }
- }
- // syscall.FdSet set fd
- func set(fdSetPtr *syscall.FdSet, fd int) {
- (*fdSetPtr).Bits[fd/64] |= 1 << uint64(fd%64)
- }
- func RuneToInt8(r rune) int8 {
- return int8(r)
- }
- const READ_SIZE = 16 // Size of read buffer
- var readerBuffer []rune
- // Translate these extended codes into these Extended values.
- var extendedKeys map[string]Extended = map[string]Extended{
- "[A": UP_ARROW,
- "[B": DOWN_ARROW,
- "[C": RIGHT_ARROW,
- "[D": LEFT_ARROW,
- "[H": HOME,
- "[F": END, // terminal
- "[K": END,
- "[V": PAGE_UP,
- "[U": PAGE_DOWN,
- "[@": INSERT,
- "[2~": INSERT, // terminal
- "[3~": DELETE, // terminal
- "[5~": PAGE_UP, // terminal
- "[6~": PAGE_DOWN, // terminal
- "[15~": F5, // terminal
- "[17~": F6, // terminal
- "[18~": F7, // terminal
- "[19~": F8, // terminal
- "[20~": F9, // terminal
- "[21~": F10, // terminal
- "[23~": F11,
- "[24~": F12, // terminal
- "OP": F1,
- "OQ": F2,
- "OR": F3,
- "OS": F4, // syncterm "[1" = F1,F2,F3, and F4)
- "Ot": F5, // syncterm
- }
- func process(d *Door, newRune bool) {
- // This is like GetKey
- var rlen int
- var r, r2 rune
- var has2 bool
- var done bool = false
- for !done {
- rlen = len(readerBuffer)
- if rlen == 0 {
- // Nothing to do here
- return
- }
- if DEBUG_INPUT {
- log.Println("rlen:", rlen, "readerBuffer:", readerBuffer, "newRune:", newRune)
- }
- r = readerBuffer[0]
- if rlen >= 2 {
- r2 = readerBuffer[1]
- has2 = true
- } else {
- r2 = unicode.ReplacementChar
- has2 = false
- }
- // if !has2 and !newRune, then we're "done" (received everything,
- // and there's nothing else coming...)
- // fyneterm CR
- if r == '\x0a' {
- if !has2 && !newRune {
- ArrayPop(&readerBuffer, 1)
- d.readerChannel <- ReaderData{R: '\x0d', Ex: NOP, Err: nil}
- return
- }
- if has2 {
- ArrayPop(&readerBuffer, 1)
- if r2 == '\x00' || r2 == '\x0a' {
- ArrayPop(&readerBuffer, 1)
- }
- d.readerChannel <- ReaderData{R: '\x0d', Ex: NOP, Err: nil}
- } else {
- // We don't have a 2nd rune, and we haven't timed out.
- return
- }
- continue
- }
- // We get 0x0d, 0x00, or 0x0d 0x0a from syncterm
- if r == '\x0d' {
- if !has2 && !newRune {
- ArrayPop(&readerBuffer, 1)
- d.readerChannel <- ReaderData{R: r, Ex: NOP, Err: nil}
- return
- }
- if has2 {
- ArrayPop(&readerBuffer, 1)
- if r2 == '\x00' || r2 == '\x0a' {
- ArrayPop(&readerBuffer, 1)
- }
- d.readerChannel <- ReaderData{R: r, Ex: NOP, Err: nil}
- } else {
- return
- }
- continue
- }
- if r == '\x00' {
- // Possibly doorway mode - deprecated?
- // syncterm does support this.
- if !has2 && !newRune {
- // timeout
- ArrayPop(&readerBuffer, 1)
- d.readerChannel <- ReaderData{R: r, Ex: NOP, Err: nil}
- return
- }
- if has2 {
- ArrayPop(&readerBuffer, 2)
- switch r2 {
- case '\x50':
- d.readerChannel <- ReaderData{0, DOWN_ARROW, nil}
- case '\x48':
- d.readerChannel <- ReaderData{0, UP_ARROW, nil}
- case '\x4b':
- d.readerChannel <- ReaderData{0, LEFT_ARROW, nil}
- case 0x4d:
- d.readerChannel <- ReaderData{0, RIGHT_ARROW, nil}
- case 0x47:
- d.readerChannel <- ReaderData{0, HOME, nil}
- case 0x4f:
- d.readerChannel <- ReaderData{0, END, nil}
- case 0x49:
- d.readerChannel <- ReaderData{0, PAGE_UP, nil}
- case 0x51:
- d.readerChannel <- ReaderData{0, PAGE_DOWN, nil}
- case 0x3b:
- d.readerChannel <- ReaderData{0, F1, nil}
- case 0x3c:
- d.readerChannel <- ReaderData{0, F2, nil}
- case 0x3d:
- d.readerChannel <- ReaderData{0, F3, nil}
- case 0x3e:
- d.readerChannel <- ReaderData{0, F4, nil}
- case 0x3f:
- d.readerChannel <- ReaderData{0, F5, nil}
- case 0x40:
- d.readerChannel <- ReaderData{0, F6, nil}
- case 0x41:
- d.readerChannel <- ReaderData{0, F7, nil}
- case 0x42:
- d.readerChannel <- ReaderData{0, F8, nil}
- case 0x43:
- d.readerChannel <- ReaderData{0, F9, nil}
- case 0x44:
- d.readerChannel <- ReaderData{0, F10, nil}
- case 0x45:
- d.readerChannel <- ReaderData{0, F11, nil}
- case 0x46:
- d.readerChannel <- ReaderData{0, F12, nil}
- case 0x52:
- d.readerChannel <- ReaderData{0, INSERT, nil}
- case 0x53:
- d.readerChannel <- ReaderData{0, DELETE, nil}
- default:
- log.Printf("ERROR Doorway mode: 0x00 %x\n", r2)
- d.readerChannel <- ReaderData{0, UNKNOWN, nil}
- }
- } else {
- return
- }
- continue
- } // end doorway mode
- if r == '\x1b' {
- // Escape key, or ?
- // This is a little harder, since we don't know how many we need.
- if !has2 && !newRune {
- ArrayPop(&readerBuffer, 1)
- d.readerChannel <- ReaderData{r, NOP, nil}
- return
- }
- if has2 {
- // We at least have the 2nd one...
- if r2 == '\x1b' {
- ArrayPop(&readerBuffer, 1)
- d.readerChannel <- ReaderData{r, NOP, nil}
- continue
- }
- // Can't distinguish between \x1bO and \x1bOP (F1)
- /*
- if unicode.IsLetter(r2) {
- // ALT-KEY
- if unicode.IsLower(r2) {
- ArrayPop(&readerBuffer, 2)
- ex := Extended(int(ALT_a) + int(r2-'a'))
- d.readerChannel <- ReaderData{0, ex, nil}
- } else {
- // Must be upper
- ArrayPop(&readerBuffer, 2)
- ex := Extended(int(ALT_A) + int(r2-'A'))
- d.readerChannel <- ReaderData{0, ex, nil}
- }
- continue
- }
- */
- var extended []rune = make([]rune, 0, 10)
- extended = append(extended, r2)
- var extlen = 2 // Length of codes to remove on successful match.
- var pos = 2 // Where we get the next rune from
- var isMouse bool = false
- var found bool = false
- if DEBUG_INPUT {
- log.Println(pos, readerBuffer)
- }
- for pos < rlen {
- r2 = readerBuffer[pos]
- pos++
- extlen++
- extended = append(extended, r2)
- if DEBUG_INPUT {
- log.Println("0x1b LOOP pos:", pos, "extlen:", extlen, "extended:", string(extended))
- }
- ext, has := extendedKeys[string(extended)]
- if has {
- // Found it!
- if DEBUG_INPUT {
- log.Println("Found Extended Match:", ext.String())
- }
- ArrayPop(&readerBuffer, extlen)
- d.readerChannel <- ReaderData{0, ext, nil}
- found = true
- break
- }
- // Mouse codes can also contain letters.
- if !isMouse && unicode.IsLetter(r2) {
- // end of extended code, unless mouse!
- if DEBUG_INPUT {
- log.Println("not mouse, is letter...")
- }
- if string(extended) == "[M" {
- isMouse = true
- } else {
- break
- }
- }
- if isMouse && len(extended) == 5 {
- break
- }
- }
- if found {
- continue
- }
- if DEBUG_INPUT {
- log.Println("POS:", pos, "EXLEN:", extlen, "Extended:", extended, "readerBuffer:", readerBuffer)
- log.Printf("(possible) Extended Code: [%s]", extended_output(extended))
- }
- var exString string = string(extended)
- if strings.HasPrefix(exString, "[M") && len(extended) == 5 {
- // Yes, "valid"
- ArrayPop(&readerBuffer, extlen)
- // Mouse Extended - input zero based (I add +1 to X, Y, and button)
- var mouse Mouse = Mouse{Button: RuneToInt8(extended[2]) - ' ' + 1,
- X: RuneToInt8(extended[3]) - '!' + 1,
- Y: RuneToInt8(extended[4]) - '!' + 1}
- d.AddMouse(mouse)
- log.Println("Mouse:", mouse)
- d.readerChannel <- ReaderData{0, MOUSE, nil}
- continue
- }
- if strings.HasSuffix(exString, "R") {
- // yes, "valid"
- ArrayPop(&readerBuffer, extlen)
- // Cursor Position information (or Shift-F3)
- var cursor CursorPos
- // ^[[1;1R^[[2;3r^[[41;173R
- // Y;X
- exString = exString[1 : len(exString)-1] // Remove [ and R
- pos := SplitToInt(exString, ";")
- if len(pos) == 2 {
- cursor.X = pos[1]
- cursor.Y = pos[0]
- d.AddCursorPos(cursor)
- /*
- d.mcMutex.Lock()
- d.LastCursor = append(d.LastCursor, cursor)
- d.mcMutex.Unlock()
- */
- log.Println("Cursor Pos:", cursor)
- d.readerChannel <- ReaderData{0, CURSOR, nil}
- continue
- } else {
- log.Println("ERROR Cursor Pos:", extended)
- d.readerChannel <- ReaderData{0, UNKNOWN, nil}
- continue
- }
- }
- // Ok, this LOOKS like something invalid...
- if !newRune {
- // Yes, this is invalid.
- ArrayPop(&readerBuffer, extlen)
- log.Println("ERROR Extended:", extended)
- d.readerChannel <- ReaderData{0, UNKNOWN, nil}
- } else {
- if DEBUG_INPUT {
- log.Println("(Possibly) invalid extended:", extended)
- }
- return
- }
- } else {
- return
- }
- continue
- }
- ArrayPop(&readerBuffer, 1)
- d.readerChannel <- ReaderData{r, NOP, nil}
- continue
- }
- }
- // go routine Reader for input
- // This "times out" every ReaderTimeval
- func Reader(d *Door) {
- // I need non-blocking reads here.
- readerBuffer = make([]rune, 0, READ_SIZE*2)
- defer func() {
- log.Printf("~Reader\n")
- if d.ReaderCanClose {
- d.wg.Done()
- }
- }()
- defer func() {
- if err := recover(); err != nil {
- log.Printf("Reader: %#v\n", err)
- }
- }()
- var fdset syscall.FdSet
- var readbuffer [READ_SIZE]byte
- var runebuffer bytes.Buffer
- var runeread = bufio.NewReaderSize(&runebuffer, 1)
- for {
- clearAll(&fdset)
- set(&fdset, d.READFD)
- v, err := syscall.Select(d.READFD+1, &fdset, nil, nil, &ReaderTimeval)
- if err == syscall.EINTR {
- continue
- }
- // log.Printf("Select: %#v / %#v\n", v, err)
- if v == -1 {
- log.Printf("Reader ERR: %#v\n", err)
- d.readerMutex.Lock()
- if !d.ReaderClosed {
- d.ReaderClosed = true
- close(d.readerChannel)
- }
- return
- }
- if v == 0 {
- // timeout
- process(d, false)
- continue
- }
- // The buffer used here must have len & cap to size you want to read.
- // use [BUFF_SIZE]byte for readone
- r, err := syscall.Read(d.READFD, readbuffer[:])
- if r == -1 {
- log.Println("syscall.Read -1 (closed)")
- d.readerMutex.Lock()
- defer d.readerMutex.Unlock()
- if !d.ReaderClosed {
- d.ReaderClosed = true
- d.Disconnected = true
- close(d.readerChannel)
- }
- return
- }
- if r == 0 {
- log.Printf("Select said ready, but: %#v %#v\n", r, err)
- d.readerMutex.Lock()
- defer d.readerMutex.Unlock()
- if !d.ReaderClosed {
- d.ReaderClosed = true
- d.Disconnected = true
- close(d.readerChannel)
- }
- return
- }
- if DEBUG_INPUT {
- log.Printf("Reader << %d, %#v\n", r, readbuffer[:r])
- }
- runebuffer.Write(readbuffer[:r])
- var input rune
- // Is this unicode?
- err = nil
- for err == nil {
- //RuneRead:
- input, _, err = runeread.ReadRune()
- if err == io.EOF {
- break // continue
- }
- if err != nil {
- if DEBUG_INPUT {
- log.Printf("ReadRune: %#v\n", err)
- }
- // errors EOF
- break // continue // break for loop
- }
- if input == unicode.ReplacementChar {
- runeread.UnreadRune()
- b, _ := runeread.ReadByte()
- if DEBUG_INPUT {
- log.Printf("Reader (byte) >> %x\n", b)
- }
- readerBuffer = append(readerBuffer, rune(b))
- // d.readerChannel <- rune(b)
- } else {
- if DEBUG_INPUT {
- log.Printf("Reader >> %x\n", input)
- }
- readerBuffer = append(readerBuffer, input)
- // d.readerChannel <- input
- }
- // process(d, true)
- } // goto RuneRead
- process(d, true)
- // buffer = append(buffer, readone[0])
- /*
- Take2:
- input, size = utf8.DecodeRune(buffer)
- if input != utf8.RuneError {
- d.readerChannel <- input
- for size > 0 {
- ArrayDelete(&buffer, 0)
- size--
- }
- if len(buffer) > 0 {
- goto Take2
- }
- timeoutCount = 0
- } else {
- // Not a valid rune
- continue
- }
- */
- // d.readerChannel <- rune(buffer[0])
- }
- }
|