-
Notifications
You must be signed in to change notification settings - Fork 638
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
52b0b45
commit 4028a62
Showing
18 changed files
with
216 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,5 +9,6 @@ public enum MotionInputBackendType : byte | |
Invalid, | ||
GamepadDriver, | ||
CemuHook, | ||
Handheld, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net9.0</TargetFramework> | ||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Ryujinx.Input\Ryujinx.Input.csproj" /> | ||
<PackageReference Include="Alimer.Bindings.SDL" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
using SDL3; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Numerics; | ||
using static SDL3.SDL3; | ||
|
||
namespace Ryujinx.Input.SDL3 | ||
{ | ||
public unsafe class SDL3MotionDriver : IHandheld, IDisposable | ||
{ | ||
private readonly Dictionary<SDL_SensorType, SDL_Sensor> sensors; | ||
private bool _disposed; | ||
|
||
public SDL3MotionDriver() | ||
{ | ||
int result = SDL_Init(SDL_InitFlags.Sensor); | ||
if (result < 0) | ||
{ | ||
throw new InvalidOperationException($"SDL sensor initialization failed: {SDL_GetError()}"); | ||
} | ||
sensors = SDL_GetSensors().ToArray().ToDictionary(SDL_GetSensorTypeForID, SDL_OpenSensor); | ||
} | ||
|
||
~SDL3MotionDriver() | ||
{ | ||
Dispose(false); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Dispose(true); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
protected virtual void Dispose(bool disposing) | ||
{ | ||
if (_disposed) | ||
{ | ||
return; | ||
} | ||
|
||
if (disposing && sensors != null) | ||
{ | ||
foreach (var sensor in sensors.Values) | ||
{ | ||
if (sensor != IntPtr.Zero) | ||
{ | ||
SDL_CloseSensor(sensor); | ||
} | ||
} | ||
} | ||
|
||
_disposed = true; | ||
} | ||
|
||
public Vector3 GetMotionData(MotionInputId inputType) | ||
{ | ||
ObjectDisposedException.ThrowIf(_disposed, this); | ||
|
||
return inputType switch | ||
{ | ||
MotionInputId.Gyroscope => GetSensorVector(SDL_SensorType.Gyro) * 180 / MathF.PI, | ||
MotionInputId.Accelerometer => GetSensorVector(SDL_SensorType.Accel) / SDL_STANDARD_GRAVITY, | ||
_ => Vector3.Zero | ||
}; | ||
} | ||
|
||
private Vector3 GetSensorVector(SDL_SensorType sensorType) | ||
{ | ||
if (!sensors.TryGetValue(sensorType, out SDL_Sensor sensor)) | ||
{ | ||
return Vector3.Zero; | ||
} | ||
|
||
var data = stackalloc float[3]; | ||
if (SDL_GetSensorData(sensor, data, 3) < 0) | ||
{ | ||
return Vector3.Zero; | ||
} | ||
|
||
return new Vector3(data[0], data[1], data[2]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System; | ||
using System.Numerics; | ||
|
||
namespace Ryujinx.Input | ||
{ | ||
public interface IHandheld : IDisposable | ||
{ | ||
Vector3 GetMotionData(MotionInputId gyroscope); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.