OSDN Git Service

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