diff --git a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt index 01206389c3..540c7ed986 100644 --- a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt @@ -85,6 +85,7 @@ import com.droiddeck.launcher.input.SteamControllerBle import com.droiddeck.launcher.input.DirectTouchHold import com.droiddeck.launcher.input.GestureTouchStreamGate import com.droiddeck.launcher.input.PointerGestures +import com.droiddeck.launcher.input.PointerSpace import com.droiddeck.launcher.input.SecondScreenDisplay import com.droiddeck.launcher.input.SecondScreenDisplays import com.droiddeck.launcher.input.SecondScreenMode @@ -2980,7 +2981,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { private val gestureOutput = object : TouchscreenGestures.Output { override fun moveTo(x: Float, y: Float) { - val rect = drawnRect() ?: return + val rect = pointerSurfaceRect() ?: return if (pointerLocked) { if (gesturePointerReady) moveBy(x - gesturePointerX, y - gesturePointerY, true) } else movePointer(x, y) @@ -2990,7 +2991,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { cursorVisible = false } override fun moveBy(dx: Float, dy: Float, relative: Boolean) { - val rect = drawnRect() ?: return + val rect = (if (pointerLocked) drawnRect() else pointerSurfaceRect()) ?: return if (relative || pointerLocked) { val scale = sceneScale(rect) sendSceneDelta(dx * scale, dy * scale) @@ -3001,7 +3002,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { } override fun prepareRelativePan() { if (pointerLocked) return - val rect = drawnRect() ?: return + val rect = pointerSurfaceRect() ?: return moveTo(rect.centerX(), rect.centerY()) } override fun key(code: Int, down: Boolean) { @@ -3045,7 +3046,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { private val scrollListener = object : ScrollTouchGestures.Listener { override fun onMove(x: Float, y: Float) { - val rect = drawnRect() ?: return + val rect = pointerSurfaceRect() ?: return val cx = x.coerceIn(rect.left, rect.right) val cy = y.coerceIn(rect.top, rect.bottom) WaylandCompositor.nativeSendPointer(1, InputSpace.x(cx, outputView().width), InputSpace.y(cy, outputView().height)) @@ -3055,7 +3056,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { WaylandCompositor.nativeSendSceneInput(3, button, if (pressed) 1 else 0) } override fun onScroll(dx: Float, dy: Float) { - val rect = drawnRect() ?: return + val rect = pointerSurfaceRect() ?: return val out = SessionState.outputSize scrollRestX += dx * out.first / rect.width() scrollRestY += dy * out.second / rect.height() @@ -3074,7 +3075,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { override fun onScrollStart() { scrollRestX = 0f scrollRestY = 0f - val rect = drawnRect() ?: return + val rect = pointerSurfaceRect() ?: return val x = rect.left + rect.width() * PAD_SCROLL_POINTER_X WaylandCompositor.nativeSendPointer(1, InputSpace.x(x, outputView().width), InputSpace.y(rect.centerY(), outputView().height)) cursorVisible = false @@ -3087,7 +3088,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { override fun onNavigate() { cursorVisible = false } - override fun scrollSpeed(): Float = (drawnRect()?.height() ?: 0f) * PAD_SCROLL_HEIGHTS_PER_S + override fun scrollSpeed(): Float = (pointerSurfaceRect()?.height() ?: 0f) * PAD_SCROLL_HEIGHTS_PER_S } private fun padMouseMode(): Boolean { @@ -3105,7 +3106,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { (routedTouchMode == SessionPrefs.TOUCH_AUTO || routedTouchMode == SessionPrefs.TOUCH_DIRECT) && padMouseMode() private fun fingerScroll(dx: Float, dy: Float) { - val rect = drawnRect() ?: return + val rect = pointerSurfaceRect() ?: return val out = SessionState.outputSize scrollRestX += dx * out.first / rect.width() * AXIS_UNITS_PER_PIXEL scrollRestY += dy * out.second / rect.height() * AXIS_UNITS_PER_PIXEL @@ -3128,7 +3129,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { if (!WaylandCompositor.nativeCursorInputPos(cursorPosBuf)) return val x = cursorPosBuf[0] / InputSpace.WIDTH.toFloat() * outputView().width val y = cursorPosBuf[1] / InputSpace.HEIGHT.toFloat() * outputView().height - if (rect.contains(x, y)) touchpad.place(x, y) + touchpad.place(x.coerceIn(rect.left, rect.right), y.coerceIn(rect.top, rect.bottom)) } private fun moveLockedPointer(x: Float, y: Float) { @@ -3140,7 +3141,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { } private fun movePointer(x: Float, y: Float, fromMouse: Boolean = false) { - val rect = drawnRect() ?: return + val rect = pointerSurfaceRect() ?: return val cx = x.coerceIn(rect.left, rect.right) val cy = y.coerceIn(rect.top, rect.bottom) WaylandCompositor.nativeSendPointer(1, InputSpace.x(cx, outputView().width), InputSpace.y(cy, outputView().height)) @@ -3511,7 +3512,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { if (width <= 0f || height <= 0f) return val nx = (x / width).coerceIn(0f, 1f) val ny = (y / height).coerceIn(0f, 1f) - val rect = drawnRect() ?: return + val rect = pointerSurfaceRect() ?: return val px = rect.left + nx * rect.width() val py = rect.top + ny * rect.height() WaylandCompositor.nativeSendPointer(1, InputSpace.x(px, outputView().width), InputSpace.y(py, outputView().height)) @@ -3694,6 +3695,16 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { return android.graphics.RectF(left, top, left + drawnW, top + drawnH) } + /** + * Where the pointer can travel on this view. With the glasses owning the output the view is + * not showing the picture, so the whole view is the touchpad; using the letterboxed picture + * rect there would leave the pointer unable to reach the scene edges. + */ + private fun pointerSurfaceRect(): android.graphics.RectF? { + val view = outputView() + return PointerSpace.area(view.width, view.height, glassesOutputActive, drawnRect()) + } + private var ignoreGameTouch = false private var touchEditorOpen by mutableStateOf(false) private var lockedSwipeUsed = false @@ -3755,7 +3766,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { if (threeFingerTap.active || wasActive) return true } if (route == com.droiddeck.launcher.input.TouchRouter.Route.TOUCHPAD) { - val rect = drawnRect() ?: return false + val rect = (if (pointerLocked) drawnRect() else pointerSurfaceRect()) ?: return false if (touchpad.bounds != rect) { val fresh = touchpad.bounds.width() <= 1f touchpad.bounds = rect diff --git a/app/src/main/java/com/droiddeck/launcher/input/PointerSpace.kt b/app/src/main/java/com/droiddeck/launcher/input/PointerSpace.kt new file mode 100644 index 0000000000..4e0a9b988b --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/input/PointerSpace.kt @@ -0,0 +1,20 @@ +package com.droiddeck.launcher.input + +import android.graphics.RectF + +/** + * The area of the session view that drives the guest pointer. + * + * The view is also the compositor output only while it shows the picture: the phone, or the + * bottom screen. With the glasses owning the output the view is a touchpad for a scene drawn + * elsewhere, so the whole view has to drive the pointer. Using the picture rect there would + * confine it to the letterboxed band the scene would occupy on the phone, and the pointer could + * not reach the scene edges. + */ +object PointerSpace { + fun area(viewWidth: Int, viewHeight: Int, glassesOwnOutput: Boolean, picture: RectF?): RectF? { + if (viewWidth <= 0 || viewHeight <= 0) return null + if (glassesOwnOutput) return RectF(0f, 0f, viewWidth.toFloat(), viewHeight.toFloat()) + return picture + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/input/PointerSpaceTest.kt b/app/src/test/java/com/droiddeck/launcher/input/PointerSpaceTest.kt new file mode 100644 index 0000000000..7ab117b82c --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/input/PointerSpaceTest.kt @@ -0,0 +1,30 @@ +package com.droiddeck.launcher.input + +import android.graphics.RectF +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNull +import org.junit.Assert.assertSame +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [28], manifest = Config.NONE) +class PointerSpaceTest { + private val phonePicture = RectF(0f, 896f, 1080f, 1503f) + + @Test fun theGlassesMakeTheWholeViewThePointerArea() { + assertEquals(RectF(0f, 0f, 1080f, 2400f), + PointerSpace.area(1080, 2400, glassesOwnOutput = true, phonePicture)) + } + + @Test fun theViewThatShowsThePictureUsesItsPictureRect() { + assertSame(phonePicture, PointerSpace.area(2400, 1504, glassesOwnOutput = false, phonePicture)) + } + + @Test fun aViewWithoutSizeHasNoPointerArea() { + assertNull(PointerSpace.area(0, 2400, glassesOwnOutput = true, phonePicture)) + assertNull(PointerSpace.area(1080, 0, glassesOwnOutput = false, phonePicture)) + } +}