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 {
27 CGContextRef _context;
30 - (id)initWithFrame:(NSRect)frameRect
31 buf : (unsigned char *) buf
35 - (void)drawRect:(NSRect)rect;
38 @implementation OSECPUView
39 - (id)initWithFrame:(NSRect)frameRect
40 buf : (unsigned char *) buf
44 self = [super initWithFrame:frameRect];
53 - (void)drawRect:(NSRect)rect {
54 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
55 _context = CGBitmapContextCreate (_buf, _sx, _sy, 8, 4 * _sx, colorSpace, (kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst));
56 CGImageRef image = CGBitmapContextCreateImage(_context);
57 CGContextRef currentContext = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
58 CGContextDrawImage(currentContext, NSRectToCGRect(rect), image);
63 @interface Main : NSObject<NSWindowDelegate> {
65 const unsigned char **argv;
71 - (void)createThread : (int)_argc
72 args : (const unsigned char **)_argv;
73 - (BOOL)windowShouldClose:(id)sender;
74 - (void)openWin : (unsigned char *)buf
77 winClosed : (char *)_winClosed;
78 - (void)flushWin : (NSRect) rect;
84 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
85 HeavyOSECPUMain(argc, (char **)argv);
86 [NSApp terminate:self];
90 - (void)createThread : (int)_argc
91 args : (const unsigned char **)_argv
95 NSThread *thread = [[[NSThread alloc] initWithTarget:self selector:@selector(runApp) object:nil] autorelease];
99 - (BOOL)windowShouldClose:(id)sender
105 - (void)openWin : (unsigned char *)buf
108 winClosed : (char *)_winClosed
111 NSWindow* window = [[NSWindow alloc] initWithContentRect: NSMakeRect(0, 0, sx, sy) styleMask: NSTitledWindowMask | NSMiniaturizableWindowMask | NSClosableWindowMask backing: NSBackingStoreBuffered defer: NO];
112 [window setTitle: @"osecpu"];
114 [window makeKeyAndOrderFront:nil];
115 [window setReleasedWhenClosed:YES];
116 window.delegate = self;
117 winClosed = _winClosed;
119 _view = [[OSECPUView alloc] initWithFrame:NSMakeRect(0,0,sx,sy) buf:buf sx:sx sy:sy];
120 [window.contentView addSubview:_view];
123 - (void)flushWin : (NSRect) rect
125 [_view drawRect:rect];
132 int main(int argc, char **argv)
134 objc_main = [[Main alloc] init];
136 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
137 app = [[NSApplication alloc] init];
138 [objc_main createThread:argc args:(const unsigned char **)argv];
144 void drv_openWin(int sx, int sy, unsigned char *buf, char *winClosed)
146 [objc_main openWin:buf sx:sx sy:sy winClosed:winClosed];
149 void drv_flshWin(int sx, int sy, int x0, int y0)
151 [objc_main flushWin:NSMakeRect(x0,y0,sx,sy)];
154 void drv_sleep(int msec)
156 [NSThread sleepForTimeInterval:0.001*msec];
161 #elif (DRV_OSNUM == 0x0003)
163 #error "Your OS is not supported."
165 #elif (DRV_OSNUM == 0x0001)
167 // for Windows 32-bit
172 #define TIMER_INTERVAL 10
178 int tmcount1, tmcount2, flags, smp; /* bit0: 終了 */
183 struct BLD_WORK bld_work;
186 int xsiz, ysiz, *buf;
192 int csiz_x, csiz_y, cx, cy, col0, col1, tabsiz, slctwin;
193 int tmcount, tmcount0, mod, rand_seed;
196 unsigned char **fptn;
198 int *kbuf, kbuf_rp, kbuf_wp, kbuf_c;
201 struct BL_WORK bl_work;
203 #define BL_SIZ_KBUF 8192
205 #define BL_WAITKEYF 0x00000001
206 #define BL_WAITKEYNF 0x00000002
207 #define BL_WAITKEY 0x00000003
208 #define BL_GETKEY 0x00000004
209 #define BL_CLEARREP 0x00000008
210 #define BL_DELFFF 0x00000010
212 #define BL_KEYMODE 0x00000000 // 作りかけ, make/remake/breakが見えるかどうか
217 void bld_openWin(int x, int y, char *winClosed);
218 void bld_flshWin(int sx, int sy, int x0, int y0);
219 LRESULT CALLBACK WndProc(HWND hw, unsigned int msg, WPARAM wp, LPARAM lp);
222 void bl_readyWin(int n);
224 static HANDLE threadhandle;
226 int main(int argc, char **argv)
228 // Program entry point
229 return HeavyOSECPUMain(argc, argv);
232 void *mallocRWE(int bytes)
234 void *p = malloc(bytes);
236 VirtualProtect(p, bytes, PAGE_EXECUTE_READWRITE, &dmy);
240 static int winthread(void *dmy)
247 x = dw.bmi.bmiHeader.biWidth;
248 y = -dw.bmi.bmiHeader.biHeight;
250 wc.cbSize = sizeof (WNDCLASSEX);
251 wc.style = CS_HREDRAW | CS_VREDRAW;
252 wc.lpfnWndProc = WndProc;
255 wc.hInstance = dw.hi;
256 wc.hIcon = (HICON)LoadImage(NULL, MAKEINTRESOURCE(IDI_APPLICATION),
257 IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_SHARED);
258 wc.hIconSm = wc.hIcon;
259 wc.hCursor = (HCURSOR)LoadImage(NULL, MAKEINTRESOURCE(IDC_ARROW),
260 IMAGE_CURSOR, 0, 0, LR_DEFAULTSIZE | LR_SHARED);
261 wc.hbrBackground = (HBRUSH)COLOR_APPWORKSPACE;
262 wc.lpszMenuName = NULL;
263 wc.lpszClassName = L"WinClass";
264 if (RegisterClassEx(&wc) == 0)
270 AdjustWindowRect(&r, WS_OVERLAPPEDWINDOW, FALSE);
271 x = r.right - r.left;
272 y = r.bottom - r.top;
276 dw.hw = CreateWindowA("WinClass", t, WS_OVERLAPPEDWINDOW,
277 CW_USEDEFAULT, CW_USEDEFAULT, x, y, NULL, NULL, dw.hi, NULL);
280 ShowWindow(dw.hw, SW_SHOW);
282 SetTimer(dw.hw, TIMER_ID, TIMER_INTERVAL, NULL);
283 SetTimer(dw.hw, TIMER_ID + 1, TIMER_INTERVAL * 10, NULL);
284 SetTimer(dw.hw, TIMER_ID + 2, TIMER_INTERVAL * 100, NULL);
288 i = GetMessage(&msg, NULL, 0, 0);
289 if (i == 0 || i == -1) /* エラーもしくは終了メッセージ */
291 /* そのほかはとりあえずデフォルト処理で */
292 TranslateMessage(&msg);
293 DispatchMessage(&msg);
295 // PostQuitMessage(0);
296 dw.flags |= 1; /* 終了, bld_waitNF()が見つける */
297 if (dw.winClosed != NULL)
302 void bld_openWin(int sx, int sy, char *winClosed)
306 dw.bmi.bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
307 dw.bmi.bmiHeader.biWidth = sx;
308 dw.bmi.bmiHeader.biHeight = -sy;
309 dw.bmi.bmiHeader.biPlanes = 1;
310 dw.bmi.bmiHeader.biBitCount = 32;
311 dw.bmi.bmiHeader.biCompression = BI_RGB;
312 dw.winClosed = winClosed;
314 threadhandle = CreateThread(NULL, 0, (void *)&winthread, NULL, 0, (void *)&i);
319 void drv_flshWin(int sx, int sy, int x0, int y0)
321 InvalidateRect(dw.hw, NULL, FALSE);
326 LRESULT CALLBACK WndProc(HWND hw, unsigned int msg, WPARAM wp, LPARAM lp)
329 if (msg == WM_PAINT) {
331 HDC hdc = BeginPaint(dw.hw, &ps);
332 SetDIBitsToDevice(hdc, 0, 0, w.win.xsiz, w.win.ysiz,
333 0, 0, 0, w.win.ysiz, w.win.buf, &dw.bmi, DIB_RGB_COLORS);
334 EndPaint(dw.hw, &ps);
336 if (msg == WM_DESTROY) {
340 if (msg == WM_TIMER && wp == TIMER_ID) {
341 w.tmcount += TIMER_INTERVAL;
344 if (msg == WM_TIMER && wp == TIMER_ID + 1) {
345 dw.tmcount1 += TIMER_INTERVAL * 10;
346 w.tmcount = dw.tmcount1;
349 if (msg == WM_TIMER && wp == TIMER_ID + 2) {
350 dw.tmcount2 += TIMER_INTERVAL * 100;
351 w.tmcount = dw.tmcount1 = dw.tmcount2;
354 if (msg == WM_KEYDOWN || msg == WM_SYSKEYDOWN) {
357 if (wp == VK_RETURN) i = KEY_ENTER;
358 if (wp == VK_ESCAPE) i = KEY_ESC;
359 if (wp == VK_BACK) i = KEY_BACKSPACE;
360 if (wp == VK_TAB) i = KEY_TAB;
361 if (wp == VK_PRIOR) i = KEY_PAGEUP;
362 if (wp == VK_NEXT) i = KEY_PAGEDWN;
363 if (wp == VK_END) i = KEY_END;
364 if (wp == VK_HOME) i = KEY_HOME;
365 if (wp == VK_LEFT) i = KEY_LEFT;
366 if (wp == VK_RIGHT) i = KEY_RIGHT;
367 if (wp == VK_UP) i = KEY_UP;
368 if (wp == VK_DOWN) i = KEY_DOWN;
369 if (wp == VK_INSERT) i = KEY_INS;
370 if (wp == VK_DELETE) i = KEY_DEL;
372 if ((GetKeyState(VK_LCONTROL) & (1 << 15)) != 0) j |= 1 << 17;
373 if ((GetKeyState(VK_LMENU) & (1 << 15)) != 0) j |= 1 << 18;
374 if ((GetKeyState(VK_RCONTROL) & (1 << 15)) != 0) j |= 1 << 25;
375 if ((GetKeyState(VK_RMENU) & (1 << 15)) != 0) j |= 1 << 26;
376 if ((GetKeyState(VK_RSHIFT) & (1 << 15)) != 0) i |= 1 << 24;
377 if ((GetKeyState(VK_LSHIFT) & (1 << 15)) != 0) i |= 1 << 16;
378 if ((GetKeyState(VK_NUMLOCK) & (1 << 0)) != 0) i |= 1 << 22;
379 if ((GetKeyState(VK_CAPITAL) & (1 << 0)) != 0) i |= 1 << 23;
381 if ('A' <= wp && wp <= 'Z') i = wp;
385 // bl_putKeyB(1, &i);
389 if (msg == WM_KEYUP) {
391 // bl_putKeyB(1, &i);
393 if (msg == WM_CHAR) {
395 if (' ' <= wp && wp <= 0x7e) {
398 if ((GetKeyState(VK_LCONTROL) & (1 << 15)) != 0) j |= 1 << 17;
399 if ((GetKeyState(VK_LMENU) & (1 << 15)) != 0) j |= 1 << 18;
400 if ((GetKeyState(VK_RCONTROL) & (1 << 15)) != 0) j |= 1 << 25;
401 if ((GetKeyState(VK_RMENU) & (1 << 15)) != 0) j |= 1 << 26;
402 if ((GetKeyState(VK_RSHIFT) & (1 << 15)) != 0) i |= 1 << 24;
403 if ((GetKeyState(VK_LSHIFT) & (1 << 15)) != 0) i |= 1 << 16;
404 if ((GetKeyState(VK_NUMLOCK) & (1 << 0)) != 0) i |= 1 << 22;
405 if ((GetKeyState(VK_CAPITAL) & (1 << 0)) != 0) i |= 1 << 23;
406 if (('A' <= wp && wp <= 'Z') || ('a' <= wp && wp <= 'z')) {
413 // bl_putKeyB(1, &i);
417 return DefWindowProc(hw, msg, wp, lp);
420 void drv_openWin(int sx, int sy, UCHAR *buf, char *winClosed)
422 w.win.buf = (int *)buf;
425 bld_openWin(sx, sy, winClosed);
429 void drv_sleep(int msec)
432 // MsgWaitForMultipleObjects(1, &threadhandle, FALSE, msec, QS_ALLINPUT);
433 /* 勉強不足でまだ書き方が分かりません! */
439 #error "Illegal OS type. Edit switch.h and look at DRV_OSNUM"