Skip to content

Commit

Permalink
fix for crash when trying to reconnect a off device when resuming.
Browse files Browse the repository at this point in the history
  • Loading branch information
duchuule committed Aug 20, 2015
1 parent a0dc0d1 commit 67885e0
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 7 deletions.
2 changes: 1 addition & 1 deletion DirectXPage.xaml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ void DirectXPage::TogglePaneButton_UnChecked(Platform::Object^ sender, Windows::

void DirectXPage::TogglePaneButton_Checked(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
m_main->emulator->HidInput->UnregisterFromInputReportEvent();
m_main->emulator->HidInput->StopListening();

//pause emulator
m_main->emulator->Pause();
Expand Down
4 changes: 3 additions & 1 deletion Emulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,10 @@ namespace VBA10
if (xboxElapsed < 3600.0f)
{
this->p1Controller->Update();
this->HidInput->Update();
this->HidInput->Update(true);
}
else
this->HidInput->Update(false);
#endif
this->virtualInput->Update();

Expand Down
10 changes: 8 additions & 2 deletions EventHandlerForDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,14 @@ IAsyncOperation<bool>^ EventHandlerForDevice::OpenDeviceAsync(Enumeration::Devic
//String^ notificationMessage = nullptr;

// This may throw an exception or return null if we could not open the device
device = deviceTask.get();

try
{
device = deviceTask.get();
}
catch (Platform::Exception^)
{
device = nullptr;
}
// Device could have been blocked by user or the device has already been opened by another app.
if (device != nullptr)
{
Expand Down
12 changes: 10 additions & 2 deletions HIDControllerInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ namespace VBA10
// a stale token. Ideally, one should remove the event token (e.g. assign to null) upon the device removal to avoid using it again.
registeredDevice = EventHandlerForDevice::Current->Device;

if (registeredDevice == nullptr)
return;

// Save event registration token so we can unregisted for events
inputReportEventToken = registeredDevice->InputReportReceived +=
ref new TypedEventHandler<HidDevice^, HidInputReportReceivedEventArgs^>(this, &HIDControllerInput::OnInputReportEvent);
Expand All @@ -68,6 +71,9 @@ namespace VBA10

void HIDControllerInput::UnregisterFromInputReportEvent(void)
{
if (registeredDevice == nullptr)
return;

if (isRegisteredForInputReportEvents)
{
// Don't unregister event token if the device was removed and reconnected because registration token is no longer valid
Expand Down Expand Up @@ -109,6 +115,8 @@ namespace VBA10
this->state.UpPressed = false;
this->state.DownPressed = false;

if (this->shouldUpdate == false)
return;

//check buttons
auto bcontrols = inputReport->ActivatedBooleanControls;
Expand Down Expand Up @@ -163,9 +171,9 @@ namespace VBA10

}

void HIDControllerInput::Update()
void HIDControllerInput::Update(bool shouldUpdate)
{

this->shouldUpdate = shouldUpdate;
}


Expand Down
3 changes: 2 additions & 1 deletion HIDControllerInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace VBA10

internal:
const ControllerState* GetControllerState(void);
void Update();
void Update(bool shouldUpdate);

void StartListening(); //start listening to report event
void StopListening();
Expand All @@ -47,6 +47,7 @@ namespace VBA10

private:
ControllerState state;
bool shouldUpdate;

Windows::Foundation::EventRegistrationToken inputReportEventToken;
bool isRegisteredForInputReportEvents;
Expand Down

0 comments on commit 67885e0

Please sign in to comment.