1
0

platform-windows.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 <shellapi.h>
  18. #include <shlobj.h>
  19. #include "base.h"
  20. #include "platform.h"
  21. #include "dstr.h"
  22. #include "../../deps/w32-pthreads/pthread.h"
  23. static bool have_clockfreq = false;
  24. static LARGE_INTEGER clock_freq;
  25. static uint32_t winver = 0;
  26. static inline uint64_t get_clockfreq(void)
  27. {
  28. if (!have_clockfreq)
  29. QueryPerformanceFrequency(&clock_freq);
  30. return clock_freq.QuadPart;
  31. }
  32. static inline uint32_t get_winver(void)
  33. {
  34. if (!winver) {
  35. OSVERSIONINFO osvi;
  36. memset(&osvi, 0, sizeof(osvi));
  37. winver = (osvi.dwMajorVersion << 16) | (osvi.dwMinorVersion);
  38. }
  39. return winver;
  40. }
  41. void *os_dlopen(const char *path)
  42. {
  43. struct dstr dll_name;
  44. wchar_t *wpath;
  45. HMODULE h_library = NULL;
  46. dstr_init_copy(&dll_name, path);
  47. if (!dstr_find(&dll_name, ".dll"))
  48. dstr_cat(&dll_name, ".dll");
  49. os_utf8_to_wcs(dll_name.array, 0, &wpath);
  50. h_library = LoadLibraryW(wpath);
  51. bfree(wpath);
  52. dstr_free(&dll_name);
  53. if (!h_library)
  54. blog(LOG_INFO, "LoadLibrary failed for '%s', error: %u",
  55. path, GetLastError());
  56. return h_library;
  57. }
  58. void *os_dlsym(void *module, const char *func)
  59. {
  60. void *handle;
  61. handle = (void*)GetProcAddress(module, func);
  62. return handle;
  63. }
  64. void os_dlclose(void *module)
  65. {
  66. FreeLibrary(module);
  67. }
  68. bool os_sleepto_ns(uint64_t time_target)
  69. {
  70. uint64_t t = os_gettime_ns();
  71. uint32_t milliseconds;
  72. if (t >= time_target)
  73. return false;
  74. milliseconds = (uint32_t)((time_target - t)/1000000);
  75. if (milliseconds > 1)
  76. os_sleep_ms(milliseconds);
  77. for (;;) {
  78. t = os_gettime_ns();
  79. if (t >= time_target)
  80. return true;
  81. #if 1
  82. Sleep(1);
  83. #else
  84. Sleep(0);
  85. #endif
  86. }
  87. }
  88. void os_sleep_ms(uint32_t duration)
  89. {
  90. /* windows 8+ appears to have decreased sleep precision */
  91. if (get_winver() >= 0x0602 && duration > 0)
  92. duration--;
  93. Sleep(duration);
  94. }
  95. uint64_t os_gettime_ns(void)
  96. {
  97. LARGE_INTEGER current_time;
  98. double time_val;
  99. QueryPerformanceCounter(&current_time);
  100. time_val = (double)current_time.QuadPart;
  101. time_val *= 1000000000.0;
  102. time_val /= (double)get_clockfreq();
  103. return (uint64_t)time_val;
  104. }
  105. /* returns %appdata%\[name] on windows */
  106. char *os_get_config_path(const char *name)
  107. {
  108. char *ptr;
  109. wchar_t path_utf16[MAX_PATH];
  110. struct dstr path;
  111. SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT,
  112. path_utf16);
  113. os_wcs_to_utf8(path_utf16, 0, &ptr);
  114. dstr_init_move_array(&path, ptr);
  115. dstr_cat(&path, "\\");
  116. dstr_cat(&path, name);
  117. return path.array;
  118. }
  119. bool os_file_exists(const char *path)
  120. {
  121. WIN32_FIND_DATAW wfd;
  122. HANDLE hFind;
  123. wchar_t *path_utf16;
  124. if (!os_utf8_to_wcs(path, 0, &path_utf16))
  125. return false;
  126. hFind = FindFirstFileW(path_utf16, &wfd);
  127. if (hFind != INVALID_HANDLE_VALUE)
  128. FindClose(hFind);
  129. bfree(path_utf16);
  130. return hFind != INVALID_HANDLE_VALUE;
  131. }
  132. int os_mkdir(const char *path)
  133. {
  134. wchar_t *path_utf16;
  135. BOOL success;
  136. if (!os_utf8_to_wcs(path, 0, &path_utf16))
  137. return MKDIR_ERROR;
  138. success = CreateDirectory(path_utf16, NULL);
  139. bfree(path_utf16);
  140. if (!success)
  141. return (GetLastError() == ERROR_ALREADY_EXISTS) ?
  142. MKDIR_EXISTS : MKDIR_ERROR;
  143. return MKDIR_SUCCESS;
  144. }
  145. #ifdef PTW32_STATIC_LIB
  146. BOOL WINAPI DllMain(HINSTANCE hinst_dll, DWORD reason, LPVOID reserved)
  147. {
  148. switch (reason) {
  149. case DLL_PROCESS_ATTACH:
  150. pthread_win32_process_attach_np();
  151. break;
  152. case DLL_PROCESS_DETACH:
  153. pthread_win32_process_detach_np();
  154. break;
  155. case DLL_THREAD_ATTACH:
  156. pthread_win32_thread_attach_np();
  157. break;
  158. case DLL_THREAD_DETACH:
  159. pthread_win32_thread_detach_np();
  160. break;
  161. }
  162. return true;
  163. }
  164. #endif