]> gitweb.factorcode.org Git - factor.git/blob - vm/os-macosx.mm
c18c6aa1ea6673d8a3e2bdfe4cbb552bd114d947
[factor.git] / vm / os-macosx.mm
1 #import <Cocoa/Cocoa.h>
2
3 #include <mach/mach_time.h>
4 #include <sys/utsname.h>
5
6 #include "master.hpp"
7
8 namespace factor
9 {
10
11 void factor_vm::c_to_factor_toplevel(cell quot)
12 {
13         c_to_factor(quot);
14 }
15
16 // Darwin 9 is 10.5, Darwin 10 is 10.6
17 // http://en.wikipedia.org/wiki/Darwin_(operating_system)#Release_history
18 void early_init(void)
19 {
20         struct utsname u;
21         int n;
22         uname(&u);
23         sscanf(u.release,"%d", &n);
24         if(n < 9) {
25                 std::cout << "Factor requires Mac OS X 10.5 or later.\n";
26                 exit(1);
27         }
28 }
29
30 const char *vm_executable_path(void)
31 {
32         return [[[NSBundle mainBundle] executablePath] UTF8String];
33 }
34
35 const char *default_image_path(void)
36 {
37         NSBundle *bundle = [NSBundle mainBundle];
38         NSString *path = [bundle bundlePath];
39         NSString *executable = [[bundle executablePath] lastPathComponent];
40         NSString *image = [executable stringByAppendingString:@".image"];
41
42         NSString *returnVal;
43
44         if([path hasSuffix:@".app"] || [path hasSuffix:@".app/"])
45         {
46                 NSFileManager *mgr = [NSFileManager defaultManager];
47
48                 NSString *imageInBundle = [[path stringByAppendingPathComponent:@"Contents/Resources"] stringByAppendingPathComponent:image];
49                 NSString *imageAlongBundle = [[path stringByDeletingLastPathComponent] stringByAppendingPathComponent:image];
50
51                 returnVal = ([mgr fileExistsAtPath:imageInBundle]
52                         ? imageInBundle : imageAlongBundle);
53         }
54         else
55                 returnVal = [path stringByAppendingPathComponent:image];
56
57         return [returnVal UTF8String];
58 }
59
60 void factor_vm::init_signals(void)
61 {
62         unix_init_signals();
63         mach_initialize();
64 }
65
66 /* Amateurs at Apple: implement this function, properly! */
67 Protocol *objc_getProtocol(char *name)
68 {
69         if(strcmp(name,"NSTextInput") == 0)
70                 return @protocol(NSTextInput);
71         else
72                 return nil;
73 }
74
75 u64 nano_count()
76 {
77         u64 time = mach_absolute_time();
78
79         static u64 scaling_factor = 0;
80         if(!scaling_factor)
81         {
82                 mach_timebase_info_data_t info;
83                 kern_return_t ret = mach_timebase_info(&info);
84                 if(ret != 0)
85                         fatal_error("mach_timebase_info failed",ret);
86                 scaling_factor = info.numer/info.denom;
87         }
88
89         return time * scaling_factor;
90 }
91
92 }