Skip to content

Commit

Permalink
misc: Small code improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
GreemDev committed Oct 14, 2024
1 parent 5f6d9ee commit b2a35ec
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 108 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
alt="">
</a>
<a href="https://discord.gg/dHPrkBkkyA">
<img src="https://img.shields.io/discord/410208534861447168?color=5865F2&label=Ryujinx&logo=discord&logoColor=white"
<img src="https://img.shields.io/discord/1294443224030511104?color=5865F2&label=Ryujinx&logo=discord&logoColor=white"
alt="Discord">
</a>
<br>
Expand Down
2 changes: 1 addition & 1 deletion src/Ryujinx.Common/Collections/IntervalTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ internal IntervalTreeNode(TKey start, TKey end, TValue value, IntervalTreeNode<T
Start = start;
End = end;
Max = end;
Values = new List<RangeNode<TKey, TValue>> { new RangeNode<TKey, TValue>(start, end, value) };
Values = [ new RangeNode<TKey, TValue>(start, end, value) ];
Parent = parent;
}
}
Expand Down
7 changes: 2 additions & 5 deletions src/Ryujinx.Headless.SDL2/HeadlessDynamicTextInputHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@ public event KeyReleasedHandler KeyReleasedEvent { add { } remove { } }

public bool TextProcessingEnabled
{
get
{
return Volatile.Read(ref _canProcessInput);
}

get => Volatile.Read(ref _canProcessInput);

set
{
Volatile.Write(ref _canProcessInput, value);
Expand Down
132 changes: 54 additions & 78 deletions src/Ryujinx.Input.SDL2/SDL2Gamepad.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ class SDL2Gamepad : IGamepad
{
private bool HasConfiguration => _configuration != null;

private record struct ButtonMappingEntry(GamepadButtonInputId To, GamepadButtonInputId From);
private readonly record struct ButtonMappingEntry(GamepadButtonInputId To, GamepadButtonInputId From)
{
public bool IsValid => To is not GamepadButtonInputId.Unbound && From is not GamepadButtonInputId.Unbound;
}

private StandardControllerInputConfig _configuration;

Expand Down Expand Up @@ -144,86 +147,64 @@ public void SetTriggerThreshold(float triggerThreshold)

public void Rumble(float lowFrequency, float highFrequency, uint durationMs)
{
if (Features.HasFlag(GamepadFeaturesFlag.Rumble))
{
ushort lowFrequencyRaw = (ushort)(lowFrequency * ushort.MaxValue);
ushort highFrequencyRaw = (ushort)(highFrequency * ushort.MaxValue);
if (!Features.HasFlag(GamepadFeaturesFlag.Rumble)) return;

if (durationMs == uint.MaxValue)
{
if (SDL_GameControllerRumble(_gamepadHandle, lowFrequencyRaw, highFrequencyRaw, SDL_HAPTIC_INFINITY) != 0)
{
Logger.Error?.Print(LogClass.Hid, "Rumble is not supported on this game controller.");
}
}
else if (durationMs > SDL_HAPTIC_INFINITY)
{
Logger.Error?.Print(LogClass.Hid, $"Unsupported rumble duration {durationMs}");
}
else
{
if (SDL_GameControllerRumble(_gamepadHandle, lowFrequencyRaw, highFrequencyRaw, durationMs) != 0)
{
Logger.Error?.Print(LogClass.Hid, "Rumble is not supported on this game controller.");
}
}
}
}

public Vector3 GetMotionData(MotionInputId inputId)
{
SDL_SensorType sensorType = SDL_SensorType.SDL_SENSOR_INVALID;
ushort lowFrequencyRaw = (ushort)(lowFrequency * ushort.MaxValue);
ushort highFrequencyRaw = (ushort)(highFrequency * ushort.MaxValue);

if (inputId == MotionInputId.Accelerometer)
if (durationMs == uint.MaxValue)
{
sensorType = SDL_SensorType.SDL_SENSOR_ACCEL;
if (SDL_GameControllerRumble(_gamepadHandle, lowFrequencyRaw, highFrequencyRaw, SDL_HAPTIC_INFINITY) != 0)
Logger.Error?.Print(LogClass.Hid, "Rumble is not supported on this game controller.");
}
else if (inputId == MotionInputId.Gyroscope)
else if (durationMs > SDL_HAPTIC_INFINITY)
{
sensorType = SDL_SensorType.SDL_SENSOR_GYRO;
Logger.Error?.Print(LogClass.Hid, $"Unsupported rumble duration {durationMs}");
}
else
{
if (SDL_GameControllerRumble(_gamepadHandle, lowFrequencyRaw, highFrequencyRaw, durationMs) != 0)
Logger.Error?.Print(LogClass.Hid, "Rumble is not supported on this game controller.");
}
}

if (Features.HasFlag(GamepadFeaturesFlag.Motion) && sensorType != SDL_SensorType.SDL_SENSOR_INVALID)
public Vector3 GetMotionData(MotionInputId inputId)
{
SDL_SensorType sensorType = inputId switch
{
const int ElementCount = 3;
MotionInputId.Accelerometer => SDL_SensorType.SDL_SENSOR_ACCEL,
MotionInputId.Gyroscope => SDL_SensorType.SDL_SENSOR_GYRO,
_ => SDL_SensorType.SDL_SENSOR_INVALID
};

unsafe
{
float* values = stackalloc float[ElementCount];
if (!Features.HasFlag(GamepadFeaturesFlag.Motion) || sensorType is SDL_SensorType.SDL_SENSOR_INVALID)
return Vector3.Zero;

int result = SDL_GameControllerGetSensorData(_gamepadHandle, sensorType, (IntPtr)values, ElementCount);
const int ElementCount = 3;

if (result == 0)
{
Vector3 value = new(values[0], values[1], values[2]);
unsafe
{
float* values = stackalloc float[ElementCount];

if (inputId == MotionInputId.Gyroscope)
{
return RadToDegree(value);
}
int result = SDL_GameControllerGetSensorData(_gamepadHandle, sensorType, (IntPtr)values, ElementCount);

if (inputId == MotionInputId.Accelerometer)
{
return GsToMs2(value);
}
if (result != 0)
return Vector3.Zero;

return value;
}
}
}
Vector3 value = new(values[0], values[1], values[2]);

return Vector3.Zero;
return inputId switch
{
MotionInputId.Gyroscope => RadToDegree(value),
MotionInputId.Accelerometer => GsToMs2(value),
_ => value
};
}
}

private static Vector3 RadToDegree(Vector3 rad)
{
return rad * (180 / MathF.PI);
}
private static Vector3 RadToDegree(Vector3 rad) => rad * (180 / MathF.PI);

private static Vector3 GsToMs2(Vector3 gs)
{
return gs / SDL_STANDARD_GRAVITY;
}
private static Vector3 GsToMs2(Vector3 gs) => gs / SDL_STANDARD_GRAVITY;

public void SetConfiguration(InputConfig configuration)
{
Expand Down Expand Up @@ -278,16 +259,13 @@ public GamepadStateSnapshot GetMappedStateSnapshot()
lock (_userMappingLock)
{
if (_buttonsUserMapping.Count == 0)
{
return rawState;
}


// ReSharper disable once ForeachCanBePartlyConvertedToQueryUsingAnotherGetEnumerator
foreach (ButtonMappingEntry entry in _buttonsUserMapping)
{
if (entry.From == GamepadButtonInputId.Unbound || entry.To == GamepadButtonInputId.Unbound)
{
continue;
}
if (!entry.IsValid) continue;

// Do not touch state of button already pressed
if (!result.IsPressed(entry.To))
Expand Down Expand Up @@ -316,9 +294,8 @@ private static float ConvertRawStickValue(short value)
public (float, float) GetStick(StickInputId inputId)
{
if (inputId == StickInputId.Unbound)
{
return (0.0f, 0.0f);
}


(short stickX, short stickY) = GetStickXY(inputId);

Expand Down Expand Up @@ -351,6 +328,7 @@ private static float ConvertRawStickValue(short value)
return (resultX, resultY);
}

// ReSharper disable once InconsistentNaming
private (short, short) GetStickXY(StickInputId inputId) =>
inputId switch
{
Expand All @@ -365,14 +343,12 @@ private static float ConvertRawStickValue(short value)

public bool IsPressed(GamepadButtonInputId inputId)
{
if (inputId == GamepadButtonInputId.LeftTrigger)
{
return ConvertRawStickValue(SDL_GameControllerGetAxis(_gamepadHandle, SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_TRIGGERLEFT)) > _triggerThreshold;
}

if (inputId == GamepadButtonInputId.RightTrigger)
switch (inputId)
{
return ConvertRawStickValue(SDL_GameControllerGetAxis(_gamepadHandle, SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_TRIGGERRIGHT)) > _triggerThreshold;
case GamepadButtonInputId.LeftTrigger:
return ConvertRawStickValue(SDL_GameControllerGetAxis(_gamepadHandle, SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_TRIGGERLEFT)) > _triggerThreshold;
case GamepadButtonInputId.RightTrigger:
return ConvertRawStickValue(SDL_GameControllerGetAxis(_gamepadHandle, SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_TRIGGERRIGHT)) > _triggerThreshold;
}

if (_buttonsDriverMapping[(int)inputId] == SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_INVALID)
Expand Down
15 changes: 5 additions & 10 deletions src/Ryujinx.UI.Common/Helper/TitleHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,18 @@ public static class TitleHelper
public static string ActiveApplicationTitle(ProcessResult activeProcess, string applicationVersion, string pauseString = "")
{
if (activeProcess == null)
{
return String.Empty;
}

return string.Empty;

string titleNameSection = string.IsNullOrWhiteSpace(activeProcess.Name) ? string.Empty : $" {activeProcess.Name}";
string titleVersionSection = string.IsNullOrWhiteSpace(activeProcess.DisplayVersion) ? string.Empty : $" v{activeProcess.DisplayVersion}";
string titleIdSection = $" ({activeProcess.ProgramIdText.ToUpper()})";
string titleArchSection = activeProcess.Is64Bit ? " (64-bit)" : " (32-bit)";

string appTitle = $"Ryujinx {applicationVersion} -{titleNameSection}{titleVersionSection}{titleIdSection}{titleArchSection}";

if (!string.IsNullOrEmpty(pauseString))
{
appTitle += $" ({pauseString})";
}

return appTitle;
return !string.IsNullOrEmpty(pauseString)
? appTitle + $" ({pauseString})"
: appTitle;
}
}
}
17 changes: 4 additions & 13 deletions src/Ryujinx/UI/Windows/TitleUpdateWindow.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Interactivity;
using Avalonia.Styling;
using FluentAvalonia.UI.Controls;
Expand Down Expand Up @@ -64,21 +62,14 @@ public void Save(object sender, RoutedEventArgs e)

private void OpenLocation(object sender, RoutedEventArgs e)
{
if (sender is Button button)
{
if (button.DataContext is TitleUpdateModel model)
{
OpenHelper.LocateFile(model.Path);
}
}
if (sender is Button { DataContext: TitleUpdateModel model })
OpenHelper.LocateFile(model.Path);
}

private void RemoveUpdate(object sender, RoutedEventArgs e)
{
if (sender is Button button)
{
ViewModel.RemoveUpdate((TitleUpdateModel)button.DataContext);
}
if (sender is Button { DataContext: TitleUpdateModel model })
ViewModel.RemoveUpdate(model);
}

private void RemoveAll(object sender, RoutedEventArgs e)
Expand Down

0 comments on commit b2a35ec

Please sign in to comment.