forked from MeloNX/MeloNX
Fix Controllers, Add Auto Controller, Fix touch screen, Rewrite SDL Controller Detection.
This commit is contained in:
parent
1d16bf0c94
commit
a166494e33
Binary file not shown.
@ -12,12 +12,12 @@
|
||||
<key>Ryujinx.xcscheme_^#shared#^_</key>
|
||||
<dict>
|
||||
<key>orderHint</key>
|
||||
<integer>1</integer>
|
||||
<integer>2</integer>
|
||||
</dict>
|
||||
<key>com.Stossy11.MeloNX.RyujinxAg.xcscheme_^#shared#^_</key>
|
||||
<dict>
|
||||
<key>orderHint</key>
|
||||
<integer>2</integer>
|
||||
<integer>1</integer>
|
||||
</dict>
|
||||
</dict>
|
||||
<key>SuppressBuildableAutocreation</key>
|
||||
|
@ -9,7 +9,7 @@
|
||||
#define RyujinxHeader
|
||||
|
||||
|
||||
#import "SDL2/SDL.h"
|
||||
#include <SDL2/SDL.h>
|
||||
#import "utils.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
@ -40,8 +40,6 @@ int get_current_fps();
|
||||
|
||||
void initialize();
|
||||
|
||||
const char* get_game_controllers();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
@ -10,64 +10,48 @@ import GameController
|
||||
import UIKit
|
||||
import SwiftUI
|
||||
|
||||
func waitforcontroller() {
|
||||
if let window = theWindow {
|
||||
// Function to recursively search for GCControllerView
|
||||
func findGCControllerView(in view: UIView) -> UIView? {
|
||||
// Check if current view is GCControllerView
|
||||
if String(describing: type(of: view)) == "ControllerView" {
|
||||
return view
|
||||
}
|
||||
var hostingController: UIHostingController<ControllerView>? // Store reference to prevent deallocation
|
||||
|
||||
// Search through subviews
|
||||
for subview in view.subviews {
|
||||
if let found = findGCControllerView(in: subview) {
|
||||
return found
|
||||
}
|
||||
}
|
||||
func waitForController() {
|
||||
guard let window = theWindow else { return }
|
||||
|
||||
return nil
|
||||
// Function to search for an existing UIHostingController with ControllerView
|
||||
func findGCControllerView(in view: UIView) -> UIHostingController<ControllerView>? {
|
||||
if let hostingVC = view.next as? UIHostingController<ControllerView> {
|
||||
return hostingVC
|
||||
}
|
||||
|
||||
let controllerView = ControllerView()
|
||||
let controllerHostingController = UIHostingController(rootView: controllerView)
|
||||
let containerView = TransparentHostingContainerView(frame: window.bounds)
|
||||
containerView.backgroundColor = .clear
|
||||
|
||||
controllerHostingController.view.frame = containerView.bounds
|
||||
controllerHostingController.view.backgroundColor = .clear
|
||||
containerView.addSubview(controllerHostingController.view)
|
||||
|
||||
class LandscapeViewController: UIViewController {
|
||||
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
|
||||
return .landscape
|
||||
}
|
||||
|
||||
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
|
||||
return .landscapeLeft
|
||||
for subview in view.subviews {
|
||||
if let found = findGCControllerView(in: subview) {
|
||||
return found
|
||||
}
|
||||
}
|
||||
|
||||
let landscapeVC = LandscapeViewController()
|
||||
landscapeVC.modalPresentationStyle = .fullScreen
|
||||
window.rootViewController?.present(landscapeVC, animated: false, completion: nil)
|
||||
return nil
|
||||
}
|
||||
|
||||
Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) { timer in
|
||||
if findGCControllerView(in: window) == nil {
|
||||
window.addSubview(containerView)
|
||||
let controllerView = ControllerView()
|
||||
let newHostingController = UIHostingController(rootView: controllerView)
|
||||
|
||||
window.bringSubviewToFront(containerView)
|
||||
// Store reference globally to prevent deallocation
|
||||
hostingController = newHostingController
|
||||
|
||||
let window = SDL_GetWindowFromID(1)
|
||||
let containerView = newHostingController.view!
|
||||
containerView.backgroundColor = .clear
|
||||
containerView.frame = window.bounds
|
||||
containerView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
|
||||
|
||||
SDL_SetWindowPosition(window, 0, 0);
|
||||
Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) { timer in
|
||||
if findGCControllerView(in: window) == nil {
|
||||
window.addSubview(containerView)
|
||||
window.bringSubviewToFront(containerView)
|
||||
|
||||
timer.invalidate()
|
||||
} else {
|
||||
timer.invalidate()
|
||||
if let sdlWindow = SDL_GetWindowFromID(1) {
|
||||
SDL_SetWindowPosition(sdlWindow, 0, 0)
|
||||
}
|
||||
}
|
||||
|
||||
timer.invalidate() // Stop the timer after adding the view
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -23,10 +23,7 @@ extension UIWindow {
|
||||
|
||||
if UserDefaults.standard.bool(forKey: "isVirtualController") {
|
||||
if let window = theWindow {
|
||||
|
||||
|
||||
|
||||
waitforcontroller()
|
||||
waitForController()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -279,35 +279,58 @@ class Ryujinx {
|
||||
}
|
||||
}
|
||||
|
||||
func getConnectedControllers() -> [Controller] {
|
||||
private func generateGamepadId(joystickIndex: Int32) -> String? {
|
||||
var guid = SDL_JoystickGetDeviceGUID(joystickIndex)
|
||||
|
||||
|
||||
guard let jsonPtr = get_game_controllers() else {
|
||||
return []
|
||||
if guid.data.0 == 0 && guid.data.1 == 0 && guid.data.2 == 0 && guid.data.3 == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Convert the unmanaged memory (C string) to a Swift String
|
||||
let jsonString = String(cString: jsonPtr)
|
||||
let reorderedGUID: [UInt8] = [
|
||||
guid.data.3, guid.data.2, guid.data.1, guid.data.0,
|
||||
guid.data.5, guid.data.4,
|
||||
guid.data.7, guid.data.6,
|
||||
guid.data.8, guid.data.9,
|
||||
guid.data.10, guid.data.11, guid.data.12, guid.data.13, guid.data.14, guid.data.15
|
||||
]
|
||||
|
||||
let guidString = reorderedGUID.map { String(format: "%02X", $0) }.joined().lowercased()
|
||||
|
||||
func substring(_ str: String, _ start: Int, _ end: Int) -> String {
|
||||
let startIdx = str.index(str.startIndex, offsetBy: start)
|
||||
let endIdx = str.index(str.startIndex, offsetBy: end)
|
||||
return String(str[startIdx..<endIdx])
|
||||
}
|
||||
|
||||
let formattedGUID = "\(substring(guidString, 0, 8))-\(substring(guidString, 8, 12))-\(substring(guidString, 12, 16))-\(substring(guidString, 16, 20))-\(substring(guidString, 20, 32))"
|
||||
|
||||
return "\(joystickIndex)-\(formattedGUID)"
|
||||
}
|
||||
|
||||
func getConnectedControllers() -> [Controller] {
|
||||
var controllers: [Controller] = []
|
||||
|
||||
// Splitting the string by newline
|
||||
let lines = jsonString.components(separatedBy: "\n")
|
||||
let numJoysticks = SDL_NumJoysticks()
|
||||
|
||||
// Parsing each line
|
||||
for line in lines {
|
||||
if line.contains(":") {
|
||||
let parts = line.components(separatedBy: ":")
|
||||
if parts.count == 2 {
|
||||
let id = parts[0].trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
let name = parts[1].trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
controllers.append(Controller(id: id, name: name))
|
||||
for i in 0..<numJoysticks {
|
||||
if let controller = SDL_GameControllerOpen(i) {
|
||||
let guid = generateGamepadId(joystickIndex: i)
|
||||
let name = String(cString: SDL_GameControllerName(controller))
|
||||
|
||||
print("Controller \(i): \(name), GUID: \(guid ?? "")")
|
||||
|
||||
guard let guid else {
|
||||
SDL_GameControllerClose(controller)
|
||||
return []
|
||||
}
|
||||
|
||||
controllers.append(Controller(id: guid, name: name))
|
||||
|
||||
SDL_GameControllerClose(controller)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return controllers
|
||||
|
||||
}
|
||||
|
||||
func removeFirmware() {
|
||||
|
@ -91,11 +91,37 @@ struct ContentView: View {
|
||||
mainMenuView
|
||||
.onAppear() {
|
||||
quits = false
|
||||
|
||||
initControllerObservers()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
private func initControllerObservers() {
|
||||
NotificationCenter.default.addObserver(
|
||||
forName: .GCControllerDidConnect,
|
||||
object: nil,
|
||||
queue: .main) { notification in
|
||||
if let controller = notification.object as? GCController {
|
||||
print("Controller connected: \(controller.productCategory)")
|
||||
refreshControllersList()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
NotificationCenter.default.addObserver(
|
||||
forName: .GCControllerDidDisconnect,
|
||||
object: nil,
|
||||
queue: .main) { notification in
|
||||
if let controller = notification.object as? GCController {
|
||||
print("Controller disconnected: \(controller.productCategory)")
|
||||
refreshControllersList()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - View Components
|
||||
private var emulationView: some View {
|
||||
GeometryReader { screenGeometry in
|
||||
@ -175,14 +201,16 @@ struct ContentView: View {
|
||||
private var mainMenuView: some View {
|
||||
MainTabView(startemu: $game, config: $config, MVKconfig: $settings, controllersList: $controllersList, currentControllers: $currentControllers, onscreencontroller: $onscreencontroller)
|
||||
.onAppear() {
|
||||
refreshControllersList()
|
||||
Timer.scheduledTimer(withTimeInterval: 1, repeats: false) { timer in
|
||||
refreshControllersList()
|
||||
}
|
||||
|
||||
|
||||
let isJIT = UserDefaults.standard.bool(forKey: "JIT-ENABLED")
|
||||
let isJIT = UserDefaults.standard.bool(forKey: "JIT-ENABLED")
|
||||
|
||||
if !isJIT, useTrollStore {
|
||||
askForJIT()
|
||||
}
|
||||
if !isJIT, useTrollStore {
|
||||
askForJIT()
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@ -229,14 +257,22 @@ struct ContentView: View {
|
||||
|
||||
controllersList.removeAll(where: { $0.id == "0"})
|
||||
|
||||
if controllersList.count > 2 {
|
||||
let controller = controllersList[2]
|
||||
currentControllers.append(controller)
|
||||
} else if let controller = controllersList.first(where: { $0.id == onscreencontroller.id }), !controllersList.isEmpty {
|
||||
currentControllers = []
|
||||
|
||||
if controllersList.count == 1 {
|
||||
let controller = controllersList[0]
|
||||
currentControllers.append(controller)
|
||||
} else if (controllersList.count - 1) >= 1 {
|
||||
for controller in controllersList {
|
||||
if controller.id != onscreencontroller.id && !currentControllers.contains(where: { $0.id == controller.id }) {
|
||||
currentControllers.append(controller)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
func showAlert(title: String, message: String, showOk: Bool, completion: @escaping (Bool) -> Void) {
|
||||
DispatchQueue.main.async {
|
||||
if let mainWindow = UIApplication.shared.windows.last {
|
||||
|
@ -198,7 +198,7 @@ struct SettingsView: View {
|
||||
|
||||
|
||||
|
||||
ForEach(controllersList) { controller in
|
||||
ForEach(currentControllers) { controller in
|
||||
|
||||
var customBinding: Binding<Bool> {
|
||||
Binding(
|
||||
@ -223,14 +223,26 @@ struct SettingsView: View {
|
||||
}
|
||||
.tint(.blue)
|
||||
} label: {
|
||||
let controller = String((controllersList.firstIndex(where: { $0.id == controller.id }) ?? 0) + 1)
|
||||
|
||||
if let controller = currentControllers.firstIndex(where: { $0.id == controller.id } ) {
|
||||
Text("Player \(controller + 1)")
|
||||
.onAppear() {
|
||||
// print(currentControllers.firstIndex(where: { $0.id == controller.id }) ?? 0)
|
||||
print(currentControllers.count)
|
||||
|
||||
Text("Player \(controller)")
|
||||
if currentControllers.count > 2 {
|
||||
print(currentControllers[1])
|
||||
print(currentControllers[2])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
.onMove { from, to in
|
||||
currentControllers.move(fromOffsets: from, toOffset: to)
|
||||
}
|
||||
} header: {
|
||||
Text("Input Selector")
|
||||
.font(.title3.weight(.semibold))
|
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -95,12 +95,6 @@ using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
using SDL2;
|
||||
|
||||
public class GamepadInfo
|
||||
{
|
||||
public string Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
}
|
||||
|
||||
namespace Ryujinx.Headless.SDL2
|
||||
{
|
||||
class Program
|
||||
@ -317,44 +311,6 @@ namespace Ryujinx.Headless.SDL2
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
[UnmanagedCallersOnly(EntryPoint = "get_game_controllers")]
|
||||
public static unsafe IntPtr GetGamepadList()
|
||||
{
|
||||
List<GamepadInfo> gamepads = new List<GamepadInfo>();
|
||||
IGamepad gamepad;
|
||||
if (_inputManager == null)
|
||||
{
|
||||
_inputManager = new InputManager(new SDL2KeyboardDriver(), new SDL2GamepadDriver());
|
||||
}
|
||||
|
||||
// Collect gamepads from the keyboard driver
|
||||
foreach (string id in _inputManager.KeyboardDriver.GamepadsIds)
|
||||
{
|
||||
gamepad = _inputManager.KeyboardDriver.GetGamepad(id);
|
||||
gamepads.Add(new GamepadInfo { Id = id, Name = gamepad.Name });
|
||||
gamepad.Dispose();
|
||||
}
|
||||
|
||||
// Collect gamepads from the gamepad driver
|
||||
foreach (string id in _inputManager.GamepadDriver.GamepadsIds)
|
||||
{
|
||||
gamepad = _inputManager.GamepadDriver.GetGamepad(id);
|
||||
gamepads.Add(new GamepadInfo { Id = id, Name = gamepad.Name });
|
||||
gamepad.Dispose();
|
||||
}
|
||||
|
||||
// Serialize the gamepad list to a custom string format
|
||||
string result = string.Join("\n", gamepads.Select(g => $"{g.Id}:{g.Name}")); // Ensure System.Linq is available
|
||||
|
||||
// Convert the string to unmanaged memory
|
||||
IntPtr ptr = Marshal.StringToHGlobalAnsi(result);
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
[UnmanagedCallersOnly(EntryPoint = "get_game_info")]
|
||||
public static GameInfoNative GetGameInfoNative(int descriptor, IntPtr extensionPtr)
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user