Skip to content

Commit

Permalink
Reduce duplication and remove dead code
Browse files Browse the repository at this point in the history
  • Loading branch information
pushrax committed Apr 6, 2020
1 parent 9c4a906 commit 7cf363e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 96 deletions.
36 changes: 0 additions & 36 deletions OpenVR-SpaceCalibrator/Configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,39 +200,3 @@ static void UpgradeProfileV1(CalibrationContext &ctx)
file.close();
std::remove("openvr_space_calibration.txt");
}

void WriteActivateMultipleDriversToConfig()
{
std::string configPath = ConfigFileName();
std::ifstream ifile(configPath);
if (!ifile.good())
throw std::runtime_error("failed to read steamvr.vrsettings");

picojson::value v;
std::string err = picojson::parse(v, ifile);
if (!err.empty())
throw std::runtime_error(err);

ifile.close();

if (!v.is<picojson::object>())
throw std::runtime_error("steamvr.vrsettings is empty");

auto &root = v.get<picojson::object>();

if (!root["steamvr"].is<picojson::object>())
throw std::runtime_error("steamvr.vrsettings is missing \"steamvr\" key");

auto &steamvr = root["steamvr"].get<picojson::object>();

const bool tru = true; // MSVC picks the wrong specialization when passing a literal...
steamvr["activateMultipleDrivers"].set<bool>(tru);

std::ofstream ofile(configPath);
if (!ofile.good())
throw std::runtime_error("failed to write steamvr.vrsettings");

v.serialize(std::ostream_iterator<char>(ofile), true);

std::cout << "Successfully set activateMultipleDrivers to true" << std::endl;
}
1 change: 0 additions & 1 deletion OpenVR-SpaceCalibrator/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@

void LoadProfile(CalibrationContext &ctx);
void SaveProfile(CalibrationContext &ctx);
void WriteActivateMultipleDriversToConfig();
95 changes: 36 additions & 59 deletions OpenVR-SpaceCalibrator/OpenVR-SpaceCalibrator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,38 @@ void TryCreateVROverlay()
vr::VROverlay()->SetOverlayFromFile(overlayThumbnailHandle, iconPath.c_str());
}

void ActivateMultipleDrivers()
{
vr::EVRSettingsError vrSettingsError;
bool enabled = vr::VRSettings()->GetBool(vr::k_pch_SteamVR_Section, vr::k_pch_SteamVR_ActivateMultipleDrivers_Bool, &vrSettingsError);

if (vrSettingsError != vr::VRSettingsError_None)
{
std::string err = "Could not read \"" + std::string(vr::k_pch_SteamVR_ActivateMultipleDrivers_Bool) + "\" setting: "
+ vr::VRSettings()->GetSettingsErrorNameFromEnum(vrSettingsError);

throw std::runtime_error(err);
}

if (!enabled)
{
vr::VRSettings()->SetBool(vr::k_pch_SteamVR_Section, vr::k_pch_SteamVR_ActivateMultipleDrivers_Bool, true, &vrSettingsError);
if (vrSettingsError != vr::VRSettingsError_None)
{
std::string err = "Could not set \"" + std::string(vr::k_pch_SteamVR_ActivateMultipleDrivers_Bool) + "\" setting: "
+ vr::VRSettings()->GetSettingsErrorNameFromEnum(vrSettingsError);

throw std::runtime_error(err);
}

std::cerr << "Enabled \"" << vr::k_pch_SteamVR_ActivateMultipleDrivers_Bool << "\" setting" << std::endl;
}
else
{
std::cerr << "\"" << vr::k_pch_SteamVR_ActivateMultipleDrivers_Bool << "\" setting previously enabled" << std::endl;
}
}

void InitVR()
{
auto initError = vr::VRInitError_None;
Expand All @@ -149,26 +181,7 @@ void InitVR()
throw std::runtime_error("OpenVR error: Outdated IVROverlay_Version");
}

vr::EVRSettingsError vrSettingsError;
bool multipledrivers = vr::VRSettings()->GetBool(
vr::k_pch_SteamVR_Section,
vr::k_pch_SteamVR_ActivateMultipleDrivers_Bool,
&vrSettingsError);
if (vrSettingsError != vr::VRSettingsError_None)
{
std::string err = "Could not read \""
+ std::string(vr::k_pch_SteamVR_ActivateMultipleDrivers_Bool)
+ "\" setting: "
+ vr::VRSettings()->GetSettingsErrorNameFromEnum(vrSettingsError);
throw std::runtime_error(err);
}
if (!multipledrivers)
{
vr::VRSettings()->SetBool(
vr::k_pch_SteamVR_Section,
vr::k_pch_SteamVR_ActivateMultipleDrivers_Bool,
true);
}
ActivateMultipleDrivers();
}

void RunLoop()
Expand Down Expand Up @@ -438,7 +451,7 @@ static void HandleCommandLine(LPWSTR lpCmdLine)
vr::VR_Shutdown();
exit(-2);
}
else if (lstrcmp(lpCmdLine, L"-activatemultipledriversManual") == 0)
else if (lstrcmp(lpCmdLine, L"-activatemultipledrivers") == 0)
{
int ret = -2;
auto vrErr = vr::VRInitError_None;
Expand All @@ -447,48 +460,12 @@ static void HandleCommandLine(LPWSTR lpCmdLine)
{
try
{
WriteActivateMultipleDriversToConfig();
ActivateMultipleDrivers();
ret = 0;
}
catch (std::runtime_error &e)
{
std::cerr << "Failed to set activateMultipleDrivers: " << e.what() << std::endl;
}
}
else
{
fprintf(stderr, "Failed to initialize OpenVR: %s\n", vr::VR_GetVRInitErrorAsEnglishDescription(vrErr));
}
vr::VR_Shutdown();
exit(ret);
}
else if (lstrcmp(lpCmdLine, L"-activatemultipledrivers") == 0)
{
int ret = -2;
auto vrErr = vr::VRInitError_None;
vr::VR_Init(&vrErr, vr::VRApplication_Utility);
if (vrErr == vr::VRInitError_None)
{
vr::EVRSettingsError vrSettingsError;
bool multipledrivers = vr::VRSettings()->GetBool(
vr::k_pch_SteamVR_Section,
vr::k_pch_SteamVR_ActivateMultipleDrivers_Bool,
&vrSettingsError);
if (vrSettingsError != vr::VRSettingsError_None)
{
std::string err = "Could not read \""
+ std::string(vr::k_pch_SteamVR_ActivateMultipleDrivers_Bool)
+ "\" setting: "
+ vr::VRSettings()->GetSettingsErrorNameFromEnum(vrSettingsError);
std::cerr << "Failed to set activateMultipleDrivers: " << err << std::endl;
}
else if (!multipledrivers)
{
vr::VRSettings()->SetBool(
vr::k_pch_SteamVR_Section,
vr::k_pch_SteamVR_ActivateMultipleDrivers_Bool,
true);
ret = 0;
std::cerr << e.what() << std::endl;
}
}
else
Expand Down

0 comments on commit 7cf363e

Please sign in to comment.