summaryrefslogtreecommitdiff
path: root/libs/input/Input.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'libs/input/Input.cpp')
-rw-r--r--libs/input/Input.cpp83
1 files changed, 29 insertions, 54 deletions
diff --git a/libs/input/Input.cpp b/libs/input/Input.cpp
index 037849eac4..30e5d5b0bc 100644
--- a/libs/input/Input.cpp
+++ b/libs/input/Input.cpp
@@ -76,36 +76,13 @@ float transformAngle(const ui::Transform& transform, float angleRadians) {
return result;
}
-// Rotates the given point to the specified orientation. If the display width and height are
-// provided, the point is rotated in the screen space. Otherwise, the point is rotated about the
-// origin. This helper is used to avoid the extra overhead of creating new Transforms.
-vec2 rotatePoint(uint32_t orientation, float x, float y, int32_t displayWidth = 0,
- int32_t displayHeight = 0) {
- if (orientation == ui::Transform::ROT_0) {
- return {x, y};
- }
-
- vec2 xy(x, y);
- if (orientation == ui::Transform::ROT_90) {
- xy.x = displayHeight - y;
- xy.y = x;
- } else if (orientation == ui::Transform::ROT_180) {
- xy.x = displayWidth - x;
- xy.y = displayHeight - y;
- } else if (orientation == ui::Transform::ROT_270) {
- xy.x = y;
- xy.y = displayWidth - x;
- }
- return xy;
-}
-
-vec2 applyTransformWithoutTranslation(const ui::Transform& transform, float x, float y) {
+vec2 transformWithoutTranslation(const ui::Transform& transform, float x, float y) {
const vec2 transformedXy = transform.transform(x, y);
const vec2 transformedOrigin = transform.transform(0, 0);
return transformedXy - transformedOrigin;
}
-bool shouldDisregardWindowTranslation(uint32_t source) {
+bool shouldDisregardTranslation(uint32_t source) {
// Pointer events are the only type of events that refer to absolute coordinates on the display,
// so we should apply the entire window transform. For other types of events, we should make
// sure to not apply the window translation/offset.
@@ -431,8 +408,7 @@ void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int3
int32_t buttonState, MotionClassification classification,
const ui::Transform& transform, float xPrecision, float yPrecision,
float rawXCursorPosition, float rawYCursorPosition,
- uint32_t displayOrientation, int32_t displayWidth,
- int32_t displayHeight, nsecs_t downTime, nsecs_t eventTime,
+ const ui::Transform& rawTransform, nsecs_t downTime, nsecs_t eventTime,
size_t pointerCount, const PointerProperties* pointerProperties,
const PointerCoords* pointerCoords) {
InputEvent::initialize(id, deviceId, source, displayId, hmac);
@@ -448,9 +424,7 @@ void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int3
mYPrecision = yPrecision;
mRawXCursorPosition = rawXCursorPosition;
mRawYCursorPosition = rawYCursorPosition;
- mDisplayOrientation = displayOrientation;
- mDisplayWidth = displayWidth;
- mDisplayHeight = displayHeight;
+ mRawTransform = rawTransform;
mDownTime = downTime;
mPointerProperties.clear();
mPointerProperties.appendArray(pointerProperties, pointerCount);
@@ -474,9 +448,7 @@ void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
mYPrecision = other->mYPrecision;
mRawXCursorPosition = other->mRawXCursorPosition;
mRawYCursorPosition = other->mRawYCursorPosition;
- mDisplayOrientation = other->mDisplayOrientation;
- mDisplayWidth = other->mDisplayWidth;
- mDisplayHeight = other->mDisplayHeight;
+ mRawTransform = other->mRawTransform;
mDownTime = other->mDownTime;
mPointerProperties = other->mPointerProperties;
@@ -542,20 +514,19 @@ float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
if (!isPerWindowInputRotationEnabled()) return coords->getAxisValue(axis);
if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
- // For compatibility, convert raw coordinates into "oriented screen space". Once app
- // developers are educated about getRaw, we can consider removing this.
- const vec2 xy = shouldDisregardWindowTranslation(mSource)
- ? rotatePoint(mDisplayOrientation, coords->getX(), coords->getY())
- : rotatePoint(mDisplayOrientation, coords->getX(), coords->getY(), mDisplayWidth,
- mDisplayHeight);
+ // For compatibility, convert raw coordinates into logical display space.
+ const vec2 xy = shouldDisregardTranslation(mSource)
+ ? transformWithoutTranslation(mRawTransform, coords->getX(), coords->getY())
+ : mRawTransform.transform(coords->getX(), coords->getY());
static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
return xy[axis];
}
if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) {
- // For compatibility, since we convert raw coordinates into "oriented screen space", we
+ // For compatibility, since we report raw coordinates in logical display space, we
// need to convert the relative axes into the same orientation for consistency.
- const vec2 relativeXy = rotatePoint(mDisplayOrientation,
+ const vec2 relativeXy =
+ transformWithoutTranslation(mRawTransform,
coords->getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
coords->getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y;
@@ -569,8 +540,8 @@ float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
const PointerCoords* coords = getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
- const vec2 xy = shouldDisregardWindowTranslation(mSource)
- ? applyTransformWithoutTranslation(mTransform, coords->getX(), coords->getY())
+ const vec2 xy = shouldDisregardTranslation(mSource)
+ ? transformWithoutTranslation(mTransform, coords->getX(), coords->getY())
: mTransform.transform(coords->getXYValue());
static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
return xy[axis];
@@ -578,11 +549,9 @@ float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) {
const vec2 relativeXy =
- applyTransformWithoutTranslation(mTransform,
- coords->getAxisValue(
- AMOTION_EVENT_AXIS_RELATIVE_X),
- coords->getAxisValue(
- AMOTION_EVENT_AXIS_RELATIVE_Y));
+ transformWithoutTranslation(mTransform,
+ coords->getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
+ coords->getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y;
}
@@ -607,6 +576,8 @@ void MotionEvent::offsetLocation(float xOffset, float yOffset) {
void MotionEvent::scale(float globalScaleFactor) {
mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor);
+ mRawTransform.set(mRawTransform.tx() * globalScaleFactor,
+ mRawTransform.ty() * globalScaleFactor);
mXPrecision *= globalScaleFactor;
mYPrecision *= globalScaleFactor;
@@ -708,9 +679,11 @@ status_t MotionEvent::readFromParcel(Parcel* parcel) {
mYPrecision = parcel->readFloat();
mRawXCursorPosition = parcel->readFloat();
mRawYCursorPosition = parcel->readFloat();
- mDisplayOrientation = parcel->readUint32();
- mDisplayWidth = parcel->readInt32();
- mDisplayHeight = parcel->readInt32();
+
+ result = android::readFromParcel(mRawTransform, *parcel);
+ if (result != OK) {
+ return result;
+ }
mDownTime = parcel->readInt64();
mPointerProperties.clear();
@@ -770,9 +743,11 @@ status_t MotionEvent::writeToParcel(Parcel* parcel) const {
parcel->writeFloat(mYPrecision);
parcel->writeFloat(mRawXCursorPosition);
parcel->writeFloat(mRawYCursorPosition);
- parcel->writeUint32(mDisplayOrientation);
- parcel->writeInt32(mDisplayWidth);
- parcel->writeInt32(mDisplayHeight);
+
+ result = android::writeToParcel(mRawTransform, *parcel);
+ if (result != OK) {
+ return result;
+ }
parcel->writeInt64(mDownTime);
for (size_t i = 0; i < pointerCount; i++) {