Checkpoint
This commit is contained in:
125 files changed
+59376
No files matched your search
@@ -0,0 +1,279 @@
|
||||
package input
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
const (
|
||||
devfsPath = "/dev"
|
||||
sysfsPath = "/sys"
|
||||
)
|
||||
|
||||
func init() {
|
||||
detectors = append(detectors, detectLinux)
|
||||
}
|
||||
|
||||
func detectLinux() ([]Source, error) {
|
||||
log := logrus.WithField("input", "detect")
|
||||
|
||||
ps, err := linuxPheripherals()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
log.Debugf("detected %d peripherals", len(ps))
|
||||
|
||||
ss := make([]Source, 0, len(ps))
|
||||
for _, p := range ps {
|
||||
if i := strings.IndexByte(p.name, '@'); i != -1 {
|
||||
switch p.name[:i] {
|
||||
case "button":
|
||||
log.WithField("path", p.path).Debug("detected button")
|
||||
ss = append(ss, newLinuxButton(p))
|
||||
case "rotary":
|
||||
log.WithField("path", p.path).Debug("detected rotary encoder")
|
||||
ss = append(ss, newLinuxRotary(p))
|
||||
default:
|
||||
log.WithField("path", p.path).Debugf("unsupported %q peripheral", p.name[:i])
|
||||
}
|
||||
}
|
||||
}
|
||||
return ss, nil
|
||||
}
|
||||
|
||||
type linuxPheripheral struct {
|
||||
name string
|
||||
bus string
|
||||
vendor string
|
||||
product string
|
||||
version string
|
||||
phys string
|
||||
path string
|
||||
}
|
||||
|
||||
/*
|
||||
I: Bus=0019 Vendor=0001 Product=0001 Version=0100
|
||||
N: Name="button@10"
|
||||
P: Phys=gpio-keys/input0
|
||||
S: Sysfs=/devices/platform/button@10/input/input0
|
||||
U: Uniq=
|
||||
H: Handlers=kbd event0
|
||||
B: PROP=0
|
||||
B: EV=3
|
||||
B: KEY=20000 0 0 0
|
||||
|
||||
I: Bus=0019 Vendor=0000 Product=0000 Version=0000
|
||||
N: Name="rotary@14"
|
||||
P: Phys=
|
||||
S: Sysfs=/devices/platform/rotary@14/input/input1
|
||||
U: Uniq=
|
||||
H: Handlers=event1
|
||||
B: PROP=0
|
||||
B: EV=5
|
||||
B: REL=1
|
||||
*/
|
||||
func linuxPheripherals() (ps []*linuxPheripheral, err error) {
|
||||
log := logrus.WithField("input", "linux")
|
||||
log.Trace("detecting peripherals")
|
||||
|
||||
b, err := os.ReadFile("/proc/bus/input/devices")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, segment := range strings.Split(string(b), "\n\n") {
|
||||
lines := strings.Split(segment, "\n")
|
||||
perip := new(linuxPheripheral)
|
||||
for _, line := range lines {
|
||||
if line == "" {
|
||||
continue
|
||||
}
|
||||
log.WithField("line", line).Trace("parsing line")
|
||||
switch {
|
||||
case strings.HasPrefix(line, "I: "):
|
||||
for _, kv := range strings.Split(strings.TrimPrefix(line, "I: "), " ") {
|
||||
kv := strings.SplitN(kv, "=", 2)
|
||||
switch kv[0] {
|
||||
case "Bus":
|
||||
case "Vendor":
|
||||
perip.vendor = kv[1]
|
||||
case "Product":
|
||||
perip.product = kv[1]
|
||||
case "Version":
|
||||
perip.version = kv[1]
|
||||
}
|
||||
}
|
||||
case strings.HasPrefix(line, "N: Name="):
|
||||
perip.name = strings.TrimFunc(strings.TrimPrefix(line, "N: Name="), func(r rune) bool {
|
||||
return r == '"' || r == '@'
|
||||
})
|
||||
case strings.HasPrefix(line, "P: Phys="):
|
||||
perip.phys = strings.TrimPrefix(line, "P: Phys=")
|
||||
//case strings.HasPrefix(line, "S: Sysfs="):
|
||||
// perip.sysfs = filepath.Join(sysfsPath, strings.TrimPrefix(line, "S: Sysfs="), "device")
|
||||
case strings.HasPrefix(line, "H: Handlers="):
|
||||
for _, handler := range strings.Split(strings.TrimPrefix(line, "H: Handlers="), " ") {
|
||||
if strings.HasPrefix(handler, "event") {
|
||||
perip.path = filepath.Join(devfsPath, "input", handler)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if perip.path != "" {
|
||||
log.WithFields(logrus.Fields{
|
||||
"name": perip.name,
|
||||
"bus": perip.bus,
|
||||
"vendor": perip.vendor,
|
||||
"product": perip.product,
|
||||
"version": perip.version,
|
||||
"phys": perip.phys,
|
||||
"path": perip.path,
|
||||
}).Trace("found peripheral")
|
||||
ps = append(ps, perip)
|
||||
}
|
||||
}
|
||||
|
||||
return ps, nil
|
||||
}
|
||||
|
||||
type linuxInputEvent struct {
|
||||
Secs uint32
|
||||
USecs uint32
|
||||
Type uint16
|
||||
Code uint16
|
||||
Value int32
|
||||
}
|
||||
|
||||
func (e linuxInputEvent) Time() time.Time {
|
||||
return time.Unix(int64(e.Secs), int64(e.USecs))
|
||||
}
|
||||
|
||||
func readLinuxInputEvents(name string, cb func(linuxInputEvent) error) error {
|
||||
f, err := os.Open(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var raw linuxInputEvent
|
||||
for {
|
||||
if err := binary.Read(f, binary.LittleEndian, &raw); err != nil {
|
||||
_ = f.Close()
|
||||
return err
|
||||
}
|
||||
if err := cb(raw); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type linuxButton struct {
|
||||
p *linuxPheripheral
|
||||
}
|
||||
|
||||
func newLinuxButton(p *linuxPheripheral) *linuxButton {
|
||||
return &linuxButton{p: p}
|
||||
}
|
||||
|
||||
func (s *linuxButton) Name() string {
|
||||
return s.p.name
|
||||
}
|
||||
|
||||
func (s *linuxButton) Events() (<-chan Event, error) {
|
||||
log := logrus.
|
||||
WithField("input", "button").
|
||||
WithField("path", s.p.path)
|
||||
log.Debug("opening linux button source")
|
||||
|
||||
events := make(chan Event)
|
||||
go s.readEvents(events)
|
||||
return events, nil
|
||||
}
|
||||
|
||||
func (s *linuxButton) readEvents(events chan<- Event) {
|
||||
defer close(events)
|
||||
|
||||
log := logrus.
|
||||
WithField("input", "button").
|
||||
WithField("path", s.p.path)
|
||||
|
||||
err := readLinuxInputEvents(s.p.path, func(ev linuxInputEvent) error {
|
||||
log.WithField("data", ev).Debug("button event")
|
||||
|
||||
if ev.Code == 0 {
|
||||
return nil // not sure what this is
|
||||
}
|
||||
|
||||
var pressed bool
|
||||
if ev.Value == 1 {
|
||||
pressed = true
|
||||
}
|
||||
|
||||
select {
|
||||
case events <- NewButtonEvent(s, pressed):
|
||||
default:
|
||||
log.Warn("dropping event")
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
log.WithError(err).Error("failed to read events")
|
||||
}
|
||||
}
|
||||
|
||||
type linuxRotary struct {
|
||||
p *linuxPheripheral
|
||||
}
|
||||
|
||||
func newLinuxRotary(p *linuxPheripheral) *linuxRotary {
|
||||
return &linuxRotary{p: p}
|
||||
}
|
||||
|
||||
func (s *linuxRotary) Name() string {
|
||||
return s.p.name
|
||||
}
|
||||
|
||||
func (s *linuxRotary) Events() (<-chan Event, error) {
|
||||
log := logrus.
|
||||
WithField("input", "rotary").
|
||||
WithField("path", s.p.path)
|
||||
log.Debug("opening linux rotary source")
|
||||
|
||||
events := make(chan Event)
|
||||
go s.readEvents(events)
|
||||
return events, nil
|
||||
}
|
||||
|
||||
func (s *linuxRotary) readEvents(events chan<- Event) {
|
||||
defer close(events)
|
||||
|
||||
log := logrus.
|
||||
WithField("input", "rotary").
|
||||
WithField("path", s.p.path)
|
||||
|
||||
err := readLinuxInputEvents(s.p.path, func(ev linuxInputEvent) error {
|
||||
log.WithField("data", ev).Debug("rotary event")
|
||||
|
||||
select {
|
||||
case events <- NewRotaryEvent(s, int(ev.Value), ev.Value == 0):
|
||||
default:
|
||||
log.Warn("dropping event")
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
log.WithError(err).Error("failed to read events")
|
||||
}
|
||||
}
|
||||
|
||||
// Interface checks
|
||||
var (
|
||||
_ Source = (*linuxButton)(nil)
|
||||
)
|
||||
Reference in new issue
Block a user