Rev 1 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | pmbaty | 1 | |
2 | #include "harness/hooks.h" |
||
3 | #include "harness/os.h" |
||
4 | #include "harness/win95_polyfill.h" |
||
5 | |||
6 | #include <assert.h> |
||
7 | #include <stdlib.h> |
||
8 | #include <string.h> |
||
9 | #include <time.h> |
||
10 | |||
11 | #if defined(_WIN32) || defined(_WIN64) |
||
12 | |||
13 | #include <direct.h> |
||
14 | #include <windows.h> |
||
15 | // https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/getcwd-wgetcwd?view=msvc-170 |
||
16 | #define getcwd _getcwd |
||
17 | #define chdir _chdir |
||
18 | #else |
||
19 | #include <dirent.h> |
||
20 | #include <libgen.h> |
||
21 | #include <unistd.h> |
||
22 | #endif |
||
23 | |||
24 | // All functions have a "_" suffix to avoid collisions with <windows.h>-defined types |
||
25 | |||
26 | uint32_t GetFileAttributesA_(char* lpFileName) { |
||
27 | |||
28 | FILE* f = fopen(lpFileName, "r"); |
||
18 | pmbaty | 29 | if (f == NULL) { |
1 | pmbaty | 30 | return INVALID_FILE_ATTRIBUTES; |
31 | } |
||
18 | pmbaty | 32 | fclose(f); |
1 | pmbaty | 33 | return FILE_ATTRIBUTE_NORMAL; |
34 | } |
||
35 | |||
36 | int SetFileAttributesA_(char* lpFileName, uint32_t dwFileAttributes) { |
||
37 | // no-op for now |
||
38 | return 0; |
||
39 | } |
||
40 | |||
41 | void* CreateFileA_(char* lpFileName, uint32_t dwDesiredAccess, uint32_t dwShareMode, void* lpSecurityAttributes, uint32_t dwCreationDisposition, uint32_t dwFlagsAndAttributes, void* hTemplateFile) { |
||
42 | |||
43 | assert(dwDesiredAccess == GENERIC_READ); |
||
44 | assert(lpSecurityAttributes == NULL); |
||
45 | assert(dwCreationDisposition == OPEN_EXISTING); |
||
46 | |||
47 | FILE* f = fopen(lpFileName, "r"); |
||
48 | if (!f) { |
||
49 | return INVALID_HANDLE_VALUE; |
||
50 | } |
||
51 | |||
52 | return f; |
||
53 | } |
||
54 | |||
55 | uint32_t GetFileSize_(void* hFile, uint32_t* lpFileSizeHigh) { |
||
56 | assert(lpFileSizeHigh == NULL); |
||
57 | |||
58 | long current_offset = ftell(hFile); |
||
59 | fseek(hFile, 0, SEEK_END); |
||
60 | long size = ftell(hFile); |
||
61 | fseek(hFile, current_offset, SEEK_SET); |
||
62 | return size; |
||
63 | } |
||
64 | |||
65 | int CloseHandle_(void* hObject) { |
||
66 | fclose(hObject); |
||
67 | return 0; |
||
68 | } |
||
69 | |||
70 | void GlobalMemoryStatus_(MEMORYSTATUS_* lpBuffer) { |
||
71 | lpBuffer->dwTotalPhys = 64000000; |
||
72 | lpBuffer->dwAvailPhys = 64000000; // pretend we have a whole 64mb of ram! |
||
73 | lpBuffer->dwAvailPageFile = 0; |
||
74 | lpBuffer->dwTotalVirtual = 0; |
||
75 | lpBuffer->dwAvailVirtual = 0; |
||
76 | lpBuffer->dwMemoryLoad = 0; |
||
77 | lpBuffer->dwTotalPageFile = 0; |
||
78 | } |
||
79 | |||
80 | int GetCursorPos_(POINT_* lpPoint) { |
||
81 | int x, y; |
||
82 | gHarness_platform.GetMousePosition(&x, &y); |
||
83 | lpPoint->x = x; |
||
84 | lpPoint->y = y; |
||
85 | return 0; |
||
86 | } |
||
87 | |||
88 | int ScreenToClient_(void* hWnd, POINT_* lpPoint) { |
||
89 | // no-op, we assume the point is already relative to client |
||
90 | return 0; |
||
91 | } |
||
92 | |||
93 | uint32_t timeGetTime_(void) { |
||
94 | return gHarness_platform.GetTicks(); |
||
95 | } |
||
96 | |||
97 | uint32_t GetCurrentDirectoryA_(uint32_t nBufferLength, char* lpBuffer) { |
||
98 | char* ret = getcwd(lpBuffer, nBufferLength); |
||
99 | if (ret == NULL) { |
||
100 | return 0; |
||
101 | } |
||
102 | return strlen(lpBuffer); |
||
103 | } |
||
104 | |||
105 | int SetCurrentDirectoryA_(char* lpPathName) { |
||
106 | int ret = chdir(lpPathName); |
||
107 | // chdir returning zero means success, SetCurrentDirectory returning non-zero means success |
||
108 | if (ret == 0) { |
||
109 | return 1; |
||
110 | } else { |
||
111 | return 0; |
||
112 | } |
||
113 | } |
||
114 | |||
115 | HANDLE_ FindFirstFileA_(char* lpFileName, WIN32_FIND_DATAA_* lpFindFileData) { |
||
116 | assert(strcmp(lpFileName, "*.???") == 0); |
||
117 | |||
118 | #if defined(_WIN32) || defined(_WIN64) |
||
119 | WIN32_FIND_DATAA fd; // Pierre-Marie Baty -- explicitly use WIN32_FIND_DATAA instead of WIN32_FIND_DATA |
||
120 | void* hFile = FindFirstFileA(lpFileName, &fd); // Pierre-Marie Baty -- explicitly use FindFirstFileA() instead of FindFirstFile() |
||
121 | if (hFile != INVALID_HANDLE_VALUE) { |
||
122 | strcpy(lpFindFileData->cFileName, fd.cFileName); |
||
123 | } |
||
124 | return hFile; |
||
125 | #else |
||
126 | DIR* dir; |
||
127 | strcpy(lpFileName, "."); |
||
128 | dir = opendir(lpFileName); |
||
129 | if (dir == NULL) { |
||
130 | return INVALID_HANDLE_VALUE; |
||
131 | } |
||
132 | if (FindNextFileA_(dir, lpFindFileData)) { |
||
133 | return dir; |
||
134 | } else { |
||
135 | closedir(dir); |
||
136 | return INVALID_HANDLE_VALUE; |
||
137 | } |
||
138 | #endif |
||
139 | } |
||
140 | |||
141 | int FindNextFileA_(HANDLE_ hFindFile, WIN32_FIND_DATAA_* lpFindFileData) { |
||
142 | #if defined(_WIN32) || defined(_WIN64) |
||
143 | WIN32_FIND_DATAA fd; // Pierre-Marie Baty -- explicitly use WIN32_FIND_DATAA instead of WIN32_FIND_DATA |
||
144 | int result = FindNextFileA(hFindFile, &fd); // Pierre-Marie Baty -- explicitly use FindNextFileA() instead of FindNextFile() |
||
145 | if (result) { |
||
146 | strcpy(lpFindFileData->cFileName, fd.cFileName); |
||
147 | } |
||
148 | return result; |
||
149 | #else |
||
150 | struct dirent* entry; |
||
151 | |||
152 | if (hFindFile == NULL) { |
||
153 | return 0; |
||
154 | } |
||
155 | while ((entry = readdir(hFindFile)) != NULL) { |
||
156 | if (entry->d_type == DT_REG) { |
||
157 | strcpy(lpFindFileData->cFileName, entry->d_name); |
||
158 | return 1; |
||
159 | } |
||
160 | } |
||
161 | return 0; |
||
162 | #endif |
||
163 | } |
||
164 | |||
165 | int FindClose_(HANDLE_ hFindFile) { |
||
166 | #if defined(_WIN32) || defined(_WIN64) |
||
167 | return FindClose(hFindFile); |
||
168 | #else |
||
169 | return closedir(hFindFile); |
||
170 | |||
171 | #endif |
||
172 | } |
||
173 | |||
174 | void* CreateWindowExA_(uint32_t dwExStyle, char* lpClassName, char* lpWindowName, uint32_t dwStyle, int X, int Y, int nWidth, int nHeight, void* hWndParent, void* hMenu, void* hInstance, void* lpParam) { |
||
175 | return gHarness_platform.CreateWindowAndRenderer(lpWindowName, X, Y, nWidth, nHeight); |
||
176 | } |
||
177 | |||
178 | int SetWindowPos_(void* hWnd, void* hWndInsertAfter, int X, int Y, int cx, int cy, unsigned int uFlags) { |
||
179 | return gHarness_platform.SetWindowPos(hWnd, X, Y, cx, cy); |
||
180 | } |
||
181 | |||
182 | int ShowCursor_(int bShow) { |
||
183 | gHarness_platform.ShowCursor(bShow); |
||
184 | return 0; |
||
185 | } |
||
186 | |||
187 | int SendMessageA_(void* hWnd, unsigned int Msg, unsigned int wParam, long lParam) { |
||
188 | return 0; |
||
189 | } |
||
190 | |||
191 | int MessageBoxA_(void* hWnd, char* lpText, char* lpCaption, unsigned int uType) { |
||
192 | // only ever used for errors |
||
193 | assert(uType == MB_ICONERROR); |
||
194 | return gHarness_platform.ShowErrorMessage(hWnd, lpText, lpCaption); |
||
195 | } |
||
196 | |||
197 | int DestroyWindow_(void* hWnd) { |
||
198 | gHarness_platform.DestroyWindow(hWnd); |
||
199 | return 0; |
||
200 | } |
||
201 | |||
202 | void ExitProcess_(unsigned int uExitCode) { |
||
203 | exit(uExitCode); |
||
204 | } |
||
205 | |||
206 | void TranslateMessage_(MSG_* lpMsg) { |
||
207 | // no-op |
||
208 | } |
||
209 | |||
210 | void DispatchMessageA_(MSG_* lpMsg) { |
||
211 | // no-op |
||
212 | } |
||
213 | |||
214 | int PeekMessageA_(MSG_* lpMsg, void* hWnd, unsigned int wMsgFilterMin, unsigned int wMsgFilterMax, unsigned int wRemoveMsg) { |
||
215 | return gHarness_platform.ProcessWindowMessages(lpMsg); |
||
216 | } |
||
217 | |||
218 | int GetMessageA_(MSG_* lpMsg, void* hWnd, unsigned int wMsgFilterMin, unsigned int wMsgFilterMax) { |
||
219 | return gHarness_platform.ProcessWindowMessages(lpMsg); |
||
220 | } |
||
221 | |||
222 | void Sleep_(uint32_t dwMilliseconds) { |
||
223 | gHarness_platform.Sleep(dwMilliseconds); |
||
224 | } |
||
225 | |||
226 | void DirectDraw_CreateSurface(int width, int height) { |
||
227 | // no-op |
||
228 | } |
||
229 | |||
230 | void DirectInputDevice_GetDeviceState(unsigned int count, uint8_t* buffer) { |
||
231 | gHarness_platform.GetKeyboardState(count, buffer); |
||
232 | } |
||
233 | |||
234 | void DirectDrawDevice_SetPaletteEntries(PALETTEENTRY_* palette, int pFirst_colour, int pCount) { |
||
235 | assert(pFirst_colour == 0); |
||
236 | assert(pCount == 256); |
||
237 | gHarness_platform.Renderer_SetPalette(palette); |
||
238 | } |
||
239 | |||
240 | void _splitpath_(char* path, char* drive, char* dir, char* fname, char* ext) { |
||
241 | assert(dir == NULL); |
||
242 | assert(fname != NULL); |
||
243 | |||
244 | char* result = OS_Basename(path); |
||
245 | strcpy(fname, result); |
||
246 | } |
||
247 | |||
248 | int _CrtDbgReport_(int reportType, const char* filename, int linenumber, const char* moduleName, const char* format, ...) { |
||
249 | printf("_CrtDbgReport: (TODO)\n"); |
||
250 | return 1; |
||
251 | } |