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);
51 CFRelease(colorSpace);
57 @interface Main : NSObject<NSWindowDelegate>
60 const unsigned char **argv;
66 - (void)createThread:(int)_argc args:(const unsigned char **)_argv;
67 - (BOOL)windowShouldClose:(id)sender;
68 - (void)openWin:(unsigned char *)buf sx:(int)sx sy:(int)sy winClosed:(char *)_winClosed;
69 - (void)flushWin:(NSRect)rect;
75 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
76 HeavyOSECPUMain(argc, (char **)argv);
77 [NSApp terminate:self];
81 - (void)createThread : (int)_argc args:(const unsigned char **)_argv
85 NSThread *thread = [[[NSThread alloc] initWithTarget:self selector:@selector(runApp) object:nil] autorelease];
89 - (BOOL)windowShouldClose:(id)sender
95 - (void)openWin:(unsigned char *)buf sx:(int)sx sy:(int) sy winClosed:(char *)_winClosed
98 NSWindow* window = [[NSWindow alloc] initWithContentRect: NSMakeRect(0, 0, sx, sy) styleMask: NSTitledWindowMask | NSMiniaturizableWindowMask | NSClosableWindowMask backing: NSBackingStoreBuffered defer: NO];
99 [window setTitle: @"osecpu"];
101 [window makeKeyAndOrderFront:nil];
102 [window setReleasedWhenClosed:YES];
103 window.delegate = self;
104 winClosed = _winClosed;
106 _view = [[OSECPUView alloc] initWithFrame:NSMakeRect(0,0,sx,sy) buf:buf sx:sx sy:sy];
107 [window.contentView addSubview:_view];
110 - (void)flushWin : (NSRect)rect
112 [_view drawRect:rect];
119 int main(int argc, char **argv)
121 objc_main = [[Main alloc] init];
123 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
124 app = [[NSApplication alloc] init];
125 [objc_main createThread:argc args:(const unsigned char **)argv];
131 void drv_openWin(int sx, int sy, unsigned char *buf, char *winClosed)
133 [objc_main openWin:buf sx:sx sy:sy winClosed:winClosed];
136 void drv_flshWin(int sx, int sy, int x0, int y0)
138 [objc_main flushWin:NSMakeRect(x0,y0,sx,sy)];
141 void drv_sleep(int msec)
143 [NSThread sleepForTimeInterval:0.001*msec];
148 #elif (DRV_OSNUM == 0x0003)
150 #error "Your OS is not supported."
152 #elif (DRV_OSNUM == 0x0001)
154 // for Windows 32-bit
159 #define TIMER_INTERVAL 10
165 int tmcount1, tmcount2, flags, smp; /* bit0: 終了 */
170 struct BLD_WORK bld_work;
173 int xsiz, ysiz, *buf;
179 int csiz_x, csiz_y, cx, cy, col0, col1, tabsiz, slctwin;
180 int tmcount, tmcount0, mod, rand_seed;
183 unsigned char **fptn;
185 int *kbuf, kbuf_rp, kbuf_wp, kbuf_c;
188 struct BL_WORK bl_work;
190 #define BL_SIZ_KBUF 8192
192 #define BL_WAITKEYF 0x00000001
193 #define BL_WAITKEYNF 0x00000002
194 #define BL_WAITKEY 0x00000003
195 #define BL_GETKEY 0x00000004
196 #define BL_CLEARREP 0x00000008
197 #define BL_DELFFF 0x00000010
199 #define BL_KEYMODE 0x00000000 // 作りかけ, make/remake/breakが見えるかどうか
204 void bld_openWin(int x, int y, char *winClosed);
205 void bld_flshWin(int sx, int sy, int x0, int y0);
206 LRESULT CALLBACK WndProc(HWND hw, unsigned int msg, WPARAM wp, LPARAM lp);
209 void bl_readyWin(int n);
211 static HANDLE threadhandle;
213 int main(int argc, char **argv)
215 // Program entry point
216 return HeavyOSECPUMain(argc, argv);
219 void *mallocRWE(int bytes)
221 void *p = malloc(bytes);
223 VirtualProtect(p, bytes, PAGE_EXECUTE_READWRITE, &dmy);
227 static int winthread(void *dmy)
234 x = dw.bmi.bmiHeader.biWidth;
235 y = -dw.bmi.bmiHeader.biHeight;
237 wc.cbSize = sizeof (WNDCLASSEX);
238 wc.style = CS_HREDRAW | CS_VREDRAW;
239 wc.lpfnWndProc = WndProc;
242 wc.hInstance = dw.hi;
243 wc.hIcon = (HICON)LoadImage(NULL, MAKEINTRESOURCE(IDI_APPLICATION),
244 IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_SHARED);
245 wc.hIconSm = wc.hIcon;
246 wc.hCursor = (HCURSOR)LoadImage(NULL, MAKEINTRESOURCE(IDC_ARROW),
247 IMAGE_CURSOR, 0, 0, LR_DEFAULTSIZE | LR_SHARED);
248 wc.hbrBackground = (HBRUSH)COLOR_APPWORKSPACE;
249 wc.lpszMenuName = NULL;
250 wc.lpszClassName = L"WinClass";
251 if (RegisterClassEx(&wc) == 0)
257 AdjustWindowRect(&r, WS_OVERLAPPEDWINDOW, FALSE);
258 x = r.right - r.left;
259 y = r.bottom - r.top;
263 dw.hw = CreateWindowA("WinClass", t, WS_OVERLAPPEDWINDOW,
264 CW_USEDEFAULT, CW_USEDEFAULT, x, y, NULL, NULL, dw.hi, NULL);
267 ShowWindow(dw.hw, SW_SHOW);
269 SetTimer(dw.hw, TIMER_ID, TIMER_INTERVAL, NULL);
270 SetTimer(dw.hw, TIMER_ID + 1, TIMER_INTERVAL * 10, NULL);
271 SetTimer(dw.hw, TIMER_ID + 2, TIMER_INTERVAL * 100, NULL);
275 i = GetMessage(&msg, NULL, 0, 0);
276 if (i == 0 || i == -1) /* エラーもしくは終了メッセージ */
278 /* そのほかはとりあえずデフォルト処理で */
279 TranslateMessage(&msg);
280 DispatchMessage(&msg);
282 // PostQuitMessage(0);
283 dw.flags |= 1; /* 終了, bld_waitNF()が見つける */
284 if (dw.winClosed != NULL)
289 void bld_openWin(int sx, int sy, char *winClosed)
293 dw.bmi.bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
294 dw.bmi.bmiHeader.biWidth = sx;
295 dw.bmi.bmiHeader.biHeight = -sy;
296 dw.bmi.bmiHeader.biPlanes = 1;
297 dw.bmi.bmiHeader.biBitCount = 32;
298 dw.bmi.bmiHeader.biCompression = BI_RGB;
299 dw.winClosed = winClosed;
301 threadhandle = CreateThread(NULL, 0, (void *)&winthread, NULL, 0, (void *)&i);
306 void drv_flshWin(int sx, int sy, int x0, int y0)
308 InvalidateRect(dw.hw, NULL, FALSE);
313 LRESULT CALLBACK WndProc(HWND hw, unsigned int msg, WPARAM wp, LPARAM lp)
316 if (msg == WM_PAINT) {
318 HDC hdc = BeginPaint(dw.hw, &ps);
319 SetDIBitsToDevice(hdc, 0, 0, w.win.xsiz, w.win.ysiz,
320 0, 0, 0, w.win.ysiz, w.win.buf, &dw.bmi, DIB_RGB_COLORS);
321 EndPaint(dw.hw, &ps);
323 if (msg == WM_DESTROY) {
327 if (msg == WM_TIMER && wp == TIMER_ID) {
328 w.tmcount += TIMER_INTERVAL;
331 if (msg == WM_TIMER && wp == TIMER_ID + 1) {
332 dw.tmcount1 += TIMER_INTERVAL * 10;
333 w.tmcount = dw.tmcount1;
336 if (msg == WM_TIMER && wp == TIMER_ID + 2) {
337 dw.tmcount2 += TIMER_INTERVAL * 100;
338 w.tmcount = dw.tmcount1 = dw.tmcount2;
341 if (msg == WM_KEYDOWN || msg == WM_SYSKEYDOWN) {
344 if (wp == VK_RETURN) i = KEY_ENTER;
345 if (wp == VK_ESCAPE) i = KEY_ESC;
346 if (wp == VK_BACK) i = KEY_BACKSPACE;
347 if (wp == VK_TAB) i = KEY_TAB;
348 if (wp == VK_PRIOR) i = KEY_PAGEUP;
349 if (wp == VK_NEXT) i = KEY_PAGEDWN;
350 if (wp == VK_END) i = KEY_END;
351 if (wp == VK_HOME) i = KEY_HOME;
352 if (wp == VK_LEFT) i = KEY_LEFT;
353 if (wp == VK_RIGHT) i = KEY_RIGHT;
354 if (wp == VK_UP) i = KEY_UP;
355 if (wp == VK_DOWN) i = KEY_DOWN;
356 if (wp == VK_INSERT) i = KEY_INS;
357 if (wp == VK_DELETE) i = KEY_DEL;
359 if ((GetKeyState(VK_LCONTROL) & (1 << 15)) != 0) j |= 1 << 17;
360 if ((GetKeyState(VK_LMENU) & (1 << 15)) != 0) j |= 1 << 18;
361 if ((GetKeyState(VK_RCONTROL) & (1 << 15)) != 0) j |= 1 << 25;
362 if ((GetKeyState(VK_RMENU) & (1 << 15)) != 0) j |= 1 << 26;
363 if ((GetKeyState(VK_RSHIFT) & (1 << 15)) != 0) i |= 1 << 24;
364 if ((GetKeyState(VK_LSHIFT) & (1 << 15)) != 0) i |= 1 << 16;
365 if ((GetKeyState(VK_NUMLOCK) & (1 << 0)) != 0) i |= 1 << 22;
366 if ((GetKeyState(VK_CAPITAL) & (1 << 0)) != 0) i |= 1 << 23;
368 if ('A' <= wp && wp <= 'Z') i = wp;
372 // bl_putKeyB(1, &i);
376 if (msg == WM_KEYUP) {
378 // bl_putKeyB(1, &i);
380 if (msg == WM_CHAR) {
382 if (' ' <= wp && wp <= 0x7e) {
385 if ((GetKeyState(VK_LCONTROL) & (1 << 15)) != 0) j |= 1 << 17;
386 if ((GetKeyState(VK_LMENU) & (1 << 15)) != 0) j |= 1 << 18;
387 if ((GetKeyState(VK_RCONTROL) & (1 << 15)) != 0) j |= 1 << 25;
388 if ((GetKeyState(VK_RMENU) & (1 << 15)) != 0) j |= 1 << 26;
389 if ((GetKeyState(VK_RSHIFT) & (1 << 15)) != 0) i |= 1 << 24;
390 if ((GetKeyState(VK_LSHIFT) & (1 << 15)) != 0) i |= 1 << 16;
391 if ((GetKeyState(VK_NUMLOCK) & (1 << 0)) != 0) i |= 1 << 22;
392 if ((GetKeyState(VK_CAPITAL) & (1 << 0)) != 0) i |= 1 << 23;
393 if (('A' <= wp && wp <= 'Z') || ('a' <= wp && wp <= 'z')) {
400 // bl_putKeyB(1, &i);
404 return DefWindowProc(hw, msg, wp, lp);
407 void drv_openWin(int sx, int sy, UCHAR *buf, char *winClosed)
409 w.win.buf = (int *)buf;
412 bld_openWin(sx, sy, winClosed);
416 void drv_sleep(int msec)
419 // MsgWaitForMultipleObjects(1, &threadhandle, FALSE, msec, QS_ALLINPUT);
420 /* 勉強不足でまだ書き方が分かりません! */
426 #error "Illegal OS type. Edit osecpu.h and look at DRV_OSNUM"