// for Mac OSX 32-bit
//
+// by Liva, 2013.05.29-
+
+#include <mach/mach.h>
+#include <Cocoa/Cocoa.h>
+
+void *mallocRWE(int bytes)
+{
+ void *p = malloc(bytes);
+ vm_protect(mach_task_self(), (vm_address_t) p, bytes, FALSE, VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
+ return p;
+}
+
+NSApplication* app;
+
+@interface OSECPUView : NSView {
+ unsigned char *_buf;
+ int _sx;
+ int _sy;
+ CGContextRef _context;
+}
+
+- (id)initWithFrame:(NSRect)frameRect
+buf : (unsigned char *) buf
+sx : (int) sx
+sy : (int) sy;
+
+- (void)drawRect:(NSRect)rect;
+@end
+
+@implementation OSECPUView
+- (id)initWithFrame:(NSRect)frameRect
+buf : (unsigned char *) buf
+sx : (int) sx
+sy : (int) sy
+{
+ self = [super initWithFrame:frameRect];
+ if (self) {
+ _buf = buf;
+ _sx = sx;
+ _sy = sy;
+ }
+ return self;
+}
+
+- (void)drawRect:(NSRect)rect {
+ CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
+ _context = CGBitmapContextCreate (_buf, _sx, _sy, 8, 4 * _sx, colorSpace, (kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst));
+ CGImageRef image = CGBitmapContextCreateImage(_context);
+ CGContextRef currentContext = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
+ CGContextDrawImage(currentContext, NSRectToCGRect(rect), image);
+}
+
+@end
+
+@interface Main : NSObject<NSWindowDelegate> {
+ int argc;
+ const unsigned char **argv;
+ char *winClosed;
+ OSECPUView *_view;
+}
+
+- (void)runApp;
+- (void)createThread : (int)_argc
+args : (const unsigned char **)_argv;
+- (BOOL)windowShouldClose:(id)sender;
+- (void)openWin : (unsigned char *)buf
+sx : (int) sx
+sy : (int) sy
+winClosed : (char *)_winClosed;
+- (void)flushWin : (NSRect) rect;
+@end
+
+@implementation Main
+- (void)runApp
+{
+ NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
+ HeavyOSECPUMain(argc, (char **)argv);
+ [NSApp terminate:self];
+ [pool release];
+}
+
+- (void)createThread : (int)_argc
+args : (const unsigned char **)_argv
+{
+ argc = _argc;
+ argv = _argv;
+ NSThread *thread = [[[NSThread alloc] initWithTarget:self selector:@selector(runApp) object:nil] autorelease];
+ [thread start];
+}
+
+- (BOOL)windowShouldClose:(id)sender
+{
+ *winClosed = 1;
+ return YES;
+}
+
+- (void)openWin : (unsigned char *)buf
+sx : (int) sx
+sy : (int) sy
+winClosed : (char *)_winClosed
+{
+
+ NSWindow* window = [[NSWindow alloc] initWithContentRect: NSMakeRect(0, 0, sx, sy) styleMask: NSTitledWindowMask | NSMiniaturizableWindowMask | NSClosableWindowMask backing: NSBackingStoreBuffered defer: NO];
+ [window setTitle: @"osecpu"];
+ [window center];
+ [window makeKeyAndOrderFront:nil];
+ [window setReleasedWhenClosed:YES];
+ window.delegate = self;
+ winClosed = _winClosed;
+
+ _view = [[OSECPUView alloc] initWithFrame:NSMakeRect(0,0,sx,sy) buf:buf sx:sx sy:sy];
+ [window.contentView addSubview:_view];
+}
+
+- (void)flushWin : (NSRect) rect
+{
+ [_view drawRect:rect];
+}
+
+@end
+
+id objc_main;
+
+int main(int argc, char **argv)
+{
+ objc_main = [[Main alloc] init];
+
+ NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
+ app = [[NSApplication alloc] init];
+ [objc_main createThread:argc args:(const unsigned char **)argv];
+ [app run];
+ [pool release];
+ return 0;
+}
+
+void drv_openWin(int sx, int sy, unsigned char *buf, char *winClosed)
+{
+ [objc_main openWin:buf sx:sx sy:sy winClosed:winClosed];
+}
+
+void drv_flshWin(int sx, int sy, int x0, int y0)
+{
+ [objc_main flushWin:NSMakeRect(x0,y0,sx,sy)];
+}
+
+void drv_sleep(int msec)
+{
+ [NSThread sleepForTimeInterval:0.001*msec];
+ return;
+}
+
#elif (DRV_OSNUM == 0x0003)
static HANDLE threadhandle;
+int main(int argc, char **argv)
+{
+ // Program entry point
+ return HeavyOSECPUMain(argc, argv);
+}
+
void *mallocRWE(int bytes)
{
void *p = malloc(bytes);