xyzz / openmw-android

OpenMW for Android
https://omw.xyz.is/
GNU General Public License v3.0
325 stars 87 forks source link

Game crashes when plugging in usb-c controller #60

Closed j13u11fr09 closed 3 years ago

j13u11fr09 commented 3 years ago

Describe the bug If OpenMW has already loaded and I plug in a usb-c controller (Gamesir x2), OpenMW crashes to the phone's home screen. If I have the controller already plugged in and try to launch OpenMW, I'm immediately sent to the phone's home screen.

To Reproduce Steps to reproduce the behavior:

  1. Launch OpenMW.
  2. Plug in usb-c controller (Gamesir x2).
  3. OpenMW crashes

OR:

  1. Plug in usb-c controller (Gamesir x2).
  2. Launch OpenMW
  3. OpenMW immediately crashes

Expected behavior The Gamesir x2 controller works fine with the stable (0.46.0-38) build from the playstore.

Screenshots Not applicable

Smartphone (please complete the following information):

Additional context The Gamesir controller works fine using the 0.46.0-38 (stable) build from the playstore.

licaon-kter commented 3 years ago

Related to https://www.xda-developers.com/android-11-trouble-getting-games-recognize-their-controllers/ ?

j13u11fr09 commented 3 years ago

No, the controller works fine otherwise. As stated, the controller works in the stable build of OpenMW. Only crashes there nightly build of OpenMW.

j13u11fr09 commented 3 years ago

Did a little bit of digging, installing older versions of the nightly (0.47.0) build: the issue starts at version 0.47.0-40. I'm able to use version 0.47.0-39 with the controller just fine.

ainodalok commented 3 years ago

@j13u11fr09 @xyzz This is related to this https://github.com/xyzz/openmw-android/commit/3497b8113fe9fe74ea232855160eeb23412f4a6d specific commit which was pushed to the latest stable release published on Google Play Store recently.

If the app targets Android Q (10) (targetSDK 29) it is required to get permission to read from USB device otherwise security exception is thrown.

SDL 2.0.14 in its HIDDeviceUSB.java particularly in one of the latest commits (https://github.com/libsdl-org/SDL/commit/eea450bc99925195beb649310d22a9d22dcc6a3f) addresses this issue by implementing exception handling in the getSerialNumber() method so that the app wouldn't crash, but I suppose requesting permission is up to app's developer himself.

Here is an error log I get on my MIUI 12 Android 10 device when I connect either mouse or Dualshock 3:

java.lang.RuntimeException: Unable to start activity ComponentInfo{is.xyz.omw/ui.activity.GameActivity}: java.lang.SecurityException: User has not given 10249/is.xyz.omw permission to access device /dev/bus/usb/001/002
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3312)
 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3461)
 at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
 at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
 at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2048)
 at android.os.Handler.dispatchMessage(Handler.java:107)
 at android.os.Looper.loop(Looper.java:227)
 at android.app.ActivityThread.main(ActivityThread.java:7582)
 at java.lang.reflect.Method.invoke(Native Method)
 at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:953)
Caused by: java.lang.SecurityException: User has not given 10249/is.xyz.omw permission to access device /dev/bus/usb/001/002
 at android.os.Parcel.createException(Parcel.java:2074)
 at android.os.Parcel.readException(Parcel.java:2042)
 at android.os.Parcel.readException(Parcel.java:1990)
 at android.hardware.usb.IUsbSerialReader$Stub$Proxy.getSerial(IUsbSerialReader.java:123)
 at android.hardware.usb.UsbDevice.getSerialNumber(UsbDevice.java:143)
 

at org.libsdl.app.HIDDeviceUSB.getSerialNumber(HIDDeviceUSB.java:56)

at org.libsdl.app.HIDDeviceManager.connectHIDDeviceUSB(HIDDeviceManager.java:362) at org.libsdl.app.HIDDeviceManager.handleUsbDeviceAttached(HIDDeviceManager.java:324) at org.libsdl.app.HIDDeviceManager.initializeUSB(HIDDeviceManager.java:228) at org.libsdl.app.HIDDeviceManager.(HIDDeviceManager.java:152) at org.libsdl.app.HIDDeviceManager.acquire(HIDDeviceManager.java:36) at org.libsdl.app.SDLActivity.onCreate(SDLActivity.java:252) at ui.activity.GameActivity.onCreate(GameActivity.kt:106) at android.app.Activity.performCreate(Activity.java:7893) at android.app.Activity.performCreate(Activity.java:7880) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3287) ... 11 more Caused by: android.os.RemoteException: Remote stack trace: at com.android.server.usb.UsbUserSettingsManager.checkPermission(UsbUserSettingsManager.java:177) at com.android.server.usb.UsbSerialReader.getSerial(UsbSerialReader.java:96) at android.hardware.usb.IUsbSerialReader$Stub.onTransact(IUsbSerialReader.java:84) at android.os.Binder.execTransactInternal(Binder.java:1021) at android.os.Binder.execTransact(Binder.java:994)
xyzz commented 3 years ago

Okay I've applied this patch (it doesn't seem the serial number is actually used anywhere), once this build completes - https://github.com/xyzz/openmw-android/actions/runs/610780501 - could you please confirm whether it fixes the problem.

ainodalok commented 3 years ago

Oh, suddenly it just works without any permission requests on my end! Both for dualshock 3 and keyboard/mouse combo through usb hub.

Hopefully this resolves issue for @j13u11fr09, though removing the logging itself as in next https://github.com/libsdl-org/SDL/commit/68e1731e02fab9f94b51bc739932547c3a651ee2 commit might be a good idea to improve performance since SDL developers themselves don't consider it useful at all.

j13u11fr09 commented 3 years ago

I installed the omw-debug-b2a6ae7-arm64 version linked above and the usb device now works correctly: openmw doesn't crash. This fixed the problem.