OSDN Git Service

Merge branch 'master' of git.sourceforge.jp:/gitroot/heavyosecpu/HeavyOSECPU
[heavyosecpu/HeavyOSECPU.git] / dpndenv.c
index e4dbe4c..855795d 100644 (file)
--- a/dpndenv.c
+++ b/dpndenv.c
@@ -1,13 +1,10 @@
-#include "osecpu.h"
+#include "osecpu.h"
 
 
 #if (DRV_OSNUM == 0x0002)
 //
 // for Mac OSX 32-bit
 //
-
-// by Liva, 2013.05.29-
-
 #include <mach/mach.h>
 #include <Cocoa/Cocoa.h>
 
@@ -22,9 +19,9 @@ NSApplication* app;
 
 @interface OSECPUView : NSView
 {
-    unsigned char *_buf;
-    int _sx;
-    int _sy;
+       unsigned char *_buf;
+       int _sx;
+       int _sy;
        CGContextRef _context;
 }
 
@@ -35,31 +32,43 @@ NSApplication* app;
 @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;
+       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);
+       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);
+       CFRelease(colorSpace);
+       CFRelease(image);
+}
+
+- (void)keyDown:(NSEvent *)theEvent
+{
+
+}
+
+- (void)keyUp:(NSEvent *)theEvent
+{
+
 }
 
 @end
 
 @interface Main : NSObject<NSWindowDelegate>
 {
-    int argc;
-    const unsigned char **argv;
-    char *winClosed;
-    OSECPUView *_view;
+       int argc;
+       const unsigned char **argv;
+       char *winClosed;
+       OSECPUView *_view;
 }
 
 - (void)runApp;
@@ -72,29 +81,31 @@ NSApplication* app;
 @implementation Main
 - (void)runApp
 {
-    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
-    HeavyOSECPUMain(argc, (char **)argv);
-    [NSApp terminate:self];
+       NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
+       int retv;
+       retv = HeavyOSECPUMain(argc, (char **)argv);
+       [NSApp terminate:self];
        [pool release];
+       exit(retv);
 }
 
 - (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];
+       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;
+       *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];
@@ -102,14 +113,14 @@ NSApplication* app;
        [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];
+       [_view setNeedsDisplayInRect:rect];
 }
 
 @end
@@ -118,30 +129,30 @@ 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];
+       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;
+       return 0;
 }
 
 void drv_openWin(int sx, int sy, unsigned char *buf, char *winClosed)
 {
-    [objc_main openWin:buf sx:sx sy:sy winClosed: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)];
+       [objc_main flushWin:NSMakeRect(x0,y0,sx,sy)];
 }
 
 void drv_sleep(int msec)
 {
-    [NSThread sleepForTimeInterval:0.001*msec];
-    return;
+       [NSThread sleepForTimeInterval:0.001*msec];
+       return;
 }
 
 
@@ -230,10 +241,10 @@ static int winthread(void *dmy)
        RECT r;
        int i, x, y;
        MSG msg;
-
+       
        x = dw.bmi.bmiHeader.biWidth;
        y = -dw.bmi.bmiHeader.biHeight;
-
+       
        wc.cbSize = sizeof (WNDCLASSEX);
        wc.style = CS_HREDRAW | CS_VREDRAW;
        wc.lpfnWndProc = WndProc;
@@ -241,13 +252,13 @@ static int winthread(void *dmy)
        wc.cbWndExtra = 0;
        wc.hInstance = dw.hi;
        wc.hIcon = (HICON)LoadImage(NULL, MAKEINTRESOURCE(IDI_APPLICATION),
-               IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_SHARED);
+                                                               IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_SHARED);
        wc.hIconSm = wc.hIcon;
        wc.hCursor = (HCURSOR)LoadImage(NULL, MAKEINTRESOURCE(IDC_ARROW),
-               IMAGE_CURSOR, 0, 0, LR_DEFAULTSIZE | LR_SHARED);
+                                                                       IMAGE_CURSOR, 0, 0, LR_DEFAULTSIZE | LR_SHARED);
        wc.hbrBackground = (HBRUSH)COLOR_APPWORKSPACE;
        wc.lpszMenuName = NULL;
-       wc.lpszClassName = L"WinClass";
+       wc.lpszClassName = "WinClass";
        if (RegisterClassEx(&wc) == 0)
                return 1;
        r.left = 0;
@@ -257,11 +268,11 @@ static int winthread(void *dmy)
        AdjustWindowRect(&r, WS_OVERLAPPEDWINDOW, FALSE);
        x = r.right - r.left;
        y = r.bottom - r.top;
-
+       
        char *t = "osecpu";
-
+       
        dw.hw = CreateWindowA("WinClass", t, WS_OVERLAPPEDWINDOW,
-               CW_USEDEFAULT, CW_USEDEFAULT, x, y, NULL, NULL, dw.hi, NULL);
+                                                 CW_USEDEFAULT, CW_USEDEFAULT, x, y, NULL, NULL, dw.hi, NULL);
        if (dw.hw == NULL)
                return 1;
        ShowWindow(dw.hw, SW_SHOW);
@@ -270,7 +281,7 @@ static int winthread(void *dmy)
        SetTimer(dw.hw, TIMER_ID + 1, TIMER_INTERVAL * 10, NULL);
        SetTimer(dw.hw, TIMER_ID + 2, TIMER_INTERVAL * 100, NULL);
        dw.flags |= 2 | 4;
-
+       
        for (;;) {
                i = GetMessage(&msg, NULL, 0, 0);
                if (i == 0 || i == -1)  /* エラーもしくは終了メッセージ */
@@ -289,7 +300,7 @@ static int winthread(void *dmy)
 void bld_openWin(int sx, int sy, char *winClosed)
 {
        static int i;
-
+       
        dw.bmi.bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
        dw.bmi.bmiHeader.biWidth = sx;
        dw.bmi.bmiHeader.biHeight = -sy;
@@ -297,9 +308,9 @@ void bld_openWin(int sx, int sy, char *winClosed)
        dw.bmi.bmiHeader.biBitCount = 32;
        dw.bmi.bmiHeader.biCompression = BI_RGB;
        dw.winClosed = winClosed;
-
+       
        threadhandle = CreateThread(NULL, 0, (void *)&winthread, NULL, 0, (void *)&i);
-
+       
        return;
 }
 
@@ -317,7 +328,7 @@ LRESULT CALLBACK WndProc(HWND hw, unsigned int msg, WPARAM wp, LPARAM lp)
                PAINTSTRUCT ps;
                HDC hdc = BeginPaint(dw.hw, &ps);
                SetDIBitsToDevice(hdc, 0, 0, w.win.xsiz, w.win.ysiz,
-                       0, 0, 0, w.win.ysiz, w.win.buf, &dw.bmi, DIB_RGB_COLORS);
+                                                 0, 0, 0, w.win.ysiz, w.win.buf, &dw.bmi, DIB_RGB_COLORS);
                EndPaint(dw.hw, &ps);
        }
        if (msg == WM_DESTROY) {
@@ -340,7 +351,7 @@ LRESULT CALLBACK WndProc(HWND hw, unsigned int msg, WPARAM wp, LPARAM lp)
        }
        if (msg == WM_KEYDOWN || msg == WM_SYSKEYDOWN) {
                i = -1;
-
+               
                if (wp == VK_RETURN)    i = KEY_ENTER;
                if (wp == VK_ESCAPE)    i = KEY_ESC;
                if (wp == VK_BACK)              i = KEY_BACKSPACE;
@@ -423,6 +434,6 @@ void drv_sleep(int msec)
 
 #else
 
-#error "Illegal OS type. Edit switch.h and look at DRV_OSNUM"
+#error "Illegal OS type. Edit osecpu.h and look at DRV_OSNUM"
 
 #endif
\ No newline at end of file