platform-windows.c 16 KB

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