Skip to content

Commit

Permalink
auto connect to hid gamepad at start up
Browse files Browse the repository at this point in the history
  • Loading branch information
duchuule committed Oct 9, 2015
1 parent 26ea125 commit 6dac305
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 55 deletions.
1 change: 1 addition & 0 deletions DirectXPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@
<TextBlock x:Name="txtNotification"
Text="test" Foreground="White"
Margin="12,4,12,4"
TextWrapping="Wrap"

/>
</StackPanel>
Expand Down
63 changes: 55 additions & 8 deletions DirectXPage.xaml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,14 +242,21 @@ DirectXPage::DirectXPage():


//check hid gamepad connection
auto deviceSelector = HidDevice::GetDeviceSelector(0x0001, 0x0005);
create_task(DeviceInformation::FindAllAsync(deviceSelector))

create_task(LoadHidConfig())
.then([loader, this] ()
{
auto deviceSelector = HidDevice::GetDeviceSelector(0x0001, 0x0005);
return DeviceInformation::FindAllAsync(deviceSelector);
}, task_continuation_context::use_current())
.then([loader, this](DeviceInformationCollection^ collection)
{

//VID_045E = microsoft
Vector<DeviceInformation^>^ HIDDeviceList = ref new Vector<DeviceInformation^>();
Vector<String^>^ deviceIDs = ref new Vector<String^>();
Vector<String^>^ deviceNames = ref new Vector<String^>();
int totalHIDDeviceNumber = 0;

for (int i = 0; i < collection->Size; i++)
{
DeviceInformation^ device = collection->GetAt(i);
Expand All @@ -259,18 +266,58 @@ DirectXPage::DirectXPage():
if (deviceid.find(L"VID_045E") != string::npos)
continue;

HIDDeviceList->Append(device);
deviceIDs->Append(device->Name);
totalHIDDeviceNumber++;

//only add known device to the list
if (hidConfigs->HasKey(device->Id))
{
HIDDeviceList->Append(device);
deviceNames->Append(device->Name);
}
}

if (HIDDeviceList->Size > 0)
{
this->ShowNotification(deviceIDs->GetAt(0) + " detected.");
//connect to the first known device

return create_task(EventHandlerForDevice::Current->OpenDeviceAsync(HIDDeviceList->GetAt(0)))
.then([this, HIDDeviceList, deviceNames](task<bool> openDeviceTask)
{
try
{
bool openSuccess = openDeviceTask.get();

auto loader = Windows::ApplicationModel::Resources::ResourceLoader::GetForViewIndependentUse();

if (openSuccess)
{

this->m_main->emulator->RestoreHidConfig();
Platform::String^ notification = EventHandlerForDevice::Current->DeviceInformation->Name + " " + loader->GetString("IsConnectedText");
if (!App::IsPremium)
notification += " " + "To use it, please go to Settings to watch a video.";
this->ShowNotification(notification);
}
else
{
this->ShowNotification( loader->GetString("FailedConnectToText") + " " + HIDDeviceList->GetAt(0)->Name);
}
}
catch (...) {}

}, task_continuation_context::use_current());




}
else
else if (totalHIDDeviceNumber > 0)
{

this->ShowNotification("A new HID gampad is detected. Please go to Settings to configure it.");
return create_task([] {});
}
else
return create_task([] {});

}, task_continuation_context::use_current());

Expand Down
43 changes: 5 additions & 38 deletions Emulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,12 +367,12 @@ namespace VBA10
SetEvent(this->updateEvent);
}

task<bool> EmulatorGame::RestoreHidConfig()
bool EmulatorGame::RestoreHidConfig()
{

return create_task(LoadHidConfig())
.then([this]
{
//return create_task(LoadHidConfig())
// .then([this]
//{
//initialize boolean button map
this->HidInput->booleanControlMapping = ref new Map <int, Platform::String^ >();

Expand Down Expand Up @@ -413,47 +413,14 @@ namespace VBA10



////transfer boolean control
//for (auto bpair : config->booleanControlMapping)
//{
// int bid = bpair->Key;
// String^ function = bpair->Value;

// this->HidInput->booleanControlMapping->Insert(bid, function);
//}

////transfer numeric control
//for (auto ncontrol : this->HidInput->allNumericControls)
//{
// for (auto ncontrol2 : config->allNumericControls)
// {
// if (ncontrol->UsagePage == ncontrol2->UsagePage && ncontrol->UsageId == ncontrol2->UsageId) //find the match
// {
// //transfer the value
// ncontrol->Type = ncontrol2->Type;
// ncontrol->DefaultValue = ncontrol2->DefaultValue;
// ncontrol->MaximumValue = ncontrol2->MaximumValue;
// for (auto npair : ncontrol2->Mapping)
// {
// int nid = npair->Key;
// String^ function = npair->Value;
// ncontrol->Mapping->Insert(nid, function);
// }

// break;
// }


// }
//}

return true;
}
else
{
return false;
}
}, task_continuation_context::use_current());
//}, task_continuation_context::use_current());
}


Expand Down
2 changes: 1 addition & 1 deletion Emulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ namespace VBA10

HIDControllerInput ^HidInput;

task<bool> RestoreHidConfig();
bool RestoreHidConfig();

void EnterButtonEditMode();
bool IsButtonEditMode();
Expand Down
10 changes: 6 additions & 4 deletions HIDGamepadConfig.xaml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,11 @@ void HIDGamepadConfig::OnNavigatedTo(NavigationEventArgs^ /* e */)


//Read stored configuration and display
create_task(emulator->RestoreHidConfig())
.then([this, loader] (bool success)
{
//create_task(emulator->RestoreHidConfig())
// .then([this, loader] (bool success)
//{
bool success = emulator->RestoreHidConfig();

RegisterForInputReportEvents();

EventHandlerForDevice::Current->OnDeviceConnected =
Expand Down Expand Up @@ -162,7 +164,7 @@ void HIDGamepadConfig::OnNavigatedTo(NavigationEventArgs^ /* e */)
}
}
}
}, task_continuation_context::use_current());
//}, task_continuation_context::use_current());



Expand Down
9 changes: 5 additions & 4 deletions SettingsPage.xaml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,9 +342,10 @@ void SettingsPage::ConnectBtn_Click(Platform::Object^ sender, Windows::UI::Xaml:
{
this->txtHIDGamepad->Text = EventHandlerForDevice::Current->DeviceInformation->Name + " " + loader->GetString("IsConnectedText");

create_task(emulator->RestoreHidConfig())
.then([this, loader](bool restoreSuccess)
{
//create_task(emulator->RestoreHidConfig())
// .then([this, loader](bool restoreSuccess)
//{
bool restoreSuccess = emulator->RestoreHidConfig();
if (!restoreSuccess)
{
//open dialog
Expand All @@ -362,7 +363,7 @@ void SettingsPage::ConnectBtn_Click(Platform::Object^ sender, Windows::UI::Xaml:
dialog->Commands->Append(confirm);
dialog->ShowAsync();
}
});
//});
}
else
{
Expand Down

0 comments on commit 6dac305

Please sign in to comment.