Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can you add support for mobile gyroscope? #1155

Open
wswtst2007 opened this issue Dec 4, 2022 · 19 comments
Open

Can you add support for mobile gyroscope? #1155

wswtst2007 opened this issue Dec 4, 2022 · 19 comments

Comments

@wswtst2007
Copy link

Can you add support for mobile gyroscope?

@sudhanshugairola
Copy link

Not support with host. Motion sensing not possible yet with rtsp.

@anthonywu1234
Copy link

Rewasd junior app can using the mobile gyroscope as ns pro/DS4 controller gyro by IP connecting.But moonlight-steam's virtual controller always occupy the first place in my PC lead to Rewasd junior app fail.Can you add a setting let the user choose not to open the moonlight-steam's virtual handle?

@baflo
Copy link

baflo commented Jul 30, 2023

Seems like moonlight-qt supports gyroscope/motion now. Any chance this can be adopted on the Android version?
moonlight-stream/moonlight-qt#960 (comment)

@cgutman
Copy link
Member

cgutman commented Jul 31, 2023

Gamepad motion is already supported with 5c6eaf2, but I'm leaving this issue open until I implement the plan detailed here

@CnC-ode
Copy link

CnC-ode commented Jul 31, 2023

hello

@cgutman
is there any nightly builds for the android version?
if not, is there any build including this commit 5c6eaf2?

Edit1:
Ok, couldn't find any info about nightly builds for the android repo, so I stopped being lazy and compiled a debug apk!

Couldn't get host to read the gyro data from my client thought... Tested with:

Client: latest main branch debug build (nvidia shield pro)
Host: latest sunshine nightly build (0.20.0.f3a257b1509f0c39cd179319bae8fc8df92c5805)
Controller: 8BitDo Pro 2 (connected through usb)

Tried emulating with ds4 on sunshine gamepads config and with forced moonlight gamepad driver checked and unchecked...

Note 1: I can confirm that the shield detects the gyro when the gamepad is connected through usb
Note 2: I can confirm that when connecting controller on the host the gyro does function~~

Am I missing something?

Edit2:
apparentely yes...:

// Hide gamepad motion sensor option when running on OSes before Android 12.
// Support for motion, LED, battery, and other extensions were introduced in S.
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
PreferenceCategory category =
(PreferenceCategory) findPreference("category_gamepad_settings");
category.removePreference(findPreference("checkbox_gamepad_motion_sensors"));
}

The code disables it for sdk versions < 31, and the shield is level 30...

@cgutman is there any fallback method that can be implemented on sdk versions < 31?

thanks

@baflo
Copy link

baflo commented Aug 25, 2023

I couldn't make it work on an Android 12 phone (Pixel 3) neither with nonRootDebug nor nonRootRelease build target.

However, I managed to make it work using moonlight-qt on another PC with an Nintendo Switch Pro Controller. So, at least my host-side with Sunshine works.

Are the any other requirement except from having Android 12?

EDIT 1: Apparently, according to my debugging, the Switch Pro Controller's acceleration sensor is not detected by the SensorManager.

EDIT 2: 8bitdo sf30pro doesn't work either.

@bassderek
Copy link

I wasn't able to get this working either, installed sunshine nightly as well as built the up to date android app. Doing some debugging it seems to not even be detecting that the android device has a gyro sensor (this is on an abxylute handheld streaming device).

@baflo
Copy link

baflo commented Aug 29, 2023

Exactly, while debugging the provided InputDevice's SensorManager doesnt return any sensor at all.

I wasn't able to verify thats Android supports gyro sensors of gamepads at all. Neither I could find definite documentation on this nor verify it with other games using my Switch Pro Controller and 8bitdo SF30 Pro.

@comanel
Copy link

comanel commented Aug 30, 2023

@baflo & @cgutman i was able to make it work after tinkering a bit with it, inspired by https://github.com/Williangalvani/moonlight-android/tree/gyro_to_mouse_cleaned.

SensorManager won't return a sensor if you don't register a sensor event listener from the device itself.
Once you do that, all you need is to override the onSensorChanged, get the (short)event.values.

        public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
            float dx = event.values[0];
            float dy = event.values[1];
            float dz = event.values[2];

            short x = (short) (dx * -gyroSensitivity);
            short y = (short) (dy * gyroSensitivity);
            short z = (short) (dz * gyroSensitivity);
            controllerHandler.onGyro(x, y, z);

        }
    }

Afterwards, pass them on to controllerHandler.onGyro(), where instead of sending controllerInputPacket, i simply send controllerMotionEvent.

public void onGyro(short x, short y, short z) {
        //defaultContext.rightStickX = (short) Math.max(Math.min(x*1000, 32000), -32000);
        //defaultContext.rightStickY = (short) Math.max(Math.min(-y*1000, 32000), -32000);

        //sendControllerInputPacket(defaultContext);

//this is where we get fucky with pitch yaw roll, after trying multiple combinations this is what seems to do the trick
        conn.sendControllerMotionEvent((byte) defaultContext.controllerNumber,
                MoonBridge.LI_MOTION_TYPE_GYRO,
                -y,
                -x,
                z);
    }

I didn't fork anything, i just rebuilt the apk locally, because as @cgutman said, it's just a starting point. At the moment it doesn't have a fallback if the device doesn't have motion sensors, nor does it take into account device orientation, not to mention that the sensor listener will impact battery life since it's permanently registered...
For me it works wonderfully, i use it with an OnePlus7 strapped into a razer Kishi and gyro stuff is mapped to whatever using Steam Input.

@comanel
Copy link

comanel commented Aug 30, 2023

Oh, and any nightly greater than 5c6eaf2 , tested with a DS4 controller, and gyro data is passed through flawlessly (as long as sunshine host emulates a ds4 controller)

@curtisfletcher
Copy link

@baflo & @cgutman i was able to make it work after tinkering a bit with it, inspired by https://github.com/Williangalvani/moonlight-android/tree/gyro_to_mouse_cleaned.

SensorManager won't return a sensor if you don't register a sensor event listener from the device itself. Once you do that, all you need is to override the onSensorChanged, get the (short)event.values.

        public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
            float dx = event.values[0];
            float dy = event.values[1];
            float dz = event.values[2];

            short x = (short) (dx * -gyroSensitivity);
            short y = (short) (dy * gyroSensitivity);
            short z = (short) (dz * gyroSensitivity);
            controllerHandler.onGyro(x, y, z);

        }
    }

Afterwards, pass them on to controllerHandler.onGyro(), where instead of sending controllerInputPacket, i simply send controllerMotionEvent.

public void onGyro(short x, short y, short z) {
        //defaultContext.rightStickX = (short) Math.max(Math.min(x*1000, 32000), -32000);
        //defaultContext.rightStickY = (short) Math.max(Math.min(-y*1000, 32000), -32000);

        //sendControllerInputPacket(defaultContext);

//this is where we get fucky with pitch yaw roll, after trying multiple combinations this is what seems to do the trick
        conn.sendControllerMotionEvent((byte) defaultContext.controllerNumber,
                MoonBridge.LI_MOTION_TYPE_GYRO,
                -y,
                -x,
                z);
    }

I didn't fork anything, i just rebuilt the apk locally, because as @cgutman said, it's just a starting point. At the moment it doesn't have a fallback if the device doesn't have motion sensors, nor does it take into account device orientation, not to mention that the sensor listener will impact battery life since it's permanently registered... For me it works wonderfully, i use it with an OnePlus7 strapped into a razer Kishi and gyro stuff is mapped to whatever using Steam Input.

Would you please create a fork of what you created?

I tried to implemented what you said you did, but I couldn't get it to work. There weren't any errors, the gyro just still didn't work.

I've also got a razer kishi and it would be awesome if it had motion controls.

@mebalzer
Copy link

mebalzer commented Sep 8, 2023

@curtisfletcher have you worked with @william Gavani’s joystick build? Does this work as is, or do I need to add your code to it as well?

Also, have tried to pass this on to ‘DS Windows’?

@comanel
Copy link

comanel commented Sep 8, 2023

@curtisfletcher make sure sunshine emulates a DS4 controller.
this is the branch you're looking for.
https://github.com/comanel/moonlight-android/tree/newGyro

@curtisfletcher
Copy link

@curtisfletcher make sure sunshine emulates a DS4 controller

Thanks. I did try that but I think I just butchered the code. The functions you edited looked different in the version of moonlight I downloaded. It's okay, Im happy to use a ds4 for now until this is added as a feature.

@FishOrBear
Copy link

Can I use the motion control of the switch pro controller on a TV with Android 10.0?

I tried it but it doesn't seem to work

@curtisfletcher
Copy link

I couldn't get it to work either (but with a ds4)

I think it might require android 12. https://github.com/moonlight-stream/moonlight-android/releases/tag/v12.0

@nilsher
Copy link

nilsher commented Jan 28, 2024

Can't this one be closed ? V12.0 release says gyro has been implemented?

@baflo
Copy link

baflo commented Feb 18, 2024

Can't this one be closed ? V12.0 release says gyro has been implemented?

I'm not sure. The release notes claim to have native support for PS4/5 controllers. At least for me it works neither with a Switch Pro Controller nor with an Android phone's integrated gyro.

Can anybody else confirm?

@nilsher
Copy link

nilsher commented Feb 18, 2024

I can confirm that Gyro works on an Ayn Odin 2 Android device.
Had to enable "Emulate gamepad motion sensor"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests