Wing Commander
Reconstructed Win32 source (1996)
Loading...
Searching...
No Matches
winsys.c
Go to the documentation of this file.
1/**
2 * @file
3 * @ingroup shell
4 * @brief Win32 system services: mouse, keyboard and clock wrappers, font
5 * workspaces, CD-ROM location and disc-swap prompts.
6 *
7 * Address range 402e80-4034ff. It is one unit, not two: PollKeyboardState's
8 * statics and LocateStreamsDirOnDisc's szStreamsDir interleave in the
9 * original's .bss (475b68-475d1f), and statics are private to one object.
10 * Its string literals occupy 46535c-4653fc.
11 */
12#include "wc1.h"
13
14/* Polled key states and the CD streams directory. CL places these statics
15 * by identifier hash, so their spellings reproduce the original order; the
16 * SDL flight adapter also reads the key states. */
17static
18int nInsertKeyDown; /* 0x00475b68 */
19static int nClearedKeyboard; /* 0x00475b6c */
20/* Nothing in the shipped executable references this buffer. */
21static unsigned char abUnusedPollArea[0x80]; /* 0x00475b70 */
22static
23int nOemPeriodKeyStatus; /* 0x00475bf0 */
24static
25int nHomeState; /* 0x00475bf4 */
26static
27int nPageDownKeyHeld; /* 0x00475bf8 */
28static
29int nOemCommaKeyPressed; /* 0x00475bfc */
30static
31int nRightArrowKeyHeld; /* 0x00475c00 */
32static
33int nEndKeyDown; /* 0x00475c04 */
34static int nClearKeyState; /* 0x00475c08 */
35static
36int nDeleteKeyDown; /* 0x00475c0c */
37static
38int nUpArrowKeyState; /* 0x00475c10 */
39static
40int nDownArrowKeyState; /* 0x00475c14 */
41static char szStreamsDir[0x100]; /* 0x00475c18 */
42static
43int nPageUpKeyState; /* 0x00475d18 */
44static
45int nLeftArrowKeyState; /* 0x00475d1c */
46
47
48/* Linker commons defined here; LINK orders them by identifier hash. */
49HWND hMainWindow; /* 0x005a89a0 */
50HANDLE hSingleInstanceSemaphore; /* 0x005a89a4 */
51unsigned long dwGameClockBaseValue; /* 0x005a89a8 */
53char szMemoryUsage[0x80]; /* 0x005a89b0 */
54
55/* Function start: 0x402E80 */
56void SetMousePosition(int x, int y)
57{
58 SetCursorPos(x, y);
59}
60
61/* Function start: 0x402EA0 */
62unsigned int PollKeyboardState(void)
63{
65 nHomeState = GetAsyncKeyState(VK_HOME);
66 nUpArrowKeyState = GetAsyncKeyState(VK_UP);
67 nPageUpKeyState = GetAsyncKeyState(VK_PRIOR);
68 nLeftArrowKeyState = GetAsyncKeyState(VK_LEFT);
69 nRightArrowKeyHeld = GetAsyncKeyState(VK_RIGHT);
70 nEndKeyDown = GetAsyncKeyState(VK_END);
71 nDownArrowKeyState = GetAsyncKeyState(VK_DOWN);
72 nPageDownKeyHeld = GetAsyncKeyState(VK_NEXT);
73 nClearKeyState = GetAsyncKeyState(VK_CLEAR);
74 nOemPeriodKeyStatus = GetAsyncKeyState(0xbe);
75 nOemCommaKeyPressed = GetAsyncKeyState(0xbc);
76 nInsertKeyDown = GetAsyncKeyState(VK_INSERT);
77 nDeleteKeyDown = GetAsyncKeyState(VK_DELETE);
78
79 if (nHomeState != 0)
80 return 0x47;
81 if (nPageUpKeyState != 0)
82 return 0x49;
83 if (nEndKeyDown != 0)
84 return 0x4f;
85 if (nPageDownKeyHeld != 0)
86 return 0x51;
87 if (nInsertKeyDown != 0 ||
89 return 0x52;
90 if (nDeleteKeyDown != 0 ||
92 return 0x53;
93 if (nClearKeyState != 0)
94 return 0x4c;
95 if (nUpArrowKeyState != 0) {
96 if (nLeftArrowKeyState != 0)
97 return 0x47;
98 return nRightArrowKeyHeld != 0 ? 0x49 : 0x48;
99 }
100 if (nDownArrowKeyState != 0) {
101 if (nLeftArrowKeyState != 0)
102 return 0x4f;
103 return nRightArrowKeyHeld != 0 ? 0x51 : 0x50;
104 }
105 if (nLeftArrowKeyState != 0)
106 return 0x4b;
107 return nRightArrowKeyHeld != 0 ? 0x4d : 0;
108}
109
110/* Function start: 0x403060 */
112{
113 return GetAsyncKeyState(VK_SHIFT);
114}
115
116/* Function start: 0x403070 */
118{
119 return GetAsyncKeyState(VK_CONTROL);
120}
121
122/* Function start: 0x403080 */
123unsigned int GetKeyboardModifiers(void)
124{
125 return nSystemKeyDown;
126}
127
128/* Function start: 0x403090 */
129unsigned int GetGameClockTicks(void)
130{
131 return (GetTickCount() - dwGameClockBaseValue) * 60 / 1000;
132}
133
134/* Function start: 0x4030B0 */
136{
137 dwGameClockBaseValue = GetTickCount() + (rand() & 3600000);
138}
139
140/* Function start: 0x4030D0 */
141void WriteDebugString(const char *s)
142{
143 OutputDebugString(s);
144}
145
146/* Function start: 0x4030E0 */
147/* Byte-identical duplicate of SetMousePosition (0x00402E80); both exist because
148 * VC++ 4.2's linker did no COMDAT folding. */
150{
151 SetCursorPos(x, y);
152}
153
154/* Function start: 0x403100 */
156{
157 FontWorkspace **workspace;
158 int index;
159
160 workspace = (FontWorkspace **)malloc(sizeof(*workspace));
161 *workspace = malloc(sizeof(**workspace));
162 for (index = 0; index < 1; index++) {
163 (*workspace)[index].width = 5;
164 (*workspace)[index].height = 5;
165 (*workspace)[index].pixels =
166 malloc((*workspace)->width * (*workspace)->height);
167 memset((*workspace)[index].pixels, 5,
168 (*workspace)->width * (*workspace)->height);
169 }
170 return workspace;
171}
172
173/* Function start: 0x403180 */
175{
176 int offset;
177
178 offset = 0;
179 do {
180 free(((FontWorkspace *)((unsigned char *)*workspace + offset))->pixels);
181 offset += sizeof(FontWorkspace);
182 } while (offset < (int)sizeof(FontWorkspace));
183 free(*workspace);
184 free(workspace);
185}
186
187/* Function start: 0x4031C0 */
189{
190 char current[256];
191 char *result;
192 char drive;
193
194 result = szStreamsDir;
195 GetCurrentDirectoryA(0xff, current);
196 drive = FindCdRomDriveByVolumeLabel("<anydisc>", "\\wc1\\streams\\");
197 if (drive != 0) {
198 sprintf(result, "%c:%s", drive,
199 "\\wc1\\streams\\");
200 return result;
201 }
202
203 if (strstr(current, "gamedat") != 0)
204 sprintf(result, "%s\\..\\%s", current, "streams\\");
205 else
206 sprintf(result, "%s\\%s", current, "streams\\");
207 GetCurrentDirectoryA(0xff, current);
208 if (!SetCurrentDirectoryA(result))
209 result = 0;
210 SetCurrentDirectoryA(current);
211 return result;
212}
213
214/* Function start: 0x403290 */
215char FindCdRomDriveByVolumeLabel(const char *label,
216 const char *directory)
217{
218 char drives[26];
219 char scanRoot[12];
220 char root[12];
221 char volume[256];
222 char filesystem[64];
223 DWORD maximumComponentLength;
224 DWORD flags;
225 int driveCount = 0;
226 int i;
227 int scanDrive;
228 char drive;
229 char result;
230
231 for (scanDrive = 'a'; scanDrive <= 'z'; scanDrive++) {
232 sprintf(scanRoot, "%c:\\", (char)scanDrive);
233 if (GetDriveTypeA(scanRoot) == DRIVE_CDROM) {
234 drives[driveCount] = (char)scanDrive;
235 driveCount++;
236 }
237 }
238
239 result = 0;
240 for (i = 0; i < driveCount; i++) {
241 drive = drives[i];
242 sprintf(root, "%c:\\", drive);
243 GetVolumeInformationA(root, volume, 0xff, 0,
244 &maximumComponentLength, &flags,
245 filesystem, sizeof(filesystem));
246 if (memcmp(label, "<anydisc>", 10) == 0) {
247 if (SetCurrentDirOnDrive(drive, directory) != 0) {
248 result = drive;
249 break;
250 }
251 } else if (strcmp(volume, label) == 0) {
252 if (SetCurrentDirOnDrive(drive, directory) != 0) {
253 result = drives[i];
254 break;
255 }
256 }
257 }
258 return result;
259}
260
261/* Function start: 0x4033E0 */
262int SetCurrentDirOnDrive(char drive, const char *directory)
263{
264 char path[256];
265 char current[256];
266 int result;
267
268 result = 0;
269 GetCurrentDirectoryA(0xff, current);
270 sprintf(path, "%c:%s", drive, directory);
271 if (SetCurrentDirectoryA(path) != 0)
272 result = 1;
273 SetCurrentDirectoryA(current);
274 return result;
275}
276
277/* Function start: 0x403450 */
279{
280 char title[18];
281 char message[1024];
282
283 strcpy(title, "Insert Correct CD");
284 sprintf(message,
285 "Please place The Kilrathi Saga disc %d into your CD-ROM drive and click OK\n"
286 "or click cancel to quit",
287 1);
288 do {
289 if (LocateStreamsDirOnDisc() != 0)
290 return 1;
291 } while (MessageBoxA(0, message, title,
292 MB_OKCANCEL | MB_ICONEXCLAMATION) != IDCANCEL);
293 return 0;
294}
295
296/* Function start: 0x4034D0 */
297short __stdcall OpenDataFileOrDie(const char *path)
298{
299 short fd = (short)_open(path, 0x8002);
300
301 nPacketError = (short)errno;
302 return fd;
303}
short nPacketError
Definition mono.c:15
HWND hMainWindow
Definition winsys.c:49
unsigned int nSystemKeyDown
Definition auto.c:17
HANDLE hSingleInstanceSemaphore
Definition winsys.c:50
unsigned long dwGameClockBaseValue
Definition winsys.c:51
char szMemoryUsage[0x80]
Definition winsys.c:53
struct GuardedAllocation * pGuardedAllocationTail
Definition winsys.c:52
Umbrella header for the game core.
void InitGameClockEpoch(void)
Definition winsys.c:135
void SetMousePosition(int x, int y)
Definition winsys.c:56
unsigned int PollKeyboardState(void)
Definition winsys.c:62
void FreeFontWorkspace(FontWorkspace **workspace)
Definition winsys.c:174
short __stdcall OpenDataFileOrDie(const char *path)
Definition winsys.c:297
char * LocateStreamsDirOnDisc(void)
Definition winsys.c:188
FontWorkspace ** AllocateFontWorkspace(short fontIndex)
Definition winsys.c:155
int GetControlKeyState(void)
Definition winsys.c:117
char FindCdRomDriveByVolumeLabel(const char *label, const char *directory)
Definition winsys.c:215
unsigned int GetGameClockTicks(void)
Definition winsys.c:129
int PromptInsertCorrectCd(void)
Definition winsys.c:278
void SetMousePositionDuplicate(int x, int y)
Definition winsys.c:149
int SetCurrentDirOnDrive(char drive, const char *directory)
Definition winsys.c:262
int GetShiftKeyState(void)
Definition winsys.c:111
void WriteDebugString(const char *s)
Definition winsys.c:141
unsigned int GetKeyboardModifiers(void)
Definition winsys.c:123
static int nDownArrowKeyState
Definition winsys.c:40
static char szStreamsDir[0x100]
Definition winsys.c:41
static int nOemPeriodKeyStatus
Definition winsys.c:23
static unsigned char abUnusedPollArea[0x80]
Definition winsys.c:21
static int nInsertKeyDown
Definition winsys.c:18
static int nEndKeyDown
Definition winsys.c:33
static int nPageUpKeyState
Definition winsys.c:43
static int nLeftArrowKeyState
Definition winsys.c:45
static int nClearKeyState
Definition winsys.c:34
static int nHomeState
Definition winsys.c:25
static int nUpArrowKeyState
Definition winsys.c:38
static int nPageDownKeyHeld
Definition winsys.c:27
static int nOemCommaKeyPressed
Definition winsys.c:29
static int nClearedKeyboard
Definition winsys.c:19
static int nRightArrowKeyHeld
Definition winsys.c:31
static int nDeleteKeyDown
Definition winsys.c:36