Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: TOML Config support #31

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3,668 changes: 3,668 additions & 0 deletions Include/cpptoml.h

Large diffs are not rendered by default.

82 changes: 58 additions & 24 deletions XJoy/XJoy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <csignal>
#include <tuple>
#include <unordered_map>
#include "Yaml.hpp"
#include "cpptoml.h"


#define u8 uint8_t
Expand All @@ -21,6 +21,9 @@ const int XBOX_ANALOG_MAX = 32767;
const int XBOX_ANALOG_DIAG_MAX = round(XBOX_ANALOG_MAX * 0.5 * sqrt(2.0));
const int XBOX_ANALOG_DIAG_MIN = round(XBOX_ANALOG_MIN * 0.5 * sqrt(2.0));

// Config file
std::shared_ptr<cpptoml::table> config;

#define DATA_BUFFER_SIZE 49
#define OUT_BUFFER_SIZE 49
u8 data[DATA_BUFFER_SIZE];
Expand Down Expand Up @@ -244,6 +247,39 @@ std::string vigem_error_to_string(VIGEM_ERROR err) {
}
}

void load_config() {
try {
config = cpptoml::parse_file("config.toml");
std::cout << "=> Successfully loaded configuration file." << std::endl;
}
catch (cpptoml::parse_exception) {
config = NULL;
std::cerr << "=> There was an error loading the configuration file, default values will be used." << std::endl;
}
}

/// <summary>
/// Gets the mapping for a specific key value
/// </summary>
/// <param name="key">The key in the config file</param>
/// <param name="fallback">The mapping to use should there be a failure in loading the config</param>
/// <returns>The mapped value, in the format of _XUSB_BUTTON enum</returns>
_XUSB_BUTTON get_mapping_from_config(std::string key, _XUSB_BUTTON fallback) {
// Return the fallback value if the config was loaded incorrectly
if (config == NULL) {
return fallback;
}

// Restrict the keys to the mapping section of the config
std::string str_value = *config->get_qualified_as<std::string>("mappings." + key);
try {
return string_to_xbox_button(str_value);
}
catch (std::string param){
std::cerr << "=> Invalid button provided in config: " << param << std::endl;
return fallback;
}
}

void subcomm(hid_device* joycon, u8* in, u8 len, u8 comm, u8 get_response, u8 is_left)
{
Expand Down Expand Up @@ -397,9 +433,6 @@ void initialize_right_joycon() {
exit(1);
}
setup_joycon(right_joycon, 0x1, 0);



}

void initialize_xbox() {
Expand Down Expand Up @@ -486,22 +519,22 @@ void process_button(JOYCON_REGION region, JOYCON_BUTTON button) {
case LEFT_DPAD:
switch(button) {
case L_DPAD_UP:
left_buttons = left_buttons | XUSB_GAMEPAD_DPAD_UP;
left_buttons = left_buttons | get_mapping_from_config("L_DPAD_UP", XUSB_GAMEPAD_DPAD_UP);
break;
case L_DPAD_DOWN:
left_buttons = left_buttons | XUSB_GAMEPAD_DPAD_DOWN;
left_buttons = left_buttons | get_mapping_from_config("L_DPAD_DOWN", XUSB_GAMEPAD_DPAD_DOWN);
break;
case L_DPAD_LEFT:
left_buttons = left_buttons | XUSB_GAMEPAD_DPAD_LEFT;
left_buttons = left_buttons | get_mapping_from_config("L_DPAD_LEFT", XUSB_GAMEPAD_DPAD_LEFT);
break;
case L_DPAD_RIGHT:
left_buttons = left_buttons | XUSB_GAMEPAD_DPAD_RIGHT;
left_buttons = left_buttons | get_mapping_from_config("L_DPAD_RIGHT", XUSB_GAMEPAD_DPAD_RIGHT);
break;
case L_DPAD_SL:
left_buttons = left_buttons | XUSB_GAMEPAD_X;
left_buttons = left_buttons | get_mapping_from_config("L_DPAD_SL", XUSB_GAMEPAD_X);
break;
case L_DPAD_SR:
left_buttons = left_buttons | XUSB_GAMEPAD_A;
left_buttons = left_buttons | get_mapping_from_config("L_DPAD_SR", XUSB_GAMEPAD_A);
break;
}
break;
Expand Down Expand Up @@ -548,19 +581,19 @@ void process_button(JOYCON_REGION region, JOYCON_BUTTON button) {
case LEFT_AUX:
switch(button) {
case L_SHOULDER:
left_buttons = left_buttons | XUSB_GAMEPAD_LEFT_SHOULDER;
left_buttons = left_buttons | get_mapping_from_config("L_SHOULDER", XUSB_GAMEPAD_LEFT_SHOULDER);
break;
case L_TRIGGER:
report.bLeftTrigger = 255;
break;
case L_CAPTURE:
left_buttons = left_buttons | XUSB_GAMEPAD_BACK;
left_buttons = left_buttons | get_mapping_from_config("L_CAPTURE", XUSB_GAMEPAD_BACK);
break;
case L_MINUS:
left_buttons = left_buttons | XUSB_GAMEPAD_BACK;
left_buttons = left_buttons | get_mapping_from_config("L_MINUS", XUSB_GAMEPAD_BACK);
break;
case L_STICK:
left_buttons = left_buttons | XUSB_GAMEPAD_LEFT_THUMB;
left_buttons = left_buttons | get_mapping_from_config("L_STICK", XUSB_GAMEPAD_LEFT_THUMB);
break;
}
break;
Expand Down Expand Up @@ -607,41 +640,41 @@ void process_button(JOYCON_REGION region, JOYCON_BUTTON button) {
case RIGHT_AUX:
switch(button) {
case R_SHOULDER:
right_buttons = right_buttons | XUSB_GAMEPAD_RIGHT_SHOULDER;
right_buttons = right_buttons | get_mapping_from_config("R_SHOULDER", XUSB_GAMEPAD_RIGHT_SHOULDER);
break;
case R_TRIGGER:
report.bRightTrigger = 255;
break;
case R_HOME:
right_buttons = right_buttons | XUSB_GAMEPAD_START;
right_buttons = right_buttons | get_mapping_from_config("R_HOME", XUSB_GAMEPAD_START);
break;
case R_PLUS:
right_buttons = right_buttons | XUSB_GAMEPAD_START;
right_buttons = right_buttons | get_mapping_from_config("R_PLUS", XUSB_GAMEPAD_START);
break;
case R_STICK:
right_buttons = right_buttons | XUSB_GAMEPAD_RIGHT_THUMB;
right_buttons = right_buttons | get_mapping_from_config("R_STICK", XUSB_GAMEPAD_RIGHT_THUMB);
break;
}
break;
case RIGHT_BUTTONS:
switch(button) {
case R_BUT_A:
right_buttons = right_buttons | XUSB_GAMEPAD_B;
right_buttons = right_buttons | get_mapping_from_config("R_BUT_A", XUSB_GAMEPAD_B);
break;
case R_BUT_B:
right_buttons = right_buttons | XUSB_GAMEPAD_A;
right_buttons = right_buttons | get_mapping_from_config("R_BUT_B", XUSB_GAMEPAD_A);
break;
case R_BUT_X:
right_buttons = right_buttons | XUSB_GAMEPAD_Y;
right_buttons = right_buttons | get_mapping_from_config("R_BUT_X", XUSB_GAMEPAD_Y);
break;
case R_BUT_Y:
right_buttons = right_buttons | XUSB_GAMEPAD_X;
right_buttons = right_buttons | get_mapping_from_config("R_BUT_Y", XUSB_GAMEPAD_X);
break;
case R_BUT_SL:
right_buttons = right_buttons | XUSB_GAMEPAD_B;
right_buttons = right_buttons | get_mapping_from_config("R_BUT_SL", XUSB_GAMEPAD_B);
break;
case R_BUT_SR:
right_buttons = right_buttons | XUSB_GAMEPAD_Y;
right_buttons = right_buttons | get_mapping_from_config("R_BUT_SR", XUSB_GAMEPAD_Y);
break;
}
break;
Expand Down Expand Up @@ -797,6 +830,7 @@ int main() {
signal(SIGINT, exit_handler);
std::cout << "XJoy v0.1.8" << std::endl << std::endl;

load_config();
initialize_xbox();
hid_init();

Expand Down
6 changes: 4 additions & 2 deletions XJoy/XJoy.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -89,27 +89,29 @@
</ResourceCompile>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="..\Include\cpptoml.h" />
<ClInclude Include="resource.h" />
<ClInclude Include="resource1.h" />
<ClInclude Include="resource2.h" />
<ClInclude Include="stdafx.h" />
<ClInclude Include="targetver.h" />
<ClInclude Include="Yaml.hpp" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="stdafx.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
</ClCompile>
<ClCompile Include="XJoy.cpp" />
<ClCompile Include="Yaml.cpp" />
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="XJoy.rc" />
</ItemGroup>
<ItemGroup>
<Image Include="XJoy.ico" />
</ItemGroup>
<ItemGroup>
<None Include="cpp.hint" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
Expand Down
8 changes: 4 additions & 4 deletions XJoy/XJoy.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<ClInclude Include="resource2.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Yaml.hpp">
<ClInclude Include="..\Include\cpptoml.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
Expand All @@ -37,14 +37,14 @@
<ClCompile Include="XJoy.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Yaml.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="XJoy.rc" />
</ItemGroup>
<ItemGroup>
<Image Include="XJoy.ico" />
</ItemGroup>
<ItemGroup>
<None Include="cpp.hint" />
</ItemGroup>
</Project>
Loading