From 1b43645d7e9618244974df0f642eafe3ba926686 Mon Sep 17 00:00:00 2001 From: beirich Date: Sun, 30 Sep 2012 06:29:30 +0000 Subject: [PATCH] add XInput support, LT/RT are possible inputs now. Any existing gamepad mappings are so, so very totally screwed. --- .../BizHawk.MultiClient.csproj | 1 + BizHawk.MultiClient/Input/GamePad.cs | 24 ++-- BizHawk.MultiClient/Input/GamePad360.cs | 113 ++++++++++++++++++ BizHawk.MultiClient/Input/Input.cs | 25 ++-- 4 files changed, 140 insertions(+), 23 deletions(-) create mode 100644 BizHawk.MultiClient/Input/GamePad360.cs diff --git a/BizHawk.MultiClient/BizHawk.MultiClient.csproj b/BizHawk.MultiClient/BizHawk.MultiClient.csproj index 4047a9ffc7..a3e5fa72cd 100644 --- a/BizHawk.MultiClient/BizHawk.MultiClient.csproj +++ b/BizHawk.MultiClient/BizHawk.MultiClient.csproj @@ -206,6 +206,7 @@ + diff --git a/BizHawk.MultiClient/Input/GamePad.cs b/BizHawk.MultiClient/Input/GamePad.cs index fbd72e844c..ce524e2924 100644 --- a/BizHawk.MultiClient/Input/GamePad.cs +++ b/BizHawk.MultiClient/Input/GamePad.cs @@ -9,7 +9,7 @@ namespace BizHawk.MultiClient { // ********************************** Static interface ********************************** - private static DirectInput dinput; + static DirectInput dinput; public static List Devices; public static void Initialize() @@ -21,7 +21,10 @@ namespace BizHawk.MultiClient foreach (DeviceInstance device in dinput.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly)) { - var joystick = new Joystick(dinput, device.InstanceGuid); + if (device.ProductName.Contains("XBOX 360")) + continue; // Don't input XBOX 360 controllers into here; we'll process them via XInput. + + var joystick = new Joystick(dinput, device.InstanceGuid); joystick.SetCooperativeLevel(Global.MainForm.Handle, CooperativeLevel.Background | CooperativeLevel.Nonexclusive); foreach (DeviceObjectInstance deviceObject in joystick.GetObjects()) { @@ -43,12 +46,12 @@ namespace BizHawk.MultiClient // ********************************** Instance Members ********************************** - private readonly string name; - private readonly Guid guid; - private readonly Joystick joystick; - private JoystickState state = new JoystickState(); + readonly string name; + readonly Guid guid; + readonly Joystick joystick; + JoystickState state = new JoystickState(); - private GamePad(string name, Guid guid, Joystick joystick) + GamePad(string name, Guid guid, Joystick joystick) { this.name = name; this.guid = guid; @@ -180,10 +183,7 @@ namespace BizHawk.MultiClient } } - - - /// Note that this does not appear to work at this time. I probably need to have more infos. - /// + // Note that this does not appear to work at this time. I probably need to have more infos. public void SetVibration(int left, int right) { int[] temp1, temp2; @@ -204,4 +204,4 @@ namespace BizHawk.MultiClient effect.Start(1); } } -} +} \ No newline at end of file diff --git a/BizHawk.MultiClient/Input/GamePad360.cs b/BizHawk.MultiClient/Input/GamePad360.cs new file mode 100644 index 0000000000..873470b771 --- /dev/null +++ b/BizHawk.MultiClient/Input/GamePad360.cs @@ -0,0 +1,113 @@ +using System; +using System.Collections.Generic; +using SlimDX; +using SlimDX.XInput; + +namespace BizHawk.MultiClient +{ + public class GamePad360 + { + // ********************************** Static interface ********************************** + + public static List Devices; + + public static void Initialize() + { + Devices = new List(); + var c1 = new SlimDX.XInput.Controller(UserIndex.One); + var c2 = new SlimDX.XInput.Controller(UserIndex.Two); + var c3 = new SlimDX.XInput.Controller(UserIndex.Three); + var c4 = new SlimDX.XInput.Controller(UserIndex.Four); + + if (c1.IsConnected) Devices.Add(new GamePad360(c1)); + if (c2.IsConnected) Devices.Add(new GamePad360(c2)); + if (c3.IsConnected) Devices.Add(new GamePad360(c3)); + if (c4.IsConnected) Devices.Add(new GamePad360(c4)); + } + + public static void UpdateAll() + { + foreach (var device in Devices) + device.Update(); + } + + // ********************************** Instance Members ********************************** + + readonly SlimDX.XInput.Controller controller; + State state; + + GamePad360(SlimDX.XInput.Controller c) + { + controller = c; + InitializeButtons(); + Update(); + } + + public void Update() + { + if (controller.IsConnected == false) + return; + + state = controller.GetState(); + } + + public int NumButtons { get; private set; } + + List names = new List(); + List> actions = new List>(); + + void InitializeButtons() + { + const int dzp = 9000; + const int dzn = -9000; + const int dzt = 40; + + AddItem("A", () => (state.Gamepad.Buttons & GamepadButtonFlags.A) != 0); + AddItem("B", () => (state.Gamepad.Buttons & GamepadButtonFlags.B) != 0); + AddItem("X", () => (state.Gamepad.Buttons & GamepadButtonFlags.X) != 0); + AddItem("Y", () => (state.Gamepad.Buttons & GamepadButtonFlags.Y) != 0); + + AddItem("Start", () => (state.Gamepad.Buttons & GamepadButtonFlags.Start) != 0); + AddItem("Back", () => (state.Gamepad.Buttons & GamepadButtonFlags.Back) != 0); + AddItem("LeftThumb", () => (state.Gamepad.Buttons & GamepadButtonFlags.LeftThumb) != 0); + AddItem("RightThumb", () => (state.Gamepad.Buttons & GamepadButtonFlags.RightThumb) != 0); + AddItem("LeftShoulder", () => (state.Gamepad.Buttons & GamepadButtonFlags.LeftShoulder) != 0); + AddItem("RightShoulder", () => (state.Gamepad.Buttons & GamepadButtonFlags.RightShoulder) != 0); + + AddItem("DpadUp", () => (state.Gamepad.Buttons & GamepadButtonFlags.DPadUp) != 0); + AddItem("DpadDown", () => (state.Gamepad.Buttons & GamepadButtonFlags.DPadDown) != 0); + AddItem("DpadLeft", () => (state.Gamepad.Buttons & GamepadButtonFlags.DPadLeft) != 0); + AddItem("DpadRight", () => (state.Gamepad.Buttons & GamepadButtonFlags.DPadRight) != 0); + + AddItem("LStickUp", () => state.Gamepad.LeftThumbY >= dzp); + AddItem("LStickDown", () => state.Gamepad.LeftThumbY <= dzn); + AddItem("LStickLeft", () => state.Gamepad.LeftThumbX <= dzn); + AddItem("LStickRight", () => state.Gamepad.LeftThumbX >= dzp); + + AddItem("RStickUp", () => state.Gamepad.RightThumbY >= dzp); + AddItem("RStickDown", () => state.Gamepad.RightThumbY <= dzn); + AddItem("RStickLeft", () => state.Gamepad.RightThumbX <= dzn); + AddItem("RStickRight", () => state.Gamepad.RightThumbX >= dzp); + + AddItem("LeftTrigger", () => state.Gamepad.LeftTrigger > dzt); + AddItem("RightTrigger", () => state.Gamepad.RightTrigger > dzt); + } + + void AddItem(string name, Func pressed) + { + names.Add(name); + actions.Add(pressed); + NumButtons++; + } + + public string ButtonName(int index) + { + return names[index]; + } + + public bool Pressed(int index) + { + return actions[index](); + } + } +} diff --git a/BizHawk.MultiClient/Input/Input.cs b/BizHawk.MultiClient/Input/Input.cs index 9780073ef9..9de740efd9 100644 --- a/BizHawk.MultiClient/Input/Input.cs +++ b/BizHawk.MultiClient/Input/Input.cs @@ -68,6 +68,7 @@ namespace BizHawk.MultiClient #if WINDOWS KeyInput.Initialize(); GamePad.Initialize(); + GamePad360.Initialize(); #endif Instance = new Input(); } @@ -204,7 +205,8 @@ namespace BizHawk.MultiClient for (; ; ) { KeyInput.Update(); - GamePad.UpdateAll(); + GamePad.UpdateAll(); + GamePad360.UpdateAll(); _Modifiers = KeyInput.GetModifierKeysAsKeys(); _NewEvents.Clear(); @@ -219,21 +221,22 @@ namespace BizHawk.MultiClient else HandleButton(k.ToString(), false); + //analyze xinput + for (int i = 0; i < GamePad360.Devices.Count; i++) + { + var pad = GamePad360.Devices[i]; + string xname = "X" + (i + 1) + " "; + for (int b = 0; b < pad.NumButtons; b++) + HandleButton(xname + pad.ButtonName(b), pad.Pressed(b)); + } + //analyze joysticks for (int i = 0; i < GamePad.Devices.Count; i++) { var pad = GamePad.Devices[i]; string jname = "J" + (i + 1) + " "; - /* - HandleButton(jname + "Up", pad.Up); - HandleButton(jname + "Down", pad.Down); - HandleButton(jname + "Left", pad.Left); - HandleButton(jname + "Right", pad.Right); - - for (int b = 0; b < pad.Buttons.Length; b++) - HandleButton(jname + "B" + (b + 1), pad.Buttons[b]); - */ - for (int b = 0; b < pad.NumButtons; b++) + + for (int b = 0; b < pad.NumButtons; b++) HandleButton(jname + pad.ButtonName(b), pad.Pressed(b)); }