Skip to content

Commit

Permalink
Use recommended path for configuration file. Adds home-dir library.
Browse files Browse the repository at this point in the history
  • Loading branch information
evgenykislov committed Oct 19, 2024
1 parent 5316247 commit 43d8211
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 12 deletions.
128 changes: 128 additions & 0 deletions libs/home-dir/home-dir.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
MIT License
Copyright (c) 2024 Evgeny Kislov
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

// Detect platform
#if defined(linux) || defined(__linux) || defined(__linux__)
#define LINUX_PLATFORM
#elif defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
#define WINDOWS_PLATFORM
#elif defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__)
#define MACOS_PLATFORM
#else
#error Unknown platform
#endif

#include "home-dir.h"

// --------------------------
// Linux implementation
// --------------------------

#ifdef LINUX_PLATFORM

#include <string>

#include <pwd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

const char kHomeEnvVar[] = "HOME";
const char kConfigDir[] = "/.config";

std::string HomeDirLibrary::GetHomeDir() {
try {
auto ev = getenv(kHomeEnvVar);
if (ev) {
return std::string(ev);
}
auto pw = getpwuid(getuid());
if (pw) {
return std::string(pw->pw_dir);
}
} catch (std::exception) {
}

return std::string();
}


std::string HomeDirLibrary::GetDataDir() {
return GetHomeDir() + kConfigDir;
}


#endif


// --------------------------
// Windows implementation
// --------------------------

#ifdef WINDOWS_PLATFORM

#include <windows.h>
#include <shlobj.h>

std::string GetCsidlPathA(int csidl) {
try {
CHAR s[MAX_PATH];
if (SHGetFolderPathA(nullptr, csidl, nullptr, 0, s) == S_OK) {
return std::string(s);
}
} catch (std::exception) {}
return std::string();
}

std::wstring GetCsidlPathW(int csidl) {
try {
WCHAR s[MAX_PATH];
if (SHGetFolderPathW(nullptr, csidl, nullptr, 0, s) == S_OK) {
return std::wstring(s);
}
} catch (std::exception) {}
return std::wstring();
}


std::string HomeDirLibrary::GetHomeDir() {
return GetCsidlPathA(CSIDL_PROFILE);
}


std::wstring HomeDirLibrary::GetHomeDirW() {
return GetCsidlPathW(CSIDL_PROFILE);
}


std::string HomeDirLibrary::GetDataDir() {
return GetCsidlPathA(CSIDL_LOCAL_APPDATA);
}


std::wstring HomeDirLibrary::GetDataDirW() {
return GetCsidlPathW(CSIDL_LOCAL_APPDATA);
}

#endif
42 changes: 42 additions & 0 deletions libs/home-dir/home-dir.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
MIT License
Copyright (c) 2024 Evgeny Kislov
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#ifndef HOMEDIR_H_EVGENY_KISLOV_20241017
#define HOMEDIR_H_EVGENY_KISLOV_20241017

#include <string>

namespace HomeDirLibrary {

std::string GetHomeDir();
std::string GetDataDir();

#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
std::wstring GetHomeDirW();
std::wstring GetDataDirW();
#endif

}

#endif // HOMEDIR_H_EVGENY_KISLOV_20241017
3 changes: 3 additions & 0 deletions psvrplayer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ set(SOURCE_FILES
"vr_helmet_hid.cpp"
"vr_helmet_view.cpp"
"glad/src/glad.c"
"../libs/home-dir/home-dir.cpp"
)

set(HEADER_FILES
Expand All @@ -39,6 +40,7 @@ set(HEADER_FILES
"vr_helmet_calibration.h"
"vr_helmet_hid.h"
"vr_helmet_view.h"
"../libs/home-dir/home-dir.h"
)

set(COMPILED_RESOURCES
Expand All @@ -60,6 +62,7 @@ set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package (Threads REQUIRED)

include_directories(../libs/iniparser/src)
include_directories(../libs/home-dir)

# Resource compiler
SET(RESOURCE_COMPILER xxd)
Expand Down
26 changes: 17 additions & 9 deletions psvrplayer/vr_helmet_calibration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,33 @@
#include <errno.h>

#include "iniparser.h"
#include "home-dir.h"

// TODO Detects home directory and generate correct configuration file name
const char kConfigFileName[] = "/tmp/psvrplayer.cfg";
const char kConfigFileName[] = "/psvrplayer.cfg";

PsvrHelmetCalibration::PsvrHelmetCalibration()
: to_right_summ_(0),
to_top_summ_(0),
to_clockwork_summ_(0),
data_counter_(0) {
config_fname_ = HomeDirLibrary::GetDataDir();
if (config_fname_.empty()) {
// Путь для конфигурационных данных неопределён
std::cerr
<< "ERROR: Can't get folder for configuration file. Calibration failed"
<< std::endl;
throw std::runtime_error("Can't get data folder ");
}
config_fname_ += kConfigFileName;
// Create configuration file if it don't exist
std::ifstream f1(kConfigFileName, std::ios_base::in);
std::ifstream f1(config_fname_, std::ios_base::in);
if (!f1) {
std::ofstream f2(
kConfigFileName, std::ios_base::out | std::ios_base::trunc);
std::ofstream f2(config_fname_, std::ios_base::out | std::ios_base::trunc);
if (f2.is_open()) {
f2 << "# ps vr player configuration file" << std::endl;
} else {
// Файл не существует и создать его нельзя
std::cerr << "ERROR: Can't create configuration file '" << kConfigFileName
std::cerr << "ERROR: Can't create configuration file '" << config_fname_
<< "'. Calibration failed" << std::endl;
throw std::runtime_error("Can't create configuration file");
}
Expand All @@ -49,7 +57,7 @@ PsvrHelmetCalibration::~PsvrHelmetCalibration() {
return;
}

auto dict = iniparser_load(kConfigFileName);
auto dict = iniparser_load(config_fname_.c_str());
if (!dict) {
std::cerr << "Can't parse configuration file. Calibration failed"
<< std::endl;
Expand All @@ -66,9 +74,9 @@ PsvrHelmetCalibration::~PsvrHelmetCalibration() {
std::cerr << "Can't generate configuration file. Calibration failed"
<< std::endl;
} else {
auto f = fopen(kConfigFileName, "w+");
auto f = fopen(config_fname_.c_str(), "w+");
if (!f) {
std::cerr << "Can't open configuration file '" << kConfigFileName
std::cerr << "Can't open configuration file '" << config_fname_
<< "'. Calibration failed" << std::endl;
} else {
iniparser_dump_ini(dict, f);
Expand Down
3 changes: 3 additions & 0 deletions psvrplayer/vr_helmet_calibration.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class PsvrHelmetCalibration: public IHelmet, PsvrHelmetHid {
int64_t to_clockwork_summ_;
int64_t data_counter_;
std::mutex data_lock_;

// Путь к файлу с конфигурационными данными
std::string config_fname_;
};

#endif // PSVRHELMETCALIBRATION_H
7 changes: 4 additions & 3 deletions psvrplayer/vr_helmet_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,22 @@
#include <hidapi/hidapi.h>

#include "iniparser.h"
#include "home-dir.h"


#define DEBUG_HELMET_VIEW

const double kPi = 3.1415926535897932384626433832795;

// TODO Detects home directory and generate correct configuration file name
const char kConfigFileName[] = "/tmp/psvrplayer.cfg";
const char kConfigFileName[] = "/psvrplayer.cfg";

PsvrHelmetView::PsvrHelmetView() {
center_view_flag_ = true;
last_sensor_time_ = std::numeric_limits<uint64_t>::max();

auto cfg = HomeDirLibrary::GetDataDir() + kConfigFileName;
std::unique_lock<std::mutex> vl(velo_lock_);
auto dict = iniparser_load(kConfigFileName);
auto dict = iniparser_load(cfg.c_str());
if (dict) {
right_velo_ = double(iniparser_getint64(dict, "Calibration:right", 0)) /
kFixedPointFactor;
Expand Down

0 comments on commit 43d8211

Please sign in to comment.