Enable xinput hotplug from a sync change when using Sony dongle

This commit is contained in:
Travis Nickles 2017-06-29 19:27:08 -07:00
parent b34c258a59
commit 2fabd860a2

View File

@ -143,12 +143,13 @@ namespace DS4Windows
device.setUiContext(SynchronizationContext.Current); device.setUiContext(SynchronizationContext.Current);
device.Removal += this.On_DS4Removal; device.Removal += this.On_DS4Removal;
device.Removal += DS4Devices.On_Removal; device.Removal += DS4Devices.On_Removal;
device.SyncChange += this.On_SyncChange;
device.SyncChange += DS4Devices.UpdateSerial; device.SyncChange += DS4Devices.UpdateSerial;
device.SerialChange += this.On_SerialChange; device.SerialChange += this.On_SerialChange;
touchPad[i] = new Mouse(i, device); touchPad[i] = new Mouse(i, device);
device.LightBarColor = getMainColor(i); device.LightBarColor = getMainColor(i);
if (!getDInputOnly(i)) if (!getDInputOnly(i) && device.isSynced())
{ {
int xinputIndex = x360Bus.FirstController + i; int xinputIndex = x360Bus.FirstController + i;
LogDebug("Plugging in X360 Controller #" + xinputIndex); LogDebug("Plugging in X360 Controller #" + xinputIndex);
@ -319,12 +320,13 @@ namespace DS4Windows
device.setUiContext(uiContext); device.setUiContext(uiContext);
device.Removal += this.On_DS4Removal; device.Removal += this.On_DS4Removal;
device.Removal += DS4Devices.On_Removal; device.Removal += DS4Devices.On_Removal;
device.SyncChange += this.On_SyncChange;
device.SyncChange += DS4Devices.UpdateSerial; device.SyncChange += DS4Devices.UpdateSerial;
device.SerialChange += this.On_SerialChange; device.SerialChange += this.On_SerialChange;
touchPad[Index] = new Mouse(Index, device); touchPad[Index] = new Mouse(Index, device);
device.LightBarColor = getMainColor(Index); device.LightBarColor = getMainColor(Index);
device.Report += this.On_Report; device.Report += this.On_Report;
if (!getDInputOnly(Index)) if (!getDInputOnly(Index) && device.isSynced())
{ {
int xinputIndex = x360Bus.FirstController + Index; int xinputIndex = x360Bus.FirstController + Index;
LogDebug("Plugging in X360 Controller #" + xinputIndex); LogDebug("Plugging in X360 Controller #" + xinputIndex);
@ -562,6 +564,60 @@ namespace DS4Windows
} }
} }
protected void On_SyncChange(object sender, EventArgs e)
{
DS4Device device = (DS4Device)sender;
int ind = -1;
for (int i = 0, arlength = DS4_CONTROLLER_COUNT; ind == -1 && i < arlength; i++)
{
DS4Device tempDev = DS4Controllers[i];
if (tempDev != null && device == tempDev)
ind = i;
}
if (ind >= 0)
{
bool synced = device.isSynced();
if (!synced)
{
if (!useDInputOnly[ind])
{
bool unplugResult = x360Bus.Unplug(ind);
int xinputIndex = x360Bus.FirstController + ind;
if (unplugResult)
{
LogDebug("X360 Controller # " + xinputIndex + " unplugged");
useDInputOnly[ind] = true;
}
else
{
LogDebug("X360 Controller # " + xinputIndex + " failed to unplug");
}
}
}
else
{
if (!getDInputOnly(ind))
{
int xinputIndex = x360Bus.FirstController + ind;
LogDebug("Plugging in X360 Controller #" + xinputIndex);
bool xinputResult = x360Bus.Plugin(ind);
if (xinputResult)
{
LogDebug("X360 Controller # " + xinputIndex + " connected");
useDInputOnly[ind] = false;
}
else
{
LogDebug("X360 Controller # " + xinputIndex + " failed. Using DInput only mode");
useDInputOnly[ind] = true;
}
}
}
}
}
//Called when DS4 is disconnected or timed out //Called when DS4 is disconnected or timed out
protected virtual void On_DS4Removal(object sender, EventArgs e) protected virtual void On_DS4Removal(object sender, EventArgs e)
{ {