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