diff --git a/internal/go-console/getch/ctrlc.go b/internal/go-console/getch/ctrlc.go deleted file mode 100644 index 8e47538..0000000 --- a/internal/go-console/getch/ctrlc.go +++ /dev/null @@ -1,35 +0,0 @@ -package getch - -import ( - "os" - "os/signal" -) - -func (h *Handle) ctrlCHandler(ch chan os.Signal) { - for _ = range ch { - event1 := Event{Key: &keyEvent{3, 0, LEFT_CTRL_PRESSED}} - if h.eventBuffer == nil { - h.eventBuffer = []Event{event1} - h.eventBufferRead = 0 - } else { - h.eventBuffer = append(h.eventBuffer, event1) - } - } -} - -func (h *Handle) IsCtrlCPressed() bool { - if h.eventBuffer != nil { - for _, p := range h.eventBuffer[h.eventBufferRead:] { - if p.Key != nil && p.Key.Rune == rune(3) { - return true - } - } - } - return false -} - -func (h *Handle) DisableCtrlC() { - ch := make(chan os.Signal, 1) - signal.Notify(ch, os.Interrupt) - go h.ctrlCHandler(ch) -} diff --git a/internal/go-console/getch/example4/example4.go b/internal/go-console/getch/example4/example4.go deleted file mode 100644 index 6355498..0000000 --- a/internal/go-console/getch/example4/example4.go +++ /dev/null @@ -1,48 +0,0 @@ -package main - -import ( - "fmt" - "time" - - "github.com/hymkor/expect/internal/go-console/getch" -) - -func main() { - time.Sleep(time.Second / 10) - getch.Flush() - - // wait keyboard-event (timeout: 10-seconds) - ok, err := getch.Wait(10000) - if err != nil { - fmt.Println(err.Error()) - return - } - if !ok { - fmt.Println("Time-out") - return - } - - // get console event - e := getch.All() - if k := e.Key; k != nil { - fmt.Printf("key down: code=%04X scan=%04X shift=%04X\n", - k.Rune, k.Scan, k.Shift) - } - if k := e.KeyUp; k != nil { - fmt.Printf("key up: code=%04X scan=%04X shift=%04X\n", - k.Rune, k.Scan, k.Shift) - } - if r := e.Resize; r != nil { - fmt.Printf("window resize: width=%d height=%d\n", - r.Width, r.Height) - } - if e.Mouse != nil { - fmt.Println("mouse event") - } - if e.Menu != nil { - fmt.Println("menu event") - } - if e.Focus != nil { - fmt.Println("focus event") - } -} diff --git a/internal/go-console/getch/example5/main.go b/internal/go-console/getch/example5/main.go deleted file mode 100644 index 9cb7bfa..0000000 --- a/internal/go-console/getch/example5/main.go +++ /dev/null @@ -1,28 +0,0 @@ -package main - -import ( - "fmt" - - "github.com/hymkor/expect/internal/go-console/getch" -) - -func main() { - getch.Flush() - - // get console event - fmt.Println("Type ESC-Key to program shutdown.") - for { - e := getch.All() - if k := e.Key; k != nil { - fmt.Printf("key down: code=%04X scan=%04X shift=%04X\n", - k.Rune, k.Scan, k.Shift) - } - if k := e.KeyUp; k != nil { - fmt.Printf("key up: code=%04X scan=%04X shift=%04X\n", - k.Rune, k.Scan, k.Shift) - if k.Rune == 0x1B { - break - } - } - } -} diff --git a/internal/go-console/getch/getch.go b/internal/go-console/getch/getch.go deleted file mode 100644 index 0aa5274..0000000 --- a/internal/go-console/getch/getch.go +++ /dev/null @@ -1,259 +0,0 @@ -package getch - -import ( - "errors" - "fmt" - "strings" - "unicode/utf16" - - "github.com/hymkor/expect/internal/go-console/input" -) - -const ( - RIGHT_ALT_PRESSED = 1 - LEFT_ALT_PRESSED = 2 - RIGHT_CTRL_PRESSED = 4 - LEFT_CTRL_PRESSED = 8 - CTRL_PRESSED = RIGHT_CTRL_PRESSED | LEFT_CTRL_PRESSED - ALT_PRESSED = RIGHT_ALT_PRESSED | LEFT_ALT_PRESSED -) - -type keyEvent struct { - Rune rune - Scan uint16 - Shift uint32 -} - -func (k keyEvent) String() string { - return fmt.Sprintf("Rune:%v,Scan=%d,Shift=%d", k.Rune, k.Scan, k.Shift) -} - -type resizeEvent struct { - Width uint - Height uint -} - -func (r resizeEvent) String() string { - return fmt.Sprintf("Width:%d,Height:%d", r.Width, r.Height) -} - -const ( // Button - FROM_LEFT_1ST_BUTTON_PRESSED = 0x0001 - FROM_LEFT_2ND_BUTTON_PRESSED = 0x0004 - FROM_LEFT_3RD_BUTTON_PRESSED = 0x0008 - FROM_LEFT_4TH_BUTTON_PRESSED = 0x0010 - RIGHTMOST_BUTTON_PRESSED = 0x0002 -) - -type Event struct { - Focus *struct{} // MS says it should be ignored - Key *keyEvent // == KeyDown - KeyDown *keyEvent - KeyUp *keyEvent - Menu *struct{} // MS says it should be ignored - Mouse *consoleinput.MouseEventRecord // not supported,yet - Resize *resizeEvent -} - -func (e Event) String() string { - event := make([]string, 0, 7) - if e.Focus != nil { - event = append(event, "Focus") - } - if e.KeyDown != nil { - event = append(event, "KeyDown("+e.KeyDown.String()+")") - } - if e.KeyUp != nil { - event = append(event, "KeyUp("+e.KeyUp.String()+")") - } - if e.Menu != nil { - event = append(event, "Menu") - } - if e.Mouse != nil { - event = append(event, "Mouse") - } - if e.Resize != nil { - event = append(event, "Resize("+e.Resize.String()+")") - } - if len(event) > 0 { - return strings.Join(event, ",") - } else { - return "no events" - } -} - -type Handle struct { - consoleinput.Handle - lastkey *keyEvent - eventBuffer []Event - eventBufferRead int -} - -func New() *Handle { - return &Handle{Handle: consoleinput.New()} -} - -func (h *Handle) Close() { - h.Handle.Close() -} - -func (h *Handle) readEvents(flag uint32) []Event { - orgConMode := h.GetConsoleMode() - h.SetConsoleMode(flag) - defer h.SetConsoleMode(orgConMode) - - result := make([]Event, 0, 2) - - for len(result) <= 0 { - var events [10]consoleinput.InputRecord - numberOfEventsRead := h.Read(events[:]) - - for i := uint32(0); i < numberOfEventsRead; i++ { - e := events[i] - var r Event - switch e.EventType { - case consoleinput.FOCUS_EVENT: - r = Event{Focus: &struct{}{}} - case consoleinput.KEY_EVENT: - p := e.KeyEvent() - k := &keyEvent{ - Rune: rune(p.UnicodeChar), - Scan: p.VirtualKeyCode, - Shift: p.ControlKeyState, - } - if p.KeyDown != 0 { - r = Event{Key: k, KeyDown: k} - } else { - r = Event{KeyUp: k} - } - case consoleinput.MENU_EVENT: - r = Event{Menu: &struct{}{}} - case consoleinput.MOUSE_EVENT: - p := e.MouseEvent() - r = Event{ - Mouse: &consoleinput.MouseEventRecord{ - X: p.X, - Y: p.Y, - Button: p.Button, - ControlKey: p.ControlKey, - Event: p.Event, - }, - } - case consoleinput.WINDOW_BUFFER_SIZE_EVENT: - width, height := e.ResizeEvent() - r = Event{ - Resize: &resizeEvent{ - Width: uint(width), - Height: uint(height), - }, - } - default: - continue - } - result = append(result, r) - } - } - return result -} - -func (h *Handle) bufReadEvent(flag uint32) Event { - for h.eventBuffer == nil || h.eventBufferRead >= len(h.eventBuffer) { - h.eventBuffer = h.readEvents(flag) - h.eventBufferRead = 0 - } - h.eventBufferRead++ - return h.eventBuffer[h.eventBufferRead-1] -} - -// Get a event with concatinating a surrogate-pair of keyevents. -func (h *Handle) getEvent(flag uint32) Event { - for { - event1 := h.bufReadEvent(flag) - if k := event1.Key; k != nil { - println(k.Rune) - if h.lastkey != nil { - k.Rune = utf16.DecodeRune(h.lastkey.Rune, k.Rune) - h.lastkey = nil - } else if utf16.IsSurrogate(k.Rune) { - h.lastkey = k - continue - } - } - return event1 - } -} - -const ALL_EVENTS = consoleinput.ENABLE_WINDOW_INPUT | consoleinput.ENABLE_MOUSE_INPUT - -// Get all console-event (keyboard,resize,...) -func (h *Handle) All() Event { - return h.getEvent(ALL_EVENTS) -} - -const IGNORE_RESIZE_EVENT uint32 = 0 - -// Get character as a Rune -func (h *Handle) Rune() rune { - for { - e := h.getEvent(IGNORE_RESIZE_EVENT) - if e.Key != nil && e.Key.Rune != 0 { - return e.Key.Rune - } - } -} - -func (h *Handle) Flush() error { - org := h.GetConsoleMode() - h.SetConsoleMode(ALL_EVENTS) - defer h.SetConsoleMode(org) - - h.eventBuffer = nil - return h.FlushConsoleInputBuffer() -} - -// wait for keyboard event -func (h *Handle) Wait(timeout_msec uintptr) (bool, error) { - status, err := h.WaitForSingleObject(timeout_msec) - switch status { - case consoleinput.WAIT_OBJECT_0: - return true, nil - case consoleinput.WAIT_TIMEOUT: - return false, nil - case consoleinput.WAIT_ABANDONED: - return false, errors.New("WAIT_ABANDONED") - default: // including WAIT_FAILED: - if err != nil { - return false, err - } else { - return false, errors.New("WAIT_FAILED") - } - } -} - -func (h *Handle) Within(msec uintptr) (Event, error) { - orgConMode := h.GetConsoleMode() - h.SetConsoleMode(ALL_EVENTS) - defer h.SetConsoleMode(orgConMode) - - if ok, err := h.Wait(msec); err != nil || !ok { - return Event{}, err - } - return h.All(), nil -} - -const NUL = '\000' - -func (h *Handle) RuneWithin(msec uintptr) (rune, error) { - orgConMode := h.GetConsoleMode() - h.SetConsoleMode(IGNORE_RESIZE_EVENT) - defer h.SetConsoleMode(orgConMode) - - if ok, err := h.Wait(msec); err != nil || !ok { - return NUL, err - } - e := h.getEvent(IGNORE_RESIZE_EVENT) - if e.Key != nil { - return e.Key.Rune, nil - } - return NUL, nil -} diff --git a/internal/go-console/getch/getch_test.go b/internal/go-console/getch/getch_test.go deleted file mode 100644 index 866cd29..0000000 --- a/internal/go-console/getch/getch_test.go +++ /dev/null @@ -1,85 +0,0 @@ -package getch - -import ( - "fmt" - "testing" - "time" -) - -func reportEvent(e Event) { - if k := e.Key; k != nil { - fmt.Printf("key down: code=%04X scan=%04X shift=%04X\n", - k.Rune, k.Scan, k.Shift) - } - if k := e.KeyUp; k != nil { - fmt.Printf("key up: code=%04X scan=%04X shift=%04X\n", - k.Rune, k.Scan, k.Shift) - } - if r := e.Resize; r != nil { - fmt.Printf("window resize: width=%d height=%d\n", - r.Width, r.Height) - } - if e.Mouse != nil { - fmt.Println("mouse event") - } - if e.Menu != nil { - fmt.Println("menu event") - } - if e.Focus != nil { - fmt.Println("focus event") - } -} - -func TestAll(t *testing.T) { - if err := Flush(); err != nil { - t.Error(err.Error()) - return - } - for i := 0; i < 3; i++ { - fmt.Printf("[%d/3] ", i+1) - reportEvent(All()) - } -} - -func TestCount(t *testing.T) { - var err error - if err = Flush(); err != nil { - t.Error(err.Error()) - return - } - var n int - for { - n, err = Count() - if err != nil { - t.Error(err.Error()) - return - } - if n > 0 { - break - } - fmt.Println("sleep") - time.Sleep(time.Second) - } - fmt.Printf("break(n=%d)\n", n) - reportEvent(All()) -} - -func TestWait(t *testing.T) { - var err error - if err = Flush(); err != nil { - t.Error(err.Error()) - return - } - for { - hit, hit_err := Wait(1000) - if hit_err != nil { - t.Error(hit_err.Error()) - return - } - if hit { - break - } - fmt.Println("Nothing") - } - reportEvent(All()) -} diff --git a/internal/go-console/getch/global.go b/internal/go-console/getch/global.go deleted file mode 100644 index 1e84c11..0000000 --- a/internal/go-console/getch/global.go +++ /dev/null @@ -1,58 +0,0 @@ -package getch - -var hconin *Handle - -func lazyinit() { - if hconin != nil { - return - } - hconin = New() -} - -// Get all console-event (keyboard,resize,...) -func All() Event { - lazyinit() - return hconin.All() -} - -// Get character as a Rune -func Rune() rune { - lazyinit() - return hconin.Rune() -} - -func Count() (int, error) { - lazyinit() - return hconin.GetNumberOfEvent() -} - -func Flush() error { - lazyinit() - return hconin.Flush() -} - -// wait for keyboard event -func Wait(timeout_msec uintptr) (bool, error) { - lazyinit() - return hconin.Wait(timeout_msec) -} - -func Within(msec uintptr) (Event, error) { - lazyinit() - return hconin.Within(msec) -} - -func RuneWithin(msec uintptr) (rune, error) { - lazyinit() - return hconin.RuneWithin(msec) -} - -func IsCtrlCPressed() bool { - lazyinit() - return hconin.IsCtrlCPressed() -} - -func DisableCtrlC() { - lazyinit() - hconin.DisableCtrlC() -} diff --git a/internal/go-console/getch/test_IsCtrlCPressed/example3.go b/internal/go-console/getch/test_IsCtrlCPressed/example3.go deleted file mode 100644 index 6186367..0000000 --- a/internal/go-console/getch/test_IsCtrlCPressed/example3.go +++ /dev/null @@ -1,20 +0,0 @@ -package main - -import ( - "fmt" - "time" - - "github.com/hymkor/expect/internal/go-console/getch" -) - -func main() { - getch.DisableCtrlC() - - for i := 5; i >= 0; i-- { - fmt.Printf("%d\n", i) - time.Sleep(time.Second) - } - if getch.IsCtrlCPressed() { - fmt.Println("^C") - } -} diff --git a/internal/go-console/getch/test_all/example2.go b/internal/go-console/getch/test_all/example2.go deleted file mode 100644 index 75ded72..0000000 --- a/internal/go-console/getch/test_all/example2.go +++ /dev/null @@ -1,18 +0,0 @@ -package main - -import ( - "fmt" - - "github.com/hymkor/expect/internal/go-console/getch" -) - -const COUNT = 5 - -func main() { - getch.Flush() - for i := 0; i < COUNT; i++ { - fmt.Printf("[%d/%d] ", i+1, COUNT) - e := getch.All() - fmt.Println(e.String()) - } -} diff --git a/internal/go-console/getch/test_rune/main.go b/internal/go-console/getch/test_rune/main.go deleted file mode 100644 index 44b4e83..0000000 --- a/internal/go-console/getch/test_rune/main.go +++ /dev/null @@ -1,16 +0,0 @@ -package main - -import ( - "fmt" - "time" - - "github.com/hymkor/expect/internal/go-console/getch" -) - -func main() { - time.Sleep(time.Second / 10) - getch.Flush() - - ch := getch.Rune() - fmt.Printf("%08X\n", ch) -} diff --git a/internal/go-console/getch/test_runewithin/test_runewithin.go b/internal/go-console/getch/test_runewithin/test_runewithin.go deleted file mode 100644 index 68633b5..0000000 --- a/internal/go-console/getch/test_runewithin/test_runewithin.go +++ /dev/null @@ -1,18 +0,0 @@ -package main - -import ( - "fmt" - - "github.com/hymkor/expect/internal/go-console/getch" -) - -func main() { - for i := 0; i < 5; i++ { - r, err := getch.RuneWithin(1000) - if err != nil { - fmt.Println(err.Error()) - } else { - fmt.Printf("typed = '%v'\n", r) - } - } -} diff --git a/internal/go-console/getch/test_within/test_within.go b/internal/go-console/getch/test_within/test_within.go deleted file mode 100644 index 7fe2fd5..0000000 --- a/internal/go-console/getch/test_within/test_within.go +++ /dev/null @@ -1,18 +0,0 @@ -package main - -import ( - "fmt" - - "github.com/hymkor/expect/internal/go-console/getch" -) - -func main() { - for i := 0; i < 5; i++ { - e, err := getch.Within(1000) - if err != nil { - fmt.Println(err.Error()) - } else { - fmt.Println(e.String()) - } - } -}