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

Add a virtual controller to merge Joy-Cons. #434

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
e9ecbd4
Add a virtual controller to merge Joy-Cons.
madwind Dec 23, 2024
86f9544
Update NpadController.cs
madwind Dec 23, 2024
ad7d9d1
Update NpadController.cs
madwind Dec 23, 2024
a4b2fee
Merge branch 'GreemDev:master' into master
madwind Dec 23, 2024
fec197d
log powerLevel
madwind Dec 25, 2024
714c68b
Merge branch 'GreemDev:master' into master
madwind Dec 25, 2024
e509ffa
delay 2000ms before ShowPowerLevel
madwind Dec 25, 2024
e0b6a01
Merge branch 'GreemDev:master' into master
madwind Dec 25, 2024
c4dea0e
add SQL_JOYBATTERYUPDATED , OnJoyBatteryUpdated
madwind Dec 26, 2024
7863e97
invoke OnGamepadConnected and OnGamepadDisconnected
madwind Dec 26, 2024
e426680
Merge branch 'GreemDev:master' into master
madwind Dec 26, 2024
20fdbff
clean log
madwind Dec 26, 2024
6dec7ff
fix motionData
madwind Dec 28, 2024
f1c0cc8
Merge branch 'Ryubing:master' into master
madwind Dec 28, 2024
a837294
Merge branch 'master' into master
GreemDev Dec 28, 2024
68c0305
For the JoyCon controller, wrap SDL2Gamepad as SDL2JoyCon to use a su…
madwind Dec 28, 2024
8e50dd9
fix right JoyCon stick
madwind Dec 28, 2024
69dfd8c
fix right Stick
madwind Dec 28, 2024
09e7b66
Merge branch 'master' into master
GreemDev Dec 29, 2024
99c7c3f
Merge branch 'Ryubing:master' into master
madwind Dec 29, 2024
7a451ab
fix GetMappedStateSnapshot
madwind Dec 30, 2024
536f792
Merge branch 'Ryubing:master' into master
madwind Dec 30, 2024
3cbd7dc
Merge branch 'Ryubing:master' into master
madwind Dec 31, 2024
4e77bcb
Merge branch 'master' into master
GreemDev Jan 2, 2025
4399eda
Fix typo: SQL_JOYBATTERYUPDATED => SDL_JOYBATTERYUPDATED
madwind Jan 2, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion src/Ryujinx.Input.SDL2/SDL2GamepadDriver.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Ryujinx.Common.Logging;
using Ryujinx.SDL2.Common;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -36,6 +37,7 @@ public SDL2GamepadDriver()
SDL2Driver.Instance.Initialize();
SDL2Driver.Instance.OnJoyStickConnected += HandleJoyStickConnected;
SDL2Driver.Instance.OnJoystickDisconnected += HandleJoyStickDisconnected;
SDL2Driver.Instance.OnJoyBatteryUpdated += HandleJoyBatteryUpdated;

// Add already connected gamepads
int numJoysticks = SDL_NumJoysticks();
Expand Down Expand Up @@ -83,19 +85,30 @@ private int GetJoystickIndexByGamepadId(string id)

private void HandleJoyStickDisconnected(int joystickInstanceId)
{
bool joyConPairDisconnected = false;
if (!_gamepadsInstanceIdsMapping.Remove(joystickInstanceId, out string id))
return;

lock (_lock)
{
_gamepadsIds.Remove(id);
if (!SDL2JoyConPair.IsCombinable(_gamepadsIds))
{
_gamepadsIds.Remove(SDL2JoyConPair.Id);
joyConPairDisconnected = true;
}
}

OnGamepadDisconnected?.Invoke(id);
if (joyConPairDisconnected)
{
OnGamepadDisconnected?.Invoke(SDL2JoyConPair.Id);
}
}

private void HandleJoyStickConnected(int joystickDeviceId, int joystickInstanceId)
{
bool joyConPairConnected = false;
if (SDL_IsGameController(joystickDeviceId) == SDL_bool.SDL_TRUE)
{
if (_gamepadsInstanceIdsMapping.ContainsKey(joystickInstanceId))
Expand All @@ -120,13 +133,29 @@ private void HandleJoyStickConnected(int joystickDeviceId, int joystickInstanceI
_gamepadsIds.Insert(joystickDeviceId, id);
else
_gamepadsIds.Add(id);
if (SDL2JoyConPair.IsCombinable(_gamepadsIds))
{
_gamepadsIds.Remove(SDL2JoyConPair.Id);
_gamepadsIds.Add(SDL2JoyConPair.Id);
joyConPairConnected = true;
}
}

OnGamepadConnected?.Invoke(id);
if (joyConPairConnected)
{
OnGamepadConnected?.Invoke(SDL2JoyConPair.Id);
}
}
}
}

private void HandleJoyBatteryUpdated(int joystickDeviceId, SDL_JoystickPowerLevel powerLevel)
{
Logger.Info?.Print(LogClass.Hid,
$"{SDL_GameControllerNameForIndex(joystickDeviceId)} power level: {powerLevel}");
}

protected virtual void Dispose(bool disposing)
{
if (disposing)
Expand Down Expand Up @@ -157,6 +186,14 @@ public void Dispose()

public IGamepad GetGamepad(string id)
{
if (id == SDL2JoyConPair.Id)
{
lock (_lock)
{
return SDL2JoyConPair.GetGamepad(_gamepadsIds);
}
}

int joystickIndex = GetJoystickIndexByGamepadId(id);

if (joystickIndex == -1)
Expand All @@ -165,12 +202,16 @@ public IGamepad GetGamepad(string id)
}

nint gamepadHandle = SDL_GameControllerOpen(joystickIndex);

if (gamepadHandle == nint.Zero)
{
return null;
}

if (SDL_GameControllerName(gamepadHandle).StartsWith(SDL2JoyCon.Prefix))
{
return new SDL2JoyCon(gamepadHandle, id);
}

return new SDL2Gamepad(gamepadHandle, id);
}
}
Expand Down
Loading