Skip to content

Commit

Permalink
can now load config
Browse files Browse the repository at this point in the history
  • Loading branch information
duchuule committed Aug 21, 2015
1 parent 25703e0 commit 1ac1d19
Show file tree
Hide file tree
Showing 8 changed files with 315 additions and 28 deletions.
134 changes: 116 additions & 18 deletions EmulatorFileHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ namespace VBA10
Windows::Foundation::Collections::IVector<CheatData ^> ^ROMCheats = nullptr;
bool ShouldApplyNewCheats = false;

int firstIndexOf(string &s, char c)
template <typename charT>
int firstIndexOf(std::basic_string<charT> &s, charT c)
{
for(int i = 0; i < s.size(); ++i)
{
Expand All @@ -85,24 +86,10 @@ namespace VBA10
return -1;
}

vector<string> &split(const string &s, char delim, vector<string> &elems)
{
stringstream ss(s);
string item;
while(getline(ss, item, delim))
{
elems.push_back(item);
}
return elems;
}

vector<string> split(const string &s, char delim)
{
vector<string> elems;
return split(s, delim, elems);
}

bool stringWhitespace(const string &s)

template <typename charT>
bool stringWhitespace(const basic_string<charT> &s)
{
for (int i = 0; i < s.size(); i++)
{
Expand All @@ -114,6 +101,8 @@ namespace VBA10
return true;
}



Platform::Array<unsigned char> ^GetSnapshotBuffer(unsigned char *backbuffer, size_t pitch, int imageWidth, int imageHeight)
{
Platform::Array<unsigned char> ^buffer = ref new Platform::Array<unsigned char>(imageWidth * imageHeight * 4);
Expand Down Expand Up @@ -2941,4 +2930,113 @@ namespace VBA10
});
}


task<void> LoadHidConfig()
{
auto reader = make_shared<DataReader ^>();

return create_task(ApplicationData::Current->LocalFolder->GetFileAsync("hid-configs.ini"))
.then([reader](StorageFile ^file)
{
return file->OpenReadAsync();
}).then([reader](IRandomAccessStreamWithContentType ^stream)
{
*reader = ref new DataReader(stream);
return create_task((*reader)->LoadAsync((unsigned int)stream->Size));

}).then([reader](unsigned int bytesRead)
{
String ^text = nullptr;
Map<Platform::String ^, ROMConfig> ^map = ref new Map<Platform::String ^, ROMConfig>();

text = (*reader)->ReadString(bytesRead);

if (text == nullptr)
return;

wstring str(text->Begin(), text->End());
vector<wstring> lines = split(str, L'\n');

for (vector<wstring>::const_iterator i = lines.begin(); i != lines.end(); ++i)
{
wstring line = *i;
int startBraces = firstIndexOf(line, L'[');
if (startBraces == -1)
{
continue;
}
int endBraces = firstIndexOf(line, L']');
if (endBraces == -1)
{
continue;
}

HIDControllerInput^ config = ref new HIDControllerInput();

wstring deviceId = line.substr(startBraces + 1, endBraces - startBraces - 1);
Platform::String^ pDeviceId = ref new Platform::String(deviceId.c_str());

for (++i; i != lines.end() && !stringWhitespace(line = *i); ++i) //look for button mapping until find a blank line
{
int colonIndex = firstIndexOf(line, L':');
if (colonIndex == -1)
{
continue;
}
if (colonIndex + 1 >= line.size())
{
continue;
}
wstring controlName = line.substr(0, colonIndex);
wstring controlValue = line.substr(colonIndex + 1);

vector<wstring> nameParts = split(controlName, L'_');


if (nameParts.at(0) == L"B") //this is a button (boolean control)
{
int buttonId = stoi(nameParts.at(1));
Platform::String^ function = ref new Platform::String(controlValue.c_str());
config->booleanControlMapping->Insert(buttonId, function);
}
else if (nameParts.at(0) == L"N") //numeric control (axis, hat)
{
//usagepage and usageid
int usagePage = stoi(nameParts.at(1));
int usageId = stoi(nameParts.at(2));
HidNumericControlExt^ control = ref new HidNumericControlExt(usagePage, usageId);

//get the control info
vector<wstring> valueParts = split(controlValue, L'_');
control->Type = stoi(valueParts.at(0));
control->DefaultValue = stoi(valueParts.at(1));
control->MaximumValue = stoi(valueParts.at(2));

int Nmapping = stoi(valueParts.at(3));
int index = 4;
for (int j = 0; j < Nmapping; j++)
{
int function_id = stoi(valueParts.at(index));
Platform::String^ function = ref new Platform::String(valueParts.at(index+1).c_str());
control->Mapping->Insert(function_id, function);

index += 2;
}

//add to list
config->allNumericControls->Append(control);
}

}

hidConfigs->Insert(pDeviceId, config);


if (i == lines.end())
{
break;
}
}
});
}
}
2 changes: 1 addition & 1 deletion EmulatorFileHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ namespace VBA10
int GetSavestateSlot(void);

task<void> SaveHidConfig();

task<void> LoadHidConfig();

}
1 change: 0 additions & 1 deletion HIDControllerInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ namespace VBA10
{
HIDControllerInput::HIDControllerInput(): isRegisteredForInputReportEvents(false)
{
InitializeCriticalSectionEx(&inputSync, NULL, NULL);

//initialize boolean button map
this->booleanControlMapping = ref new Map <int, Platform::String^>();
Expand Down
3 changes: 1 addition & 2 deletions HIDControllerInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ namespace VBA10
void UnregisterFromInputReportEvent(void);

//Windows::Devices::HumanInterfaceDevice::HidDevice ^Device;

Platform::Collections::Vector < HidNumericControlExt^>^ allNumericControls;
Platform::Collections::Map <int, Platform::String^>^ booleanControlMapping;

Expand Down Expand Up @@ -66,7 +65,7 @@ namespace VBA10



CRITICAL_SECTION inputSync;


void GetMapping(Platform::String^ tag, bool* left, bool* right, bool* up, bool* down, bool* a, bool* b, bool* l, bool* r, bool* select, bool* start, bool* turbo);

Expand Down
143 changes: 140 additions & 3 deletions HIDGamepadConfig.xaml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ void HIDGamepadConfig::OnNavigatedTo(NavigationEventArgs^ /* e */)
//this->gridMain->Visibility = Windows::UI::Xaml::Visibility::Visible;
this->txtNotification->Text = "First press Start button.";


//initialize boolean button map
emulator->HidInput->booleanControlMapping = ref new Map <int, Platform::String^ >();

Expand Down Expand Up @@ -92,6 +93,92 @@ void HIDGamepadConfig::OnNavigatedTo(NavigationEventArgs^ /* e */)
}
}

//Read stored configuration and transfer config to current one
create_task(LoadHidConfig())
.then([this] {
if (hidConfigs->HasKey(EventHandlerForDevice::Current->DeviceInformation->Id))
{
auto config = hidConfigs->Lookup(EventHandlerForDevice::Current->DeviceInformation->Id);

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

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

//transfer numeric control
for (auto ncontrol : emulator->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;
}


}
}


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

TextBox^ txtbox = FindTextbox(function);

if (txtbox != nullptr)
txtbox->Text = "Button " + bid.ToString();
}

//display numeric control
for (auto ncontrol : emulator->HidInput->allNumericControls)
{
if (ncontrol->UsagePage == 0x01 && ncontrol->UsageId == 0x39) //text for hat switch has already been printed
continue;

for (auto npair : ncontrol->Mapping)
{
int nid = npair->Key;
String^ function = npair->Value;

TextBox^ txtbox = FindTextbox(function);

if (txtbox != nullptr)
{
if (ncontrol->Type == 0)
txtbox->Text = "Axis (" + ncontrol->UsagePage.ToString() + "," + ncontrol->UsageId.ToString() + ")";
else if (ncontrol->Type == 1)
{
if (nid == -1)
txtbox->Text = "Axis (" + ncontrol->UsagePage.ToString() + "," + ncontrol->UsageId.ToString() + ") -";
else if (nid == 1)
txtbox->Text = "Axis (" + ncontrol->UsagePage.ToString() + "," + ncontrol->UsageId.ToString() + ") +";
}

}
}
}
}
}, task_continuation_context::use_current());


RegisterForInputReportEvents();

EventHandlerForDevice::Current->OnDeviceConnected =
Expand All @@ -104,6 +191,56 @@ void HIDGamepadConfig::OnNavigatedTo(NavigationEventArgs^ /* e */)

}

TextBox^ HIDGamepadConfig::FindTextbox(Platform::String^ tag)
{
if (tag == "Left1")
return this->txtLeft1;
else if (tag == "Left2")
return this->txtLeft2;
else if (tag == "Right1")
return this->txtRight1;
else if (tag == "Right2")
return this->txtRight2;
else if (tag == "Up1")
return this->txtUp1;
else if (tag == "Up2")
return this->txtUp2;
else if (tag == "Down1")
return this->txtDown1;
else if (tag == "Down2")
return this->txtDown2;
else if (tag == "A1")
return this->txtA1;
else if (tag == "A2")
return this->txtA2;
else if (tag == "B1")
return this->txtB1;
else if (tag == "B2")
return this->txtB2;
else if (tag == "L1")
return this->txtL1;
else if (tag == "L2")
return this->txtL2;
else if (tag == "R1")
return this->txtR1;
else if (tag == "R2")
return this->txtR2;
else if (tag == "Select1")
return this->txtSelect1;
else if (tag == "Select2")
return this->txtSelect2;
else if (tag == "Start1")
return this->txtStart1;
else if (tag == "Start2")
return this->txtStart2;
else if (tag == "Turbo1")
return this->txtTurbo1;
else if (tag == "Turbo2")
return this->txtTurbo2;
else
return nullptr;
}



void HIDGamepadConfig::OnInputReportEvent(HidDevice^ sender, HidInputReportReceivedEventArgs^ eventArgs)
Expand Down Expand Up @@ -278,7 +415,7 @@ void HIDGamepadConfig::OnInputReportEvent(HidDevice^ sender, HidInputReportRecei

this->Dispatcher->RunAsync(CoreDispatcherPriority::Normal, ref new DispatchedHandler([this, control, controlExt]()
{
focusTextbox->Text = "Button (" + control->UsagePage.ToString() + "," + control->UsageId.ToString() + ")";
focusTextbox->Text = "Axis (" + control->UsagePage.ToString() + "," + control->UsageId.ToString() + ")";


//remove the tag if it has been asiggned to a different button value
Expand All @@ -305,7 +442,7 @@ void HIDGamepadConfig::OnInputReportEvent(HidDevice^ sender, HidInputReportRecei

this->Dispatcher->RunAsync(CoreDispatcherPriority::Normal, ref new DispatchedHandler([this, control, controlExt]()
{
focusTextbox->Text = "Button (" + control->UsagePage.ToString() + "," + control->UsageId.ToString() + ") -";
focusTextbox->Text = "Axis (" + control->UsagePage.ToString() + "," + control->UsageId.ToString() + ") -";

//remove the tag if it has been asiggned to a different button value
for (auto pair : controlExt->Mapping)
Expand All @@ -331,7 +468,7 @@ void HIDGamepadConfig::OnInputReportEvent(HidDevice^ sender, HidInputReportRecei

this->Dispatcher->RunAsync(CoreDispatcherPriority::Normal, ref new DispatchedHandler([this, control, controlExt]()
{
focusTextbox->Text = "Button (" + control->UsagePage.ToString() + "," + control->UsageId.ToString() + ") +";
focusTextbox->Text = "Axis (" + control->UsagePage.ToString() + "," + control->UsageId.ToString() + ") +";

//remove the tag if it has been asiggned to a different button value
for (auto pair : controlExt->Mapping)
Expand Down
2 changes: 2 additions & 0 deletions HIDGamepadConfig.xaml.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,7 @@ namespace VBA10

void OnDeviceConnected(EventHandlerForDevice^ sender, OnDeviceConnectedEventArgs^ onDeviceConnectedEventArgs);
void OnDeviceClosing(EventHandlerForDevice^ sender, Windows::Devices::Enumeration::DeviceInformation^ deviceInformation);

Windows::UI::Xaml::Controls::TextBox^ FindTextbox(Platform::String^ tag);
};
}
Loading

0 comments on commit 1ac1d19

Please sign in to comment.