scrcpy icon indicating copy to clipboard operation
scrcpy copied to clipboard

Fix hardcode deviceId while inject touch event.

Open baker915 opened this issue 2 years ago • 12 comments

AOSP: https://android.googlesource.com/platform/frameworks/base.git/+/refs/heads/android10-release/cmds/input/src/com/android/commands/input/Input.java#359

fix touch events not working on some unity game

baker915 avatar Feb 27 '23 14:02 baker915

Thank you for the PR.

This is the alternative mentioned in 40febf4a91f0d9c9d9e1a6390930639b3bf5a70e, so it's probably ok if it fixes the problem for some games.

Could you mention the game(s) that you tested which is (are) fixed by this change?

rom1v avatar Feb 27 '23 14:02 rom1v

Thanks for you reply

This is the download link of game i've tested: https://ugame.9game.cn/game/downloadGame?pack.cooperateModelId=235099&pack.id=47093428

baker915 avatar Feb 27 '23 14:02 baker915

This is the download link of game i've tested: https://ugame.9game.cn/game/downloadGame?pack.cooperateModelId=235099&pack.id=47093428

I didn't see any issue in this game with Scrcpy 1.25.

Plus I don't think it will work. In my touch event tester app the device id is still -1

image

This is because Android set device id of all injected events to -1, unless the event is from InputFilter (accessibility service).

https://cs.android.com/android/platform/superproject/+/master:frameworks/native/services/inputflinger/dispatcher/InputDispatcher.cpp;l=4197-4206;drc=f3f3a95030a6811f722aced141e15e29e2105d11

    // For all injected events, set device id = VIRTUAL_KEYBOARD_ID. The only exception is events
    // that have gone through the InputFilter. If the event passed through the InputFilter, assign
    // the provided device id. If the InputFilter is accessibility, and it modifies or synthesizes
    // the injected event, it is responsible for setting POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY.
    // For those events, we will set FLAG_IS_ACCESSIBILITY_EVENT to allow apps to distinguish them
    // from events that originate from actual hardware.
    int32_t resolvedDeviceId = VIRTUAL_KEYBOARD_ID;
    if (policyFlags & POLICY_FLAG_FILTERED) {
        resolvedDeviceId = event->getDeviceId();
    }

The only method to get real device id is using AOA HID or uinput.

yume-chan avatar Feb 28 '23 08:02 yume-chan

This is the download link of game i've tested: https://ugame.9game.cn/game/downloadGame?pack.cooperateModelId=235099&pack.id=47093428

I didn't see any issue in this game with Scrcpy 1.25.

Plus I don't think it will work. In my touch event tester app the device id is still -1

image

This is because Android set device id of all injected events to -1, unless the event is from InputFilter (accessibility service).

https://cs.android.com/android/platform/superproject/+/master:frameworks/native/services/inputflinger/dispatcher/InputDispatcher.cpp;l=4197-4206;drc=f3f3a95030a6811f722aced141e15e29e2105d11

    // For all injected events, set device id = VIRTUAL_KEYBOARD_ID. The only exception is events
    // that have gone through the InputFilter. If the event passed through the InputFilter, assign
    // the provided device id. If the InputFilter is accessibility, and it modifies or synthesizes
    // the injected event, it is responsible for setting POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY.
    // For those events, we will set FLAG_IS_ACCESSIBILITY_EVENT to allow apps to distinguish them
    // from events that originate from actual hardware.
    int32_t resolvedDeviceId = VIRTUAL_KEYBOARD_ID;
    if (policyFlags & POLICY_FLAG_FILTERED) {
        resolvedDeviceId = event->getDeviceId();
    }

The only method to get real device id is using AOA HID or uinput.

Thanks.

I ran this app in a android in container,like redroid.I think maybe this is the difference Is it possible that you provide me touch event tester app apk you metioned?. So i can test in my environment, then i can provide more information

Btw this issue only happend on main scene

baker915 avatar Feb 28 '23 14:02 baker915

@baker915 Did you test that the change proposed in this MR actually works for the game you mentioned?

rom1v avatar Feb 28 '23 14:02 rom1v

@baker915 Did you test that the change proposed in this MR actually works for the game you mentioned?

I did,and i tested other apps,everything works fine.

baker915 avatar Feb 28 '23 14:02 baker915

OK so without this MR, it does not work, with this MR, it works? If so, I think this could be merged.

On which Android version?

rom1v avatar Feb 28 '23 15:02 rom1v

If so, I think this could be merged.

(But I'm a bit afraid that it could break other use cases…)

rom1v avatar Feb 28 '23 16:02 rom1v

Is it possible that you provide me touch event tester app apk you metioned?

Source code: https://github.com/yume-chan/android-motion-event-tester

APK: com.companyname.AndroidApp1.zip

Touch or click in the empty area, it will display properties of received events.

yume-chan avatar Feb 28 '23 17:02 yume-chan

OK so without this MR, it does not work, with this MR, it works? If so, I think this could be merged.

On which Android version?

on Android 10

baker915 avatar Feb 28 '23 17:02 baker915

If so, I think this could be merged.

(But I'm a bit afraid that it could break other use cases…)

Agreed!I'm going to test more devices and read the source code @yume-chan metioned to figure out this MR is necessary.

baker915 avatar Feb 28 '23 17:02 baker915

Is it possible that you provide me touch event tester app apk you metioned?

Source code: https://github.com/yume-chan/android-motion-event-tester

APK: com.companyname.AndroidApp1.zip

Touch or click in the empty area, it will display properties of received events.

Thanks. I will test it in my environment.

baker915 avatar Feb 28 '23 18:02 baker915