OSDN Git Service

54a520b075ada25148c5a5923a72bdbcfd91cdaa
[heavyosecpu/HeavyOSECPU.git] / dpndenv.c
1 #include "osecpu.h"\r
2 \r
3 \r
4 #if (DRV_OSNUM == 0x0002)\r
5 //\r
6 // for Mac OSX 32-bit\r
7 //\r
8 #include <mach/mach.h>\r
9 #include <Cocoa/Cocoa.h>\r
10 \r
11 void *mallocRWE(int bytes)\r
12 {\r
13         void *p = malloc(bytes);\r
14         vm_protect(mach_task_self(), (vm_address_t) p, bytes, FALSE, VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);\r
15         return p;\r
16 }\r
17 \r
18 NSApplication* app;\r
19 \r
20 @interface OSECPUView : NSView\r
21 {\r
22         unsigned char *_buf;\r
23         int _sx;\r
24         int _sy;\r
25         CGContextRef _context;\r
26 }\r
27 \r
28 - (id)initWithFrame:(NSRect)frameRect buf:(unsigned char *)buf sx:(int)sx sy:(int)sy;\r
29 - (void)drawRect:(NSRect)rect;\r
30 @end\r
31 \r
32 @implementation OSECPUView\r
33 - (id)initWithFrame:(NSRect)frameRect buf:(unsigned char *)buf sx:(int)sx sy:(int)sy\r
34 {\r
35         self = [super initWithFrame:frameRect];\r
36         if (self) {\r
37                 _buf = buf;\r
38                 _sx = sx;\r
39                 _sy = sy;\r
40         }\r
41         return self;\r
42 }\r
43 \r
44 - (void)drawRect:(NSRect)rect {\r
45         CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();\r
46         _context = CGBitmapContextCreate (_buf, _sx, _sy, 8, 4 * _sx, colorSpace, (kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst));\r
47         CGImageRef image = CGBitmapContextCreateImage(_context);\r
48         CGContextRef currentContext = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];\r
49         CGContextDrawImage(currentContext, NSRectToCGRect(rect), image);\r
50         \r
51         CFRelease(colorSpace);\r
52         CFRelease(image);\r
53 }\r
54 \r
55 @end\r
56 \r
57 @interface Main : NSObject<NSWindowDelegate>\r
58 {\r
59         int argc;\r
60         const unsigned char **argv;\r
61         char *winClosed;\r
62         OSECPUView *_view;\r
63 }\r
64 \r
65 - (void)runApp;\r
66 - (void)createThread:(int)_argc args:(const unsigned char **)_argv;\r
67 - (BOOL)windowShouldClose:(id)sender;\r
68 - (void)openWin:(unsigned char *)buf sx:(int)sx sy:(int)sy winClosed:(char *)_winClosed;\r
69 - (void)flushWin:(NSRect)rect;\r
70 @end\r
71 \r
72 @implementation Main\r
73 - (void)runApp\r
74 {\r
75         NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];\r
76         HeavyOSECPUMain(argc, (char **)argv);\r
77         [NSApp terminate:self];\r
78         [pool release];\r
79 }\r
80 \r
81 - (void)createThread : (int)_argc args:(const unsigned char **)_argv\r
82 {\r
83         argc = _argc;\r
84         argv = _argv;\r
85         NSThread *thread = [[[NSThread alloc] initWithTarget:self selector:@selector(runApp) object:nil] autorelease];\r
86         [thread start];\r
87 }\r
88 \r
89 - (BOOL)windowShouldClose:(id)sender\r
90 {\r
91         *winClosed = 1;\r
92         return YES;\r
93 }\r
94 \r
95 - (void)openWin:(unsigned char *)buf sx:(int)sx sy:(int) sy winClosed:(char *)_winClosed\r
96 {\r
97         \r
98         NSWindow* window = [[NSWindow alloc] initWithContentRect: NSMakeRect(0, 0, sx, sy) styleMask: NSTitledWindowMask | NSMiniaturizableWindowMask | NSClosableWindowMask backing: NSBackingStoreBuffered defer: NO];\r
99         [window setTitle: @"osecpu"];\r
100         [window center];\r
101         [window makeKeyAndOrderFront:nil];\r
102         [window setReleasedWhenClosed:YES];\r
103         window.delegate = self;\r
104         winClosed = _winClosed;\r
105         \r
106         _view = [[OSECPUView alloc] initWithFrame:NSMakeRect(0,0,sx,sy) buf:buf sx:sx sy:sy];\r
107         [window.contentView addSubview:_view];\r
108 }\r
109 \r
110 - (void)flushWin : (NSRect)rect\r
111 {\r
112         [_view drawRect:rect];\r
113 }\r
114 \r
115 @end\r
116 \r
117 id objc_main;\r
118 \r
119 int main(int argc, char **argv)\r
120 {\r
121         objc_main = [[Main alloc] init];\r
122         \r
123         NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];\r
124         app = [[NSApplication alloc] init];\r
125         [objc_main createThread:argc args:(const unsigned char **)argv];\r
126         [app run];\r
127         [pool release];\r
128         return 0;\r
129 }\r
130 \r
131 void drv_openWin(int sx, int sy, unsigned char *buf, char *winClosed)\r
132 {\r
133         [objc_main openWin:buf sx:sx sy:sy winClosed:winClosed];\r
134 }\r
135 \r
136 void drv_flshWin(int sx, int sy, int x0, int y0)\r
137 {\r
138         [objc_main flushWin:NSMakeRect(x0,y0,sx,sy)];\r
139 }\r
140 \r
141 void drv_sleep(int msec)\r
142 {\r
143         [NSThread sleepForTimeInterval:0.001*msec];\r
144         return;\r
145 }\r
146 \r
147 \r
148 #elif (DRV_OSNUM == 0x0003)\r
149 \r
150 #error "Your OS is not supported."\r
151 \r
152 #elif (DRV_OSNUM == 0x0001)\r
153 //\r
154 // for Windows 32-bit\r
155 //\r
156 #include <windows.h>\r
157 \r
158 #define TIMER_ID                         1\r
159 #define TIMER_INTERVAL          10\r
160 \r
161 struct BLD_WORK {\r
162         HINSTANCE hi;\r
163         HWND hw;\r
164         BITMAPINFO bmi;\r
165         int tmcount1, tmcount2, flags, smp; /* bit0: 終了 */\r
166         HANDLE mtx;\r
167         char *winClosed;\r
168 };\r
169 \r
170 struct BLD_WORK bld_work;\r
171 \r
172 struct BL_WIN {\r
173         int xsiz, ysiz, *buf;\r
174 };\r
175 \r
176 struct BL_WORK {\r
177         struct BL_WIN win;\r
178         jmp_buf jb;\r
179         int csiz_x, csiz_y, cx, cy, col0, col1, tabsiz, slctwin;\r
180         int tmcount, tmcount0, mod, rand_seed;\r
181         int *cbuf;\r
182         unsigned char *ftyp;\r
183         unsigned char **fptn;\r
184         int *ccol, *cbak;\r
185         int *kbuf, kbuf_rp, kbuf_wp, kbuf_c;\r
186 };\r
187 \r
188 struct BL_WORK bl_work;\r
189 \r
190 #define BL_SIZ_KBUF                     8192\r
191 \r
192 #define BL_WAITKEYF             0x00000001\r
193 #define BL_WAITKEYNF    0x00000002\r
194 #define BL_WAITKEY              0x00000003\r
195 #define BL_GETKEY               0x00000004\r
196 #define BL_CLEARREP             0x00000008\r
197 #define BL_DELFFF               0x00000010\r
198 \r
199 #define BL_KEYMODE              0x00000000      // 作りかけ, make/remake/breakが見えるかどうか\r
200 \r
201 #define w       bl_work\r
202 #define dw      bld_work\r
203 \r
204 void bld_openWin(int x, int y, char *winClosed);\r
205 void bld_flshWin(int sx, int sy, int x0, int y0);\r
206 LRESULT CALLBACK WndProc(HWND hw, unsigned int msg, WPARAM wp, LPARAM lp);\r
207 void bl_cls();\r
208 int bl_iCol(int i);\r
209 void bl_readyWin(int n);\r
210 \r
211 static HANDLE threadhandle;\r
212 \r
213 int main(int argc, char **argv)\r
214 {\r
215         // Program entry point\r
216         return HeavyOSECPUMain(argc, argv);\r
217 }\r
218 \r
219 void *mallocRWE(int bytes)\r
220 {\r
221         void *p = malloc(bytes);\r
222         DWORD dmy;\r
223         VirtualProtect(p, bytes, PAGE_EXECUTE_READWRITE, &dmy);\r
224         return p;\r
225 }\r
226 \r
227 static int winthread(void *dmy)\r
228 {\r
229         WNDCLASSEX wc;\r
230         RECT r;\r
231         int i, x, y;\r
232         MSG msg;\r
233         \r
234         x = dw.bmi.bmiHeader.biWidth;\r
235         y = -dw.bmi.bmiHeader.biHeight;\r
236         \r
237         wc.cbSize = sizeof (WNDCLASSEX);\r
238         wc.style = CS_HREDRAW | CS_VREDRAW;\r
239         wc.lpfnWndProc = WndProc;\r
240         wc.cbClsExtra = 0;\r
241         wc.cbWndExtra = 0;\r
242         wc.hInstance = dw.hi;\r
243         wc.hIcon = (HICON)LoadImage(NULL, MAKEINTRESOURCE(IDI_APPLICATION),\r
244                                                                 IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_SHARED);\r
245         wc.hIconSm = wc.hIcon;\r
246         wc.hCursor = (HCURSOR)LoadImage(NULL, MAKEINTRESOURCE(IDC_ARROW),\r
247                                                                         IMAGE_CURSOR, 0, 0, LR_DEFAULTSIZE | LR_SHARED);\r
248         wc.hbrBackground = (HBRUSH)COLOR_APPWORKSPACE;\r
249         wc.lpszMenuName = NULL;\r
250         wc.lpszClassName = L"WinClass";\r
251         if (RegisterClassEx(&wc) == 0)\r
252                 return 1;\r
253         r.left = 0;\r
254         r.top = 0;\r
255         r.right = x;\r
256         r.bottom = y;\r
257         AdjustWindowRect(&r, WS_OVERLAPPEDWINDOW, FALSE);\r
258         x = r.right - r.left;\r
259         y = r.bottom - r.top;\r
260         \r
261         char *t = "osecpu";\r
262         \r
263         dw.hw = CreateWindowA("WinClass", t, WS_OVERLAPPEDWINDOW,\r
264                                                   CW_USEDEFAULT, CW_USEDEFAULT, x, y, NULL, NULL, dw.hi, NULL);\r
265         if (dw.hw == NULL)\r
266                 return 1;\r
267         ShowWindow(dw.hw, SW_SHOW);\r
268         UpdateWindow(dw.hw);\r
269         SetTimer(dw.hw, TIMER_ID, TIMER_INTERVAL, NULL);\r
270         SetTimer(dw.hw, TIMER_ID + 1, TIMER_INTERVAL * 10, NULL);\r
271         SetTimer(dw.hw, TIMER_ID + 2, TIMER_INTERVAL * 100, NULL);\r
272         dw.flags |= 2 | 4;\r
273         \r
274         for (;;) {\r
275                 i = GetMessage(&msg, NULL, 0, 0);\r
276                 if (i == 0 || i == -1)  /* エラーもしくは終了メッセージ */\r
277                         break;\r
278                 /* そのほかはとりあえずデフォルト処理で */\r
279                 TranslateMessage(&msg);\r
280                 DispatchMessage(&msg);\r
281         }\r
282         //      PostQuitMessage(0);\r
283         dw.flags |= 1; /* 終了, bld_waitNF()が見つける */\r
284         if (dw.winClosed != NULL)\r
285                 *dw.winClosed = 1;\r
286         return 0;\r
287 }\r
288 \r
289 void bld_openWin(int sx, int sy, char *winClosed)\r
290 {\r
291         static int i;\r
292         \r
293         dw.bmi.bmiHeader.biSize = sizeof (BITMAPINFOHEADER);\r
294         dw.bmi.bmiHeader.biWidth = sx;\r
295         dw.bmi.bmiHeader.biHeight = -sy;\r
296         dw.bmi.bmiHeader.biPlanes = 1;\r
297         dw.bmi.bmiHeader.biBitCount = 32;\r
298         dw.bmi.bmiHeader.biCompression = BI_RGB;\r
299         dw.winClosed = winClosed;\r
300         \r
301         threadhandle = CreateThread(NULL, 0, (void *)&winthread, NULL, 0, (void *)&i);\r
302         \r
303         return;\r
304 }\r
305 \r
306 void drv_flshWin(int sx, int sy, int x0, int y0)\r
307 {\r
308         InvalidateRect(dw.hw, NULL, FALSE);\r
309         UpdateWindow(dw.hw);\r
310         return;\r
311 }\r
312 \r
313 LRESULT CALLBACK WndProc(HWND hw, unsigned int msg, WPARAM wp, LPARAM lp)\r
314 {\r
315         int i, j;\r
316         if (msg == WM_PAINT) {\r
317                 PAINTSTRUCT ps;\r
318                 HDC hdc = BeginPaint(dw.hw, &ps);\r
319                 SetDIBitsToDevice(hdc, 0, 0, w.win.xsiz, w.win.ysiz,\r
320                                                   0, 0, 0, w.win.ysiz, w.win.buf, &dw.bmi, DIB_RGB_COLORS);\r
321                 EndPaint(dw.hw, &ps);\r
322         }\r
323         if (msg == WM_DESTROY) {\r
324                 PostQuitMessage(0);\r
325                 return 0;\r
326         }\r
327         if (msg == WM_TIMER && wp == TIMER_ID) {\r
328                 w.tmcount += TIMER_INTERVAL;\r
329                 return 0;\r
330         }\r
331         if (msg == WM_TIMER && wp == TIMER_ID + 1) {\r
332                 dw.tmcount1 += TIMER_INTERVAL * 10;\r
333                 w.tmcount = dw.tmcount1;\r
334                 return 0;\r
335         }\r
336         if (msg == WM_TIMER && wp == TIMER_ID + 2) {\r
337                 dw.tmcount2 += TIMER_INTERVAL * 100;\r
338                 w.tmcount = dw.tmcount1 = dw.tmcount2;\r
339                 return 0;\r
340         }\r
341         if (msg == WM_KEYDOWN || msg == WM_SYSKEYDOWN) {\r
342                 i = -1;\r
343                 \r
344                 if (wp == VK_RETURN)    i = KEY_ENTER;\r
345                 if (wp == VK_ESCAPE)    i = KEY_ESC;\r
346                 if (wp == VK_BACK)              i = KEY_BACKSPACE;\r
347                 if (wp == VK_TAB)               i = KEY_TAB;\r
348                 if (wp == VK_PRIOR)             i = KEY_PAGEUP;\r
349                 if (wp == VK_NEXT)              i = KEY_PAGEDWN;\r
350                 if (wp == VK_END)               i = KEY_END;\r
351                 if (wp == VK_HOME)              i = KEY_HOME;\r
352                 if (wp == VK_LEFT)              i = KEY_LEFT;\r
353                 if (wp == VK_RIGHT)             i = KEY_RIGHT;\r
354                 if (wp == VK_UP)                i = KEY_UP;\r
355                 if (wp == VK_DOWN)              i = KEY_DOWN;\r
356                 if (wp == VK_INSERT)    i = KEY_INS;\r
357                 if (wp == VK_DELETE)    i = KEY_DEL;\r
358                 j &= 0;\r
359                 if ((GetKeyState(VK_LCONTROL) & (1 << 15)) != 0) j |= 1 << 17;\r
360                 if ((GetKeyState(VK_LMENU)    & (1 << 15)) != 0) j |= 1 << 18;\r
361                 if ((GetKeyState(VK_RCONTROL) & (1 << 15)) != 0) j |= 1 << 25;\r
362                 if ((GetKeyState(VK_RMENU)    & (1 << 15)) != 0) j |= 1 << 26;\r
363                 if ((GetKeyState(VK_RSHIFT)   & (1 << 15)) != 0) i |= 1 << 24;\r
364                 if ((GetKeyState(VK_LSHIFT)   & (1 << 15)) != 0) i |= 1 << 16;\r
365                 if ((GetKeyState(VK_NUMLOCK)  & (1 << 0)) != 0) i |= 1 << 22;\r
366                 if ((GetKeyState(VK_CAPITAL)  & (1 << 0)) != 0) i |= 1 << 23;\r
367                 if (j != 0) {\r
368                         if ('A' <= wp && wp <= 'Z') i = wp;\r
369                 }\r
370                 if (i != -1) {\r
371                         putKeybuf(i | j);\r
372                         //                      bl_putKeyB(1, &i);\r
373                         return 0;\r
374                 }\r
375         }\r
376         if (msg == WM_KEYUP) {\r
377                 i = 0xfff;\r
378                 //              bl_putKeyB(1, &i);\r
379         }\r
380         if (msg == WM_CHAR) {\r
381                 i = 0;\r
382                 if (' ' <= wp && wp <= 0x7e) {\r
383                         i = wp;\r
384                         j &= 0;\r
385                         if ((GetKeyState(VK_LCONTROL) & (1 << 15)) != 0) j |= 1 << 17;\r
386                         if ((GetKeyState(VK_LMENU)    & (1 << 15)) != 0) j |= 1 << 18;\r
387                         if ((GetKeyState(VK_RCONTROL) & (1 << 15)) != 0) j |= 1 << 25;\r
388                         if ((GetKeyState(VK_RMENU)    & (1 << 15)) != 0) j |= 1 << 26;\r
389                         if ((GetKeyState(VK_RSHIFT)   & (1 << 15)) != 0) i |= 1 << 24;\r
390                         if ((GetKeyState(VK_LSHIFT)   & (1 << 15)) != 0) i |= 1 << 16;\r
391                         if ((GetKeyState(VK_NUMLOCK)  & (1 << 0)) != 0) i |= 1 << 22;\r
392                         if ((GetKeyState(VK_CAPITAL)  & (1 << 0)) != 0) i |= 1 << 23;\r
393                         if (('A' <= wp && wp <= 'Z') || ('a' <= wp && wp <= 'z')) {\r
394                                 if (j != 0) {\r
395                                         i |= j;\r
396                                         i &= ~0x20;\r
397                                 }\r
398                         }\r
399                         putKeybuf(i);\r
400                         //                      bl_putKeyB(1, &i);\r
401                         return 0;\r
402                 }\r
403         }\r
404         return DefWindowProc(hw, msg, wp, lp);\r
405 }\r
406 \r
407 void drv_openWin(int sx, int sy, UCHAR *buf, char *winClosed)\r
408 {\r
409         w.win.buf = (int *)buf;\r
410         w.win.xsiz = sx;\r
411         w.win.ysiz = sy;\r
412         bld_openWin(sx, sy, winClosed);\r
413         return;\r
414 }\r
415 \r
416 void drv_sleep(int msec)\r
417 {\r
418         Sleep(msec);\r
419         //      MsgWaitForMultipleObjects(1, &threadhandle, FALSE, msec, QS_ALLINPUT);\r
420         /* 勉強不足でまだ書き方が分かりません! */\r
421         return;\r
422 }\r
423 \r
424 #else\r
425 \r
426 #error "Illegal OS type. Edit osecpu.h and look at DRV_OSNUM"\r
427 \r
428 #endif