123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462 |
- package door
- import (
- "bytes"
- "flag"
- "fmt"
- "log"
- "os"
- "path/filepath"
- "runtime/debug"
- "sync"
- "time"
- "golang.org/x/term"
- )
- const DEBUG_DOOR bool = false
- const DEBUG_INPUT bool = false
- const DEBUG_OUTPUT bool = false
- const SavePos = "\x1b[s"
- const RestorePos = "\x1b[u"
- const CRNL = "\n"
- const Clrscr = "\x1b[0m\x1b[2J\x1b[H"
- const HideCursor = "\x1b[?25l"
- const ShowCursor = "\x1b[?25h"
- const Reset string = "\x1b[0m"
- const CURSOR_POS = "\x1b[6n"
- const SAVE_POS = "\x1b[s"
- const RESTORE_POS = "\x1b[u"
- var Unicode bool
- var CP437 bool
- var Full_CP437 bool
- var Height int
- var Width int
- var Inactivity time.Duration = time.Duration(120) * time.Second
- type ColorRender func(*bytes.Buffer, []byte) []byte
- type Updater func(*bytes.Buffer)
- type UpdaterI interface {
- Update() bool
- }
- type OutputI interface {
- Output() []byte
- }
- type CursorPos struct {
- X, Y int
- }
- type Mouse struct {
- Button int8
- X int8
- Y int8
- }
- type MouseMode int16
- const (
- Off MouseMode = 0
- X10 MouseMode = 9
- Normal MouseMode = 1000
- Button MouseMode = 1002
- AnyEvent MouseMode = 1003
- )
- type ReaderData struct {
- R rune
- Ex Extended
- Err error
- }
- type BaseWriter struct {
- Closed bool
- TranslateNL bool
- TranslateToUnicode bool
- nlBuffer *bytes.Buffer
- uniBuffer *bytes.Buffer
- ansiCSI bool
- ansiCode []byte
- LastSavedColor []int
- }
- type Door struct {
- Config DropfileConfig
- READFD int
- WRITEFD int
- Writer OSWriter
- writeMutex sync.Mutex
- ansiCSI bool
- ansiCode []byte
- Disconnected bool
- TimeOut time.Time
- StartTime time.Time
- Pushback FIFOBuffer[rune]
- LastColor []int
- ReaderClosed bool
- readerChannel chan ReaderData
- readerMutex sync.Mutex
- ReaderEnd bool
- LastMouse []Mouse
- LastCursor []CursorPos
- mcMutex sync.Mutex
- wg sync.WaitGroup
- tio_default *term.State
- Mouse MouseMode
- }
- func (d *Door) EnableMouse(mode MouseMode) {
- if d.Mouse != Off {
-
- d.DisableMouse()
- }
- d.Mouse = mode
- if d.Mouse != Off {
- var output []byte
- output = fmt.Appendf(output, "\x1b[?%dh", int(d.Mouse))
- d.Write(output)
-
- }
- }
- func (d *Door) DisableMouse() {
- if d.Mouse != Off {
- var output []byte
- output = fmt.Appendf(output, "\x1b[?%dl", int(d.Mouse))
- d.Write(output)
-
- }
- d.Mouse = Off
- }
- func (d *Door) AddMouse(mouse Mouse) {
- d.mcMutex.Lock()
- defer d.mcMutex.Unlock()
- d.LastMouse = append(d.LastMouse, mouse)
- }
- var WantGetMouse bool
- func (d *Door) GetMouse() (Mouse, bool) {
- if WantGetMouse {
- WantGetMouse = false
- } else {
- log.Println("GetMouse called, but WantGetMouse not true.")
- }
- d.mcMutex.Lock()
- defer d.mcMutex.Unlock()
- return ArrayDelete(&d.LastMouse, 0)
- }
- func (d *Door) GetCursorPos() (CursorPos, bool) {
- d.mcMutex.Lock()
- if DEBUG_DOOR {
- log.Printf("LastCursor %p/%p %d, %d\n", d, &d.LastCursor, len(d.LastCursor), cap(d.LastCursor))
- }
- defer d.mcMutex.Unlock()
- if DEBUG_DOOR {
- log.Printf("LastCursor: %#v\n", d.LastCursor)
- }
- return ArrayDelete(&d.LastCursor, 0)
- }
- func (d *Door) AddCursorPos(cursor CursorPos) {
- d.mcMutex.Lock()
- if DEBUG_DOOR {
- log.Printf("LastCursor %p/%p %d, %d\n", d, &d.LastCursor, len(d.LastCursor), cap(d.LastCursor))
- }
- defer d.mcMutex.Unlock()
- d.LastCursor = append(d.LastCursor, cursor)
- if DEBUG_DOOR {
- log.Printf("LastCursor now %d, %d\n", len(d.LastCursor), cap(d.LastCursor))
- log.Printf("AddCursor: %#v\n", d.LastCursor)
- }
- }
- func (d *Door) ClearMouseCursor() {
- d.mcMutex.Lock()
- defer d.mcMutex.Unlock()
- if DEBUG_DOOR {
- log.Println("ClearMouseCursor")
- }
- d.LastMouse = make([]Mouse, 0, 2)
- d.LastCursor = make([]CursorPos, 0, 3)
- }
- func (d *Door) TimeLeft() time.Duration {
- return time.Until(d.TimeOut)
- }
- func (d *Door) TimeUsed() time.Duration {
- return time.Since(d.StartTime)
- }
- func (d *Door) Disconnect() bool {
- return d.Disconnected
- }
- func (d *Door) Detect() bool {
-
-
- var detect bytes.Buffer
- detect.WriteString("\r\x03\x04" + CURSOR_POS + "\b \b\b \b" +
- "\r\u2615" + CURSOR_POS + "\b \b\b \b\b \b" +
- SAVE_POS + "\x1b[999C\x1b[999B" + CURSOR_POS + RESTORE_POS)
-
- d.Write(detect.Bytes())
- var info []CursorPos = make([]CursorPos, 0, 3)
- var done bool
- for !done {
- _, ex, err := d.WaitKey(time.Second)
- log.Println("WaitKey:", ex, err)
- if ex == CURSOR {
- cursor, ok := d.GetCursorPos()
- if ok {
- info = append(info, cursor)
- if len(info) == 3 {
- done = true
- }
- }
- }
- if err != nil {
- done = true
- }
- }
- if len(info) != 3 {
-
- log.Println("Detect FAILED:", info)
- return false
- }
-
- var valid bool
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- if info[0].X == 1 && info[1].X == 3 {
- Unicode = true
- valid = true
- } else {
-
- Unicode = false
- CP437 = true
- valid = true
- }
- if info[0].X == 3 {
- Full_CP437 = true
- }
- if !valid {
- log.Println("Detect FAILED (not valid):", info)
- return false
- }
- Width, Height = info[2].X, info[2].Y
-
- d.Write(Goto(1, info[0].Y))
- log.Printf("Unicode: %t, CP437: %t, Full %t, Screen %v\n", Unicode, CP437, Full_CP437, info[2])
- return true
- }
- func (d *Door) Init(doorname string) {
- var dropfile string
- d.Pushback = NewFIFOBuffer[rune](5)
-
- var binaryPath string
- binaryPath, _ = os.Executable()
- binaryPath = filepath.Dir(binaryPath)
- _ = os.Chdir(binaryPath)
- flag.StringVar(&dropfile, "d", "", "Path to dropfile")
- flag.Parse()
- if len(dropfile) == 0 {
- flag.PrintDefaults()
- os.Exit(2)
- }
- d.ReadDropfile(dropfile)
- if d.Config.Comm_type == 0 {
-
- d.tio_default, _ = term.MakeRaw(d.READFD)
- }
-
- var logfilename string = fmt.Sprintf("%s-%d.log", doorname, d.Config.Node)
- var logf *os.File
- var err error
- logf, err = os.OpenFile(logfilename, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
- if err != nil {
- log.Panicf("Error creating log file %s: %v", logfilename, err)
- }
-
- log.SetOutput(logf)
- log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
- log.Printf("Loading dropfile %s\n", dropfile)
- log.Printf("BBS %s, User %s / Handle %s / File %d\n", d.Config.BBSID, d.Config.Real_name, d.Config.Handle, d.Config.Comm_handle)
- d.readerChannel = make(chan ReaderData, 16)
- d.ClearMouseCursor()
- d.setupChannels()
- d.Writer.Init(d)
- d.Detect()
- if Unicode {
- BOXES = BOXES_UNICODE
- BARS = BARS_UNICODE
- } else {
- BOXES = BOXES_CP437
- BARS = BARS_CP437
- }
- }
- func (d *Door) Close() {
- defer func() {
- if err := recover(); err != nil {
- log.Println("door.Close FAILURE:", err)
-
- debug.PrintStack()
- }
- }()
- d.DisableMouse()
-
- d.Writer.Stop()
-
-
-
- d.readerMutex.Lock()
-
- d.ReaderEnd = true
- d.readerMutex.Unlock()
-
-
- log.Println("wg.Wait()")
- d.wg.Wait()
- log.Println("Closed.")
- if d.Config.Comm_type == 0 {
-
- term.Restore(d.READFD, d.tio_default)
- }
- }
- func Goto(x int, y int) []byte {
- var output []byte
- output = fmt.Appendf(output, "\x1b[%d;%dH", y, x)
- return output
- }
- func GotoS(x int, y int) string {
- return fmt.Sprintf("\x1b[%d;%dH", y, x)
- }
- func (d *Door) setupChannels() {
- d.wg.Add(1)
- go Reader(d)
- }
|