platform-cocoa.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. dstr_init_copy(&dylib_name, path);
  31. if (!dstr_find(&dylib_name, ".so"))
  32. dstr_cat(&dylib_name, ".so");
  33. void *res = dlopen(dylib_name.array, RTLD_LAZY);
  34. if (!res)
  35. blog(LOG_ERROR, "os_dlopen(%s->%s): %s\n",
  36. path, dylib_name.array, dlerror());
  37. dstr_free(&dylib_name);
  38. return res;
  39. }
  40. void *os_dlsym(void *module, const char *func)
  41. {
  42. return dlsym(module, func);
  43. }
  44. void os_dlclose(void *module)
  45. {
  46. dlclose(module);
  47. }
  48. bool os_sleepto_ns(uint64_t time_target)
  49. {
  50. uint64_t current = os_gettime_ns();
  51. if (time_target < current)
  52. return false;
  53. time_target -= current;
  54. struct timespec req, remain;
  55. memset(&req, 0, sizeof(req));
  56. memset(&remain, 0, sizeof(remain));
  57. req.tv_sec = time_target/1000000000;
  58. req.tv_nsec = time_target%1000000000;
  59. while (nanosleep(&req, &remain)) {
  60. req = remain;
  61. memset(&remain, 0, sizeof(remain));
  62. }
  63. return true;
  64. }
  65. void os_sleep_ms(uint32_t duration)
  66. {
  67. usleep(duration*1000);
  68. }
  69. uint64_t os_gettime_ns(void)
  70. {
  71. uint64_t t = mach_absolute_time();
  72. #pragma clang diagnostic push
  73. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  74. Nanoseconds nano = AbsoluteToNanoseconds(*(AbsoluteTime*) &t);
  75. #pragma clang diagnostic pop
  76. return *(uint64_t*) &nano;
  77. }
  78. /* gets the location ~/Library/Application Support/[name] */
  79. char *os_get_config_path(const char *name)
  80. {
  81. NSArray *paths = NSSearchPathForDirectoriesInDomains(
  82. NSApplicationSupportDirectory, NSUserDomainMask, YES);
  83. if([paths count] == 0)
  84. bcrash("Could not get home directory (platform-cocoa)");
  85. NSString *application_support = paths[0];
  86. NSUInteger len = [application_support
  87. lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
  88. char *path_ptr = bmalloc(len+1);
  89. path_ptr[len] = 0;
  90. memcpy(path_ptr, [application_support UTF8String], len);
  91. struct dstr path;
  92. dstr_init_move_array(&path, path_ptr);
  93. dstr_cat(&path, "/");
  94. dstr_cat(&path, name);
  95. return path.array;
  96. }
  97. bool os_file_exists(const char *path)
  98. {
  99. return access(path, F_OK) == 0;
  100. }
  101. int os_mkdir(const char *path)
  102. {
  103. if(!mkdir(path, 0777))
  104. return MKDIR_SUCCESS;
  105. if(errno == EEXIST)
  106. return MKDIR_EXISTS;
  107. return MKDIR_ERROR;
  108. }