platform-windows.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  1. /*
  2. * Copyright (c) 2013 Hugh Bailey <[email protected]>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <windows.h>
  17. #include <mmsystem.h>
  18. #include <shellapi.h>
  19. #include <shlobj.h>
  20. #include <intrin.h>
  21. #include "base.h"
  22. #include "platform.h"
  23. #include "darray.h"
  24. #include "dstr.h"
  25. #include "windows/win-version.h"
  26. #include "../../deps/w32-pthreads/pthread.h"
  27. static bool have_clockfreq = false;
  28. static LARGE_INTEGER clock_freq;
  29. static uint32_t winver = 0;
  30. static inline uint64_t get_clockfreq(void)
  31. {
  32. if (!have_clockfreq)
  33. QueryPerformanceFrequency(&clock_freq);
  34. return clock_freq.QuadPart;
  35. }
  36. static inline uint32_t get_winver(void)
  37. {
  38. if (!winver) {
  39. struct win_version_info ver;
  40. get_win_ver(&ver);
  41. winver = (ver.major << 16) | ver.minor;
  42. }
  43. return winver;
  44. }
  45. void *os_dlopen(const char *path)
  46. {
  47. struct dstr dll_name;
  48. wchar_t *wpath;
  49. wchar_t *wpath_slash;
  50. HMODULE h_library = NULL;
  51. if (!path)
  52. return NULL;
  53. dstr_init_copy(&dll_name, path);
  54. dstr_replace(&dll_name, "\\", "/");
  55. if (!dstr_find(&dll_name, ".dll"))
  56. dstr_cat(&dll_name, ".dll");
  57. os_utf8_to_wcs_ptr(dll_name.array, 0, &wpath);
  58. /* to make module dependency issues easier to deal with, allow
  59. * dynamically loaded libraries on windows to search for dependent
  60. * libraries that are within the library's own directory */
  61. wpath_slash = wcsrchr(wpath, L'/');
  62. if (wpath_slash) {
  63. *wpath_slash = 0;
  64. SetDllDirectoryW(wpath);
  65. *wpath_slash = L'/';
  66. }
  67. h_library = LoadLibraryW(wpath);
  68. bfree(wpath);
  69. dstr_free(&dll_name);
  70. if (wpath_slash)
  71. SetDllDirectoryW(NULL);
  72. if (!h_library) {
  73. DWORD error = GetLastError();
  74. char *message = NULL;
  75. FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM |
  76. FORMAT_MESSAGE_IGNORE_INSERTS |
  77. FORMAT_MESSAGE_ALLOCATE_BUFFER,
  78. NULL, error,
  79. MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
  80. (LPSTR)&message, 0, NULL);
  81. blog(LOG_INFO, "LoadLibrary failed for '%s': %s (%lu)",
  82. path, message, error);
  83. if (message)
  84. LocalFree(message);
  85. }
  86. return h_library;
  87. }
  88. void *os_dlsym(void *module, const char *func)
  89. {
  90. void *handle;
  91. handle = (void*)GetProcAddress(module, func);
  92. return handle;
  93. }
  94. void os_dlclose(void *module)
  95. {
  96. FreeLibrary(module);
  97. }
  98. union time_data {
  99. FILETIME ft;
  100. unsigned long long val;
  101. };
  102. struct os_cpu_usage_info {
  103. union time_data last_time, last_sys_time, last_user_time;
  104. DWORD core_count;
  105. };
  106. os_cpu_usage_info_t *os_cpu_usage_info_start(void)
  107. {
  108. struct os_cpu_usage_info *info = bzalloc(sizeof(*info));
  109. SYSTEM_INFO si;
  110. FILETIME dummy;
  111. GetSystemInfo(&si);
  112. GetSystemTimeAsFileTime(&info->last_time.ft);
  113. GetProcessTimes(GetCurrentProcess(), &dummy, &dummy,
  114. &info->last_sys_time.ft, &info->last_user_time.ft);
  115. info->core_count = si.dwNumberOfProcessors;
  116. return info;
  117. }
  118. double os_cpu_usage_info_query(os_cpu_usage_info_t *info)
  119. {
  120. union time_data cur_time, cur_sys_time, cur_user_time;
  121. FILETIME dummy;
  122. double percent;
  123. if (!info)
  124. return 0.0;
  125. GetSystemTimeAsFileTime(&cur_time.ft);
  126. GetProcessTimes(GetCurrentProcess(), &dummy, &dummy,
  127. &cur_sys_time.ft, &cur_user_time.ft);
  128. percent = (double)(cur_sys_time.val - info->last_sys_time.val +
  129. (cur_user_time.val - info->last_user_time.val));
  130. percent /= (double)(cur_time.val - info->last_time.val);
  131. percent /= (double)info->core_count;
  132. info->last_time.val = cur_time.val;
  133. info->last_sys_time.val = cur_sys_time.val;
  134. info->last_user_time.val = cur_user_time.val;
  135. return percent * 100.0;
  136. }
  137. void os_cpu_usage_info_destroy(os_cpu_usage_info_t *info)
  138. {
  139. if (info)
  140. bfree(info);
  141. }
  142. bool os_sleepto_ns(uint64_t time_target)
  143. {
  144. uint64_t t = os_gettime_ns();
  145. uint32_t milliseconds;
  146. if (t >= time_target)
  147. return false;
  148. milliseconds = (uint32_t)((time_target - t)/1000000);
  149. if (milliseconds > 1)
  150. Sleep(milliseconds-1);
  151. for (;;) {
  152. t = os_gettime_ns();
  153. if (t >= time_target)
  154. return true;
  155. #if 0
  156. Sleep(1);
  157. #else
  158. Sleep(0);
  159. #endif
  160. }
  161. }
  162. void os_sleep_ms(uint32_t duration)
  163. {
  164. /* windows 8+ appears to have decreased sleep precision */
  165. if (get_winver() >= 0x0602 && duration > 0)
  166. duration--;
  167. Sleep(duration);
  168. }
  169. uint64_t os_gettime_ns(void)
  170. {
  171. LARGE_INTEGER current_time;
  172. double time_val;
  173. QueryPerformanceCounter(&current_time);
  174. time_val = (double)current_time.QuadPart;
  175. time_val *= 1000000000.0;
  176. time_val /= (double)get_clockfreq();
  177. return (uint64_t)time_val;
  178. }
  179. /* returns [folder]\[name] on windows */
  180. static int os_get_path_internal(char *dst, size_t size, const char *name,
  181. int folder)
  182. {
  183. wchar_t path_utf16[MAX_PATH];
  184. SHGetFolderPathW(NULL, folder, NULL, SHGFP_TYPE_CURRENT,
  185. path_utf16);
  186. if (os_wcs_to_utf8(path_utf16, 0, dst, size) != 0) {
  187. if (!name || !*name) {
  188. return (int)strlen(dst);
  189. }
  190. if (strcat_s(dst, size, "\\") == 0) {
  191. if (strcat_s(dst, size, name) == 0) {
  192. return (int)strlen(dst);
  193. }
  194. }
  195. }
  196. return -1;
  197. }
  198. static char *os_get_path_ptr_internal(const char *name, int folder)
  199. {
  200. char *ptr;
  201. wchar_t path_utf16[MAX_PATH];
  202. struct dstr path;
  203. SHGetFolderPathW(NULL, folder, NULL, SHGFP_TYPE_CURRENT,
  204. path_utf16);
  205. os_wcs_to_utf8_ptr(path_utf16, 0, &ptr);
  206. dstr_init_move_array(&path, ptr);
  207. dstr_cat(&path, "\\");
  208. dstr_cat(&path, name);
  209. return path.array;
  210. }
  211. int os_get_config_path(char *dst, size_t size, const char *name)
  212. {
  213. return os_get_path_internal(dst, size, name, CSIDL_APPDATA);
  214. }
  215. char *os_get_config_path_ptr(const char *name)
  216. {
  217. return os_get_path_ptr_internal(name, CSIDL_APPDATA);
  218. }
  219. int os_get_program_data_path(char *dst, size_t size, const char *name)
  220. {
  221. return os_get_path_internal(dst, size, name, CSIDL_COMMON_APPDATA);
  222. }
  223. char *os_get_program_data_path_ptr(const char *name)
  224. {
  225. return os_get_path_ptr_internal(name, CSIDL_COMMON_APPDATA);
  226. }
  227. bool os_file_exists(const char *path)
  228. {
  229. WIN32_FIND_DATAW wfd;
  230. HANDLE hFind;
  231. wchar_t *path_utf16;
  232. if (!os_utf8_to_wcs_ptr(path, 0, &path_utf16))
  233. return false;
  234. hFind = FindFirstFileW(path_utf16, &wfd);
  235. if (hFind != INVALID_HANDLE_VALUE)
  236. FindClose(hFind);
  237. bfree(path_utf16);
  238. return hFind != INVALID_HANDLE_VALUE;
  239. }
  240. size_t os_get_abs_path(const char *path, char *abspath, size_t size)
  241. {
  242. wchar_t wpath[512];
  243. wchar_t wabspath[512];
  244. size_t out_len = 0;
  245. size_t len;
  246. if (!abspath)
  247. return 0;
  248. len = os_utf8_to_wcs(path, 0, wpath, 512);
  249. if (!len)
  250. return 0;
  251. if (_wfullpath(wabspath, wpath, 512) != NULL)
  252. out_len = os_wcs_to_utf8(wabspath, 0, abspath, size);
  253. return out_len;
  254. }
  255. char *os_get_abs_path_ptr(const char *path)
  256. {
  257. char *ptr = bmalloc(512);
  258. if (!os_get_abs_path(path, ptr, 512)) {
  259. bfree(ptr);
  260. ptr = NULL;
  261. }
  262. return ptr;
  263. }
  264. struct os_dir {
  265. HANDLE handle;
  266. WIN32_FIND_DATA wfd;
  267. bool first;
  268. struct os_dirent out;
  269. };
  270. os_dir_t *os_opendir(const char *path)
  271. {
  272. struct dstr path_str = {0};
  273. struct os_dir *dir = NULL;
  274. WIN32_FIND_DATA wfd;
  275. HANDLE handle;
  276. wchar_t *w_path;
  277. dstr_copy(&path_str, path);
  278. dstr_cat(&path_str, "/*.*");
  279. if (os_utf8_to_wcs_ptr(path_str.array, path_str.len, &w_path) > 0) {
  280. handle = FindFirstFileW(w_path, &wfd);
  281. if (handle != INVALID_HANDLE_VALUE) {
  282. dir = bzalloc(sizeof(struct os_dir));
  283. dir->handle = handle;
  284. dir->first = true;
  285. dir->wfd = wfd;
  286. }
  287. bfree(w_path);
  288. }
  289. dstr_free(&path_str);
  290. return dir;
  291. }
  292. static inline bool is_dir(WIN32_FIND_DATA *wfd)
  293. {
  294. return !!(wfd->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
  295. }
  296. struct os_dirent *os_readdir(os_dir_t *dir)
  297. {
  298. if (!dir)
  299. return NULL;
  300. if (dir->first) {
  301. dir->first = false;
  302. } else {
  303. if (!FindNextFileW(dir->handle, &dir->wfd))
  304. return NULL;
  305. }
  306. os_wcs_to_utf8(dir->wfd.cFileName, 0, dir->out.d_name,
  307. sizeof(dir->out.d_name));
  308. dir->out.directory = is_dir(&dir->wfd);
  309. return &dir->out;
  310. }
  311. void os_closedir(os_dir_t *dir)
  312. {
  313. if (dir) {
  314. FindClose(dir->handle);
  315. bfree(dir);
  316. }
  317. }
  318. int64_t os_get_free_space(const char *path)
  319. {
  320. ULARGE_INTEGER remainingSpace;
  321. char abs_path[512];
  322. wchar_t w_abs_path[512];
  323. if (os_get_abs_path(path, abs_path, 512) > 0) {
  324. if (os_utf8_to_wcs(abs_path, 0, w_abs_path, 512) > 0) {
  325. BOOL success = GetDiskFreeSpaceExW(w_abs_path,
  326. (PULARGE_INTEGER)&remainingSpace,
  327. NULL, NULL);
  328. if (success)
  329. return (int64_t)remainingSpace.QuadPart;
  330. }
  331. }
  332. return -1;
  333. }
  334. static void make_globent(struct os_globent *ent, WIN32_FIND_DATA *wfd,
  335. const char *pattern)
  336. {
  337. struct dstr name = {0};
  338. struct dstr path = {0};
  339. char *slash;
  340. dstr_from_wcs(&name, wfd->cFileName);
  341. dstr_copy(&path, pattern);
  342. slash = strrchr(path.array, '/');
  343. if (slash)
  344. dstr_resize(&path, slash + 1 - path.array);
  345. else
  346. dstr_free(&path);
  347. dstr_cat_dstr(&path, &name);
  348. ent->path = path.array;
  349. ent->directory = is_dir(wfd);
  350. dstr_free(&name);
  351. }
  352. int os_glob(const char *pattern, int flags, os_glob_t **pglob)
  353. {
  354. DARRAY(struct os_globent) files;
  355. HANDLE handle;
  356. WIN32_FIND_DATA wfd;
  357. int ret = -1;
  358. wchar_t *w_path;
  359. da_init(files);
  360. if (os_utf8_to_wcs_ptr(pattern, 0, &w_path) > 0) {
  361. handle = FindFirstFileW(w_path, &wfd);
  362. if (handle != INVALID_HANDLE_VALUE) {
  363. do {
  364. struct os_globent ent = {0};
  365. make_globent(&ent, &wfd, pattern);
  366. if (ent.path)
  367. da_push_back(files, &ent);
  368. } while (FindNextFile(handle, &wfd));
  369. FindClose(handle);
  370. *pglob = bmalloc(sizeof(**pglob));
  371. (*pglob)->gl_pathc = files.num;
  372. (*pglob)->gl_pathv = files.array;
  373. ret = 0;
  374. }
  375. bfree(w_path);
  376. }
  377. if (ret != 0)
  378. *pglob = NULL;
  379. UNUSED_PARAMETER(flags);
  380. return ret;
  381. }
  382. void os_globfree(os_glob_t *pglob)
  383. {
  384. if (pglob) {
  385. for (size_t i = 0; i < pglob->gl_pathc; i++)
  386. bfree(pglob->gl_pathv[i].path);
  387. bfree(pglob->gl_pathv);
  388. bfree(pglob);
  389. }
  390. }
  391. int os_unlink(const char *path)
  392. {
  393. wchar_t *w_path;
  394. bool success;
  395. os_utf8_to_wcs_ptr(path, 0, &w_path);
  396. if (!w_path)
  397. return -1;
  398. success = !!DeleteFileW(w_path);
  399. bfree(w_path);
  400. return success ? 0 : -1;
  401. }
  402. int os_rmdir(const char *path)
  403. {
  404. wchar_t *w_path;
  405. bool success;
  406. os_utf8_to_wcs_ptr(path, 0, &w_path);
  407. if (!w_path)
  408. return -1;
  409. success = !!RemoveDirectoryW(w_path);
  410. bfree(w_path);
  411. return success ? 0 : -1;
  412. }
  413. int os_mkdir(const char *path)
  414. {
  415. wchar_t *path_utf16;
  416. BOOL success;
  417. if (!os_utf8_to_wcs_ptr(path, 0, &path_utf16))
  418. return MKDIR_ERROR;
  419. success = CreateDirectory(path_utf16, NULL);
  420. bfree(path_utf16);
  421. if (!success)
  422. return (GetLastError() == ERROR_ALREADY_EXISTS) ?
  423. MKDIR_EXISTS : MKDIR_ERROR;
  424. return MKDIR_SUCCESS;
  425. }
  426. int os_rename(const char *old_path, const char *new_path)
  427. {
  428. wchar_t *old_path_utf16 = NULL;
  429. wchar_t *new_path_utf16 = NULL;
  430. int code = -1;
  431. if (!os_utf8_to_wcs_ptr(old_path, 0, &old_path_utf16)) {
  432. return -1;
  433. }
  434. if (!os_utf8_to_wcs_ptr(new_path, 0, &new_path_utf16)) {
  435. goto error;
  436. }
  437. code = MoveFileW(old_path_utf16, new_path_utf16) ? 0 : -1;
  438. error:
  439. bfree(old_path_utf16);
  440. bfree(new_path_utf16);
  441. return code;
  442. }
  443. BOOL WINAPI DllMain(HINSTANCE hinst_dll, DWORD reason, LPVOID reserved)
  444. {
  445. switch (reason) {
  446. case DLL_PROCESS_ATTACH:
  447. timeBeginPeriod(1);
  448. #ifdef PTW32_STATIC_LIB
  449. pthread_win32_process_attach_np();
  450. #endif
  451. break;
  452. case DLL_PROCESS_DETACH:
  453. timeEndPeriod(1);
  454. #ifdef PTW32_STATIC_LIB
  455. pthread_win32_process_detach_np();
  456. #endif
  457. break;
  458. case DLL_THREAD_ATTACH:
  459. #ifdef PTW32_STATIC_LIB
  460. pthread_win32_thread_attach_np();
  461. #endif
  462. break;
  463. case DLL_THREAD_DETACH:
  464. #ifdef PTW32_STATIC_LIB
  465. pthread_win32_thread_detach_np();
  466. #endif
  467. break;
  468. }
  469. UNUSED_PARAMETER(hinst_dll);
  470. UNUSED_PARAMETER(reserved);
  471. return true;
  472. }
  473. os_performance_token_t *os_request_high_performance(const char *reason)
  474. {
  475. UNUSED_PARAMETER(reason);
  476. return NULL;
  477. }
  478. void os_end_high_performance(os_performance_token_t *token)
  479. {
  480. UNUSED_PARAMETER(token);
  481. }
  482. int os_copyfile(const char *file_in, const char *file_out)
  483. {
  484. wchar_t *file_in_utf16 = NULL;
  485. wchar_t *file_out_utf16 = NULL;
  486. int code = -1;
  487. if (!os_utf8_to_wcs_ptr(file_in, 0, &file_in_utf16)) {
  488. return -1;
  489. }
  490. if (!os_utf8_to_wcs_ptr(file_out, 0, &file_out_utf16)) {
  491. goto error;
  492. }
  493. code = CopyFileW(file_in_utf16, file_out_utf16, true) ? 0 : -1;
  494. error:
  495. bfree(file_in_utf16);
  496. bfree(file_out_utf16);
  497. return code;
  498. }
  499. char *os_getcwd(char *path, size_t size)
  500. {
  501. wchar_t *path_w;
  502. DWORD len;
  503. len = GetCurrentDirectoryW(0, NULL);
  504. if (!len)
  505. return NULL;
  506. path_w = bmalloc((len + 1) * sizeof(wchar_t));
  507. GetCurrentDirectoryW(len + 1, path_w);
  508. os_wcs_to_utf8(path_w, (size_t)len, path, size);
  509. bfree(path_w);
  510. return path;
  511. }
  512. int os_chdir(const char *path)
  513. {
  514. wchar_t *path_w = NULL;
  515. size_t size;
  516. int ret;
  517. size = os_utf8_to_wcs_ptr(path, 0, &path_w);
  518. if (!path_w)
  519. return -1;
  520. ret = SetCurrentDirectoryW(path_w) ? 0 : -1;
  521. bfree(path_w);
  522. return ret;
  523. }
  524. typedef DWORD (WINAPI *get_file_version_info_size_w_t)(
  525. LPCWSTR module,
  526. LPDWORD unused);
  527. typedef BOOL (WINAPI *get_file_version_info_w_t)(
  528. LPCWSTR module,
  529. DWORD unused,
  530. DWORD len,
  531. LPVOID data);
  532. typedef BOOL (WINAPI *ver_query_value_w_t)(
  533. LPVOID data,
  534. LPCWSTR subblock,
  535. LPVOID *buf,
  536. PUINT sizeout);
  537. static get_file_version_info_size_w_t get_file_version_info_size = NULL;
  538. static get_file_version_info_w_t get_file_version_info = NULL;
  539. static ver_query_value_w_t ver_query_value = NULL;
  540. static bool ver_initialized = false;
  541. static bool ver_initialize_success = false;
  542. static bool initialize_version_functions(void)
  543. {
  544. HMODULE ver = GetModuleHandleW(L"version");
  545. ver_initialized = true;
  546. if (!ver) {
  547. ver = LoadLibraryW(L"version");
  548. if (!ver) {
  549. blog(LOG_ERROR, "Failed to load windows "
  550. "version library");
  551. return false;
  552. }
  553. }
  554. get_file_version_info_size = (get_file_version_info_size_w_t)
  555. GetProcAddress(ver, "GetFileVersionInfoSizeW");
  556. get_file_version_info = (get_file_version_info_w_t)
  557. GetProcAddress(ver, "GetFileVersionInfoW");
  558. ver_query_value = (ver_query_value_w_t)
  559. GetProcAddress(ver, "VerQueryValueW");
  560. if (!get_file_version_info_size ||
  561. !get_file_version_info ||
  562. !ver_query_value) {
  563. blog(LOG_ERROR, "Failed to load windows version "
  564. "functions");
  565. return false;
  566. }
  567. ver_initialize_success = true;
  568. return true;
  569. }
  570. bool get_dll_ver(const wchar_t *lib, struct win_version_info *ver_info)
  571. {
  572. VS_FIXEDFILEINFO *info = NULL;
  573. UINT len = 0;
  574. BOOL success;
  575. LPVOID data;
  576. DWORD size;
  577. char utf8_lib[512];
  578. if (!ver_initialized && !initialize_version_functions())
  579. return false;
  580. if (!ver_initialize_success)
  581. return false;
  582. os_wcs_to_utf8(lib, 0, utf8_lib, sizeof(utf8_lib));
  583. size = get_file_version_info_size(lib, NULL);
  584. if (!size) {
  585. blog(LOG_ERROR, "Failed to get %s version info size", utf8_lib);
  586. return false;
  587. }
  588. data = bmalloc(size);
  589. if (!get_file_version_info(lib, 0, size, data)) {
  590. blog(LOG_ERROR, "Failed to get %s version info", utf8_lib);
  591. bfree(data);
  592. return false;
  593. }
  594. success = ver_query_value(data, L"\\", (LPVOID*)&info, &len);
  595. if (!success || !info || !len) {
  596. blog(LOG_ERROR, "Failed to get %s version info value", utf8_lib);
  597. bfree(data);
  598. return false;
  599. }
  600. ver_info->major = (int)HIWORD(info->dwFileVersionMS);
  601. ver_info->minor = (int)LOWORD(info->dwFileVersionMS);
  602. ver_info->build = (int)HIWORD(info->dwFileVersionLS);
  603. ver_info->revis = (int)LOWORD(info->dwFileVersionLS);
  604. bfree(data);
  605. return true;
  606. }
  607. bool is_64_bit_windows(void)
  608. {
  609. #if defined(_WIN64)
  610. return true;
  611. #elif defined(_WIN32)
  612. BOOL b64 = false;
  613. return IsWow64Process(GetCurrentProcess(), &b64) && b64;
  614. #endif
  615. }
  616. #define WINVER_REG_KEY L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion"
  617. void get_win_ver(struct win_version_info *info)
  618. {
  619. static struct win_version_info ver = {0};
  620. static bool got_version = false;
  621. if (!info)
  622. return;
  623. if (!got_version) {
  624. get_dll_ver(L"kernel32", &ver);
  625. got_version = true;
  626. if (ver.major == 10 && ver.revis == 0) {
  627. HKEY key;
  628. DWORD size, win10_revision;
  629. LSTATUS status;
  630. status = RegOpenKeyW(HKEY_LOCAL_MACHINE,
  631. WINVER_REG_KEY, &key);
  632. if (status != ERROR_SUCCESS)
  633. return;
  634. size = sizeof(win10_revision);
  635. status = RegQueryValueExW(key, L"UBR", NULL, NULL,
  636. (LPBYTE)&win10_revision, &size);
  637. if (status == ERROR_SUCCESS)
  638. ver.revis = (int)win10_revision;
  639. RegCloseKey(key);
  640. }
  641. }
  642. *info = ver;
  643. }
  644. struct os_inhibit_info {
  645. bool active;
  646. };
  647. os_inhibit_t *os_inhibit_sleep_create(const char *reason)
  648. {
  649. UNUSED_PARAMETER(reason);
  650. return bzalloc(sizeof(struct os_inhibit_info));
  651. }
  652. bool os_inhibit_sleep_set_active(os_inhibit_t *info, bool active)
  653. {
  654. if (!info)
  655. return false;
  656. if (info->active == active)
  657. return false;
  658. if (active) {
  659. SetThreadExecutionState(
  660. ES_CONTINUOUS |
  661. ES_SYSTEM_REQUIRED |
  662. ES_AWAYMODE_REQUIRED |
  663. ES_DISPLAY_REQUIRED);
  664. } else {
  665. SetThreadExecutionState(ES_CONTINUOUS);
  666. }
  667. info->active = active;
  668. return true;
  669. }
  670. void os_inhibit_sleep_destroy(os_inhibit_t *info)
  671. {
  672. if (info) {
  673. os_inhibit_sleep_set_active(info, false);
  674. bfree(info);
  675. }
  676. }
  677. void os_breakpoint(void)
  678. {
  679. __debugbreak();
  680. }