Fix button timing and event decoding and avoid redundant menu refreshes
This commit is contained in:
8 files changed
+276
-44
No files matched your search
@@ -1,13 +1,11 @@
|
||||
package input
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
@@ -168,32 +166,16 @@ func linuxPheripherals() (ps []*linuxPheripheral, err error) {
|
||||
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
|
||||
defer f.Close()
|
||||
for {
|
||||
if err := binary.Read(f, binary.LittleEndian, &raw); err != nil {
|
||||
_ = f.Close()
|
||||
raw, err := readLinuxInputEvent(f, strconv.IntSize/8)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := cb(raw); err != nil {
|
||||
@@ -232,24 +214,28 @@ func (s *linuxButton) readEvents(events chan<- hardware.InputEvent) {
|
||||
WithField("input", "button").
|
||||
WithField("path", s.p.path)
|
||||
|
||||
var last time.Time
|
||||
var button buttonState
|
||||
err := readLinuxInputEvents(s.p.path, func(ev linuxInputEvent) error {
|
||||
if ev.Code == 0 {
|
||||
return nil // Ignore
|
||||
if ev.Type != 1 || ev.Code == 0 { // EV_KEY only; ignore synchronization events.
|
||||
return nil
|
||||
}
|
||||
|
||||
log.WithField("data", ev).Trace("button event")
|
||||
|
||||
var event hardware.InputEvent
|
||||
switch ev.Value {
|
||||
case 0: // Released
|
||||
if ev.Time().Sub(last) > LongPressDuration {
|
||||
case 1: // Pressed; repeated presses must not restart the timer.
|
||||
button.press(ev.Time())
|
||||
return nil
|
||||
case 0: // Released.
|
||||
duration, ok := button.release(ev.Time())
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
if duration >= LongPressDuration {
|
||||
event = hardware.RotaryLongPressEvent{Time: ev.Time()}
|
||||
} else {
|
||||
event = hardware.RotaryPressEvent{Time: ev.Time()}
|
||||
}
|
||||
case 1: // Pressed
|
||||
last = ev.Time()
|
||||
default: // Ignore key autorepeat and unknown values.
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -297,7 +283,7 @@ func (s *linuxRotary) readEvents(events chan<- hardware.InputEvent) {
|
||||
WithField("path", s.p.path)
|
||||
|
||||
err := readLinuxInputEvents(s.p.path, func(ev linuxInputEvent) error {
|
||||
if ev.Value == 0 {
|
||||
if ev.Type != 2 || ev.Value == 0 { // EV_REL only.
|
||||
return nil // Ignore
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user