-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuser.cpp
364 lines (363 loc) · 10.6 KB
/
user.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#include "user.h"
#include <map>
//ウインドウメッセージを表示するかどうか
#ifdef WIN16_DEBUG_MSG
#define DMSGPRINTF dprintf
#else
#define DMSGPRINTF
#endif
bool case_insensitive_string::operator < (const case_insensitive_string&rhs) const
{
return _stricmp(this->c_str(), rhs.c_str()) < 0;
}
bool case_insensitive_string::operator > (const case_insensitive_string&rhs) const
{
return _stricmp(this->c_str(), rhs.c_str()) > 1;
}
bool case_insensitive_string::operator <= (const case_insensitive_string&rhs) const
{
return _stricmp(this->c_str(), rhs.c_str()) <= 0;
}
bool case_insensitive_string::operator >= (const case_insensitive_string&rhs) const
{
return _stricmp(this->c_str(), rhs.c_str()) >= 0;
}
bool case_insensitive_string::operator == (const case_insensitive_string&rhs) const
{
return _stricmp(this->c_str(), rhs.c_str()) == 0;
}
case_insensitive_string::case_insensitive_string(const char* c) : std::string(c)
{
}
std::map<case_insensitive_string, HMENU16> resource_menu;
std::map<case_insensitive_string, HMENU16> class_resource_menu;
//1
INT16 MessageBox16(HWND16 hWndParent, LPCSTR lpszMessage, LPCSTR lpszTitle, UINT16 uStyle)
{
return MessageBoxA((HWND)HANDLE16ToHANDLE(hWndParent), lpszMessage, lpszTitle, uStyle);
}
//5
UINT16 InitApp16(HINSTANCE16 hInstance)
{
return TRUE;//なんか
}
//6
void PostQuitMessage16(INT16 nExitCode)
{
PostQuitMessage(nExitCode);
}
void RECTToRECT16(RECT *from, RECT16 *to)
{
to->left = (INT16)from->left;
to->top = (INT16)from->top;
to->right = (INT16)from->right;
to->bottom = (INT16)from->bottom;
}
//32
void GetWindowRect16(HWND16 hWnd, RECT16 *RectPtr)
{
HWND hWnd32 = (HWND)HANDLE16ToHANDLE(hWnd);
RECT rect32;
GetWindowRect(hWnd32, &rect32);
RECTToRECT16(&rect32, RectPtr);
}
//33
void GetClientRect16(HWND16 hWnd, RECT16 *RectPtr)
{
HWND hWnd32 = (HWND)HANDLE16ToHANDLE(hWnd);
RECT rect32;
GetClientRect(hWnd32, &rect32);
RECTToRECT16(&rect32, RectPtr);
}
//41
HWND16 CreateWindow16(LPCSTR lpszClassName, LPCSTR lpszWindowName, DWORD dwStyle,
INT16 x, INT16 y, INT16 nWidth, INT16 nHeight, HWND16 hWndParent, HMENU16 hMenu, HINSTANCE16 hInstance,
LPVOID16 lpCreateParams)
{
return CreateWindowEx16(0, lpszClassName, lpszWindowName, dwStyle,
x, y, nWidth, nHeight, hWndParent, hMenu, hInstance,
lpCreateParams);
}
HWND16 CreateWindowEx16(DWORD dwExStyle, LPCSTR lpszClassName, LPCSTR lpszWindowName,
DWORD dwStyle, INT16 x, INT16 y, INT16 nWidth, INT16 nHeight, HWND16 hWndParent, HMENU16 hMenu,
HINSTANCE16 hInstance, LPVOID16 lpCreateParams)
{
HMENU hMenu32 = (HMENU)HANDLE16ToHANDLE(hMenu);
if (!hMenu32)
{
hMenu32 = (HMENU)HANDLE16ToHANDLE(class_resource_menu[lpszClassName]);
}
HWND hWnd = CreateWindowExA(dwExStyle,
lpszClassName,
lpszWindowName,
dwStyle,
x == CW_USEDEFAULT16 ? CW_USEDEFAULT : x,
y == CW_USEDEFAULT16 ? CW_USEDEFAULT : y,
nWidth == CW_USEDEFAULT16 ? CW_USEDEFAULT : nWidth,
nHeight == CW_USEDEFAULT16 ? CW_USEDEFAULT : nHeight,
(HWND)HANDLE16ToHANDLE(hWndParent),
hMenu32,
(HINSTANCE)HANDLE16ToHANDLE(hInstance),
(LPVOID)lpCreateParams);
return HANDLEToHANDLE16(hWnd);
}
//42
BOOL16 ShowWindow16(HWND16 hWnd, INT16 nCmdShow)
{
HWND hWnd32 = (HWND)HANDLE16ToHANDLE(hWnd);
return ShowWindow(hWnd32, nCmdShow);
}
//53
BOOL16 DestroyWindow16(HWND16 hWnd)
{
HWND hWnd32 = (HWND)HANDLE16ToHANDLE(hWnd);
return DestroyWindow(hWnd32);
}
//56
BOOL16 MoveWindow16(HWND16 hWnd, INT16 LeftPos, INT16 TopPos, INT16 Width, INT16 Height, BOOL16 bRepaint)
{
HWND hWnd32 = (HWND)HANDLE16ToHANDLE(hWnd);
return MoveWindow(hWnd32, LeftPos, TopPos, Width, Height, bRepaint);
}
//とりあえず
//TODO:ATOM実装したい
extern std::map<std::string, DWORD> wprocmap;
LRESULT CALLBACK Win16WndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
{
//dprintf("hwnd:%X,msg:%X,wp:%X,lp:%X,cs:%X,ip:%X\n", hwnd, msg, wp, lp);
//return DefWindowProc(hwnd, msg, wp, lp);
HWND16 hwnd16 = HANDLEToHANDLE16((HANDLE)hwnd);
if (hwnd16)
{
//dprintf("wpp:%d,%X\n", inging, msg, msg, wp, lp);
char name[256];
//dprintf("%X\t", msg, msg, wp, lp);
GetClassNameA(hwnd, name, sizeof(name));
std::map<std::string, DWORD>::iterator itr;
if ((itr = wprocmap.find(name)) != wprocmap.end())
{/*
if (inging == 2)
{
inging = 3;
return DefWindowProc(hwnd, msg, wp, lp);
}*///if (inging) inging = 2;
WORD cs, ip;
ip = m_pc - m_base[SREGS::CS];
cs = m_sregs[CS];
UINT16 selector = itr->second >> 16, offset = (UINT16)itr->second;
m_sregs[CS] = selector;
m_base[CS] = selector << 4;
m_rights[CS] = 0x9a;
m_limit[CS] = 0xffff;
UINT16 stk = m_regs.w[SP];
int stkcnt = 0;
switch (msg)
{
case WM_CREATE:
{
//lparam=LPCREATESTRUCT
LPCREATESTRUCTA create = (LPCREATESTRUCTA)lp;
CREATESTRUCT16 create16;//スタック上に確保
create16.lpCreateParams = (LPVOID16)create->lpCreateParams;
create16.hInstance = HANDLEToHANDLE16(create->hInstance);
create16.hMenu = HANDLEToHANDLE16(create->hMenu);
create16.hwndParent = HANDLEToHANDLE16(create->hwndParent);
create16.cy = (INT16)create->cy;
create16.cx = (INT16)create->cx;
create16.y = (INT16)create->y;
create16.x = (INT16)create->x;
create16.style = (LONG32)create->style;
//TODO:この値をずっと保持する場合互換性が保たれない
create16.lpszName = i86_galloca_ptr((void*)create->lpszName, strlen(create->lpszName) + 2);
stkcnt += strlen(create->lpszName) + 2;
create16.lpszClass = i86_galloca_ptr((void*)create->lpszClass, strlen(create->lpszClass) + 2);
stkcnt += strlen(create->lpszClass) + 2;
create16.dwExStyle = create->dwExStyle;
lp = i86_galloca_ptr(&create16, sizeof(CREATESTRUCT16));
stkcnt += sizeof(CREATESTRUCT16);
}
break;
case WM_SETCURSOR:
case WM_MOUSEACTIVATE:
case WM_ERASEBKGND:
case WM_NCPAINT:
wp = HANDLEToHANDLE16((HANDLE)wp);
break;
case WM_CAPTURECHANGED:
case WM_SETICON:
lp = HANDLEToHANDLE16((HANDLE)lp);
break;
}
PUSH(hwnd16);
PUSH(msg);
PUSH(wp);
PUSH(lp >> 16);
PUSH(lp & 0xFFFF);
PUSH(cs);
PUSH(ip);
m_pc = (m_base[CS] + offset)&AMASK;
DMSGPRINTF("hwnd:%X,msg:%X,wp:%X,lp:%X,cs:%X,ip:%X\n", hwnd16, msg, wp, lp, cs, ip);
//CreateWindowの中でも呼ばれるので強引に
//メッセージを溜めるようにした方がいいかもしれない
while (m_regs.w[SP] < stk)
{
cpu_exexute_call_wrap();
}
i86_gfree_ptr(stkcnt);
//dprintf("%X\n", m_pc, msg, wp, lp, cs, ip);
return REG16(AX) | REG16(DX) << 16;
}
}
return DefWindowProc(hwnd, msg, wp, lp);
}
//57
ATOM16 RegisterClass16(const WNDCLASS16 *lpWndClass)
{
WNDCLASSEXA wca;
wca.cbSize = sizeof(wca);
wca.style = lpWndClass->style;
wca.lpfnWndProc = Win16WndProc;
wca.hInstance = (HINSTANCE)HANDLE16ToHANDLE(lpWndClass->hInstance);
wca.cbClsExtra = lpWndClass->cbClsExtra;
wca.cbWndExtra = lpWndClass->cbWndExtra;
wca.hIcon = (HICON)HANDLE16ToHANDLE(lpWndClass->hIcon);
wca.hCursor = (HCURSOR)HANDLE16ToHANDLE(lpWndClass->hCursor);
wca.hbrBackground = (HBRUSH)HANDLE16ToHANDLE(lpWndClass->hbrBackground);
wca.lpszMenuName = (LPCSTR)FARPTRToPTR32(lpWndClass->lpszMenuName);
wca.lpszClassName = (LPCSTR)FARPTRToPTR32(lpWndClass->lpszClassName);
wca.hIconSm = NULL;
wprocmap[wca.lpszClassName] = lpWndClass->lpfnWndProc;
class_resource_menu[wca.lpszClassName] = resource_menu[wca.lpszMenuName];
ATOM atom = RegisterClassExA(&wca);
//convert atom table
return atom;
}
//66
HDC16 GetDC16(HWND16 hWnd)
{
HWND hWnd32 = (HWND)HANDLE16ToHANDLE(hWnd);
HDC hdc = GetDC(hWnd32);
return HANDLEToHANDLE16(hdc);
}
//68
BOOL16 ReleaseDC16(HWND16 hWnd, HDC16 hdc)
{
HWND hWnd32 = (HWND)HANDLE16ToHANDLE(hWnd);
HDC hdc32 = (HDC)HANDLE16ToHANDLE(hdc);
return (BOOL16)ReleaseDC(hWnd32, hdc32);
}
//107
LRESULT16 DefWindowProc16(HWND16 hWnd, UINT16 uMsg, WPARAM16 wParam, LPARAM16 lParam)
{
HWND hwnd32 = (HWND)HANDLE16ToHANDLE(hWnd);
WPARAM wParam32 = wParam;
LPARAM lParam32 = lParam;
switch (uMsg)
{
case WM_CREATE:
{
CREATESTRUCT16 *create16 = (CREATESTRUCT16*)FARPTRToPTR32(lParam);
CREATESTRUCTA create;
create.lpCreateParams = (LPVOID)create16->lpCreateParams;
create.hInstance = (HINSTANCE)HANDLE16ToHANDLE(create16->hInstance);
create.hMenu = (HMENU)HANDLE16ToHANDLE(create16->hMenu);
create.hwndParent = (HWND)HANDLE16ToHANDLE(create16->hwndParent);
create.cy = create16->cy;
create.cx = create16->cx;
create.y = create16->y;
create.x = create16->x;
create.style = (LONG32)create16->style;
create.lpszName = (LPCSTR)FARPTRToPTR32(create16->lpszName);
create.lpszClass = (LPCSTR)FARPTRToPTR32(create16->lpszClass);
create.dwExStyle = create16->dwExStyle;
lParam32 = (LPARAM)&create;
}
break;
case WM_SETCURSOR:
case WM_MOUSEACTIVATE:
case WM_ERASEBKGND:
case WM_NCPAINT:
wParam32 = (WPARAM)HANDLE16ToHANDLE(wParam);
break;
case WM_CAPTURECHANGED:
case WM_SETICON:
lParam32 = (LPARAM)HANDLE16ToHANDLE(lParam);
}
//TODO:result
DMSGPRINTF("hwnd:%X,msg:%X,wp:%X,lp:%X\n", hWnd, uMsg, wParam, lParam);
return DefWindowProcA(hwnd32, uMsg, wParam32, lParam32);
}
//108
BOOL16 GetMessage16(MSG16 *lpMsg, HWND16 hWnd, UINT16 uMsgFilterMin,
UINT16 uMsgFilterMax)
{
HWND hWnd32 = (HWND)HANDLE16ToHANDLE(hWnd);
MSG msg32;
BOOL16 result = GetMessageA(&msg32, hWnd32, uMsgFilterMin, uMsgFilterMax);
lpMsg->hwnd = HANDLEToHANDLE16(msg32.hwnd);
lpMsg->message = msg32.message;
lpMsg->wParam = msg32.wParam;
lpMsg->lParam = msg32.lParam;
lpMsg->time = msg32.time;
lpMsg->pt.x = msg32.pt.x;
lpMsg->pt.y = msg32.pt.y;
return result;
}
void MSG16ToMSG(const MSG16 *from, MSG *to)
{
to->hwnd = (HWND)HANDLE16ToHANDLE(from->hwnd);
to->message = from->message;
to->wParam = from->wParam;
to->lParam = from->lParam;
to->time = from->time;
to->pt.x = from->pt.x;
to->pt.y = from->pt.y;
}
//113
BOOL16 TranslateMessage16(const MSG16 *lpMsg)
{
MSG msg32;
MSG16ToMSG(lpMsg, &msg32);
return TranslateMessage(&msg32);
}
//114
LONG16 DispatchMessage16(const MSG16 *lpMsg)
{
MSG msg32;
MSG16ToMSG(lpMsg, &msg32);
return DispatchMessageA(&msg32);
}
//124
void UpdateWindow16(HWND16 hwnd)
{
HWND hWnd32 = (HWND)HANDLE16ToHANDLE(hwnd);
UpdateWindow(hWnd32);
}
//135
LONG16 GetWindowLong16(HWND16 hWnd, INT16 ByteOffset)
{
HWND hWnd32 = (HWND)HANDLE16ToHANDLE(hWnd);
if (ByteOffset == GWL_WNDPROC)
{
hWnd = hWnd;
}
return GetWindowLongA(hWnd32, ByteOffset);
}
//136
LONG16 SetWindowLong16(HWND16 hWnd, INT16 ByteOffset, LONG16 Value)
{
HWND hWnd32 = (HWND)HANDLE16ToHANDLE(hWnd);
return SetWindowLongA(hWnd32, ByteOffset, Value);
}
//173
HCURSOR16 LoadCursor16(HINSTANCE16 hInstance, LPCSTR pszName)
{
NOTIMPL("LoadCursor(0x%X,%s)\n", hInstance, pszName);
return NULL;
}
//174
HICON16 LoadIcon16(HINSTANCE16 hInstance, LPCSTR ResourceID)
{
NOTIMPL("LoadIcon(0x%X,%s)\n", hInstance, ResourceID);
return NULL;
}