Skip to content

Commit

Permalink
Added Xinput support, guitar now acts as a 360 controller.
Browse files Browse the repository at this point in the history
SCPDrivers required however
  • Loading branch information
artman41 committed Jul 23, 2018
1 parent 8f648ac commit e0948a4
Show file tree
Hide file tree
Showing 9 changed files with 1,206 additions and 37 deletions.
35 changes: 35 additions & 0 deletions GuitarSniffer/DataValues.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
namespace GuitarSniffer {
public class Digital {

public bool Green { get; set; }
public bool Red { get; set; }
public bool Yellow { get; set; }
public bool Blue { get; set; }
public bool Orange { get; set; }
public bool Start { get; set; }
public bool Menu { get; set; }
}

public class Analogue {
public byte Whammy { get; set; }
public byte Acceleration { get; set; }
}

public class States {
public enum StrumState {
Released, Up, Down
}

public enum SliderState {
Pos1, Pos2, Pos3, Pos4, Pos5
}

public SliderState Slider { get; set; }
public StrumState Strum { get; set; }
}
public class DataValues {
public Digital Buttons { get; set; } = new Digital();
public Analogue Analogue { get; set; } = new Analogue();
public States States { get; set; } = new States();
}
}
13 changes: 13 additions & 0 deletions GuitarSniffer/GuitarSniffer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,29 @@
<Reference Include="PcapDotNet.Packets, Version=1.0.4.25028, Culture=neutral, PublicKeyToken=06a20bc2fabb1931">
<HintPath>..\packages\Pcap.Net.x86.1.0.4.1\lib\net45\PcapDotNet.Packets.dll</HintPath>
</Reference>
<Reference Include="SharpDX, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b4dcf0f35e5521f1">
<HintPath>..\packages\SharpDX.4.2.0-ci259\lib\net45\SharpDX.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="SharpDX.DirectInput, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b4dcf0f35e5521f1">
<HintPath>..\packages\SharpDX.DirectInput.3.0.0\lib\net45\SharpDX.DirectInput.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="DataValues.cs" />
<Compile Include="InputManager.cs" />
<Compile Include="PacketSniffer.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ScpDevice.cs" />
<Compile Include="ScpDevice.Designer.cs" />
<Compile Include="X360Device.cs" />
<Compile Include="X360Device.designer.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
139 changes: 121 additions & 18 deletions GuitarSniffer/InputManager.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,117 @@
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using DS4Windows;
using SharpDX.DirectInput;

//using XInputDotNetPure;

namespace GuitarSniffer {
public static class InputManager {
//private static GamePad GuitarGamePad { get; set; }
public static DataValues Keys { get; set; } = new DataValues();
private static Thread UpdateThread { get; set; }
private static X360Device x360Bus { get; set; }

static InputManager() {
x360Bus = new X360Device();
Console.WriteLine($"BUS OPEN: {x360Bus.Open()}");
Console.WriteLine($"BUS START: {x360Bus.Start()}");
if (x360Bus.Open() && x360Bus.Start())
{
if (!x360Bus.Plugin(0)) {
Debug.WriteLine("Error 'plugging in' xinput controller.");
}
}
UpdateThread = new Thread(UpdateController);
UpdateThread.Start();
//GuitarGamePad = new GamePad();
//XInputDotNetPure.GamePad.GetState()
}

private static void UpdateController() {
var Rumble = new byte[8];
while (true) {
x360Bus.Report(GetXinputData(), Rumble);
}
}

static byte[] GetXinputData() {
var xinputData = new byte[28];
xinputData[0] = 0x1C;
xinputData[4] = 1; //plugging in is zero indexed, but update frames are 1 indexed
xinputData[9] = 0x14;

for (int i = 10; i < xinputData.Length; i++)
{
xinputData[i] = 0;
}

xinputData[14] = 0; //LX
xinputData[15] = 0; //LX
xinputData[16] = 0; //LY
xinputData[17] = 0; //LY

xinputData[18] = Keys.Analogue.Whammy; //RX
xinputData[19] = Keys.Analogue.Whammy; //RX
//Console.WriteLine($"WHAMMY: {Keys.Analogue.Whammy}");

xinputData[20] = Keys.Analogue.Acceleration; //RY
xinputData[21] = Keys.Analogue.Acceleration; //RY
//Console.WriteLine($"TILT: {Keys.Analogue.Acceleration}");

/*if (left)
{
xinputData[10] |= (Byte)(1 << 2);
}
if (right)
{
xinputData[10] |= (Byte)(1 << 3);
}*/
if (Keys.States.Strum == States.StrumState.Up)
{
xinputData[10] |= (Byte)(1 << 0);
}
if (Keys.States.Strum == States.StrumState.Down)
{
xinputData[10] |= (Byte)(1 << 1);
}

if (Keys.Buttons.Green) xinputData[11] |= (Byte)(1 << 4); // A
if (Keys.Buttons.Red) xinputData[11] |= (Byte)(1 << 5); // B
if (Keys.Buttons.Blue) xinputData[11] |= (Byte)(1 << 6); // X
if (Keys.Buttons.Yellow) xinputData[11] |= (Byte)(1 << 7); // Y

if (Keys.Buttons.Orange) xinputData[11] |= (Byte)(1 << 0); // Left Shoulder
//if (r) xinputData[11] |= (Byte)(1 << 1); // Right Shoulder
if (Keys.Buttons.Menu) xinputData[10] |= (Byte)(1 << 5); // Back
if (Keys.Buttons.Start) xinputData[10] |= (Byte)(1 << 4); // Start

xinputData[12] = 0; // Left Trigger
xinputData[13] = 0; // Right Trigger

return xinputData;
}

public static void Acceleration(byte accel) {
//Console.WriteLine($"Guitar moving at speed {accel}");
Keys.Analogue.Acceleration = accel;
}

public static void Whammy(byte pressure) {
Console.WriteLine($"Whammy at pressure {pressure}");
Keys.Analogue.Whammy = pressure;
}

public static void Slider(int pos) {
public static void Slider(States.SliderState pos) {
Console.WriteLine($"Slider at position {pos}");
Keys.States.Slider = pos;
}

public static void Green(bool isDown) {
Console.WriteLine($"Green is {isDown}");
if (isDown) {
/*if (isDown) {
keybd_event(VK_0,
0x45,
KEYEVENTF_EXTENDEDKEY | 0,
Expand All @@ -28,12 +121,13 @@ public static void Green(bool isDown) {
0x45,
KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,
(UIntPtr) 0);
}
}*/
Keys.Buttons.Green = isDown;
}

public static void Red(bool isDown) {
Console.WriteLine($"Red is {isDown}");
if (isDown) {
/*if (isDown) {
keybd_event(VK_1,
0x45,
KEYEVENTF_EXTENDEDKEY | 0,
Expand All @@ -43,12 +137,13 @@ public static void Red(bool isDown) {
0x45,
KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,
(UIntPtr) 0);
}
}*/
Keys.Buttons.Red = isDown;
}

public static void Yellow(bool isDown) {
Console.WriteLine($"Yellow is {isDown}");
if (isDown) {
/*if (isDown) {
keybd_event(VK_2,
0x45,
KEYEVENTF_EXTENDEDKEY | 0,
Expand All @@ -58,11 +153,13 @@ public static void Yellow(bool isDown) {
0x45,
KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,
(UIntPtr) 0);
}
}*/
Keys.Buttons.Yellow = isDown;
}

public static void Blue(bool isDown) {
Console.WriteLine($"Blue is {isDown}");
/*
if (isDown) {
keybd_event(VK_3,
0x45,
Expand All @@ -73,11 +170,13 @@ public static void Blue(bool isDown) {
0x45,
KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,
(UIntPtr) 0);
}
}*/
Keys.Buttons.Blue = isDown;
}

public static void Orange(bool isDown) {
Console.WriteLine($"Orange is {isDown}");
/*
if (isDown) {
keybd_event(VK_4,
0x45,
Expand All @@ -88,13 +187,14 @@ public static void Orange(bool isDown) {
0x45,
KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,
(UIntPtr) 0);
}
}*/
Keys.Buttons.Orange = isDown;
}

private static readonly string[] str = {"Released", "Up", "Down"};
public static void Strum(int state) {
Console.WriteLine($"Strum is {str[state]}");
switch(state) {
// private static readonly string[] str = {"Released", "Up", "Down"};
public static void Strum(States.StrumState state) {
Console.WriteLine($"Strum is {state}");
/*switch(state) {
case 0: //released
keybd_event(VK_5,
0x45,
Expand Down Expand Up @@ -126,12 +226,13 @@ public static void Strum(int state) {
(UIntPtr) 0);
break;
}
}*/
Keys.States.Strum = state;
}

public static void Start(bool isDown) {
Console.WriteLine($"Start is {isDown}");
if (isDown) {
/*if (isDown) {
keybd_event(VK_RETURN,
0x45,
KEYEVENTF_EXTENDEDKEY | 0,
Expand All @@ -141,12 +242,13 @@ public static void Start(bool isDown) {
0x45,
KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,
(UIntPtr) 0);
}
}*/
Keys.Buttons.Start = isDown;
}

public static void Menu(bool isDown) {
Console.WriteLine($"Menu is {isDown}");
if (isDown) {
/*if (isDown) {
keybd_event(VK_ESCAPE,
0x45,
KEYEVENTF_EXTENDEDKEY | 0,
Expand All @@ -156,7 +258,8 @@ public static void Menu(bool isDown) {
0x45,
KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,
(UIntPtr) 0);
}
}*/
Keys.Buttons.Menu = isDown;
}

#region dll stuff
Expand Down
Loading

0 comments on commit e0948a4

Please sign in to comment.