platform-cocoa.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. uint64_t os_gettime_ns(void)
  72. {
  73. uint64_t t = mach_absolute_time();
  74. #pragma clang diagnostic push
  75. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  76. Nanoseconds nano = AbsoluteToNanoseconds(*(AbsoluteTime*) &t);
  77. #pragma clang diagnostic pop
  78. return *(uint64_t*) &nano;
  79. }
  80. /* gets the location ~/Library/Application Support/[name] */
  81. char *os_get_config_path(const char *name)
  82. {
  83. NSArray *paths = NSSearchPathForDirectoriesInDomains(
  84. NSApplicationSupportDirectory, NSUserDomainMask, YES);
  85. if([paths count] == 0)
  86. bcrash("Could not get home directory (platform-cocoa)");
  87. NSString *application_support = paths[0];
  88. NSUInteger len = [application_support
  89. lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
  90. char *path_ptr = bmalloc(len+1);
  91. path_ptr[len] = 0;
  92. memcpy(path_ptr, [application_support UTF8String], len);
  93. struct dstr path;
  94. dstr_init_move_array(&path, path_ptr);
  95. dstr_cat(&path, "/");
  96. dstr_cat(&path, name);
  97. return path.array;
  98. }
  99. bool os_file_exists(const char *path)
  100. {
  101. return access(path, F_OK) == 0;
  102. }
  103. int os_mkdir(const char *path)
  104. {
  105. if(!mkdir(path, 0777))
  106. return MKDIR_SUCCESS;
  107. if(errno == EEXIST)
  108. return MKDIR_EXISTS;
  109. return MKDIR_ERROR;
  110. }