input_windows.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package door
  2. // Don't use : #cgo CFLAGS: -Wall
  3. // https://github.com/golang/go/issues/6883#issuecomment-383800123
  4. // #cgo LDFLAGS: -lws2_32
  5. // #include "input_windows.h"
  6. import "C"
  7. import (
  8. "bufio"
  9. "bytes"
  10. "log"
  11. "strings"
  12. "syscall"
  13. "time"
  14. "unsafe"
  15. )
  16. var ReaderInterval = time.Duration(200) * time.Millisecond
  17. var ReaderTimeval syscall.Timeval = syscall.Timeval{Sec: 0, Usec: 200}
  18. // Output a nice string representation of the rune buffer.
  19. func extended_output(buffer []rune) string {
  20. var output string = string(buffer)
  21. output = strings.Replace(output, "\x1b", "^[", -1)
  22. return output
  23. }
  24. func Reader(d *Door) {
  25. // handle syscall.Handle, readerChannel *chan byte
  26. C.init_wsa()
  27. var runebuffer bytes.Buffer
  28. var runeread = bufio.NewReaderSize(&runebuffer, 1)
  29. // readerBuffer = make([]rune, 0, READ_SIZE*2)
  30. readerbuffer := C.malloc(C.sizeof_char * READ_SIZE)
  31. defer C.free(unsafe.Pointer(readerbuffer))
  32. defer func() {
  33. log.Printf("~Reader")
  34. // if d.ReaderCanClose {
  35. d.wg.Done()
  36. // }
  37. }()
  38. defer func() {
  39. if err := recover(); err != nil {
  40. log.Printf("Reader: %#v\n", err)
  41. }
  42. }()
  43. var r int
  44. for {
  45. read := C.windows_read(C.int(d.Config.Comm_handle), (*C.char)(readerbuffer), C.int(READ_SIZE))
  46. r = int(read)
  47. // log.Printf("windows_read: %d\n", r)
  48. if r == -1 {
  49. log.Println("window_read -1 (closed)")
  50. d.readerMutex.Lock()
  51. defer d.readerMutex.Unlock()
  52. if !d.ReaderClosed {
  53. d.ReaderClosed = true
  54. d.Disconnected = true
  55. close(d.readerChannel)
  56. }
  57. return
  58. }
  59. if r == 0 {
  60. // timeout
  61. ReadRune(d, runeread, true)
  62. // process(d, false)
  63. continue
  64. }
  65. gobytes := C.GoBytes(unsafe.Pointer(readerbuffer), read)
  66. if DEBUG_INPUT {
  67. log.Printf("Reader << %d, %#v\n", r, gobytes)
  68. }
  69. runebuffer.Write(gobytes)
  70. ReadRune(d, runeread, false)
  71. /*
  72. var input rune
  73. // Is this unicode?
  74. var err error = nil
  75. for err == nil {
  76. //RuneRead:
  77. input, _, err = runeread.ReadRune()
  78. if err == io.EOF {
  79. break // continue
  80. }
  81. if err != nil {
  82. if DEBUG_INPUT {
  83. log.Printf("ReadRune: %#v\n", err)
  84. }
  85. // errors EOF
  86. break // continue // break for loop
  87. }
  88. if input == unicode.ReplacementChar {
  89. runeread.UnreadRune()
  90. b, _ := runeread.ReadByte()
  91. if DEBUG_INPUT {
  92. log.Printf("Reader (byte) >> %x\n", b)
  93. }
  94. readerBuffer = append(readerBuffer, rune(b))
  95. // d.readerChannel <- rune(b)
  96. } else {
  97. if DEBUG_INPUT {
  98. log.Printf("Reader >> %x\n", input)
  99. }
  100. readerBuffer = append(readerBuffer, input)
  101. // d.readerChannel <- input
  102. }
  103. // process(d, true)
  104. } // goto RuneRead
  105. process(d, true)
  106. */
  107. }
  108. }