-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathFindDriver.cpp
314 lines (280 loc) · 9.83 KB
/
FindDriver.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/** @file
@brief Implementation
@date 2016
@author
Sensics, Inc.
<http://sensics.com/osvr>
*/
// Copyright 2016 Razer Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Internal Includes
#include "FindDriver.h"
// Library/third-party includes
#include <boost/iostreams/stream.hpp>
#include <json/reader.h>
#include <json/value.h>
#include <osvr/Util/Finally.h>
#include <osvr/Util/PlatformConfig.h>
// Standard includes
#include <fstream> // std::ifstream
#include <iostream>
#include <limits.h>
#include <vector>
#if defined(OSVR_USING_FILESYSTEM_HEADER)
#include <filesystem>
#elif defined(OSVR_USING_BOOST_FILESYSTEM)
#include <boost/filesystem.hpp>
#endif
#if defined(OSVR_WINDOWS)
#include <shlobj.h>
#else
#include <cstdlib> // for getenv
#endif
#undef VIVELOADER_VERBOSE
using osvr::util::finally;
namespace osvr {
namespace vive {
#if defined(OSVR_WINDOWS)
static const auto PLATFORM_DIRNAME_BASE = "win";
static const auto DRIVER_EXTENSION = ".dll";
static const auto TOOL_EXTENSION = ".exe";
static const auto PATH_SEP = "\\";
#elif defined(OSVR_MACOSX)
/// @todo Note that there are no 64-bit steamvr runtimes or drivers on OS X
static const auto PLATFORM_DIRNAME_BASE = "osx";
static const auto DRIVER_EXTENSION = ".dylib";
static const auto TOOL_EXTENSION = "";
static const auto PATH_SEP = "/";
#elif defined(OSVR_LINUX)
static const auto PLATFORM_DIRNAME_BASE = "linux";
static const auto DRIVER_EXTENSION = ".so";
static const auto TOOL_EXTENSION = "";
static const auto PATH_SEP = "/";
#else
#error "Sorry, Valve does not produce a SteamVR runtime for your platform."
#endif
#if defined(OSVR_USING_FILESYSTEM_TR2)
using std::tr2::sys::path;
using std::tr2::sys::wpath;
using std::tr2::sys::exists;
#elif defined(OSVR_USING_FILESYSTEM_EXPERIMENTAL)
using std::experimental::filesystem::path;
#ifdef _WIN32
/// Some Windows functions deal in wide strings, easier to just let it cope
/// with it.
using wpath = path;
#endif
using std::experimental::filesystem::exists;
#elif defined(OSVR_USING_BOOST_FILESYSTEM)
using boost::filesystem::path;
using boost::filesystem::exists;
#endif
#ifdef OSVR_MACOSX
inline std::string getPlatformBitSuffix() {
// they use universal binaries but stick them in "32"
return "32";
}
#else
inline std::string getPlatformBitSuffix() {
return std::to_string(sizeof(void *) * CHAR_BIT);
}
#endif
inline std::string getPlatformDirname() {
return PLATFORM_DIRNAME_BASE + getPlatformBitSuffix();
}
inline void parsePathConfigFile(std::istream &is, Json::Value &ret) {
Json::Reader reader;
if (!reader.parse(is, ret)) {
std::cerr << "Error parsing file containing path configuration - "
"have you run SteamVR yet?"
<< std::endl;
std::cerr << reader.getFormattedErrorMessages() << std::endl;
}
}
#if defined(OSVR_WINDOWS)
inline Json::Value getPathConfig() {
PWSTR outString = nullptr;
Json::Value ret;
// It's OK to use Vista+ stuff here, since openvr_api.dll uses Vista+
// stuff too.
auto hr =
SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, nullptr, &outString);
if (!SUCCEEDED(hr)) {
std::cerr << "Could not get local app data directory!" << std::endl;
return ret;
}
// Free the string returned when we're all done.
/// @todo Shouldn't this be in a "finally()" call?
auto freeString = [&] { CoTaskMemFree(outString); };
// Build the path to the file.
auto vrPaths =
wpath(outString) / wpath(L"openvr") / wpath(L"openvrpaths.vrpath");
std::ifstream is(vrPaths.string());
if (!is) {
std::wcerr << L"Could not open file containing path configuration "
L"- have you run SteamVR yet? "
<< vrPaths << L"\n";
return ret;
}
parsePathConfigFile(is, ret);
return ret;
}
#elif defined(OSVR_MACOSX) || defined(OSVR_LINUX)
inline Json::Value getPathConfig() {
auto home = std::getenv("HOME");
path homePath =
(nullptr == home
? path{"~"}
/*that's weird, should have been in environment...*/
: path{home});
auto vrPaths = homePath / path{".openvr"} / path{"openvrpaths.vrpath"};
std::ifstream is(vrPaths.string());
Json::Value ret;
if (!is) {
std::cerr << "Could not open file containing path configuration "
"- have you run SteamVR yet? "
<< vrPaths << "\n";
return ret;
}
parsePathConfigFile(is, ret);
return ret;
}
#endif
inline std::vector<std::string> getSteamVRRoots(Json::Value const &json) {
std::vector<std::string> ret;
auto &runtimes = json["runtime"];
if (!runtimes.isArray()) {
return ret;
}
for (auto &runtime : runtimes) {
ret.emplace_back(runtime.asString());
}
return ret;
}
inline std::vector<std::string> getSteamVRRoots() {
return getSteamVRRoots(getPathConfig());
}
inline void computeDriverRootAndFilePath(DriverLocationInfo &info,
std::string const &driver) {
auto p = path{info.steamVrRoot};
p /= "drivers";
p /= driver;
p /= "bin";
p /= getPlatformDirname();
info.driverRoot = p.string();
p /= ("driver_" + driver + DRIVER_EXTENSION);
info.driverFile = p.string();
}
/// Underlying implementation - hand it the preloaded json.
inline DriverLocationInfo findDriver(Json::Value const &json,
std::string const &driver) {
DriverLocationInfo info;
auto &runtimes = json["runtime"];
if (!runtimes.isArray()) {
return info;
}
for (auto &root : runtimes) {
info.steamVrRoot = root.asString();
info.driverName = driver;
computeDriverRootAndFilePath(info, driver);
#ifdef VIVELOADER_VERBOSE
std::cout << "Will try to load driver from:\n"
<< info.driverFile << std::endl;
if (exists(path{info.driverRoot})) {
std::cout << "Driver root exists" << std::endl;
}
if (exists(path{info.driverFile})) {
std::cout << "Driver file exists" << std::endl;
}
#endif
if (exists(path{info.driverRoot}) &&
exists(path{info.driverFile})) {
info.found = true;
return info;
}
}
info = DriverLocationInfo{};
return info;
}
DriverLocationInfo findDriver(std::string const &driver) {
return findDriver(getPathConfig(), driver);
}
std::string getToolLocation(std::string const &toolName,
std::string const &steamVrRoot) {
std::vector<std::string> searchPath;
if (!steamVrRoot.empty()) {
searchPath = {steamVrRoot};
} else {
searchPath = getSteamVRRoots();
}
for (auto &root : searchPath) {
auto p = path{root};
p /= "bin";
p /= getPlatformDirname();
p /= (toolName + TOOL_EXTENSION);
if (exists(p)) {
return p.string();
}
}
return std::string{};
}
/// Underlying implementation - hand it the preloaded json.
inline ConfigDirs findConfigDirs(Json::Value const &json,
std::string const &driver) {
ConfigDirs ret;
auto const &configLocations = json["config"];
if (!configLocations.isArray()) {
return ret;
}
for (auto &configDir : configLocations) {
auto configPath = path{configDir.asString()};
if (!exists(configPath)) {
continue;
}
ret.rootConfigDir = configPath.string();
ret.driverConfigDir = (configPath / path{driver}).string();
ret.valid = true;
return ret;
}
ret = ConfigDirs{};
return ret;
}
#if 0
ConfigDirs findConfigDirs(std::string const & /*steamVrRoot*/,
std::string const &driver) {
return findConfigDirs(getPathConfig(), driver);
}
#endif
LocationInfo findLocationInfoForDriver(std::string const &driver) {
auto json = getPathConfig();
LocationInfo ret;
auto config = findConfigDirs(json, driver);
if (config.valid) {
ret.configFound = true;
ret.rootConfigDir = config.rootConfigDir;
ret.driverConfigDir = config.driverConfigDir;
}
auto driverLoc = findDriver(json, driver);
if (driverLoc.found) {
ret.driverFound = true;
ret.steamVrRoot = driverLoc.steamVrRoot;
ret.driverRoot = driverLoc.driverRoot;
ret.driverName = driverLoc.driverName;
ret.driverFile = driverLoc.driverFile;
}
ret.found = (driverLoc.found && config.valid);
return ret;
}
} // namespace vive
} // namespace osvr