OSDN Git Service

comment added
[heavyosecpu/HeavyOSECPU.git] / dependent.c
1 #include "osecpu.h"
2 #include <windows.h>
3
4 #if (DRV_OSNUM == 0x0002)
5
6 #error "Your OS is not supported."
7
8 #elif (DRV_OSNUM == 0x0003)
9
10 #error "Your OS is not supported."
11
12 #elif (DRV_OSNUM == 0x0001)
13
14 #define TIMER_ID                         1
15 #define TIMER_INTERVAL          10
16
17 struct BLD_WORK {
18         HINSTANCE hi;
19         HWND hw;
20         BITMAPINFO bmi;
21         int tmcount1, tmcount2, flags, smp; /* bit0: 終了 */
22         HANDLE mtx;
23         char *winClosed;
24 };
25
26 struct BLD_WORK bld_work;
27
28 struct BL_WIN {
29         int xsiz, ysiz, *buf;
30 };
31
32 struct BL_WORK {
33         struct BL_WIN win;
34         jmp_buf jb;
35         int csiz_x, csiz_y, cx, cy, col0, col1, tabsiz, slctwin;
36         int tmcount, tmcount0, mod, rand_seed;
37         int *cbuf;
38         unsigned char *ftyp;
39         unsigned char **fptn;
40         int *ccol, *cbak;
41         int *kbuf, kbuf_rp, kbuf_wp, kbuf_c;
42 };
43
44 struct BL_WORK bl_work;
45
46 #define BL_SIZ_KBUF                     8192
47
48 #define BL_WAITKEYF             0x00000001
49 #define BL_WAITKEYNF    0x00000002
50 #define BL_WAITKEY              0x00000003
51 #define BL_GETKEY               0x00000004
52 #define BL_CLEARREP             0x00000008
53 #define BL_DELFFF               0x00000010
54
55 #define BL_KEYMODE              0x00000000      // 作りかけ, make/remake/breakが見えるかどうか
56
57 #define w       bl_work
58 #define dw      bld_work
59
60 void bld_openWin(int x, int y, char *winClosed);
61 void bld_flshWin(int sx, int sy, int x0, int y0);
62 LRESULT CALLBACK WndProc(HWND hw, unsigned int msg, WPARAM wp, LPARAM lp);
63 void bl_cls();
64 int bl_iCol(int i);
65 void bl_readyWin(int n);
66
67 static HANDLE threadhandle;
68
69 void *mallocRWE(int bytes)
70 {
71         void *p = malloc(bytes);
72         DWORD dmy;
73         VirtualProtect(p, bytes, PAGE_EXECUTE_READWRITE, &dmy);
74         return p;
75 }
76
77 static int winthread(void *dmy)
78 {
79         WNDCLASSEX wc;
80         RECT r;
81         int i, x, y;
82         MSG msg;
83
84         x = dw.bmi.bmiHeader.biWidth;
85         y = -dw.bmi.bmiHeader.biHeight;
86
87         wc.cbSize = sizeof (WNDCLASSEX);
88         wc.style = CS_HREDRAW | CS_VREDRAW;
89         wc.lpfnWndProc = WndProc;
90         wc.cbClsExtra = 0;
91         wc.cbWndExtra = 0;
92         wc.hInstance = dw.hi;
93         wc.hIcon = (HICON)LoadImage(NULL, MAKEINTRESOURCE(IDI_APPLICATION),
94                 IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_SHARED);
95         wc.hIconSm = wc.hIcon;
96         wc.hCursor = (HCURSOR)LoadImage(NULL, MAKEINTRESOURCE(IDC_ARROW),
97                 IMAGE_CURSOR, 0, 0, LR_DEFAULTSIZE | LR_SHARED);
98         wc.hbrBackground = (HBRUSH)COLOR_APPWORKSPACE;
99         wc.lpszMenuName = NULL;
100         wc.lpszClassName = L"WinClass";
101         if (RegisterClassEx(&wc) == 0)
102                 return 1;
103         r.left = 0;
104         r.top = 0;
105         r.right = x;
106         r.bottom = y;
107         AdjustWindowRect(&r, WS_OVERLAPPEDWINDOW, FALSE);
108         x = r.right - r.left;
109         y = r.bottom - r.top;
110
111         char *t = "osecpu";
112
113         dw.hw = CreateWindowA("WinClass", t, WS_OVERLAPPEDWINDOW,
114                 CW_USEDEFAULT, CW_USEDEFAULT, x, y, NULL, NULL, dw.hi, NULL);
115         if (dw.hw == NULL)
116                 return 1;
117         ShowWindow(dw.hw, SW_SHOW);
118         UpdateWindow(dw.hw);
119         SetTimer(dw.hw, TIMER_ID, TIMER_INTERVAL, NULL);
120         SetTimer(dw.hw, TIMER_ID + 1, TIMER_INTERVAL * 10, NULL);
121         SetTimer(dw.hw, TIMER_ID + 2, TIMER_INTERVAL * 100, NULL);
122         dw.flags |= 2 | 4;
123
124         for (;;) {
125                 i = GetMessage(&msg, NULL, 0, 0);
126                 if (i == 0 || i == -1)  /* エラーもしくは終了メッセージ */
127                         break;
128                 /* そのほかはとりあえずデフォルト処理で */
129                 TranslateMessage(&msg);
130                 DispatchMessage(&msg);
131         }
132         //      PostQuitMessage(0);
133         dw.flags |= 1; /* 終了, bld_waitNF()が見つける */
134         if (dw.winClosed != NULL)
135                 *dw.winClosed = 1;
136         return 0;
137 }
138
139 void bld_openWin(int sx, int sy, char *winClosed)
140 {
141         static int i;
142
143         dw.bmi.bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
144         dw.bmi.bmiHeader.biWidth = sx;
145         dw.bmi.bmiHeader.biHeight = -sy;
146         dw.bmi.bmiHeader.biPlanes = 1;
147         dw.bmi.bmiHeader.biBitCount = 32;
148         dw.bmi.bmiHeader.biCompression = BI_RGB;
149         dw.winClosed = winClosed;
150
151         threadhandle = CreateThread(NULL, 0, (void *)&winthread, NULL, 0, (void *)&i);
152
153         return;
154 }
155
156 void drv_flshWin(int sx, int sy, int x0, int y0)
157 {
158         InvalidateRect(dw.hw, NULL, FALSE);
159         UpdateWindow(dw.hw);
160         return;
161 }
162
163 LRESULT CALLBACK WndProc(HWND hw, unsigned int msg, WPARAM wp, LPARAM lp)
164 {
165         int i, j;
166         if (msg == WM_PAINT) {
167                 PAINTSTRUCT ps;
168                 HDC hdc = BeginPaint(dw.hw, &ps);
169                 SetDIBitsToDevice(hdc, 0, 0, w.win.xsiz, w.win.ysiz,
170                         0, 0, 0, w.win.ysiz, w.win.buf, &dw.bmi, DIB_RGB_COLORS);
171                 EndPaint(dw.hw, &ps);
172         }
173         if (msg == WM_DESTROY) {
174                 PostQuitMessage(0);
175                 return 0;
176         }
177         if (msg == WM_TIMER && wp == TIMER_ID) {
178                 w.tmcount += TIMER_INTERVAL;
179                 return 0;
180         }
181         if (msg == WM_TIMER && wp == TIMER_ID + 1) {
182                 dw.tmcount1 += TIMER_INTERVAL * 10;
183                 w.tmcount = dw.tmcount1;
184                 return 0;
185         }
186         if (msg == WM_TIMER && wp == TIMER_ID + 2) {
187                 dw.tmcount2 += TIMER_INTERVAL * 100;
188                 w.tmcount = dw.tmcount1 = dw.tmcount2;
189                 return 0;
190         }
191         if (msg == WM_KEYDOWN || msg == WM_SYSKEYDOWN) {
192                 i = -1;
193
194                 if (wp == VK_RETURN)    i = KEY_ENTER;
195                 if (wp == VK_ESCAPE)    i = KEY_ESC;
196                 if (wp == VK_BACK)              i = KEY_BACKSPACE;
197                 if (wp == VK_TAB)               i = KEY_TAB;
198                 if (wp == VK_PRIOR)             i = KEY_PAGEUP;
199                 if (wp == VK_NEXT)              i = KEY_PAGEDWN;
200                 if (wp == VK_END)               i = KEY_END;
201                 if (wp == VK_HOME)              i = KEY_HOME;
202                 if (wp == VK_LEFT)              i = KEY_LEFT;
203                 if (wp == VK_RIGHT)             i = KEY_RIGHT;
204                 if (wp == VK_UP)                i = KEY_UP;
205                 if (wp == VK_DOWN)              i = KEY_DOWN;
206                 if (wp == VK_INSERT)    i = KEY_INS;
207                 if (wp == VK_DELETE)    i = KEY_DEL;
208                 j &= 0;
209                 if ((GetKeyState(VK_LCONTROL) & (1 << 15)) != 0) j |= 1 << 17;
210                 if ((GetKeyState(VK_LMENU)    & (1 << 15)) != 0) j |= 1 << 18;
211                 if ((GetKeyState(VK_RCONTROL) & (1 << 15)) != 0) j |= 1 << 25;
212                 if ((GetKeyState(VK_RMENU)    & (1 << 15)) != 0) j |= 1 << 26;
213                 if ((GetKeyState(VK_RSHIFT)   & (1 << 15)) != 0) i |= 1 << 24;
214                 if ((GetKeyState(VK_LSHIFT)   & (1 << 15)) != 0) i |= 1 << 16;
215                 if ((GetKeyState(VK_NUMLOCK)  & (1 << 0)) != 0) i |= 1 << 22;
216                 if ((GetKeyState(VK_CAPITAL)  & (1 << 0)) != 0) i |= 1 << 23;
217                 if (j != 0) {
218                         if ('A' <= wp && wp <= 'Z') i = wp;
219                 }
220                 if (i != -1) {
221                         putKeybuf(i | j);
222                         //                      bl_putKeyB(1, &i);
223                         return 0;
224                 }
225         }
226         if (msg == WM_KEYUP) {
227                 i = 0xfff;
228                 //              bl_putKeyB(1, &i);
229         }
230         if (msg == WM_CHAR) {
231                 i = 0;
232                 if (' ' <= wp && wp <= 0x7e) {
233                         i = wp;
234                         j &= 0;
235                         if ((GetKeyState(VK_LCONTROL) & (1 << 15)) != 0) j |= 1 << 17;
236                         if ((GetKeyState(VK_LMENU)    & (1 << 15)) != 0) j |= 1 << 18;
237                         if ((GetKeyState(VK_RCONTROL) & (1 << 15)) != 0) j |= 1 << 25;
238                         if ((GetKeyState(VK_RMENU)    & (1 << 15)) != 0) j |= 1 << 26;
239                         if ((GetKeyState(VK_RSHIFT)   & (1 << 15)) != 0) i |= 1 << 24;
240                         if ((GetKeyState(VK_LSHIFT)   & (1 << 15)) != 0) i |= 1 << 16;
241                         if ((GetKeyState(VK_NUMLOCK)  & (1 << 0)) != 0) i |= 1 << 22;
242                         if ((GetKeyState(VK_CAPITAL)  & (1 << 0)) != 0) i |= 1 << 23;
243                         if (('A' <= wp && wp <= 'Z') || ('a' <= wp && wp <= 'z')) {
244                                 if (j != 0) {
245                                         i |= j;
246                                         i &= ~0x20;
247                                 }
248                         }
249                         putKeybuf(i);
250                         //                      bl_putKeyB(1, &i);
251                         return 0;
252                 }
253         }
254         return DefWindowProc(hw, msg, wp, lp);
255 }
256
257 void drv_openWin(int sx, int sy, UCHAR *buf, char *winClosed)
258 {
259         w.win.buf = (int *)buf;
260         w.win.xsiz = sx;
261         w.win.ysiz = sy;
262         bld_openWin(sx, sy, winClosed);
263         return;
264 }
265
266 void drv_sleep(int msec)
267 {
268         Sleep(msec);
269         //      MsgWaitForMultipleObjects(1, &threadhandle, FALSE, msec, QS_ALLINPUT);
270         /* 勉強不足でまだ書き方が分かりません! */
271         return;
272 }
273
274 #else
275
276 #error "Illegal OS type. Edit switch.h and look at DRV_OSNUM"
277
278 #endif