add XInput support, LT/RT are possible inputs now.

Any existing gamepad mappings are so, so very totally screwed.
This commit is contained in:
beirich 2012-09-30 06:29:30 +00:00
parent e0f506002d
commit 1b43645d7e
4 changed files with 140 additions and 23 deletions

View File

@ -206,6 +206,7 @@
<Compile Include="HawkFile.cs" />
<Compile Include="Input\ControllerBinding.cs" />
<Compile Include="Input\GamePad.cs" Condition=" '$(OS)' == 'Windows_NT' " />
<Compile Include="Input\GamePad360.cs" />
<Compile Include="Input\Input.cs" />
<Compile Include="LogConsole.cs" />
<Compile Include="LogWindow.cs">

View File

@ -9,7 +9,7 @@ namespace BizHawk.MultiClient
{
// ********************************** Static interface **********************************
private static DirectInput dinput;
static DirectInput dinput;
public static List<GamePad> 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.
/// </summary>
// 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);
}
}
}
}

View File

@ -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<GamePad360> Devices;
public static void Initialize()
{
Devices = new List<GamePad360>();
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<string> names = new List<string>();
List<Func<bool>> actions = new List<Func<bool>>();
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<bool> pressed)
{
names.Add(name);
actions.Add(pressed);
NumButtons++;
}
public string ButtonName(int index)
{
return names[index];
}
public bool Pressed(int index)
{
return actions[index]();
}
}
}

View File

@ -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));
}