4 #if (DRV_OSNUM == 0x0002)
\r
6 // for Mac OSX 32-bit
\r
8 #include <mach/mach.h>
\r
9 #include <Cocoa/Cocoa.h>
\r
11 @interface OSECPUView : NSView
\r
13 unsigned char *_buf;
\r
16 CGContextRef _context;
\r
19 - (id)initWithFrame:(NSRect)frameRect buf:(unsigned char *)buf sx:(int)sx sy:(int)sy;
\r
20 - (void)drawRect:(NSRect)rect;
\r
23 @implementation OSECPUView
\r
24 - (id)initWithFrame:(NSRect)frameRect buf:(unsigned char *)buf sx:(int)sx sy:(int)sy
\r
26 self = [super initWithFrame:frameRect];
\r
35 - (void)drawRect:(NSRect)rect {
\r
36 CGColorSpaceRef colorSpace;
\r
38 CGContextRef currentContext;
\r
40 currentContext = [[NSGraphicsContext currentContext] graphicsPort];
\r
41 colorSpace= CGColorSpaceCreateDeviceRGB();
\r
43 // ビットマップコンテキストにvramイメージを生成
\r
44 _context = CGBitmapContextCreate (_buf, _sx, _sy, 8, 4 * _sx, colorSpace, (kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst));
\r
45 image = CGBitmapContextCreateImage(_context);
\r
47 // 生成したvramイメージをViewに描画
\r
48 CGContextDrawImage(currentContext, NSRectToCGRect(rect), image);
\r
51 CFRelease(colorSpace);
\r
55 - (void)keyDown:(NSEvent *)theEvent
\r
60 if([[theEvent characters] length] != 1){
\r
64 kc = [[theEvent characters] characterAtIndex:0];
\r
68 if (kc == '\r') i = KEY_ENTER;
\r
69 if (kc == 0x1b) i = KEY_ESC;
\r
70 if (kc == 0x7F) i = KEY_BACKSPACE;
\r
71 if (kc == '\t') i = KEY_TAB;
\r
72 if (kc == NSPageUpFunctionKey) i = KEY_PAGEUP;
\r
73 if (kc == NSPageDownFunctionKey) i = KEY_PAGEDWN;
\r
74 if (kc == NSEndFunctionKey) i = KEY_END;
\r
75 if (kc == NSHomeFunctionKey) i = KEY_HOME;
\r
76 if (kc == NSLeftArrowFunctionKey) i = KEY_LEFT;
\r
77 if (kc == NSRightArrowFunctionKey) i = KEY_RIGHT;
\r
78 if (kc == NSUpArrowFunctionKey) i = KEY_UP;
\r
79 if (kc == NSDownArrowFunctionKey) i = KEY_DOWN;
\r
80 if (kc == NSInsertFunctionKey) i = KEY_INS;
\r
81 if (kc == NSDeleteFunctionKey) i = KEY_DEL;
\r
83 // TODO : 下の方の右辺、iとjが混ざっているのは修正すべき?
\r
86 if ((GetKeyState(VK_LCONTROL) & (1 << 15)) != 0) j |= KEY_MODIFIER_CONTROL_L;
\r
87 if ((GetKeyState(VK_LMENU) & (1 << 15)) != 0) j |= KEY_MODIFIER_MENU_L;
\r
88 if ((GetKeyState(VK_RCONTROL) & (1 << 15)) != 0) j |= KEY_MODIFIER_CONTROL_R;
\r
89 if ((GetKeyState(VK_RMENU) & (1 << 15)) != 0) j |= KEY_MODIFIER_MENU_R;
\r
90 if ((GetKeyState(VK_RSHIFT) & (1 << 15)) != 0) i |= KEY_MODIFIER_SHIFT_R;
\r
91 if ((GetKeyState(VK_LSHIFT) & (1 << 15)) != 0) i |= KEY_MODIFIER_SHIFT_L;
\r
92 if ((GetKeyState(VK_NUMLOCK) & (1 << 0)) != 0) i |= KEY_MODIFIER_NUMLOCK;
\r
93 if ((GetKeyState(VK_CAPITAL) & (1 << 0)) != 0) i |= KEY_MODIFIER_CAPITAL;
\r
106 - (void)keyUp:(NSEvent *)theEvent
\r
113 @interface Main : NSObject<NSWindowDelegate>
\r
116 const unsigned char **argv;
\r
119 NSWindow *mainWindow;
\r
124 - (void)createThread:(int)_argc args:(const unsigned char **)_argv;
\r
125 - (BOOL)windowShouldClose:(id)sender;
\r
126 - (void)openWin:(unsigned char *)buf sx:(int)sx sy:(int)sy winClosed:(char *)_winClosed;
\r
127 - (void)flushWin:(NSRect)rect;
\r
130 @implementation Main
\r
134 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
\r
136 retv = HeavyOSECPUMain(argc, (char **)argv);
\r
137 [NSApp terminate:self];
\r
142 - (void)createThread : (int)_argc args:(const unsigned char **)_argv
\r
146 NSThread *thread = [[[NSThread alloc] initWithTarget:self selector:@selector(runApp) object:nil] autorelease];
\r
150 - (BOOL)windowShouldClose:(id)sender
\r
156 - (void)windowDidBecomeKey:(NSNotification *)notification
\r
158 [mainWindow makeFirstResponder:_view];
\r
161 - (void)openWin:(unsigned char *)buf sx:(int)sx sy:(int) sy winClosed:(char *)_winClosed
\r
163 mainWindow = [[NSWindow alloc] initWithContentRect: NSMakeRect(0, 0, sx, sy) styleMask: NSTitledWindowMask | NSMiniaturizableWindowMask | NSClosableWindowMask backing: NSBackingStoreBuffered defer: NO];
\r
164 mainWindow.delegate = self;
\r
165 [mainWindow setTitle: @"osecpu"];
\r
166 [mainWindow center];
\r
167 [mainWindow makeKeyAndOrderFront:self];
\r
168 [mainWindow setReleasedWhenClosed:YES];
\r
170 winClosed = _winClosed;
\r
172 _view = [[OSECPUView alloc] initWithFrame:NSMakeRect(0,0,sx,sy) buf:buf sx:sx sy:sy];
\r
173 [mainWindow.contentView addSubview:_view];
\r
176 - (void)flushWin : (NSRect)rect
\r
178 [_view setNeedsDisplayInRect:rect];
\r
185 int main(int argc, char **argv)
\r
188 NSAutoreleasePool* pool;
\r
189 ProcessSerialNumber psn = {0, kCurrentProcess};
\r
191 // ForegroundApplicationになれるように設定(こうしないとキーイベントが取得できない)
\r
192 TransformProcessType(&psn, kProcessTransformToForegroundApplication);
\r
194 objc_main = [[Main alloc] init];
\r
195 NSApp = [NSApplication sharedApplication];
\r
197 pool = [[NSAutoreleasePool alloc] init];
\r
198 [objc_main createThread:argc args:(const unsigned char **)argv];
\r
204 void *mallocRWE(int bytes)
\r
206 void *p = malloc(bytes);
\r
207 vm_protect(mach_task_self(), (vm_address_t) p, bytes, FALSE, VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
\r
211 void drv_openWin(int sx, int sy, unsigned char *buf, char *winClosed)
\r
213 [objc_main openWin:buf sx:sx sy:sy winClosed:winClosed];
\r
216 void drv_flshWin(int sx, int sy, int x0, int y0)
\r
218 [objc_main flushWin:NSMakeRect(x0,y0,sx,sy)];
\r
221 void drv_sleep(int msec)
\r
223 [NSThread sleepForTimeInterval:0.001*msec];
\r
228 #elif (DRV_OSNUM == 0x0003)
\r
230 #error "Your OS is not supported."
\r
232 #elif (DRV_OSNUM == 0x0001)
\r
234 // for Windows 32-bit
\r
236 #include <windows.h>
\r
239 #define TIMER_INTERVAL 10
\r
245 int tmcount1, tmcount2, flags, smp; /* bit0: 終了 */
\r
250 struct BLD_WORK bld_work;
\r
253 int xsiz, ysiz, *buf;
\r
259 int csiz_x, csiz_y, cx, cy, col0, col1, tabsiz, slctwin;
\r
260 int tmcount, tmcount0, mod, rand_seed;
\r
262 unsigned char *ftyp;
\r
263 unsigned char **fptn;
\r
265 int *kbuf, kbuf_rp, kbuf_wp, kbuf_c;
\r
268 struct BL_WORK bl_work;
\r
270 #define BL_SIZ_KBUF 8192
\r
272 #define BL_WAITKEYF 0x00000001
\r
273 #define BL_WAITKEYNF 0x00000002
\r
274 #define BL_WAITKEY 0x00000003
\r
275 #define BL_GETKEY 0x00000004
\r
276 #define BL_CLEARREP 0x00000008
\r
277 #define BL_DELFFF 0x00000010
\r
279 #define BL_KEYMODE 0x00000000 // 作りかけ, make/remake/breakが見えるかどうか
\r
282 #define dw bld_work
\r
284 void bld_openWin(int x, int y, char *winClosed);
\r
285 void bld_flshWin(int sx, int sy, int x0, int y0);
\r
286 LRESULT CALLBACK WndProc(HWND hw, unsigned int msg, WPARAM wp, LPARAM lp);
\r
288 int bl_iCol(int i);
\r
289 void bl_readyWin(int n);
\r
291 static HANDLE threadhandle;
\r
293 int main(int argc, char **argv)
\r
295 // Program entry point
\r
296 return HeavyOSECPUMain(argc, argv);
\r
299 void *mallocRWE(int bytes)
\r
301 void *p = malloc(bytes);
\r
303 VirtualProtect(p, bytes, PAGE_EXECUTE_READWRITE, &dmy);
\r
307 static int winthread(void *dmy)
\r
314 x = dw.bmi.bmiHeader.biWidth;
\r
315 y = -dw.bmi.bmiHeader.biHeight;
\r
317 wc.cbSize = sizeof (WNDCLASSEX);
\r
318 wc.style = CS_HREDRAW | CS_VREDRAW;
\r
319 wc.lpfnWndProc = WndProc;
\r
322 wc.hInstance = dw.hi;
\r
323 wc.hIcon = (HICON)LoadImage(NULL, MAKEINTRESOURCE(IDI_APPLICATION),
\r
324 IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_SHARED);
\r
325 wc.hIconSm = wc.hIcon;
\r
326 wc.hCursor = (HCURSOR)LoadImage(NULL, MAKEINTRESOURCE(IDC_ARROW),
\r
327 IMAGE_CURSOR, 0, 0, LR_DEFAULTSIZE | LR_SHARED);
\r
328 wc.hbrBackground = (HBRUSH)COLOR_APPWORKSPACE;
\r
329 wc.lpszMenuName = NULL;
\r
330 wc.lpszClassName = "WinClass";
\r
331 if (RegisterClassEx(&wc) == 0)
\r
337 AdjustWindowRect(&r, WS_OVERLAPPEDWINDOW, FALSE);
\r
338 x = r.right - r.left;
\r
339 y = r.bottom - r.top;
\r
341 char *t = "osecpu";
\r
343 dw.hw = CreateWindowA("WinClass", t, WS_OVERLAPPEDWINDOW,
\r
344 CW_USEDEFAULT, CW_USEDEFAULT, x, y, NULL, NULL, dw.hi, NULL);
\r
347 ShowWindow(dw.hw, SW_SHOW);
\r
348 UpdateWindow(dw.hw);
\r
349 SetTimer(dw.hw, TIMER_ID, TIMER_INTERVAL, NULL);
\r
350 SetTimer(dw.hw, TIMER_ID + 1, TIMER_INTERVAL * 10, NULL);
\r
351 SetTimer(dw.hw, TIMER_ID + 2, TIMER_INTERVAL * 100, NULL);
\r
355 i = GetMessage(&msg, NULL, 0, 0);
\r
356 if (i == 0 || i == -1) /* エラーもしくは終了メッセージ */
\r
358 /* そのほかはとりあえずデフォルト処理で */
\r
359 TranslateMessage(&msg);
\r
360 DispatchMessage(&msg);
\r
362 // PostQuitMessage(0);
\r
363 dw.flags |= 1; /* 終了, bld_waitNF()が見つける */
\r
364 if (dw.winClosed != NULL)
\r
369 void bld_openWin(int sx, int sy, char *winClosed)
\r
373 dw.bmi.bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
\r
374 dw.bmi.bmiHeader.biWidth = sx;
\r
375 dw.bmi.bmiHeader.biHeight = -sy;
\r
376 dw.bmi.bmiHeader.biPlanes = 1;
\r
377 dw.bmi.bmiHeader.biBitCount = 32;
\r
378 dw.bmi.bmiHeader.biCompression = BI_RGB;
\r
379 dw.winClosed = winClosed;
\r
381 threadhandle = CreateThread(NULL, 0, (void *)&winthread, NULL, 0, (void *)&i);
\r
386 void drv_flshWin(int sx, int sy, int x0, int y0)
\r
388 InvalidateRect(dw.hw, NULL, FALSE);
\r
389 UpdateWindow(dw.hw);
\r
393 LRESULT CALLBACK WndProc(HWND hw, unsigned int msg, WPARAM wp, LPARAM lp)
\r
396 if (msg == WM_PAINT) {
\r
398 HDC hdc = BeginPaint(dw.hw, &ps);
\r
399 SetDIBitsToDevice(hdc, 0, 0, w.win.xsiz, w.win.ysiz,
\r
400 0, 0, 0, w.win.ysiz, w.win.buf, &dw.bmi, DIB_RGB_COLORS);
\r
401 EndPaint(dw.hw, &ps);
\r
403 if (msg == WM_DESTROY) {
\r
404 PostQuitMessage(0);
\r
407 if (msg == WM_TIMER && wp == TIMER_ID) {
\r
408 w.tmcount += TIMER_INTERVAL;
\r
411 if (msg == WM_TIMER && wp == TIMER_ID + 1) {
\r
412 dw.tmcount1 += TIMER_INTERVAL * 10;
\r
413 w.tmcount = dw.tmcount1;
\r
416 if (msg == WM_TIMER && wp == TIMER_ID + 2) {
\r
417 dw.tmcount2 += TIMER_INTERVAL * 100;
\r
418 w.tmcount = dw.tmcount1 = dw.tmcount2;
\r
421 if (msg == WM_KEYDOWN || msg == WM_SYSKEYDOWN) {
\r
424 if (wp == VK_RETURN) i = KEY_ENTER;
\r
425 if (wp == VK_ESCAPE) i = KEY_ESC;
\r
426 if (wp == VK_BACK) i = KEY_BACKSPACE;
\r
427 if (wp == VK_TAB) i = KEY_TAB;
\r
428 if (wp == VK_PRIOR) i = KEY_PAGEUP;
\r
429 if (wp == VK_NEXT) i = KEY_PAGEDWN;
\r
430 if (wp == VK_END) i = KEY_END;
\r
431 if (wp == VK_HOME) i = KEY_HOME;
\r
432 if (wp == VK_LEFT) i = KEY_LEFT;
\r
433 if (wp == VK_RIGHT) i = KEY_RIGHT;
\r
434 if (wp == VK_UP) i = KEY_UP;
\r
435 if (wp == VK_DOWN) i = KEY_DOWN;
\r
436 if (wp == VK_INSERT) i = KEY_INS;
\r
437 if (wp == VK_DELETE) i = KEY_DEL;
\r
439 // TODO : 下の方の右辺、iとjが混ざっているのは修正すべき?
\r
441 if ((GetKeyState(VK_LCONTROL) & (1 << 15)) != 0) j |= KEY_MODIFIER_CONTROL_L;
\r
442 if ((GetKeyState(VK_LMENU) & (1 << 15)) != 0) j |= KEY_MODIFIER_MENU_L;
\r
443 if ((GetKeyState(VK_RCONTROL) & (1 << 15)) != 0) j |= KEY_MODIFIER_CONTROL_R;
\r
444 if ((GetKeyState(VK_RMENU) & (1 << 15)) != 0) j |= KEY_MODIFIER_MENU_R;
\r
445 if ((GetKeyState(VK_RSHIFT) & (1 << 15)) != 0) i |= KEY_MODIFIER_SHIFT_R;
\r
446 if ((GetKeyState(VK_LSHIFT) & (1 << 15)) != 0) i |= KEY_MODIFIER_SHIFT_L;
\r
447 if ((GetKeyState(VK_NUMLOCK) & (1 << 0)) != 0) i |= KEY_MODIFIER_NUMLOCK;
\r
448 if ((GetKeyState(VK_CAPITAL) & (1 << 0)) != 0) i |= KEY_MODIFIER_CAPITAL;
\r
450 if ('A' <= wp && wp <= 'Z'){
\r
459 if (msg == WM_KEYUP) {
\r
462 if (msg == WM_CHAR) {
\r
464 if (' ' <= wp && wp <= 0x7e) {
\r
467 if ((GetKeyState(VK_LCONTROL) & (1 << 15)) != 0) j |= 1 << 17;
\r
468 if ((GetKeyState(VK_LMENU) & (1 << 15)) != 0) j |= 1 << 18;
\r
469 if ((GetKeyState(VK_RCONTROL) & (1 << 15)) != 0) j |= 1 << 25;
\r
470 if ((GetKeyState(VK_RMENU) & (1 << 15)) != 0) j |= 1 << 26;
\r
471 if ((GetKeyState(VK_RSHIFT) & (1 << 15)) != 0) i |= 1 << 24;
\r
472 if ((GetKeyState(VK_LSHIFT) & (1 << 15)) != 0) i |= 1 << 16;
\r
473 if ((GetKeyState(VK_NUMLOCK) & (1 << 0)) != 0) i |= 1 << 22;
\r
474 if ((GetKeyState(VK_CAPITAL) & (1 << 0)) != 0) i |= 1 << 23;
\r
475 if (('A' <= wp && wp <= 'Z') || ('a' <= wp && wp <= 'z')) {
\r
485 return DefWindowProc(hw, msg, wp, lp);
\r
488 void drv_openWin(int sx, int sy, UCHAR *buf, char *winClosed)
\r
490 w.win.buf = (int *)buf;
\r
493 bld_openWin(sx, sy, winClosed);
\r
497 void drv_sleep(int msec)
\r
500 // MsgWaitForMultipleObjects(1, &threadhandle, FALSE, msec, QS_ALLINPUT);
\r
501 /* 勉強不足でまだ書き方が分かりません! */
\r
507 #error "Illegal OS type. Edit osecpu.h and look at DRV_OSNUM"
\r