platform-windows.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. int os_mkdir(const char *path)
  136. {
  137. wchar_t *path_utf16;
  138. BOOL success;
  139. if (!os_utf8_to_wcs_ptr(path, 0, &path_utf16))
  140. return MKDIR_ERROR;
  141. success = CreateDirectory(path_utf16, NULL);
  142. bfree(path_utf16);
  143. if (!success)
  144. return (GetLastError() == ERROR_ALREADY_EXISTS) ?
  145. MKDIR_EXISTS : MKDIR_ERROR;
  146. return MKDIR_SUCCESS;
  147. }
  148. BOOL WINAPI DllMain(HINSTANCE hinst_dll, DWORD reason, LPVOID reserved)
  149. {
  150. switch (reason) {
  151. case DLL_PROCESS_ATTACH:
  152. timeBeginPeriod(1);
  153. #ifdef PTW32_STATIC_LIB
  154. pthread_win32_process_attach_np();
  155. #endif
  156. break;
  157. case DLL_PROCESS_DETACH:
  158. timeEndPeriod(1);
  159. #ifdef PTW32_STATIC_LIB
  160. pthread_win32_process_detach_np();
  161. #endif
  162. break;
  163. case DLL_THREAD_ATTACH:
  164. #ifdef PTW32_STATIC_LIB
  165. pthread_win32_thread_attach_np();
  166. #endif
  167. break;
  168. case DLL_THREAD_DETACH:
  169. #ifdef PTW32_STATIC_LIB
  170. pthread_win32_thread_detach_np();
  171. #endif
  172. break;
  173. }
  174. return true;
  175. }