Util_SystemInfo.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. /************************************************************************************
  2. Filename : Util_SystemInfo.cpp
  3. Content : Various operations to get information about the system
  4. Created : September 26, 2014
  5. Author : Kevin Jenkins
  6. Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved.
  7. Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License");
  8. you may not use the Oculus VR Rift SDK except in compliance with the License,
  9. which is provided at the time of installation or download, or which
  10. otherwise accompanies this software in either electronic or hard copy form.
  11. You may obtain a copy of the License at
  12. http://www.oculusvr.com/licenses/LICENSE-3.2
  13. Unless required by applicable law or agreed to in writing, the Oculus VR SDK
  14. distributed under the License is distributed on an "AS IS" BASIS,
  15. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. See the License for the specific language governing permissions and
  17. limitations under the License.
  18. ************************************************************************************/
  19. #include "Util_SystemInfo.h"
  20. #include "Kernel/OVR_Timer.h"
  21. #include "Kernel/OVR_Threads.h"
  22. #include "Kernel/OVR_Log.h"
  23. #include "Kernel/OVR_Array.h"
  24. #if defined(OVR_OS_LINUX)
  25. #include <sys/utsname.h>
  26. #endif
  27. // Includes used for GetBaseOVRPath()
  28. #ifdef OVR_OS_WIN32
  29. #include "Kernel/OVR_Win32_IncludeWindows.h"
  30. #include <Shlobj.h>
  31. #include <Shlwapi.h>
  32. #pragma comment(lib, "Shlwapi")
  33. #elif defined(OVR_OS_MS) // Other Microsoft OSs
  34. // Nothing, thanks.
  35. #else
  36. #include <dirent.h>
  37. #include <sys/stat.h>
  38. #ifdef OVR_OS_LINUX
  39. #include <unistd.h>
  40. #include <pwd.h>
  41. #endif
  42. #endif
  43. namespace OVR { namespace Util {
  44. // From http://blogs.msdn.com/b/oldnewthing/archive/2005/02/01/364563.aspx
  45. #if defined (OVR_OS_WIN64) || defined (OVR_OS_WIN32)
  46. #pragma comment(lib, "version.lib")
  47. typedef BOOL(WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
  48. bool Is64BitWindows()
  49. {
  50. #if defined(_WIN64)
  51. return TRUE; // 64-bit programs run only on Win64
  52. #elif defined(_WIN32)
  53. // 32-bit programs run on both 32-bit and 64-bit Windows
  54. // so must sniff
  55. BOOL f64 = FALSE;
  56. LPFN_ISWOW64PROCESS fnIsWow64Process;
  57. fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(GetModuleHandleW(L"kernel32"), "IsWow64Process");
  58. if (NULL != fnIsWow64Process)
  59. {
  60. return fnIsWow64Process(GetCurrentProcess(), &f64) && f64;
  61. }
  62. return FALSE;
  63. #else
  64. return FALSE; // Win64 does not support Win16
  65. #endif
  66. }
  67. #endif
  68. const char * OSAsString()
  69. {
  70. #if defined (OVR_OS_IPHONE)
  71. return "IPhone";
  72. #elif defined (OVR_OS_DARWIN)
  73. return "Darwin";
  74. #elif defined (OVR_OS_MAC)
  75. return "Mac";
  76. #elif defined (OVR_OS_BSD)
  77. return "BSD";
  78. #elif defined (OVR_OS_WIN64) || defined (OVR_OS_WIN32)
  79. if (Is64BitWindows())
  80. return "Win64";
  81. else
  82. return "Win32";
  83. #elif defined (OVR_OS_ANDROID)
  84. return "Android";
  85. #elif defined (OVR_OS_LINUX)
  86. return "Linux";
  87. #elif defined (OVR_OS_BSD)
  88. return "BSD";
  89. #else
  90. return "Other";
  91. #endif
  92. }
  93. uint64_t GetGuidInt()
  94. {
  95. uint64_t g = Timer::GetTicksNanos();
  96. uint64_t lastTime, thisTime;
  97. int j;
  98. // Sleep a small random time, then use the last 4 bits as a source of randomness
  99. for (j = 0; j < 8; j++)
  100. {
  101. lastTime = Timer::GetTicksNanos();
  102. Thread::MSleep(1);
  103. // Note this does not actually sleep for "only" 1 millisecond
  104. // necessarily. Since we do not call timeBeginPeriod(1) explicitly
  105. // before invoking this function it may be sleeping for 10+ milliseconds.
  106. thisTime = Timer::GetTicksNanos();
  107. uint64_t diff = thisTime - lastTime;
  108. unsigned int diff4Bits = (unsigned int)(diff & 15);
  109. diff4Bits <<= 32 - 4;
  110. diff4Bits >>= j * 4;
  111. ((char*)&g)[j] ^= diff4Bits;
  112. }
  113. return g;
  114. }
  115. String GetGuidString()
  116. {
  117. uint64_t guid = GetGuidInt();
  118. char buff[64];
  119. #if defined(OVR_CC_MSVC)
  120. OVR_sprintf(buff, sizeof(buff), "%I64u", guid);
  121. #else
  122. OVR_sprintf(buff, sizeof(buff), "%llu", (unsigned long long) guid);
  123. #endif
  124. return String(buff);
  125. }
  126. const char * GetProcessInfo()
  127. {
  128. #if defined (OVR_CPU_X86_64 )
  129. return "64 bit";
  130. #elif defined (OVR_CPU_X86)
  131. return "32 bit";
  132. #else
  133. return "TODO";
  134. #endif
  135. }
  136. #ifdef OVR_OS_WIN32
  137. String OSVersionAsString()
  138. {
  139. return GetSystemFileVersionStringW(L"\\kernel32.dll");
  140. }
  141. String GetSystemFileVersionStringW(wchar_t filePath[MAX_PATH])
  142. {
  143. wchar_t strFilePath[MAX_PATH]; // Local variable
  144. UINT sysDirLen = GetSystemDirectoryW(strFilePath, ARRAYSIZE(strFilePath));
  145. if (sysDirLen != 0)
  146. {
  147. OVR_wcscat(strFilePath, MAX_PATH, filePath);
  148. return GetFileVersionStringW(strFilePath);
  149. }
  150. else
  151. {
  152. return "GetSystemDirectoryW failed";
  153. }
  154. }
  155. // See http://stackoverflow.com/questions/940707/how-do-i-programatically-get-the-version-of-a-dll-or-exe-file
  156. String GetFileVersionStringW(wchar_t filePath[MAX_PATH])
  157. {
  158. String result;
  159. DWORD dwSize = GetFileVersionInfoSizeW(filePath, NULL);
  160. if (dwSize == 0)
  161. {
  162. OVR_DEBUG_LOG(("Error in GetFileVersionInfoSizeW: %d (for %s)", GetLastError(), String(filePath).ToCStr()));
  163. result = String(filePath) + " not found";
  164. }
  165. else
  166. {
  167. BYTE* pVersionInfo = new BYTE[dwSize];
  168. if (!pVersionInfo)
  169. {
  170. OVR_DEBUG_LOG(("Out of memory allocating %d bytes (for %s)", dwSize, filePath));
  171. result = "Out of memory";
  172. }
  173. else
  174. {
  175. if (!GetFileVersionInfoW(filePath, 0, dwSize, pVersionInfo))
  176. {
  177. OVR_DEBUG_LOG(("Error in GetFileVersionInfo: %d (for %s)", GetLastError(), String(filePath).ToCStr()));
  178. result = "Cannot get version info";
  179. }
  180. else
  181. {
  182. VS_FIXEDFILEINFO* pFileInfo = NULL;
  183. UINT pLenFileInfo = 0;
  184. if (!VerQueryValueW(pVersionInfo, L"\\", (LPVOID*)&pFileInfo, &pLenFileInfo))
  185. {
  186. OVR_DEBUG_LOG(("Error in VerQueryValueW: %d (for %s)", GetLastError(), String(filePath).ToCStr()));
  187. result = "File has no version info";
  188. }
  189. else
  190. {
  191. int major = (pFileInfo->dwFileVersionMS >> 16) & 0xffff;
  192. int minor = (pFileInfo->dwFileVersionMS) & 0xffff;
  193. int hotfix = (pFileInfo->dwFileVersionLS >> 16) & 0xffff;
  194. int other = (pFileInfo->dwFileVersionLS) & 0xffff;
  195. char str[128];
  196. OVR::OVR_sprintf(str, 128, "%d.%d.%d.%d", major, minor, hotfix, other);
  197. result = str;
  198. }
  199. }
  200. delete[] pVersionInfo;
  201. }
  202. }
  203. return result;
  204. }
  205. String GetDisplayDriverVersion()
  206. {
  207. if (Is64BitWindows())
  208. {
  209. return GetSystemFileVersionStringW(L"\\OVRDisplay64.dll");
  210. }
  211. else
  212. {
  213. return GetSystemFileVersionStringW(L"\\OVRDisplay32.dll");
  214. }
  215. }
  216. String GetCameraDriverVersion()
  217. {
  218. return GetSystemFileVersionStringW(L"\\drivers\\OCUSBVID.sys");
  219. }
  220. // From http://stackoverflow.com/questions/9524309/enumdisplaydevices-function-not-working-for-me
  221. void GetGraphicsCardList( Array< String > &gpus)
  222. {
  223. gpus.Clear();
  224. DISPLAY_DEVICEW dd;
  225. dd.cb = sizeof(dd);
  226. DWORD deviceNum = 0;
  227. while( EnumDisplayDevicesW(NULL, deviceNum, &dd, 0) )
  228. {
  229. if (dd.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE)
  230. gpus.PushBack(String(dd.DeviceString));
  231. deviceNum++;
  232. }
  233. }
  234. String GetProcessorInfo()
  235. {
  236. char brand[0x40] = {};
  237. int cpui[4] = { -1 };
  238. __cpuidex(cpui, 0x80000002, 0);
  239. //unsigned int blocks = cpui[0];
  240. for (int i = 0; i <= 2; ++i)
  241. {
  242. __cpuidex(cpui, 0x80000002 + i, 0);
  243. *reinterpret_cast<int*>(brand + i * 16) = cpui[0];
  244. *reinterpret_cast<int*>(brand + 4 + i * 16) = cpui[1];
  245. *reinterpret_cast<int*>(brand + 8 + i * 16) = cpui[2];
  246. *reinterpret_cast<int*>(brand + 12 + i * 16) = cpui[3];
  247. }
  248. return String(brand, 0x40);
  249. }
  250. #else
  251. #ifdef OVR_OS_MAC
  252. //use objective c source
  253. // used for driver files
  254. String GetFileVersionString(String /*filePath*/)
  255. {
  256. return String();
  257. }
  258. String GetSystemFileVersionString(String /*filePath*/)
  259. {
  260. return String();
  261. }
  262. String GetDisplayDriverVersion()
  263. {
  264. return String();
  265. }
  266. String GetCameraDriverVersion()
  267. {
  268. return String();
  269. }
  270. #else
  271. String GetDisplayDriverVersion()
  272. {
  273. char info[256] = { 0 };
  274. FILE *file = popen("/usr/bin/glxinfo", "r");
  275. if (file)
  276. {
  277. int status = 0;
  278. while (status == 0)
  279. {
  280. status = fscanf(file, "%*[^\n]\n"); // Read up till the end of the current line, leaving the file pointer at the beginning of the next line (skipping any leading whitespace).
  281. OVR_UNUSED(status); // Prevent GCC compiler warning: "ignoring return value of ‘int fscanf(FILE*, const char*, ...)"
  282. status = fscanf(file, "OpenGL version string: %255[^\n]", info);
  283. }
  284. pclose(file);
  285. if (status == 1)
  286. {
  287. return String(info);
  288. }
  289. }
  290. return String("No graphics driver details found.");
  291. }
  292. String GetCameraDriverVersion()
  293. {
  294. struct utsname kver;
  295. if (uname(&kver))
  296. {
  297. return String();
  298. }
  299. return String(kver.release);
  300. }
  301. void GetGraphicsCardList(OVR::Array< OVR::String > &gpus)
  302. {
  303. gpus.Clear();
  304. char info[256] = { 0 };
  305. FILE *file = popen("/usr/bin/lspci", "r");
  306. if (file)
  307. {
  308. int status = 0;
  309. while (status >= 0)
  310. {
  311. status = fscanf(file, "%*[^\n]\n"); // Read up till the end of the current line, leaving the file pointer at the beginning of the next line (skipping any leading whitespace).
  312. OVR_UNUSED(status); // Prevent GCC compiler warning: "ignoring return value of ‘int fscanf(FILE*, const char*, ...)"
  313. status = fscanf(file, "%*[^ ] VGA compatible controller: %255[^\n]", info);
  314. if (status == 1)
  315. {
  316. gpus.PushBack(String(info));
  317. }
  318. }
  319. pclose(file);
  320. }
  321. if (gpus.GetSizeI() <= 0)
  322. {
  323. gpus.PushBack(String("No video card details found."));
  324. }
  325. }
  326. String OSVersionAsString()
  327. {
  328. char info[256] = { 0 };
  329. FILE *file = fopen("/etc/issue", "r");
  330. if (file)
  331. {
  332. int status = fscanf(file, "%255[^\n\\]", info);
  333. fclose(file);
  334. if (status == 1)
  335. {
  336. return String(info);
  337. }
  338. }
  339. return String("No OS version details found.");
  340. }
  341. String GetProcessorInfo()
  342. {
  343. char info[256] = { 0 };
  344. FILE *file = fopen("/proc/cpuinfo", "r");
  345. if (file)
  346. {
  347. int status = 0;
  348. while (status == 0)
  349. {
  350. status = fscanf(file, "%*[^\n]\n"); // Read up till the end of the current line, leaving the file pointer at the beginning of the next line (skipping any leading whitespace).
  351. OVR_UNUSED(status); // Prevent GCC compiler warning: "ignoring return value of ‘int fscanf(FILE*, const char*, ...)"
  352. status = fscanf(file, "model name : %255[^\n]", info);
  353. }
  354. fclose(file);
  355. if (status == 1)
  356. {
  357. return String(info);
  358. }
  359. }
  360. return String("No processor details found.");
  361. }
  362. #endif //OVR_OS_MAC
  363. #endif // WIN32
  364. //-----------------------------------------------------------------------------
  365. // Get the path for local app data.
  366. String GetBaseOVRPath(bool create_dir)
  367. {
  368. #if defined(OVR_OS_WIN32)
  369. wchar_t path[MAX_PATH];
  370. ::SHGetFolderPathW(0, CSIDL_LOCAL_APPDATA, NULL, 0, path);
  371. OVR_wcscat(path, MAX_PATH, L"\\Oculus");
  372. if (create_dir)
  373. { // Create the Oculus directory if it doesn't exist
  374. DWORD attrib = ::GetFileAttributesW(path);
  375. bool exists = attrib != INVALID_FILE_ATTRIBUTES && (attrib & FILE_ATTRIBUTE_DIRECTORY);
  376. if (!exists)
  377. {
  378. ::CreateDirectoryW(path, NULL);
  379. }
  380. }
  381. #elif defined(OVR_OS_MS) // Other Microsoft OSs
  382. // TODO: figure this out.
  383. OVR_UNUSED ( create_dir );
  384. path = "";
  385. #elif defined(OVR_OS_MAC)
  386. const char* home = getenv("HOME");
  387. path = home;
  388. path += "/Library/Preferences/Oculus";
  389. if (create_dir)
  390. { // Create the Oculus directory if it doesn't exist
  391. DIR* dir = opendir(path);
  392. if (dir == NULL)
  393. {
  394. mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO);
  395. }
  396. else
  397. {
  398. closedir(dir);
  399. }
  400. }
  401. #else
  402. const char* home = getenv("HOME");
  403. String path = home;
  404. path += "/.config/Oculus";
  405. if (create_dir)
  406. { // Create the Oculus directory if it doesn't exist
  407. DIR* dir = opendir(path);
  408. if (dir == NULL)
  409. {
  410. mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO);
  411. }
  412. else
  413. {
  414. closedir(dir);
  415. }
  416. }
  417. #endif
  418. return String(path);
  419. }
  420. #ifdef OVR_OS_MS
  421. //widechar functions are Windows only for now
  422. bool GetRegistryStringW(const wchar_t* pSubKey, const wchar_t* stringName, wchar_t out[MAX_PATH], bool wow64value, bool currentUser)
  423. {
  424. HKEY root = currentUser ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE;
  425. DWORD dwType = REG_SZ;
  426. HKEY hKey = 0;
  427. wchar_t value[MAX_PATH + 1]; // +1 because RegQueryValueEx doesn't necessarily 0-terminate.
  428. DWORD value_length = MAX_PATH;
  429. if ((RegOpenKeyExW(root, pSubKey, 0, KEY_QUERY_VALUE | (wow64value ? KEY_WOW64_32KEY : 0), &hKey) != ERROR_SUCCESS) ||
  430. (RegQueryValueExW(hKey, stringName, NULL, &dwType, (LPBYTE)&value, &value_length) != ERROR_SUCCESS) || (dwType != REG_SZ))
  431. {
  432. out[0] = L'\0';
  433. RegCloseKey(hKey);
  434. return false;
  435. }
  436. RegCloseKey(hKey);
  437. value[value_length] = L'\0';
  438. wcscpy_s(out, MAX_PATH, value);
  439. return true;
  440. }
  441. bool GetRegistryDwordW(const wchar_t* pSubKey, const wchar_t* stringName, DWORD& out, bool wow64value, bool currentUser)
  442. {
  443. HKEY root = currentUser ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE;
  444. DWORD dwType = REG_DWORD;
  445. HKEY hKey = 0;
  446. DWORD value_length = sizeof(DWORD);
  447. if ((RegOpenKeyExW(root, pSubKey, 0, KEY_QUERY_VALUE | (wow64value ? KEY_WOW64_32KEY : 0), &hKey) != ERROR_SUCCESS) ||
  448. (RegQueryValueExW(hKey, stringName, NULL, &dwType, (LPBYTE)&out, &value_length) != ERROR_SUCCESS) || (dwType != REG_DWORD))
  449. {
  450. out = 0;
  451. RegCloseKey(hKey);
  452. return false;
  453. }
  454. RegCloseKey(hKey);
  455. return true;
  456. }
  457. bool GetRegistryBinaryW(const wchar_t* pSubKey, const wchar_t* stringName, LPBYTE out, DWORD* size, bool wow64value, bool currentUser)
  458. {
  459. HKEY root = currentUser ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE;
  460. DWORD dwType = REG_BINARY;
  461. HKEY hKey = 0;
  462. if ((RegOpenKeyExW(root, pSubKey, 0, KEY_QUERY_VALUE | (wow64value ? KEY_WOW64_32KEY : 0), &hKey) != ERROR_SUCCESS) ||
  463. (RegQueryValueExW(hKey, stringName, NULL, &dwType, out, size) != ERROR_SUCCESS) || (dwType != REG_BINARY))
  464. {
  465. *out = 0;
  466. RegCloseKey(hKey);
  467. return false;
  468. }
  469. RegCloseKey(hKey);
  470. return true;
  471. }
  472. // When reading Oculus registry keys, we recognize that the user may have inconsistently
  473. // used a DWORD 1 vs. a string "1", and so we support either when checking booleans.
  474. bool GetRegistryBoolW(const wchar_t* pSubKey, const wchar_t* stringName, bool defaultValue, bool wow64value, bool currentUser)
  475. {
  476. wchar_t out[MAX_PATH];
  477. if (GetRegistryStringW(pSubKey, stringName, out, wow64value, currentUser))
  478. {
  479. return (_wtoi64(out) != 0);
  480. }
  481. DWORD dw;
  482. if (GetRegistryDwordW(pSubKey, stringName, dw, wow64value, currentUser))
  483. {
  484. return (dw != 0);
  485. }
  486. return defaultValue;
  487. }
  488. bool SetRegistryBinaryW(const wchar_t* pSubKey, const wchar_t* stringName, LPBYTE value, DWORD size, bool wow64value, bool currentUser)
  489. {
  490. HKEY root = currentUser ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE;
  491. HKEY hKey = 0;
  492. if ((RegCreateKeyExW(root, pSubKey, 0, nullptr, 0, KEY_CREATE_SUB_KEY | KEY_SET_VALUE | (wow64value ? KEY_WOW64_32KEY : 0), nullptr, &hKey, nullptr) != ERROR_SUCCESS) ||
  493. (RegSetValueExW(hKey, stringName, 0, REG_BINARY, value, size) != ERROR_SUCCESS))
  494. {
  495. RegCloseKey(hKey);
  496. return false;
  497. }
  498. RegCloseKey(hKey);
  499. return true;
  500. }
  501. bool DeleteRegistryValue(const wchar_t* pSubKey, const wchar_t* stringName, bool wow64value, bool currentUser)
  502. {
  503. HKEY root = currentUser ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE;
  504. HKEY hKey = 0;
  505. if (RegOpenKeyExW(root, pSubKey, 0, KEY_ALL_ACCESS | (wow64value ? KEY_WOW64_32KEY : 0), &hKey) != ERROR_SUCCESS)
  506. {
  507. RegCloseKey(hKey);
  508. return false;
  509. }
  510. bool result = (RegDeleteValueW(hKey, stringName) == ERROR_SUCCESS);
  511. RegCloseKey(hKey);
  512. return result;
  513. }
  514. bool GetOVRRuntimePathW(wchar_t out[MAX_PATH])
  515. {
  516. if (Is64BitWindows())
  517. {
  518. if (!GetRegistryStringW(L"Software\\Wow6432Node\\Oculus VR, LLC\\Oculus Runtime", L"Location", out))
  519. {
  520. return false;
  521. }
  522. }
  523. else if (!GetRegistryStringW(L"Software\\Oculus VR, LLC\\Oculus Runtime", L"Location", out))
  524. {
  525. return false;
  526. }
  527. return true;
  528. }
  529. #endif // OVR_OS_MS
  530. bool GetOVRRuntimePath(String& runtimePath)
  531. {
  532. runtimePath = "";
  533. #ifdef OVR_OS_MS
  534. wchar_t path[MAX_PATH];
  535. if (GetOVRRuntimePathW(path))
  536. {
  537. runtimePath = String(path);
  538. return true;
  539. }
  540. #endif // OVR_OS_MS
  541. //mac/linux uses environment variables
  542. return false;
  543. }
  544. bool GetDefaultFirmwarePath(String& firmwarePath)
  545. {
  546. if (!GetOVRRuntimePath(firmwarePath))
  547. {
  548. return false;
  549. }
  550. else
  551. {
  552. firmwarePath + "\\Tools\\FirmwareBundle.json";
  553. return true;
  554. }
  555. }
  556. bool GetFirmwarePathFromService(OVR::String& firmwarePath, int numSearchDirs)
  557. {
  558. #ifdef OVR_OS_MS
  559. firmwarePath = "";
  560. //Try relative file locations
  561. wchar_t path[MAX_PATH]; // FIXME: This is Windows-specific.
  562. // Get full path to our module.
  563. int pathlen = ::GetModuleFileNameW(nullptr, path, MAX_PATH);
  564. OVR_ASSERT_AND_UNUSED(pathlen, pathlen);
  565. //try the registry default
  566. if (GetOVRRuntimePath(firmwarePath) && !firmwarePath.IsEmpty())
  567. {
  568. if (OVR_strncmp(OVR::String(path), firmwarePath, firmwarePath.GetSize()) == 0)
  569. {
  570. //we are in the default runtime directory.
  571. firmwarePath += "/Tools/Firmware/";
  572. return true;
  573. }
  574. else
  575. {
  576. firmwarePath = "";
  577. }
  578. }
  579. if (firmwarePath.IsEmpty())
  580. {
  581. //try internal path.
  582. wchar_t relpath[MAX_PATH];
  583. relpath[0] = L'\0';
  584. for (int i = 0; i < numSearchDirs; i++)
  585. {
  586. wchar_t* trailingSlash = wcsrchr(path, L'\\');
  587. if (trailingSlash == nullptr)
  588. {
  589. break; // no more paths to traverse
  590. }
  591. *(trailingSlash + 1) = L'\0'; //delete after the last trailing slash
  592. //Then attach this prefix
  593. OVR_wcscat(path, MAX_PATH, L"Firmware\\");
  594. //And attempt to find the path
  595. if (::PathFileExistsW(path))
  596. {
  597. firmwarePath = String(path);
  598. return true;
  599. }
  600. else
  601. {
  602. *trailingSlash = L'\0'; //remove trailing slash, traversing up 1 directory
  603. }
  604. }
  605. }
  606. firmwarePath = L"";
  607. #else
  608. OVR_UNUSED2(firmwarePath, numSearchDirs);
  609. //#error "FIXME"
  610. #endif
  611. return false;
  612. }
  613. }} // namespace OVR::Util