OSDN Git Service

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