Unify output device plugging and unplugging

This commit is contained in:
Travis Nickles 2019-12-02 15:04:23 -06:00
parent 2121f45771
commit 424b456175
2 changed files with 84 additions and 145 deletions

View File

@ -359,6 +359,69 @@ namespace DS4Windows
}
}
public void PluginOutDev(int index, DS4Device device)
{
OutContType contType = Global.OutContType[index];
if (useDInputOnly[index])
{
if (contType == OutContType.X360)
{
//LogDebug("Plugging in X360 Controller for input #" + (index + 1));
activeOutDevType[index] = OutContType.X360;
Xbox360OutDevice tempXbox = new Xbox360OutDevice(vigemTestClient);
//Xbox360OutDevice tempXbox = outputslotMan.AllocateController(OutContType.X360, vigemTestClient)
// as Xbox360OutDevice;
outputDevices[index] = tempXbox;
int devIndex = index;
tempXbox.cont.FeedbackReceived += (sender, args) =>
{
SetDevRumble(device, args.LargeMotor, args.SmallMotor, devIndex);
};
//outputslotMan.DeferredPlugin(tempXbox, index, outputDevices);
tempXbox.Connect();
LogDebug("X360 Controller #" + (index + 1) + " connected");
}
else if (contType == OutContType.DS4)
{
//LogDebug("Plugging in DS4 Controller for input #" + (index + 1));
activeOutDevType[index] = OutContType.DS4;
DS4OutDevice tempDS4 = new DS4OutDevice(vigemTestClient);
//DS4OutDevice tempDS4 = outputslotMan.AllocateController(OutContType.DS4, vigemTestClient)
// as DS4OutDevice;
outputDevices[index] = tempDS4;
int devIndex = index;
tempDS4.cont.FeedbackReceived += (sender, args) =>
{
SetDevRumble(device, args.LargeMotor, args.SmallMotor, devIndex);
};
//outputslotMan.DeferredPlugin(tempDS4, index, outputDevices);
tempDS4.Connect();
LogDebug("DS4 Controller #" + (index + 1) + " connected");
}
}
useDInputOnly[index] = false;
}
public void UnplugOutDev(int index, DS4Device device)
{
if (!useDInputOnly[index])
{
//OutContType contType = Global.OutContType[index];
string tempType = outputDevices[index].GetDeviceType();
OutputDevice dev = outputDevices[index];
outputDevices[index] = null;
activeOutDevType[index] = OutContType.None;
//outputslotMan.DeferredRemoval(dev);
dev.Disconnect();
LogDebug(tempType + " Controller # " + (index + 1) + " unplugged");
useDInputOnly[index] = true;
}
}
private SynchronizationContext uiContext = null;
public bool Start(object tempui, bool showlog = true)
{
@ -436,39 +499,7 @@ namespace DS4Windows
if (!getDInputOnly(i) && device.isSynced())
{
useDInputOnly[i] = false;
OutContType contType = Global.OutContType[i];
if (contType == OutContType.X360)
{
LogDebug("Plugging in X360 Controller #" + (i + 1));
Global.activeOutDevType[i] = OutContType.X360;
Xbox360OutDevice tempXbox = new Xbox360OutDevice(vigemTestClient);
outputDevices[i] = tempXbox;
int devIndex = i;
tempXbox.cont.FeedbackReceived += (sender, args) =>
{
SetDevRumble(device, args.LargeMotor, args.SmallMotor, devIndex);
};
tempXbox.Connect();
LogDebug("X360 Controller #" + (i + 1) + " connected");
}
else if (contType == OutContType.DS4)
{
LogDebug("Plugging in DS4 Controller #" + (i + 1));
Global.activeOutDevType[i] = OutContType.DS4;
DS4OutDevice tempDS4 = new DS4OutDevice(vigemTestClient);
outputDevices[i] = tempDS4;
int devIndex = i;
tempDS4.cont.FeedbackReceived += (sender, args) =>
{
SetDevRumble(device, args.LargeMotor, args.SmallMotor, devIndex);
};
tempDS4.Connect();
LogDebug("DS4 Controller #" + (i + 1) + " connected");
}
PluginOutDev(i, device);
}
else
{
@ -731,39 +762,7 @@ namespace DS4Windows
if (!getDInputOnly(Index) && device.isSynced())
{
useDInputOnly[Index] = false;
OutContType contType = Global.OutContType[Index];
if (contType == OutContType.X360)
{
LogDebug("Plugging in X360 Controller #" + (Index + 1));
Global.activeOutDevType[Index] = OutContType.X360;
Xbox360OutDevice tempXbox = new Xbox360OutDevice(vigemTestClient);
outputDevices[Index] = tempXbox;
int devIndex = Index;
tempXbox.cont.FeedbackReceived += (sender, args) =>
{
SetDevRumble(device, args.LargeMotor, args.SmallMotor, devIndex);
};
tempXbox.Connect();
LogDebug("X360 Controller #" + (Index + 1) + " connected");
}
else if (contType == OutContType.DS4)
{
LogDebug("Plugging in DS4 Controller #" + (Index + 1));
Global.activeOutDevType[Index] = OutContType.DS4;
DS4OutDevice tempDS4 = new DS4OutDevice(vigemTestClient);
outputDevices[Index] = tempDS4;
int devIndex = Index;
tempDS4.cont.FeedbackReceived += (sender, args) =>
{
SetDevRumble(device, args.LargeMotor, args.SmallMotor, devIndex);
};
tempDS4.Connect();
LogDebug("DS4 Controller #" + (Index + 1) + " connected");
}
PluginOutDev(Index, device);
}
else
{
@ -1126,50 +1125,20 @@ namespace DS4Windows
{
if (!useDInputOnly[ind])
{
string tempType = outputDevices[ind].GetDeviceType();
outputDevices[ind].Disconnect();
outputDevices[ind] = null;
useDInputOnly[ind] = true;
LogDebug(tempType + " Controller #" + (ind + 1) + " unplugged");
//string tempType = outputDevices[ind].GetDeviceType();
//outputDevices[ind].Disconnect();
//outputDevices[ind] = null;
//useDInputOnly[ind] = true;
//LogDebug(tempType + " Controller #" + (ind + 1) + " unplugged");
Global.activeOutDevType[ind] = OutContType.None;
UnplugOutDev(ind, device);
}
}
else
{
if (!getDInputOnly(ind))
{
OutContType conType = Global.OutContType[ind];
if (conType == OutContType.X360)
{
LogDebug("Plugging in X360 Controller #" + (ind + 1));
Global.activeOutDevType[ind] = OutContType.X360;
Xbox360OutDevice tempXbox = new Xbox360OutDevice(vigemTestClient);
outputDevices[ind] = tempXbox;
tempXbox.cont.FeedbackReceived += (eventsender, args) =>
{
SetDevRumble(device, args.LargeMotor, args.SmallMotor, ind);
};
tempXbox.Connect();
LogDebug("X360 Controller #" + (ind + 1) + " connected");
}
else if (conType == OutContType.DS4)
{
LogDebug("Plugging in DS4 Controller #" + (ind + 1));
Global.activeOutDevType[ind] = OutContType.DS4;
DS4OutDevice tempDS4 = new DS4OutDevice(vigemTestClient);
outputDevices[ind] = tempDS4;
int devIndex = ind;
tempDS4.cont.FeedbackReceived += (eventsender, args) =>
{
SetDevRumble(device, args.LargeMotor, args.SmallMotor, devIndex);
};
tempDS4.Connect();
LogDebug("DS4 Controller #" + (ind + 1) + " connected");
}
useDInputOnly[ind] = false;
PluginOutDev(ind, device);
}
}
}
@ -1203,12 +1172,13 @@ namespace DS4Windows
CurrentState[ind].Battery = PreviousState[ind].Battery = 0; // Reset for the next connection's initial status change.
if (!useDInputOnly[ind])
{
string tempType = outputDevices[ind].GetDeviceType();
outputDevices[ind].Disconnect();
outputDevices[ind] = null;
UnplugOutDev(ind, device);
//string tempType = outputDevices[ind].GetDeviceType();
//outputDevices[ind].Disconnect();
//outputDevices[ind] = null;
//x360controls[ind].Disconnect();
//x360controls[ind] = null;
LogDebug(tempType + " Controller # " + (ind + 1) + " unplugged");
//LogDebug(tempType + " Controller # " + (ind + 1) + " unplugged");
}
// Use Task to reset device synth state and commit it

View File

@ -3642,53 +3642,22 @@ namespace DS4Windows
OutputDevice tempOutDev = control.outputDevices[device];
if (tempOutDev != null)
{
string tempType = tempOutDev.GetDeviceType();
AppLogger.LogToGui("Unplug " + tempType + " Controller #" + (device + 1), false);
tempOutDev.Disconnect();
//string tempType = tempOutDev.GetDeviceType();
//AppLogger.LogToGui("Unplug " + tempType + " Controller #" + (device + 1), false);
//tempOutDev.Disconnect();
tempOutDev = null;
control.outputDevices[device] = null;
//control.outputDevices[device] = null;
Global.activeOutDevType[device] = OutContType.None;
control.UnplugOutDev(device, tempDev);
}
OutContType tempContType = outputDevType[device];
if (tempContType == OutContType.X360)
{
Global.activeOutDevType[device] = OutContType.X360;
Xbox360OutDevice tempXbox = new Xbox360OutDevice(control.vigemTestClient);
control.outputDevices[device] = tempXbox;
tempXbox.cont.FeedbackReceived += (eventsender, args) =>
{
control.SetDevRumble(tempDev, args.LargeMotor, args.SmallMotor, device);
};
tempXbox.Connect();
AppLogger.LogToGui("X360 Controller #" + (device + 1) + " connected", false);
}
else if (tempContType == OutContType.DS4)
{
Global.activeOutDevType[device] = OutContType.DS4;
DS4OutDevice tempDS4 = new DS4OutDevice(control.vigemTestClient);
control.outputDevices[device] = tempDS4;
tempDS4.cont.FeedbackReceived += (eventsender, args) =>
{
control.SetDevRumble(tempDev, args.LargeMotor, args.SmallMotor, device);
};
tempDS4.Connect();
AppLogger.LogToGui("DS4 Controller #" + (device + 1) + " connected", false);
}
Global.useDInputOnly[device] = false;
control.PluginOutDev(device, tempDev);
}
else if (xinputStatus && !xinputPlug)
{
string tempType = control.outputDevices[device].GetDeviceType();
control.outputDevices[device].Disconnect();
control.outputDevices[device] = null;
Global.useDInputOnly[device] = true;
AppLogger.LogToGui(tempType + " Controller #" + (device + 1) + " unplugged", false);
Global.activeOutDevType[device] = OutContType.None;
control.UnplugOutDev(device, tempDev);
}
tempDev.setRumble(0, 0);