add getEventSlices

This commit is contained in:
2022-07-19 21:00:13 -07:00
parent a0a4684c95
commit c0c37a203b
2 changed files with 88 additions and 8 deletions

View File

@@ -131,7 +131,7 @@ func (d Device) CaptureSequence(ctx context.Context) (t TapSequence, err error)
return TapSequence{}, nil return TapSequence{}, nil
} }
type Event struct { type event struct {
TimeStamp time.Time TimeStamp time.Time
DevicePath string DevicePath string
Type string Type string
@@ -139,20 +139,75 @@ type Event struct {
Value string Value string
} }
func (e event) isBTNTouch() bool {
return e.Key == "BTN_TOUCH"
}
func (e event) isEvABS() bool {
return e.Type == "EV_ABS"
}
func (e event) isBTNUp() bool {
return e.isBTNTouch() && e.Value == "UP"
}
func (e event) isBTNDown() bool {
return e.isBTNTouch() && e.Value == "DOWN"
}
func (e event) GetNumeric() (int, error) {
i, err := strconv.ParseInt(e.Value, 16, 64)
if err != nil {
return 0, err
}
return int(i), nil
}
func parseGetEvent(input string) (events []Input) { func parseGetEvent(input string) (events []Input) {
lines := strings.Split(input, "\n") lines := strings.Split(input, "\n")
lines = trimDeviceDescriptors(lines) lines = trimDeviceDescriptors(lines)
touchEvents := parseInputToEvent(lines)
touches := getEventSlices(touchEvents)
events = touchesToInputs(touches)
// Trim off the beginning with device descriptors // Trim off the beginning with device descriptors
return return
} }
func parseInputToEvent(input []string) []Event { func touchesToInputs([][]event) []Input {
var e []Event return []Input{}
r := regexp.MustCompile(`\[\W*(\d+\.\d+)]`) }
func getEventSlices(events []event) [][]event {
eventSets := [][]event{{}}
current := 0
foundDown := false
for _, e := range events {
if !foundDown {
if e.isBTNDown() {
foundDown = true
} else {
continue
}
}
eventSets[current] = append(eventSets[current], e)
if e.isBTNUp() {
current++
foundDown = false
eventSets = append(eventSets, []event{})
}
}
eventSets = eventSets[:len(eventSets)-1]
// fmt.Println(eventSets[len(eventSets)-1])
return eventSets
}
func parseInputToEvent(input []string) []event {
var e []event
r := regexp.MustCompile(`\[\s*(\d+\.\d+)]\s*(.*):\s*(\w*)\s*(\w*)\s*(\w*)`)
for _, line := range input { for _, line := range input {
var l Event var l event
timeStr := r.FindStringSubmatch(line) timeStr := r.FindStringSubmatch(line)
if len(timeStr) != 2 { if len(timeStr) != 6 {
continue continue
} }
f, err := strconv.ParseFloat(timeStr[1], 32) f, err := strconv.ParseFloat(timeStr[1], 32)
@@ -161,9 +216,12 @@ func parseInputToEvent(input []string) []Event {
} }
msec := int64(f * 1000) msec := int64(f * 1000)
l.TimeStamp = time.UnixMilli(msec) l.TimeStamp = time.UnixMilli(msec)
l.DevicePath = timeStr[2]
l.Type = timeStr[3]
l.Key = timeStr[4]
l.Value = timeStr[5]
e = append(e, l) e = append(e, l)
} }
// fmt.Println(e[0].TimeStamp.Sub(e[len(e)-1].TimeStamp))
return e return e
} }
@@ -179,5 +237,5 @@ func trimDeviceDescriptors(input []string) []string {
for i := range input { for i := range input {
input[i] = strings.TrimSpace(input[i]) input[i] = strings.TrimSpace(input[i])
} }
return input[start:] return input[start : len(input)-1]
} }

View File

@@ -40,6 +40,28 @@ func TestParseInputToEvent(t *testing.T) {
} }
} }
func TestGetEventSlices(t *testing.T) {
testCases := []struct {
name string
testFile string
touchSetCount int
}{
{"pixel", pixel, 7},
{"tablet", tablet, 8},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
lines := strings.Split(tc.testFile, "\n")
lines = trimDeviceDescriptors(lines)
touchEvents := parseInputToEvent(lines)
touches := getEventSlices(touchEvents)
if len(touches) != tc.touchSetCount {
t.Errorf("Expected %d touches but found %d", tc.touchSetCount, len(touches))
}
})
}
}
func TestTrimDeviceDescriptors(t *testing.T) { func TestTrimDeviceDescriptors(t *testing.T) {
testCases := []struct { testCases := []struct {
name string name string