4 #if (DRV_OSNUM == 0x0002)
9 #include <Cocoa/Cocoa.h>
11 void *mallocRWE(int bytes)
13 void *p = malloc(bytes);
14 vm_protect(mach_task_self(), (vm_address_t) p, bytes, FALSE, VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
20 @interface OSECPUView : NSView
25 CGContextRef _context;
28 - (id)initWithFrame:(NSRect)frameRect buf:(unsigned char *)buf sx:(int)sx sy:(int)sy;
29 - (void)drawRect:(NSRect)rect;
32 @implementation OSECPUView
33 - (id)initWithFrame:(NSRect)frameRect buf:(unsigned char *)buf sx:(int)sx sy:(int)sy
35 self = [super initWithFrame:frameRect];
44 - (void)drawRect:(NSRect)rect {
45 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
46 _context = CGBitmapContextCreate (_buf, _sx, _sy, 8, 4 * _sx, colorSpace, (kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst));
47 CGImageRef image = CGBitmapContextCreateImage(_context);
48 CGContextRef currentContext = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
49 CGContextDrawImage(currentContext, NSRectToCGRect(rect), image);
50 CFRelease(colorSpace);
54 - (void)keyDown:(NSEvent *)theEvent
59 - (void)keyUp:(NSEvent *)theEvent
66 @interface Main : NSObject<NSWindowDelegate>
69 const unsigned char **argv;
75 - (void)createThread:(int)_argc args:(const unsigned char **)_argv;
76 - (BOOL)windowShouldClose:(id)sender;
77 - (void)openWin:(unsigned char *)buf sx:(int)sx sy:(int)sy winClosed:(char *)_winClosed;
78 - (void)flushWin:(NSRect)rect;
84 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
86 retv = HeavyOSECPUMain(argc, (char **)argv);
87 [NSApp terminate:self];
92 - (void)createThread : (int)_argc args:(const unsigned char **)_argv
96 NSThread *thread = [[[NSThread alloc] initWithTarget:self selector:@selector(runApp) object:nil] autorelease];
100 - (BOOL)windowShouldClose:(id)sender
106 - (void)openWin:(unsigned char *)buf sx:(int)sx sy:(int) sy winClosed:(char *)_winClosed
109 NSWindow* window = [[NSWindow alloc] initWithContentRect: NSMakeRect(0, 0, sx, sy) styleMask: NSTitledWindowMask | NSMiniaturizableWindowMask | NSClosableWindowMask backing: NSBackingStoreBuffered defer: NO];
110 [window setTitle: @"osecpu"];
112 [window makeKeyAndOrderFront:nil];
113 [window setReleasedWhenClosed:YES];
114 window.delegate = self;
115 winClosed = _winClosed;
117 _view = [[OSECPUView alloc] initWithFrame:NSMakeRect(0,0,sx,sy) buf:buf sx:sx sy:sy];
118 [window.contentView addSubview:_view];
121 - (void)flushWin : (NSRect)rect
123 [_view setNeedsDisplayInRect:rect];
130 int main(int argc, char **argv)
132 objc_main = [[Main alloc] init];
134 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
135 app = [[NSApplication alloc] init];
136 [objc_main createThread:argc args:(const unsigned char **)argv];
142 void drv_openWin(int sx, int sy, unsigned char *buf, char *winClosed)
144 [objc_main openWin:buf sx:sx sy:sy winClosed:winClosed];
147 void drv_flshWin(int sx, int sy, int x0, int y0)
149 [objc_main flushWin:NSMakeRect(x0,y0,sx,sy)];
152 void drv_sleep(int msec)
154 [NSThread sleepForTimeInterval:0.001*msec];
159 #elif (DRV_OSNUM == 0x0003)
161 #error "Your OS is not supported."
163 #elif (DRV_OSNUM == 0x0001)
165 // for Windows 32-bit
170 #define TIMER_INTERVAL 10
176 int tmcount1, tmcount2, flags, smp; /* bit0: 終了 */
181 struct BLD_WORK bld_work;
184 int xsiz, ysiz, *buf;
190 int csiz_x, csiz_y, cx, cy, col0, col1, tabsiz, slctwin;
191 int tmcount, tmcount0, mod, rand_seed;
194 unsigned char **fptn;
196 int *kbuf, kbuf_rp, kbuf_wp, kbuf_c;
199 struct BL_WORK bl_work;
201 #define BL_SIZ_KBUF 8192
203 #define BL_WAITKEYF 0x00000001
204 #define BL_WAITKEYNF 0x00000002
205 #define BL_WAITKEY 0x00000003
206 #define BL_GETKEY 0x00000004
207 #define BL_CLEARREP 0x00000008
208 #define BL_DELFFF 0x00000010
210 #define BL_KEYMODE 0x00000000 // 作りかけ, make/remake/breakが見えるかどうか
215 void bld_openWin(int x, int y, char *winClosed);
216 void bld_flshWin(int sx, int sy, int x0, int y0);
217 LRESULT CALLBACK WndProc(HWND hw, unsigned int msg, WPARAM wp, LPARAM lp);
220 void bl_readyWin(int n);
222 static HANDLE threadhandle;
224 int main(int argc, char **argv)
226 // Program entry point
227 return HeavyOSECPUMain(argc, argv);
230 void *mallocRWE(int bytes)
232 void *p = malloc(bytes);
234 VirtualProtect(p, bytes, PAGE_EXECUTE_READWRITE, &dmy);
238 static int winthread(void *dmy)
245 x = dw.bmi.bmiHeader.biWidth;
246 y = -dw.bmi.bmiHeader.biHeight;
248 wc.cbSize = sizeof (WNDCLASSEX);
249 wc.style = CS_HREDRAW | CS_VREDRAW;
250 wc.lpfnWndProc = WndProc;
253 wc.hInstance = dw.hi;
254 wc.hIcon = (HICON)LoadImage(NULL, MAKEINTRESOURCE(IDI_APPLICATION),
255 IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_SHARED);
256 wc.hIconSm = wc.hIcon;
257 wc.hCursor = (HCURSOR)LoadImage(NULL, MAKEINTRESOURCE(IDC_ARROW),
258 IMAGE_CURSOR, 0, 0, LR_DEFAULTSIZE | LR_SHARED);
259 wc.hbrBackground = (HBRUSH)COLOR_APPWORKSPACE;
260 wc.lpszMenuName = NULL;
261 wc.lpszClassName = "WinClass";
262 if (RegisterClassEx(&wc) == 0)
268 AdjustWindowRect(&r, WS_OVERLAPPEDWINDOW, FALSE);
269 x = r.right - r.left;
270 y = r.bottom - r.top;
274 dw.hw = CreateWindowA("WinClass", t, WS_OVERLAPPEDWINDOW,
275 CW_USEDEFAULT, CW_USEDEFAULT, x, y, NULL, NULL, dw.hi, NULL);
278 ShowWindow(dw.hw, SW_SHOW);
280 SetTimer(dw.hw, TIMER_ID, TIMER_INTERVAL, NULL);
281 SetTimer(dw.hw, TIMER_ID + 1, TIMER_INTERVAL * 10, NULL);
282 SetTimer(dw.hw, TIMER_ID + 2, TIMER_INTERVAL * 100, NULL);
286 i = GetMessage(&msg, NULL, 0, 0);
287 if (i == 0 || i == -1) /* エラーもしくは終了メッセージ */
289 /* そのほかはとりあえずデフォルト処理で */
290 TranslateMessage(&msg);
291 DispatchMessage(&msg);
293 // PostQuitMessage(0);
294 dw.flags |= 1; /* 終了, bld_waitNF()が見つける */
295 if (dw.winClosed != NULL)
300 void bld_openWin(int sx, int sy, char *winClosed)
304 dw.bmi.bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
305 dw.bmi.bmiHeader.biWidth = sx;
306 dw.bmi.bmiHeader.biHeight = -sy;
307 dw.bmi.bmiHeader.biPlanes = 1;
308 dw.bmi.bmiHeader.biBitCount = 32;
309 dw.bmi.bmiHeader.biCompression = BI_RGB;
310 dw.winClosed = winClosed;
312 threadhandle = CreateThread(NULL, 0, (void *)&winthread, NULL, 0, (void *)&i);
317 void drv_flshWin(int sx, int sy, int x0, int y0)
319 InvalidateRect(dw.hw, NULL, FALSE);
324 LRESULT CALLBACK WndProc(HWND hw, unsigned int msg, WPARAM wp, LPARAM lp)
327 if (msg == WM_PAINT) {
329 HDC hdc = BeginPaint(dw.hw, &ps);
330 SetDIBitsToDevice(hdc, 0, 0, w.win.xsiz, w.win.ysiz,
331 0, 0, 0, w.win.ysiz, w.win.buf, &dw.bmi, DIB_RGB_COLORS);
332 EndPaint(dw.hw, &ps);
334 if (msg == WM_DESTROY) {
338 if (msg == WM_TIMER && wp == TIMER_ID) {
339 w.tmcount += TIMER_INTERVAL;
342 if (msg == WM_TIMER && wp == TIMER_ID + 1) {
343 dw.tmcount1 += TIMER_INTERVAL * 10;
344 w.tmcount = dw.tmcount1;
347 if (msg == WM_TIMER && wp == TIMER_ID + 2) {
348 dw.tmcount2 += TIMER_INTERVAL * 100;
349 w.tmcount = dw.tmcount1 = dw.tmcount2;
352 if (msg == WM_KEYDOWN || msg == WM_SYSKEYDOWN) {
355 if (wp == VK_RETURN) i = KEY_ENTER;
356 if (wp == VK_ESCAPE) i = KEY_ESC;
357 if (wp == VK_BACK) i = KEY_BACKSPACE;
358 if (wp == VK_TAB) i = KEY_TAB;
359 if (wp == VK_PRIOR) i = KEY_PAGEUP;
360 if (wp == VK_NEXT) i = KEY_PAGEDWN;
361 if (wp == VK_END) i = KEY_END;
362 if (wp == VK_HOME) i = KEY_HOME;
363 if (wp == VK_LEFT) i = KEY_LEFT;
364 if (wp == VK_RIGHT) i = KEY_RIGHT;
365 if (wp == VK_UP) i = KEY_UP;
366 if (wp == VK_DOWN) i = KEY_DOWN;
367 if (wp == VK_INSERT) i = KEY_INS;
368 if (wp == VK_DELETE) i = KEY_DEL;
370 if ((GetKeyState(VK_LCONTROL) & (1 << 15)) != 0) j |= 1 << 17;
371 if ((GetKeyState(VK_LMENU) & (1 << 15)) != 0) j |= 1 << 18;
372 if ((GetKeyState(VK_RCONTROL) & (1 << 15)) != 0) j |= 1 << 25;
373 if ((GetKeyState(VK_RMENU) & (1 << 15)) != 0) j |= 1 << 26;
374 if ((GetKeyState(VK_RSHIFT) & (1 << 15)) != 0) i |= 1 << 24;
375 if ((GetKeyState(VK_LSHIFT) & (1 << 15)) != 0) i |= 1 << 16;
376 if ((GetKeyState(VK_NUMLOCK) & (1 << 0)) != 0) i |= 1 << 22;
377 if ((GetKeyState(VK_CAPITAL) & (1 << 0)) != 0) i |= 1 << 23;
379 if ('A' <= wp && wp <= 'Z') i = wp;
383 // bl_putKeyB(1, &i);
387 if (msg == WM_KEYUP) {
389 // bl_putKeyB(1, &i);
391 if (msg == WM_CHAR) {
393 if (' ' <= wp && wp <= 0x7e) {
396 if ((GetKeyState(VK_LCONTROL) & (1 << 15)) != 0) j |= 1 << 17;
397 if ((GetKeyState(VK_LMENU) & (1 << 15)) != 0) j |= 1 << 18;
398 if ((GetKeyState(VK_RCONTROL) & (1 << 15)) != 0) j |= 1 << 25;
399 if ((GetKeyState(VK_RMENU) & (1 << 15)) != 0) j |= 1 << 26;
400 if ((GetKeyState(VK_RSHIFT) & (1 << 15)) != 0) i |= 1 << 24;
401 if ((GetKeyState(VK_LSHIFT) & (1 << 15)) != 0) i |= 1 << 16;
402 if ((GetKeyState(VK_NUMLOCK) & (1 << 0)) != 0) i |= 1 << 22;
403 if ((GetKeyState(VK_CAPITAL) & (1 << 0)) != 0) i |= 1 << 23;
404 if (('A' <= wp && wp <= 'Z') || ('a' <= wp && wp <= 'z')) {
411 // bl_putKeyB(1, &i);
415 return DefWindowProc(hw, msg, wp, lp);
418 void drv_openWin(int sx, int sy, UCHAR *buf, char *winClosed)
420 w.win.buf = (int *)buf;
423 bld_openWin(sx, sy, winClosed);
427 void drv_sleep(int msec)
430 // MsgWaitForMultipleObjects(1, &threadhandle, FALSE, msec, QS_ALLINPUT);
431 /* 勉強不足でまだ書き方が分かりません! */
437 #error "Illegal OS type. Edit osecpu.h and look at DRV_OSNUM"