Skip to content

Commit

Permalink
Add Guide button support (thanks benblo), and various fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
speps committed Aug 7, 2014
1 parent b169811 commit 1ab05fd
Show file tree
Hide file tree
Showing 19 changed files with 120 additions and 37 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
*.xcuserdatad
*.orig
*.meta
*.unityPackage
obj/
Binaries/
XInputUnity/Library/
1 change: 1 addition & 0 deletions Build/UpdateUnityProject.bat
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ if not exist ..\XInputUnity\Assets\Plugins\x86 md ..\XInputUnity\Assets\Plugins\
if not exist ..\XInputUnity\Assets\Plugins\x86_64 md ..\XInputUnity\Assets\Plugins\x86_64

copy ..\BinariesX86\Release\XInputInterface.dll ..\XInputUnity\Assets\Plugins\x86\
copy ..\BinariesX86\Release\XInputInterface.dll ..\XInputUnity\
copy ..\BinariesX86\Release\XInputDotNetPure.dll ..\XInputUnity\Assets\Plugins\x86\

copy ..\BinariesX64\Release\XInputInterface.dll ..\XInputUnity\Assets\Plugins\x86_64\
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ Some examples are available in this repository :
* Copy `[Project Folder]\Assets\Plugins\x86\XInputInterface.dll` to `[Project Folder]\XInputInterface.dll`
* **Making a Build does NOT require to copy `XInputInterface.dll` in the same folder the your game .exe file**

**NOTE** : you may see this error message but it should still work as expected, the message won't appear in a Build

![License Error Message](https://raw.github.com/speps/XInputDotNet/master/XInputUnity/LicenseError.jpg)

### Notes

* Under Windows XP, you'll need special drivers for your Xbox 360 Controller. You can find them at this address : [XBox 360 Controller for Windows Software](http://www.microsoft.com/en-us/download/details.aspx?id=34001)
Expand Down
4 changes: 3 additions & 1 deletion XInputDemo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ static void Main(string[] args)
Console.WriteLine("IsConnected {0} Packet #{1}", state.IsConnected, state.PacketNumber);
Console.WriteLine("\tTriggers {0} {1}", state.Triggers.Left, state.Triggers.Right);
Console.WriteLine("\tD-Pad {0} {1} {2} {3}", state.DPad.Up, state.DPad.Right, state.DPad.Down, state.DPad.Left);
Console.WriteLine("\tButtons Start {0} Back {1} LeftStick {2} RightStick {3} LeftShoulder {4} RightShoulder {5} A {6} B {7} X {8} Y {9}", state.Buttons.Start, state.Buttons.Back, state.Buttons.LeftStick, state.Buttons.RightStick, state.Buttons.LeftShoulder, state.Buttons.RightShoulder, state.Buttons.A, state.Buttons.B, state.Buttons.X, state.Buttons.Y);
Console.WriteLine("\tButtons Start {0} Back {1} LeftStick {2} RightStick {3} LeftShoulder {4} RightShoulder {5} Guide {6} A {7} B {9} X {9} Y {10}",
state.Buttons.Start, state.Buttons.Back, state.Buttons.LeftStick, state.Buttons.RightStick, state.Buttons.LeftShoulder, state.Buttons.RightShoulder,
state.Buttons.Guide, state.Buttons.A, state.Buttons.B, state.Buttons.X, state.Buttons.Y);
Console.WriteLine("\tSticks Left {0} {1} Right {2} {3}", state.ThumbSticks.Left.X, state.ThumbSticks.Left.Y, state.ThumbSticks.Right.X, state.ThumbSticks.Right.Y);
GamePad.SetVibration(PlayerIndex.One, state.Triggers.Left, state.Triggers.Right);
Thread.Sleep(16);
Expand Down
57 changes: 32 additions & 25 deletions XInputDotNetPure/GamePad.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@

namespace XInputDotNetPure
{

class Imports
{
internal const string DLLName = "XInputInterface";

[DllImport(DLLName)]
public static extern uint XInputGamePadGetState(uint playerIndex, IntPtr state);
public static extern uint XInputGamePadGetState(uint playerIndex, out GamePadState.RawState state);
[DllImport(DLLName)]
public static extern void XInputGamePadSetState(uint playerIndex, float leftMotor, float rightMotor);
}
Expand All @@ -22,18 +21,19 @@ public enum ButtonState

public struct GamePadButtons
{
ButtonState start, back, leftStick, rightStick, leftShoulder, rightShoulder, a, b, x, y;
ButtonState start, back, leftStick, rightStick, leftShoulder, rightShoulder, guide, a, b, x, y;

internal GamePadButtons(ButtonState start, ButtonState back, ButtonState leftStick, ButtonState rightStick,
ButtonState leftShoulder, ButtonState rightShoulder, ButtonState a, ButtonState b,
ButtonState x, ButtonState y)
ButtonState leftShoulder, ButtonState rightShoulder, ButtonState guide,
ButtonState a, ButtonState b, ButtonState x, ButtonState y)
{
this.start = start;
this.back = back;
this.leftStick = leftStick;
this.rightStick = rightStick;
this.leftShoulder = leftShoulder;
this.rightShoulder = rightShoulder;
this.guide = guide;
this.a = a;
this.b = b;
this.x = x;
Expand Down Expand Up @@ -70,6 +70,11 @@ public ButtonState RightShoulder
get { return rightShoulder; }
}

public ButtonState Guide
{
get { return guide; }
}

public ButtonState A
{
get { return a; }
Expand Down Expand Up @@ -190,14 +195,16 @@ public float Right

public struct GamePadState
{
[StructLayout(LayoutKind.Sequential)]
internal struct RawState
{
public uint dwPacketNumber;
public GamePad Gamepad;

[StructLayout(LayoutKind.Sequential)]
public struct GamePad
{
public ushort dwButtons;
public ushort wButtons;
public byte bLeftTrigger;
public byte bRightTrigger;
public short sThumbLX;
Expand Down Expand Up @@ -226,6 +233,7 @@ enum ButtonsConstants
RightThumb = 0x00000080,
LeftShoulder = 0x0100,
RightShoulder = 0x0200,
Guide = 0x0400,
A = 0x1000,
B = 0x2000,
X = 0x4000,
Expand All @@ -239,7 +247,7 @@ internal GamePadState(bool isConnected, RawState rawState, GamePadDeadZone deadZ
if (!isConnected)
{
rawState.dwPacketNumber = 0;
rawState.Gamepad.dwButtons = 0;
rawState.Gamepad.wButtons = 0;
rawState.Gamepad.bLeftTrigger = 0;
rawState.Gamepad.bRightTrigger = 0;
rawState.Gamepad.sThumbLX = 0;
Expand All @@ -250,22 +258,23 @@ internal GamePadState(bool isConnected, RawState rawState, GamePadDeadZone deadZ

packetNumber = rawState.dwPacketNumber;
buttons = new GamePadButtons(
(rawState.Gamepad.dwButtons & (uint)ButtonsConstants.Start) != 0 ? ButtonState.Pressed : ButtonState.Released,
(rawState.Gamepad.dwButtons & (uint)ButtonsConstants.Back) != 0 ? ButtonState.Pressed : ButtonState.Released,
(rawState.Gamepad.dwButtons & (uint)ButtonsConstants.LeftThumb) != 0 ? ButtonState.Pressed : ButtonState.Released,
(rawState.Gamepad.dwButtons & (uint)ButtonsConstants.RightThumb) != 0 ? ButtonState.Pressed : ButtonState.Released,
(rawState.Gamepad.dwButtons & (uint)ButtonsConstants.LeftShoulder) != 0 ? ButtonState.Pressed : ButtonState.Released,
(rawState.Gamepad.dwButtons & (uint)ButtonsConstants.RightShoulder) != 0 ? ButtonState.Pressed : ButtonState.Released,
(rawState.Gamepad.dwButtons & (uint)ButtonsConstants.A) != 0 ? ButtonState.Pressed : ButtonState.Released,
(rawState.Gamepad.dwButtons & (uint)ButtonsConstants.B) != 0 ? ButtonState.Pressed : ButtonState.Released,
(rawState.Gamepad.dwButtons & (uint)ButtonsConstants.X) != 0 ? ButtonState.Pressed : ButtonState.Released,
(rawState.Gamepad.dwButtons & (uint)ButtonsConstants.Y) != 0 ? ButtonState.Pressed : ButtonState.Released
(rawState.Gamepad.wButtons & (uint)ButtonsConstants.Start) != 0 ? ButtonState.Pressed : ButtonState.Released,
(rawState.Gamepad.wButtons & (uint)ButtonsConstants.Back) != 0 ? ButtonState.Pressed : ButtonState.Released,
(rawState.Gamepad.wButtons & (uint)ButtonsConstants.LeftThumb) != 0 ? ButtonState.Pressed : ButtonState.Released,
(rawState.Gamepad.wButtons & (uint)ButtonsConstants.RightThumb) != 0 ? ButtonState.Pressed : ButtonState.Released,
(rawState.Gamepad.wButtons & (uint)ButtonsConstants.LeftShoulder) != 0 ? ButtonState.Pressed : ButtonState.Released,
(rawState.Gamepad.wButtons & (uint)ButtonsConstants.RightShoulder) != 0 ? ButtonState.Pressed : ButtonState.Released,
(rawState.Gamepad.wButtons & (uint)ButtonsConstants.Guide) != 0 ? ButtonState.Pressed : ButtonState.Released,
(rawState.Gamepad.wButtons & (uint)ButtonsConstants.A) != 0 ? ButtonState.Pressed : ButtonState.Released,
(rawState.Gamepad.wButtons & (uint)ButtonsConstants.B) != 0 ? ButtonState.Pressed : ButtonState.Released,
(rawState.Gamepad.wButtons & (uint)ButtonsConstants.X) != 0 ? ButtonState.Pressed : ButtonState.Released,
(rawState.Gamepad.wButtons & (uint)ButtonsConstants.Y) != 0 ? ButtonState.Pressed : ButtonState.Released
);
dPad = new GamePadDPad(
(rawState.Gamepad.dwButtons & (uint)ButtonsConstants.DPadUp) != 0 ? ButtonState.Pressed : ButtonState.Released,
(rawState.Gamepad.dwButtons & (uint)ButtonsConstants.DPadDown) != 0 ? ButtonState.Pressed : ButtonState.Released,
(rawState.Gamepad.dwButtons & (uint)ButtonsConstants.DPadLeft) != 0 ? ButtonState.Pressed : ButtonState.Released,
(rawState.Gamepad.dwButtons & (uint)ButtonsConstants.DPadRight) != 0 ? ButtonState.Pressed : ButtonState.Released
(rawState.Gamepad.wButtons & (uint)ButtonsConstants.DPadUp) != 0 ? ButtonState.Pressed : ButtonState.Released,
(rawState.Gamepad.wButtons & (uint)ButtonsConstants.DPadDown) != 0 ? ButtonState.Pressed : ButtonState.Released,
(rawState.Gamepad.wButtons & (uint)ButtonsConstants.DPadLeft) != 0 ? ButtonState.Pressed : ButtonState.Released,
(rawState.Gamepad.wButtons & (uint)ButtonsConstants.DPadRight) != 0 ? ButtonState.Pressed : ButtonState.Released
);

thumbSticks = new GamePadThumbSticks(
Expand Down Expand Up @@ -333,10 +342,8 @@ public static GamePadState GetState(PlayerIndex playerIndex)

public static GamePadState GetState(PlayerIndex playerIndex, GamePadDeadZone deadZone)
{
IntPtr gamePadStatePointer = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(GamePadState.RawState)));
uint result = Imports.XInputGamePadGetState((uint)playerIndex, gamePadStatePointer);
GamePadState.RawState state = (GamePadState.RawState)Marshal.PtrToStructure(gamePadStatePointer, typeof(GamePadState.RawState));
Marshal.FreeHGlobal(gamePadStatePointer);
GamePadState.RawState state;
uint result = Imports.XInputGamePadGetState((uint)playerIndex, out state);
return new GamePadState(result == Utils.Success, state, deadZone);
}

Expand Down
42 changes: 38 additions & 4 deletions XInputInterface/GamePad.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,46 @@
#include "GamePad.h"

DWORD XInputGamePadGetState(DWORD dwUserIndex, XINPUT_STATE *pState)
namespace
{
return XInputGetState(dwUserIndex, pState);
typedef DWORD (*XInputGetStatePointer)(DWORD dwUserIndex, XINPUT_STATE* pState);
typedef DWORD (*XInputSetStatePointer)(DWORD dwUserIndex, XINPUT_VIBRATION* pVibration);

class XInputLoader
{
public:
XInputLoader()
: mHandle(0), mGetState(0), mSetState(0)
{
mHandle = LoadLibrary("xinput1_3.dll");

// Ordinal 100 is the same as XInputGetState but supports the Guide button.
mGetState = (XInputGetStatePointer)GetProcAddress(mHandle, (LPCSTR)100);

mSetState = (XInputSetStatePointer)GetProcAddress(mHandle, "XInputSetState");
}

~XInputLoader()
{
FreeLibrary(mHandle);
}

XInputGetStatePointer mGetState;
XInputSetStatePointer mSetState;

private:
HMODULE mHandle;
};

XInputLoader gXInputLoader;
}

DWORD XInputGamePadGetState(DWORD dwUserIndex, XINPUT_STATE* pState)
{
return gXInputLoader.mGetState(dwUserIndex, pState);
}

void XInputGamePadSetState(DWORD dwUserIndex, float leftMotor, float rightMotor)
{
XINPUT_VIBRATION vibration = { (int)(leftMotor * 65535), (int)(rightMotor * 65535) };
XInputSetState(dwUserIndex, &vibration);
XINPUT_VIBRATION vibration = { (int)(leftMotor * 65535), (int)(rightMotor * 65535) };
gXInputLoader.mSetState(dwUserIndex, &vibration);
}
4 changes: 0 additions & 4 deletions XInputInterface/XInputInterface.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@
<DebugInformationFormat>EditAndContinue</DebugInformationFormat>
</ClCompile>
<Link>
<AdditionalDependencies>XInput.lib;%(AdditionalDependencies)</AdditionalDependencies>
<GenerateDebugInformation>true</GenerateDebugInformation>
<TargetMachine>MachineX86</TargetMachine>
<ImportLibrary>$(IntDir)\$(TargetName).lib</ImportLibrary>
Expand All @@ -103,7 +102,6 @@
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
</ClCompile>
<Link>
<AdditionalDependencies>XInput.lib;%(AdditionalDependencies)</AdditionalDependencies>
<GenerateDebugInformation>true</GenerateDebugInformation>
<ImportLibrary>$(IntDir)\$(TargetName).lib</ImportLibrary>
</Link>
Expand All @@ -119,7 +117,6 @@
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
</ClCompile>
<Link>
<AdditionalDependencies>XInput.lib;%(AdditionalDependencies)</AdditionalDependencies>
<GenerateDebugInformation>false</GenerateDebugInformation>
<OptimizeReferences>true</OptimizeReferences>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
Expand All @@ -138,7 +135,6 @@
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
</ClCompile>
<Link>
<AdditionalDependencies>XInput.lib;%(AdditionalDependencies)</AdditionalDependencies>
<GenerateDebugInformation>false</GenerateDebugInformation>
<OptimizeReferences>true</OptimizeReferences>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
Expand Down
14 changes: 14 additions & 0 deletions XInputReporter/MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions XInputReporter/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ private void UpdateState()
checkY.Checked = reporterState.LastActiveState.Buttons.Y == XInputDotNetPure.ButtonState.Pressed;
checkStart.Checked = reporterState.LastActiveState.Buttons.Start == XInputDotNetPure.ButtonState.Pressed;
checkBack.Checked = reporterState.LastActiveState.Buttons.Back == XInputDotNetPure.ButtonState.Pressed;
checkGuide.Checked = reporterState.LastActiveState.Buttons.Guide == XInputDotNetPure.ButtonState.Pressed;
checkStickLeft.Checked = reporterState.LastActiveState.Buttons.LeftStick == XInputDotNetPure.ButtonState.Pressed;
checkStickRight.Checked = reporterState.LastActiveState.Buttons.RightStick == XInputDotNetPure.ButtonState.Pressed;
checkShoulderLeft.Checked = reporterState.LastActiveState.Buttons.LeftShoulder == XInputDotNetPure.ButtonState.Pressed;
Expand Down
6 changes: 4 additions & 2 deletions XInputReporter/ReporterState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,16 @@ public bool Poll()
}
}

GamePad.SetVibration(playerIndices[lastActivePlayerIndex], 0.0f, 0.0f);

lastActivePlayerIndex = activePlayerIndex;

if (LinkTriggersToVibration)
{
GamePad.SetVibration(playerIndices[lastActivePlayerIndex], LastActiveState.Triggers.Left, LastActiveState.Triggers.Right);
}
else
{
GamePad.SetVibration(playerIndices[lastActivePlayerIndex], 0.0f, 0.0f);
}

return changed;
}
Expand Down
Binary file modified XInputUnity/Assets/Plugins/x86/XInputDotNetPure.dll
Binary file not shown.
Binary file modified XInputUnity/Assets/Plugins/x86/XInputInterface.dll
Binary file not shown.
Binary file modified XInputUnity/Assets/Plugins/x86_64/XInputDotNetPure.dll
Binary file not shown.
Binary file modified XInputUnity/Assets/Plugins/x86_64/XInputInterface.dll
Binary file not shown.
2 changes: 1 addition & 1 deletion XInputUnity/Assets/XInputDotNet/Examples/XInputTestCS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void OnGUI()
text += string.Format("IsConnected {0} Packet #{1}\n", state.IsConnected, state.PacketNumber);
text += string.Format("\tTriggers {0} {1}\n", state.Triggers.Left, state.Triggers.Right);
text += string.Format("\tD-Pad {0} {1} {2} {3}\n", state.DPad.Up, state.DPad.Right, state.DPad.Down, state.DPad.Left);
text += string.Format("\tButtons Start {0} Back {1}\n", state.Buttons.Start, state.Buttons.Back);
text += string.Format("\tButtons Start {0} Back {1} Guide {2}\n", state.Buttons.Start, state.Buttons.Back, state.Buttons.Guide);
text += string.Format("\tButtons LeftStick {0} RightStick {1} LeftShoulder {2} RightShoulder {3}\n", state.Buttons.LeftStick, state.Buttons.RightStick, state.Buttons.LeftShoulder, state.Buttons.RightShoulder);
text += string.Format("\tButtons A {0} B {1} X {2} Y {3}\n", state.Buttons.A, state.Buttons.B, state.Buttons.X, state.Buttons.Y);
text += string.Format("\tSticks Left {0} {1} Right {2} {3}\n", state.ThumbSticks.Left.X, state.ThumbSticks.Left.Y, state.ThumbSticks.Right.X, state.ThumbSticks.Right.Y);
Expand Down
21 changes: 21 additions & 0 deletions XInputUnity/Assets/XInputDotNet/MITLicense.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License

Copyright (c) 2009 Remi Gillig <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Binary file added XInputUnity/LicenseError.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified XInputUnity/ProjectSettings/ProjectSettings.asset
Binary file not shown.
Binary file added XInputUnity/XInputInterface.dll
Binary file not shown.

0 comments on commit 1ab05fd

Please sign in to comment.