platform-cocoa.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (c) 2013 Ruwen Hahn <[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 "base.h"
  17. #include "platform.h"
  18. #include "dstr.h"
  19. #include <dlfcn.h>
  20. #include <time.h>
  21. #include <unistd.h>
  22. #include <sys/stat.h>
  23. #include <CoreServices/CoreServices.h>
  24. #include <mach/mach.h>
  25. #include <mach/mach_time.h>
  26. #import <Cocoa/Cocoa.h>
  27. void *os_dlopen(const char *path)
  28. {
  29. struct dstr dylib_name;
  30. if (!path)
  31. return NULL;
  32. dstr_init_copy(&dylib_name, path);
  33. if (!dstr_find(&dylib_name, ".so"))
  34. dstr_cat(&dylib_name, ".so");
  35. void *res = dlopen(dylib_name.array, RTLD_LAZY);
  36. if (!res)
  37. blog(LOG_ERROR, "os_dlopen(%s->%s): %s\n",
  38. path, dylib_name.array, dlerror());
  39. dstr_free(&dylib_name);
  40. return res;
  41. }
  42. void *os_dlsym(void *module, const char *func)
  43. {
  44. return dlsym(module, func);
  45. }
  46. void os_dlclose(void *module)
  47. {
  48. dlclose(module);
  49. }
  50. bool os_sleepto_ns(uint64_t time_target)
  51. {
  52. uint64_t current = os_gettime_ns();
  53. if (time_target < current)
  54. return false;
  55. time_target -= current;
  56. struct timespec req, remain;
  57. memset(&req, 0, sizeof(req));
  58. memset(&remain, 0, sizeof(remain));
  59. req.tv_sec = time_target/1000000000;
  60. req.tv_nsec = time_target%1000000000;
  61. while (nanosleep(&req, &remain)) {
  62. req = remain;
  63. memset(&remain, 0, sizeof(remain));
  64. }
  65. return true;
  66. }
  67. void os_sleep_ms(uint32_t duration)
  68. {
  69. usleep(duration*1000);
  70. }
  71. /* clock function selection taken from libc++ */
  72. static uint64_t ns_time_simple()
  73. {
  74. return mach_absolute_time();
  75. }
  76. static double ns_time_compute_factor()
  77. {
  78. mach_timebase_info_data_t info = {1, 1};
  79. mach_timebase_info(&info);
  80. return ((double)info.numer) / info.denom;
  81. }
  82. static uint64_t ns_time_full()
  83. {
  84. static double factor = 0.;
  85. if (factor == 0.) factor = ns_time_compute_factor();
  86. return (uint64_t)(mach_absolute_time() * factor);
  87. }
  88. typedef uint64_t (*time_func)();
  89. static time_func ns_time_select_func()
  90. {
  91. mach_timebase_info_data_t info = {1, 1};
  92. mach_timebase_info(&info);
  93. if (info.denom == info.numer)
  94. return ns_time_simple;
  95. return ns_time_full;
  96. }
  97. uint64_t os_gettime_ns(void)
  98. {
  99. static time_func f = NULL;
  100. if (!f) f = ns_time_select_func();
  101. return f();
  102. }
  103. /* gets the location ~/Library/Application Support/[name] */
  104. char *os_get_config_path(const char *name)
  105. {
  106. NSArray *paths = NSSearchPathForDirectoriesInDomains(
  107. NSApplicationSupportDirectory, NSUserDomainMask, YES);
  108. if([paths count] == 0)
  109. bcrash("Could not get home directory (platform-cocoa)");
  110. NSString *application_support = paths[0];
  111. NSUInteger len = [application_support
  112. lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
  113. char *path_ptr = bmalloc(len+1);
  114. path_ptr[len] = 0;
  115. memcpy(path_ptr, [application_support UTF8String], len);
  116. struct dstr path;
  117. dstr_init_move_array(&path, 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. return access(path, F_OK) == 0;
  125. }
  126. int os_mkdir(const char *path)
  127. {
  128. if(!mkdir(path, 0777))
  129. return MKDIR_SUCCESS;
  130. if(errno == EEXIST)
  131. return MKDIR_EXISTS;
  132. return MKDIR_ERROR;
  133. }