4 #if (DRV_OSNUM == 0x0002)
9 // by Liva, 2013.05.29-
11 #include <mach/mach.h>
12 #include <Cocoa/Cocoa.h>
14 void *mallocRWE(int bytes)
16 void *p = malloc(bytes);
17 vm_protect(mach_task_self(), (vm_address_t) p, bytes, FALSE, VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
23 @interface OSECPUView : NSView
28 CGContextRef _context;
31 - (id)initWithFrame:(NSRect)frameRect buf:(unsigned char *)buf sx:(int)sx sy:(int)sy;
32 - (void)drawRect:(NSRect)rect;
35 @implementation OSECPUView
36 - (id)initWithFrame:(NSRect)frameRect buf:(unsigned char *)buf sx:(int)sx sy:(int)sy
38 self = [super initWithFrame:frameRect];
47 - (void)drawRect:(NSRect)rect {
48 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
49 _context = CGBitmapContextCreate (_buf, _sx, _sy, 8, 4 * _sx, colorSpace, (kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst));
50 CGImageRef image = CGBitmapContextCreateImage(_context);
51 CGContextRef currentContext = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
52 CGContextDrawImage(currentContext, NSRectToCGRect(rect), image);
54 CFRelease(colorSpace);
60 @interface Main : NSObject<NSWindowDelegate>
63 const unsigned char **argv;
69 - (void)createThread:(int)_argc args:(const unsigned char **)_argv;
70 - (BOOL)windowShouldClose:(id)sender;
71 - (void)openWin:(unsigned char *)buf sx:(int)sx sy:(int)sy winClosed:(char *)_winClosed;
72 - (void)flushWin:(NSRect)rect;
78 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
79 HeavyOSECPUMain(argc, (char **)argv);
80 [NSApp terminate:self];
84 - (void)createThread : (int)_argc args:(const unsigned char **)_argv
88 NSThread *thread = [[[NSThread alloc] initWithTarget:self selector:@selector(runApp) object:nil] autorelease];
92 - (BOOL)windowShouldClose:(id)sender
98 - (void)openWin:(unsigned char *)buf sx:(int)sx sy:(int) sy winClosed:(char *)_winClosed
101 NSWindow* window = [[NSWindow alloc] initWithContentRect: NSMakeRect(0, 0, sx, sy) styleMask: NSTitledWindowMask | NSMiniaturizableWindowMask | NSClosableWindowMask backing: NSBackingStoreBuffered defer: NO];
102 [window setTitle: @"osecpu"];
104 [window makeKeyAndOrderFront:nil];
105 [window setReleasedWhenClosed:YES];
106 window.delegate = self;
107 winClosed = _winClosed;
109 _view = [[OSECPUView alloc] initWithFrame:NSMakeRect(0,0,sx,sy) buf:buf sx:sx sy:sy];
110 [window.contentView addSubview:_view];
113 - (void)flushWin : (NSRect)rect
115 [_view drawRect:rect];
122 int main(int argc, char **argv)
124 objc_main = [[Main alloc] init];
126 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
127 app = [[NSApplication alloc] init];
128 [objc_main createThread:argc args:(const unsigned char **)argv];
134 void drv_openWin(int sx, int sy, unsigned char *buf, char *winClosed)
136 [objc_main openWin:buf sx:sx sy:sy winClosed:winClosed];
139 void drv_flshWin(int sx, int sy, int x0, int y0)
141 [objc_main flushWin:NSMakeRect(x0,y0,sx,sy)];
144 void drv_sleep(int msec)
146 [NSThread sleepForTimeInterval:0.001*msec];
151 #elif (DRV_OSNUM == 0x0003)
153 #error "Your OS is not supported."
155 #elif (DRV_OSNUM == 0x0001)
157 // for Windows 32-bit
162 #define TIMER_INTERVAL 10
168 int tmcount1, tmcount2, flags, smp; /* bit0: 終了 */
173 struct BLD_WORK bld_work;
176 int xsiz, ysiz, *buf;
182 int csiz_x, csiz_y, cx, cy, col0, col1, tabsiz, slctwin;
183 int tmcount, tmcount0, mod, rand_seed;
186 unsigned char **fptn;
188 int *kbuf, kbuf_rp, kbuf_wp, kbuf_c;
191 struct BL_WORK bl_work;
193 #define BL_SIZ_KBUF 8192
195 #define BL_WAITKEYF 0x00000001
196 #define BL_WAITKEYNF 0x00000002
197 #define BL_WAITKEY 0x00000003
198 #define BL_GETKEY 0x00000004
199 #define BL_CLEARREP 0x00000008
200 #define BL_DELFFF 0x00000010
202 #define BL_KEYMODE 0x00000000 // 作りかけ, make/remake/breakが見えるかどうか
207 void bld_openWin(int x, int y, char *winClosed);
208 void bld_flshWin(int sx, int sy, int x0, int y0);
209 LRESULT CALLBACK WndProc(HWND hw, unsigned int msg, WPARAM wp, LPARAM lp);
212 void bl_readyWin(int n);
214 static HANDLE threadhandle;
216 int main(int argc, char **argv)
218 // Program entry point
219 return HeavyOSECPUMain(argc, argv);
222 void *mallocRWE(int bytes)
224 void *p = malloc(bytes);
226 VirtualProtect(p, bytes, PAGE_EXECUTE_READWRITE, &dmy);
230 static int winthread(void *dmy)
237 x = dw.bmi.bmiHeader.biWidth;
238 y = -dw.bmi.bmiHeader.biHeight;
240 wc.cbSize = sizeof (WNDCLASSEX);
241 wc.style = CS_HREDRAW | CS_VREDRAW;
242 wc.lpfnWndProc = WndProc;
245 wc.hInstance = dw.hi;
246 wc.hIcon = (HICON)LoadImage(NULL, MAKEINTRESOURCE(IDI_APPLICATION),
247 IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_SHARED);
248 wc.hIconSm = wc.hIcon;
249 wc.hCursor = (HCURSOR)LoadImage(NULL, MAKEINTRESOURCE(IDC_ARROW),
250 IMAGE_CURSOR, 0, 0, LR_DEFAULTSIZE | LR_SHARED);
251 wc.hbrBackground = (HBRUSH)COLOR_APPWORKSPACE;
252 wc.lpszMenuName = NULL;
253 wc.lpszClassName = L"WinClass";
254 if (RegisterClassEx(&wc) == 0)
260 AdjustWindowRect(&r, WS_OVERLAPPEDWINDOW, FALSE);
261 x = r.right - r.left;
262 y = r.bottom - r.top;
266 dw.hw = CreateWindowA("WinClass", t, WS_OVERLAPPEDWINDOW,
267 CW_USEDEFAULT, CW_USEDEFAULT, x, y, NULL, NULL, dw.hi, NULL);
270 ShowWindow(dw.hw, SW_SHOW);
272 SetTimer(dw.hw, TIMER_ID, TIMER_INTERVAL, NULL);
273 SetTimer(dw.hw, TIMER_ID + 1, TIMER_INTERVAL * 10, NULL);
274 SetTimer(dw.hw, TIMER_ID + 2, TIMER_INTERVAL * 100, NULL);
278 i = GetMessage(&msg, NULL, 0, 0);
279 if (i == 0 || i == -1) /* エラーもしくは終了メッセージ */
281 /* そのほかはとりあえずデフォルト処理で */
282 TranslateMessage(&msg);
283 DispatchMessage(&msg);
285 // PostQuitMessage(0);
286 dw.flags |= 1; /* 終了, bld_waitNF()が見つける */
287 if (dw.winClosed != NULL)
292 void bld_openWin(int sx, int sy, char *winClosed)
296 dw.bmi.bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
297 dw.bmi.bmiHeader.biWidth = sx;
298 dw.bmi.bmiHeader.biHeight = -sy;
299 dw.bmi.bmiHeader.biPlanes = 1;
300 dw.bmi.bmiHeader.biBitCount = 32;
301 dw.bmi.bmiHeader.biCompression = BI_RGB;
302 dw.winClosed = winClosed;
304 threadhandle = CreateThread(NULL, 0, (void *)&winthread, NULL, 0, (void *)&i);
309 void drv_flshWin(int sx, int sy, int x0, int y0)
311 InvalidateRect(dw.hw, NULL, FALSE);
316 LRESULT CALLBACK WndProc(HWND hw, unsigned int msg, WPARAM wp, LPARAM lp)
319 if (msg == WM_PAINT) {
321 HDC hdc = BeginPaint(dw.hw, &ps);
322 SetDIBitsToDevice(hdc, 0, 0, w.win.xsiz, w.win.ysiz,
323 0, 0, 0, w.win.ysiz, w.win.buf, &dw.bmi, DIB_RGB_COLORS);
324 EndPaint(dw.hw, &ps);
326 if (msg == WM_DESTROY) {
330 if (msg == WM_TIMER && wp == TIMER_ID) {
331 w.tmcount += TIMER_INTERVAL;
334 if (msg == WM_TIMER && wp == TIMER_ID + 1) {
335 dw.tmcount1 += TIMER_INTERVAL * 10;
336 w.tmcount = dw.tmcount1;
339 if (msg == WM_TIMER && wp == TIMER_ID + 2) {
340 dw.tmcount2 += TIMER_INTERVAL * 100;
341 w.tmcount = dw.tmcount1 = dw.tmcount2;
344 if (msg == WM_KEYDOWN || msg == WM_SYSKEYDOWN) {
347 if (wp == VK_RETURN) i = KEY_ENTER;
348 if (wp == VK_ESCAPE) i = KEY_ESC;
349 if (wp == VK_BACK) i = KEY_BACKSPACE;
350 if (wp == VK_TAB) i = KEY_TAB;
351 if (wp == VK_PRIOR) i = KEY_PAGEUP;
352 if (wp == VK_NEXT) i = KEY_PAGEDWN;
353 if (wp == VK_END) i = KEY_END;
354 if (wp == VK_HOME) i = KEY_HOME;
355 if (wp == VK_LEFT) i = KEY_LEFT;
356 if (wp == VK_RIGHT) i = KEY_RIGHT;
357 if (wp == VK_UP) i = KEY_UP;
358 if (wp == VK_DOWN) i = KEY_DOWN;
359 if (wp == VK_INSERT) i = KEY_INS;
360 if (wp == VK_DELETE) i = KEY_DEL;
362 if ((GetKeyState(VK_LCONTROL) & (1 << 15)) != 0) j |= 1 << 17;
363 if ((GetKeyState(VK_LMENU) & (1 << 15)) != 0) j |= 1 << 18;
364 if ((GetKeyState(VK_RCONTROL) & (1 << 15)) != 0) j |= 1 << 25;
365 if ((GetKeyState(VK_RMENU) & (1 << 15)) != 0) j |= 1 << 26;
366 if ((GetKeyState(VK_RSHIFT) & (1 << 15)) != 0) i |= 1 << 24;
367 if ((GetKeyState(VK_LSHIFT) & (1 << 15)) != 0) i |= 1 << 16;
368 if ((GetKeyState(VK_NUMLOCK) & (1 << 0)) != 0) i |= 1 << 22;
369 if ((GetKeyState(VK_CAPITAL) & (1 << 0)) != 0) i |= 1 << 23;
371 if ('A' <= wp && wp <= 'Z') i = wp;
375 // bl_putKeyB(1, &i);
379 if (msg == WM_KEYUP) {
381 // bl_putKeyB(1, &i);
383 if (msg == WM_CHAR) {
385 if (' ' <= wp && wp <= 0x7e) {
388 if ((GetKeyState(VK_LCONTROL) & (1 << 15)) != 0) j |= 1 << 17;
389 if ((GetKeyState(VK_LMENU) & (1 << 15)) != 0) j |= 1 << 18;
390 if ((GetKeyState(VK_RCONTROL) & (1 << 15)) != 0) j |= 1 << 25;
391 if ((GetKeyState(VK_RMENU) & (1 << 15)) != 0) j |= 1 << 26;
392 if ((GetKeyState(VK_RSHIFT) & (1 << 15)) != 0) i |= 1 << 24;
393 if ((GetKeyState(VK_LSHIFT) & (1 << 15)) != 0) i |= 1 << 16;
394 if ((GetKeyState(VK_NUMLOCK) & (1 << 0)) != 0) i |= 1 << 22;
395 if ((GetKeyState(VK_CAPITAL) & (1 << 0)) != 0) i |= 1 << 23;
396 if (('A' <= wp && wp <= 'Z') || ('a' <= wp && wp <= 'z')) {
403 // bl_putKeyB(1, &i);
407 return DefWindowProc(hw, msg, wp, lp);
410 void drv_openWin(int sx, int sy, UCHAR *buf, char *winClosed)
412 w.win.buf = (int *)buf;
415 bld_openWin(sx, sy, winClosed);
419 void drv_sleep(int msec)
422 // MsgWaitForMultipleObjects(1, &threadhandle, FALSE, msec, QS_ALLINPUT);
423 /* 勉強不足でまだ書き方が分かりません! */
429 #error "Illegal OS type. Edit osecpu.h and look at DRV_OSNUM"