Skip to content

Commit f8e3bd5

Browse files
gordonklauscrawshaw
authored andcommitted
mobile: Deliver all touch events.
Android delivers events in a batch. Previously, only the first event of the batch was passed to the Go callback, resulting in many events (typically corresponding to non-primary touches) being dropped. Change-Id: I4b01bb6c4d1c88dec6c8fa379875043dfc0e9986 Reviewed-on: https://go-review.googlesource.com/1894 Reviewed-by: David Crawshaw <[email protected]>
1 parent 93ef356 commit f8e3bd5

File tree

1 file changed

+25
-18
lines changed

1 file changed

+25
-18
lines changed

app/loop_android.go

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -143,25 +143,32 @@ func processEvent(cb Callbacks, e *C.AInputEvent) {
143143
if cb.Touch == nil {
144144
return
145145
}
146-
x := C.AMotionEvent_getX(e, 0)
147-
y := C.AMotionEvent_getY(e, 0)
148-
149-
var ty event.TouchType
150-
switch C.AMotionEvent_getAction(e) {
151-
case C.AMOTION_EVENT_ACTION_DOWN:
152-
ty = event.TouchStart
153-
case C.AMOTION_EVENT_ACTION_MOVE:
154-
ty = event.TouchMove
155-
case C.AMOTION_EVENT_ACTION_UP:
156-
ty = event.TouchEnd
146+
147+
// At most one of the events in this batch is an up or down event; get its index and type.
148+
upDownIndex := C.size_t(C.AMotionEvent_getAction(e)&C.AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> C.AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT
149+
upDownTyp := event.TouchMove
150+
switch C.AMotionEvent_getAction(e) & C.AMOTION_EVENT_ACTION_MASK {
151+
case C.AMOTION_EVENT_ACTION_DOWN, C.AMOTION_EVENT_ACTION_POINTER_DOWN:
152+
upDownTyp = event.TouchStart
153+
case C.AMOTION_EVENT_ACTION_UP, C.AMOTION_EVENT_ACTION_POINTER_UP:
154+
upDownTyp = event.TouchEnd
155+
}
156+
157+
for i, n := C.size_t(0), C.AMotionEvent_getPointerCount(e); i < n; i++ {
158+
typ := event.TouchMove
159+
if i == upDownIndex {
160+
typ = upDownTyp
161+
}
162+
x := C.AMotionEvent_getX(e, i)
163+
y := C.AMotionEvent_getY(e, i)
164+
cb.Touch(event.Touch{
165+
Type: typ,
166+
Loc: geom.Point{
167+
X: geom.Pt(float32(x) / geom.PixelsPerPt),
168+
Y: geom.Pt(float32(y) / geom.PixelsPerPt),
169+
},
170+
})
157171
}
158-
cb.Touch(event.Touch{
159-
Type: ty,
160-
Loc: geom.Point{
161-
X: geom.Pt(float32(x) / geom.PixelsPerPt),
162-
Y: geom.Pt(float32(y) / geom.PixelsPerPt),
163-
},
164-
})
165172
default:
166173
log.Printf("unknown input event, type=%d", C.AInputEvent_getType(e))
167174
}

0 commit comments

Comments
 (0)