package input import ( "encoding/binary" "errors" "fmt" "os" "path/filepath" "strings" "time" "github.com/sirupsen/logrus" "github.com/BeatGlow/beatglow/hardware" ) const ( devfsPath = "/dev" procfsPath = "/proc" procInputDevices = procfsPath + "/bus/input/devices" ) type devInputEncoder struct { path string button *linuxButton rotary *linuxRotary } func newDevInputEncoder(click string) (enc Encoder, err error) { var ps []*linuxPheripheral if ps, err = linuxPheripherals(); err != nil { return } var ( rotary *linuxRotary button *linuxButton ) for _, p := range ps { switch { case strings.HasPrefix(p.name, "rotary@"): rotary = &linuxRotary{p: p} case strings.HasPrefix(p.name, "button@") && (click == "" || strings.HasSuffix(p.name, "@"+click)): button = &linuxButton{p: p} } } if rotary == nil { return nil, errors.New("no rotary encoder found, is the rotary_encoder dtoveraly loaded?") } if button == nil { return nil, errors.New("no button found, is the gpio-key dtoverlay loaded?") } return &devInputEncoder{ button: button, rotary: rotary, }, nil } func (e devInputEncoder) Halt() error { return nil } func (e devInputEncoder) String() string { return "input:" + e.path } func (e devInputEncoder) Events() (<-chan hardware.InputEvent, error) { events := make(chan hardware.InputEvent, 8) go e.rotary.readEvents(events) go e.button.readEvents(events) return events, 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(procInputDevices) 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) String() string { return fmt.Sprintf("time=%s type=%#04x code=%#04x value=%d", e.Time(), e.Type, e.Code, e.Value) } 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 hardware.InputEvent, error) { log := logrus. WithField("input", "button"). WithField("path", s.p.path) log.Debug("opening linux button source") events := make(chan hardware.InputEvent) go s.readEvents(events) return events, nil } func (s *linuxButton) readEvents(events chan<- hardware.InputEvent) { defer close(events) log := logrus. WithField("input", "button"). WithField("path", s.p.path) var last time.Time err := readLinuxInputEvents(s.p.path, func(ev linuxInputEvent) error { if ev.Code == 0 { return nil // Ignore } log.WithField("data", ev).Trace("button event") var event hardware.InputEvent switch ev.Value { case 0: // Released if ev.Time().Sub(last) > LongPressDuration { event = hardware.RotaryLongPressEvent{Time: ev.Time()} } else { event = hardware.RotaryPressEvent{Time: ev.Time()} } case 1: // Pressed last = ev.Time() return nil } select { case events <- event: 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 hardware.InputEvent, error) { log := logrus. WithField("input", "rotary"). WithField("path", s.p.path) log.Debug("opening linux rotary source") events := make(chan hardware.InputEvent) go s.readEvents(events) return events, nil } func (s *linuxRotary) readEvents(events chan<- hardware.InputEvent) { defer close(events) log := logrus. WithField("input", "rotary"). WithField("path", s.p.path) err := readLinuxInputEvents(s.p.path, func(ev linuxInputEvent) error { if ev.Value == 0 { return nil // Ignore } log.WithField("data", ev).Trace("rotary event") event := hardware.RotaryEvent{ Time: ev.Time(), Delta: int(ev.Value), } select { case events <- event: default: log.Warn("dropping event") } return nil }) if err != nil { log.WithError(err).Error("failed to read events") } }