2 Copyright (C) 1996-1997 Id Software, Inc.
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.
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.
13 See the GNU General Public License for more details.
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.
20 // sys_win.c -- Win32 system interface code
22 #define WIN32_USETIMEGETTIME 0
31 #define CONSOLE_ERROR_TIMEOUT 60.0 // # of seconds to wait on Sys_Error running
32 // dedicated before exiting
33 #define PAUSE_SLEEP 50 // sleep time on pause or minimization
34 #define NOT_FOCUS_SLEEP 20 // sleep time when not focus
37 qboolean ActiveApp, Minimized;
39 static qboolean sc_return_on_enter = false;
40 HANDLE hinput, houtput;
42 static char *tracking_tag = "Clams & Mooses";
46 static HANDLE heventParent;
47 static HANDLE heventChild;
50 ===============================================================================
54 ===============================================================================
57 // LordHavoc: 256 pak files (was 10)
58 #define MAX_HANDLES 256
59 QFile *sys_handles[MAX_HANDLES];
65 for (i=1 ; i<MAX_HANDLES ; i++)
68 Sys_Error ("out of handles");
77 int filelength (QFile *f)
83 Qseek (f, 0, SEEK_END);
85 Qseek (f, pos, SEEK_SET);
90 int Sys_FileOpenRead (char *path, int *hndl)
97 f = Qopen(path, "rbz");
108 retval = filelength(f);
114 int Sys_FileOpenWrite (char *path)
121 f = Qopen(path, "wb");
124 Con_Printf("Sys_FileOpenWrite: Error opening %s: %s", path, strerror(errno));
132 void Sys_FileClose (int handle)
134 Qclose (sys_handles[handle]);
135 sys_handles[handle] = NULL;
138 void Sys_FileSeek (int handle, int position)
140 Qseek (sys_handles[handle], position, SEEK_SET);
143 int Sys_FileRead (int handle, void *dest, int count)
145 return Qread (sys_handles[handle], dest, count);
148 int Sys_FileWrite (int handle, void *data, int count)
150 return Qwrite (sys_handles[handle], data, count);
153 int Sys_FileTime (char *path)
157 f = Qopen(path, "rb");
167 void Sys_mkdir (char *path)
174 ===============================================================================
178 ===============================================================================
181 void Sys_Error (char *error, ...)
184 char text[1024], text2[1024];
185 char *text3 = "Press Enter to exit\n";
186 char *text4 = "***********************************\n";
190 static int in_sys_error0 = 0;
191 static int in_sys_error1 = 0;
192 static int in_sys_error2 = 0;
193 static int in_sys_error3 = 0;
198 va_start (argptr, error);
199 vsprintf (text, error, argptr);
202 if (cls.state == ca_dedicated)
204 va_start (argptr, error);
205 vsprintf (text, error, argptr);
208 sprintf (text2, "ERROR: %s\n", text);
209 WriteFile (houtput, text5, strlen (text5), &dummy, NULL);
210 WriteFile (houtput, text4, strlen (text4), &dummy, NULL);
211 WriteFile (houtput, text2, strlen (text2), &dummy, NULL);
212 WriteFile (houtput, text3, strlen (text3), &dummy, NULL);
213 WriteFile (houtput, text4, strlen (text4), &dummy, NULL);
216 starttime = Sys_DoubleTime ();
217 sc_return_on_enter = true; // so Enter will get us out of here
219 while (!Sys_ConsoleInput () && ((Sys_DoubleTime () - starttime) < CONSOLE_ERROR_TIMEOUT))
225 // switch to windowed so the message box is visible, unless we already
226 // tried that and failed
230 VID_SetDefaultMode ();
231 MessageBox(NULL, text, "Quake Error", MB_OK | MB_SETFOREGROUND | MB_ICONSTOP);
235 MessageBox(NULL, text, "Double Quake Error", MB_OK | MB_SETFOREGROUND | MB_ICONSTOP);
245 // shut down QHOST hooks if necessary
261 CloseHandle (tevent);
263 if (cls.state == ca_dedicated)
266 // shut down QHOST hooks if necessary
278 double Sys_DoubleTime (void)
280 // LordHavoc: note to people modifying this code, DWORD is specifically defined as an unsigned 32bit number, therefore the 65536.0 * 65536.0 is fine.
281 #if WIN32_USETIMEGETTIME
284 // Windows 95/98/ME/NT/2000
286 // reasonable accuracy (millisecond)
289 static int first = true;
290 static double oldtime = 0.0, basetime = 0.0, old = 0.0;
293 now = (double) timeGetTime () + basetime;
305 basetime += (65536.0 * 65536.0);
306 now += (65536.0 * 65536.0);
310 newtime = now / 1000.0;
312 if (newtime < oldtime)
313 Sys_Error("Sys_DoubleTime: time running backwards??\n");
319 // QueryPerformanceCounter
321 // Windows 95/98/ME/NT/2000
323 // very accurate (CPU cycles)
325 // does not necessarily match realtime too well (tends to get faster and faster in win98)
326 static int first = true;
327 static double oldtime = 0.0, basetime = 0.0, timescale = 0.0;
329 LARGE_INTEGER PerformanceFreq;
330 LARGE_INTEGER PerformanceCount;
334 if (!QueryPerformanceFrequency (&PerformanceFreq))
335 Sys_Error ("No hardware timer available");
338 timescale = 1.0 / ((double) PerformanceFreq.u.LowPart + (double) PerformanceFreq.u.HighPart * 65536.0 * 65536.0);
340 timescale = 1.0 / ((double) PerformanceFreq.LowPart + (double) PerformanceFreq.HighPart * 65536.0 * 65536.0);
344 QueryPerformanceCounter (&PerformanceCount);
347 newtime = ((double) PerformanceCount.u.LowPart + (double) PerformanceCount.u.HighPart * 65536.0 * 65536.0) * timescale - basetime;
349 newtime = ((double) PerformanceCount.LowPart + (double) PerformanceCount.HighPart * 65536.0 * 65536.0) * timescale - basetime;
359 if (newtime < oldtime)
360 Sys_Error("Sys_DoubleTime: time running backwards??\n");
369 char *Sys_ConsoleInput (void)
371 static char text[256];
373 INPUT_RECORD recs[1024];
375 int ch, numread, numevents;
377 if (cls.state != ca_dedicated)
383 if (!GetNumberOfConsoleInputEvents (hinput, &numevents))
384 Sys_Error ("Error getting # of console events");
389 if (!ReadConsoleInput(hinput, recs, 1, &numread))
390 Sys_Error ("Error reading console input");
393 Sys_Error ("Couldn't read console input");
395 if (recs[0].EventType == KEY_EVENT)
397 if (!recs[0].Event.KeyEvent.bKeyDown)
399 ch = recs[0].Event.KeyEvent.uChar.AsciiChar;
404 WriteFile(houtput, "\r\n", 2, &dummy, NULL);
412 else if (sc_return_on_enter)
414 // special case to allow exiting from the error handler on Enter
423 WriteFile(houtput, "\b \b", 3, &dummy, NULL);
433 WriteFile(houtput, &ch, 1, &dummy, NULL);
435 len = (len + 1) & 0xff;
448 void Sys_Sleep (void)
454 void Sys_SendKeyEvents (void)
458 while (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
460 // we always update if there are any event, even if we're paused
463 if (!GetMessage (&msg, NULL, 0, 0))
466 TranslateMessage (&msg);
467 DispatchMessage (&msg);
473 ==============================================================================
477 ==============================================================================
486 void SleepUntilInput (int time)
489 MsgWaitForMultipleObjects(1, &tevent, false, time, QS_ALLINPUT);
498 HINSTANCE global_hInstance;
500 char *argv[MAX_NUM_ARGVS];
501 static char *empty_string = "";
503 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
505 double oldtime, newtime;
506 MEMORYSTATUS lpBuffer;
507 static char cwd[1024];
510 /* previous instances do not exist in Win32 */
514 global_hInstance = hInstance;
515 global_nCmdShow = nCmdShow;
517 lpBuffer.dwLength = sizeof(MEMORYSTATUS);
518 GlobalMemoryStatus (&lpBuffer);
520 if (!GetCurrentDirectory (sizeof(cwd), cwd))
521 Sys_Error ("Couldn't determine current directory");
523 if (cwd[strlen(cwd)-1] == '/')
524 cwd[strlen(cwd)-1] = 0;
526 memset(&host_parms, 0, sizeof(host_parms));
528 host_parms.basedir = cwd;
531 argv[0] = empty_string;
533 while (*lpCmdLine && (host_parms.argc < MAX_NUM_ARGVS))
535 while (*lpCmdLine && ((*lpCmdLine <= 32) || (*lpCmdLine > 126)))
540 argv[host_parms.argc] = lpCmdLine;
543 while (*lpCmdLine && ((*lpCmdLine > 32) && (*lpCmdLine <= 126)))
555 host_parms.argv = argv;
557 COM_InitArgv (host_parms.argc, host_parms.argv);
559 host_parms.argc = com_argc;
560 host_parms.argv = com_argv;
562 Sys_Shared_EarlyInit();
564 tevent = CreateEvent(NULL, false, false, NULL);
567 Sys_Error ("Couldn't create event");
569 // LordHavoc: can't check cls.state because it hasn't been initialized yet
570 // if (cls.state == ca_dedicated)
571 if (COM_CheckParm("-dedicated"))
573 if (!AllocConsole ())
574 Sys_Error ("Couldn't create dedicated server console");
576 hinput = GetStdHandle (STD_INPUT_HANDLE);
577 houtput = GetStdHandle (STD_OUTPUT_HANDLE);
579 // give QHOST a chance to hook into the console
580 if ((t = COM_CheckParm ("-HFILE")) > 0)
583 hFile = (HANDLE)atoi (com_argv[t+1]);
586 if ((t = COM_CheckParm ("-HPARENT")) > 0)
589 heventParent = (HANDLE)atoi (com_argv[t+1]);
592 if ((t = COM_CheckParm ("-HCHILD")) > 0)
595 heventChild = (HANDLE)atoi (com_argv[t+1]);
598 InitConProc (hFile, heventParent, heventChild);
601 // because sound is off until we become active
604 #if WIN32_USETIMEGETTIME
605 // make sure the timer is high precision, otherwise NT gets 18ms resolution
607 // Windows 2000 Advanced Server (and possibly other versions)
608 // apparently have a broken timer, because it runs at more like 10x speed
609 // if this isn't used, heh
615 Sys_Shared_LateInit();
617 oldtime = Sys_DoubleTime ();
619 /* main window message loop */
622 if (cls.state != ca_dedicated)
624 // yield the CPU for a little while when paused, minimized, or not the focus
625 if ((cl.paused && !ActiveApp) || Minimized)
627 SleepUntilInput (PAUSE_SLEEP);
628 scr_skipupdate = 1; // no point in bothering to draw
631 SleepUntilInput (NOT_FOCUS_SLEEP);
634 newtime = Sys_DoubleTime ();
635 Host_Frame (newtime - oldtime);
639 /* return success of application */