]> icculus.org git repositories - divverent/darkplaces.git/blob - sys_win.c
close packs when clearing search path
[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 "quakedef.h"
23 #include <windows.h>
24 #include <mmsystem.h>
25 #include <dsound.h>
26 #include "errno.h"
27 #include "resource.h"
28 #include "conproc.h"
29 #include "direct.h"
30
31 cvar_t sys_usetimegettime = {CVAR_SAVE, "sys_usetimegettime", "1", "use windows timeGetTime function (which has issues on some motherboards) for timing rather than QueryPerformanceCounter timer (which has issues on multicore/multiprocessor machines and processors which are designed to conserve power)"};
32
33 HANDLE                          hinput, houtput;
34
35 static HANDLE   tevent;
36 static HANDLE   hFile;
37 static HANDLE   heventParent;
38 static HANDLE   heventChild;
39
40
41 /*
42 ===============================================================================
43
44 SYSTEM IO
45
46 ===============================================================================
47 */
48
49 void Sys_Error (const char *error, ...)
50 {
51         va_list         argptr;
52         char            text[MAX_INPUTLINE];
53         static int      in_sys_error0 = 0;
54         static int      in_sys_error1 = 0;
55         static int      in_sys_error2 = 0;
56         static int      in_sys_error3 = 0;
57
58         va_start (argptr, error);
59         dpvsnprintf (text, sizeof (text), error, argptr);
60         va_end (argptr);
61
62         Con_Printf ("Quake Error: %s\n", text);
63
64         // close video so the message box is visible, unless we already tried that
65         if (!in_sys_error0 && cls.state != ca_dedicated)
66         {
67                 in_sys_error0 = 1;
68                 VID_Shutdown();
69         }
70
71         if (!in_sys_error3 && cls.state != ca_dedicated)
72         {
73                 in_sys_error3 = true;
74                 MessageBox(NULL, text, "Quake Error", MB_OK | MB_SETFOREGROUND | MB_ICONSTOP);
75         }
76
77         if (!in_sys_error1)
78         {
79                 in_sys_error1 = 1;
80                 Host_Shutdown ();
81         }
82
83 // shut down QHOST hooks if necessary
84         if (!in_sys_error2)
85         {
86                 in_sys_error2 = 1;
87                 Sys_Shutdown ();
88         }
89
90         exit (1);
91 }
92
93 void Sys_Shutdown (void)
94 {
95         if (tevent)
96                 CloseHandle (tevent);
97
98         if (cls.state == ca_dedicated)
99                 FreeConsole ();
100
101 // shut down QHOST hooks if necessary
102         DeinitConProc ();
103 }
104
105 void Sys_PrintToTerminal(const char *text)
106 {
107         DWORD dummy;
108         extern HANDLE houtput;
109         if (cls.state == ca_dedicated)
110                 WriteFile(houtput, text, (DWORD) strlen(text), &dummy, NULL);
111 }
112
113 /*
114 ================
115 Sys_DoubleTime
116 ================
117 */
118 double Sys_DoubleTime (void)
119 {
120         static int first = true;
121         static double oldtime = 0.0, curtime = 0.0;
122         double newtime;
123         // LordHavoc: note to people modifying this code, DWORD is specifically defined as an unsigned 32bit number, therefore the 65536.0 * 65536.0 is fine.
124         if (sys_usetimegettime.integer)
125         {
126                 static int firsttimegettime = true;
127                 // timeGetTime
128                 // platform:
129                 // Windows 95/98/ME/NT/2000/XP
130                 // features:
131                 // reasonable accuracy (millisecond)
132                 // issues:
133                 // wraps around every 47 days or so (but this is non-fatal to us, odd times are rejected, only causes a one frame stutter)
134
135                 // make sure the timer is high precision, otherwise different versions of windows have varying accuracy
136                 if (firsttimegettime)
137                 {
138                         timeBeginPeriod (1);
139                         firsttimegettime = false;
140                 }
141
142                 newtime = (double) timeGetTime () / 1000.0;
143         }
144         else
145         {
146                 // QueryPerformanceCounter
147                 // platform:
148                 // Windows 95/98/ME/NT/2000/XP
149                 // features:
150                 // very accurate (CPU cycles)
151                 // known issues:
152                 // does not necessarily match realtime too well (tends to get faster and faster in win98)
153                 // wraps around occasionally on some platforms (depends on CPU speed and probably other unknown factors)
154                 double timescale;
155                 LARGE_INTEGER PerformanceFreq;
156                 LARGE_INTEGER PerformanceCount;
157
158                 if (!QueryPerformanceFrequency (&PerformanceFreq))
159                 {
160                         Con_Printf ("No hardware timer available\n");
161                         // fall back to timeGetTime
162                         Cvar_SetValueQuick(&sys_usetimegettime, true);
163                         return Sys_DoubleTime();
164                 }
165                 QueryPerformanceCounter (&PerformanceCount);
166
167                 #ifdef __BORLANDC__
168                 timescale = 1.0 / ((double) PerformanceFreq.u.LowPart + (double) PerformanceFreq.u.HighPart * 65536.0 * 65536.0);
169                 newtime = ((double) PerformanceCount.u.LowPart + (double) PerformanceCount.u.HighPart * 65536.0 * 65536.0) * timescale;
170                 #else
171                 timescale = 1.0 / ((double) PerformanceFreq.LowPart + (double) PerformanceFreq.HighPart * 65536.0 * 65536.0);
172                 newtime = ((double) PerformanceCount.LowPart + (double) PerformanceCount.HighPart * 65536.0 * 65536.0) * timescale;
173                 #endif
174         }
175
176         if (first)
177         {
178                 first = false;
179                 oldtime = newtime;
180         }
181
182         if (newtime < oldtime)
183         {
184                 // warn if it's significant
185                 if (newtime - oldtime < -0.01)
186                         Con_Printf("Sys_DoubleTime: time stepped backwards (went from %f to %f, difference %f)\n", oldtime, newtime, newtime - oldtime);
187         }
188         else
189                 curtime += newtime - oldtime;
190         oldtime = newtime;
191
192         return curtime;
193 }
194
195
196 char *Sys_ConsoleInput (void)
197 {
198         static char text[MAX_INPUTLINE];
199         static int len;
200         INPUT_RECORD recs[1024];
201         int ch;
202         DWORD numread, numevents, dummy;
203
204         if (cls.state != ca_dedicated)
205                 return NULL;
206
207
208         for ( ;; )
209         {
210                 if (!GetNumberOfConsoleInputEvents (hinput, &numevents))
211                         Sys_Error ("Error getting # of console events");
212
213                 if (numevents <= 0)
214                         break;
215
216                 if (!ReadConsoleInput(hinput, recs, 1, &numread))
217                         Sys_Error ("Error reading console input");
218
219                 if (numread != 1)
220                         Sys_Error ("Couldn't read console input");
221
222                 if (recs[0].EventType == KEY_EVENT)
223                 {
224                         if (!recs[0].Event.KeyEvent.bKeyDown)
225                         {
226                                 ch = recs[0].Event.KeyEvent.uChar.AsciiChar;
227
228                                 switch (ch)
229                                 {
230                                         case '\r':
231                                                 WriteFile(houtput, "\r\n", 2, &dummy, NULL);
232
233                                                 if (len)
234                                                 {
235                                                         text[len] = 0;
236                                                         len = 0;
237                                                         return text;
238                                                 }
239
240                                                 break;
241
242                                         case '\b':
243                                                 WriteFile(houtput, "\b \b", 3, &dummy, NULL);
244                                                 if (len)
245                                                 {
246                                                         len--;
247                                                 }
248                                                 break;
249
250                                         default:
251                                                 if (ch >= ' ')
252                                                 {
253                                                         WriteFile(houtput, &ch, 1, &dummy, NULL);
254                                                         text[len] = ch;
255                                                         len = (len + 1) & 0xff;
256                                                 }
257
258                                                 break;
259
260                                 }
261                         }
262                 }
263         }
264
265         return NULL;
266 }
267
268 void Sys_Sleep(int microseconds)
269 {
270         if (microseconds < 1000)
271                 microseconds = 1000;
272         Sleep(microseconds / 1000);
273 }
274
275 char *Sys_GetClipboardData (void)
276 {
277         char *data = NULL;
278         char *cliptext;
279
280         if (OpenClipboard (NULL) != 0)
281         {
282                 HANDLE hClipboardData;
283
284                 if ((hClipboardData = GetClipboardData (CF_TEXT)) != 0)
285                 {
286                         if ((cliptext = GlobalLock (hClipboardData)) != 0)
287                         {
288                                 size_t allocsize;
289                                 allocsize = GlobalSize (hClipboardData) + 1;
290                                 data = Z_Malloc (allocsize);
291                                 strlcpy (data, cliptext, allocsize);
292                                 GlobalUnlock (hClipboardData);
293                         }
294                 }
295                 CloseClipboard ();
296         }
297         return data;
298 }
299
300 void Sys_InitConsole (void)
301 {
302         int t;
303
304         // initialize the windows dedicated server console if needed
305         tevent = CreateEvent(NULL, false, false, NULL);
306
307         if (!tevent)
308                 Sys_Error ("Couldn't create event");
309
310         // LordHavoc: can't check cls.state because it hasn't been initialized yet
311         // if (cls.state == ca_dedicated)
312         if (COM_CheckParm("-dedicated"))
313         {
314                 if (!AllocConsole ())
315                         Sys_Error ("Couldn't create dedicated server console");
316
317                 hinput = GetStdHandle (STD_INPUT_HANDLE);
318                 houtput = GetStdHandle (STD_OUTPUT_HANDLE);
319
320 #ifdef _WIN64
321 #define atoi _atoi64
322 #endif
323         // give QHOST a chance to hook into the console
324                 if ((t = COM_CheckParm ("-HFILE")) > 0)
325                 {
326                         if (t < com_argc)
327                                 hFile = (HANDLE)atoi (com_argv[t+1]);
328                 }
329
330                 if ((t = COM_CheckParm ("-HPARENT")) > 0)
331                 {
332                         if (t < com_argc)
333                                 heventParent = (HANDLE)atoi (com_argv[t+1]);
334                 }
335
336                 if ((t = COM_CheckParm ("-HCHILD")) > 0)
337                 {
338                         if (t < com_argc)
339                                 heventChild = (HANDLE)atoi (com_argv[t+1]);
340                 }
341
342                 InitConProc (hFile, heventParent, heventChild);
343         }
344
345 // because sound is off until we become active
346         S_BlockSound ();
347 }
348
349 void Sys_Init_Commands (void)
350 {
351         Cvar_RegisterVariable(&sys_usetimegettime);
352 }
353
354 /*
355 ==============================================================================
356
357 WINDOWS CRAP
358
359 ==============================================================================
360 */
361
362
363 /*
364 ==================
365 WinMain
366 ==================
367 */
368 HINSTANCE       global_hInstance;
369 const char      *argv[MAX_NUM_ARGVS];
370 char            program_name[MAX_OSPATH];
371
372 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
373 {
374         MEMORYSTATUS lpBuffer;
375
376         /* previous instances do not exist in Win32 */
377         if (hPrevInstance)
378                 return 0;
379
380         global_hInstance = hInstance;
381
382         lpBuffer.dwLength = sizeof(MEMORYSTATUS);
383         GlobalMemoryStatus (&lpBuffer);
384
385         program_name[sizeof(program_name)-1] = 0;
386         GetModuleFileNameA(NULL, program_name, sizeof(program_name) - 1);
387
388         com_argc = 1;
389         com_argv = argv;
390         argv[0] = program_name;
391
392         // FIXME: this tokenizer is rather redundent, call a more general one
393         while (*lpCmdLine && (com_argc < MAX_NUM_ARGVS))
394         {
395                 while (*lpCmdLine && *lpCmdLine <= ' ')
396                         lpCmdLine++;
397
398                 if (!*lpCmdLine)
399                         break;
400
401                 if (*lpCmdLine == '\"')
402                 {
403                         // quoted string
404                         lpCmdLine++;
405                         argv[com_argc] = lpCmdLine;
406                         com_argc++;
407                         while (*lpCmdLine && (*lpCmdLine != '\"'))
408                                 lpCmdLine++;
409                 }
410                 else
411                 {
412                         // unquoted word
413                         argv[com_argc] = lpCmdLine;
414                         com_argc++;
415                         while (*lpCmdLine && *lpCmdLine > ' ')
416                                 lpCmdLine++;
417                 }
418
419                 if (*lpCmdLine)
420                 {
421                         *lpCmdLine = 0;
422                         lpCmdLine++;
423                 }
424         }
425
426         Host_Main();
427
428         /* return success of application */
429         return true;
430 }