platform-cocoa.m 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright (c) 2013-2014 Ruwen Hahn <[email protected]>
  3. * Hugh "Jim" Bailey <[email protected]>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include "base.h"
  18. #include "platform.h"
  19. #include "dstr.h"
  20. #include <dlfcn.h>
  21. #include <time.h>
  22. #include <unistd.h>
  23. #include <CoreServices/CoreServices.h>
  24. #include <mach/mach.h>
  25. #include <mach/mach_time.h>
  26. #import <Cocoa/Cocoa.h>
  27. /* clock function selection taken from libc++ */
  28. static uint64_t ns_time_simple()
  29. {
  30. return mach_absolute_time();
  31. }
  32. static double ns_time_compute_factor()
  33. {
  34. mach_timebase_info_data_t info = {1, 1};
  35. mach_timebase_info(&info);
  36. return ((double)info.numer) / info.denom;
  37. }
  38. static uint64_t ns_time_full()
  39. {
  40. static double factor = 0.;
  41. if (factor == 0.) factor = ns_time_compute_factor();
  42. return (uint64_t)(mach_absolute_time() * factor);
  43. }
  44. typedef uint64_t (*time_func)();
  45. static time_func ns_time_select_func()
  46. {
  47. mach_timebase_info_data_t info = {1, 1};
  48. mach_timebase_info(&info);
  49. if (info.denom == info.numer)
  50. return ns_time_simple;
  51. return ns_time_full;
  52. }
  53. uint64_t os_gettime_ns(void)
  54. {
  55. static time_func f = NULL;
  56. if (!f) f = ns_time_select_func();
  57. return f();
  58. }
  59. /* gets the location ~/Library/Application Support/[name] */
  60. char *os_get_config_path(const char *name)
  61. {
  62. NSArray *paths = NSSearchPathForDirectoriesInDomains(
  63. NSApplicationSupportDirectory, NSUserDomainMask, YES);
  64. if([paths count] == 0)
  65. bcrash("Could not get home directory (platform-cocoa)");
  66. NSString *application_support = paths[0];
  67. NSUInteger len = [application_support
  68. lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
  69. char *path_ptr = bmalloc(len+1);
  70. path_ptr[len] = 0;
  71. memcpy(path_ptr, [application_support UTF8String], len);
  72. struct dstr path;
  73. dstr_init_move_array(&path, path_ptr);
  74. dstr_cat(&path, "/");
  75. dstr_cat(&path, name);
  76. return path.array;
  77. }