diff --git a/app/src/main/cpp/waylandcomp/src/compositor.c b/app/src/main/cpp/waylandcomp/src/compositor.c index 3510ea29a3..39a20bc7ac 100644 --- a/app/src/main/cpp/waylandcomp/src/compositor.c +++ b/app/src/main/cpp/waylandcomp/src/compositor.c @@ -127,6 +127,9 @@ static int log_budget(void) { #ifndef BTN_RIGHT #define BTN_RIGHT 0x111 #endif +#ifndef BTN_MIDDLE +#define BTN_MIDDLE 0x112 +#endif #define INPUT_SPACE_W 1920 #define INPUT_SPACE_H 1080 @@ -582,6 +585,13 @@ int droiddeck_cursor_snapshot(int *out, int cap) { } struct active_touch { struct surface *surface; int pointer_fallback; }; static struct active_touch g_touch_points[MAX_TOUCH_POINTS]; +#define PEN_IN (1 << 0) +#define PEN_TIP (1 << 1) +#define PEN_RIGHT (1 << 2) +#define PEN_MIDDLE (1 << 3) +#define PEN_BIT(button) (1 << ((button) - BTN_LEFT)) +struct pen_state { int in, buttons; struct surface *grab; }; +static struct pen_state g_pen; static void touch_cancel_surface(struct surface *surface); static struct surface *g_grab; static void publish_query_state(void); @@ -1339,6 +1349,7 @@ static void surface_resource_destroy(struct wl_resource *r) { if (g_grab == s) g_grab = NULL; if (g_key_target == s) g_key_target = NULL; if (g_ime_click == s) g_ime_click = NULL; + if (g_pen.grab == s) g_pen.grab = NULL; droiddeck_text_input_surface_gone(r); if (g_desktop == s) { g_desktop = NULL; droiddeck_log("desktop", "the desktop closed"); } if (g_hud_surface == s) { g_hud_surface = NULL; droiddeck_on_game_surface(NULL, NULL); } @@ -3240,14 +3251,14 @@ static void deliver_pointer(const struct input_msg *m) { pointer_event(x, y, m->p1 == 1 ? 0 : BTN_LEFT, m->p1 == 0); } -static void input_output_to_scene(int px, int py, double *x, double *y) { +static void input_output_to_scene(double px, double py, double *x, double *y) { int w, h, ow, oh; scene_size(&w, &h); vkp_output_size(&ow, &oh); if (ow <= 0 || oh <= 0 || - !vkp_output_to_scene((double)px * ow / INPUT_SPACE_W, (double)py * oh / INPUT_SPACE_H, x, y)) { - *x = (double)px * w / INPUT_SPACE_W; - *y = (double)py * h / INPUT_SPACE_H; + !vkp_output_to_scene(px * ow / INPUT_SPACE_W, py * oh / INPUT_SPACE_H, x, y)) { + *x = px * w / INPUT_SPACE_W; + *y = py * h / INPUT_SPACE_H; } if (*x < 0) *x = 0; if (*y < 0) *y = 0; @@ -3476,6 +3487,57 @@ static void deliver_touch(const struct input_msg *m) { publish_query_state(); } +static void pen_log_target(void) { + static struct wl_client *said; + for (int i = 0; i < g_nptrs; i++) { + if (!g_ptrs[i].focus) continue; + struct wl_client *client = wl_resource_get_client(g_ptrs[i].focus); + if (client != said && log_budget()) { + char name[160]; + describe(wl_resource_get_user_data(g_ptrs[i].focus), name, sizeof(name)); + droiddeck_log("pen", "%s gets the pen as the pointer", name); + } + said = client; + return; + } +} + +static int pen_buttons(int flags) { + int held = 0; + if (flags & PEN_TIP) held |= PEN_BIT(BTN_LEFT); + if (flags & PEN_RIGHT) held |= PEN_BIT(BTN_RIGHT); + if (flags & PEN_MIDDLE) held |= PEN_BIT(BTN_MIDDLE); + return held; +} + +static void pen_release(int buttons) { + for (uint32_t button = BTN_LEFT; button <= BTN_MIDDLE; button++) + if (buttons & PEN_BIT(button)) pointer_button(button, 0); +} + +static void deliver_pen(const struct input_msg *m) { + if (!(m->p1 & PEN_IN)) { + pen_release(g_pen.buttons); + if (g_pen.in) g_raw_valid = 0; + memset(&g_pen, 0, sizeof(g_pen)); + return; + } + int held = pen_buttons(m->p1); + if (!g_pen.in) g_raw_valid = 0; + g_pen.in = 1; + double x, y; + input_output_to_scene(m->p2 / 256.0, m->p3 / 256.0, &x, &y); + if (g_pen.buttons) g_grab = g_pen.grab; + if (!pointer_locked_for_cursor()) pointer_event(x, y, 0, 0); + for (uint32_t button = BTN_LEFT; button <= BTN_MIDDLE; button++) + if (held & ~g_pen.buttons & PEN_BIT(button)) pointer_button(button, 1); + g_pen.grab = g_grab; + pen_release(g_pen.buttons & ~held); + if (held) g_grab = g_pen.grab; + g_pen.buttons = held; + pen_log_target(); +} + static void key_event(uint32_t evdev, int pressed); struct wl_resource *droiddeck_ime_target(void); static void deliver_key(const struct input_msg *m) { @@ -3627,6 +3689,7 @@ static int on_input_readable(int fd, uint32_t mask, void *data) { case 10: break; case 11: axis_event(m.p1, m.p2, 0); break; case 12: axis_event(m.p1, m.p2, 1); break; + case 13: deliver_pen(&m); break; case 20: stream_take(); break; case 21: if (!m.p1) stream_forget_all(); schedule_render(); break; case 22: schedule_render(); break; @@ -3666,6 +3729,13 @@ void droiddeck_wayland_send_touch(int action, int pointer_id, int x, int y) { (void)n; } +void droiddeck_wayland_send_pen(int flags, int x, int y) { + if (g_input_pipe[1] < 0) return; + struct input_msg m = { 13, flags, x, y, 0 }; + ssize_t n = write(g_input_pipe[1], &m, sizeof(m)); + (void)n; +} + void droiddeck_wayland_send_key(int evdev, int state) { if (g_input_pipe[1] < 0) return; struct input_msg m = { 1, evdev, state, 0, 0 }; diff --git a/app/src/main/cpp/waylandcomp/src/waylandcomp_jni.c b/app/src/main/cpp/waylandcomp/src/waylandcomp_jni.c index e5032a72a0..f53c947070 100644 --- a/app/src/main/cpp/waylandcomp/src/waylandcomp_jni.c +++ b/app/src/main/cpp/waylandcomp/src/waylandcomp_jni.c @@ -24,6 +24,7 @@ extern int droiddeck_wayland_run(void); extern void droiddeck_wayland_set_scale_mode(int mode, int alignment); extern void droiddeck_wayland_send_pointer(int action, int x, int y); extern void droiddeck_wayland_send_touch(int action, int pointer_id, int x, int y); +extern void droiddeck_wayland_send_pen(int flags, int x, int y); extern int droiddeck_cursor_snapshot(int *out, int cap); extern int droiddeck_cursor_serial(void); extern int droiddeck_hud_frame_times(float *out, int len, int64_t *total); @@ -280,6 +281,12 @@ Java_com_droiddeck_launcher_wayland_WaylandCompositor_nativeSendTouch( droiddeck_wayland_send_touch(action, pointer_id, x, y); } +JNIEXPORT void JNICALL +Java_com_droiddeck_launcher_wayland_WaylandCompositor_nativeSendPen( + JNIEnv *env, jclass clazz, jint flags, jint x, jint y) { + droiddeck_wayland_send_pen(flags, x, y); +} + JNIEXPORT jint JNICALL Java_com_droiddeck_launcher_wayland_WaylandCompositor_nativeCursorSnapshot( JNIEnv *env, jclass clazz, jintArray out) { diff --git a/app/src/main/java/com/droiddeck/launcher/FrontEndActivity.kt b/app/src/main/java/com/droiddeck/launcher/FrontEndActivity.kt index b195192643..1de35daddc 100644 --- a/app/src/main/java/com/droiddeck/launcher/FrontEndActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/FrontEndActivity.kt @@ -535,6 +535,7 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet private var upscaleSharpness by mutableStateOf(75) private var hdrReason by mutableStateOf(null) private var touchMode by mutableStateOf(SessionPrefs.TOUCH_AUTO) + private var pen by mutableStateOf(null) private var suspendPolicy by mutableStateOf(SessionPrefs.SUSPEND_MANUAL) private var steamDownloadsInBackground by mutableStateOf(false) private var oscMode by mutableStateOf(SessionPrefs.OSC_AUTO) @@ -2471,6 +2472,7 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet upscaler = upscaler, upscaleSharpness = upscaleSharpness, gpuDrivers = drivers.summary(), touchMode = touchMode, + pen = pen, suspendPolicy = suspendPolicy, steamDownloadsInBackground = steamDownloadsInBackground, pipSupported = com.droiddeck.launcher.session.SessionPipController.supported(this), @@ -2523,6 +2525,7 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet onUpscaler = { m -> SessionPrefs.setUpscaler(this, m); upscaler = m }, onUpscaleSharpness = { pct -> SessionPrefs.setUpscaleSharpness(this, pct); upscaleSharpness = pct }, onTouch = { t -> SessionPrefs.setTouchMode(this, t); touchMode = t }, + onPen = { key, value -> SessionPrefs.setPenSetting(this, key, value); pen = SessionPrefs.pen(this) }, onSuspendPolicy = { policy -> SessionPrefs.setSuspendPolicy(this, mode, policy); suspendPolicy = policy }, onSteamDownloadsInBackground = { enabled -> SessionPrefs.setSteamDownloadsInBackground(this, enabled) @@ -2869,6 +2872,8 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet upscaleSharpness = SessionPrefs.upscaleSharpness(this) hdrReason = com.droiddeck.launcher.wayland.HdrSupport.probe(this).reason touchMode = SessionPrefs.touchMode(this) + if (!SessionPrefs.penSeen(this) && com.droiddeck.launcher.input.PenStream.penConnected()) SessionPrefs.setPenSeen(this) + pen = SessionPrefs.pen(this).takeIf { SessionPrefs.penSeen(this) } suspendPolicy = SessionPrefs.suspendPolicy(this, mode) steamDownloadsInBackground = SessionPrefs.steamDownloadsInBackground(this) oscMode = SessionPrefs.oscMode(this) diff --git a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt index bae71a4159..3775b229b2 100644 --- a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt @@ -97,8 +97,11 @@ import com.droiddeck.launcher.input.PadState import com.droiddeck.launcher.input.SteamControllerBle import com.droiddeck.launcher.input.DirectTouchHold import com.droiddeck.launcher.input.GestureTouchStreamGate +import com.droiddeck.launcher.input.PenRouter +import com.droiddeck.launcher.input.PenStream import com.droiddeck.launcher.input.PointerGestures import com.droiddeck.launcher.input.PointerSpace +import com.droiddeck.launcher.input.pointerSubset import com.droiddeck.launcher.input.SecondScreenDisplay import com.droiddeck.launcher.input.SecondScreenDisplays import com.droiddeck.launcher.input.SecondScreenMode @@ -266,6 +269,11 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde private var steamTouchOptions: com.droiddeck.launcher.input.SteamTouchConfig.Options? = null private var routedTouchMode = SessionPrefs.TOUCH_AUTO private var touchRoute: com.droiddeck.launcher.input.TouchRouter.Route? = null + private var penSettings by mutableStateOf(SessionPrefs.Pen()) + private var penSeen by mutableStateOf(false) + private var penRoute: PenRouter.Route? = null + private var penDown = false + private val penStream = PenStream(WaylandCompositor::nativeSendPen) private var keyboard: KeyboardHost? = null private var desktopKeyboardButton: com.droiddeck.launcher.input.DesktopKeyboardButton? = null private var lastUserPressMs = 0L @@ -667,6 +675,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde private val deviceListener = object : InputManager.InputDeviceListener { override fun onInputDeviceAdded(deviceId: Int) { logInputDevice("connected", deviceId) + if (!penSeen && InputDevice.getDevice(deviceId)?.let { PenStream.isPenDevice(it.sources) } == true) notePenSeen() joyConVr?.refresh() padBridge?.syncConnectedControllers() refreshControls() @@ -677,6 +686,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde } override fun onInputDeviceRemoved(deviceId: Int) { physicalShortcuts.onDeviceRemoved(deviceId) + penStream.deviceRemoved(deviceId) Log.i(TAG, "input device $deviceId disconnected") dropMouseCursor() backQamInput.onDeviceRemoved(deviceId) @@ -771,6 +781,9 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde uiHandler.postDelayed(::updatePointerCapture, 250) } } + override fun onResolvePointerIcon(event: MotionEvent, pointerIndex: Int): android.view.PointerIcon? = + if (platformPointerShown == false && PenStream.isPen(event, pointerIndex)) pointerIcon + else super.onResolvePointerIcon(event, pointerIndex) } sessionRoot = root surfaceView = SurfaceView(this) @@ -852,7 +865,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde refreshGestureConfig() root.isMotionEventSplittingEnabled = true surfaceView.setOnTouchListener { _, event -> - if (!event.isFromSource(InputDevice.SOURCE_TOUCHSCREEN)) { + if (!event.isFromSource(InputDevice.SOURCE_TOUCHSCREEN) && PenStream.penIndex(event) < 0) { false } else if (!gestureTouchStreamGate.accepts(GestureTouchStreamGate.Source.SURFACE, event)) { false @@ -1374,6 +1387,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde micEnabled = SessionState.micEnabled, onMicEnabled = { SessionService.setMicEnabled(this@SessionActivity, it) }, touchAuto = getString(if (usingTouchpad()) R.string.session_touch_auto_touchpad else R.string.session_touch_auto_direct), + pen = penSettings.takeIf { penSeen }, + onPen = { key, value -> SessionPrefs.setPenSetting(this@SessionActivity, key, value); readPrefs() }, fexPreset = fexPreset, secondScreenMode = secondScreenMode, secondScreenNone = !bottomOutputActive && !dockDrawer(), @@ -2365,6 +2380,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde textureLodBias = SessionPrefs.textureLodBias(this) val nextTouchMode = SessionPrefs.touchMode(this) if (touchMode != nextTouchMode) { + cancelPen() if (::touchpad.isInitialized) touchpad.cancel() if (::directTouchHold.isInitialized) directTouchHold.cancel() if (::twoFingerGestures.isInitialized) twoFingerGestures.cancel() @@ -2378,6 +2394,11 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde if (nextTouchMode == SessionPrefs.TOUCH_OFF || !usingTouchpad()) cursorVisible = false } touchMode = nextTouchMode + val nextPen = SessionPrefs.pen(this) + if (penSettings != nextPen) cancelPen() + penSettings = nextPen + penStream.sideButton = nextPen.button + if (!penSeen && (SessionPrefs.penSeen(this) || PenStream.penConnected())) notePenSeen() val nextRoutedTouchMode = effectiveTouchMode() if (routedTouchMode != nextRoutedTouchMode) gestureTouchStreamGate.reset() routedTouchMode = nextRoutedTouchMode @@ -2753,9 +2774,20 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde if (::directTouchHold.isInitialized) directTouchHold.cancel() if (::twoFingerGestures.isInitialized) twoFingerGestures.cancel() if (::scrollTouch.isInitialized) scrollTouch.cancel() + if (sessionTouchDown || penDown) dismissTouch = true + cancelPen() scrollTouchGesture = false gesturePointerReady = false - if (sessionTouchDown) dismissTouch = true + } + + private fun cancelPen(releaseKey: Boolean = false) { + penStream.cancel(releaseKey) + penDown = false + } + + private fun notePenSeen() { + penSeen = true + SessionPrefs.setPenSeen(this) } private fun panelBounds(): android.graphics.Rect = if (Build.VERSION.SDK_INT >= 30) { @@ -3219,6 +3251,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde return super.dispatchKeyEvent(event) } } + if (penStream.holdsKey(event) || (penSettings.mode != SessionPrefs.PEN_TOUCH && PenStream.isPenKey(event))) return penStream.onKey(event) if (appTextFocus) return super.dispatchKeyEvent(event) if (!pipUi) { if (physicalInputBindings?.onKey(event, resumed && !drawerOpen && !pcKeyboardOpen && !achievementsOpen && @@ -3404,6 +3437,19 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde if (joyConVr?.onMotionEvent(event) == true) return true if (PadBridge.isControllerMotionEvent(event) && padMouseMode() && padMouse.onMotion(event)) return true if (padBridge?.onMotionEvent(event) == true) return true + val pen = PenStream.penIndex(event) + if (pen >= 0) when (routePen(event, pen)) { + PenRouter.Route.TOUCH -> {} + PenRouter.Route.PEN -> { + if (!appUiOwnsPointer()) return penSample(event, pen) || super.dispatchGenericMotionEvent(event) + if (super.dispatchGenericMotionEvent(event)) { + penStream.cancel() + return true + } + return !touchEditorOpen && penSample(event, pen) + } + else -> return super.dispatchGenericMotionEvent(event) + } if (event.isFromSource(android.view.InputDevice.SOURCE_MOUSE) && !drawerOpen) { if (appUiOwnsPointer()) { if (super.dispatchGenericMotionEvent(event)) return true @@ -3438,7 +3484,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde override fun dispatchTouchEvent(event: MotionEvent): Boolean { if (pipUi) return true notePress(event) - if (event.actionMasked == MotionEvent.ACTION_DOWN && event.isFromSource(InputDevice.SOURCE_TOUCHSCREEN)) { + if (event.actionMasked == MotionEvent.ACTION_DOWN && + (event.isFromSource(InputDevice.SOURCE_TOUCHSCREEN) || PenStream.penIndex(event) >= 0)) { drawerControllerActive = false gestureTouchStreamGate.reset() } @@ -4440,6 +4487,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde } if (next == routedTouchMode) return if (::touchscreenGestures.isInitialized) touchscreenGestures.cancel() + cancelPen() gesturePointerReady = false if (::touchpad.isInitialized) touchpad.cancel() if (::directTouchHold.isInitialized) directTouchHold.cancel() @@ -4608,7 +4656,9 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde private var sessionTouchDown = false override fun onTouchEvent(event: MotionEvent): Boolean { - if (event.isFromSource(android.view.InputDevice.SOURCE_MOUSE)) return onMouse(event) + val pen = PenStream.penIndex(event) + val penRouted = if (pen >= 0) routePen(event, pen).takeIf { it != PenRouter.Route.TOUCH } else null + if (penRouted == null && event.isFromSource(android.view.InputDevice.SOURCE_MOUSE)) return onMouse(event) when (event.actionMasked) { MotionEvent.ACTION_DOWN -> { sessionTouchDown = true @@ -4627,19 +4677,14 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde if (event.actionMasked == MotionEvent.ACTION_UP || event.actionMasked == MotionEvent.ACTION_CANCEL) dismissTouch = false return true } - val uiBlocking = drawerOpen || achievementsOpen || gestureSettingsOpen || inputSettingsOpen || pipUi || SessionState.suspended + if (penRouted != null) return onPenTouch(event, pen, penRouted) + val uiBlocking = uiBlocksTouch() if (event.actionMasked == MotionEvent.ACTION_DOWN && !uiBlocking && routedTouchMode != SessionPrefs.TOUCH_GESTURES) { scrollTouchGesture = scrollTouchMode() scrollRestX = 0f scrollRestY = 0f } - val snapshot = com.droiddeck.launcher.input.TouchRouter.Snapshot( - uiBlocking = uiBlocking, - mode = routedTouchMode, - scrollLayer = scrollTouchGesture, - steamSession = SessionState.mode == SessionService.MODE_STEAM, - steamDesktopUi = SessionState.steamUi == "desktop", - ) + val snapshot = fingerTouchSnapshot(uiBlocking) val route = com.droiddeck.launcher.input.TouchRouter.decide(snapshot) if (route != touchRoute) { Log.i(TAG, "touch route $touchRoute -> $route (mode=$routedTouchMode steamUi=${SessionState.steamUi} scrollLayer=$scrollTouchGesture)") @@ -4667,17 +4712,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde if (threeFingerTap.active || wasActive) return true } if (route == com.droiddeck.launcher.input.TouchRouter.Route.TOUCHPAD) { - val rect = (if (pointerLocked) drawnRect() else pointerSurfaceRect()) ?: return false - if (touchpad.bounds != rect) { - val fresh = touchpad.bounds.width() <= 1f - touchpad.bounds = rect - if (fresh) touchpad.place(rect.centerX(), rect.centerY()) - } - if (event.actionMasked == MotionEvent.ACTION_DOWN) { - touchpad.desktop = SessionState.mode == SessionService.MODE_DESKTOP - placeTouchpadAtGuest(rect) - } - touchpad.onTouch(event) + touchpadTouch(event) } else { dispatchDirectTouch(event) } @@ -4685,6 +4720,92 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde } } + private fun uiBlocksTouch(): Boolean = + drawerOpen || achievementsOpen || gestureSettingsOpen || inputSettingsOpen || pipUi || SessionState.suspended + + private fun fingerTouchSnapshot(uiBlocking: Boolean) = com.droiddeck.launcher.input.TouchRouter.Snapshot( + uiBlocking = uiBlocking, + mode = routedTouchMode, + scrollLayer = scrollTouchGesture, + steamSession = SessionState.mode == SessionService.MODE_STEAM, + steamDesktopUi = SessionState.steamUi == "desktop", + ) + + private fun touchpadTouch(event: MotionEvent): Boolean { + val rect = (if (pointerLocked) drawnRect() else pointerSurfaceRect()) ?: return false + if (touchpad.bounds != rect) { + val fresh = touchpad.bounds.width() <= 1f + touchpad.bounds = rect + if (fresh) touchpad.place(rect.centerX(), rect.centerY()) + } + if (event.actionMasked == MotionEvent.ACTION_DOWN) { + touchpad.desktop = SessionState.mode == SessionService.MODE_DESKTOP + placeTouchpadAtGuest(rect) + } + return touchpad.onTouch(event) + } + + private fun routePen(event: MotionEvent, pen: Int): PenRouter.Route { + if (!penSeen) notePenSeen() + val action = event.actionMasked + if (action == MotionEvent.ACTION_HOVER_ENTER || action == MotionEvent.ACTION_HOVER_MOVE || action == MotionEvent.ACTION_HOVER_EXIT) penDown = false + val own = PenStream.isPenAction(event, pen) + val starts = own && (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_POINTER_DOWN) + val route = penRoute.takeIf { penDown && !starts } ?: decidePen() + if (own) penDown = starts + return route + } + + private fun decidePen(): PenRouter.Route { + val mode = penSettings.mode + val route = PenRouter.decide(uiBlocksTouch(), mode, glassesOutputActive, programLocksPointer()) + if (route != penRoute) { + if (penRoute == PenRouter.Route.PEN) penStream.cancel() + Log.i(TAG, "pen route $penRoute -> $route (mode=$mode)") + penRoute = route + } + return route + } + + private fun onPenTouch(event: MotionEvent, pen: Int, route: PenRouter.Route): Boolean { + if (route == PenRouter.Route.RELATIVE && event.pointerCount > 1 && + com.droiddeck.launcher.input.TouchRouter.decide(fingerTouchSnapshot(uiBlocksTouch())) == + com.droiddeck.launcher.input.TouchRouter.Route.TOUCHPAD) { + touchpadTouch(event) + return true + } + if (event.pointerCount > 1) { + var fingers = 0 + for (index in 0 until event.pointerCount) if (!PenStream.isPen(event, index)) fingers = fingers or (1 shl event.getPointerId(index)) + event.pointerSubset(fingers)?.let { split -> try { onTouchEvent(split) } finally { split.recycle() } } + } + when (route) { + PenRouter.Route.PEN -> penSample(event, pen) + PenRouter.Route.RELATIVE -> { + val split = if (event.pointerCount == 1) MotionEvent.obtain(event) else event.pointerSubset(1 shl event.getPointerId(pen)) + split?.let { + try { + if (it.actionMasked == MotionEvent.ACTION_UP && it.flags and MotionEvent.FLAG_CANCELED != 0) it.action = MotionEvent.ACTION_CANCEL + touchpadTouch(it) + } finally { it.recycle() } + } + } + else -> {} + } + return true + } + + private fun penSample(event: MotionEvent, pen: Int): Boolean { + val area = pointerSurfaceRect() ?: return false + val view = outputView() + if (!penStream.onMotion(event, pen, area, view.width, view.height)) return false + if (penStream.inProximity) { + if (pointerLocked) releaseDesktopMouseGrab() + showCursor(event.getX(pen).coerceIn(area.left, area.right), event.getY(pen).coerceIn(area.top, area.bottom), fromMouse = true) + } + return true + } + private val directTouchSink = object : DirectTouchHold.Touch { override fun down(pointerId: Int, x: Float, y: Float) = sendDirectTouch(0, pointerId, x, y) override fun move(pointerId: Int, x: Float, y: Float) = sendDirectTouch(1, pointerId, x, y) @@ -4770,7 +4891,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde } private fun isHardwareMouse(d: InputDevice?): Boolean = - d != null && !d.isVirtual && (d.supportsSource(InputDevice.SOURCE_MOUSE) || d.supportsSource(InputDevice.SOURCE_MOUSE_RELATIVE)) + d != null && !d.isVirtual && !d.supportsSource(InputDevice.SOURCE_STYLUS) && + (d.supportsSource(InputDevice.SOURCE_MOUSE) || d.supportsSource(InputDevice.SOURCE_MOUSE_RELATIVE)) /** Android also sends the Back and Forward buttons as key events, so they are handled as keys. */ private fun guestMouseButton(actionButton: Int): Int? = when (actionButton) { @@ -4809,7 +4931,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde pointerLocked && SessionState.mode != SessionService.MODE_DESKTOP private fun desktopMouseGrabAllowed(): Boolean = - SessionState.mode == SessionService.MODE_DESKTOP && relativeMouseAttached() && !bottomOutputActive + SessionState.mode == SessionService.MODE_DESKTOP && !penStream.inProximity && relativeMouseAttached() && !bottomOutputActive private fun tapDesktopGrabKey() { val now = android.os.SystemClock.uptimeMillis() @@ -5104,6 +5226,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde private fun releaseHeldGuestInput() { mouseBackInput.cancel() + cancelPen(releaseKey = true) heldButtons.release(1 or 2 or 8 or 16) guestKeysDown.toList().forEach { sendGuestKey(it, false) } heldKeys.release(8 or 16 or 32) @@ -5195,6 +5318,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde scrollTouch.cancel() directTouchHold.cancel() twoFingerGestures.cancel() + cancelPen() guestKeysDown.toList().forEach { sendGuestKey(it, false) } heldKeys.clear() heldButtons.clear() @@ -5391,6 +5515,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde if (::touchpad.isInitialized) touchpad.cancel() if (::directTouchHold.isInitialized) directTouchHold.cancel() if (::twoFingerGestures.isInitialized) twoFingerGestures.cancel() + cancelPen(releaseKey = true) uiHandler.removeCallbacks(clearRecognizedGesture) uiHandler.removeCallbacks(cursorResync) val screenOff = screenIsOff() @@ -5697,7 +5822,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde } if (shown != null) { closeSecondScreen(reset = false) - val screen = com.droiddeck.launcher.ui.BottomGameScreen(this, shown, this, ::onBottomGameTouch) + val screen = com.droiddeck.launcher.ui.BottomGameScreen(this, shown, this, ::onBottomGameTouch, ::onBottomGameHover) bottomGame = screen surfaceW = 0 surfaceH = 0 @@ -5749,9 +5874,23 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde if (surfaceView.holder.surface?.isValid == true) surfaceCreated(surfaceView.holder) } + private fun onBottomGameHover(event: MotionEvent): Boolean { + val pen = PenStream.penIndex(event) + if (pipUi || pen < 0) return false + return when (routePen(event, pen)) { + PenRouter.Route.TOUCH -> false + PenRouter.Route.PEN -> { + notePress(event) + penSample(event, pen) + true + } + else -> true + } + } + private fun onBottomGameTouch(event: MotionEvent): Boolean { if (pipUi) return true - if (!event.isFromSource(InputDevice.SOURCE_TOUCHSCREEN)) return false + if (!event.isFromSource(InputDevice.SOURCE_TOUCHSCREEN) && PenStream.penIndex(event) < 0) return false notePress(event) if (event.actionMasked == MotionEvent.ACTION_DOWN) { drawerControllerActive = false @@ -6052,6 +6191,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde padBridge?.stop() DeckControls.clearRouter(inputRouter) inputRouter = null + cancelPen() heldKeys.clear() heldButtons.clear() steamController?.stop() diff --git a/app/src/main/java/com/droiddeck/launcher/core/DeviceReport.kt b/app/src/main/java/com/droiddeck/launcher/core/DeviceReport.kt index 080ed80c31..3c047d2e13 100644 --- a/app/src/main/java/com/droiddeck/launcher/core/DeviceReport.kt +++ b/app/src/main/java/com/droiddeck/launcher/core/DeviceReport.kt @@ -296,6 +296,8 @@ object DeviceReport { k("Microphone", SessionPrefs.micEnabled(context)) k("On-screen controls", SessionPrefs.oscMode(context)) k("Touch mode", SessionPrefs.touchMode(context)) + k("Pen mode", SessionPrefs.penMode(context)) + k("Pen side button", SessionPrefs.penButton(context)) k("Performance HUD", SessionPrefs.hudEnabled(context)) k("Game storage", when (val g = SessionPrefs.gameStorage(context)) { "" -> "automatic (" + (com.droiddeck.launcher.session.GameStorage.effective(context)?.path ?: "no card present") + ")" diff --git a/app/src/main/java/com/droiddeck/launcher/input/InputSpace.kt b/app/src/main/java/com/droiddeck/launcher/input/InputSpace.kt index 513962b811..90b5da1381 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/InputSpace.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/InputSpace.kt @@ -3,11 +3,16 @@ package com.droiddeck.launcher.input object InputSpace { const val WIDTH = 1920 const val HEIGHT = 1080 + const val FIXED_ONE = 256 fun x(position: Float, viewWidth: Int): Int = scale(position, viewWidth, WIDTH) fun y(position: Float, viewHeight: Int): Int = scale(position, viewHeight, HEIGHT) + fun fixedX(position: Float, viewWidth: Int): Int = scale(position, viewWidth, WIDTH * FIXED_ONE) + + fun fixedY(position: Float, viewHeight: Int): Int = scale(position, viewHeight, HEIGHT * FIXED_ONE) + private fun scale(position: Float, extent: Int, size: Int): Int = if (extent <= 0) 0 else (position / extent * size).toInt().coerceIn(0, size - 1) } diff --git a/app/src/main/java/com/droiddeck/launcher/input/PenRouter.kt b/app/src/main/java/com/droiddeck/launcher/input/PenRouter.kt new file mode 100644 index 0000000000..622fab4dd8 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/input/PenRouter.kt @@ -0,0 +1,14 @@ +package com.droiddeck.launcher.input + +import com.droiddeck.launcher.session.SessionPrefs + +object PenRouter { + enum class Route { BLOCKED, PEN, RELATIVE, TOUCH } + + fun decide(uiBlocking: Boolean, mode: String, glasses: Boolean, programLock: Boolean): Route = when { + uiBlocking -> Route.BLOCKED + mode == SessionPrefs.PEN_TOUCH -> Route.TOUCH + glasses || programLock -> Route.RELATIVE + else -> Route.PEN + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/input/PenStream.kt b/app/src/main/java/com/droiddeck/launcher/input/PenStream.kt new file mode 100644 index 0000000000..67f90a679d --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/input/PenStream.kt @@ -0,0 +1,247 @@ +package com.droiddeck.launcher.input + +import android.graphics.RectF +import android.os.Build +import android.os.Handler +import android.os.Looper +import android.os.SystemClock +import android.view.InputDevice +import android.view.KeyEvent +import android.view.MotionEvent +import com.droiddeck.launcher.session.SessionPrefs + +class PenStream(private val sink: Sink) { + fun interface Sink { + fun sample(flags: Int, x: Int, y: Int) + } + + private val handler = Handler(Looper.getMainLooper()) + private val exitLater = Runnable { if (exitPending) leave() } + private val hoverWatchdog = Runnable { checkHover() } + private var watching = false + private var contact = false + private var barrel = 0 + private var keyDown = false + private var keyDevice = Int.MIN_VALUE + private var keyCode = 0 + private var exitPending = false + private var exitTime = 0L + private var deviceId = Int.MIN_VALUE + private var x = 0 + private var y = 0 + private var sampleTime = 0L + + var sideButton = SessionPrefs.PEN_BUTTON_RIGHT + + var inProximity = false + private set + + fun onMotion(event: MotionEvent, pointer: Int, area: RectF, width: Int, height: Int): Boolean { + val action = event.actionMasked + val time = event.eventTime + val own = isPenAction(event, pointer) + expire(time) + when (action) { + MotionEvent.ACTION_HOVER_ENTER, MotionEvent.ACTION_HOVER_MOVE -> { + continueAt(time) + contact = false + enter(event) + samples(event, pointer, area, width, height) + } + MotionEvent.ACTION_HOVER_EXIT -> if (inProximity && !exitPending && !contact) pendExit(time) + MotionEvent.ACTION_BUTTON_PRESS, MotionEvent.ACTION_BUTTON_RELEASE -> { + continueAt(time) + if (!inProximity) return true + barrel = event.buttonState + samples(event, pointer, area, width, height) + } + MotionEvent.ACTION_DOWN, MotionEvent.ACTION_POINTER_DOWN -> if (own) { + continueAt(time) + enter(event) + contact = true + samples(event, pointer, area, width, height) + } else if (contact) samples(event, pointer, area, width, height) + MotionEvent.ACTION_MOVE -> if (contact) { + barrel = event.buttonState + samples(event, pointer, area, width, height) + } + MotionEvent.ACTION_UP, MotionEvent.ACTION_POINTER_UP -> if (!own) { + if (contact) samples(event, pointer, area, width, height) + } else if (contact) { + if (event.flags and MotionEvent.FLAG_CANCELED != 0) cancel() + else { + barrel = event.buttonState + samples(event, pointer, area, width, height, lift = true) + pendExit(time) + } + } + MotionEvent.ACTION_CANCEL -> cancel() + else -> return false + } + return true + } + + fun onKey(event: KeyEvent): Boolean { + expire(event.eventTime) + val down = event.action == KeyEvent.ACTION_DOWN + val repeat = event.repeatCount > 0 + if (exitPending && !repeat) leave() + if (!inProximity || exitPending) { + if (!repeat && event.deviceId == keyDevice && event.keyCode == keyCode) keyDown = down + return true + } + if (down == keyDown) return true + val before = state() + keyDown = down + if (down) { + keyDevice = event.deviceId + keyCode = event.keyCode + } + if (state() == before) return true + emit(state()) + return true + } + + fun cancel(releaseKey: Boolean = false) { + if (releaseKey) keyDown = false + leave() + } + + fun holdsKey(event: KeyEvent): Boolean = keyDown && event.deviceId == keyDevice && event.keyCode == keyCode + + fun deviceRemoved(deviceId: Int) { + if (deviceId == this.deviceId) cancel() + if (!keyDown || deviceId != keyDevice) return + val before = state() + keyDown = false + if (inProximity && state() != before) emit(state()) + } + + private fun enter(event: MotionEvent) { + barrel = event.buttonState + if (inProximity) return + inProximity = true + deviceId = event.deviceId + } + + private fun continueAt(time: Long) { + if (!exitPending) return + if (time - exitTime > EXIT_GRACE_MS) { leave(); return } + exitPending = false + handler.removeCallbacks(exitLater) + } + + private fun pendExit(time: Long) { + exitPending = true + exitTime = time + handler.removeCallbacks(exitLater) + handler.postDelayed(exitLater, EXIT_GRACE_MS) + } + + private fun expire(time: Long) { + if (inProximity && !contact && time - sampleTime >= HOVER_TIMEOUT_MS) leave() + } + + private fun watchHover(delay: Long = HOVER_TIMEOUT_MS) { + if (watching || contact) return + watching = true + handler.postDelayed(hoverWatchdog, delay) + } + + private fun checkHover() { + watching = false + if (!inProximity || contact || exitPending) return + val silent = SystemClock.uptimeMillis() - sampleTime + if (silent >= HOVER_TIMEOUT_MS) leave() else watchHover(HOVER_TIMEOUT_MS - silent) + } + + private fun leave() { + if (!inProximity) return + emit(0) + handler.removeCallbacks(exitLater) + handler.removeCallbacks(hoverWatchdog) + watching = false + inProximity = false + contact = false + exitPending = false + barrel = 0 + } + + private fun samples(event: MotionEvent, pointer: Int, area: RectF, width: Int, height: Int, lift: Boolean = false) { + for (h in 0 until event.historySize) sample(event, pointer, h, area, width, height) + if (lift) contact = false + sample(event, pointer, -1, area, width, height) + } + + private fun sample(event: MotionEvent, pointer: Int, h: Int, area: RectF, width: Int, height: Int) { + x = InputSpace.fixedX(event.axis(MotionEvent.AXIS_X, pointer, h).coerceIn(area.left, area.right), width) + y = InputSpace.fixedY(event.axis(MotionEvent.AXIS_Y, pointer, h).coerceIn(area.top, area.bottom), height) + sampleTime = if (h < 0) event.eventTime else event.getHistoricalEventTime(h) + emit(state()) + watchHover() + } + + private fun emit(flags: Int) = sink.sample(flags, x, y) + + private fun state(): Int = + PEN_IN or (if (contact) PEN_TIP else 0) or buttonFlags(keyDown || barrel and MotionEvent.BUTTON_STYLUS_PRIMARY != 0, + barrel and MotionEvent.BUTTON_STYLUS_SECONDARY != 0, sideButton) + + private fun MotionEvent.axis(axis: Int, pointer: Int, h: Int): Float = + if (h < 0) getAxisValue(axis, pointer) else getHistoricalAxisValue(axis, pointer, h) + + companion object { + const val PEN_IN = 1 shl 0 + const val PEN_TIP = 1 shl 1 + const val PEN_RIGHT = 1 shl 2 + const val PEN_MIDDLE = 1 shl 3 + const val HOVER_TIMEOUT_MS = 2_000L + const val EXIT_GRACE_MS = 50L + + private val PEN_NAME = Regex("\\b(pen|stylus)", RegexOption.IGNORE_CASE) + + fun isPen(event: MotionEvent, index: Int): Boolean = when (event.getToolType(index)) { + MotionEvent.TOOL_TYPE_STYLUS, MotionEvent.TOOL_TYPE_ERASER -> true + MotionEvent.TOOL_TYPE_UNKNOWN -> event.isFromSource(InputDevice.SOURCE_STYLUS) + else -> false + } + + fun penConnected(): Boolean = + InputDevice.getDeviceIds().any { id -> InputDevice.getDevice(id)?.let { isPenDevice(it.sources) } == true } + + // Before Android 14 a touchscreen that declares ABS_MT_TOOL_TYPE reports SOURCE_STYLUS without any pen. + fun isPenDevice(sources: Int, sdk: Int = Build.VERSION.SDK_INT): Boolean = + sources and InputDevice.SOURCE_STYLUS == InputDevice.SOURCE_STYLUS && + (sources and InputDevice.SOURCE_TOUCHSCREEN != InputDevice.SOURCE_TOUCHSCREEN || sdk >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) + + fun penIndex(event: MotionEvent): Int { + for (index in 0 until event.pointerCount) if (isPen(event, index)) return index + return -1 + } + + fun isPenAction(event: MotionEvent, index: Int): Boolean = when (event.actionMasked) { + MotionEvent.ACTION_DOWN, MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> true + MotionEvent.ACTION_POINTER_DOWN, MotionEvent.ACTION_POINTER_UP -> event.actionIndex == index + else -> false + } + + fun isPenKey(event: KeyEvent): Boolean { + val device = event.device ?: return false + return isPenKeyboard(device.name, device.sources, Build.VERSION.SDK_INT < Build.VERSION_CODES.Q || device.isExternal, + device.keyboardType == InputDevice.KEYBOARD_TYPE_ALPHABETIC) + } + + fun isPenKeyboard(name: String?, sources: Int, external: Boolean, alphabetic: Boolean): Boolean = + external && !alphabetic && sources == InputDevice.SOURCE_KEYBOARD && name != null && PEN_NAME.containsMatchIn(name) + + fun buttonFlags(primary: Boolean, secondary: Boolean, sideButton: String): Int { + val first = when (sideButton) { + SessionPrefs.PEN_BUTTON_RIGHT -> PEN_RIGHT + SessionPrefs.PEN_BUTTON_MIDDLE -> PEN_MIDDLE + else -> 0 + } + val second = if (sideButton == SessionPrefs.PEN_BUTTON_RIGHT) PEN_MIDDLE else PEN_RIGHT + return (if (primary) first else 0) or (if (secondary) second else 0) + } + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/input/PointerSubset.kt b/app/src/main/java/com/droiddeck/launcher/input/PointerSubset.kt index d6d990ea8f..badf6ea95b 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/PointerSubset.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/PointerSubset.kt @@ -23,13 +23,20 @@ internal fun MotionEvent.pointerSubset(pointerIdBits: Int): MotionEvent? { } val properties = Array(indices.size) { MotionEvent.PointerProperties() } val coordinates = Array(indices.size) { MotionEvent.PointerCoords() } - indices.forEachIndexed { target, index -> - getPointerProperties(index, properties[target]) - getPointerCoords(index, coordinates[target]) + indices.forEachIndexed { target, index -> getPointerProperties(index, properties[target]) } + fun sample(position: Int): Array { + indices.forEachIndexed { target, index -> + if (position < historySize) getHistoricalPointerCoords(index, position, coordinates[target]) + else getPointerCoords(index, coordinates[target]) + } + return coordinates } - return MotionEvent.obtain( - downTime, eventTime, splitAction, indices.size, properties, coordinates, + fun time(position: Int) = if (position < historySize) getHistoricalEventTime(position) else eventTime + val split = MotionEvent.obtain( + downTime, time(0), splitAction, indices.size, properties, sample(0), metaState, buttonState, xPrecision, yPrecision, deviceId, edgeFlags, source, flags, ) + for (position in 1..historySize) split.addBatch(time(position), sample(position), metaState) + return split } diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt index 6886f0fb3d..1ef1e5f592 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt @@ -516,6 +516,38 @@ object SessionPrefs { prefs(context).edit().putString("touch", mode).apply() } + const val PEN_MOUSE = "mouse" + const val PEN_TOUCH = "touch" + const val PEN_BUTTON_RIGHT = "right" + const val PEN_BUTTON_MIDDLE = "middle" + const val PEN_BUTTON_OFF = "off" + + data class Pen(val mode: String = PEN_MOUSE, val button: String = PEN_BUTTON_RIGHT) + + private val PEN_SETTINGS = mapOf( + "pen" to setOf(PEN_MOUSE, PEN_TOUCH), + "penButton" to setOf(PEN_BUTTON_RIGHT, PEN_BUTTON_MIDDLE, PEN_BUTTON_OFF), + ) + + private fun penSetting(context: Context, key: String, default: String): String = + prefs(context).getString(key, default)?.takeIf { it in PEN_SETTINGS.getValue(key) } ?: default + + fun setPenSetting(context: Context, key: String, value: String) { + require(value in PEN_SETTINGS.getValue(key)) + prefs(context).edit().putString(key, value).apply() + } + + fun penMode(context: Context): String = penSetting(context, "pen", PEN_MOUSE) + fun penButton(context: Context): String = penSetting(context, "penButton", PEN_BUTTON_RIGHT) + + fun pen(context: Context): Pen = Pen(penMode(context), penButton(context)) + + fun penSeen(context: Context): Boolean = prefs(context).getBoolean("penSeen", false) + + fun setPenSeen(context: Context) { + if (!penSeen(context)) prefs(context).edit().putBoolean("penSeen", true).apply() + } + const val SHAPE_AUTO = "auto" const val SHAPE_WIDE = "16:9" const val SHAPE_EXACT = "exact" diff --git a/app/src/main/java/com/droiddeck/launcher/session/SettingsReset.kt b/app/src/main/java/com/droiddeck/launcher/session/SettingsReset.kt index 17696c44a3..c851a4458b 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SettingsReset.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SettingsReset.kt @@ -16,7 +16,7 @@ object SettingsReset { "dualScreenMode", "dualScreenSessionMode", "dualScreenLauncher", "dualScreenSessionControls", "androidTextFieldKeyboard", "gameStoresEnabled", "soundtracksEnabled", "soundtracksIngame", "gameStoresSpeedTier", "gameStoresParallel", "storesOpenTab", "storesShowMature", "storesInstallLanguage", - "hud", "hudDeck", "hudFps", "hudBattery", "hudGraph", "hudColor", "hudOpacity", "__KEEP_droidDeckHud__", "__KEEP_droidDeckHudPos__", "backHold", "backActionsInverted", "backDoubleTapMs", "touch", "osc", "shape", "steamController", + "hud", "hudDeck", "hudFps", "hudBattery", "hudGraph", "hudColor", "hudOpacity", "__KEEP_droidDeckHud__", "__KEEP_droidDeckHudPos__", "backHold", "backActionsInverted", "backDoubleTapMs", "touch", "pen", "penButton", "osc", "shape", "steamController", "gamepadLeft", "gamepadRight", "pipAutoEnter", "unfoldedControls", "unfoldedControlsWithController", "stretch16x9", "picturePosition", "railLabels", "agentTab", "navTabOrder", "navTabShelved", "navTabHidden", "pcKeyboardSize", "pcKeyboardOpacity", "pcKeyboardHiddenKeys", "gpuDriverMode", "androidDriver", "linuxDriver", "linuxDriver.steam", "linuxDriver.desktop", diff --git a/app/src/main/java/com/droiddeck/launcher/ui/BottomGameScreen.kt b/app/src/main/java/com/droiddeck/launcher/ui/BottomGameScreen.kt index ba5b5af126..046142ad1d 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/BottomGameScreen.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/BottomGameScreen.kt @@ -19,6 +19,7 @@ class BottomGameScreen( display: Display, private val callback: SurfaceHolder.Callback, private val onTouch: (MotionEvent) -> Boolean, + private val onHover: (MotionEvent) -> Boolean, ) : Presentation(activity, display) { val surfaceView = SurfaceView(getContext()) val holder: SurfaceHolder get() = surfaceView.holder @@ -51,7 +52,7 @@ class BottomGameScreen( activity.dispatchKeyEvent(event) || super.dispatchKeyEvent(event) override fun dispatchGenericMotionEvent(event: MotionEvent): Boolean = - activity.dispatchGenericMotionEvent(event) || super.dispatchGenericMotionEvent(event) + onHover(event) || activity.dispatchGenericMotionEvent(event) || super.dispatchGenericMotionEvent(event) } object GameScreenSwap { diff --git a/app/src/main/java/com/droiddeck/launcher/ui/ModeSettingsDialog.kt b/app/src/main/java/com/droiddeck/launcher/ui/ModeSettingsDialog.kt index 70fd435c94..4c882ab1e9 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/ModeSettingsDialog.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/ModeSettingsDialog.kt @@ -63,6 +63,7 @@ class ModeSettings( val upscaler: Int = 0, val upscaleSharpness: Int = 75, val touchMode: String, + val pen: SessionPrefs.Pen? = null, val suspendPolicy: String, val steamDownloadsInBackground: Boolean = false, val pipSupported: Boolean = false, @@ -116,6 +117,7 @@ class ModeSettingsActions( val onUpscaler: (Int) -> Unit = {}, val onUpscaleSharpness: (Int) -> Unit = {}, val onTouch: (String) -> Unit, + val onPen: (key: String, value: String) -> Unit = { _, _ -> }, val onSuspendPolicy: (String) -> Unit, val onSteamDownloadsInBackground: (Boolean) -> Unit = {}, val onPipAutoEnter: (Boolean) -> Unit = {}, @@ -321,6 +323,7 @@ fun ModeSheetRows(s: ModeSettings, a: ModeSettingsActions, tab: String, extras: } PhysicalShortcutRows() } + s.pen?.let { PenSettingsGroup(host, it, onPick = a.onPen) } } if (tab == SheetTab.AUDIO && steam) { if (s.directAudio != null && s.mic != null) SettingsGroup(stringResource(R.string.mode_audio)) { diff --git a/app/src/main/java/com/droiddeck/launcher/ui/PenSettingsGroup.kt b/app/src/main/java/com/droiddeck/launcher/ui/PenSettingsGroup.kt new file mode 100644 index 0000000000..646c8cfcc6 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/ui/PenSettingsGroup.kt @@ -0,0 +1,25 @@ +package com.droiddeck.launcher.ui + +import androidx.compose.runtime.Composable +import androidx.compose.ui.Modifier +import androidx.compose.ui.res.stringResource +import com.droiddeck.launcher.R +import com.droiddeck.launcher.session.SessionPrefs + +@Composable +internal fun PenSettingsGroup( + host: MenuHost, pen: SessionPrefs.Pen, track: (String) -> Modifier = { Modifier }, + onPick: (key: String, value: String) -> Unit, +) { + SettingsGroup(stringResource(R.string.mode_pen)) { + ChoiceRow(host, "pen", stringResource(R.string.mode_pen), null, + listOf(SessionPrefs.PEN_MOUSE to stringResource(R.string.mode_pen_mouse), + SessionPrefs.PEN_TOUCH to stringResource(R.string.mode_pen_touch)), + pen.mode, chipModifier = track("pen")) { onPick("pen", it) } + if (pen.mode != SessionPrefs.PEN_TOUCH) ChoiceRow(host, "pen-button", stringResource(R.string.mode_pen_button), null, + listOf(SessionPrefs.PEN_BUTTON_RIGHT to stringResource(R.string.gesture_right_click), + SessionPrefs.PEN_BUTTON_MIDDLE to stringResource(R.string.gesture_middle_click), + SessionPrefs.PEN_BUTTON_OFF to stringResource(R.string.widgets_off)), + pen.button, chipModifier = track("pen-button")) { onPick("penButton", it) } + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/ui/SessionOverlay.kt b/app/src/main/java/com/droiddeck/launcher/ui/SessionOverlay.kt index 90a2ba884f..7174fd8b25 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/SessionOverlay.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/SessionOverlay.kt @@ -239,6 +239,8 @@ class DrawerActions( val onPadType: (String) -> Unit = {}, val touchMode: String, val touchAuto: String, + val pen: SessionPrefs.Pen? = null, + val onPen: (key: String, value: String) -> Unit = { _, _ -> }, val fexPreset: String, val upscaler: Int = 0, val upscaleSharpness: Int = 75, @@ -672,6 +674,7 @@ internal fun SessionDrawer(open: Boolean, requestedPage: SessionDrawerPage, cont onPick = a.onDirectHoldBehavior) } } + a.pen?.let { PenSettingsGroup(host, it, track = { key -> focus.track(page, key) }, onPick = a.onPen) } SettingsGroup(stringResource(R.string.input_screen_visibility)) { ChoiceRow(host, "osc", stringResource(R.string.mode_osc), null, if (a.steam) listOf(SessionPrefs.OSC_AUTO to stringResource(R.string.common_auto), SessionPrefs.OSC_ALWAYS to stringResource(R.string.common_always), SessionPrefs.OSC_STEAM_TOUCH to stringResource(R.string.osc_steam_touch), SessionPrefs.OSC_STEAM_QAM to stringResource(R.string.mode_osc_qam), SessionPrefs.OSC_NEVER to stringResource(R.string.common_never)) diff --git a/app/src/main/java/com/droiddeck/launcher/wayland/WaylandCompositor.java b/app/src/main/java/com/droiddeck/launcher/wayland/WaylandCompositor.java index 895a48476f..d348c49d31 100644 --- a/app/src/main/java/com/droiddeck/launcher/wayland/WaylandCompositor.java +++ b/app/src/main/java/com/droiddeck/launcher/wayland/WaylandCompositor.java @@ -162,6 +162,8 @@ public final class WaylandCompositor { public static native void nativeSendTouch(int action, int pointerId, int x, int y); + public static native void nativeSendPen(int flags, int x, int y); + public static native int nativeCursorSnapshot(int[] out); public static native int nativeCursorSerial(); diff --git a/app/src/main/res/values-ar/strings.xml b/app/src/main/res/values-ar/strings.xml index 0efb6a3d98..c727bb56de 100644 --- a/app/src/main/res/values-ar/strings.xml +++ b/app/src/main/res/values-ar/strings.xml @@ -269,6 +269,10 @@ لوحة اللمس مباشر تلقائي: لوحة لمس على سطح المكتب، ولمس مباشر في Steam. + القلم + الفأرة + اللمس + الزر الجانبي عناصر التحكم على الشاشة Steam + QAM يعرض الخيار «تلقائي» عناصر التحكم عند عدم توصيل وحدة تحكم. diff --git a/app/src/main/res/values-b+es+419/strings.xml b/app/src/main/res/values-b+es+419/strings.xml index c5277a46cd..4efea900f0 100644 --- a/app/src/main/res/values-b+es+419/strings.xml +++ b/app/src/main/res/values-b+es+419/strings.xml @@ -269,6 +269,10 @@ Touchpad Directo Auto: touchpad en Escritorio, directo en Steam. + Lápiz + Ratón + Táctil + Botón lateral Controles en pantalla Steam + QAM Auto muestra los controles en pantalla cuando no hay ningún control conectado. diff --git a/app/src/main/res/values-bg/strings.xml b/app/src/main/res/values-bg/strings.xml index 21c12beb44..00e4828713 100644 --- a/app/src/main/res/values-bg/strings.xml +++ b/app/src/main/res/values-bg/strings.xml @@ -269,6 +269,10 @@ Тъчпад Директно Автоматично: тъчпад на работния плот, директно в Steam. + Писалка + Мишка + Докосване + Страничен бутон Екранни контроли Steam + QAM Автоматично показва контролите, когато няма свързан контролер. diff --git a/app/src/main/res/values-cs/strings.xml b/app/src/main/res/values-cs/strings.xml index d31c8ccfad..4975ad28e9 100644 --- a/app/src/main/res/values-cs/strings.xml +++ b/app/src/main/res/values-cs/strings.xml @@ -269,6 +269,10 @@ Touchpad Přímé Automaticky: touchpad na Ploše, přímo ve Steamu. + Pero + Myš + Dotyk + Boční tlačítko Ovládání na obrazovce Steam + QAM Automaticky zobrazí ovládání, pokud není připojen žádný ovladač. diff --git a/app/src/main/res/values-da/strings.xml b/app/src/main/res/values-da/strings.xml index 49f73e8841..f4e50fcf52 100644 --- a/app/src/main/res/values-da/strings.xml +++ b/app/src/main/res/values-da/strings.xml @@ -269,6 +269,10 @@ Touchpad Direkte Auto: touchpad på Skrivebord, direkte i Steam. + Pen + Mus + Berøring + Sideknap Kontroller på skærmen Steam + QAM Auto viser kontrollerne, når ingen controller er tilsluttet. diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index ce94b712c6..42b94372bb 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -269,6 +269,10 @@ Touchpad Direkt Auto: Touchpad auf dem Desktop, direkt in Steam. + Stift + Maus + Touch + Seitentaste Bildschirmsteuerung Steam + QAM Auto zeigt die Steuerung an, wenn kein Controller verbunden ist. diff --git a/app/src/main/res/values-el/strings.xml b/app/src/main/res/values-el/strings.xml index 7189d25ce6..b134c56bd3 100644 --- a/app/src/main/res/values-el/strings.xml +++ b/app/src/main/res/values-el/strings.xml @@ -269,6 +269,10 @@ Πινακίδα αφής Απευθείας Αυτόματο: πινακίδα αφής στην Επιφάνεια εργασίας, απευθείας στο Steam. + Γραφίδα + Ποντίκι + Αφή + Πλαϊνό κουμπί Χειριστήρια στην οθόνη Steam + QAM Το Αυτόματο εμφανίζει τα χειριστήρια όταν δεν είναι συνδεδεμένο χειριστήριο. diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml index 9c12d314e8..ab98f22cf8 100644 --- a/app/src/main/res/values-es/strings.xml +++ b/app/src/main/res/values-es/strings.xml @@ -269,6 +269,10 @@ Touchpad Directo Auto: touchpad en Escritorio, directo en Steam. + Lápiz + Ratón + Táctil + Botón lateral Controles en pantalla Steam + QAM Auto muestra los controles cuando no hay ningún mando conectado. diff --git a/app/src/main/res/values-fi/strings.xml b/app/src/main/res/values-fi/strings.xml index d3c9c2a6a7..682c4eeeef 100644 --- a/app/src/main/res/values-fi/strings.xml +++ b/app/src/main/res/values-fi/strings.xml @@ -269,6 +269,10 @@ Kosketuslevy Suora Automaattinen: kosketuslevy Työpöydällä, suora Steamissa. + Kynä + Hiiri + Kosketus + Sivupainike Näytön ohjaimet Steam + QAM Automaattinen näyttää ohjaimet, kun yhtään ohjainta ei ole kytketty. diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index fd2a751806..f0aaf7dae6 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -269,6 +269,10 @@ Pavé tactile Direct Auto : pavé tactile pour le bureau, tactile direct pour Steam. + Stylet + Souris + Tactile + Bouton latéral Commandes à l\'écran Steam + QAM Le mode automatique affiche les commandes à l\'écran lorsqu\'aucune manette n\'est connectée. diff --git a/app/src/main/res/values-hi/strings.xml b/app/src/main/res/values-hi/strings.xml index ab18cf97f7..6aa2f1b3c2 100644 --- a/app/src/main/res/values-hi/strings.xml +++ b/app/src/main/res/values-hi/strings.xml @@ -269,6 +269,10 @@ टचपैड डायरेक्ट ऑटो: डेस्कटॉप पर टचपैड, Steam में डायरेक्ट। + पेन + माउस + टच + साइड बटन ऑन-स्क्रीन कंट्रोल Steam + QAM कोई कंट्रोलर कनेक्ट न होने पर ऑटो कंट्रोल दिखाता है। diff --git a/app/src/main/res/values-hu/strings.xml b/app/src/main/res/values-hu/strings.xml index 4dc1f7ab25..5b7efc62c4 100644 --- a/app/src/main/res/values-hu/strings.xml +++ b/app/src/main/res/values-hu/strings.xml @@ -269,6 +269,10 @@ Érintőpad Közvetlen Automatikus: érintőpad az Asztalon, közvetlen a Steamben. + Toll + Egér + Érintés + Oldalsó gomb Képernyőn megjelenő vezérlők Steam + QAM Az Automatikus megjeleníti a vezérlőket, ha nincs csatlakoztatott kontroller. diff --git a/app/src/main/res/values-in/strings.xml b/app/src/main/res/values-in/strings.xml index 193ee742f7..12c28892e3 100644 --- a/app/src/main/res/values-in/strings.xml +++ b/app/src/main/res/values-in/strings.xml @@ -269,6 +269,10 @@ Touchpad Langsung Otomatis: touchpad di Desktop, langsung di Steam. + Pena + Mouse + Sentuhan + Tombol samping Kontrol di layar Steam + QAM Otomatis menampilkan kontrol saat tidak ada pengontrol yang terhubung. diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xml index 5bd4191615..3c830c5560 100644 --- a/app/src/main/res/values-it/strings.xml +++ b/app/src/main/res/values-it/strings.xml @@ -269,6 +269,10 @@ Touchpad Diretto Auto: touchpad sul Desktop, diretto in Steam. + Penna + Mouse + Tocco + Tasto laterale Controlli su schermo Steam + QAM Auto mostra i controlli quando non è collegato nessun controller. diff --git a/app/src/main/res/values-ja/strings.xml b/app/src/main/res/values-ja/strings.xml index 188a1f3afd..cf673d8800 100644 --- a/app/src/main/res/values-ja/strings.xml +++ b/app/src/main/res/values-ja/strings.xml @@ -269,6 +269,10 @@ トラックパッド ダイレクト 自動:デスクトップではトラックパッド、Steamではダイレクトタッチを使用します。 + ペン + マウス + タッチ + サイドボタン 画面コントローラー Steam + QAM 自動では、コントローラーが接続されていないときに画面コントローラーを表示します。 diff --git a/app/src/main/res/values-ko/strings.xml b/app/src/main/res/values-ko/strings.xml index 388b0fbf9b..558894cf18 100644 --- a/app/src/main/res/values-ko/strings.xml +++ b/app/src/main/res/values-ko/strings.xml @@ -269,6 +269,10 @@ 터치패드 직접 터치 자동: 데스크톱에서는 터치패드, Steam에서는 직접 터치를 사용합니다. + 펜 + 마우스 + 터치 + 측면 버튼 화면 컨트롤 Steam + QAM 자동 모드에서는 컨트롤러가 연결되지 않은 경우 화면 컨트롤을 표시합니다. diff --git a/app/src/main/res/values-ms/strings.xml b/app/src/main/res/values-ms/strings.xml index 0539110e2a..fc2b93e758 100644 --- a/app/src/main/res/values-ms/strings.xml +++ b/app/src/main/res/values-ms/strings.xml @@ -269,6 +269,10 @@ Pad sentuh Terus Auto: pad sentuh pada Desktop, terus dalam Steam. + Pen + Tetikus + Sentuhan + Butang sisi Kawalan pada skrin Steam + QAM Auto memaparkan kawalan apabila tiada pengawal disambungkan. diff --git a/app/src/main/res/values-nb/strings.xml b/app/src/main/res/values-nb/strings.xml index 186b730e79..2d071b6811 100644 --- a/app/src/main/res/values-nb/strings.xml +++ b/app/src/main/res/values-nb/strings.xml @@ -269,6 +269,10 @@ Styreflate Direkte Auto: styreflate på Skrivebord, direkte i Steam. + Penn + Mus + Berøring + Sideknapp Skjermkontroller Steam + QAM Auto viser kontrollene når ingen kontroller er tilkoblet. diff --git a/app/src/main/res/values-nl/strings.xml b/app/src/main/res/values-nl/strings.xml index 32d84c5b0d..2ed2805c76 100644 --- a/app/src/main/res/values-nl/strings.xml +++ b/app/src/main/res/values-nl/strings.xml @@ -269,6 +269,10 @@ Touchpad Direct Automatisch: touchpad op Desktop, direct in Steam. + Pen + Muis + Aanraken + Zijknop Schermbediening Steam + QAM Automatisch toont de bediening als er geen controller is verbonden. diff --git a/app/src/main/res/values-pl/strings.xml b/app/src/main/res/values-pl/strings.xml index f0aad30bf5..9c9baa4442 100644 --- a/app/src/main/res/values-pl/strings.xml +++ b/app/src/main/res/values-pl/strings.xml @@ -269,6 +269,10 @@ Touchpad Bezpośredni Auto: touchpad na pulpicie, bezpośredni w Steam. + Rysik + Mysz + Dotyk + Przycisk boczny Sterowanie ekranowe Steam + QAM Auto pokazuje sterowanie, gdy nie jest podłączony żaden kontroler. diff --git a/app/src/main/res/values-pt-rPT/strings.xml b/app/src/main/res/values-pt-rPT/strings.xml index 25b5b3a69b..ce3be30b49 100644 --- a/app/src/main/res/values-pt-rPT/strings.xml +++ b/app/src/main/res/values-pt-rPT/strings.xml @@ -269,6 +269,10 @@ Painel tátil Direto Automático: painel tátil no Ambiente de trabalho, direto no Steam. + Caneta + Rato + Toque + Botão lateral Controlos no ecrã Steam + QAM Em Automático, os controlos aparecem quando não há nenhum comando ligado. diff --git a/app/src/main/res/values-pt/strings.xml b/app/src/main/res/values-pt/strings.xml index 5a7942708f..7f57fcfecf 100644 --- a/app/src/main/res/values-pt/strings.xml +++ b/app/src/main/res/values-pt/strings.xml @@ -269,6 +269,10 @@ Touchpad Direto Automático: touchpad na Área de trabalho, direto na Steam. + Caneta + Mouse + Toque + Botão lateral Controles na tela Steam + QAM No Automático, os controles na tela aparecem quando não há um controle conectado. diff --git a/app/src/main/res/values-ro/strings.xml b/app/src/main/res/values-ro/strings.xml index 1c84500c16..753a09a44c 100644 --- a/app/src/main/res/values-ro/strings.xml +++ b/app/src/main/res/values-ro/strings.xml @@ -269,6 +269,10 @@ Touchpad Direct Automat: touchpad pe Desktop, direct în Steam. + Stilou + Mouse + Atingere + Buton lateral Comenzi pe ecran Steam + QAM Automat afișează comenzile când niciun controller nu este conectat. diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml index 73ff1ab7d7..f2a009ca15 100644 --- a/app/src/main/res/values-ru/strings.xml +++ b/app/src/main/res/values-ru/strings.xml @@ -269,6 +269,10 @@ Тачпад Прямой Авто: тачпад для рабочего стола, прямой сенсорный ввод для Steam. + Перо + Мышь + Сенсорный ввод + Боковая кнопка Экранные элементы управления Steam + QAM Автоматический режим показывает экранные элементы управления, если контроллер не подключен. diff --git a/app/src/main/res/values-sv/strings.xml b/app/src/main/res/values-sv/strings.xml index 0459e70db0..34a9af51a3 100644 --- a/app/src/main/res/values-sv/strings.xml +++ b/app/src/main/res/values-sv/strings.xml @@ -269,6 +269,10 @@ Pekplatta Direkt Auto: pekplatta på skrivbordet, direkt i Steam. + Penna + Mus + Pekskärm + Sidoknapp Skärmkontroller Steam + QAM Auto visar kontrollerna när ingen handkontroll är ansluten. diff --git a/app/src/main/res/values-th/strings.xml b/app/src/main/res/values-th/strings.xml index b27791fe29..c582fb2d9c 100644 --- a/app/src/main/res/values-th/strings.xml +++ b/app/src/main/res/values-th/strings.xml @@ -269,6 +269,10 @@ ทัชแพด โดยตรง อัตโนมัติ: ทัชแพดในเดสก์ท็อป โดยตรงใน Steam + ปากกา + เมาส์ + การสัมผัส + ปุ่มด้านข้าง ปุ่มควบคุมบนหน้าจอ Steam + QAM อัตโนมัติจะแสดงปุ่มควบคุมเมื่อไม่มีคอนโทรลเลอร์เชื่อมต่ออยู่ diff --git a/app/src/main/res/values-tr/strings.xml b/app/src/main/res/values-tr/strings.xml index 3af54ba1eb..091423dc4d 100644 --- a/app/src/main/res/values-tr/strings.xml +++ b/app/src/main/res/values-tr/strings.xml @@ -269,6 +269,10 @@ Dokunmatik yüzey Doğrudan Otomatik: Masaüstünde dokunmatik yüzey, Steam\'de doğrudan. + Kalem + Fare + Dokunma + Yan düğme Ekran üstü kontroller Steam + QAM Otomatik, kumanda bağlı değilken kontrolleri gösterir. diff --git a/app/src/main/res/values-uk/strings.xml b/app/src/main/res/values-uk/strings.xml index a05c56f0a9..12a675a0be 100644 --- a/app/src/main/res/values-uk/strings.xml +++ b/app/src/main/res/values-uk/strings.xml @@ -269,6 +269,10 @@ Сенсорна панель Пряме «Авто»: сенсорна панель на робочому столі, пряме введення в Steam. + Перо + Миша + Сенсорне введення + Бічна кнопка Екранне керування Steam + QAM «Авто» показує екранне керування, коли контролер не під’єднано. diff --git a/app/src/main/res/values-vi/strings.xml b/app/src/main/res/values-vi/strings.xml index b1ea497c9f..ee8dfd4c49 100644 --- a/app/src/main/res/values-vi/strings.xml +++ b/app/src/main/res/values-vi/strings.xml @@ -269,6 +269,10 @@ Touchpad Trực tiếp Tự động: touchpad ở Desktop, trực tiếp ở Steam. + Bút + Chuột + Chạm + Nút bên Điều khiển trên màn hình Steam + QAM Tự động hiện các nút điều khiển khi không có tay cầm kết nối. diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index 1840a4706d..c8bbecff77 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -269,6 +269,10 @@ 触控板 直接 自动:桌面用触控板,Steam 中用直接触摸。 + 手写笔 + 鼠标 + 触摸 + 侧键 屏幕控制 Steam + QAM 自动模式会在未连接手柄时显示屏幕控制。 diff --git a/app/src/main/res/values-zh-rHK/strings.xml b/app/src/main/res/values-zh-rHK/strings.xml index c902f1629c..a0632feceb 100644 --- a/app/src/main/res/values-zh-rHK/strings.xml +++ b/app/src/main/res/values-zh-rHK/strings.xml @@ -269,6 +269,10 @@ 觸控板 直接 自動:桌面使用觸控板,Steam 使用直接觸控。 + 觸控筆 + 滑鼠 + 觸控 + 側鍵 螢幕控制 Steam + QAM 「自動」會在未連接控制器時顯示螢幕控制。 diff --git a/app/src/main/res/values-zh-rTW/strings.xml b/app/src/main/res/values-zh-rTW/strings.xml index e23fe7fda9..3dd5aa0655 100644 --- a/app/src/main/res/values-zh-rTW/strings.xml +++ b/app/src/main/res/values-zh-rTW/strings.xml @@ -269,6 +269,10 @@ 觸控板 直接 自動:桌面使用觸控板,Steam 使用直接觸控。 + 觸控筆 + 滑鼠 + 觸控 + 側鍵 螢幕控制 Steam + QAM 自動模式會在未連接控制器時顯示螢幕控制。 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5193d6f7fd..e0553b82eb 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -297,6 +297,10 @@ Touchpad Direct Auto: touchpad on Desktop, direct in Steam. + Pen + Mouse + Touch + Side button On-screen controls Steam + QAM Auto shows the controls when no controller is connected. diff --git a/app/src/test/java/com/droiddeck/launcher/input/InputSpaceTest.kt b/app/src/test/java/com/droiddeck/launcher/input/InputSpaceTest.kt index c8c63c0444..fa7d043028 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/InputSpaceTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/InputSpaceTest.kt @@ -40,4 +40,14 @@ class InputSpaceTest { assertEquals(InputSpace.HEIGHT - 1, InputSpace.y(5000f, 1504)) assertEquals(0, InputSpace.y(10f, 0)) } + + @Test fun fixedPositionsKeepTheFractionOfAnInputUnit() { + assertEquals(960 * 256, InputSpace.fixedX(1200f, 2400)) + assertEquals(1700, InputSpace.fixedX(5.534f, 1600)) + assertEquals(InputSpace.x(5.534f, 1600), InputSpace.fixedX(5.534f, 1600) / InputSpace.FIXED_ONE) + assertEquals(InputSpace.WIDTH * InputSpace.FIXED_ONE - 1, InputSpace.fixedX(2400f, 2400)) + assertEquals(InputSpace.HEIGHT * InputSpace.FIXED_ONE - 1, InputSpace.fixedY(5000f, 1504)) + assertEquals(0, InputSpace.fixedY(-1f, 1504)) + assertEquals(0, InputSpace.fixedY(10f, 0)) + } } diff --git a/app/src/test/java/com/droiddeck/launcher/input/PenRouterTest.kt b/app/src/test/java/com/droiddeck/launcher/input/PenRouterTest.kt new file mode 100644 index 0000000000..6347a4470c --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/input/PenRouterTest.kt @@ -0,0 +1,27 @@ +package com.droiddeck.launcher.input + +import com.droiddeck.launcher.session.SessionPrefs +import org.junit.Assert.assertEquals +import org.junit.Test + +class PenRouterTest { + @Test fun appUiBlocksThePenInEveryModeAndScreen() { + for (mode in listOf(SessionPrefs.PEN_MOUSE, SessionPrefs.PEN_TOUCH)) + for (glasses in listOf(false, true)) for (lock in listOf(false, true)) + assertEquals("$mode $glasses $lock", PenRouter.Route.BLOCKED, PenRouter.decide(true, mode, glasses, lock)) + } + + @Test fun touchModeKeepsThePenAFingerEverywhere() { + for (glasses in listOf(false, true)) for (lock in listOf(false, true)) + assertEquals("$glasses $lock", PenRouter.Route.TOUCH, PenRouter.decide(false, SessionPrefs.PEN_TOUCH, glasses, lock)) + } + + @Test fun glassesAndProgramLocksMakeTheMouseRelative() { + for ((glasses, lock) in listOf(true to false, false to true, true to true)) + assertEquals("$glasses $lock", PenRouter.Route.RELATIVE, PenRouter.decide(false, SessionPrefs.PEN_MOUSE, glasses, lock)) + } + + @Test fun mouseDrivesThePenRouteOnThePhone() { + assertEquals(PenRouter.Route.PEN, PenRouter.decide(false, SessionPrefs.PEN_MOUSE, false, false)) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/input/PenStreamTest.kt b/app/src/test/java/com/droiddeck/launcher/input/PenStreamTest.kt new file mode 100644 index 0000000000..69ee6a636f --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/input/PenStreamTest.kt @@ -0,0 +1,384 @@ +package com.droiddeck.launcher.input + +import android.graphics.RectF +import android.os.Looper +import android.os.SystemClock +import android.view.InputDevice +import android.view.KeyEvent +import android.view.MotionEvent +import com.droiddeck.launcher.input.PenStream.Companion.PEN_IN +import com.droiddeck.launcher.input.PenStream.Companion.PEN_MIDDLE +import com.droiddeck.launcher.input.PenStream.Companion.PEN_RIGHT +import com.droiddeck.launcher.input.PenStream.Companion.PEN_TIP +import com.droiddeck.launcher.session.SessionPrefs +import java.time.Duration +import org.junit.Assert.* +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.Shadows.shadowOf +import org.robolectric.annotation.Config + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [28], manifest = Config.NONE) +class PenStreamTest { + data class Record(val flags: Int, val x: Int, val y: Int) + data class Sample(val time: Long, val x: Float, val y: Float = 300f) + + private val records = mutableListOf() + private val stream = PenStream { flags, x, y -> records += Record(flags, x, y) } + private val area = RectF(0f, 0f, 1000f, 600f) + + @Test fun hoverEntersMovesAndLeavesProximity() { + send(MotionEvent.ACTION_HOVER_ENTER, Sample(10, 100f)) + send(MotionEvent.ACTION_HOVER_MOVE, Sample(20, 200f)) + send(MotionEvent.ACTION_HOVER_EXIT, Sample(30, 250f)) + assertEquals(listOf(PEN_IN, PEN_IN), flags()) + idle() + assertEquals(listOf(PEN_IN, PEN_IN), flags()) + grace() + assertEquals(listOf(PEN_IN, PEN_IN, 0), flags()) + assertEquals(listOf(100f, 200f).map(::x), records.take(2).map { it.x }) + assertFalse(stream.inProximity) + } + + @Test fun hoverExitThenDownWithTheSameTimeStaysInProximity() { + send(MotionEvent.ACTION_HOVER_ENTER, Sample(10, 100f)) + send(MotionEvent.ACTION_HOVER_EXIT, Sample(20, 100f)) + send(MotionEvent.ACTION_DOWN, Sample(20, 100f)) + grace() + assertEquals(listOf(PEN_IN, PEN_IN or PEN_TIP), flags()) + } + + @Test fun hoverExitThenADownAfterTheGraceLeavesProximityFirst() { + send(MotionEvent.ACTION_HOVER_ENTER, Sample(10, 100f)) + send(MotionEvent.ACTION_HOVER_EXIT, Sample(20, 100f)) + send(MotionEvent.ACTION_DOWN, Sample(21 + PenStream.EXIT_GRACE_MS, 100f)) + assertEquals(listOf(PEN_IN, 0, PEN_IN or PEN_TIP), flags()) + } + + @Test fun aPenFlickeringAtTheEdgeOfHoverRangeStaysInProximity() { + send(MotionEvent.ACTION_HOVER_ENTER, Sample(10, 100f)) + for (i in 0 until 5) { + send(MotionEvent.ACTION_HOVER_EXIT, Sample(20L + i * 22, 100f)) + send(MotionEvent.ACTION_HOVER_ENTER, Sample(34L + i * 22, 101f)) + } + send(MotionEvent.ACTION_HOVER_EXIT, Sample(130, 101f)) + send(MotionEvent.ACTION_DOWN, Sample(130 + PenStream.EXIT_GRACE_MS, 101f)) + grace() + assertEquals(List(6) { PEN_IN } + (PEN_IN or PEN_TIP), flags()) + assertTrue(stream.inProximity) + } + + @Test fun liftingIntoHoverKeepsProximityAndLiftingAwayLeavesIt() { + send(MotionEvent.ACTION_DOWN, Sample(10, 100f)) + send(MotionEvent.ACTION_UP, Sample(20, 110f)) + send(MotionEvent.ACTION_HOVER_ENTER, Sample(20, 110f)) + grace() + assertEquals(listOf(PEN_IN or PEN_TIP, PEN_IN, PEN_IN), flags()) + records.clear() + send(MotionEvent.ACTION_DOWN, Sample(30, 100f)) + send(MotionEvent.ACTION_UP, Sample(40, 100f)) + grace() + assertEquals(listOf(PEN_IN or PEN_TIP, PEN_IN, 0), flags()) + } + + @Test fun aPenKeyDuringTheExitGraceEndsProximityFirst() { + send(MotionEvent.ACTION_HOVER_ENTER, Sample(10, 100f)) + send(MotionEvent.ACTION_HOVER_EXIT, Sample(20, 100f)) + assertTrue(stream.onKey(key(KeyEvent.ACTION_DOWN, 25))) + grace() + assertEquals(listOf(PEN_IN, 0), flags()) + } + + @Test fun historicalSamplesGoOutOldestFirst() { + send(MotionEvent.ACTION_DOWN, Sample(100, 10f)) + send(MotionEvent.ACTION_MOVE, Sample(103, 20f), Sample(106, 30f), Sample(109, 40f)) + assertEquals(listOf(10f, 20f, 30f, 40f).map(::x), records.map { it.x }) + assertTrue(records.all { it.flags == PEN_IN or PEN_TIP }) + assertEquals(y(300f), records.last().y) + } + + @Test fun cancelEndsTheStrokeAndIgnoresTheRest() { + send(MotionEvent.ACTION_DOWN, Sample(10, 100f)) + send(MotionEvent.ACTION_MOVE, Sample(20, 150f)) + send(MotionEvent.ACTION_CANCEL, Sample(30, 160f)) + send(MotionEvent.ACTION_MOVE, Sample(40, 170f)) + send(MotionEvent.ACTION_UP, Sample(50, 170f)) + assertEquals(listOf(PEN_IN or PEN_TIP, PEN_IN or PEN_TIP, 0), flags()) + assertFalse(stream.inProximity) + } + + @Test fun anUpThatAndroidCanceledIsACancelNotALift() { + send(MotionEvent.ACTION_DOWN, Sample(10, 100f)) + send(MotionEvent.ACTION_UP, Sample(20, 100f), flags = MotionEvent.FLAG_CANCELED) + assertEquals(listOf(PEN_IN or PEN_TIP, 0), flags()) + } + + @Test fun theSideButtonSettingMapsThePrimaryButtonAndTheSecondaryGetsTheOtherClick() { + val cases = mapOf(SessionPrefs.PEN_BUTTON_RIGHT to (PEN_RIGHT to PEN_MIDDLE), + SessionPrefs.PEN_BUTTON_MIDDLE to (PEN_MIDDLE to PEN_RIGHT), SessionPrefs.PEN_BUTTON_OFF to (0 to PEN_RIGHT)) + for ((setting, expected) in cases) { + assertEquals(setting, 0, PenStream.buttonFlags(primary = false, secondary = false, sideButton = setting)) + assertEquals(setting, expected.first, PenStream.buttonFlags(primary = true, secondary = false, sideButton = setting)) + assertEquals(setting, expected.second, PenStream.buttonFlags(primary = false, secondary = true, sideButton = setting)) + assertEquals(setting, expected.first or expected.second, PenStream.buttonFlags(primary = true, secondary = true, sideButton = setting)) + } + stream.sideButton = SessionPrefs.PEN_BUTTON_MIDDLE + send(MotionEvent.ACTION_HOVER_ENTER, Sample(10, 100f), buttons = MotionEvent.BUTTON_STYLUS_PRIMARY) + send(MotionEvent.ACTION_HOVER_MOVE, Sample(20, 100f), buttons = MotionEvent.BUTTON_STYLUS_SECONDARY) + assertEquals(listOf(PEN_IN or PEN_MIDDLE, PEN_IN or PEN_RIGHT), flags()) + } + + @Test fun barrelButtonsFollowTheButtonState() { + send(MotionEvent.ACTION_HOVER_ENTER, Sample(10, 100f)) + send(MotionEvent.ACTION_HOVER_MOVE, Sample(20, 100f), buttons = MotionEvent.BUTTON_STYLUS_PRIMARY) + send(MotionEvent.ACTION_BUTTON_PRESS, Sample(21, 100f), buttons = MotionEvent.BUTTON_STYLUS_PRIMARY or MotionEvent.BUTTON_STYLUS_SECONDARY) + send(MotionEvent.ACTION_DOWN, Sample(30, 100f), buttons = MotionEvent.BUTTON_STYLUS_SECONDARY) + send(MotionEvent.ACTION_MOVE, Sample(40, 100f)) + assertEquals(listOf(PEN_IN, PEN_IN or PEN_RIGHT, PEN_IN or PEN_RIGHT or PEN_MIDDLE, PEN_IN or PEN_TIP or PEN_MIDDLE, + PEN_IN or PEN_TIP), flags()) + } + + @Test fun aBluetoothPenKeyIsTheSideButtonWhileThePenIsNear() { + send(MotionEvent.ACTION_HOVER_ENTER, Sample(10, 100f)) + assertTrue(stream.onKey(key(KeyEvent.ACTION_DOWN, 15))) + assertTrue(stream.onKey(key(KeyEvent.ACTION_DOWN, 16, repeat = 1))) + send(MotionEvent.ACTION_HOVER_MOVE, Sample(20, 130f)) + assertTrue(stream.onKey(key(KeyEvent.ACTION_UP, 25))) + stream.cancel() + stream.sideButton = SessionPrefs.PEN_BUTTON_MIDDLE + send(MotionEvent.ACTION_HOVER_ENTER, Sample(30, 100f)) + stream.onKey(key(KeyEvent.ACTION_DOWN, 35)) + stream.onKey(key(KeyEvent.ACTION_UP, 36)) + stream.cancel() + assertEquals(listOf(PEN_IN, PEN_IN or PEN_RIGHT, PEN_IN or PEN_RIGHT, PEN_IN, 0, + PEN_IN, PEN_IN or PEN_MIDDLE, PEN_IN, 0), flags()) + assertEquals(x(130f), records[2].x) + records.clear() + stream.sideButton = SessionPrefs.PEN_BUTTON_OFF + send(MotionEvent.ACTION_HOVER_ENTER, Sample(40, 100f)) + assertTrue(stream.onKey(key(KeyEvent.ACTION_DOWN, 45))) + assertTrue(stream.holdsKey(key(KeyEvent.ACTION_UP, 46))) + assertTrue(stream.onKey(key(KeyEvent.ACTION_UP, 46))) + assertEquals(listOf(PEN_IN), flags()) + } + + @Test fun aBluetoothPenKeyIsConsumedWhenThePenIsAway() { + assertTrue(stream.onKey(key(KeyEvent.ACTION_DOWN, 10))) + send(MotionEvent.ACTION_HOVER_ENTER, Sample(20, 100f)) + assertTrue(stream.onKey(key(KeyEvent.ACTION_UP, 30))) + assertEquals(listOf(PEN_IN), flags()) + } + + @Test fun aKeyHeldWhileThePenIsAwayIsReleasedAndPressedAgainOnReturn() { + send(MotionEvent.ACTION_HOVER_ENTER, Sample(10, 100f)) + stream.onKey(key(KeyEvent.ACTION_DOWN, 15)) + send(MotionEvent.ACTION_HOVER_EXIT, Sample(20, 100f)) + grace() + stream.onKey(key(KeyEvent.ACTION_DOWN, 500, repeat = 1)) + stream.onKey(key(KeyEvent.ACTION_DOWN, 550, repeat = 2)) + send(MotionEvent.ACTION_HOVER_ENTER, Sample(600, 100f)) + send(MotionEvent.ACTION_DOWN, Sample(610, 100f)) + send(MotionEvent.ACTION_UP, Sample(620, 100f)) + send(MotionEvent.ACTION_HOVER_ENTER, Sample(620, 100f)) + stream.onKey(key(KeyEvent.ACTION_UP, 630)) + val right = PEN_IN or PEN_RIGHT + assertEquals(listOf(PEN_IN, right, 0, right, right or PEN_TIP, right, right, PEN_IN), flags()) + } + + @Test fun aKeyReleasedWhileThePenIsAwayStaysReleased() { + send(MotionEvent.ACTION_HOVER_ENTER, Sample(10, 100f)) + stream.onKey(key(KeyEvent.ACTION_DOWN, 15)) + send(MotionEvent.ACTION_HOVER_EXIT, Sample(20, 100f)) + grace() + assertTrue(stream.holdsKey(key(KeyEvent.ACTION_UP, 40))) + assertTrue(stream.onKey(key(KeyEvent.ACTION_UP, 40))) + assertFalse(stream.holdsKey(key(KeyEvent.ACTION_UP, 41))) + send(MotionEvent.ACTION_HOVER_ENTER, Sample(50, 100f)) + assertEquals(listOf(PEN_IN, PEN_IN or PEN_RIGHT, 0, PEN_IN), flags()) + } + + @Test fun onlyALifecycleCancelForgetsAHeldKeyAndARepeatBringsItBack() { + send(MotionEvent.ACTION_HOVER_ENTER, Sample(10, 100f)) + stream.onKey(key(KeyEvent.ACTION_DOWN, 15)) + stream.cancel() + send(MotionEvent.ACTION_HOVER_ENTER, Sample(20, 100f)) + stream.cancel(releaseKey = true) + send(MotionEvent.ACTION_HOVER_ENTER, Sample(30, 100f)) + assertFalse(stream.holdsKey(key(KeyEvent.ACTION_UP, 35))) + stream.onKey(key(KeyEvent.ACTION_DOWN, 40, repeat = 1)) + stream.onKey(key(KeyEvent.ACTION_UP, 50)) + val right = PEN_IN or PEN_RIGHT + assertEquals(listOf(PEN_IN, right, 0, right, 0, PEN_IN, right, PEN_IN), flags()) + } + + @Test fun removingTheBluetoothPenReleasesItsKeyAndForgetsIt() { + send(MotionEvent.ACTION_HOVER_ENTER, Sample(10, 100f)) + stream.onKey(key(KeyEvent.ACTION_DOWN, 15)) + stream.deviceRemoved(10) + assertFalse(stream.holdsKey(key(KeyEvent.ACTION_UP, 18))) + assertTrue(stream.inProximity) + stream.onKey(key(KeyEvent.ACTION_DOWN, 20)) + send(MotionEvent.ACTION_HOVER_EXIT, Sample(25, 100f)) + grace() + stream.deviceRemoved(10) + send(MotionEvent.ACTION_HOVER_ENTER, Sample(30, 100f)) + assertEquals(listOf(PEN_IN, PEN_IN or PEN_RIGHT, PEN_IN, PEN_IN or PEN_RIGHT, 0, PEN_IN), flags()) + } + + @Test fun aHoverThatStopsReportingCountsAsGone() { + send(MotionEvent.ACTION_HOVER_ENTER, Sample(1_000, 100f)) + send(MotionEvent.ACTION_HOVER_MOVE, Sample(2_000, 100f)) + send(MotionEvent.ACTION_HOVER_MOVE, Sample(4_500, 110f)) + assertEquals(listOf(PEN_IN, PEN_IN, 0, PEN_IN), flags()) + records.clear() + assertTrue(stream.onKey(key(KeyEvent.ACTION_DOWN, 7_000))) + assertEquals(listOf(0), flags()) + } + + @Test fun aPenKeyDoesNotKeepASilentHoverNear() { + send(MotionEvent.ACTION_HOVER_ENTER, Sample(1_000, 100f)) + assertTrue(stream.onKey(key(KeyEvent.ACTION_DOWN, 2_500))) + assertTrue(stream.onKey(key(KeyEvent.ACTION_UP, 3_000))) + assertEquals(listOf(PEN_IN, PEN_IN or PEN_RIGHT, 0), flags()) + } + + @Test fun aSilentHoverIsSentOutByTheWatchdogWithoutAnotherEvent() { + val start = SystemClock.uptimeMillis() + send(MotionEvent.ACTION_HOVER_ENTER, Sample(start, 100f), buttons = MotionEvent.BUTTON_STYLUS_PRIMARY) + shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(1_500)) + send(MotionEvent.ACTION_HOVER_MOVE, Sample(start + 1_500, 110f), buttons = MotionEvent.BUTTON_STYLUS_PRIMARY) + shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(PenStream.HOVER_TIMEOUT_MS - 1)) + assertTrue(stream.inProximity) + shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(1)) + assertFalse(stream.inProximity) + assertEquals(listOf(PEN_IN or PEN_RIGHT, PEN_IN or PEN_RIGHT, 0), flags()) + } + + @Test fun theWatchdogNeverEndsAContact() { + val start = SystemClock.uptimeMillis() + send(MotionEvent.ACTION_HOVER_ENTER, Sample(start, 100f)) + send(MotionEvent.ACTION_DOWN, Sample(start + 10, 100f)) + shadowOf(Looper.getMainLooper()).idleFor(Duration.ofSeconds(5)) + assertTrue(stream.inProximity) + assertEquals(listOf(PEN_IN, PEN_IN or PEN_TIP), flags()) + } + + @Test fun aStrokeIsNeverTimedOut() { + send(MotionEvent.ACTION_DOWN, Sample(1_000, 100f)) + send(MotionEvent.ACTION_MOVE, Sample(9_000, 110f)) + assertEquals(listOf(PEN_IN or PEN_TIP, PEN_IN or PEN_TIP), flags()) + } + + @Test fun lifecycleCancelEndsEverythingOnce() { + send(MotionEvent.ACTION_DOWN, Sample(10, 100f)) + stream.cancel() + stream.cancel() + assertEquals(listOf(PEN_IN or PEN_TIP, 0), flags()) + } + + @Test fun removingThePenDeviceEndsItsStream() { + send(MotionEvent.ACTION_HOVER_ENTER, Sample(10, 100f), device = 6) + stream.deviceRemoved(8) + assertTrue(stream.inProximity) + stream.deviceRemoved(6) + assertEquals(listOf(PEN_IN, 0), flags()) + } + + @Test fun aPenNextToAFingerKeepsItsOwnStroke() { + val finger = MotionEvent.TOOL_TYPE_FINGER + val stylus = MotionEvent.TOOL_TYPE_STYLUS + mixed(MotionEvent.ACTION_DOWN, 10, finger to 50f) + assertTrue(records.isEmpty()) + mixed(MotionEvent.ACTION_POINTER_DOWN or (1 shl MotionEvent.ACTION_POINTER_INDEX_SHIFT), 20, finger to 50f, stylus to 400f) + mixed(MotionEvent.ACTION_MOVE, 30, finger to 60f, stylus to 410f) + mixed(MotionEvent.ACTION_POINTER_UP, 40, finger to 60f, stylus to 420f) + mixed(MotionEvent.ACTION_POINTER_UP or (1 shl MotionEvent.ACTION_POINTER_INDEX_SHIFT), 50, finger to 60f, stylus to 430f) + assertEquals(listOf(PEN_IN or PEN_TIP, PEN_IN or PEN_TIP, PEN_IN or PEN_TIP, PEN_IN), flags()) + assertEquals(listOf(400f, 410f, 420f, 430f).map(::x), records.map { it.x }) + } + + @Test fun positionsAreClampedToThePicture() { + val event = event(MotionEvent.ACTION_HOVER_ENTER, listOf(Sample(10, 40f, y = 580f)), MotionEvent.TOOL_TYPE_STYLUS) + try { stream.onMotion(event, 0, RectF(100f, 50f, 900f, 550f), 1000, 600) } finally { event.recycle() } + assertEquals(Record(PEN_IN, x(100f), y(550f)), records.single()) + } + + @Test fun pensAreFoundByToolTypeFirstAndSourceSecond() { + fun index(tool: Int, source: Int): Int { + val event = event(MotionEvent.ACTION_DOWN, listOf(Sample(0, 1f)), tool, source = source) + return try { PenStream.penIndex(event) } finally { event.recycle() } + } + assertEquals(0, index(MotionEvent.TOOL_TYPE_STYLUS, InputDevice.SOURCE_MOUSE)) + assertEquals(0, index(MotionEvent.TOOL_TYPE_ERASER, InputDevice.SOURCE_TOUCHSCREEN)) + assertEquals(0, index(MotionEvent.TOOL_TYPE_UNKNOWN, InputDevice.SOURCE_STYLUS)) + assertEquals(-1, index(MotionEvent.TOOL_TYPE_FINGER, InputDevice.SOURCE_STYLUS)) + assertEquals(-1, index(MotionEvent.TOOL_TYPE_MOUSE, InputDevice.SOURCE_MOUSE or InputDevice.SOURCE_STYLUS)) + assertEquals(-1, index(MotionEvent.TOOL_TYPE_UNKNOWN, InputDevice.SOURCE_TOUCHSCREEN)) + } + + @Test fun penButtonKeyboardsAreRecognisedByNameAndShape() { + val keyboard = InputDevice.SOURCE_KEYBOARD + assertTrue(PenStream.isPenKeyboard("OPPO AI Pen", keyboard, external = true, alphabetic = false)) + assertTrue(PenStream.isPenKeyboard("Xiaomi Smart STYLUS", keyboard, external = true, alphabetic = false)) + assertTrue(PenStream.isPenKeyboard("HUAWEI M-Pencil", keyboard, external = true, alphabetic = false)) + assertFalse(PenStream.isPenKeyboard("OPPO AI Pen", keyboard, external = false, alphabetic = false)) + assertFalse(PenStream.isPenKeyboard("OPPO AI Pen", keyboard or InputDevice.SOURCE_MOUSE, external = true, alphabetic = false)) + assertFalse(PenStream.isPenKeyboard("Pen Keyboard K380", keyboard, external = true, alphabetic = true)) + assertFalse(PenStream.isPenKeyboard("Open Remote", keyboard, external = true, alphabetic = false)) + assertFalse(PenStream.isPenKeyboard("8BitDo Remote", keyboard, external = true, alphabetic = false)) + assertFalse(PenStream.isPenKeyboard(null, keyboard, external = true, alphabetic = false)) + } + + @Test fun aTouchscreenCountsAsAPenDeviceOnlyWhereAndroidSetsTheStylusBitForPens() { + val screen = InputDevice.SOURCE_TOUCHSCREEN or InputDevice.SOURCE_STYLUS + assertFalse(PenStream.isPenDevice(screen, sdk = 33)) + assertTrue(PenStream.isPenDevice(screen, sdk = 34)) + for (sdk in listOf(26, 33, 34)) { + assertTrue(PenStream.isPenDevice(InputDevice.SOURCE_MOUSE or InputDevice.SOURCE_STYLUS, sdk)) + assertTrue(PenStream.isPenDevice(InputDevice.SOURCE_BLUETOOTH_STYLUS, sdk)) + assertTrue(PenStream.isPenDevice(InputDevice.SOURCE_STYLUS, sdk)) + assertFalse(PenStream.isPenDevice(InputDevice.SOURCE_TOUCHSCREEN, sdk)) + assertFalse(PenStream.isPenDevice(InputDevice.SOURCE_MOUSE, sdk)) + } + } + + private fun flags() = records.map { it.flags } + private fun x(position: Float) = InputSpace.fixedX(position, 1000) + private fun y(position: Float) = InputSpace.fixedY(position, 600) + private fun idle() = shadowOf(Looper.getMainLooper()).idle() + private fun grace() = shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(PenStream.EXIT_GRACE_MS)) + + private fun key(action: Int, time: Long, repeat: Int = 0) = + KeyEvent(time, time, action, KeyEvent.KEYCODE_UNKNOWN, repeat, 0, 10, 191, 0, InputDevice.SOURCE_KEYBOARD) + + private fun send(action: Int, vararg samples: Sample, buttons: Int = 0, flags: Int = 0, device: Int = 6) { + val event = event(action, samples.toList(), MotionEvent.TOOL_TYPE_STYLUS, buttons, flags, device = device) + try { stream.onMotion(event, 0, area, 1000, 600) } finally { event.recycle() } + } + + private fun mixed(action: Int, time: Long, vararg pointers: Pair) { + val properties = Array(pointers.size) { MotionEvent.PointerProperties().apply { id = it; toolType = pointers[it].first } } + val coords = Array(pointers.size) { MotionEvent.PointerCoords().apply { x = pointers[it].second; y = 300f; pressure = 0.5f } } + val event = MotionEvent.obtain(0, time, action, pointers.size, properties, coords, 0, 0, 1f, 1f, 6, 0, + InputDevice.SOURCE_TOUCHSCREEN or InputDevice.SOURCE_STYLUS, 0) + try { + val pen = PenStream.penIndex(event) + if (pen >= 0) stream.onMotion(event, pen, area, 1000, 600) + } finally { event.recycle() } + } + + private fun event(action: Int, samples: List, tool: Int, buttons: Int = 0, flags: Int = 0, + source: Int = InputDevice.SOURCE_TOUCHSCREEN or InputDevice.SOURCE_STYLUS, device: Int = 6): MotionEvent { + val properties = arrayOf(MotionEvent.PointerProperties().apply { id = 0; toolType = tool }) + fun coords(sample: Sample) = arrayOf(MotionEvent.PointerCoords().apply { + x = sample.x + y = sample.y + }) + val first = samples.first() + val event = MotionEvent.obtain(0, first.time, action, 1, properties, coords(first), 0, buttons, 1f, 1f, device, 0, source, flags) + for (sample in samples.drop(1)) event.addBatch(sample.time, coords(sample), 0) + return event + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/input/PointerSubsetTest.kt b/app/src/test/java/com/droiddeck/launcher/input/PointerSubsetTest.kt new file mode 100644 index 0000000000..8326c1b26a --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/input/PointerSubsetTest.kt @@ -0,0 +1,57 @@ +package com.droiddeck.launcher.input + +import android.view.InputDevice +import android.view.MotionEvent +import org.junit.Assert.* +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 PointerSubsetTest { + @Test fun aForwardedPointerKeepsItsHistoryAndTimes() { + val properties = Array(2) { MotionEvent.PointerProperties().apply { id = it; toolType = if (it == 0) MotionEvent.TOOL_TYPE_FINGER else MotionEvent.TOOL_TYPE_STYLUS } } + fun coords(step: Int) = Array(2) { MotionEvent.PointerCoords().apply { x = 100f * it + step; y = 50f; pressure = 0.1f * step } } + val event = MotionEvent.obtain(5, 10, MotionEvent.ACTION_MOVE, 2, properties, coords(1), 0, 0, 1f, 1f, 3, 0, + InputDevice.SOURCE_TOUCHSCREEN or InputDevice.SOURCE_STYLUS, MotionEvent.FLAG_CANCELED) + event.addBatch(14, coords(2), 0) + event.addBatch(18, coords(3), 0) + val split = event.pointerSubset(1 shl 1)!! + try { + assertEquals(1, split.pointerCount) + assertEquals(MotionEvent.TOOL_TYPE_STYLUS, split.getToolType(0)) + assertEquals(2, split.historySize) + assertEquals(listOf(10L, 14L), (0 until split.historySize).map { split.getHistoricalEventTime(it) }) + assertEquals(listOf(101f, 102f), (0 until split.historySize).map { split.getHistoricalX(0, it) }) + assertEquals(0.2f, split.getHistoricalPressure(0, 1), 1e-6f) + assertEquals(18L, split.eventTime) + assertEquals(103f, split.x, 0f) + assertEquals(5L, split.downTime) + assertEquals(MotionEvent.ACTION_MOVE, split.actionMasked) + assertEquals(event.source, split.source) + assertEquals(MotionEvent.FLAG_CANCELED, split.flags and MotionEvent.FLAG_CANCELED) + } finally { + split.recycle() + event.recycle() + } + } + + @Test fun anEventWithoutHistoryStaysASingleSample() { + val properties = arrayOf(MotionEvent.PointerProperties().apply { id = 2; toolType = MotionEvent.TOOL_TYPE_FINGER }) + val coords = arrayOf(MotionEvent.PointerCoords().apply { x = 7f; y = 8f }) + val event = MotionEvent.obtain(0, 20, MotionEvent.ACTION_DOWN, 1, properties, coords, 0, 0, 1f, 1f, 0, 0, InputDevice.SOURCE_TOUCHSCREEN, 0) + val split = event.pointerSubset(1 shl 2)!! + try { + assertEquals(0, split.historySize) + assertEquals(20L, split.eventTime) + assertEquals(MotionEvent.ACTION_DOWN, split.actionMasked) + assertEquals(7f, split.x, 0f) + assertNull(event.pointerSubset(1 shl 0)) + } finally { + split.recycle() + event.recycle() + } + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/input/SessionPenRoutingTest.kt b/app/src/test/java/com/droiddeck/launcher/input/SessionPenRoutingTest.kt new file mode 100644 index 0000000000..cd6a40bc24 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/input/SessionPenRoutingTest.kt @@ -0,0 +1,426 @@ +package com.droiddeck.launcher.input + +import android.hardware.input.InputManager +import android.os.Looper +import android.view.InputDevice +import android.view.KeyEvent +import android.view.MotionEvent +import android.view.SurfaceView +import androidx.compose.runtime.MutableState +import androidx.compose.ui.geometry.Offset +import com.droiddeck.launcher.SessionActivity +import com.droiddeck.launcher.input.PenStream.Companion.PEN_IN +import com.droiddeck.launcher.input.PenStream.Companion.PEN_MIDDLE +import com.droiddeck.launcher.input.PenStream.Companion.PEN_RIGHT +import com.droiddeck.launcher.input.PenStream.Companion.PEN_TIP +import com.droiddeck.launcher.session.SessionPrefs +import com.droiddeck.launcher.session.SessionService +import com.droiddeck.launcher.session.SessionState +import com.droiddeck.launcher.wayland.WaylandCompositor +import java.time.Duration +import org.junit.After +import org.junit.Assert.* +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.Robolectric +import org.robolectric.RobolectricTestRunner +import org.robolectric.Shadows.shadowOf +import org.robolectric.annotation.Config +import org.robolectric.annotation.Implementation +import org.robolectric.annotation.Implements +import org.robolectric.shadows.InputDeviceBuilder +import org.robolectric.shadows.ShadowInputDevice +import org.robolectric.util.ReflectionHelpers + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [28], manifest = Config.NONE, shadows = [SessionPenRoutingTest.CompositorShadow::class]) +class SessionPenRoutingTest { + private lateinit var activity: SessionActivity + private val previousSize = SessionState.outputSize + private val previousMode = SessionState.mode + private val previousSteamUi = SessionState.steamUi + private val pen = MotionEvent.TOOL_TYPE_STYLUS + private val finger = MotionEvent.TOOL_TYPE_FINGER + private val stylusScreen = InputDevice.SOURCE_TOUCHSCREEN or InputDevice.SOURCE_STYLUS + + @Before fun setUp() { + shadowOf(Looper.getMainLooper()).idleFor(Duration.ofSeconds(2)) + activity = Robolectric.buildActivity(SessionActivity::class.java).get() + activity.window.decorView + activity.getSharedPreferences("session", 0).edit().clear().commit() + field("surfaceView", SurfaceView(activity).apply { layout(0, 0, 1000, 600) }) + SessionState.outputSize = 1000 to 600 + SessionState.mode = SessionService.MODE_DESKTOP + SessionState.steamUi = null + CompositorShadow.clear() + } + + @After fun tearDown() { + shadowOf(Looper.getMainLooper()).idle() + SessionState.outputSize = previousSize + SessionState.mode = previousMode + SessionState.steamUi = previousSteamUi + } + + @Test fun penHoverMovesThePointerAndTheCursorOnTheDesktopAndInSteam() { + assertTrue(activity.dispatchGenericMotionEvent(event(MotionEvent.ACTION_HOVER_MOVE, 10, pen to 500f))) + assertEquals(listOf(Pen(PEN_IN, InputSpace.fixedX(500f, 1000), InputSpace.fixedY(300f, 600))), CompositorShadow.pens) + assertTrue(state("cursorVisible").value) + assertEquals(Offset(500f, 300f), state("cursorPos").value) + steamDirect(SessionPrefs.PEN_MOUSE) + assertTrue(activity.dispatchGenericMotionEvent(event(MotionEvent.ACTION_HOVER_MOVE, 20, pen to 600f))) + assertEquals(Pen(PEN_IN, InputSpace.fixedX(600f, 1000), InputSpace.fixedY(300f, 600)), CompositorShadow.pens.last()) + assertEquals(Offset(600f, 300f), state("cursorPos").value) + assertTrue(CompositorShadow.pointers.isEmpty()) + assertTrue(CompositorShadow.touches.isEmpty()) + } + + @Test fun theSideButtonSettingDecidesWhatThePrimaryButtonSends() { + val cases = listOf(SessionPrefs.PEN_BUTTON_RIGHT to PEN_RIGHT, SessionPrefs.PEN_BUTTON_MIDDLE to PEN_MIDDLE, + SessionPrefs.PEN_BUTTON_OFF to 0) + for ((step, case) in cases.withIndex()) { + SessionPrefs.setPenSetting(activity, "penButton", case.first) + invoke("readPrefs") + CompositorShadow.pens.clear() + assertTrue(activity.dispatchGenericMotionEvent(event(MotionEvent.ACTION_HOVER_MOVE, 10L + 10 * step, pen to 500f, + buttons = MotionEvent.BUTTON_STYLUS_PRIMARY))) + assertEquals(case.first, listOf(PEN_IN or case.second), CompositorShadow.pens.map { it.flags }) + } + } + + @Test fun penContactInMouseModeHoldsTheTipUntilItLifts() { + steamDirect(SessionPrefs.PEN_MOUSE) + assertTrue(activity.onTouchEvent(event(MotionEvent.ACTION_DOWN, 10, pen to 500f))) + assertTrue(activity.onTouchEvent(event(MotionEvent.ACTION_MOVE, 20, pen to 520f))) + assertTrue(activity.onTouchEvent(event(MotionEvent.ACTION_UP, 30, pen to 520f))) + shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(PenStream.EXIT_GRACE_MS)) + assertEquals(listOf(PEN_IN or PEN_TIP, PEN_IN or PEN_TIP, PEN_IN, 0), CompositorShadow.pens.map { it.flags }) + assertTrue(CompositorShadow.touches.isEmpty()) + } + + @Test fun anExternalTabletTipReachesThePenInsteadOfTheMouse() { + steamDirect(SessionPrefs.PEN_MOUSE) + val source = InputDevice.SOURCE_MOUSE or InputDevice.SOURCE_STYLUS + assertTrue(activity.onTouchEvent(event(MotionEvent.ACTION_DOWN, 10, pen to 500f, source = source))) + assertEquals(listOf(PEN_IN or PEN_TIP), CompositorShadow.pens.map { it.flags }) + assertTrue(CompositorShadow.pointers.isEmpty()) + } + + @Test fun penContactInTouchModeTakesTheFingerRouteAsBefore() { + steamDirect(SessionPrefs.PEN_TOUCH) + assertTrue(activity.onTouchEvent(event(MotionEvent.ACTION_DOWN, 10, pen to 500f))) + assertEquals(listOf(Touch(0, 0, InputSpace.x(500f, 1000), InputSpace.y(300f, 600))), CompositorShadow.touches) + activity.dispatchGenericMotionEvent(event(MotionEvent.ACTION_HOVER_MOVE, 20, pen to 600f)) + assertTrue(CompositorShadow.pens.isEmpty()) + assertTrue(CompositorShadow.pointers.isEmpty()) + } + + @Test fun anExternalTabletStaysOnTheMousePathInTouchMode() { + steamDirect(SessionPrefs.PEN_TOUCH) + val source = InputDevice.SOURCE_MOUSE or InputDevice.SOURCE_STYLUS + assertTrue(activity.onTouchEvent(event(MotionEvent.ACTION_DOWN, 10, pen to 500f, source = source))) + assertTrue(CompositorShadow.touches.isEmpty()) + assertTrue(CompositorShadow.pens.isEmpty()) + } + + @Test fun fingersKeepTheirRouteWhateverThePenMode() { + for (mode in listOf(SessionPrefs.PEN_MOUSE, SessionPrefs.PEN_TOUCH)) { + steamDirect(mode) + CompositorShadow.clear() + assertTrue(activity.onTouchEvent(event(MotionEvent.ACTION_DOWN, 10, finger to 500f, source = InputDevice.SOURCE_TOUCHSCREEN))) + assertEquals(listOf(Touch(0, 0, InputSpace.x(500f, 1000), InputSpace.y(300f, 600))), CompositorShadow.touches) + assertTrue(CompositorShadow.pens.isEmpty()) + activity.onTouchEvent(event(MotionEvent.ACTION_CANCEL, 20, finger to 500f, source = InputDevice.SOURCE_TOUCHSCREEN)) + } + } + + @Test fun aFingerAndAPenInOneEventAreSplitByToolType() { + steamDirect(SessionPrefs.PEN_MOUSE) + activity.onTouchEvent(event(MotionEvent.ACTION_DOWN, 10, finger to 100f)) + activity.onTouchEvent(event(MotionEvent.ACTION_POINTER_DOWN or (1 shl MotionEvent.ACTION_POINTER_INDEX_SHIFT), 20, finger to 110f, pen to 500f)) + assertEquals(listOf(PEN_IN or PEN_TIP), CompositorShadow.pens.map { it.flags }) + assertEquals(InputSpace.fixedX(500f, 1000), CompositorShadow.pens.last().x) + assertTrue(CompositorShadow.touches.isNotEmpty()) + assertTrue(CompositorShadow.touches.all { it.id == 0 && it.x < InputSpace.x(200f, 1000) }) + } + + @Test fun theLowerScreenGameTakesPenHoverAndContact() { + steamDirect(SessionPrefs.PEN_MOUSE) + fun hover(event: MotionEvent) = ReflectionHelpers.callInstanceMethod(activity, "onBottomGameHover", + ReflectionHelpers.ClassParameter.from(MotionEvent::class.java, event)) + fun touch(event: MotionEvent) = ReflectionHelpers.callInstanceMethod(activity, "onBottomGameTouch", + ReflectionHelpers.ClassParameter.from(MotionEvent::class.java, event)) + assertTrue(hover(event(MotionEvent.ACTION_HOVER_MOVE, 10, pen to 500f))) + assertTrue(touch(event(MotionEvent.ACTION_DOWN, 20, pen to 500f, source = InputDevice.SOURCE_MOUSE or InputDevice.SOURCE_STYLUS))) + assertEquals(listOf(PEN_IN, PEN_IN or PEN_TIP), CompositorShadow.pens.map { it.flags }) + SessionPrefs.setPenSetting(activity, "pen", SessionPrefs.PEN_TOUCH) + invoke("readPrefs") + assertFalse(hover(event(MotionEvent.ACTION_HOVER_MOVE, 30, pen to 500f))) + assertFalse(hover(event(MotionEvent.ACTION_HOVER_MOVE, 40, finger to 500f, source = InputDevice.SOURCE_MOUSE))) + assertEquals(listOf(PEN_IN, PEN_IN or PEN_TIP, 0), CompositorShadow.pens.map { it.flags }) + } + + @Test fun glassesAProgramPointerLockAndAppUiDropPenHover() { + state("glassesOutputActive").value = true + activity.dispatchGenericMotionEvent(event(MotionEvent.ACTION_HOVER_MOVE, 10, pen to 500f)) + state("glassesOutputActive").value = false + SessionState.mode = SessionService.MODE_STEAM + field("pointerLocked", true) + activity.dispatchGenericMotionEvent(event(MotionEvent.ACTION_HOVER_MOVE, 20, pen to 500f)) + field("pointerLocked", false) + state("achievementsOpen").value = true + activity.dispatchGenericMotionEvent(event(MotionEvent.ACTION_HOVER_MOVE, 30, pen to 500f)) + assertTrue(CompositorShadow.pens.isEmpty()) + assertTrue(CompositorShadow.pointers.isEmpty()) + } + + @Config(sdk = [34]) + @Test fun aPenNearTheDesktopReleasesTheMouseGrabUntilItLeaves() { + shadowOf(activity.getSystemService(InputManager::class.java)).addInputDevice( + InputDeviceBuilder.newBuilder().setId(11).setName("mouse").setSources(InputDevice.SOURCE_MOUSE).build()) + field("desktopMousePlaced", true) + field("pointerLocked", true) + val start = android.os.SystemClock.uptimeMillis() + assertTrue(activity.dispatchGenericMotionEvent(event(MotionEvent.ACTION_HOVER_MOVE, start, pen to 500f))) + shadowOf(Looper.getMainLooper()).idleFor(Duration.ofSeconds(1)) + assertEquals(listOf(RIGHT_CTRL to 1, RIGHT_CTRL to 0), CompositorShadow.keys) + field("pointerLocked", false) + invoke("grabDesktopMouse") + assertEquals(2, CompositorShadow.keys.size) + activity.dispatchGenericMotionEvent(event(MotionEvent.ACTION_HOVER_EXIT, start + 1_000, pen to 500f)) + shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(PenStream.EXIT_GRACE_MS)) + invoke("grabDesktopMouse") + shadowOf(Looper.getMainLooper()).idleFor(Duration.ofSeconds(1)) + assertEquals(listOf(RIGHT_CTRL to 1, RIGHT_CTRL to 0, RIGHT_CTRL to 1, RIGHT_CTRL to 0), CompositorShadow.keys) + } + + @Test fun openingAppUiEndsAHoveringPen() { + activity.dispatchGenericMotionEvent(event(MotionEvent.ACTION_HOVER_MOVE, 10, pen to 500f)) + state("achievementsOpen").value = true + activity.dispatchGenericMotionEvent(event(MotionEvent.ACTION_HOVER_MOVE, 20, pen to 510f)) + assertEquals(listOf(PEN_IN, 0), CompositorShadow.pens.map { it.flags }) + } + + @Test fun touchModeAndRouteChangesDeviceRemovalAndPauseEndThePen() { + invoke("readPrefs") + assertTrue(activity.dispatchGenericMotionEvent(event(MotionEvent.ACTION_HOVER_MOVE, 10, pen to 500f))) + SessionPrefs.setTouchMode(activity, SessionPrefs.TOUCH_DIRECT) + invoke("readPrefs") + assertTrue(activity.dispatchGenericMotionEvent(event(MotionEvent.ACTION_HOVER_MOVE, 20, pen to 500f))) + field("routedTouchMode", SessionPrefs.TOUCH_PAD) + invoke("applyTouchRouting") + assertTrue(activity.dispatchGenericMotionEvent(event(MotionEvent.ACTION_HOVER_MOVE, 30, pen to 500f))) + ReflectionHelpers.getField(activity, "deviceListener").onInputDeviceRemoved(6) + assertTrue(activity.dispatchGenericMotionEvent(event(MotionEvent.ACTION_HOVER_MOVE, 40, pen to 500f))) + invoke("onPause") + assertEquals(listOf(PEN_IN, 0, PEN_IN, 0, PEN_IN, 0, PEN_IN, 0), CompositorShadow.pens.map { it.flags }) + } + + @Config(sdk = [34], shadows = [PadBridgeRoutingTest.RegisteredKeyEvent::class]) + @Test fun aHeldPenKeyOutlivesATouchCancelButNotAFocusLoss() { + penKeyboard() + assertTrue(activity.dispatchGenericMotionEvent(event(MotionEvent.ACTION_HOVER_MOVE, 10, pen to 500f))) + assertTrue(penKey(KeyEvent.ACTION_DOWN, 15)) + invoke("cancelTouchGestures") + assertTrue(activity.dispatchGenericMotionEvent(event(MotionEvent.ACTION_HOVER_MOVE, 20, pen to 500f))) + invoke("releaseHeldGuestInput") + assertTrue(activity.dispatchGenericMotionEvent(event(MotionEvent.ACTION_HOVER_MOVE, 30, pen to 500f))) + val right = PEN_IN or PEN_RIGHT + assertEquals(listOf(PEN_IN, right, 0, right, 0, PEN_IN), CompositorShadow.pens.map { it.flags }) + } + + @Config(sdk = [34], shadows = [PadBridgeRoutingTest.RegisteredKeyEvent::class]) + @Test fun aPenKeyIsAnOrdinaryKeyInTouchMode() { + penKeyboard() + assertTrue(activity.dispatchGenericMotionEvent(event(MotionEvent.ACTION_HOVER_MOVE, 10, pen to 500f))) + assertTrue(penKey(KeyEvent.ACTION_DOWN, 15)) + SessionPrefs.setPenSetting(activity, "pen", SessionPrefs.PEN_TOUCH) + invoke("readPrefs") + assertTrue(penKey(KeyEvent.ACTION_UP, 20)) + assertFalse(penKey(KeyEvent.ACTION_DOWN, 25)) + assertFalse(penKey(KeyEvent.ACTION_UP, 30)) + assertEquals(listOf(PEN_IN, PEN_IN or PEN_RIGHT, 0), CompositorShadow.pens.map { it.flags }) + } + + @Test fun aFingerAndARelativePenOnTheTouchpadStayOneTwoPointerStream() { + state("glassesOutputActive").value = true + field("padMouseCheckedAt", android.os.SystemClock.uptimeMillis()) + for (penFirst in listOf(false, true)) { + val pad = touchpad() + val first = if (penFirst) pen to 500f else finger to 100f + val second = if (penFirst) finger to 100f else pen to 500f + fun both(step: Int) = arrayOf(first.first to first.second + 10f * step, second.first to second.second + 10f * step) + activity.onTouchEvent(event(MotionEvent.ACTION_DOWN, 10_000, first)) + activity.onTouchEvent(event(MotionEvent.ACTION_POINTER_DOWN or (1 shl MotionEvent.ACTION_POINTER_INDEX_SHIFT), 10_010, *both(0))) + for (step in 1..4) activity.onTouchEvent(event(MotionEvent.ACTION_MOVE, 10_010L + 10 * step, *both(step))) + activity.onTouchEvent(event(MotionEvent.ACTION_POINTER_UP or (1 shl MotionEvent.ACTION_POINTER_INDEX_SHIFT), 10_060, *both(4))) + activity.onTouchEvent(event(MotionEvent.ACTION_UP, 10_070, first.first to first.second + 40f)) + assertTrue("penFirst=$penFirst", pad.scrolls.isNotEmpty()) + assertTrue("penFirst=$penFirst ${pad.moves}", pad.moves.all { it in 450f..550f }) + assertTrue(CompositorShadow.pens.isEmpty()) + } + } + + @Test fun aRelativePenStrokeThatAndroidCanceledDoesNotClick() { + steamDirect(SessionPrefs.PEN_MOUSE) + state("glassesOutputActive").value = true + val pad = touchpad() + activity.onTouchEvent(event(MotionEvent.ACTION_DOWN, 10_000, finger to 100f)) + activity.onTouchEvent(event(MotionEvent.ACTION_POINTER_DOWN or (1 shl MotionEvent.ACTION_POINTER_INDEX_SHIFT), 10_010, finger to 100f, pen to 500f)) + activity.onTouchEvent(event(MotionEvent.ACTION_POINTER_UP or (1 shl MotionEvent.ACTION_POINTER_INDEX_SHIFT), 10_050, finger to 100f, pen to 500f, + flags = MotionEvent.FLAG_CANCELED)) + activity.onTouchEvent(event(MotionEvent.ACTION_UP, 10_060, finger to 100f)) + activity.onTouchEvent(event(MotionEvent.ACTION_DOWN, 20_000, pen to 500f)) + activity.onTouchEvent(event(MotionEvent.ACTION_UP, 20_050, pen to 500f, flags = MotionEvent.FLAG_CANCELED)) + assertTrue(pad.presses.isEmpty()) + assertTrue(CompositorShadow.touches.isNotEmpty()) + activity.onTouchEvent(event(MotionEvent.ACTION_DOWN, 30_000, pen to 500f)) + activity.onTouchEvent(event(MotionEvent.ACTION_UP, 30_050, pen to 500f)) + assertEquals(listOf(PointerGestures.BTN_LEFT), pad.presses) + assertTrue(CompositorShadow.pens.isEmpty()) + } + + @Test fun aTouchCancelAfterAFingerLiftsEndsTheRelativePenStroke() { + steamDirect(SessionPrefs.PEN_MOUSE) + state("glassesOutputActive").value = true + val pad = touchpad() + activity.onTouchEvent(event(MotionEvent.ACTION_DOWN, 10_000, pen to 500f)) + activity.onTouchEvent(event(MotionEvent.ACTION_POINTER_DOWN or (1 shl MotionEvent.ACTION_POINTER_INDEX_SHIFT), 10_010, pen to 500f, finger to 100f)) + activity.onTouchEvent(event(MotionEvent.ACTION_POINTER_UP or (1 shl MotionEvent.ACTION_POINTER_INDEX_SHIFT), 10_020, pen to 500f, finger to 100f)) + invoke("cancelTouchGestures") + activity.onTouchEvent(event(MotionEvent.ACTION_UP, 10_050, pen to 500f)) + assertTrue(pad.presses.isEmpty()) + activity.onTouchEvent(event(MotionEvent.ACTION_DOWN, 20_000, pen to 500f)) + activity.onTouchEvent(event(MotionEvent.ACTION_UP, 20_050, pen to 500f)) + assertEquals(listOf(PointerGestures.BTN_LEFT), pad.presses) + } + + @Test fun changingAPenSettingEndsThePenAndRereadingTheSameSettingsDoesNot() { + assertTrue(activity.dispatchGenericMotionEvent(event(MotionEvent.ACTION_HOVER_MOVE, 10, pen to 500f))) + invoke("readPrefs") + assertEquals(listOf(PEN_IN), CompositorShadow.pens.map { it.flags }) + for ((step, setting) in listOf("penButton" to SessionPrefs.PEN_BUTTON_MIDDLE, "pen" to SessionPrefs.PEN_TOUCH).withIndex()) { + assertTrue(activity.dispatchGenericMotionEvent(event(MotionEvent.ACTION_HOVER_MOVE, 20L + 10 * step, pen to 500f))) + SessionPrefs.setPenSetting(activity, setting.first, setting.second) + invoke("readPrefs") + assertEquals(setting.first, 0, CompositorShadow.pens.last().flags) + } + } + + @Test fun theFirstPenEventRemembersThatThePhoneHasAPen() { + invoke("readPrefs") + assertFalse(state("penSeen").value) + assertTrue(activity.dispatchGenericMotionEvent(event(MotionEvent.ACTION_HOVER_MOVE, 10, pen to 500f))) + assertTrue(state("penSeen").value) + assertTrue(SessionPrefs.penSeen(activity)) + } + + @Config(sdk = [34]) + @Test fun aConnectedStylusDeviceCountsAsSeenBeforeAnyPenEvent() { + invoke("readPrefs") + assertFalse(state("penSeen").value) + shadowOf(activity.getSystemService(InputManager::class.java)).addInputDevice( + InputDeviceBuilder.newBuilder().setId(42).setName("digitizer").setSources(stylusScreen).build()) + invoke("readPrefs") + assertTrue(state("penSeen").value) + assertTrue(SessionPrefs.penSeen(activity)) + } + + @Config(sdk = [33]) + @Test fun beforeAndroid14ATouchscreenWithTheStylusBitWaitsForAPenEvent() { + shadowOf(activity.getSystemService(InputManager::class.java)).addInputDevice( + ShadowInputDevice.makeInputDeviceNamed("touchscreen").also { + ReflectionHelpers.setField(it, "mId", 42) + ReflectionHelpers.setField(it, "mSources", stylusScreen) + }) + assertEquals(listOf(42), InputDevice.getDeviceIds().toList()) + invoke("readPrefs") + assertFalse(state("penSeen").value) + assertFalse(SessionPrefs.penSeen(activity)) + assertTrue(activity.dispatchGenericMotionEvent(event(MotionEvent.ACTION_HOVER_MOVE, 10, pen to 500f))) + assertTrue(state("penSeen").value) + } + + @Config(sdk = [34]) + @Test fun aStylusDeviceIsNotAHardwareMouse() { + fun device(sources: Int) = InputDeviceBuilder.newBuilder().setId(7).setName("device").setSources(sources).build() + fun mouse(sources: Int) = ReflectionHelpers.callInstanceMethod(activity, "isHardwareMouse", + ReflectionHelpers.ClassParameter.from(InputDevice::class.java, device(sources))) + assertTrue(mouse(InputDevice.SOURCE_MOUSE)) + assertFalse(mouse(InputDevice.SOURCE_MOUSE or InputDevice.SOURCE_STYLUS)) + assertFalse(mouse(InputDevice.SOURCE_MOUSE or InputDevice.SOURCE_STYLUS or InputDevice.SOURCE_KEYBOARD)) + assertFalse(mouse(stylusScreen)) + } + + private fun steamDirect(mode: String) { + SessionPrefs.setPenSetting(activity, "pen", mode) + invoke("readPrefs") + SessionState.mode = SessionService.MODE_STEAM + field("routedTouchMode", SessionPrefs.TOUCH_DIRECT) + field("padMouseCheckedAt", android.os.SystemClock.uptimeMillis()) + field("threeFingerTap", ThreeFingerTap(8f)) + field("directTouchHold", DirectTouchHold(8f, object : PointerGestures.Listener { + override fun onMove(x: Float, y: Float) {} + override fun onButton(button: Int, pressed: Boolean, x: Float, y: Float) {} + override fun onWheel(steps: Int) {} + override fun onLongPress() {} + }, ReflectionHelpers.getField(activity, "directTouchSink")).also { + it.configure(TouchGestureConfig(directHoldBehavior = TouchGestureConfig.MOUSE_BEHAVIOR_NONE)) + }) + } + + private fun penKeyboard() = shadowOf(activity.getSystemService(InputManager::class.java)).addInputDevice( + InputDeviceBuilder.newBuilder().setId(10).setName("OPPO AI Pen").setSources(InputDevice.SOURCE_KEYBOARD).setExternal(true) + .setKeyboardType(InputDevice.KEYBOARD_TYPE_NON_ALPHABETIC).build()) + + private fun penKey(action: Int, time: Long) = + activity.dispatchKeyEvent(KeyEvent(time, time, action, KeyEvent.KEYCODE_UNKNOWN, 0, 0, 10, 191, 0, InputDevice.SOURCE_KEYBOARD)) + + private fun event(action: Int, time: Long, vararg pointers: Pair, source: Int = stylusScreen, flags: Int = 0, + buttons: Int = 0): MotionEvent { + val properties = Array(pointers.size) { MotionEvent.PointerProperties().apply { id = it; toolType = pointers[it].first } } + val coords = Array(pointers.size) { MotionEvent.PointerCoords().apply { x = pointers[it].second; y = 300f; pressure = 0.5f } } + return MotionEvent.obtain(0, time, action, pointers.size, properties, coords, 0, buttons, 1f, 1f, 6, 0, source, flags) + } + + private class Pad : PointerGestures.Listener { + val moves = mutableListOf() + val scrolls = mutableListOf() + val presses = mutableListOf() + override fun onMove(x: Float, y: Float) { moves += x } + override fun onButton(button: Int, pressed: Boolean, x: Float, y: Float) { if (pressed) presses += button } + override fun onWheel(steps: Int) {} + override fun onLongPress() {} + override fun onScroll(dx: Float, dy: Float) { scrolls += dx } + } + + private fun touchpad() = Pad().also { field("touchpad", TouchpadGestures(8f, it)) } + + private fun field(name: String, value: Any) = ReflectionHelpers.setField(activity, name, value) + private fun invoke(name: String) = ReflectionHelpers.callInstanceMethod(activity, name) + private fun state(name: String): MutableState = ReflectionHelpers.getField(activity, "$name\$delegate") + + private companion object { const val RIGHT_CTRL = 97 } + + data class Pen(val flags: Int, val x: Int, val y: Int) + data class Touch(val action: Int, val id: Int, val x: Int, val y: Int) + + @Implements(value = WaylandCompositor::class, isInAndroidSdk = false) + class CompositorShadow { + companion object { + val pens = mutableListOf() + val touches = mutableListOf() + val pointers = mutableListOf>() + val keys = mutableListOf>() + fun clear() { pens.clear(); touches.clear(); pointers.clear(); keys.clear() } + @JvmStatic @Implementation fun __staticInitializer__() {} + @JvmStatic @Implementation fun nativeSendPen(flags: Int, x: Int, y: Int) { pens += Pen(flags, x, y) } + @JvmStatic @Implementation fun nativeSendTouch(action: Int, id: Int, x: Int, y: Int) { touches += Touch(action, id, x, y) } + @JvmStatic @Implementation fun nativeSendPointer(action: Int, x: Int, y: Int) { pointers += Triple(action, x, y) } + @JvmStatic @Implementation fun nativeSendSceneInput(type: Int, code: Int, value: Int) {} + @JvmStatic @Implementation fun nativeSendKey(code: Int, value: Int) { keys += code to value } + @JvmStatic @Implementation fun nativeFocusTakesTouch(): Boolean = false + } + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/session/SettingsResetTest.kt b/app/src/test/java/com/droiddeck/launcher/session/SettingsResetTest.kt index 4220961071..bc49532c66 100644 --- a/app/src/test/java/com/droiddeck/launcher/session/SettingsResetTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/session/SettingsResetTest.kt @@ -34,6 +34,9 @@ class SettingsResetTest { @Test fun settingsGoBackToTheirDefaults() { SessionPrefs.setTheme(app, "ocean") SessionPrefs.setBackDoubleTapMs(app, 100) + SessionPrefs.setPenSetting(app, "pen", SessionPrefs.PEN_TOUCH) + SessionPrefs.setPenSetting(app, "penButton", SessionPrefs.PEN_BUTTON_OFF) + SessionPrefs.setPenSeen(app) SessionPrefs.setUnfoldedControlsWithController(app, true) SessionPrefs.setNoXalia(app, false) session.edit().putInt("fpsLimit.steam", 30).commit() @@ -58,6 +61,8 @@ class SettingsResetTest { assertEquals("graphite", SessionPrefs.theme(app)) assertEquals(500, SessionPrefs.backDoubleTapMs(app)) + assertEquals(SessionPrefs.Pen(), SessionPrefs.pen(app)) + assertTrue(SessionPrefs.penSeen(app)) assertFalse(SessionPrefs.unfoldedControlsWithController(app)) assertTrue(SessionPrefs.noXalia(app)) assertFalse(session.contains("fpsLimit.steam")) @@ -82,6 +87,26 @@ class SettingsResetTest { assertFalse(prefs("steam_touch").contains("gyro")) } + @Test fun penSettingsKeepOnlyKnownValues() { + val settings = mapOf( + "pen" to listOf(SessionPrefs.PEN_MOUSE, SessionPrefs.PEN_TOUCH), + "penButton" to listOf(SessionPrefs.PEN_BUTTON_RIGHT, SessionPrefs.PEN_BUTTON_MIDDLE, SessionPrefs.PEN_BUTTON_OFF), + ) + fun read(key: String) = if (key == "pen") SessionPrefs.penMode(app) else SessionPrefs.penButton(app) + for ((key, values) in settings) { + assertTrue(key, SettingsReset.isSessionSetting(key)) + assertEquals(key, values.first(), read(key)) + for (value in values.drop(1)) { + SessionPrefs.setPenSetting(app, key, value) + assertEquals(key, value, read(key)) + } + session.edit().putString(key, "left").commit() + assertEquals(key, values.first(), read(key)) + assertThrows(IllegalArgumentException::class.java) { SessionPrefs.setPenSetting(app, key, "left") } + } + assertFalse(SettingsReset.isSessionSetting("penSeen")) + } + @Test fun aFailedDebugOptOutDoesNotHideTheEnabledPreference() { assertTrue(SessionPrefs.setGameDebugLogsEnabled(app, true)) val directory = java.io.File(com.droiddeck.launcher.runtime.LinuxRuntime.rootDir(app), diff --git a/app/src/test/java/com/droiddeck/launcher/ui/InputSurfaceTouchTest.kt b/app/src/test/java/com/droiddeck/launcher/ui/InputSurfaceTouchTest.kt index 27f0669620..3df8d65e0f 100644 --- a/app/src/test/java/com/droiddeck/launcher/ui/InputSurfaceTouchTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/ui/InputSurfaceTouchTest.kt @@ -98,6 +98,24 @@ class InputSurfaceTouchTest { assertFalse(state().isDown(PadState.A)) } + @Test fun aPenPressesButtonsLikeAFingerAndItsMissesKeepTheirHistory() { + touch(MotionEvent.ACTION_DOWN, 1 to (800f to 240f), tool = MotionEvent.TOOL_TYPE_STYLUS) + assertTrue(state().isDown(PadState.A)) + touch(MotionEvent.ACTION_UP, 1 to (800f to 240f), tool = MotionEvent.TOOL_TYPE_STYLUS) + assertFalse(state().isDown(PadState.A)) + val missed = mutableListOf>() + view.onTouchMiss = { event -> missed += (0 until event.historySize).map { event.getHistoricalEventTime(it) } + event.eventTime } + touch(MotionEvent.ACTION_DOWN, 1 to (500f to 100f), tool = MotionEvent.TOOL_TYPE_STYLUS) + val properties = arrayOf(MotionEvent.PointerProperties().apply { id = 1; toolType = MotionEvent.TOOL_TYPE_STYLUS }) + fun coords(x: Float) = arrayOf(MotionEvent.PointerCoords().apply { this.x = x; y = 100f; pressure = 0.5f }) + val move = MotionEvent.obtain(1000L, 2000L, MotionEvent.ACTION_MOVE, 1, properties, coords(510f), 0, 0, 1f, 1f, 0, 0, 0, 0) + move.addBatch(2003L, coords(520f), 0) + move.addBatch(2006L, coords(530f), 0) + try { view.dispatchTouchEvent(move) } finally { move.recycle() } + assertEquals(listOf(listOf(time - 1), listOf(2000L, 2003L, 2006L)), missed) + assertFalse(state().isDown(PadState.A)) + } + @Test fun keyboardShortcutOpensOnRelease() { touch(MotionEvent.ACTION_DOWN, 1 to (450f to 510f)) assertTrue(events.isEmpty()) diff --git a/app/src/test/java/com/droiddeck/launcher/ui/PenSettingsGroupTest.kt b/app/src/test/java/com/droiddeck/launcher/ui/PenSettingsGroupTest.kt new file mode 100644 index 0000000000..cc416ce52c --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/ui/PenSettingsGroupTest.kt @@ -0,0 +1,99 @@ +package com.droiddeck.launcher.ui + +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.rememberScrollState +import androidx.compose.foundation.verticalScroll +import androidx.compose.runtime.mutableStateOf +import androidx.compose.ui.Modifier +import androidx.compose.ui.test.* +import com.droiddeck.launcher.R +import com.droiddeck.launcher.gpu.FrameGen +import com.droiddeck.launcher.gpu.Lossless +import com.droiddeck.launcher.input.SecondScreenMode +import com.droiddeck.launcher.session.SessionPrefs +import com.droiddeck.launcher.session.SessionService +import org.junit.Assert.assertEquals +import org.junit.Assert.assertTrue +import org.junit.Rule +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.annotation.Config +import org.robolectric.annotation.GraphicsMode + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [28], qualifiers = "w800dp-h600dp") +@GraphicsMode(GraphicsMode.Mode.NATIVE) +class PenSettingsGroupTest { + @get:Rule val compose = createRobolectricComposeRule() + private val context get() = RuntimeEnvironment.getApplication() + + @Test fun theDrawerHasNoPenGroupUntilAPenIsKnown() { + compose.setContent { DroidDeckTheme { SessionDrawer(true, SessionDrawerPage.CONTROLLER, false, {}, actions(pen = null)) } } + compose.onNodeWithTag("setting-touch").assertExists() + compose.onNodeWithTag("setting-pen").assertDoesNotExist() + compose.onNodeWithTag("setting-pen-button").assertDoesNotExist() + } + + @Test fun theDrawerSavesPenPicks() { + val picks = mutableListOf>() + compose.setContent { + DroidDeckTheme { + SessionDrawer(true, SessionDrawerPage.CONTROLLER, false, {}, actions(SessionPrefs.Pen()) { k, v -> picks += k to v }) + } + } + compose.onNodeWithTag("setting-pen-button").performScrollTo().performClick() + compose.onNodeWithText(context.getString(R.string.gesture_middle_click)).performScrollTo().performClick() + compose.onNodeWithTag("setting-pen").performScrollTo().performClick() + compose.onNodeWithText(context.getString(R.string.mode_pen_touch)).performScrollTo().performClick() + compose.runOnIdle { + assertEquals(listOf("penButton" to SessionPrefs.PEN_BUTTON_MIDDLE, "pen" to SessionPrefs.PEN_TOUCH), picks) + } + } + + @Test fun theSideButtonRowIsHiddenInTouchMode() { + val pen = mutableStateOf(SessionPrefs.Pen()) + compose.setContent { DroidDeckTheme { Column { PenSettingsGroup(rememberMenuHost(), pen.value) { _, _ -> } } } } + compose.onNodeWithTag("setting-pen-button").assertExists() + compose.runOnIdle { pen.value = SessionPrefs.Pen(mode = SessionPrefs.PEN_TOUCH) } + compose.onNodeWithTag("setting-pen").assertExists() + compose.onNodeWithTag("setting-pen-button").assertDoesNotExist() + } + + @Test fun theControlsTabShowsThePenGroupAfterTheTouchRowsOnceAPenIsKnown() { + val pen = mutableStateOf(null) + val picks = mutableListOf>() + compose.setContent { + DroidDeckTheme { + Column(Modifier.verticalScroll(rememberScrollState())) { + ModeSheetRows(ModeSettings(mode = SessionService.MODE_DESKTOP, resolution = "auto", hdr = false, hdrReason = null, + touchMode = "auto", pen = pen.value, suspendPolicy = "auto", oscMode = "auto", directAudio = null, mic = null), + ModeSettingsActions(onResolution = {}, onHdr = {}, onTouch = {}, onPen = { k, v -> picks += k to v }, + onSuspendPolicy = {}, onOsc = {}, onInputSettings = {}, onDirectAudio = {}, onMic = {}, onDismiss = {}), + SheetTab.CONTROLS) + } + } + } + compose.onNodeWithTag("setting-touch").assertExists() + compose.onNodeWithTag("setting-pen").assertDoesNotExist() + compose.runOnIdle { pen.value = SessionPrefs.Pen() } + compose.onNodeWithTag("setting-pen-button").assertExists() + assertTrue(compose.onNodeWithTag("setting-touch").fetchSemanticsNode().positionInRoot.y < + compose.onNodeWithTag("setting-pen").fetchSemanticsNode().positionInRoot.y) + compose.onNodeWithTag("setting-pen-button").performScrollTo().performClick() + compose.onNodeWithText(context.getString(R.string.gesture_middle_click)).performScrollTo().performClick() + compose.runOnIdle { assertEquals(listOf("penButton" to SessionPrefs.PEN_BUTTON_MIDDLE), picks) } + } + + private fun actions(pen: SessionPrefs.Pen?, onPen: (String, String) -> Unit = { _, _ -> }) = + DrawerActions(steam = false, desktop = true, isHomeApp = false, androidApps = emptyList(), hudOn = false, + frameGen = FrameGen.Mode.OFF, lossless = Lossless.State.NONE, oscMode = "never", + suspendPolicy = "auto", backActionsInverted = false, + touchMode = "auto", touchAuto = "touchpad", pen = pen, onPen = onPen, fexPreset = "default", + secondScreenMode = SecondScreenMode.NONE, secondScreenDisplays = emptyList(), selectedSecondScreenDisplay = 1, + onHud = {}, onFrameGenPick = {}, onImportLossless = {}, onKeyboard = {}, onHardwareKeyboard = {}, + onSteamMenu = null, onQam = null, onOsc = {}, onSuspendPolicy = {}, onBackActionsInverted = {}, + onTouch = {}, onFexPreset = {}, onSecondScreenMode = {}, onSecondScreenDisplay = {}, + onLaunchAndroidApp = { _, _ -> }, onBackground = {}, onShareLogs = {}, onStop = {}, onClose = {}) +} diff --git a/docs/development/pen.md b/docs/development/pen.md new file mode 100644 index 0000000000..67da1d91d4 --- /dev/null +++ b/docs/development/pen.md @@ -0,0 +1,91 @@ +# Pen input + +A pen on the phone's screen, or an external drawing tablet, drives the session's pointer like a mouse: hover moves +the pointer, the tip holds the left button and the side button right-clicks. Fingers keep their touch mode. + +The settings are a "Pen" group after the touch settings, in the Steam and Desktop sheets' **Controls** tab and on +the session drawer's **Controller** page. The group is shown once the phone has a stylus device or a pen event was +seen; the first of either sets `penSeen`, which a settings reset keeps. Before Android 14 every touchscreen whose +driver reports a tool type has the stylus source, pen or not, so there a touchscreen counts only with its first pen +event. + +## Modes + +`pen`: Mouse (default) or Touch. + +| | Mouse | Touch | +|---|---|---| +| Hover | moves the pointer and the cursor | ignored | +| Tip | holds the left button | a finger in the current touch mode | +| Side button | as the Side button setting says | ignored; a Bluetooth pen key is an ordinary key | + +- With the picture on the glasses, or while a program in gamescope holds a pointer lock, Mouse drives the touchpad + as a finger does: a tap clicks and a drag moves the pointer relatively. +- App UI on top (the drawer, achievements, input settings, picture in picture) takes the pen as Android input, as + it takes a mouse. +- The Touch column is for a pen on the phone's screen. In Touch an external tablet stays on the mouse path, so its + hover moves the pointer. The stylus rules under Android capture apply in both modes. +- A touch mode change ends the pen whatever its route, so it cannot leave a pen click held + ([#318](https://github.com/Droid-Deck/DroidDeck/issues/318)). + +## Side button + +`penButton`: Right click (default), Middle click or Off. It applies to the pen's first barrel button. A second +barrel button, where a pen has one, sends the other click: middle when the setting is Right click, right otherwise. +Off sends nothing. The row is hidden in Touch. + +Some pens send their button as a key from a separate Bluetooth keyboard; the OPPO AI Pen sends `KEYCODE_F21`. Such a +key is the side button when its device is external, keyboard-only and not alphabetic, and has a word starting with +"pen" or "stylus" in its name. In Mouse the pen always takes it, so neither the program nor the key bindings see +it, and it acts only while the pen is near: a key held when the pen comes back presses the button. Pause, focus +loss, pointer-capture loss and removing its device forget it. In Touch it is an ordinary key. + +## Android capture + +- Pens are found by tool type (stylus or eraser), or by `SOURCE_STYLUS` when the tool type is unknown. External + tablets report `SOURCE_MOUSE | SOURCE_STYLUS`, so the source alone is not enough. Events that mix a finger and a + pen are split, and the finger keeps its route. +- Contact is taken at the top of `SessionActivity.onTouchEvent`, after the Compose UI and the on-screen controls + declined it, so the pen presses on-screen buttons like a finger. Hover is taken in `dispatchGenericMotionEvent` + before the mouse branch. The lower-screen game takes both. +- With Low latency mode on (the default) the window asks for unbuffered pointer dispatch, so pen samples are not + batched per frame. Every historical sample is forwarded in order, and `pointerSubset` keeps the history when it + splits an event. +- `PenStream` keeps proximity, tip and buttons. A `HOVER_EXIT` followed within 50 ms by a hover or a `DOWN` does not + leave proximity: Android splits a landing into an exit and a down with the same time, and a digitizer can flicker + out and in at the top of its hover range. A hover that sends no sample for 2 s leaves proximity. +- Stylus devices are not hardware mice, so a drawing tablet does not turn on pointer capture. +- On the desktop a hardware mouse makes KWin grab the pointer. A pen that comes near releases the grab, so it + moves KWin's pointer, and the mouse takes the grab back when it moves after the pen has left. +- Where the session hides the mouse pointer it also hides Android's stylus hover icon. + +## What ends the pen + +Everything that cancels touches: touch mode, touch route and pen setting changes, app UI opening, configuration +changes, surface resize, the game moving between screens, picture in picture, pause, focus loss, pointer-capture +loss and the activity's end. So do a change of the pen's own route and the removal of its device. `UP` with +`FLAG_CANCELED` and `ACTION_CANCEL` end it at once instead of lifting into hover. Ending the pen releases every +button and leaves proximity; `wl_pointer` has no cancel, so on the pointer a cancelled tap still clicks. + +## Compositor + +Each sample is one input record, type 13: the flags (in proximity, tip, right, middle) and the position in input +space as 24.8 fixed point. A record is the pen's whole state, so the next record makes good any lost one. + +`deliver_pen` emulates the pointer, as the touch `pointer_fallback` does: hover is motion, the tip is `BTN_LEFT`, +the side button `BTN_RIGHT` or `BTN_MIDDLE`, and a record without the in-proximity bit releases every button. A +button pressed or released mid-stroke keeps the implicit grab on the window the stroke started in. The +relative-motion origin is reset when the pen comes and goes, so its jump never reaches a program as relative +motion. Under a program's pointer lock only the buttons pass, which also covers the time before the app sees the +lock. + +The app logs `pen route -> (mode=)` and the compositor `[pen] gets the pen as the +pointer`. + +## Limits + +- A hardware eraser end clicks like the tip. +- The pen and a mouse share the pointer's buttons, so a release from one ends a press of the other, as with the + touch fallback. +- Some systems keep the pen's button for themselves, such as Samsung's Air actions. ColorOS opens its stylus + palette for the OPPO AI Pen's key unless that action is turned off in the system's stylus settings. diff --git a/tools/tests/pen_emulation_harness.c b/tools/tests/pen_emulation_harness.c new file mode 100644 index 0000000000..f058e77147 --- /dev/null +++ b/tools/tests/pen_emulation_harness.c @@ -0,0 +1,158 @@ +#include +#include +#include +#include + +#define BTN_LEFT 0x110 +#define BTN_RIGHT 0x111 +#define BTN_MIDDLE 0x112 +#define INPUT_SPACE_W 1920 +#define INPUT_SPACE_H 1080 +#include "input_msg.inc" +#include "pen_state.inc" + +enum { MOTION, PRESS, RELEASE }; +struct event { int kind; uint32_t button; double x, y; int raw_valid; }; +struct constraint { int is_lock; }; +static struct constraint *g_active_constraint; +static struct surface *g_grab; +static int g_raw_valid; +static struct event events[16]; +static int nevents, logs; +static uint32_t held; + +static void scene_size(int *w, int *h) { *w = 3840; *h = 2160; } +static void vkp_output_size(int *w, int *h) { *w = 0; *h = 0; } +static int vkp_output_to_scene(double ox, double oy, double *x, double *y) { + (void)ox; (void)oy; (void)x; (void)y; + return 0; +} +static void pointer_event(double x, double y, uint32_t button, int pressed) { + assert(!button && !pressed && nevents < 16); + events[nevents++] = (struct event){MOTION, 0, x, y, g_raw_valid}; + g_raw_valid = 1; +} +static void pointer_button(uint32_t button, int pressed) { + assert(button >= BTN_LEFT && button <= BTN_MIDDLE && nevents < 16); + uint32_t bit = 1u << (button - BTN_LEFT); + if (pressed) assert(!(held & bit)); + else assert(held & bit); + held = pressed ? held | bit : held & ~bit; + events[nevents++] = (struct event){pressed ? PRESS : RELEASE, button, 0, 0, g_raw_valid}; +} +static void pen_log_target(void) { logs++; } +#include "pen.inc" + +static void pen(int flags, double x, double y) { + struct input_msg m = {13, flags, (int)(x * 256), (int)(y * 256), 0}; + nevents = 0; + deliver_pen(&m); +} + +static void motion(int i, double x, double y) { + assert(events[i].kind == MOTION && events[i].x == x && events[i].y == y); +} + +static void button(int i, int kind, uint32_t code) { + assert(events[i].kind == kind && events[i].button == code); +} + +static void expect(int count, uint32_t buttons) { + assert(nevents == count && held == buttons); + assert(g_pen.buttons == (int)buttons); +} + +#define L (1u << 0) +#define R (1u << 1) +#define M (1u << 2) + +int main(int argc, char **argv) { + assert(argc == 2); + const char *c = argv[1]; + if (!strcmp(c, "hover")) { + pen(PEN_IN, 100.5, 50.25); + expect(1, 0); motion(0, 201, 100.5); + pen(PEN_IN, 2000, -4); + expect(1, 0); motion(0, 3839, 0); + assert(g_pen.in && logs == 2); + pen(0, 2000, -4); + expect(0, 0); + assert(!g_pen.in && logs == 2); + } else if (!strcmp(c, "tip")) { + pen(PEN_IN, 10, 10); + expect(1, 0); + pen(PEN_IN | PEN_TIP, 10, 10); + expect(2, L); motion(0, 20, 20); button(1, PRESS, BTN_LEFT); + pen(PEN_IN | PEN_TIP, 11, 10); + expect(1, L); motion(0, 22, 20); + pen(PEN_IN, 12, 10); + expect(2, 0); motion(0, 24, 20); button(1, RELEASE, BTN_LEFT); + pen(PEN_IN | PEN_TIP, 12, 10); + pen(PEN_IN, 12, 10); + pen(0, 12, 10); + expect(0, 0); + } else if (!strcmp(c, "buttons")) { + pen(PEN_IN | PEN_RIGHT, 10, 10); + expect(2, R); motion(0, 20, 20); button(1, PRESS, BTN_RIGHT); + pen(PEN_IN | PEN_RIGHT | PEN_MIDDLE, 10, 10); + expect(2, R | M); button(1, PRESS, BTN_MIDDLE); + pen(PEN_IN | PEN_MIDDLE | PEN_TIP, 11, 10); + expect(3, L | M); motion(0, 22, 20); button(1, PRESS, BTN_LEFT); button(2, RELEASE, BTN_RIGHT); + pen(PEN_IN | PEN_RIGHT, 12, 10); + expect(4, R); motion(0, 24, 20); button(1, PRESS, BTN_RIGHT); + button(2, RELEASE, BTN_LEFT); button(3, RELEASE, BTN_MIDDLE); + pen(PEN_IN, 12, 10); + expect(2, 0); button(1, RELEASE, BTN_RIGHT); + } else if (!strcmp(c, "out")) { + pen(PEN_IN | PEN_TIP | PEN_RIGHT | PEN_MIDDLE, 10, 10); + expect(4, L | R | M); motion(0, 20, 20); + button(1, PRESS, BTN_LEFT); button(2, PRESS, BTN_RIGHT); button(3, PRESS, BTN_MIDDLE); + pen(PEN_TIP | PEN_RIGHT | PEN_MIDDLE, 30, 30); + expect(3, 0); + button(0, RELEASE, BTN_LEFT); button(1, RELEASE, BTN_RIGHT); button(2, RELEASE, BTN_MIDDLE); + assert(!g_pen.in && !g_pen.buttons); + pen(0, 30, 30); + expect(0, 0); + } else if (!strcmp(c, "lock")) { + struct constraint lock = {1}, confine = {0}; + g_active_constraint = &lock; + pen(PEN_IN, 10, 10); + expect(0, 0); + assert(g_pen.in && logs == 1); + pen(PEN_IN | PEN_TIP, 11, 10); + expect(1, L); button(0, PRESS, BTN_LEFT); + pen(PEN_IN | PEN_TIP, 40, 10); + expect(0, L); + pen(PEN_IN | PEN_RIGHT, 40, 10); + expect(2, R); button(0, PRESS, BTN_RIGHT); button(1, RELEASE, BTN_LEFT); + pen(0, 40, 10); + expect(1, 0); button(0, RELEASE, BTN_RIGHT); + g_active_constraint = &confine; + pen(PEN_IN, 10, 10); + expect(1, 0); motion(0, 20, 20); + } else if (!strcmp(c, "lost")) { + pen(PEN_IN | PEN_TIP, 10, 10); + expect(2, L); motion(0, 20, 20); button(1, PRESS, BTN_LEFT); + pen(0, 10, 10); + expect(1, 0); button(0, RELEASE, BTN_LEFT); + pen(PEN_IN | PEN_TIP | PEN_MIDDLE, 10, 10); + expect(3, L | M); + pen(PEN_IN, 50, 10); + expect(3, 0); motion(0, 100, 20); button(1, RELEASE, BTN_LEFT); button(2, RELEASE, BTN_MIDDLE); + } else { + assert(!strcmp(c, "raw")); + g_raw_valid = 1; + pen(PEN_IN, 10, 10); + assert(!events[0].raw_valid && g_raw_valid); + pen(PEN_IN, 11, 10); + assert(events[0].raw_valid); + pen(PEN_IN | PEN_TIP, 12, 10); + assert(events[0].raw_valid && events[1].raw_valid); + pen(0, 12, 10); + assert(!g_raw_valid); + g_raw_valid = 1; + pen(0, 12, 10); + assert(g_raw_valid); + } + puts("PASS"); +} diff --git a/tools/tests/pen_pointer_harness.c b/tools/tests/pen_pointer_harness.c new file mode 100644 index 0000000000..01dd5b45b9 --- /dev/null +++ b/tools/tests/pen_pointer_harness.c @@ -0,0 +1,182 @@ +#include +#include +#include +#include +#include + +#define BTN_LEFT 0x110 +#define BTN_RIGHT 0x111 +#define BTN_MIDDLE 0x112 +#define INPUT_SPACE_W 1920 +#define INPUT_SPACE_H 1080 +#define WL_POINTER_FRAME_SINCE_VERSION 5 +#define WL_POINTER_BUTTON_STATE_RELEASED 0 +#define WL_POINTER_BUTTON_STATE_PRESSED 1 +#include "input_msg.inc" +#include "pen_state.inc" + +typedef int32_t wl_fixed_t; +struct wl_client { int id; }; +struct wl_resource { struct wl_client *client; }; +struct wl_display { int unused; }; +struct surface { struct wl_resource *resource; int placed, x, y; }; +struct seat_pointer { struct wl_resource *ptr, *focus; }; +struct constraint { int is_lock; struct surface *surface; }; + +static struct wl_client client_a, client_b; +static struct wl_resource surface_a = {&client_a}, surface_b = {&client_b}; +static struct wl_resource pointer_a = {&client_a}, pointer_b = {&client_b}; +static struct surface a = {&surface_a, 1, 0, 0}, b = {&surface_b, 1, 1000, 0}; +static struct seat_pointer g_ptrs[2] = {{&pointer_a, NULL}, {&pointer_b, NULL}}; +static int g_nptrs = 2; +static struct wl_display *g_display; +static struct constraint *g_active_constraint; +static struct surface *g_desktop, *g_grab, *g_key_target, *g_ime_click; +static double g_ptr_x, g_ptr_y, g_raw_x, g_raw_y; +static int g_ptr_known, g_raw_valid; +static atomic_int g_cursor_composite; + +enum { ENTER, LEAVE, PRESS, RELEASE }; +struct event { int kind; struct wl_resource *pointer, *surface; uint32_t button; }; +static struct event events[32]; +static int nevents; + +static void record(int kind, struct wl_resource *pointer, struct wl_resource *surface, uint32_t button) { + assert(nevents < 32); + events[nevents++] = (struct event){kind, pointer, surface, button}; +} + +static void scene_size(int *w, int *h) { *w = 2000; *h = 1000; } +static void vkp_output_size(int *w, int *h) { *w = 0; *h = 0; } +static int vkp_output_to_scene(double ox, double oy, double *x, double *y) { + (void)ox; (void)oy; (void)x; (void)y; + return 0; +} +static struct surface *toplevel_at(double x, double y) { + (void)y; + return x < 1000 ? &a : &b; +} +static void confine_clamp(struct constraint *k, double *x, double *y) { (void)k; (void)x; (void)y; } +static void schedule_render(void) {} +static void publish_query_state(void) {} +static void constraints_focus_entered(struct wl_resource *target, struct wl_client *client) { + (void)target; (void)client; +} +static void droiddeck_text_input_refocus(void) {} +static void pen_log_target(void) {} +static struct surface *dying; +static void wl_display_flush_clients(struct wl_display *display) { + (void)display; + if (!dying || !nevents || events[nevents - 1].kind != RELEASE) return; + if (g_grab == dying) g_grab = NULL; + if (g_pen.grab == dying) g_pen.grab = NULL; + dying = NULL; +} +static uint32_t now_ms(void) { return 0; } +static uint32_t wl_display_next_serial(struct wl_display *display) { (void)display; return 0; } +static int wl_resource_get_version(struct wl_resource *r) { (void)r; return 7; } +static struct wl_client *wl_resource_get_client(struct wl_resource *r) { return r->client; } +static wl_fixed_t wl_fixed_from_double(double d) { return (wl_fixed_t)(d * 256); } +static int send_relative_motion(struct wl_client *client, double dx, double dy) { + (void)client; (void)dx; (void)dy; + return 0; +} +static void wl_pointer_send_enter(struct wl_resource *p, uint32_t serial, struct wl_resource *s, + wl_fixed_t x, wl_fixed_t y) { + (void)serial; (void)x; (void)y; + record(ENTER, p, s, 0); +} +static void wl_pointer_send_leave(struct wl_resource *p, uint32_t serial, struct wl_resource *s) { + (void)serial; + record(LEAVE, p, s, 0); +} +static void wl_pointer_send_motion(struct wl_resource *p, uint32_t t, wl_fixed_t x, wl_fixed_t y) { + (void)p; (void)t; (void)x; (void)y; +} +static void wl_pointer_send_frame(struct wl_resource *p) { (void)p; } +static void wl_pointer_send_button(struct wl_resource *p, uint32_t serial, uint32_t t, uint32_t button, + uint32_t state) { + (void)serial; (void)t; + record(state == WL_POINTER_BUTTON_STATE_PRESSED ? PRESS : RELEASE, p, NULL, button); +} +#include "pointer.inc" +#include "pen.inc" + +static void pen(int flags, double x) { + struct input_msg m = {13, flags, (int)(x * INPUT_SPACE_W / 2000 * 256), 100 * 256, 0}; + nevents = 0; + deliver_pen(&m); +} + +static void focus(int i, struct wl_resource *pointer, struct wl_resource *surface) { + assert(i < nevents && events[i].kind == ENTER && events[i].pointer == pointer && events[i].surface == surface); +} + +static void left(int i, struct wl_resource *pointer, struct wl_resource *surface) { + assert(i < nevents && events[i].kind == LEAVE && events[i].pointer == pointer && events[i].surface == surface); +} + +static void button(int i, int kind, uint32_t code) { + assert(i < nevents && events[i].kind == kind && events[i].pointer == &pointer_a && events[i].button == code); +} + +static void hover_away(void) { + assert(!g_grab); + pen(PEN_IN, 1500); + assert(nevents == 2); + left(0, &pointer_a, &surface_a); focus(1, &pointer_b, &surface_b); +} + +int main(int argc, char **argv) { + assert(argc == 2); + const char *c = argv[1]; + pen(PEN_IN, 500); + assert(nevents == 1); + focus(0, &pointer_a, &surface_a); + pen(PEN_IN | PEN_TIP, 500); + assert(nevents == 1 && g_grab == &a); + button(0, PRESS, BTN_LEFT); + if (!strcmp(c, "side")) { + pen(PEN_IN | PEN_TIP | PEN_RIGHT, 600); + assert(nevents == 1 && g_grab == &a); + button(0, PRESS, BTN_RIGHT); + pen(PEN_IN | PEN_TIP, 700); + assert(nevents == 1 && g_grab == &a); + button(0, RELEASE, BTN_RIGHT); + pen(PEN_IN | PEN_TIP, 1500); + assert(nevents == 0 && g_grab == &a); + pen(PEN_IN, 1500); + assert(nevents == 1); + button(0, RELEASE, BTN_LEFT); + } else if (!strcmp(c, "swap")) { + pen(PEN_IN | PEN_RIGHT, 600); + assert(nevents == 2 && g_grab == &a); + button(0, PRESS, BTN_RIGHT); button(1, RELEASE, BTN_LEFT); + pen(PEN_IN | PEN_RIGHT, 1500); + assert(nevents == 0 && g_grab == &a); + pen(PEN_IN, 1500); + assert(nevents == 1); + button(0, RELEASE, BTN_RIGHT); + } else if (!strcmp(c, "mouse")) { + pointer_button(BTN_RIGHT, 1); + pointer_button(BTN_RIGHT, 0); + pen(PEN_IN | PEN_TIP, 1500); + assert(nevents == 0 && g_grab == &a); + pen(PEN_IN, 1500); + assert(nevents == 1); + button(0, RELEASE, BTN_LEFT); + } else { + assert(!strcmp(c, "gone")); + pen(PEN_IN | PEN_TIP | PEN_RIGHT, 600); + assert(nevents == 1 && g_grab == &a); + dying = &a; + pen(PEN_IN | PEN_TIP, 700); + assert(nevents == 1 && !dying && !g_grab && !g_pen.grab); + button(0, RELEASE, BTN_RIGHT); + pen(PEN_IN, 700); + assert(nevents == 1); + button(0, RELEASE, BTN_LEFT); + } + hover_away(); + puts("PASS"); +} diff --git a/tools/tests/test_output_refresh.py b/tools/tests/test_output_refresh.py index 456e230d4e..5ad9aa36ed 100644 --- a/tools/tests/test_output_refresh.py +++ b/tools/tests/test_output_refresh.py @@ -70,6 +70,7 @@ static void send_toplevel_configure(struct surface *s) { (void)s; resizes++; } #define stream_forget_all(...) ((void)0) #define pointer_delta(...) ((void)0) #define deliver_touch(m) ((void)0) +#define deliver_pen(m) ((void)0) #define deliver_pointer(m) (pointers++) static void on_vsync(int64_t ns) { assert(ns > 0); ticks++; } static void vk_present_set_scale_mode(int mode, int alignment) { diff --git a/tools/tests/test_pen_emulation.py b/tools/tests/test_pen_emulation.py new file mode 100644 index 0000000000..1cf2b4b485 --- /dev/null +++ b/tools/tests/test_pen_emulation.py @@ -0,0 +1,102 @@ +import os +from pathlib import Path +import re +import shutil +import subprocess +import tempfile +import unittest + +REPO = Path(__file__).resolve().parents[2] +SOURCE = REPO / 'app/src/main/cpp/waylandcomp/src/compositor.c' +PEN_STREAM = REPO / 'app/src/main/java/com/droiddeck/launcher/input/PenStream.kt' + + +def function(source, signature): + start = source.index(signature) + return source[start:source.index('\n}\n', start) + 3] + + +class PenEmulationTest(unittest.TestCase): + @classmethod + def setUpClass(cls): + compiler = shutil.which(os.environ.get('CC', 'cc')) + if compiler is None: + raise unittest.SkipTest('A host C compiler is required') + source = SOURCE.read_text() + cls.temp = tempfile.TemporaryDirectory(prefix='droiddeck-pen-emulation-') + cls.addClassCleanup(cls.temp.cleanup) + folder = Path(cls.temp.name) + start = source.index('struct input_msg {') + (folder / 'input_msg.inc').write_text(source[start:source.index('\n', start) + 1]) + start = source.index('#define PEN_IN ') + end = source.index('static struct pen_state g_pen;\n', start) + (folder / 'pen_state.inc').write_text(source[start:source.index('\n', end) + 1]) + (folder / 'pen.inc').write_text(''.join(function(source, signature) for signature in ( + 'static int pointer_locked_for_cursor(void) {', + 'static void input_output_to_scene(double px, double py, double *x, double *y) {', + 'static int pen_buttons(int flags) {', + 'static void pen_release(int buttons) {', + 'static void deliver_pen(const struct input_msg *m) {'))) + start = source.index('#define for_each_pointer_of(client, sp)') + (folder / 'pointer.inc').write_text( + function(source, 'static struct seat_pointer *pointer_for(struct wl_client *client) {') + + source[start:source.index('#define for_each_keyboard_of', start)] + + ''.join(function(source, signature) for signature in ( + 'static void pointer_focus(struct wl_resource *target, wl_fixed_t fx, wl_fixed_t fy) {', + 'static void pointer_input(double x, double y, int relative, uint32_t button, int pressed) {', + 'static void pointer_event(double x, double y, uint32_t button, int pressed) {', + 'static void pointer_button(uint32_t button, int pressed) {'))) + cls.binary = folder / 'pen-test' + cls.pointer_binary = folder / 'pen-pointer-test' + for harness, binary in (('pen_emulation_harness.c', cls.binary), + ('pen_pointer_harness.c', cls.pointer_binary)): + subprocess.run([compiler, '-std=c11', '-Wall', '-Wextra', '-Werror', '-I', str(folder), + str(REPO / 'tools/tests' / harness), + '-o', str(binary)], check=True, capture_output=True, text=True) + + def check_case(self, case, binary=None): + result = subprocess.run([str(binary or self.binary), case], capture_output=True, text=True) + self.assertEqual(result.returncode, 0, result.stdout + result.stderr) + + def test_flags_match_the_kotlin_pen_stream(self): + native = dict(re.findall(r'^#define (PEN_[A-Z]+) \(1 << (\d+)\)$', SOURCE.read_text(), re.MULTILINE)) + kotlin = dict(re.findall(r'^\s*const val (PEN_[A-Z]+) = 1 shl (\d+)$', PEN_STREAM.read_text(), re.MULTILINE)) + self.assertEqual({'PEN_IN', 'PEN_TIP', 'PEN_RIGHT', 'PEN_MIDDLE'}, set(native)) + self.assertEqual(native, kotlin) + + def test_hover_moves_the_pointer_in_scene_coordinates(self): + self.check_case('hover') + + def test_tip_presses_and_releases_the_left_button_once(self): + self.check_case('tip') + + def test_buttons_press_after_motion_and_before_releases(self): + self.check_case('buttons') + + def test_leaving_proximity_releases_every_button_without_motion(self): + self.check_case('out') + + def test_pointer_lock_blocks_motion_but_passes_buttons(self): + self.check_case('lock') + + def test_lost_records_still_release_held_buttons(self): + self.check_case('lost') + + def test_relative_origin_resets_when_proximity_starts_and_ends(self): + self.check_case('raw') + + def test_side_button_release_keeps_the_grab(self): + self.check_case('side', self.pointer_binary) + + def test_swapping_buttons_in_one_record_keeps_the_grab(self): + self.check_case('swap', self.pointer_binary) + + def test_a_button_release_from_another_pointer_keeps_the_grab(self): + self.check_case('mouse', self.pointer_binary) + + def test_a_window_closed_by_a_button_release_is_not_grabbed_again(self): + self.check_case('gone', self.pointer_binary) + + +if __name__ == '__main__': + unittest.main() diff --git a/tools/tests/test_pen_localization.py b/tools/tests/test_pen_localization.py new file mode 100644 index 0000000000..55596d1915 --- /dev/null +++ b/tools/tests/test_pen_localization.py @@ -0,0 +1,39 @@ +from pathlib import Path +import re +import unittest +import xml.etree.ElementTree as ET + + +ROOT = Path(__file__).resolve().parents[2] +RES = ROOT / "app/src/main/res" + + +def strings(path): + return { + element.attrib["name"]: "".join(element.itertext()) + for element in ET.parse(path).getroot() + if element.tag == "string" and element.get("translatable") != "false" + } + + +class PenLocalizationTest(unittest.TestCase): + @classmethod + def setUpClass(cls): + cls.required = [key for key in strings(RES / "values/strings.xml") if key.startswith("mode_pen")] + language_source = (ROOT / "app/src/main/java/com/droiddeck/launcher/core/AppLanguage.kt").read_text(encoding="utf-8") + tags = re.findall(r'"([a-zA-Z0-9-]+)"', re.search(r"val supported:.*?listOf\((.*?)\)", language_source, re.S).group(1)) + special = {"id": "in", "es-419": "b+es+419", "pt-BR": "pt"} + cls.locales = { + tag: strings(RES / f"values-{special.get(tag, tag.replace('-', '-r'))}/strings.xml") + for tag in tags if tag != "en" + } + + def test_every_supported_language_translates_the_pen_settings(self): + for locale, translated in self.locales.items(): + for key in self.required: + with self.subTest(locale=locale, key=key): + self.assertTrue(translated.get(key, "").strip(), "Missing translation would fall back to English") + + +if __name__ == "__main__": + unittest.main()