platform-cocoa.m 3.1 KB

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