OSDN Git Service

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