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;
49 volatile int sys_checksum;
58 void Sys_PageIn (void *ptr, int size)
63 // touch all the memory to make sure it's there. The 16-page skip is to
64 // keep Win 95 from thinking we're trying to page ourselves in (we are
65 // doing that, of course, but there's no reason we shouldn't)
70 for (m=0 ; m<(size - 16 * 0x1000) ; m += 4)
72 sys_checksum += *(int *)&x[m];
73 sys_checksum += *(int *)&x[m + 16 * 0x1000];
81 ===============================================================================
85 ===============================================================================
88 // LordHavoc: 256 pak files (was 10)
89 #define MAX_HANDLES 256
90 QFile *sys_handles[MAX_HANDLES];
96 for (i=1 ; i<MAX_HANDLES ; i++)
99 Sys_Error ("out of handles");
108 int filelength (QFile *f)
114 Qseek (f, 0, SEEK_END);
116 Qseek (f, pos, SEEK_SET);
121 int Sys_FileOpenRead (char *path, int *hndl)
128 f = Qopen(path, "rbz");
139 retval = filelength(f);
145 int Sys_FileOpenWrite (char *path)
152 f = Qopen(path, "wb");
155 Con_Printf("Sys_FileOpenWrite: Error opening %s: %s", path, strerror(errno));
163 void Sys_FileClose (int handle)
165 Qclose (sys_handles[handle]);
166 sys_handles[handle] = NULL;
169 void Sys_FileSeek (int handle, int position)
171 Qseek (sys_handles[handle], position, SEEK_SET);
174 int Sys_FileRead (int handle, void *dest, int count)
176 return Qread (sys_handles[handle], dest, count);
179 int Sys_FileWrite (int handle, void *data, int count)
181 return Qwrite (sys_handles[handle], data, count);
184 int Sys_FileTime (char *path)
188 f = Qopen(path, "rb");
198 void Sys_mkdir (char *path)
205 ===============================================================================
209 ===============================================================================
212 void Sys_Error (char *error, ...)
215 char text[1024], text2[1024];
216 char *text3 = "Press Enter to exit\n";
217 char *text4 = "***********************************\n";
221 static int in_sys_error0 = 0;
222 static int in_sys_error1 = 0;
223 static int in_sys_error2 = 0;
224 static int in_sys_error3 = 0;
229 va_start (argptr, error);
230 vsprintf (text, error, argptr);
233 if (cls.state == ca_dedicated)
235 va_start (argptr, error);
236 vsprintf (text, error, argptr);
239 sprintf (text2, "ERROR: %s\n", text);
240 WriteFile (houtput, text5, strlen (text5), &dummy, NULL);
241 WriteFile (houtput, text4, strlen (text4), &dummy, NULL);
242 WriteFile (houtput, text2, strlen (text2), &dummy, NULL);
243 WriteFile (houtput, text3, strlen (text3), &dummy, NULL);
244 WriteFile (houtput, text4, strlen (text4), &dummy, NULL);
247 starttime = Sys_DoubleTime ();
248 sc_return_on_enter = true; // so Enter will get us out of here
250 while (!Sys_ConsoleInput () && ((Sys_DoubleTime () - starttime) < CONSOLE_ERROR_TIMEOUT))
256 // switch to windowed so the message box is visible, unless we already
257 // tried that and failed
261 VID_SetDefaultMode ();
262 MessageBox(NULL, text, "Quake Error", MB_OK | MB_SETFOREGROUND | MB_ICONSTOP);
266 MessageBox(NULL, text, "Double Quake Error", MB_OK | MB_SETFOREGROUND | MB_ICONSTOP);
276 // shut down QHOST hooks if necessary
292 CloseHandle (tevent);
294 if (cls.state == ca_dedicated)
297 // shut down QHOST hooks if necessary
309 double Sys_DoubleTime (void)
311 // LordHavoc: note to people modifying this code, DWORD is specifically defined as an unsigned 32bit number, therefore the 65536.0 * 65536.0 is fine.
312 #if WIN32_USETIMEGETTIME
315 // Windows 95/98/ME/NT/2000
317 // reasonable accuracy (millisecond)
320 static int first = true;
321 static double oldtime = 0.0, basetime = 0.0, old = 0.0;
324 now = (double) timeGetTime () + basetime;
336 basetime += (65536.0 * 65536.0);
337 now += (65536.0 * 65536.0);
341 newtime = now / 1000.0;
343 if (newtime < oldtime)
344 Sys_Error("Sys_DoubleTime: time running backwards??\n");
350 // QueryPerformanceCounter
352 // Windows 95/98/ME/NT/2000
354 // very accurate (CPU cycles)
356 // does not necessarily match realtime too well (tends to get faster and faster in win98)
357 static int first = true;
358 static double oldtime = 0.0, basetime = 0.0, timescale = 0.0;
360 LARGE_INTEGER PerformanceFreq;
361 LARGE_INTEGER PerformanceCount;
365 if (!QueryPerformanceFrequency (&PerformanceFreq))
366 Sys_Error ("No hardware timer available");
369 timescale = 1.0 / ((double) PerformanceFreq.u.LowPart + (double) PerformanceFreq.u.HighPart * 65536.0 * 65536.0);
371 timescale = 1.0 / ((double) PerformanceFreq.LowPart + (double) PerformanceFreq.HighPart * 65536.0 * 65536.0);
375 QueryPerformanceCounter (&PerformanceCount);
378 newtime = ((double) PerformanceCount.u.LowPart + (double) PerformanceCount.u.HighPart * 65536.0 * 65536.0) * timescale - basetime;
380 newtime = ((double) PerformanceCount.LowPart + (double) PerformanceCount.HighPart * 65536.0 * 65536.0) * timescale - basetime;
390 if (newtime < oldtime)
391 Sys_Error("Sys_DoubleTime: time running backwards??\n");
400 char *Sys_ConsoleInput (void)
402 static char text[256];
404 INPUT_RECORD recs[1024];
406 int ch, numread, numevents;
408 if (cls.state != ca_dedicated)
414 if (!GetNumberOfConsoleInputEvents (hinput, &numevents))
415 Sys_Error ("Error getting # of console events");
420 if (!ReadConsoleInput(hinput, recs, 1, &numread))
421 Sys_Error ("Error reading console input");
424 Sys_Error ("Couldn't read console input");
426 if (recs[0].EventType == KEY_EVENT)
428 if (!recs[0].Event.KeyEvent.bKeyDown)
430 ch = recs[0].Event.KeyEvent.uChar.AsciiChar;
435 WriteFile(houtput, "\r\n", 2, &dummy, NULL);
443 else if (sc_return_on_enter)
445 // special case to allow exiting from the error handler on Enter
454 WriteFile(houtput, "\b \b", 3, &dummy, NULL);
464 WriteFile(houtput, &ch, 1, &dummy, NULL);
466 len = (len + 1) & 0xff;
479 void Sys_Sleep (void)
485 void Sys_SendKeyEvents (void)
489 while (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
491 // we always update if there are any event, even if we're paused
494 if (!GetMessage (&msg, NULL, 0, 0))
497 TranslateMessage (&msg);
498 DispatchMessage (&msg);
504 ==============================================================================
508 ==============================================================================
517 void SleepUntilInput (int time)
520 MsgWaitForMultipleObjects(1, &tevent, false, time, QS_ALLINPUT);
529 HINSTANCE global_hInstance;
531 char *argv[MAX_NUM_ARGVS];
532 static char *empty_string = "";
534 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
536 double oldtime, newtime;
537 MEMORYSTATUS lpBuffer;
538 static char cwd[1024];
541 /* previous instances do not exist in Win32 */
545 global_hInstance = hInstance;
546 global_nCmdShow = nCmdShow;
548 lpBuffer.dwLength = sizeof(MEMORYSTATUS);
549 GlobalMemoryStatus (&lpBuffer);
551 if (!GetCurrentDirectory (sizeof(cwd), cwd))
552 Sys_Error ("Couldn't determine current directory");
554 if (cwd[strlen(cwd)-1] == '/')
555 cwd[strlen(cwd)-1] = 0;
557 memset(&host_parms, 0, sizeof(host_parms));
559 host_parms.basedir = cwd;
562 argv[0] = empty_string;
564 while (*lpCmdLine && (host_parms.argc < MAX_NUM_ARGVS))
566 while (*lpCmdLine && ((*lpCmdLine <= 32) || (*lpCmdLine > 126)))
571 argv[host_parms.argc] = lpCmdLine;
574 while (*lpCmdLine && ((*lpCmdLine > 32) && (*lpCmdLine <= 126)))
586 host_parms.argv = argv;
588 COM_InitArgv (host_parms.argc, host_parms.argv);
590 host_parms.argc = com_argc;
591 host_parms.argv = com_argv;
595 // take the greater of all the available memory or half the total memory,
596 // but at least 8 Mb and no more than 16 Mb, unless they explicitly
599 host_parms.memsize = lpBuffer.dwAvailPhys;
601 if (host_parms.memsize < MINIMUM_WIN_MEMORY)
602 host_parms.memsize = MINIMUM_WIN_MEMORY;
604 if (host_parms.memsize < (lpBuffer.dwTotalPhys >> 1))
605 host_parms.memsize = lpBuffer.dwTotalPhys >> 1;
607 if (host_parms.memsize > MAXIMUM_WIN_MEMORY)
608 host_parms.memsize = MAXIMUM_WIN_MEMORY;
611 // Sys_PageIn (parms.membase, parms.memsize);
613 tevent = CreateEvent(NULL, false, false, NULL);
616 Sys_Error ("Couldn't create event");
618 // LordHavoc: can't check cls.state because it hasn't been initialized yet
619 // if (cls.state == ca_dedicated)
620 if (COM_CheckParm("-dedicated"))
622 if (!AllocConsole ())
623 Sys_Error ("Couldn't create dedicated server console");
625 hinput = GetStdHandle (STD_INPUT_HANDLE);
626 houtput = GetStdHandle (STD_OUTPUT_HANDLE);
628 // give QHOST a chance to hook into the console
629 if ((t = COM_CheckParm ("-HFILE")) > 0)
632 hFile = (HANDLE)atoi (com_argv[t+1]);
635 if ((t = COM_CheckParm ("-HPARENT")) > 0)
638 heventParent = (HANDLE)atoi (com_argv[t+1]);
641 if ((t = COM_CheckParm ("-HCHILD")) > 0)
644 heventChild = (HANDLE)atoi (com_argv[t+1]);
647 InitConProc (hFile, heventParent, heventChild);
650 // because sound is off until we become active
653 #if WIN32_USETIMEGETTIME
654 // make sure the timer is high precision, otherwise NT gets 18ms resolution
656 // Windows 2000 Advanced Server (and possibly other versions)
657 // apparently have a broken timer, because it runs at more like 10x speed
658 // if this isn't used, heh
662 Sys_Printf ("Host_Init\n");
665 oldtime = Sys_DoubleTime ();
667 /* main window message loop */
670 if (cls.state != ca_dedicated)
672 // yield the CPU for a little while when paused, minimized, or not the focus
673 if ((cl.paused && !ActiveApp) || Minimized)
675 SleepUntilInput (PAUSE_SLEEP);
676 scr_skipupdate = 1; // no point in bothering to draw
679 SleepUntilInput (NOT_FOCUS_SLEEP);
682 newtime = Sys_DoubleTime ();
683 Host_Frame (newtime - oldtime);
687 /* return success of application */