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;
40 static qboolean sc_return_on_enter = false;
41 HANDLE hinput, houtput;
43 static char *tracking_tag = "Clams & Mooses";
47 static HANDLE heventParent;
48 static HANDLE heventChild;
50 volatile int sys_checksum;
59 void Sys_PageIn (void *ptr, int size)
64 // touch all the memory to make sure it's there. The 16-page skip is to
65 // keep Win 95 from thinking we're trying to page ourselves in (we are
66 // doing that, of course, but there's no reason we shouldn't)
71 for (m=0 ; m<(size - 16 * 0x1000) ; m += 4)
73 sys_checksum += *(int *)&x[m];
74 sys_checksum += *(int *)&x[m + 16 * 0x1000];
82 ===============================================================================
86 ===============================================================================
89 // LordHavoc: 256 pak files (was 10)
90 #define MAX_HANDLES 256
91 QFile *sys_handles[MAX_HANDLES];
97 for (i=1 ; i<MAX_HANDLES ; i++)
100 Sys_Error ("out of handles");
109 int filelength (QFile *f)
115 Qseek (f, 0, SEEK_END);
117 Qseek (f, pos, SEEK_SET);
122 int Sys_FileOpenRead (char *path, int *hndl)
129 f = Qopen(path, "rbz");
140 retval = filelength(f);
146 int Sys_FileOpenWrite (char *path)
153 f = Qopen(path, "wb");
156 Con_Printf("Sys_FileOpenWrite: Error opening %s: %s", path, strerror(errno));
164 void Sys_FileClose (int handle)
166 Qclose (sys_handles[handle]);
167 sys_handles[handle] = NULL;
170 void Sys_FileSeek (int handle, int position)
172 Qseek (sys_handles[handle], position, SEEK_SET);
175 int Sys_FileRead (int handle, void *dest, int count)
177 return Qread (sys_handles[handle], dest, count);
180 int Sys_FileWrite (int handle, void *data, int count)
182 return Qwrite (sys_handles[handle], data, count);
185 int Sys_FileTime (char *path)
189 f = Qopen(path, "rb");
199 void Sys_mkdir (char *path)
206 ===============================================================================
210 ===============================================================================
213 void Sys_Error (char *error, ...)
216 char text[1024], text2[1024];
217 char *text3 = "Press Enter to exit\n";
218 char *text4 = "***********************************\n";
222 static int in_sys_error0 = 0;
223 static int in_sys_error1 = 0;
224 static int in_sys_error2 = 0;
225 static int in_sys_error3 = 0;
230 va_start (argptr, error);
231 vsprintf (text, error, argptr);
236 va_start (argptr, error);
237 vsprintf (text, error, argptr);
240 sprintf (text2, "ERROR: %s\n", text);
241 WriteFile (houtput, text5, strlen (text5), &dummy, NULL);
242 WriteFile (houtput, text4, strlen (text4), &dummy, NULL);
243 WriteFile (houtput, text2, strlen (text2), &dummy, NULL);
244 WriteFile (houtput, text3, strlen (text3), &dummy, NULL);
245 WriteFile (houtput, text4, strlen (text4), &dummy, NULL);
248 starttime = Sys_DoubleTime ();
249 sc_return_on_enter = true; // so Enter will get us out of here
251 while (!Sys_ConsoleInput () &&
252 ((Sys_DoubleTime () - starttime) < CONSOLE_ERROR_TIMEOUT))
258 // switch to windowed so the message box is visible, unless we already
259 // tried that and failed
263 VID_SetDefaultMode ();
264 MessageBox(NULL, text, "Quake Error",
265 MB_OK | MB_SETFOREGROUND | MB_ICONSTOP);
269 MessageBox(NULL, text, "Double Quake Error",
270 MB_OK | MB_SETFOREGROUND | MB_ICONSTOP);
280 // shut down QHOST hooks if necessary
290 void Sys_Printf (char *fmt, ...)
298 va_start (argptr,fmt);
299 vsprintf (text, fmt, argptr);
302 WriteFile(houtput, text, strlen (text), &dummy, NULL);
312 CloseHandle (tevent);
317 // shut down QHOST hooks if necessary
329 double Sys_DoubleTime (void)
331 // LordHavoc: note to people modifying this code, DWORD is specifically defined as an unsigned 32bit number, therefore the 65536.0 * 65536.0 is fine.
332 #if WIN32_USETIMEGETTIME
335 // Windows 95/98/ME/NT/2000
337 // reasonable accuracy (millisecond)
340 static int first = true;
341 static double oldtime = 0.0, basetime = 0.0, old = 0.0;
344 now = (double) timeGetTime () + basetime;
356 basetime += (65536.0 * 65536.0);
357 now += (65536.0 * 65536.0);
361 newtime = now / 1000.0;
363 if (newtime < oldtime)
364 Sys_Error("Sys_DoubleTime: time running backwards??\n");
370 // QueryPerformanceCounter
372 // Windows 95/98/ME/NT/2000
374 // very accurate (CPU cycles)
376 // does not necessarily match realtime too well (tends to get faster and faster in win98)
377 static int first = true;
378 static double oldtime = 0.0, basetime = 0.0, timescale = 0.0;
380 LARGE_INTEGER PerformanceFreq;
381 LARGE_INTEGER PerformanceCount;
385 if (!QueryPerformanceFrequency (&PerformanceFreq))
386 Sys_Error ("No hardware timer available");
389 timescale = 1.0 / ((double) PerformanceFreq.u.LowPart + (double) PerformanceFreq.u.HighPart * 65536.0 * 65536.0);
391 timescale = 1.0 / ((double) PerformanceFreq.LowPart + (double) PerformanceFreq.HighPart * 65536.0 * 65536.0);
395 QueryPerformanceCounter (&PerformanceCount);
398 newtime = ((double) PerformanceCount.u.LowPart + (double) PerformanceCount.u.HighPart * 65536.0 * 65536.0) * timescale - basetime;
400 newtime = ((double) PerformanceCount.LowPart + (double) PerformanceCount.HighPart * 65536.0 * 65536.0) * timescale - basetime;
410 if (newtime < oldtime)
411 Sys_Error("Sys_DoubleTime: time running backwards??\n");
420 char *Sys_ConsoleInput (void)
422 static char text[256];
424 INPUT_RECORD recs[1024];
426 int ch, numread, numevents;
434 if (!GetNumberOfConsoleInputEvents (hinput, &numevents))
435 Sys_Error ("Error getting # of console events");
440 if (!ReadConsoleInput(hinput, recs, 1, &numread))
441 Sys_Error ("Error reading console input");
444 Sys_Error ("Couldn't read console input");
446 if (recs[0].EventType == KEY_EVENT)
448 if (!recs[0].Event.KeyEvent.bKeyDown)
450 ch = recs[0].Event.KeyEvent.uChar.AsciiChar;
455 WriteFile(houtput, "\r\n", 2, &dummy, NULL);
463 else if (sc_return_on_enter)
465 // special case to allow exiting from the error handler on Enter
474 WriteFile(houtput, "\b \b", 3, &dummy, NULL);
484 WriteFile(houtput, &ch, 1, &dummy, NULL);
486 len = (len + 1) & 0xff;
499 void Sys_Sleep (void)
505 void Sys_SendKeyEvents (void)
509 while (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
511 // we always update if there are any event, even if we're paused
514 if (!GetMessage (&msg, NULL, 0, 0))
517 TranslateMessage (&msg);
518 DispatchMessage (&msg);
524 ==============================================================================
528 ==============================================================================
537 void SleepUntilInput (int time)
540 MsgWaitForMultipleObjects(1, &tevent, false, time, QS_ALLINPUT);
549 HINSTANCE global_hInstance;
551 char *argv[MAX_NUM_ARGVS];
552 static char *empty_string = "";
554 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
556 double time, oldtime, newtime/*, timediff*/;
557 MEMORYSTATUS lpBuffer;
558 static char cwd[1024];
561 /* previous instances do not exist in Win32 */
565 global_hInstance = hInstance;
566 global_nCmdShow = nCmdShow;
568 lpBuffer.dwLength = sizeof(MEMORYSTATUS);
569 GlobalMemoryStatus (&lpBuffer);
571 if (!GetCurrentDirectory (sizeof(cwd), cwd))
572 Sys_Error ("Couldn't determine current directory");
574 if (cwd[strlen(cwd)-1] == '/')
575 cwd[strlen(cwd)-1] = 0;
577 memset(&host_parms, 0, sizeof(host_parms));
579 host_parms.basedir = cwd;
582 argv[0] = empty_string;
584 while (*lpCmdLine && (host_parms.argc < MAX_NUM_ARGVS))
586 while (*lpCmdLine && ((*lpCmdLine <= 32) || (*lpCmdLine > 126)))
591 argv[host_parms.argc] = lpCmdLine;
594 while (*lpCmdLine && ((*lpCmdLine > 32) && (*lpCmdLine <= 126)))
606 host_parms.argv = argv;
608 COM_InitArgv (host_parms.argc, host_parms.argv);
610 host_parms.argc = com_argc;
611 host_parms.argv = com_argv;
613 isDedicated = (COM_CheckParm ("-dedicated") != 0);
615 // take the greater of all the available memory or half the total memory,
616 // but at least 8 Mb and no more than 16 Mb, unless they explicitly
619 host_parms.memsize = lpBuffer.dwAvailPhys;
621 if (host_parms.memsize < MINIMUM_WIN_MEMORY)
622 host_parms.memsize = MINIMUM_WIN_MEMORY;
624 if (host_parms.memsize < (lpBuffer.dwTotalPhys >> 1))
625 host_parms.memsize = lpBuffer.dwTotalPhys >> 1;
627 if (host_parms.memsize > MAXIMUM_WIN_MEMORY)
628 host_parms.memsize = MAXIMUM_WIN_MEMORY;
631 // Sys_PageIn (parms.membase, parms.memsize);
633 tevent = CreateEvent(NULL, false, false, NULL);
636 Sys_Error ("Couldn't create event");
640 if (!AllocConsole ())
642 Sys_Error ("Couldn't create dedicated server console");
645 hinput = GetStdHandle (STD_INPUT_HANDLE);
646 houtput = GetStdHandle (STD_OUTPUT_HANDLE);
648 // give QHOST a chance to hook into the console
649 if ((t = COM_CheckParm ("-HFILE")) > 0)
652 hFile = (HANDLE)atoi (com_argv[t+1]);
655 if ((t = COM_CheckParm ("-HPARENT")) > 0)
658 heventParent = (HANDLE)atoi (com_argv[t+1]);
661 if ((t = COM_CheckParm ("-HCHILD")) > 0)
664 heventChild = (HANDLE)atoi (com_argv[t+1]);
667 InitConProc (hFile, heventParent, heventChild);
670 // because sound is off until we become active
673 #if WIN32_USETIMEGETTIME
674 // make sure the timer is high precision, otherwise NT gets 18ms resolution
676 // Windows 2000 Advanced Server (and possibly other versions)
677 // apparently have a broken timer, because it runs at more like 10x speed
678 // if this isn't used, heh
682 Sys_Printf ("Host_Init\n");
685 oldtime = Sys_DoubleTime ();
687 /* main window message loop */
692 // yield the CPU for a little while when paused, minimized, or not the focus
693 if ((cl.paused && (!ActiveApp && !DDActive)) || Minimized)
695 SleepUntilInput (PAUSE_SLEEP);
696 scr_skipupdate = 1; // no point in bothering to draw
698 else if (!ActiveApp && !DDActive)
700 SleepUntilInput (NOT_FOCUS_SLEEP);
703 newtime = Sys_DoubleTime ();
704 time = newtime - oldtime;
711 /* return success of application */