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 FILE *sys_handles[MAX_HANDLES];
97 for (i=1 ; i<MAX_HANDLES ; i++)
100 Sys_Error ("out of handles");
109 int filelength (FILE *f)
115 fseek (f, 0, SEEK_END);
117 fseek (f, pos, SEEK_SET);
122 int Sys_FileOpenRead (char *path, int *hndl)
129 f = fopen(path, "rb");
140 retval = filelength(f);
146 int Sys_FileOpenWrite (char *path)
153 f = fopen(path, "wb");
155 Host_Error ("Error opening %s: %s", path,strerror(errno));
161 void Sys_FileClose (int handle)
163 fclose (sys_handles[handle]);
164 sys_handles[handle] = NULL;
167 void Sys_FileSeek (int handle, int position)
169 fseek (sys_handles[handle], position, SEEK_SET);
172 int Sys_FileRead (int handle, void *dest, int count)
174 return fread (dest, 1, count, sys_handles[handle]);
177 int Sys_FileWrite (int handle, void *data, int count)
179 return fwrite (data, 1, count, sys_handles[handle]);
182 int Sys_FileTime (char *path)
186 f = fopen(path, "rb");
196 void Sys_mkdir (char *path)
203 ===============================================================================
207 ===============================================================================
210 void Sys_Error (char *error, ...)
213 char text[1024], text2[1024];
214 char *text3 = "Press Enter to exit\n";
215 char *text4 = "***********************************\n";
219 static int in_sys_error0 = 0;
220 static int in_sys_error1 = 0;
221 static int in_sys_error2 = 0;
222 static int in_sys_error3 = 0;
227 va_start (argptr, error);
228 vsprintf (text, error, argptr);
233 va_start (argptr, error);
234 vsprintf (text, error, argptr);
237 sprintf (text2, "ERROR: %s\n", text);
238 WriteFile (houtput, text5, strlen (text5), &dummy, NULL);
239 WriteFile (houtput, text4, strlen (text4), &dummy, NULL);
240 WriteFile (houtput, text2, strlen (text2), &dummy, NULL);
241 WriteFile (houtput, text3, strlen (text3), &dummy, NULL);
242 WriteFile (houtput, text4, strlen (text4), &dummy, NULL);
245 starttime = Sys_DoubleTime ();
246 sc_return_on_enter = true; // so Enter will get us out of here
248 while (!Sys_ConsoleInput () &&
249 ((Sys_DoubleTime () - starttime) < CONSOLE_ERROR_TIMEOUT))
255 // switch to windowed so the message box is visible, unless we already
256 // tried that and failed
260 VID_SetDefaultMode ();
261 MessageBox(NULL, text, "Quake Error",
262 MB_OK | MB_SETFOREGROUND | MB_ICONSTOP);
266 MessageBox(NULL, text, "Double Quake Error",
267 MB_OK | MB_SETFOREGROUND | MB_ICONSTOP);
277 // shut down QHOST hooks if necessary
287 void Sys_Printf (char *fmt, ...)
295 va_start (argptr,fmt);
296 vsprintf (text, fmt, argptr);
299 WriteFile(houtput, text, strlen (text), &dummy, NULL);
309 CloseHandle (tevent);
314 // shut down QHOST hooks if necessary
326 double Sys_DoubleTime (void)
328 // LordHavoc: note to people modifying this code, DWORD is specifically defined as an unsigned 32bit number, therefore the 65536.0 * 65536.0 is fine.
329 #if WIN32_USETIMEGETTIME
332 // Windows 95/98/ME/NT/2000
334 // reasonable accuracy (millisecond)
337 static int first = true;
338 static double oldtime = 0.0, basetime = 0.0, old = 0.0;
341 now = (double) timeGetTime () - basetime;
353 basetime -= (65536.0 * 65536.0);
354 now += (65536.0 * 65536.0);
358 newtime = now / 1000.0;
360 if (newtime < oldtime)
361 Sys_Error("Sys_DoubleTime: time running backwards??\n");
367 // QueryPerformanceCounter
369 // Windows 95/98/ME/NT/2000
371 // very accurate (CPU cycles)
373 // does not necessarily match realtime too well (tends to get faster and faster in win98)
374 static int first = true;
375 static double oldtime = 0.0, basetime = 0.0, timescale = 0.0;
377 LARGE_INTEGER PerformanceFreq;
378 LARGE_INTEGER PerformanceCount;
382 if (!QueryPerformanceFrequency (&PerformanceFreq))
383 Sys_Error ("No hardware timer available");
386 timescale = 1.0 / ((double) PerformanceFreq.u.LowPart + (double) PerformanceFreq.u.HighPart * 65536.0 * 65536.0);
388 timescale = 1.0 / ((double) PerformanceFreq.LowPart + (double) PerformanceFreq.HighPart * 65536.0 * 65536.0);
392 QueryPerformanceCounter (&PerformanceCount);
395 newtime = ((double) PerformanceCount.u.LowPart + (double) PerformanceCount.u.HighPart * 65536.0 * 65536.0) * timescale - basetime;
397 newtime = ((double) PerformanceCount.LowPart + (double) PerformanceCount.HighPart * 65536.0 * 65536.0) * timescale - basetime;
407 if (newtime < oldtime)
408 Sys_Error("Sys_DoubleTime: time running backwards??\n");
417 char *Sys_ConsoleInput (void)
419 static char text[256];
421 INPUT_RECORD recs[1024];
423 int ch, numread, numevents;
431 if (!GetNumberOfConsoleInputEvents (hinput, &numevents))
432 Sys_Error ("Error getting # of console events");
437 if (!ReadConsoleInput(hinput, recs, 1, &numread))
438 Sys_Error ("Error reading console input");
441 Sys_Error ("Couldn't read console input");
443 if (recs[0].EventType == KEY_EVENT)
445 if (!recs[0].Event.KeyEvent.bKeyDown)
447 ch = recs[0].Event.KeyEvent.uChar.AsciiChar;
452 WriteFile(houtput, "\r\n", 2, &dummy, NULL);
460 else if (sc_return_on_enter)
462 // special case to allow exiting from the error handler on Enter
471 WriteFile(houtput, "\b \b", 3, &dummy, NULL);
481 WriteFile(houtput, &ch, 1, &dummy, NULL);
483 len = (len + 1) & 0xff;
496 void Sys_Sleep (void)
502 void Sys_SendKeyEvents (void)
506 while (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
508 // we always update if there are any event, even if we're paused
511 if (!GetMessage (&msg, NULL, 0, 0))
514 TranslateMessage (&msg);
515 DispatchMessage (&msg);
521 ==============================================================================
525 ==============================================================================
534 void SleepUntilInput (int time)
537 MsgWaitForMultipleObjects(1, &tevent, false, time, QS_ALLINPUT);
546 HINSTANCE global_hInstance;
548 char *argv[MAX_NUM_ARGVS];
549 static char *empty_string = "";
551 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
553 double time, oldtime, newtime/*, timediff*/;
554 MEMORYSTATUS lpBuffer;
555 static char cwd[1024];
558 /* previous instances do not exist in Win32 */
562 global_hInstance = hInstance;
563 global_nCmdShow = nCmdShow;
565 lpBuffer.dwLength = sizeof(MEMORYSTATUS);
566 GlobalMemoryStatus (&lpBuffer);
568 if (!GetCurrentDirectory (sizeof(cwd), cwd))
569 Sys_Error ("Couldn't determine current directory");
571 if (cwd[strlen(cwd)-1] == '/')
572 cwd[strlen(cwd)-1] = 0;
574 memset(&host_parms, 0, sizeof(host_parms));
576 host_parms.basedir = cwd;
579 argv[0] = empty_string;
581 while (*lpCmdLine && (host_parms.argc < MAX_NUM_ARGVS))
583 while (*lpCmdLine && ((*lpCmdLine <= 32) || (*lpCmdLine > 126)))
588 argv[host_parms.argc] = lpCmdLine;
591 while (*lpCmdLine && ((*lpCmdLine > 32) && (*lpCmdLine <= 126)))
603 host_parms.argv = argv;
605 COM_InitArgv (host_parms.argc, host_parms.argv);
607 host_parms.argc = com_argc;
608 host_parms.argv = com_argv;
610 isDedicated = (COM_CheckParm ("-dedicated") != 0);
612 // take the greater of all the available memory or half the total memory,
613 // but at least 8 Mb and no more than 16 Mb, unless they explicitly
616 host_parms.memsize = lpBuffer.dwAvailPhys;
618 if (host_parms.memsize < MINIMUM_WIN_MEMORY)
619 host_parms.memsize = MINIMUM_WIN_MEMORY;
621 if (host_parms.memsize < (lpBuffer.dwTotalPhys >> 1))
622 host_parms.memsize = lpBuffer.dwTotalPhys >> 1;
624 if (host_parms.memsize > MAXIMUM_WIN_MEMORY)
625 host_parms.memsize = MAXIMUM_WIN_MEMORY;
628 // Sys_PageIn (parms.membase, parms.memsize);
630 tevent = CreateEvent(NULL, false, false, NULL);
633 Sys_Error ("Couldn't create event");
637 if (!AllocConsole ())
639 Sys_Error ("Couldn't create dedicated server console");
642 hinput = GetStdHandle (STD_INPUT_HANDLE);
643 houtput = GetStdHandle (STD_OUTPUT_HANDLE);
645 // give QHOST a chance to hook into the console
646 if ((t = COM_CheckParm ("-HFILE")) > 0)
649 hFile = (HANDLE)atoi (com_argv[t+1]);
652 if ((t = COM_CheckParm ("-HPARENT")) > 0)
655 heventParent = (HANDLE)atoi (com_argv[t+1]);
658 if ((t = COM_CheckParm ("-HCHILD")) > 0)
661 heventChild = (HANDLE)atoi (com_argv[t+1]);
664 InitConProc (hFile, heventParent, heventChild);
667 // because sound is off until we become active
670 #if WIN32_USETIMEGETTIME
671 // make sure the timer is high precision, otherwise NT gets 18ms resolution
673 // Windows 2000 Advanced Server (and possibly other versions)
674 // apparently have a broken timer, because it runs at more like 10x speed
675 // if this isn't used, heh
679 Sys_Printf ("Host_Init\n");
682 oldtime = Sys_DoubleTime ();
684 /* main window message loop */
689 // yield the CPU for a little while when paused, minimized, or not the focus
690 if ((cl.paused && (!ActiveApp && !DDActive)) || Minimized)
692 SleepUntilInput (PAUSE_SLEEP);
693 scr_skipupdate = 1; // no point in bothering to draw
695 else if (!ActiveApp && !DDActive)
697 SleepUntilInput (NOT_FOCUS_SLEEP);
700 newtime = Sys_DoubleTime ();
701 time = newtime - oldtime;
708 /* return success of application */