platform-windows.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  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 = MoveFileExW(old_path_utf16, new_path_utf16,
  438. MOVEFILE_REPLACE_EXISTING) ? 0 : -1;
  439. error:
  440. bfree(old_path_utf16);
  441. bfree(new_path_utf16);
  442. return code;
  443. }
  444. int os_safe_replace(const char *target, const char *from, const char *backup)
  445. {
  446. wchar_t *wtarget = NULL;
  447. wchar_t *wfrom = NULL;
  448. wchar_t *wbackup = NULL;
  449. int code = -1;
  450. if (!target || !from)
  451. return -1;
  452. if (!os_utf8_to_wcs_ptr(target, 0, &wtarget))
  453. return -1;
  454. if (!os_utf8_to_wcs_ptr(from, 0, &wfrom))
  455. goto fail;
  456. if (backup && !os_utf8_to_wcs_ptr(backup, 0, &wbackup))
  457. goto fail;
  458. if (ReplaceFileW(wtarget, wfrom, wbackup, 0, NULL, NULL)) {
  459. code = 0;
  460. } else if (GetLastError() == ERROR_FILE_NOT_FOUND) {
  461. code = MoveFileExW(wfrom, wtarget, MOVEFILE_REPLACE_EXISTING)
  462. ? 0 : -1;
  463. }
  464. fail:
  465. bfree(wtarget);
  466. bfree(wfrom);
  467. bfree(wbackup);
  468. return code;
  469. }
  470. BOOL WINAPI DllMain(HINSTANCE hinst_dll, DWORD reason, LPVOID reserved)
  471. {
  472. switch (reason) {
  473. case DLL_PROCESS_ATTACH:
  474. timeBeginPeriod(1);
  475. #ifdef PTW32_STATIC_LIB
  476. pthread_win32_process_attach_np();
  477. #endif
  478. break;
  479. case DLL_PROCESS_DETACH:
  480. timeEndPeriod(1);
  481. #ifdef PTW32_STATIC_LIB
  482. pthread_win32_process_detach_np();
  483. #endif
  484. break;
  485. case DLL_THREAD_ATTACH:
  486. #ifdef PTW32_STATIC_LIB
  487. pthread_win32_thread_attach_np();
  488. #endif
  489. break;
  490. case DLL_THREAD_DETACH:
  491. #ifdef PTW32_STATIC_LIB
  492. pthread_win32_thread_detach_np();
  493. #endif
  494. break;
  495. }
  496. UNUSED_PARAMETER(hinst_dll);
  497. UNUSED_PARAMETER(reserved);
  498. return true;
  499. }
  500. os_performance_token_t *os_request_high_performance(const char *reason)
  501. {
  502. UNUSED_PARAMETER(reason);
  503. return NULL;
  504. }
  505. void os_end_high_performance(os_performance_token_t *token)
  506. {
  507. UNUSED_PARAMETER(token);
  508. }
  509. int os_copyfile(const char *file_in, const char *file_out)
  510. {
  511. wchar_t *file_in_utf16 = NULL;
  512. wchar_t *file_out_utf16 = NULL;
  513. int code = -1;
  514. if (!os_utf8_to_wcs_ptr(file_in, 0, &file_in_utf16)) {
  515. return -1;
  516. }
  517. if (!os_utf8_to_wcs_ptr(file_out, 0, &file_out_utf16)) {
  518. goto error;
  519. }
  520. code = CopyFileW(file_in_utf16, file_out_utf16, true) ? 0 : -1;
  521. error:
  522. bfree(file_in_utf16);
  523. bfree(file_out_utf16);
  524. return code;
  525. }
  526. char *os_getcwd(char *path, size_t size)
  527. {
  528. wchar_t *path_w;
  529. DWORD len;
  530. len = GetCurrentDirectoryW(0, NULL);
  531. if (!len)
  532. return NULL;
  533. path_w = bmalloc((len + 1) * sizeof(wchar_t));
  534. GetCurrentDirectoryW(len + 1, path_w);
  535. os_wcs_to_utf8(path_w, (size_t)len, path, size);
  536. bfree(path_w);
  537. return path;
  538. }
  539. int os_chdir(const char *path)
  540. {
  541. wchar_t *path_w = NULL;
  542. size_t size;
  543. int ret;
  544. size = os_utf8_to_wcs_ptr(path, 0, &path_w);
  545. if (!path_w)
  546. return -1;
  547. ret = SetCurrentDirectoryW(path_w) ? 0 : -1;
  548. bfree(path_w);
  549. return ret;
  550. }
  551. typedef DWORD (WINAPI *get_file_version_info_size_w_t)(
  552. LPCWSTR module,
  553. LPDWORD unused);
  554. typedef BOOL (WINAPI *get_file_version_info_w_t)(
  555. LPCWSTR module,
  556. DWORD unused,
  557. DWORD len,
  558. LPVOID data);
  559. typedef BOOL (WINAPI *ver_query_value_w_t)(
  560. LPVOID data,
  561. LPCWSTR subblock,
  562. LPVOID *buf,
  563. PUINT sizeout);
  564. static get_file_version_info_size_w_t get_file_version_info_size = NULL;
  565. static get_file_version_info_w_t get_file_version_info = NULL;
  566. static ver_query_value_w_t ver_query_value = NULL;
  567. static bool ver_initialized = false;
  568. static bool ver_initialize_success = false;
  569. static bool initialize_version_functions(void)
  570. {
  571. HMODULE ver = GetModuleHandleW(L"version");
  572. ver_initialized = true;
  573. if (!ver) {
  574. ver = LoadLibraryW(L"version");
  575. if (!ver) {
  576. blog(LOG_ERROR, "Failed to load windows "
  577. "version library");
  578. return false;
  579. }
  580. }
  581. get_file_version_info_size = (get_file_version_info_size_w_t)
  582. GetProcAddress(ver, "GetFileVersionInfoSizeW");
  583. get_file_version_info = (get_file_version_info_w_t)
  584. GetProcAddress(ver, "GetFileVersionInfoW");
  585. ver_query_value = (ver_query_value_w_t)
  586. GetProcAddress(ver, "VerQueryValueW");
  587. if (!get_file_version_info_size ||
  588. !get_file_version_info ||
  589. !ver_query_value) {
  590. blog(LOG_ERROR, "Failed to load windows version "
  591. "functions");
  592. return false;
  593. }
  594. ver_initialize_success = true;
  595. return true;
  596. }
  597. bool get_dll_ver(const wchar_t *lib, struct win_version_info *ver_info)
  598. {
  599. VS_FIXEDFILEINFO *info = NULL;
  600. UINT len = 0;
  601. BOOL success;
  602. LPVOID data;
  603. DWORD size;
  604. char utf8_lib[512];
  605. if (!ver_initialized && !initialize_version_functions())
  606. return false;
  607. if (!ver_initialize_success)
  608. return false;
  609. os_wcs_to_utf8(lib, 0, utf8_lib, sizeof(utf8_lib));
  610. size = get_file_version_info_size(lib, NULL);
  611. if (!size) {
  612. blog(LOG_ERROR, "Failed to get %s version info size", utf8_lib);
  613. return false;
  614. }
  615. data = bmalloc(size);
  616. if (!get_file_version_info(lib, 0, size, data)) {
  617. blog(LOG_ERROR, "Failed to get %s version info", utf8_lib);
  618. bfree(data);
  619. return false;
  620. }
  621. success = ver_query_value(data, L"\\", (LPVOID*)&info, &len);
  622. if (!success || !info || !len) {
  623. blog(LOG_ERROR, "Failed to get %s version info value", utf8_lib);
  624. bfree(data);
  625. return false;
  626. }
  627. ver_info->major = (int)HIWORD(info->dwFileVersionMS);
  628. ver_info->minor = (int)LOWORD(info->dwFileVersionMS);
  629. ver_info->build = (int)HIWORD(info->dwFileVersionLS);
  630. ver_info->revis = (int)LOWORD(info->dwFileVersionLS);
  631. bfree(data);
  632. return true;
  633. }
  634. bool is_64_bit_windows(void)
  635. {
  636. #if defined(_WIN64)
  637. return true;
  638. #elif defined(_WIN32)
  639. BOOL b64 = false;
  640. return IsWow64Process(GetCurrentProcess(), &b64) && b64;
  641. #endif
  642. }
  643. #define WINVER_REG_KEY L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion"
  644. void get_win_ver(struct win_version_info *info)
  645. {
  646. static struct win_version_info ver = {0};
  647. static bool got_version = false;
  648. if (!info)
  649. return;
  650. if (!got_version) {
  651. get_dll_ver(L"kernel32", &ver);
  652. got_version = true;
  653. if (ver.major == 10 && ver.revis == 0) {
  654. HKEY key;
  655. DWORD size, win10_revision;
  656. LSTATUS status;
  657. status = RegOpenKeyW(HKEY_LOCAL_MACHINE,
  658. WINVER_REG_KEY, &key);
  659. if (status != ERROR_SUCCESS)
  660. return;
  661. size = sizeof(win10_revision);
  662. status = RegQueryValueExW(key, L"UBR", NULL, NULL,
  663. (LPBYTE)&win10_revision, &size);
  664. if (status == ERROR_SUCCESS)
  665. ver.revis = (int)win10_revision;
  666. RegCloseKey(key);
  667. }
  668. }
  669. *info = ver;
  670. }
  671. struct os_inhibit_info {
  672. bool active;
  673. };
  674. os_inhibit_t *os_inhibit_sleep_create(const char *reason)
  675. {
  676. UNUSED_PARAMETER(reason);
  677. return bzalloc(sizeof(struct os_inhibit_info));
  678. }
  679. bool os_inhibit_sleep_set_active(os_inhibit_t *info, bool active)
  680. {
  681. if (!info)
  682. return false;
  683. if (info->active == active)
  684. return false;
  685. if (active) {
  686. SetThreadExecutionState(
  687. ES_CONTINUOUS |
  688. ES_SYSTEM_REQUIRED |
  689. ES_AWAYMODE_REQUIRED |
  690. ES_DISPLAY_REQUIRED);
  691. } else {
  692. SetThreadExecutionState(ES_CONTINUOUS);
  693. }
  694. info->active = active;
  695. return true;
  696. }
  697. void os_inhibit_sleep_destroy(os_inhibit_t *info)
  698. {
  699. if (info) {
  700. os_inhibit_sleep_set_active(info, false);
  701. bfree(info);
  702. }
  703. }
  704. void os_breakpoint(void)
  705. {
  706. __debugbreak();
  707. }
  708. DWORD num_logical_cores(ULONG_PTR mask)
  709. {
  710. DWORD left_shift = sizeof(ULONG_PTR) * 8 - 1;
  711. DWORD bit_set_count = 0;
  712. ULONG_PTR bit_test = (ULONG_PTR)1 << left_shift;
  713. for (DWORD i = 0; i <= left_shift; ++i) {
  714. bit_set_count += ((mask & bit_test) ? 1 : 0);
  715. bit_test /= 2;
  716. }
  717. return bit_set_count;
  718. }
  719. static int physical_cores = 0;
  720. static int logical_cores = 0;
  721. static bool core_count_initialized = false;
  722. static void os_get_cores_internal(void)
  723. {
  724. PSYSTEM_LOGICAL_PROCESSOR_INFORMATION info = NULL, temp = NULL;
  725. DWORD len = 0;
  726. if (core_count_initialized)
  727. return;
  728. core_count_initialized = true;
  729. GetLogicalProcessorInformation(info, &len);
  730. if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
  731. return;
  732. info = malloc(len);
  733. if (GetLogicalProcessorInformation(info, &len)) {
  734. DWORD num = len / sizeof(*info);
  735. temp = info;
  736. for (DWORD i = 0; i < num; i++) {
  737. if (temp->Relationship == RelationProcessorCore) {
  738. ULONG_PTR mask = temp->ProcessorMask;
  739. physical_cores++;
  740. logical_cores += num_logical_cores(mask);
  741. }
  742. temp++;
  743. }
  744. }
  745. free(info);
  746. }
  747. int os_get_physical_cores(void)
  748. {
  749. if (!core_count_initialized)
  750. os_get_cores_internal();
  751. return physical_cores;
  752. }
  753. int os_get_logical_cores(void)
  754. {
  755. if (!core_count_initialized)
  756. os_get_cores_internal();
  757. return logical_cores;
  758. }
  759. uint64_t os_get_free_disk_space(const char *dir)
  760. {
  761. wchar_t *wdir = NULL;
  762. if (!os_utf8_to_wcs_ptr(dir, 0, &wdir))
  763. return 0;
  764. ULARGE_INTEGER free;
  765. bool success = !!GetDiskFreeSpaceExW(wdir, &free, NULL, NULL);
  766. bfree(wdir);
  767. return success ? free.QuadPart : 0;
  768. }