summaryrefslogtreecommitdiff
path: root/converter
diff options
context:
space:
mode:
authorJacob Palecki <[email protected]>2020-09-29 10:08:13 -0700
committerJacob Palecki <[email protected]>2020-09-29 10:08:13 -0700
commit49675a0fa01a1d7f04a7a2e31ec1872125a0cf37 (patch)
treeac4a331892ff777de67deb8b8be8905bb017434e /converter
parentUse 0 for cap\offset when not visible (diff)
parentset invisible args to default values before write (diff)
downloadrawaccel-49675a0fa01a1d7f04a7a2e31ec1872125a0cf37.tar.xz
rawaccel-49675a0fa01a1d7f04a7a2e31ec1872125a0cf37.zip
merge + small fixes
Diffstat (limited to 'converter')
-rw-r--r--converter/converter.cpp222
-rw-r--r--converter/converter.vcxproj109
-rw-r--r--converter/converter.vcxproj.filters22
3 files changed, 353 insertions, 0 deletions
diff --git a/converter/converter.cpp b/converter/converter.cpp
new file mode 100644
index 0000000..3aff02c
--- /dev/null
+++ b/converter/converter.cpp
@@ -0,0 +1,222 @@
+#include <array>
+#include <charconv>
+#include <filesystem>
+#include <fstream>
+#include <iostream>
+#include <optional>
+#include <string>
+
+#include <rawaccel-settings.h>
+
+using namespace System;
+using namespace System::Runtime::InteropServices;
+using namespace Newtonsoft::Json;
+
+namespace ra = rawaccel;
+namespace fs = std::filesystem;
+
+const wchar_t* IA_SETTINGS_NAME = L"settings.txt";
+const wchar_t* IA_PROFILE_EXT = L".profile";
+
+enum IA_MODES_ENUM { IA_QL, IA_NAT, IA_LOG };
+
+constexpr std::array<std::string_view, 3> IA_MODES = {
+ "QuakeLive", "Natural", "Logarithmic"
+};
+
+// trim from start (in place)
+static inline void ltrim(std::string& s) {
+ s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) {
+ return !std::isspace(ch);
+ }));
+}
+
+// trim from end (in place)
+static inline void rtrim(std::string& s) {
+ s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) {
+ return !std::isspace(ch);
+ }).base(), s.end());
+}
+
+// trim from both ends (in place)
+static inline void trim(std::string& s) {
+ ltrim(s);
+ rtrim(s);
+}
+
+bool yes() {
+ bool b;
+ while (wchar_t c = _getwch()) {
+ if (c == 'y') {
+ b = true;
+ break;
+ }
+ else if (c == 'n') {
+ b = false;
+ break;
+ }
+ }
+ return b;
+}
+
+bool try_convert(const fs::path& fp) {
+ std::vector<std::pair<std::string, double>> kv_pairs;
+
+ {
+ std::ifstream ifs(fp);
+ std::string line;
+
+ while (std::getline(ifs, line)) {
+ if (line.empty()) continue;
+
+ auto delim_pos = line.find('=');
+ if (delim_pos == std::string::npos) continue;
+
+ std::string key(line.substr(0, delim_pos));
+ trim(key);
+
+ auto val_pos = line.find_first_not_of(" \t", delim_pos + 1);
+ if (val_pos == std::string::npos) continue;
+
+ double val = 0;
+
+ auto [p, ec] = std::from_chars(&line[val_pos], &line[0] + line.size(), val);
+
+ if (ec == std::errc()) {
+ kv_pairs.emplace_back(key, val);
+ }
+ else if (key == "AccelMode") {
+ std::string mode_val = line.substr(val_pos);
+ rtrim(mode_val);
+ auto it = std::find(IA_MODES.begin(), IA_MODES.end(), mode_val);
+ if (it != IA_MODES.end()) {
+ val = static_cast<double>(std::distance(IA_MODES.begin(), it));
+ kv_pairs.emplace_back(key, val);
+ }
+ }
+ }
+ }
+
+ auto get = [&](auto... keys) -> std::optional<double> {
+ auto it = std::find_if(kv_pairs.begin(), kv_pairs.end(), [=](auto&& p) {
+ return ((p.first == keys) || ...);
+ });
+ if (it == kv_pairs.end()) return std::nullopt;
+ return it->second;
+ };
+
+ ra::settings ra_settings;
+ ra::accel_args& args = ra_settings.argsv.x;
+
+ auto opt_mode = get("AccelMode");
+ if (!opt_mode) opt_mode = 0.0;
+
+ double accel = std::max(get("Acceleration").value_or(0), 0.0);
+ double sens = get("Sensitivity").value_or(1);
+ double cap = get("SensitivityCap").value_or(0);
+
+ std::cout << "We recommend trying out our new cap and offset styles.\n"
+ "Use new cap and offset? (y|n)\n";
+
+ args.legacy_offset = !yes();
+ args.offset = get("Offset").value_or(0);
+ args.accel = accel / sens * get("Pre-ScaleX").value_or(1);
+ args.limit = 1 + std::abs(cap - sens);
+
+ ra_settings.degrees_rotation = get("Angle", "AngleAdjustment").value_or(0);
+ ra_settings.sens = {
+ get("Post-ScaleX").value_or(1),
+ get("Post-ScaleY").value_or(1)
+ };
+
+ switch (static_cast<IA_MODES_ENUM>(opt_mode.value())) {
+ case IA_QL: {
+ auto opt_pow = get("Power");
+ if (!opt_pow || opt_pow.value() <= 1) return false;
+ args.exponent = opt_pow.value();
+
+ if (args.legacy_offset || cap <= 1) {
+ args.scale_cap = cap;
+ }
+ else {
+ double b = (cap - 1) / args.exponent;
+ double e = 1 / (args.exponent - 1);
+ args.gain_cap = args.offset + (1 / accel) * std::pow(b, e);
+ }
+ ra_settings.modes.x = ra::accel_mode::classic;
+ break;
+ }
+ case IA_NAT: {
+ std::cout << "Raw Accel offers a new mode that you might like more than Natural.\n"
+ "Wanna try it out now? (y|n)\n";
+ ra_settings.modes.x = yes() ? ra::accel_mode::naturalgain : ra::accel_mode::natural;
+ break;
+ }
+ case IA_LOG: {
+ std::cout << "Logarithmic accel mode is not supported.\n";
+ return true;
+ }
+ default: return false;
+ }
+
+ DriverSettings^ new_settings = Marshal::PtrToStructure<DriverSettings^>((IntPtr)&ra_settings);
+ auto errors = DriverInterop::GetSettingsErrors(new_settings);
+
+ if (errors->Empty()) {
+ Console::Write("Sending to driver... ");
+ DriverInterop::Write(new_settings);
+ Console::WriteLine("done");
+
+ Console::Write("Generating settings.json... ");
+ auto json = JsonConvert::SerializeObject(new_settings, Formatting::Indented);
+ System::IO::File::WriteAllText("settings.json", json);
+ Console::WriteLine("done");
+ }
+ else {
+ Console::WriteLine("Bad settings:\n" + errors->x->ToArray()[0]);
+ }
+
+ return true;
+}
+
+int main()
+{
+ std::optional<fs::path> opt_path;
+
+ if (fs::exists(IA_SETTINGS_NAME)) {
+ opt_path = IA_SETTINGS_NAME;
+ }
+ else {
+ for (auto&& entry : fs::directory_iterator(".")) {
+ if (fs::is_regular_file(entry) &&
+ entry.path().extension() == IA_PROFILE_EXT) {
+ opt_path = entry;
+ break;
+ }
+ }
+ }
+
+ if (opt_path) {
+ std::cout << "Found " << opt_path->filename() <<
+ "\n\nConvert and send settings generated from " <<
+ opt_path->filename() << "? (y|n)\n";
+ if (!yes()) return 0;
+ try {
+ if (!try_convert(opt_path.value()))
+ std::cout << "Unable to convert settings.\n";
+ }
+ catch (DriverNotInstalledException^) {
+ std::cout << "\nDriver is not installed\n";
+ }
+ catch (const std::exception& e) {
+ std::cout << "Error: \n" << e.what() << '\n';
+ }
+ }
+ else {
+ std::cout << "Drop your InterAccel settings/profile into this directory.\n"
+ "Then run this program to generate the equivalent Raw Accel settings.\n";
+ }
+
+ std::cout << "Press any key to close this window . . .\n";
+ _getwch();
+}
diff --git a/converter/converter.vcxproj b/converter/converter.vcxproj
new file mode 100644
index 0000000..3cbe7bf
--- /dev/null
+++ b/converter/converter.vcxproj
@@ -0,0 +1,109 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <ItemGroup Label="ProjectConfigurations">
+ <ProjectConfiguration Include="Debug|x64">
+ <Configuration>Debug</Configuration>
+ <Platform>x64</Platform>
+ </ProjectConfiguration>
+ <ProjectConfiguration Include="Release|x64">
+ <Configuration>Release</Configuration>
+ <Platform>x64</Platform>
+ </ProjectConfiguration>
+ </ItemGroup>
+ <PropertyGroup Label="Globals">
+ <VCProjectVersion>16.0</VCProjectVersion>
+ <Keyword>Win32Proj</Keyword>
+ <ProjectGuid>{4c421992-9a27-4860-a40c-add76fbace55}</ProjectGuid>
+ <RootNamespace>converter</RootNamespace>
+ <WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
+ <TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
+ </PropertyGroup>
+ <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
+ <ConfigurationType>Application</ConfigurationType>
+ <UseDebugLibraries>true</UseDebugLibraries>
+ <PlatformToolset>v142</PlatformToolset>
+ <CharacterSet>Unicode</CharacterSet>
+ <CLRSupport>true</CLRSupport>
+ </PropertyGroup>
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
+ <ConfigurationType>Application</ConfigurationType>
+ <UseDebugLibraries>false</UseDebugLibraries>
+ <PlatformToolset>v142</PlatformToolset>
+ <WholeProgramOptimization>false</WholeProgramOptimization>
+ <CharacterSet>Unicode</CharacterSet>
+ <CLRSupport>true</CLRSupport>
+ </PropertyGroup>
+ <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
+ <ImportGroup Label="ExtensionSettings">
+ </ImportGroup>
+ <ImportGroup Label="Shared">
+ <Import Project="..\common\common.vcxitems" Label="Shared" />
+ </ImportGroup>
+ <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
+ <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+ </ImportGroup>
+ <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
+ <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+ </ImportGroup>
+ <PropertyGroup Label="UserMacros" />
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
+ <LinkIncremental>false</LinkIncremental>
+ </PropertyGroup>
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
+ <LinkIncremental>false</LinkIncremental>
+ </PropertyGroup>
+ <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
+ <ClCompile>
+ <WarningLevel>Level3</WarningLevel>
+ <SDLCheck>true</SDLCheck>
+ <PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+ <ConformanceMode>true</ConformanceMode>
+ <LanguageStandard>stdcpp17</LanguageStandard>
+ <AdditionalOptions>/Zc:twoPhase- %(AdditionalOptions)</AdditionalOptions>
+ <RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
+ </ClCompile>
+ <Link>
+ <SubSystem>Console</SubSystem>
+ <GenerateDebugInformation>true</GenerateDebugInformation>
+ </Link>
+ </ItemDefinitionGroup>
+ <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
+ <ClCompile>
+ <WarningLevel>Level3</WarningLevel>
+ <FunctionLevelLinking>true</FunctionLevelLinking>
+ <IntrinsicFunctions>true</IntrinsicFunctions>
+ <SDLCheck>true</SDLCheck>
+ <PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+ <ConformanceMode>true</ConformanceMode>
+ <LanguageStandard>stdcpp17</LanguageStandard>
+ <AdditionalOptions>/Zc:twoPhase- %(AdditionalOptions)</AdditionalOptions>
+ <RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
+ </ClCompile>
+ <Link>
+ <SubSystem>Console</SubSystem>
+ <EnableCOMDATFolding>true</EnableCOMDATFolding>
+ <OptimizeReferences>true</OptimizeReferences>
+ <GenerateDebugInformation>true</GenerateDebugInformation>
+ </Link>
+ <PostBuildEvent>
+ <Command>copy /Y "$(TargetPath)" "$(SolutionDir)signed\$(TargetFileName)"</Command>
+ </PostBuildEvent>
+ </ItemDefinitionGroup>
+ <ItemGroup>
+ <ClCompile Include="converter.cpp" />
+ </ItemGroup>
+ <ItemGroup>
+ <ProjectReference Include="..\wrapper\wrapper.vcxproj">
+ <Project>{28a3656f-a1de-405c-b547-191c32ec555f}</Project>
+ </ProjectReference>
+ </ItemGroup>
+ <ItemGroup>
+ <Reference Include="Newtonsoft.Json">
+ <HintPath>..\packages\Newtonsoft.Json.12.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
+ </Reference>
+ </ItemGroup>
+ <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
+ <ImportGroup Label="ExtensionTargets">
+ </ImportGroup>
+</Project> \ No newline at end of file
diff --git a/converter/converter.vcxproj.filters b/converter/converter.vcxproj.filters
new file mode 100644
index 0000000..954dbfa
--- /dev/null
+++ b/converter/converter.vcxproj.filters
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <ItemGroup>
+ <Filter Include="Source Files">
+ <UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
+ <Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
+ </Filter>
+ <Filter Include="Header Files">
+ <UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
+ <Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
+ </Filter>
+ <Filter Include="Resource Files">
+ <UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
+ <Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
+ </Filter>
+ </ItemGroup>
+ <ItemGroup>
+ <ClCompile Include="converter.cpp">
+ <Filter>Source Files</Filter>
+ </ClCompile>
+ </ItemGroup>
+</Project> \ No newline at end of file