Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions XOutput/ControllerDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ byte DPad(byte subType, byte num)
private void updateInput()
{
joystick.Poll();
JoystickState jState = joystick.GetCurrentState();
JoystickState jState;
jState = joystick.GetCurrentState();
buttons = jState.GetButtons();
dPads = jState.GetPointOfViewControllers();
analogs = GetAxes(jState);
Expand Down Expand Up @@ -230,8 +231,7 @@ public byte[] getoutput()
Report[27] = cOutput.L2;

return Report;
}


}

}
}
63 changes: 46 additions & 17 deletions XOutput/ControllerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ private class ContData
private ContData[] processingData = new ContData[4];
private Control handle;
public bool isExclusive = false;
public bool isPersistent = true;


private object[] ds4locks = new object[4];
private bool[] enabled = new bool[4];
private bool[] plugged = new bool[4];
private bool[] threadRunning = new bool[4];

public ControllerManager(Control _handle)
: base(BUS_CLASS_GUID)
Expand All @@ -38,6 +42,7 @@ public ControllerManager(Control _handle)
ds4locks[1] = new object();
ds4locks[2] = new object();
ds4locks[3] = new object();
enabled[0] = enabled[1] = enabled[2] = enabled[3] = true;
}

#region Utility Functions
Expand Down Expand Up @@ -65,6 +70,11 @@ public void changeExclusive(bool e)
}
}

public void changePersistent(bool v)
{
isPersistent = v;
}

public ControllerDevice getController(int n)
{
return devices[n];
Expand All @@ -80,6 +90,8 @@ public void Swap(int i, int p)
devices[i - 1] = devices[p - 1];
devices[p - 1] = s;
devices[p - 1].changeNumber(p);
enabled[i - 1] = devices[i - 1].enabled;
enabled[p - 1] = devices[p - 1].enabled;

if (devices[i - 1] != null)
devices[i - 1].changeNumber(i);
Expand All @@ -89,6 +101,7 @@ public void Swap(int i, int p)

public void setControllerEnable(int i, bool b)
{
enabled[i] = b;
devices[i].enabled = b;
}

Expand Down Expand Up @@ -133,13 +146,13 @@ public override bool Start()
{
if (devices[i] != null && devices[i].enabled)
{
running = true;
processingData[i] = new ContData();
Console.WriteLine("Plug " + i);
Plugin(i + 1);
int t = i;
workers[i] = new Thread(() =>
{ ProcessData(t); });
threadRunning[i] = true;
workers[i].Start();
}
}
Expand All @@ -154,10 +167,15 @@ public ControllerDevice[] detectControllers()
if (devices[i] != null && !directInput.IsDeviceAttached(devices[i].joystick.Information.InstanceGuid))
{
Console.WriteLine(devices[i].joystick.Properties.InstanceName + " Removed");
devices[i] = null;
workers[i].Abort();
workers[i] = null;
Unplug(i + 1);
if (IsActive && devices[i].enabled)
{
threadRunning[i] = false;
workers[i].Join();
workers[i] = null;
if (!isPersistent)
Unplug(i + 1);
}
devices[i] = null;
}
}

Expand Down Expand Up @@ -205,14 +223,19 @@ public ControllerDevice[] detectControllers()
joystick.Acquire();

devices[spot] = new ControllerDevice(joystick, spot + 1);
if (IsActive)
devices[spot].enabled = enabled[spot];
if (IsActive && devices[spot].enabled)
{
processingData[spot] = new ContData();
Console.WriteLine("Plug " + spot);
Plugin(spot + 1);
if (!isPersistent)
{
Console.WriteLine("Plug " + spot);
Plugin(spot + 1);
}
int t = spot;
workers[spot] = new Thread(() =>
{ ProcessData(t); });
threadRunning[spot] = true;
workers[spot].Start();
}
}
Expand All @@ -229,10 +252,11 @@ public override bool Stop()
if (devices[i] != null && devices[i].enabled)
{
Console.WriteLine(i);
workers[i].Abort();
threadRunning[i] = false;
workers[i].Join();
workers[i] = null;
Unplug(i + 1);
}
}
Unplug(i + 1);
}

}
Expand All @@ -241,7 +265,7 @@ public override bool Stop()

public Boolean Plugin(Int32 Serial)
{
if (IsActive)
if (IsActive && !plugged[Serial-1])
{
Int32 Transfered = 0;
Byte[] Buffer = new Byte[16];
Expand All @@ -255,6 +279,7 @@ public Boolean Plugin(Int32 Serial)
Buffer[5] = (Byte)((Serial >> 8) & 0xFF);
Buffer[6] = (Byte)((Serial >> 16) & 0xFF);
Buffer[7] = (Byte)((Serial >> 24) & 0xFF);
plugged[Serial - 1] = true;

return DeviceIoControl(m_FileHandle, 0x2A4000, Buffer, Buffer.Length, null, 0, ref Transfered, IntPtr.Zero);
}
Expand All @@ -264,7 +289,7 @@ public Boolean Plugin(Int32 Serial)

public Boolean Unplug(Int32 Serial)
{
if (IsActive)
if (IsActive && plugged[Serial-1])
{
Int32 Transfered = 0;
Byte[] Buffer = new Byte[16];
Expand All @@ -278,7 +303,7 @@ public Boolean Unplug(Int32 Serial)
Buffer[5] = (Byte)((Serial >> 8) & 0xFF);
Buffer[6] = (Byte)((Serial >> 16) & 0xFF);
Buffer[7] = (Byte)((Serial >> 24) & 0xFF);

plugged[Serial - 1] = false;
return DeviceIoControl(m_FileHandle, 0x2A4004, Buffer, Buffer.Length, null, 0, ref Transfered, IntPtr.Zero);
}

Expand All @@ -287,7 +312,7 @@ public Boolean Unplug(Int32 Serial)

private void ProcessData(int n)
{
while (IsActive)
while (IsActive && threadRunning[n])
{
lock (ds4locks[n])
{
Expand All @@ -297,14 +322,14 @@ private void ProcessData(int n)
//continue;
}
byte[] data = devices[n].getoutput();
if (data != null && devices[n].enabled)
if (data != null && directInput.IsDeviceAttached(devices[n].joystick.Information.InstanceGuid))
{

data[0] = (byte)n;
Parse(data, processingData[n].parsedData);
Report(processingData[n].parsedData, processingData[n].output);
}
Thread.Sleep(1);
Thread.Sleep(5);
}
}
}
Expand Down Expand Up @@ -381,5 +406,9 @@ public void Parse(Byte[] Input, Byte[] Output)
}
}

~ControllerManager()
{
Stop();
}
}
}
Loading