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