platform-windows.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /******************************************************************************
  2. Copyright (c) 2013 by Hugh Bailey <[email protected]>
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely, subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not
  10. claim that you wrote the original software. If you use this software
  11. in a product, an acknowledgment in the product documentation would be
  12. appreciated but is not required.
  13. 2. Altered source versions must be plainly marked as such, and must not be
  14. misrepresented as being the original software.
  15. 3. This notice may not be removed or altered from any source
  16. distribution.
  17. ******************************************************************************/
  18. #include <windows.h>
  19. #include <shellapi.h>
  20. #include <shlobj.h>
  21. #include "base.h"
  22. #include "platform.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. dstr_init_copy(&dll_name, path);
  49. dstr_cat(&dll_name, ".dll");
  50. os_utf8_to_wcs(dll_name.array, 0, &wpath);
  51. h_library = LoadLibraryW(wpath);
  52. bfree(wpath);
  53. dstr_free(&dll_name);
  54. if (!h_library)
  55. blog(LOG_INFO, "LoadLibrary failed for '%s', error: %u",
  56. path, GetLastError());
  57. return h_library;
  58. }
  59. void *os_dlsym(void *module, const char *func)
  60. {
  61. void *handle;
  62. handle = (void*)GetProcAddress(module, func);
  63. return handle;
  64. }
  65. void os_dlclose(void *module)
  66. {
  67. FreeLibrary(module);
  68. }
  69. void os_sleepto_ns(uint64_t time_target)
  70. {
  71. uint64_t t = os_gettime_ns();
  72. uint32_t milliseconds;
  73. if (t >= time_target)
  74. return;
  75. milliseconds = (uint32_t)((time_target - t)/1000000);
  76. if (milliseconds > 1)
  77. os_sleep_ms(milliseconds);
  78. for (;;) {
  79. t = os_gettime_ns();
  80. if (t >= time_target)
  81. return;
  82. #if 1
  83. Sleep(1);
  84. #else
  85. Sleep(0);
  86. #endif
  87. }
  88. }
  89. void os_sleep_ms(uint32_t duration)
  90. {
  91. /* windows 8+ appears to have decreased sleep precision */
  92. if (get_winver() >= 0x0602 && duration > 0)
  93. duration--;
  94. Sleep(duration);
  95. }
  96. uint64_t os_gettime_ns(void)
  97. {
  98. LARGE_INTEGER current_time;
  99. double time_val;
  100. QueryPerformanceCounter(&current_time);
  101. time_val = (double)current_time.QuadPart;
  102. time_val *= 1000000000.0;
  103. time_val /= (double)get_clockfreq();
  104. return (uint64_t)time_val;
  105. }
  106. /* returns %appdata%/[name] on windows */
  107. char *os_get_config_path(const char *name)
  108. {
  109. char *ptr;
  110. wchar_t path_utf16[MAX_PATH];
  111. struct dstr path;
  112. SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT,
  113. path_utf16);
  114. os_wcs_to_utf8(path_utf16, 0, &ptr);
  115. dstr_init_move_array(&path, ptr);
  116. dstr_cat(&path, "\\");
  117. dstr_cat(&path, name);
  118. return path.array;
  119. }
  120. bool os_file_exists(const char *path)
  121. {
  122. WIN32_FIND_DATA wfd;
  123. HANDLE hFind;
  124. wchar_t *path_utf16;
  125. if (!os_utf8_to_wcs(path, 0, &path_utf16))
  126. return false;
  127. hFind = FindFirstFileW(path_utf16, &wfd);
  128. if (hFind != INVALID_HANDLE_VALUE)
  129. FindClose(hFind);
  130. bfree(path_utf16);
  131. return hFind != INVALID_HANDLE_VALUE;
  132. }
  133. int os_mkdir(const char *path)
  134. {
  135. wchar_t *path_utf16;
  136. BOOL success;
  137. if (!os_utf8_to_wcs(path, 0, &path_utf16))
  138. return MKDIR_ERROR;
  139. success = CreateDirectory(path_utf16, NULL);
  140. bfree(path_utf16);
  141. if (!success)
  142. return (GetLastError() == ERROR_ALREADY_EXISTS) ?
  143. MKDIR_EXISTS : MKDIR_ERROR;
  144. return MKDIR_SUCCESS;
  145. }
  146. #ifdef PTW32_STATIC_LIB
  147. BOOL WINAPI DllMain(HINSTANCE hinst_dll, DWORD reason, LPVOID reserved)
  148. {
  149. switch (reason) {
  150. case DLL_PROCESS_ATTACH:
  151. pthread_win32_process_attach_np();
  152. break;
  153. case DLL_PROCESS_DETACH:
  154. pthread_win32_process_detach_np();
  155. break;
  156. case DLL_THREAD_ATTACH:
  157. pthread_win32_thread_attach_np();
  158. break;
  159. case DLL_THREAD_DETACH:
  160. pthread_win32_thread_detach_np();
  161. break;
  162. }
  163. return true;
  164. }
  165. #endif