Merge changes from topic "pc-return-val" into udc-dev
* changes: Return values in PointerController's getters Get mouse cursor position from PointerController instead of WM
This commit is contained in:
committed by
Android (Google) Code Review
commit
ffe3d2239e
@@ -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)};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -262,19 +259,31 @@ void PointerController::reloadPointerResources() {
|
||||
}
|
||||
|
||||
void PointerController::setDisplayViewport(const DisplayViewport& viewport) {
|
||||
std::scoped_lock lock(getLock());
|
||||
struct PointerDisplayChangeArgs {
|
||||
int32_t displayId;
|
||||
FloatPoint cursorPosition;
|
||||
};
|
||||
std::optional<PointerDisplayChangeArgs> pointerDisplayChanged;
|
||||
|
||||
bool getAdditionalMouseResources = false;
|
||||
if (mLocked.presentation == PointerController::Presentation::POINTER ||
|
||||
mLocked.presentation == PointerController::Presentation::STYLUS_HOVER) {
|
||||
getAdditionalMouseResources = true;
|
||||
}
|
||||
mCursorController.setDisplayViewport(viewport, getAdditionalMouseResources);
|
||||
if (viewport.displayId != mLocked.pointerDisplayId) {
|
||||
float xPos, yPos;
|
||||
mCursorController.getPosition(&xPos, &yPos);
|
||||
mContext.getPolicy()->onPointerDisplayIdChanged(viewport.displayId, xPos, yPos);
|
||||
mLocked.pointerDisplayId = viewport.displayId;
|
||||
{ // acquire lock
|
||||
std::scoped_lock lock(getLock());
|
||||
|
||||
bool getAdditionalMouseResources = false;
|
||||
if (mLocked.presentation == PointerController::Presentation::POINTER ||
|
||||
mLocked.presentation == PointerController::Presentation::STYLUS_HOVER) {
|
||||
getAdditionalMouseResources = true;
|
||||
}
|
||||
mCursorController.setDisplayViewport(viewport, getAdditionalMouseResources);
|
||||
if (viewport.displayId != mLocked.pointerDisplayId) {
|
||||
mLocked.pointerDisplayId = viewport.displayId;
|
||||
pointerDisplayChanged = {viewport.displayId, mCursorController.getPosition()};
|
||||
}
|
||||
} // release lock
|
||||
|
||||
if (pointerDisplayChanged) {
|
||||
// Notify the policy without holding the pointer controller lock.
|
||||
mContext.getPolicy()->onPointerDisplayIdChanged(pointerDisplayChanged->displayId,
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@@ -96,7 +96,11 @@ public abstract class InputManagerInternal {
|
||||
*/
|
||||
public abstract int getVirtualMousePointerDisplayId();
|
||||
|
||||
/** Gets the current position of the mouse cursor. */
|
||||
/**
|
||||
* Gets the current position of the mouse cursor.
|
||||
*
|
||||
* Returns NaN-s as the coordinates if the cursor is not available.
|
||||
*/
|
||||
public abstract PointF getCursorPosition();
|
||||
|
||||
/**
|
||||
|
||||
@@ -2863,9 +2863,6 @@ public class InputManagerService extends IInputManager.Stub
|
||||
|
||||
int getPointerDisplayId();
|
||||
|
||||
/** Gets the x and y coordinates of the cursor's current position. */
|
||||
PointF getCursorPosition();
|
||||
|
||||
/**
|
||||
* Notifies window manager that a {@link android.view.MotionEvent#ACTION_DOWN} pointer event
|
||||
* occurred on a window that did not have focus.
|
||||
@@ -3189,7 +3186,11 @@ public class InputManagerService extends IInputManager.Stub
|
||||
|
||||
@Override
|
||||
public PointF getCursorPosition() {
|
||||
return mWindowManagerCallbacks.getCursorPosition();
|
||||
final float[] p = mNative.getMouseCursorPosition();
|
||||
if (p == null || p.length != 2) {
|
||||
throw new IllegalStateException("Failed to get mouse cursor position");
|
||||
}
|
||||
return new PointF(p[0], p[1]);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -224,6 +224,16 @@ interface NativeInputManagerService {
|
||||
/** Set whether stylus button reporting through motion events should be enabled. */
|
||||
void setStylusButtonMotionEventsEnabled(boolean enabled);
|
||||
|
||||
/**
|
||||
* Get the current position of the mouse cursor.
|
||||
*
|
||||
* If the mouse cursor is not currently shown, the coordinate values will be NaN-s.
|
||||
*
|
||||
* NOTE: This will grab the PointerController's lock, so we must be careful about calling this
|
||||
* from the InputReader or Display threads, which may result in a deadlock.
|
||||
*/
|
||||
float[] getMouseCursorPosition();
|
||||
|
||||
/** The native implementation of InputManagerService methods. */
|
||||
class NativeImpl implements NativeInputManagerService {
|
||||
/** Pointer to native input manager service object, used by native code. */
|
||||
@@ -465,5 +475,8 @@ interface NativeInputManagerService {
|
||||
|
||||
@Override
|
||||
public native void setStylusButtonMotionEventsEnabled(boolean enabled);
|
||||
|
||||
@Override
|
||||
public native float[] getMouseCursorPosition();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,7 +25,6 @@ import static com.android.server.wm.WindowManagerService.H.ON_POINTER_DOWN_OUTSI
|
||||
|
||||
import android.annotation.NonNull;
|
||||
import android.annotation.Nullable;
|
||||
import android.graphics.PointF;
|
||||
import android.os.Debug;
|
||||
import android.os.IBinder;
|
||||
import android.util.Slog;
|
||||
@@ -220,11 +219,6 @@ final class InputManagerCallback implements InputManagerService.WindowManagerCal
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public PointF getCursorPosition() {
|
||||
return mService.getLatestMousePosition();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPointerDownOutsideFocus(IBinder touchedToken) {
|
||||
mService.mH.obtainMessage(ON_POINTER_DOWN_OUTSIDE_FOCUS, touchedToken).sendToTarget();
|
||||
|
||||
@@ -186,7 +186,6 @@ import android.database.ContentObserver;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Matrix;
|
||||
import android.graphics.Point;
|
||||
import android.graphics.PointF;
|
||||
import android.graphics.Rect;
|
||||
import android.graphics.Region;
|
||||
import android.hardware.configstore.V1_0.OptionalBool;
|
||||
@@ -7361,14 +7360,6 @@ public class WindowManagerService extends IWindowManager.Stub
|
||||
.setPointerIconType(PointerIcon.TYPE_DEFAULT);
|
||||
}
|
||||
}
|
||||
|
||||
PointF getLatestMousePosition() {
|
||||
synchronized (mMousePositionTracker) {
|
||||
return new PointF(mMousePositionTracker.mLatestMouseX,
|
||||
mMousePositionTracker.mLatestMouseY);
|
||||
}
|
||||
}
|
||||
|
||||
void setMousePointerDisplayId(int displayId) {
|
||||
mMousePositionTracker.setPointerDisplayId(displayId);
|
||||
}
|
||||
|
||||
@@ -308,6 +308,7 @@ public:
|
||||
void setMotionClassifierEnabled(bool enabled);
|
||||
std::optional<std::string> getBluetoothAddress(int32_t deviceId);
|
||||
void setStylusButtonMotionEventsEnabled(bool enabled);
|
||||
FloatPoint getMouseCursorPosition();
|
||||
|
||||
/* --- InputReaderPolicyInterface implementation --- */
|
||||
|
||||
@@ -366,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 --- */
|
||||
|
||||
@@ -730,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");
|
||||
}
|
||||
|
||||
@@ -1655,6 +1656,14 @@ bool NativeInputManager::isPerDisplayTouchModeEnabled() {
|
||||
return static_cast<bool>(enabled);
|
||||
}
|
||||
|
||||
FloatPoint NativeInputManager::getMouseCursorPosition() {
|
||||
AutoMutex _l(mLock);
|
||||
const auto pc = mLocked.pointerController.lock();
|
||||
if (!pc) return {AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION};
|
||||
|
||||
return pc->getPosition();
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
static NativeInputManager* getNativeInputManager(JNIEnv* env, jobject clazz) {
|
||||
@@ -2547,6 +2556,15 @@ static void nativeSetStylusButtonMotionEventsEnabled(JNIEnv* env, jobject native
|
||||
im->setStylusButtonMotionEventsEnabled(enabled);
|
||||
}
|
||||
|
||||
static jfloatArray nativeGetMouseCursorPosition(JNIEnv* env, jobject nativeImplObj) {
|
||||
NativeInputManager* im = getNativeInputManager(env, nativeImplObj);
|
||||
const auto p = im->getMouseCursorPosition();
|
||||
const std::array<float, 2> arr = {{p.x, p.y}};
|
||||
jfloatArray outArr = env->NewFloatArray(2);
|
||||
env->SetFloatArrayRegion(outArr, 0, arr.size(), arr.data());
|
||||
return outArr;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
static const JNINativeMethod gInputManagerMethods[] = {
|
||||
@@ -2640,6 +2658,7 @@ static const JNINativeMethod gInputManagerMethods[] = {
|
||||
{"getBluetoothAddress", "(I)Ljava/lang/String;", (void*)nativeGetBluetoothAddress},
|
||||
{"setStylusButtonMotionEventsEnabled", "(Z)V",
|
||||
(void*)nativeSetStylusButtonMotionEventsEnabled},
|
||||
{"getMouseCursorPosition", "()[F", (void*)nativeGetMouseCursorPosition},
|
||||
};
|
||||
|
||||
#define FIND_CLASS(var, className) \
|
||||
|
||||
Reference in New Issue
Block a user