platform-windows.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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: %u",
  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. char *os_get_config_path(const char *name)
  155. {
  156. char *ptr;
  157. wchar_t path_utf16[MAX_PATH];
  158. struct dstr path;
  159. SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT,
  160. path_utf16);
  161. os_wcs_to_utf8_ptr(path_utf16, 0, &ptr);
  162. dstr_init_move_array(&path, ptr);
  163. dstr_cat(&path, "\\");
  164. dstr_cat(&path, name);
  165. return path.array;
  166. }
  167. bool os_file_exists(const char *path)
  168. {
  169. WIN32_FIND_DATAW wfd;
  170. HANDLE hFind;
  171. wchar_t *path_utf16;
  172. if (!os_utf8_to_wcs_ptr(path, 0, &path_utf16))
  173. return false;
  174. hFind = FindFirstFileW(path_utf16, &wfd);
  175. if (hFind != INVALID_HANDLE_VALUE)
  176. FindClose(hFind);
  177. bfree(path_utf16);
  178. return hFind != INVALID_HANDLE_VALUE;
  179. }
  180. struct os_dir {
  181. HANDLE handle;
  182. WIN32_FIND_DATA wfd;
  183. bool first;
  184. struct os_dirent out;
  185. };
  186. os_dir_t *os_opendir(const char *path)
  187. {
  188. struct dstr path_str = {0};
  189. struct os_dir *dir = NULL;
  190. WIN32_FIND_DATA wfd;
  191. HANDLE handle;
  192. wchar_t *w_path;
  193. dstr_copy(&path_str, path);
  194. dstr_cat(&path_str, "/*.*");
  195. if (os_utf8_to_wcs_ptr(path_str.array, path_str.len, &w_path) > 0) {
  196. handle = FindFirstFileW(w_path, &wfd);
  197. if (handle != INVALID_HANDLE_VALUE) {
  198. dir = bzalloc(sizeof(struct os_dir));
  199. dir->handle = handle;
  200. dir->first = true;
  201. dir->wfd = wfd;
  202. }
  203. bfree(w_path);
  204. }
  205. dstr_free(&path_str);
  206. return dir;
  207. }
  208. static inline bool is_dir(WIN32_FIND_DATA *wfd)
  209. {
  210. return !!(wfd->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
  211. }
  212. struct os_dirent *os_readdir(os_dir_t *dir)
  213. {
  214. if (!dir)
  215. return NULL;
  216. if (dir->first) {
  217. dir->first = false;
  218. } else {
  219. if (!FindNextFileW(dir->handle, &dir->wfd))
  220. return NULL;
  221. }
  222. os_wcs_to_utf8(dir->wfd.cFileName, 0, dir->out.d_name,
  223. sizeof(dir->out.d_name));
  224. dir->out.directory = is_dir(&dir->wfd);
  225. return &dir->out;
  226. }
  227. void os_closedir(os_dir_t *dir)
  228. {
  229. if (dir) {
  230. FindClose(dir->handle);
  231. bfree(dir);
  232. }
  233. }
  234. static void make_globent(struct os_globent *ent, WIN32_FIND_DATA *wfd,
  235. const char *pattern)
  236. {
  237. struct dstr name = {0};
  238. struct dstr path = {0};
  239. char *slash;
  240. dstr_from_wcs(&name, wfd->cFileName);
  241. dstr_copy(&path, pattern);
  242. slash = strrchr(path.array, '/');
  243. if (slash)
  244. dstr_resize(&path, slash + 1 - path.array);
  245. else
  246. dstr_free(&path);
  247. dstr_cat_dstr(&path, &name);
  248. ent->path = path.array;
  249. ent->directory = is_dir(wfd);
  250. dstr_free(&name);
  251. }
  252. int os_glob(const char *pattern, int flags, os_glob_t **pglob)
  253. {
  254. DARRAY(struct os_globent) files;
  255. HANDLE handle;
  256. WIN32_FIND_DATA wfd;
  257. int ret = -1;
  258. os_glob_t *out = NULL;
  259. wchar_t *w_path;
  260. da_init(files);
  261. if (os_utf8_to_wcs_ptr(pattern, 0, &w_path) > 0) {
  262. handle = FindFirstFileW(w_path, &wfd);
  263. if (handle != INVALID_HANDLE_VALUE) {
  264. do {
  265. struct os_globent ent = {0};
  266. make_globent(&ent, &wfd, pattern);
  267. if (ent.path)
  268. da_push_back(files, &ent);
  269. } while (FindNextFile(handle, &wfd));
  270. FindClose(handle);
  271. *pglob = bmalloc(sizeof(**pglob));
  272. (*pglob)->gl_pathc = files.num;
  273. (*pglob)->gl_pathv = files.array;
  274. ret = 0;
  275. }
  276. bfree(w_path);
  277. }
  278. if (ret != 0)
  279. *pglob = NULL;
  280. UNUSED_PARAMETER(flags);
  281. return ret;
  282. }
  283. void os_globfree(os_glob_t *pglob)
  284. {
  285. if (pglob) {
  286. for (size_t i = 0; i < pglob->gl_pathc; i++)
  287. bfree(pglob->gl_pathv[i].path);
  288. bfree(pglob->gl_pathv);
  289. bfree(pglob);
  290. }
  291. }
  292. int os_unlink(const char *path)
  293. {
  294. wchar_t *w_path;
  295. bool success;
  296. os_utf8_to_wcs_ptr(path, 0, &w_path);
  297. if (!w_path)
  298. return -1;
  299. success = !!DeleteFileW(w_path);
  300. bfree(w_path);
  301. return success ? 0 : -1;
  302. }
  303. int os_mkdir(const char *path)
  304. {
  305. wchar_t *path_utf16;
  306. BOOL success;
  307. if (!os_utf8_to_wcs_ptr(path, 0, &path_utf16))
  308. return MKDIR_ERROR;
  309. success = CreateDirectory(path_utf16, NULL);
  310. bfree(path_utf16);
  311. if (!success)
  312. return (GetLastError() == ERROR_ALREADY_EXISTS) ?
  313. MKDIR_EXISTS : MKDIR_ERROR;
  314. return MKDIR_SUCCESS;
  315. }
  316. BOOL WINAPI DllMain(HINSTANCE hinst_dll, DWORD reason, LPVOID reserved)
  317. {
  318. switch (reason) {
  319. case DLL_PROCESS_ATTACH:
  320. timeBeginPeriod(1);
  321. #ifdef PTW32_STATIC_LIB
  322. pthread_win32_process_attach_np();
  323. #endif
  324. break;
  325. case DLL_PROCESS_DETACH:
  326. timeEndPeriod(1);
  327. #ifdef PTW32_STATIC_LIB
  328. pthread_win32_process_detach_np();
  329. #endif
  330. break;
  331. case DLL_THREAD_ATTACH:
  332. #ifdef PTW32_STATIC_LIB
  333. pthread_win32_thread_attach_np();
  334. #endif
  335. break;
  336. case DLL_THREAD_DETACH:
  337. #ifdef PTW32_STATIC_LIB
  338. pthread_win32_thread_detach_np();
  339. #endif
  340. break;
  341. }
  342. UNUSED_PARAMETER(hinst_dll);
  343. UNUSED_PARAMETER(reserved);
  344. return true;
  345. }
  346. os_performance_token_t *os_request_high_performance(const char *reason)
  347. {
  348. UNUSED_PARAMETER(reason);
  349. return NULL;
  350. }
  351. void os_end_high_performance(os_performance_token_t *token)
  352. {
  353. UNUSED_PARAMETER(token);
  354. }