don't load corrupt wav files
[divverent/darkplaces.git] / sys_win.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 // sys_win.c -- Win32 system interface code
21
22 #include <windows.h>
23 #include <mmsystem.h>
24 #include <direct.h>
25 #ifdef SUPPORTDIRECTX
26 #include <dsound.h>
27 #endif
28
29 #include "qtypes.h"
30
31 #include "quakedef.h"
32 #include "errno.h"
33 #include "resource.h"
34 #include "conproc.h"
35
36 HANDLE                          hinput, houtput;
37
38 #ifdef QHOST
39 static HANDLE   tevent;
40 static HANDLE   hFile;
41 static HANDLE   heventParent;
42 static HANDLE   heventChild;
43 #endif
44
45
46 /*
47 ===============================================================================
48
49 SYSTEM IO
50
51 ===============================================================================
52 */
53
54 void Sys_Error (const char *error, ...)
55 {
56         va_list         argptr;
57         char            text[MAX_INPUTLINE];
58         static int      in_sys_error0 = 0;
59         static int      in_sys_error1 = 0;
60         static int      in_sys_error2 = 0;
61         static int      in_sys_error3 = 0;
62
63         va_start (argptr, error);
64         dpvsnprintf (text, sizeof (text), error, argptr);
65         va_end (argptr);
66
67         Con_Printf ("Quake Error: %s\n", text);
68
69         // close video so the message box is visible, unless we already tried that
70         if (!in_sys_error0 && cls.state != ca_dedicated)
71         {
72                 in_sys_error0 = 1;
73                 VID_Shutdown();
74         }
75
76         if (!in_sys_error3 && cls.state != ca_dedicated)
77         {
78                 in_sys_error3 = true;
79                 MessageBox(NULL, text, "Quake Error", MB_OK | MB_SETFOREGROUND | MB_ICONSTOP);
80         }
81
82         if (!in_sys_error1)
83         {
84                 in_sys_error1 = 1;
85                 Host_Shutdown ();
86         }
87
88 // shut down QHOST hooks if necessary
89         if (!in_sys_error2)
90         {
91                 in_sys_error2 = 1;
92                 Sys_Shutdown ();
93         }
94
95         exit (1);
96 }
97
98 void Sys_Shutdown (void)
99 {
100 #ifdef QHOST
101         if (tevent)
102                 CloseHandle (tevent);
103 #endif
104
105         if (cls.state == ca_dedicated)
106                 FreeConsole ();
107
108 #ifdef QHOST
109 // shut down QHOST hooks if necessary
110         DeinitConProc ();
111 #endif
112 }
113
114 void Sys_PrintToTerminal(const char *text)
115 {
116         DWORD dummy;
117         extern HANDLE houtput;
118
119         if ((houtput != 0) && (houtput != INVALID_HANDLE_VALUE))
120                 WriteFile(houtput, text, (DWORD) strlen(text), &dummy, NULL);
121 }
122
123 char *Sys_ConsoleInput (void)
124 {
125         static char text[MAX_INPUTLINE];
126         static int len;
127         INPUT_RECORD recs[1024];
128         int ch;
129         DWORD numread, numevents, dummy;
130
131         if (cls.state != ca_dedicated)
132                 return NULL;
133
134         for ( ;; )
135         {
136                 if (!GetNumberOfConsoleInputEvents (hinput, &numevents))
137                 {
138                         cls.state = ca_disconnected;
139                         Sys_Error ("Error getting # of console events (error code %x)", (unsigned int)GetLastError());
140                 }
141
142                 if (numevents <= 0)
143                         break;
144
145                 if (!ReadConsoleInput(hinput, recs, 1, &numread))
146                 {
147                         cls.state = ca_disconnected;
148                         Sys_Error ("Error reading console input (error code %x)", (unsigned int)GetLastError());
149                 }
150
151                 if (numread != 1)
152                 {
153                         cls.state = ca_disconnected;
154                         Sys_Error ("Couldn't read console input (error code %x)", (unsigned int)GetLastError());
155                 }
156
157                 if (recs[0].EventType == KEY_EVENT)
158                 {
159                         if (!recs[0].Event.KeyEvent.bKeyDown)
160                         {
161                                 ch = recs[0].Event.KeyEvent.uChar.AsciiChar;
162
163                                 switch (ch)
164                                 {
165                                         case '\r':
166                                                 WriteFile(houtput, "\r\n", 2, &dummy, NULL);
167
168                                                 if (len)
169                                                 {
170                                                         text[len] = 0;
171                                                         len = 0;
172                                                         return text;
173                                                 }
174
175                                                 break;
176
177                                         case '\b':
178                                                 WriteFile(houtput, "\b \b", 3, &dummy, NULL);
179                                                 if (len)
180                                                 {
181                                                         len--;
182                                                 }
183                                                 break;
184
185                                         default:
186                                                 if (ch >= (int) (unsigned char) ' ')
187                                                 {
188                                                         WriteFile(houtput, &ch, 1, &dummy, NULL);
189                                                         text[len] = ch;
190                                                         len = (len + 1) & 0xff;
191                                                 }
192
193                                                 break;
194
195                                 }
196                         }
197                 }
198         }
199
200         return NULL;
201 }
202
203 char *Sys_GetClipboardData (void)
204 {
205         char *data = NULL;
206         char *cliptext;
207
208         if (OpenClipboard (NULL) != 0)
209         {
210                 HANDLE hClipboardData;
211
212                 if ((hClipboardData = GetClipboardData (CF_TEXT)) != 0)
213                 {
214                         if ((cliptext = (char *)GlobalLock (hClipboardData)) != 0)
215                         {
216                                 size_t allocsize;
217                                 allocsize = GlobalSize (hClipboardData) + 1;
218                                 data = (char *)Z_Malloc (allocsize);
219                                 strlcpy (data, cliptext, allocsize);
220                                 GlobalUnlock (hClipboardData);
221                         }
222                 }
223                 CloseClipboard ();
224         }
225         return data;
226 }
227
228 void Sys_InitConsole (void)
229 {
230 #ifdef QHOST
231         int t;
232
233         // initialize the windows dedicated server console if needed
234         tevent = CreateEvent(NULL, false, false, NULL);
235
236         if (!tevent)
237                 Sys_Error ("Couldn't create event");
238 #endif
239
240         houtput = GetStdHandle (STD_OUTPUT_HANDLE);
241         hinput = GetStdHandle (STD_INPUT_HANDLE);
242
243         // LordHavoc: can't check cls.state because it hasn't been initialized yet
244         // if (cls.state == ca_dedicated)
245         if (COM_CheckParm("-dedicated"))
246         {
247                 //if ((houtput == 0) || (houtput == INVALID_HANDLE_VALUE)) // LordHavoc: on Windows XP this is never 0 or invalid, but hinput is invalid
248                 {
249                         if (!AllocConsole ())
250                                 Sys_Error ("Couldn't create dedicated server console (error code %x)", (unsigned int)GetLastError());
251                         houtput = GetStdHandle (STD_OUTPUT_HANDLE);
252                         hinput = GetStdHandle (STD_INPUT_HANDLE);
253                 }
254                 if ((houtput == 0) || (houtput == INVALID_HANDLE_VALUE))
255                         Sys_Error ("Couldn't create dedicated server console");
256
257
258 #ifdef QHOST
259 #ifdef _WIN64
260 #define atoi _atoi64
261 #endif
262         // give QHOST a chance to hook into the console
263                 if ((t = COM_CheckParm ("-HFILE")) > 0)
264                 {
265                         if (t < com_argc)
266                                 hFile = (HANDLE)atoi (com_argv[t+1]);
267                 }
268
269                 if ((t = COM_CheckParm ("-HPARENT")) > 0)
270                 {
271                         if (t < com_argc)
272                                 heventParent = (HANDLE)atoi (com_argv[t+1]);
273                 }
274
275                 if ((t = COM_CheckParm ("-HCHILD")) > 0)
276                 {
277                         if (t < com_argc)
278                                 heventChild = (HANDLE)atoi (com_argv[t+1]);
279                 }
280
281                 InitConProc (hFile, heventParent, heventChild);
282 #endif
283         }
284
285 // because sound is off until we become active
286         S_BlockSound ();
287 }
288
289 /*
290 ==============================================================================
291
292 WINDOWS CRAP
293
294 ==============================================================================
295 */
296
297
298 /*
299 ==================
300 WinMain
301 ==================
302 */
303 HINSTANCE       global_hInstance;
304 const char      *argv[MAX_NUM_ARGVS];
305 char            program_name[MAX_OSPATH];
306
307 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
308 {
309         MEMORYSTATUS lpBuffer;
310
311         /* previous instances do not exist in Win32 */
312         if (hPrevInstance)
313                 return 0;
314
315         global_hInstance = hInstance;
316
317         lpBuffer.dwLength = sizeof(MEMORYSTATUS);
318         GlobalMemoryStatus (&lpBuffer);
319
320         program_name[sizeof(program_name)-1] = 0;
321         GetModuleFileNameA(NULL, program_name, sizeof(program_name) - 1);
322
323         com_argc = 1;
324         com_argv = argv;
325         argv[0] = program_name;
326
327         // FIXME: this tokenizer is rather redundent, call a more general one
328         while (*lpCmdLine && (com_argc < MAX_NUM_ARGVS))
329         {
330                 while (*lpCmdLine && ISWHITESPACE(*lpCmdLine))
331                         lpCmdLine++;
332
333                 if (!*lpCmdLine)
334                         break;
335
336                 if (*lpCmdLine == '\"')
337                 {
338                         // quoted string
339                         lpCmdLine++;
340                         argv[com_argc] = lpCmdLine;
341                         com_argc++;
342                         while (*lpCmdLine && (*lpCmdLine != '\"'))
343                                 lpCmdLine++;
344                 }
345                 else
346                 {
347                         // unquoted word
348                         argv[com_argc] = lpCmdLine;
349                         com_argc++;
350                         while (*lpCmdLine && !ISWHITESPACE(*lpCmdLine))
351                                 lpCmdLine++;
352                 }
353
354                 if (*lpCmdLine)
355                 {
356                         *lpCmdLine = 0;
357                         lpCmdLine++;
358                 }
359         }
360
361         Sys_ProvideSelfFD();
362
363         Host_Main();
364
365         /* return success of application */
366         return true;
367 }
368
369 #if 0
370 // unused, this file is only used when building windows client and vid_wgl provides WinMain() instead
371 int main (int argc, const char* argv[])
372 {
373         MEMORYSTATUS lpBuffer;
374
375         global_hInstance = GetModuleHandle (0);
376
377         lpBuffer.dwLength = sizeof(MEMORYSTATUS);
378         GlobalMemoryStatus (&lpBuffer);
379
380         program_name[sizeof(program_name)-1] = 0;
381         GetModuleFileNameA(NULL, program_name, sizeof(program_name) - 1);
382
383         com_argc = argc;
384         com_argv = argv;
385
386         Host_Main();
387
388         return true;
389 }
390 #endif
391
392 qboolean sys_supportsdlgetticks = false;
393 unsigned int Sys_SDL_GetTicks (void)
394 {
395         Sys_Error("Called Sys_SDL_GetTicks on non-SDL target");
396         return 0;
397 }
398 void Sys_SDL_Delay (unsigned int milliseconds)
399 {
400         Sys_Error("Called Sys_SDL_Delay on non-SDL target");
401 }