split src/devices/input into src/devices/input/{raw,wrap}
This commit is contained in:
57
src/device/input/raw/keyboard.go
Normal file
57
src/device/input/raw/keyboard.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package raw
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
type Keyboard struct {
|
||||
}
|
||||
|
||||
func NewKeyboard() Keyboard {
|
||||
switch runtime.GOOS {
|
||||
case "linux":
|
||||
if err := exec.Command("stty", "-F", "/dev/tty", "cbreak", "min", "1").Run(); err != nil {
|
||||
panic(err)
|
||||
} else if err := exec.Command("stty", "-F", "/dev/tty", "-echo").Run(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
case "darwin":
|
||||
if err := exec.Command("stty", "-f", "/dev/tty", "cbreak", "min", "1").Run(); err != nil {
|
||||
panic(err)
|
||||
} else if err := exec.Command("stty", "-f", "/dev/tty", "-echo").Run(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
default:
|
||||
panic(runtime.GOOS)
|
||||
}
|
||||
return Keyboard{}
|
||||
}
|
||||
|
||||
func (kb Keyboard) Close() {
|
||||
switch runtime.GOOS {
|
||||
case "linux":
|
||||
if err := exec.Command("stty", "-F", "/dev/tty", "echo").Run(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
case "darwin":
|
||||
if err := exec.Command("stty", "-f", "/dev/tty", "echo").Run(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (kb Keyboard) Read() []byte {
|
||||
b := make([]byte, 5)
|
||||
n, err := os.Stdin.Read(b)
|
||||
if err != nil && err != io.EOF {
|
||||
panic(err)
|
||||
}
|
||||
if os.Getenv("DEBUG") == "true" {
|
||||
log.Printf("raw.Keyboard.Read() %s", b[:n])
|
||||
}
|
||||
return b[:n]
|
||||
}
|
||||
Reference in New Issue
Block a user