platform-windows.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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 "../../deps/w32-pthreads/pthread.h"
  25. static bool have_clockfreq = false;
  26. static LARGE_INTEGER clock_freq;
  27. static uint32_t winver = 0;
  28. static inline uint64_t get_clockfreq(void)
  29. {
  30. if (!have_clockfreq)
  31. QueryPerformanceFrequency(&clock_freq);
  32. return clock_freq.QuadPart;
  33. }
  34. static inline uint32_t get_winver(void)
  35. {
  36. if (!winver) {
  37. OSVERSIONINFO osvi;
  38. memset(&osvi, 0, sizeof(osvi));
  39. winver = (osvi.dwMajorVersion << 16) | (osvi.dwMinorVersion);
  40. }
  41. return winver;
  42. }
  43. void *os_dlopen(const char *path)
  44. {
  45. struct dstr dll_name;
  46. wchar_t *wpath;
  47. HMODULE h_library = NULL;
  48. if (!path)
  49. return NULL;
  50. dstr_init_copy(&dll_name, path);
  51. if (!dstr_find(&dll_name, ".dll"))
  52. dstr_cat(&dll_name, ".dll");
  53. os_utf8_to_wcs_ptr(dll_name.array, 0, &wpath);
  54. h_library = LoadLibraryW(wpath);
  55. bfree(wpath);
  56. dstr_free(&dll_name);
  57. if (!h_library)
  58. blog(LOG_INFO, "LoadLibrary failed for '%s', error: %ld",
  59. path, GetLastError());
  60. return h_library;
  61. }
  62. void *os_dlsym(void *module, const char *func)
  63. {
  64. void *handle;
  65. handle = (void*)GetProcAddress(module, func);
  66. return handle;
  67. }
  68. void os_dlclose(void *module)
  69. {
  70. FreeLibrary(module);
  71. }
  72. union time_data {
  73. FILETIME ft;
  74. unsigned long long val;
  75. };
  76. struct os_cpu_usage_info {
  77. union time_data last_time, last_sys_time, last_user_time;
  78. DWORD core_count;
  79. };
  80. os_cpu_usage_info_t *os_cpu_usage_info_start(void)
  81. {
  82. struct os_cpu_usage_info *info = bzalloc(sizeof(*info));
  83. SYSTEM_INFO si;
  84. FILETIME dummy;
  85. GetSystemInfo(&si);
  86. GetSystemTimeAsFileTime(&info->last_time.ft);
  87. GetProcessTimes(GetCurrentProcess(), &dummy, &dummy,
  88. &info->last_sys_time.ft, &info->last_user_time.ft);
  89. info->core_count = si.dwNumberOfProcessors;
  90. return info;
  91. }
  92. double os_cpu_usage_info_query(os_cpu_usage_info_t *info)
  93. {
  94. union time_data cur_time, cur_sys_time, cur_user_time;
  95. FILETIME dummy;
  96. double percent;
  97. if (!info)
  98. return 0.0;
  99. GetSystemTimeAsFileTime(&cur_time.ft);
  100. GetProcessTimes(GetCurrentProcess(), &dummy, &dummy,
  101. &cur_sys_time.ft, &cur_user_time.ft);
  102. percent = (double)(cur_sys_time.val - info->last_sys_time.val +
  103. (cur_user_time.val - info->last_user_time.val));
  104. percent /= (double)(cur_time.val - info->last_time.val);
  105. percent /= (double)info->core_count;
  106. info->last_time.val = cur_time.val;
  107. info->last_sys_time.val = cur_sys_time.val;
  108. info->last_user_time.val = cur_user_time.val;
  109. return percent * 100.0;
  110. }
  111. void os_cpu_usage_info_destroy(os_cpu_usage_info_t *info)
  112. {
  113. if (info)
  114. bfree(info);
  115. }
  116. bool os_sleepto_ns(uint64_t time_target)
  117. {
  118. uint64_t t = os_gettime_ns();
  119. uint32_t milliseconds;
  120. if (t >= time_target)
  121. return false;
  122. milliseconds = (uint32_t)((time_target - t)/1000000);
  123. if (milliseconds > 1)
  124. Sleep(milliseconds-1);
  125. for (;;) {
  126. t = os_gettime_ns();
  127. if (t >= time_target)
  128. return true;
  129. #if 1
  130. Sleep(1);
  131. #else
  132. Sleep(0);
  133. #endif
  134. }
  135. }
  136. void os_sleep_ms(uint32_t duration)
  137. {
  138. /* windows 8+ appears to have decreased sleep precision */
  139. if (get_winver() >= 0x0602 && duration > 0)
  140. duration--;
  141. Sleep(duration);
  142. }
  143. uint64_t os_gettime_ns(void)
  144. {
  145. LARGE_INTEGER current_time;
  146. double time_val;
  147. QueryPerformanceCounter(&current_time);
  148. time_val = (double)current_time.QuadPart;
  149. time_val *= 1000000000.0;
  150. time_val /= (double)get_clockfreq();
  151. return (uint64_t)time_val;
  152. }
  153. /* returns %appdata%\[name] on windows */
  154. int os_get_config_path(char *dst, size_t size, const char *name)
  155. {
  156. wchar_t path_utf16[MAX_PATH];
  157. SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT,
  158. path_utf16);
  159. if (os_wcs_to_utf8(path_utf16, 0, dst, size) != 0) {
  160. if (strcat_s(dst, size, "\\") == 0) {
  161. if (strcat_s(dst, size, name) == 0) {
  162. return (int)strlen(dst);
  163. }
  164. }
  165. }
  166. return -1;
  167. }
  168. char *os_get_config_path_ptr(const char *name)
  169. {
  170. char *ptr;
  171. wchar_t path_utf16[MAX_PATH];
  172. struct dstr path;
  173. SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT,
  174. path_utf16);
  175. os_wcs_to_utf8_ptr(path_utf16, 0, &ptr);
  176. dstr_init_move_array(&path, ptr);
  177. dstr_cat(&path, "\\");
  178. dstr_cat(&path, name);
  179. return path.array;
  180. }
  181. bool os_file_exists(const char *path)
  182. {
  183. WIN32_FIND_DATAW wfd;
  184. HANDLE hFind;
  185. wchar_t *path_utf16;
  186. if (!os_utf8_to_wcs_ptr(path, 0, &path_utf16))
  187. return false;
  188. hFind = FindFirstFileW(path_utf16, &wfd);
  189. if (hFind != INVALID_HANDLE_VALUE)
  190. FindClose(hFind);
  191. bfree(path_utf16);
  192. return hFind != INVALID_HANDLE_VALUE;
  193. }
  194. struct os_dir {
  195. HANDLE handle;
  196. WIN32_FIND_DATA wfd;
  197. bool first;
  198. struct os_dirent out;
  199. };
  200. os_dir_t *os_opendir(const char *path)
  201. {
  202. struct dstr path_str = {0};
  203. struct os_dir *dir = NULL;
  204. WIN32_FIND_DATA wfd;
  205. HANDLE handle;
  206. wchar_t *w_path;
  207. dstr_copy(&path_str, path);
  208. dstr_cat(&path_str, "/*.*");
  209. if (os_utf8_to_wcs_ptr(path_str.array, path_str.len, &w_path) > 0) {
  210. handle = FindFirstFileW(w_path, &wfd);
  211. if (handle != INVALID_HANDLE_VALUE) {
  212. dir = bzalloc(sizeof(struct os_dir));
  213. dir->handle = handle;
  214. dir->first = true;
  215. dir->wfd = wfd;
  216. }
  217. bfree(w_path);
  218. }
  219. dstr_free(&path_str);
  220. return dir;
  221. }
  222. static inline bool is_dir(WIN32_FIND_DATA *wfd)
  223. {
  224. return !!(wfd->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
  225. }
  226. struct os_dirent *os_readdir(os_dir_t *dir)
  227. {
  228. if (!dir)
  229. return NULL;
  230. if (dir->first) {
  231. dir->first = false;
  232. } else {
  233. if (!FindNextFileW(dir->handle, &dir->wfd))
  234. return NULL;
  235. }
  236. os_wcs_to_utf8(dir->wfd.cFileName, 0, dir->out.d_name,
  237. sizeof(dir->out.d_name));
  238. dir->out.directory = is_dir(&dir->wfd);
  239. return &dir->out;
  240. }
  241. void os_closedir(os_dir_t *dir)
  242. {
  243. if (dir) {
  244. FindClose(dir->handle);
  245. bfree(dir);
  246. }
  247. }
  248. static void make_globent(struct os_globent *ent, WIN32_FIND_DATA *wfd,
  249. const char *pattern)
  250. {
  251. struct dstr name = {0};
  252. struct dstr path = {0};
  253. char *slash;
  254. dstr_from_wcs(&name, wfd->cFileName);
  255. dstr_copy(&path, pattern);
  256. slash = strrchr(path.array, '/');
  257. if (slash)
  258. dstr_resize(&path, slash + 1 - path.array);
  259. else
  260. dstr_free(&path);
  261. dstr_cat_dstr(&path, &name);
  262. ent->path = path.array;
  263. ent->directory = is_dir(wfd);
  264. dstr_free(&name);
  265. }
  266. int os_glob(const char *pattern, int flags, os_glob_t **pglob)
  267. {
  268. DARRAY(struct os_globent) files;
  269. HANDLE handle;
  270. WIN32_FIND_DATA wfd;
  271. int ret = -1;
  272. wchar_t *w_path;
  273. da_init(files);
  274. if (os_utf8_to_wcs_ptr(pattern, 0, &w_path) > 0) {
  275. handle = FindFirstFileW(w_path, &wfd);
  276. if (handle != INVALID_HANDLE_VALUE) {
  277. do {
  278. struct os_globent ent = {0};
  279. make_globent(&ent, &wfd, pattern);
  280. if (ent.path)
  281. da_push_back(files, &ent);
  282. } while (FindNextFile(handle, &wfd));
  283. FindClose(handle);
  284. *pglob = bmalloc(sizeof(**pglob));
  285. (*pglob)->gl_pathc = files.num;
  286. (*pglob)->gl_pathv = files.array;
  287. ret = 0;
  288. }
  289. bfree(w_path);
  290. }
  291. if (ret != 0)
  292. *pglob = NULL;
  293. UNUSED_PARAMETER(flags);
  294. return ret;
  295. }
  296. void os_globfree(os_glob_t *pglob)
  297. {
  298. if (pglob) {
  299. for (size_t i = 0; i < pglob->gl_pathc; i++)
  300. bfree(pglob->gl_pathv[i].path);
  301. bfree(pglob->gl_pathv);
  302. bfree(pglob);
  303. }
  304. }
  305. int os_unlink(const char *path)
  306. {
  307. wchar_t *w_path;
  308. bool success;
  309. os_utf8_to_wcs_ptr(path, 0, &w_path);
  310. if (!w_path)
  311. return -1;
  312. success = !!DeleteFileW(w_path);
  313. bfree(w_path);
  314. return success ? 0 : -1;
  315. }
  316. int os_mkdir(const char *path)
  317. {
  318. wchar_t *path_utf16;
  319. BOOL success;
  320. if (!os_utf8_to_wcs_ptr(path, 0, &path_utf16))
  321. return MKDIR_ERROR;
  322. success = CreateDirectory(path_utf16, NULL);
  323. bfree(path_utf16);
  324. if (!success)
  325. return (GetLastError() == ERROR_ALREADY_EXISTS) ?
  326. MKDIR_EXISTS : MKDIR_ERROR;
  327. return MKDIR_SUCCESS;
  328. }
  329. BOOL WINAPI DllMain(HINSTANCE hinst_dll, DWORD reason, LPVOID reserved)
  330. {
  331. switch (reason) {
  332. case DLL_PROCESS_ATTACH:
  333. timeBeginPeriod(1);
  334. #ifdef PTW32_STATIC_LIB
  335. pthread_win32_process_attach_np();
  336. #endif
  337. break;
  338. case DLL_PROCESS_DETACH:
  339. timeEndPeriod(1);
  340. #ifdef PTW32_STATIC_LIB
  341. pthread_win32_process_detach_np();
  342. #endif
  343. break;
  344. case DLL_THREAD_ATTACH:
  345. #ifdef PTW32_STATIC_LIB
  346. pthread_win32_thread_attach_np();
  347. #endif
  348. break;
  349. case DLL_THREAD_DETACH:
  350. #ifdef PTW32_STATIC_LIB
  351. pthread_win32_thread_detach_np();
  352. #endif
  353. break;
  354. }
  355. UNUSED_PARAMETER(hinst_dll);
  356. UNUSED_PARAMETER(reserved);
  357. return true;
  358. }
  359. os_performance_token_t *os_request_high_performance(const char *reason)
  360. {
  361. UNUSED_PARAMETER(reason);
  362. return NULL;
  363. }
  364. void os_end_high_performance(os_performance_token_t *token)
  365. {
  366. UNUSED_PARAMETER(token);
  367. }