platform-cocoa.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /******************************************************************************
  2. Copyright (c) 2013 by Ruwen Hahn <[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 "base.h"
  19. #include "platform.h"
  20. #include "dstr.h"
  21. #include <dlfcn.h>
  22. #include <time.h>
  23. #include <unistd.h>
  24. #include <sys/stat.h>
  25. #include <CoreServices/CoreServices.h>
  26. #include <mach/mach.h>
  27. #include <mach/mach_time.h>
  28. #import <Cocoa/Cocoa.h>
  29. void *os_dlopen(const char *path)
  30. {
  31. struct dstr dylib_name;
  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. void os_sleepto_ns(uint64_t time_target)
  51. {
  52. uint64_t current = os_gettime_ns();
  53. if(time_target < current)
  54. return;
  55. time_target -= current;
  56. struct timespec req,
  57. remain;
  58. memset(&req, 0, sizeof(req));
  59. memset(&remain, 0, sizeof(remain));
  60. req.tv_sec = time_target/1000000000;
  61. req.tv_nsec = time_target%1000000000;
  62. while(nanosleep(&req, &remain))
  63. {
  64. req = remain;
  65. memset(&remain, 0, sizeof(remain));
  66. }
  67. }
  68. void os_sleep_ms(uint32_t duration)
  69. {
  70. usleep(duration*1000);
  71. }
  72. uint64_t os_gettime_ns(void)
  73. {
  74. uint64_t t = mach_absolute_time();
  75. #pragma clang diagnostic push
  76. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  77. Nanoseconds nano = AbsoluteToNanoseconds(*(AbsoluteTime*) &t);
  78. #pragma clang diagnostic pop
  79. return *(uint64_t*) &nano;
  80. }
  81. uint64_t os_gettime_ms(void)
  82. {
  83. return os_gettime_ns()/1000000;
  84. }
  85. char *os_get_home_path(void)
  86. {
  87. NSArray *paths = NSSearchPathForDirectoriesInDomains(
  88. NSApplicationSupportDirectory, NSUserDomainMask, YES);
  89. if([paths count] == 0)
  90. bcrash("Could not get home directory (platform-cocoa)");
  91. NSString *application_support = paths[0];
  92. NSUInteger len = [application_support
  93. lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
  94. char *path = bmalloc(len+1);
  95. path[len] = 0;
  96. memcpy(path, [application_support UTF8String], len);
  97. return path;
  98. }
  99. int os_mkdir(const char *path)
  100. {
  101. if(!mkdir(path, 0777))
  102. return MKDIR_SUCCESS;
  103. if(errno == EEXIST)
  104. return MKDIR_EXISTS;
  105. return MKDIR_ERROR;
  106. }