platform-windows.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 "dstr.h"
  23. #include "../../deps/w32-pthreads/pthread.h"
  24. static bool have_clockfreq = false;
  25. static LARGE_INTEGER clock_freq;
  26. static uint32_t winver = 0;
  27. static inline uint64_t get_clockfreq(void)
  28. {
  29. if (!have_clockfreq)
  30. QueryPerformanceFrequency(&clock_freq);
  31. return clock_freq.QuadPart;
  32. }
  33. static inline uint32_t get_winver(void)
  34. {
  35. if (!winver) {
  36. OSVERSIONINFO osvi;
  37. memset(&osvi, 0, sizeof(osvi));
  38. winver = (osvi.dwMajorVersion << 16) | (osvi.dwMinorVersion);
  39. }
  40. return winver;
  41. }
  42. void *os_dlopen(const char *path)
  43. {
  44. struct dstr dll_name;
  45. wchar_t *wpath;
  46. HMODULE h_library = NULL;
  47. if (!path)
  48. return NULL;
  49. dstr_init_copy(&dll_name, path);
  50. if (!dstr_find(&dll_name, ".dll"))
  51. dstr_cat(&dll_name, ".dll");
  52. os_utf8_to_wcs_ptr(dll_name.array, 0, &wpath);
  53. h_library = LoadLibraryW(wpath);
  54. bfree(wpath);
  55. dstr_free(&dll_name);
  56. if (!h_library)
  57. blog(LOG_INFO, "LoadLibrary failed for '%s', error: %u",
  58. path, GetLastError());
  59. return h_library;
  60. }
  61. void *os_dlsym(void *module, const char *func)
  62. {
  63. void *handle;
  64. handle = (void*)GetProcAddress(module, func);
  65. return handle;
  66. }
  67. void os_dlclose(void *module)
  68. {
  69. FreeLibrary(module);
  70. }
  71. bool os_sleepto_ns(uint64_t time_target)
  72. {
  73. uint64_t t = os_gettime_ns();
  74. uint32_t milliseconds;
  75. if (t >= time_target)
  76. return false;
  77. milliseconds = (uint32_t)((time_target - t)/1000000);
  78. if (milliseconds > 1)
  79. Sleep(milliseconds-1);
  80. for (;;) {
  81. t = os_gettime_ns();
  82. if (t >= time_target)
  83. return true;
  84. #if 1
  85. Sleep(1);
  86. #else
  87. Sleep(0);
  88. #endif
  89. }
  90. }
  91. void os_sleep_ms(uint32_t duration)
  92. {
  93. /* windows 8+ appears to have decreased sleep precision */
  94. if (get_winver() >= 0x0602 && duration > 0)
  95. duration--;
  96. Sleep(duration);
  97. }
  98. uint64_t os_gettime_ns(void)
  99. {
  100. LARGE_INTEGER current_time;
  101. double time_val;
  102. QueryPerformanceCounter(&current_time);
  103. time_val = (double)current_time.QuadPart;
  104. time_val *= 1000000000.0;
  105. time_val /= (double)get_clockfreq();
  106. return (uint64_t)time_val;
  107. }
  108. /* returns %appdata%\[name] on windows */
  109. char *os_get_config_path(const char *name)
  110. {
  111. char *ptr;
  112. wchar_t path_utf16[MAX_PATH];
  113. struct dstr path;
  114. SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT,
  115. path_utf16);
  116. os_wcs_to_utf8_ptr(path_utf16, 0, &ptr);
  117. dstr_init_move_array(&path, ptr);
  118. dstr_cat(&path, "\\");
  119. dstr_cat(&path, name);
  120. return path.array;
  121. }
  122. bool os_file_exists(const char *path)
  123. {
  124. WIN32_FIND_DATAW wfd;
  125. HANDLE hFind;
  126. wchar_t *path_utf16;
  127. if (!os_utf8_to_wcs_ptr(path, 0, &path_utf16))
  128. return false;
  129. hFind = FindFirstFileW(path_utf16, &wfd);
  130. if (hFind != INVALID_HANDLE_VALUE)
  131. FindClose(hFind);
  132. bfree(path_utf16);
  133. return hFind != INVALID_HANDLE_VALUE;
  134. }
  135. struct os_dir {
  136. HANDLE handle;
  137. WIN32_FIND_DATA wfd;
  138. bool first;
  139. struct os_dirent out;
  140. };
  141. os_dir_t os_opendir(const char *path)
  142. {
  143. struct dstr path_str = {0};
  144. struct os_dir *dir = NULL;
  145. WIN32_FIND_DATA wfd;
  146. HANDLE handle;
  147. wchar_t *w_path;
  148. dstr_copy(&path_str, path);
  149. dstr_cat(&path_str, "/*.*");
  150. if (os_utf8_to_wcs_ptr(path_str.array, path_str.len, &w_path) > 0) {
  151. handle = FindFirstFileW(w_path, &wfd);
  152. if (handle != INVALID_HANDLE_VALUE) {
  153. dir = bzalloc(sizeof(struct os_dir));
  154. dir->handle = handle;
  155. dir->first = true;
  156. dir->wfd = wfd;
  157. }
  158. bfree(w_path);
  159. }
  160. dstr_free(&path_str);
  161. return dir;
  162. }
  163. struct os_dirent *os_readdir(os_dir_t dir)
  164. {
  165. if (!dir)
  166. return NULL;
  167. if (dir->first) {
  168. dir->first = false;
  169. } else {
  170. if (!FindNextFileW(dir->handle, &dir->wfd))
  171. return NULL;
  172. }
  173. os_wcs_to_utf8(dir->wfd.cFileName, 0, dir->out.d_name,
  174. sizeof(dir->out.d_name));
  175. dir->out.directory =
  176. !!(dir->wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
  177. return &dir->out;
  178. }
  179. void os_closedir(os_dir_t dir)
  180. {
  181. if (dir) {
  182. FindClose(dir->handle);
  183. bfree(dir);
  184. }
  185. }
  186. int os_unlink(const char *path)
  187. {
  188. wchar_t *w_path;
  189. bool success;
  190. os_utf8_to_wcs_ptr(path, 0, &w_path);
  191. if (!w_path)
  192. return -1;
  193. success = !!DeleteFileW(w_path);
  194. bfree(w_path);
  195. return success ? 0 : -1;
  196. }
  197. int os_mkdir(const char *path)
  198. {
  199. wchar_t *path_utf16;
  200. BOOL success;
  201. if (!os_utf8_to_wcs_ptr(path, 0, &path_utf16))
  202. return MKDIR_ERROR;
  203. success = CreateDirectory(path_utf16, NULL);
  204. bfree(path_utf16);
  205. if (!success)
  206. return (GetLastError() == ERROR_ALREADY_EXISTS) ?
  207. MKDIR_EXISTS : MKDIR_ERROR;
  208. return MKDIR_SUCCESS;
  209. }
  210. BOOL WINAPI DllMain(HINSTANCE hinst_dll, DWORD reason, LPVOID reserved)
  211. {
  212. switch (reason) {
  213. case DLL_PROCESS_ATTACH:
  214. timeBeginPeriod(1);
  215. #ifdef PTW32_STATIC_LIB
  216. pthread_win32_process_attach_np();
  217. #endif
  218. break;
  219. case DLL_PROCESS_DETACH:
  220. timeEndPeriod(1);
  221. #ifdef PTW32_STATIC_LIB
  222. pthread_win32_process_detach_np();
  223. #endif
  224. break;
  225. case DLL_THREAD_ATTACH:
  226. #ifdef PTW32_STATIC_LIB
  227. pthread_win32_thread_attach_np();
  228. #endif
  229. break;
  230. case DLL_THREAD_DETACH:
  231. #ifdef PTW32_STATIC_LIB
  232. pthread_win32_thread_detach_np();
  233. #endif
  234. break;
  235. }
  236. return true;
  237. }