platform-windows.c 15 KB

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