OSDN Git Service

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