Return values in PointerController's getters
Rather than passing pointers in to get output from the functions, return values from getPosition() and getBounds(). Bug: 245989146 Bug: 21566609 Test: Build, presubmit Change-Id: Ie5b44a433c94b6c455486922f6894ccde1f1b127
This commit is contained in:
@@ -63,24 +63,23 @@ MouseCursorController::~MouseCursorController() {
|
||||
mLocked.pointerSprite.clear();
|
||||
}
|
||||
|
||||
bool MouseCursorController::getBounds(float* outMinX, float* outMinY, float* outMaxX,
|
||||
float* outMaxY) const {
|
||||
std::optional<FloatRect> MouseCursorController::getBounds() const {
|
||||
std::scoped_lock lock(mLock);
|
||||
|
||||
return getBoundsLocked(outMinX, outMinY, outMaxX, outMaxY);
|
||||
return getBoundsLocked();
|
||||
}
|
||||
|
||||
bool MouseCursorController::getBoundsLocked(float* outMinX, float* outMinY, float* outMaxX,
|
||||
float* outMaxY) const REQUIRES(mLock) {
|
||||
std::optional<FloatRect> MouseCursorController::getBoundsLocked() const REQUIRES(mLock) {
|
||||
if (!mLocked.viewport.isValid()) {
|
||||
return false;
|
||||
return {};
|
||||
}
|
||||
|
||||
*outMinX = mLocked.viewport.logicalLeft;
|
||||
*outMinY = mLocked.viewport.logicalTop;
|
||||
*outMaxX = mLocked.viewport.logicalRight - 1;
|
||||
*outMaxY = mLocked.viewport.logicalBottom - 1;
|
||||
return true;
|
||||
return FloatRect{
|
||||
static_cast<float>(mLocked.viewport.logicalLeft),
|
||||
static_cast<float>(mLocked.viewport.logicalTop),
|
||||
static_cast<float>(mLocked.viewport.logicalRight - 1),
|
||||
static_cast<float>(mLocked.viewport.logicalBottom - 1),
|
||||
};
|
||||
}
|
||||
|
||||
void MouseCursorController::move(float deltaX, float deltaY) {
|
||||
@@ -121,31 +120,19 @@ void MouseCursorController::setPosition(float x, float y) {
|
||||
}
|
||||
|
||||
void MouseCursorController::setPositionLocked(float x, float y) REQUIRES(mLock) {
|
||||
float minX, minY, maxX, maxY;
|
||||
if (getBoundsLocked(&minX, &minY, &maxX, &maxY)) {
|
||||
if (x <= minX) {
|
||||
mLocked.pointerX = minX;
|
||||
} else if (x >= maxX) {
|
||||
mLocked.pointerX = maxX;
|
||||
} else {
|
||||
mLocked.pointerX = x;
|
||||
}
|
||||
if (y <= minY) {
|
||||
mLocked.pointerY = minY;
|
||||
} else if (y >= maxY) {
|
||||
mLocked.pointerY = maxY;
|
||||
} else {
|
||||
mLocked.pointerY = y;
|
||||
}
|
||||
updatePointerLocked();
|
||||
}
|
||||
const auto bounds = getBoundsLocked();
|
||||
if (!bounds) return;
|
||||
|
||||
mLocked.pointerX = std::max(bounds->left, std::min(bounds->right, x));
|
||||
mLocked.pointerY = std::max(bounds->top, std::min(bounds->bottom, y));
|
||||
|
||||
updatePointerLocked();
|
||||
}
|
||||
|
||||
void MouseCursorController::getPosition(float* outX, float* outY) const {
|
||||
FloatPoint MouseCursorController::getPosition() const {
|
||||
std::scoped_lock lock(mLock);
|
||||
|
||||
*outX = mLocked.pointerX;
|
||||
*outY = mLocked.pointerY;
|
||||
return {mLocked.pointerX, mLocked.pointerY};
|
||||
}
|
||||
|
||||
int32_t MouseCursorController::getDisplayId() const {
|
||||
@@ -235,10 +222,9 @@ void MouseCursorController::setDisplayViewport(const DisplayViewport& viewport,
|
||||
// Reset cursor position to center if size or display changed.
|
||||
if (oldViewport.displayId != viewport.displayId || oldDisplayWidth != newDisplayWidth ||
|
||||
oldDisplayHeight != newDisplayHeight) {
|
||||
float minX, minY, maxX, maxY;
|
||||
if (getBoundsLocked(&minX, &minY, &maxX, &maxY)) {
|
||||
mLocked.pointerX = (minX + maxX) * 0.5f;
|
||||
mLocked.pointerY = (minY + maxY) * 0.5f;
|
||||
if (const auto bounds = getBoundsLocked(); bounds) {
|
||||
mLocked.pointerX = (bounds->left + bounds->right) * 0.5f;
|
||||
mLocked.pointerY = (bounds->top + bounds->bottom) * 0.5f;
|
||||
// Reload icon resources for density may be changed.
|
||||
loadResourcesLocked(getAdditionalMouseResources);
|
||||
} else {
|
||||
|
||||
@@ -43,12 +43,12 @@ public:
|
||||
MouseCursorController(PointerControllerContext& context);
|
||||
~MouseCursorController();
|
||||
|
||||
bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const;
|
||||
std::optional<FloatRect> getBounds() const;
|
||||
void move(float deltaX, float deltaY);
|
||||
void setButtonState(int32_t buttonState);
|
||||
int32_t getButtonState() const;
|
||||
void setPosition(float x, float y);
|
||||
void getPosition(float* outX, float* outY) const;
|
||||
FloatPoint getPosition() const;
|
||||
int32_t getDisplayId() const;
|
||||
void fade(PointerControllerInterface::Transition transition);
|
||||
void unfade(PointerControllerInterface::Transition transition);
|
||||
@@ -102,7 +102,7 @@ private:
|
||||
|
||||
} mLocked GUARDED_BY(mLock);
|
||||
|
||||
bool getBoundsLocked(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const;
|
||||
std::optional<FloatRect> getBoundsLocked() const;
|
||||
void setPositionLocked(float x, float y);
|
||||
|
||||
void updatePointerLocked();
|
||||
|
||||
@@ -114,16 +114,15 @@ PointerController::PointerController(const sp<PointerControllerPolicyInterface>&
|
||||
PointerController::~PointerController() {
|
||||
mDisplayInfoListener->onPointerControllerDestroyed();
|
||||
mUnregisterWindowInfosListener(mDisplayInfoListener);
|
||||
mContext.getPolicy()->onPointerDisplayIdChanged(ADISPLAY_ID_NONE, 0, 0);
|
||||
mContext.getPolicy()->onPointerDisplayIdChanged(ADISPLAY_ID_NONE, FloatPoint{0, 0});
|
||||
}
|
||||
|
||||
std::mutex& PointerController::getLock() const {
|
||||
return mDisplayInfoListener->mLock;
|
||||
}
|
||||
|
||||
bool PointerController::getBounds(float* outMinX, float* outMinY, float* outMaxX,
|
||||
float* outMaxY) const {
|
||||
return mCursorController.getBounds(outMinX, outMinY, outMaxX, outMaxY);
|
||||
std::optional<FloatRect> PointerController::getBounds() const {
|
||||
return mCursorController.getBounds();
|
||||
}
|
||||
|
||||
void PointerController::move(float deltaX, float deltaY) {
|
||||
@@ -156,15 +155,13 @@ void PointerController::setPosition(float x, float y) {
|
||||
mCursorController.setPosition(transformed.x, transformed.y);
|
||||
}
|
||||
|
||||
void PointerController::getPosition(float* outX, float* outY) const {
|
||||
FloatPoint PointerController::getPosition() const {
|
||||
const int32_t displayId = mCursorController.getDisplayId();
|
||||
mCursorController.getPosition(outX, outY);
|
||||
const auto p = mCursorController.getPosition();
|
||||
{
|
||||
std::scoped_lock lock(getLock());
|
||||
const auto& transform = getTransformForDisplayLocked(displayId);
|
||||
const auto xy = transform.inverse().transform(*outX, *outY);
|
||||
*outX = xy.x;
|
||||
*outY = xy.y;
|
||||
return FloatPoint{transform.inverse().transform(p.x, p.y)};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -264,7 +261,7 @@ void PointerController::reloadPointerResources() {
|
||||
void PointerController::setDisplayViewport(const DisplayViewport& viewport) {
|
||||
struct PointerDisplayChangeArgs {
|
||||
int32_t displayId;
|
||||
float x, y;
|
||||
FloatPoint cursorPosition;
|
||||
};
|
||||
std::optional<PointerDisplayChangeArgs> pointerDisplayChanged;
|
||||
|
||||
@@ -278,18 +275,15 @@ void PointerController::setDisplayViewport(const DisplayViewport& viewport) {
|
||||
}
|
||||
mCursorController.setDisplayViewport(viewport, getAdditionalMouseResources);
|
||||
if (viewport.displayId != mLocked.pointerDisplayId) {
|
||||
float xPos, yPos;
|
||||
mCursorController.getPosition(&xPos, &yPos);
|
||||
mLocked.pointerDisplayId = viewport.displayId;
|
||||
pointerDisplayChanged = {viewport.displayId, xPos, yPos};
|
||||
pointerDisplayChanged = {viewport.displayId, mCursorController.getPosition()};
|
||||
}
|
||||
} // release lock
|
||||
|
||||
if (pointerDisplayChanged) {
|
||||
// Notify the policy without holding the pointer controller lock.
|
||||
mContext.getPolicy()->onPointerDisplayIdChanged(pointerDisplayChanged->displayId,
|
||||
pointerDisplayChanged->x,
|
||||
pointerDisplayChanged->y);
|
||||
pointerDisplayChanged->cursorPosition);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -50,12 +50,12 @@ public:
|
||||
|
||||
~PointerController() override;
|
||||
|
||||
virtual bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const;
|
||||
virtual std::optional<FloatRect> getBounds() const;
|
||||
virtual void move(float deltaX, float deltaY);
|
||||
virtual void setButtonState(int32_t buttonState);
|
||||
virtual int32_t getButtonState() const;
|
||||
virtual void setPosition(float x, float y);
|
||||
virtual void getPosition(float* outX, float* outY) const;
|
||||
virtual FloatPoint getPosition() const;
|
||||
virtual int32_t getDisplayId() const;
|
||||
virtual void fade(Transition transition);
|
||||
virtual void unfade(Transition transition);
|
||||
|
||||
@@ -81,7 +81,7 @@ public:
|
||||
virtual PointerIconStyle getDefaultPointerIconId() = 0;
|
||||
virtual PointerIconStyle getDefaultStylusIconId() = 0;
|
||||
virtual PointerIconStyle getCustomPointerIconId() = 0;
|
||||
virtual void onPointerDisplayIdChanged(int32_t displayId, float xPos, float yPos) = 0;
|
||||
virtual void onPointerDisplayIdChanged(int32_t displayId, const FloatPoint& position) = 0;
|
||||
};
|
||||
|
||||
/*
|
||||
|
||||
@@ -60,7 +60,7 @@ public:
|
||||
virtual PointerIconStyle getDefaultPointerIconId() override;
|
||||
virtual PointerIconStyle getDefaultStylusIconId() override;
|
||||
virtual PointerIconStyle getCustomPointerIconId() override;
|
||||
virtual void onPointerDisplayIdChanged(int32_t displayId, float xPos, float yPos) override;
|
||||
virtual void onPointerDisplayIdChanged(int32_t displayId, const FloatPoint& position) override;
|
||||
|
||||
bool allResourcesAreLoaded();
|
||||
bool noResourcesAreLoaded();
|
||||
@@ -143,8 +143,8 @@ void MockPointerControllerPolicyInterface::loadPointerIconForType(SpriteIcon* ic
|
||||
}
|
||||
|
||||
void MockPointerControllerPolicyInterface::onPointerDisplayIdChanged(int32_t displayId,
|
||||
float /*xPos*/,
|
||||
float /*yPos*/) {
|
||||
const FloatPoint& /*position*/
|
||||
) {
|
||||
latestPointerDisplayId = displayId;
|
||||
}
|
||||
|
||||
|
||||
@@ -308,7 +308,7 @@ public:
|
||||
void setMotionClassifierEnabled(bool enabled);
|
||||
std::optional<std::string> getBluetoothAddress(int32_t deviceId);
|
||||
void setStylusButtonMotionEventsEnabled(bool enabled);
|
||||
std::array<float, 2> getMouseCursorPosition();
|
||||
FloatPoint getMouseCursorPosition();
|
||||
|
||||
/* --- InputReaderPolicyInterface implementation --- */
|
||||
|
||||
@@ -367,7 +367,7 @@ public:
|
||||
virtual PointerIconStyle getDefaultPointerIconId();
|
||||
virtual PointerIconStyle getDefaultStylusIconId();
|
||||
virtual PointerIconStyle getCustomPointerIconId();
|
||||
virtual void onPointerDisplayIdChanged(int32_t displayId, float xPos, float yPos);
|
||||
virtual void onPointerDisplayIdChanged(int32_t displayId, const FloatPoint& position);
|
||||
|
||||
/* --- If touch mode is enabled per display or global --- */
|
||||
|
||||
@@ -731,11 +731,11 @@ std::shared_ptr<PointerControllerInterface> NativeInputManager::obtainPointerCon
|
||||
return controller;
|
||||
}
|
||||
|
||||
void NativeInputManager::onPointerDisplayIdChanged(int32_t pointerDisplayId, float xPos,
|
||||
float yPos) {
|
||||
void NativeInputManager::onPointerDisplayIdChanged(int32_t pointerDisplayId,
|
||||
const FloatPoint& position) {
|
||||
JNIEnv* env = jniEnv();
|
||||
env->CallVoidMethod(mServiceObj, gServiceClassInfo.onPointerDisplayIdChanged, pointerDisplayId,
|
||||
xPos, yPos);
|
||||
position.x, position.y);
|
||||
checkAndClearExceptionFromCallback(env, "onPointerDisplayIdChanged");
|
||||
}
|
||||
|
||||
@@ -1656,14 +1656,12 @@ bool NativeInputManager::isPerDisplayTouchModeEnabled() {
|
||||
return static_cast<bool>(enabled);
|
||||
}
|
||||
|
||||
std::array<float, 2> NativeInputManager::getMouseCursorPosition() {
|
||||
FloatPoint NativeInputManager::getMouseCursorPosition() {
|
||||
AutoMutex _l(mLock);
|
||||
const auto pc = mLocked.pointerController.lock();
|
||||
if (!pc) return {{std::nanf(""), std::nanf("")}};
|
||||
if (!pc) return {AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION};
|
||||
|
||||
std::array<float, 2> p{};
|
||||
pc->getPosition(&p[0], &p[1]);
|
||||
return p;
|
||||
return pc->getPosition();
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
@@ -2560,9 +2558,10 @@ static void nativeSetStylusButtonMotionEventsEnabled(JNIEnv* env, jobject native
|
||||
|
||||
static jfloatArray nativeGetMouseCursorPosition(JNIEnv* env, jobject nativeImplObj) {
|
||||
NativeInputManager* im = getNativeInputManager(env, nativeImplObj);
|
||||
const std::array<float, 2> p = im->getMouseCursorPosition();
|
||||
const auto p = im->getMouseCursorPosition();
|
||||
const std::array<float, 2> arr = {{p.x, p.y}};
|
||||
jfloatArray outArr = env->NewFloatArray(2);
|
||||
env->SetFloatArrayRegion(outArr, 0, p.size(), p.data());
|
||||
env->SetFloatArrayRegion(outArr, 0, arr.size(), arr.data());
|
||||
return outArr;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user