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
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)"};
33 HANDLE hinput, houtput;
37 static HANDLE heventParent;
38 static HANDLE heventChild;
42 ===============================================================================
46 ===============================================================================
49 void Sys_Error (const char *error, ...)
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;
58 va_start (argptr, error);
59 dpvsnprintf (text, sizeof (text), error, argptr);
62 Con_Printf ("Quake Error: %s\n", text);
64 // close video so the message box is visible, unless we already tried that
65 if (!in_sys_error0 && cls.state != ca_dedicated)
71 if (!in_sys_error3 && cls.state != ca_dedicated)
74 MessageBox(NULL, text, "Quake Error", MB_OK | MB_SETFOREGROUND | MB_ICONSTOP);
83 // shut down QHOST hooks if necessary
93 void Sys_Shutdown (void)
98 if (cls.state == ca_dedicated)
101 // shut down QHOST hooks if necessary
105 void Sys_PrintToTerminal(const char *text)
108 extern HANDLE houtput;
110 if ((houtput != 0) && (houtput != INVALID_HANDLE_VALUE))
111 WriteFile(houtput, text, (DWORD) strlen(text), &dummy, NULL);
119 double Sys_DoubleTime (void)
121 static int first = true;
122 static double oldtime = 0.0, curtime = 0.0;
124 // LordHavoc: note to people modifying this code, DWORD is specifically defined as an unsigned 32bit number, therefore the 65536.0 * 65536.0 is fine.
125 if (sys_usetimegettime.integer)
127 static int firsttimegettime = true;
130 // Windows 95/98/ME/NT/2000/XP
132 // reasonable accuracy (millisecond)
134 // wraps around every 47 days or so (but this is non-fatal to us, odd times are rejected, only causes a one frame stutter)
136 // make sure the timer is high precision, otherwise different versions of windows have varying accuracy
137 if (firsttimegettime)
140 firsttimegettime = false;
143 newtime = (double) timeGetTime () / 1000.0;
147 // QueryPerformanceCounter
149 // Windows 95/98/ME/NT/2000/XP
151 // very accurate (CPU cycles)
153 // does not necessarily match realtime too well (tends to get faster and faster in win98)
154 // wraps around occasionally on some platforms (depends on CPU speed and probably other unknown factors)
156 LARGE_INTEGER PerformanceFreq;
157 LARGE_INTEGER PerformanceCount;
159 if (!QueryPerformanceFrequency (&PerformanceFreq))
161 Con_Printf ("No hardware timer available\n");
162 // fall back to timeGetTime
163 Cvar_SetValueQuick(&sys_usetimegettime, true);
164 return Sys_DoubleTime();
166 QueryPerformanceCounter (&PerformanceCount);
169 timescale = 1.0 / ((double) PerformanceFreq.u.LowPart + (double) PerformanceFreq.u.HighPart * 65536.0 * 65536.0);
170 newtime = ((double) PerformanceCount.u.LowPart + (double) PerformanceCount.u.HighPart * 65536.0 * 65536.0) * timescale;
172 timescale = 1.0 / ((double) PerformanceFreq.LowPart + (double) PerformanceFreq.HighPart * 65536.0 * 65536.0);
173 newtime = ((double) PerformanceCount.LowPart + (double) PerformanceCount.HighPart * 65536.0 * 65536.0) * timescale;
183 if (newtime < oldtime)
185 // warn if it's significant
186 if (newtime - oldtime < -0.01)
187 Con_Printf("Sys_DoubleTime: time stepped backwards (went from %f to %f, difference %f)\n", oldtime, newtime, newtime - oldtime);
190 curtime += newtime - oldtime;
197 char *Sys_ConsoleInput (void)
199 static char text[MAX_INPUTLINE];
201 INPUT_RECORD recs[1024];
203 DWORD numread, numevents, dummy;
205 if (cls.state != ca_dedicated)
211 if (!GetNumberOfConsoleInputEvents (hinput, &numevents))
212 Sys_Error ("Error getting # of console events");
217 if (!ReadConsoleInput(hinput, recs, 1, &numread))
218 Sys_Error ("Error reading console input");
221 Sys_Error ("Couldn't read console input");
223 if (recs[0].EventType == KEY_EVENT)
225 if (!recs[0].Event.KeyEvent.bKeyDown)
227 ch = recs[0].Event.KeyEvent.uChar.AsciiChar;
232 WriteFile(houtput, "\r\n", 2, &dummy, NULL);
244 WriteFile(houtput, "\b \b", 3, &dummy, NULL);
254 WriteFile(houtput, &ch, 1, &dummy, NULL);
256 len = (len + 1) & 0xff;
269 void Sys_Sleep(int microseconds)
271 Sleep(microseconds / 1000);
274 char *Sys_GetClipboardData (void)
279 if (OpenClipboard (NULL) != 0)
281 HANDLE hClipboardData;
283 if ((hClipboardData = GetClipboardData (CF_TEXT)) != 0)
285 if ((cliptext = GlobalLock (hClipboardData)) != 0)
288 allocsize = GlobalSize (hClipboardData) + 1;
289 data = Z_Malloc (allocsize);
290 strlcpy (data, cliptext, allocsize);
291 GlobalUnlock (hClipboardData);
299 void Sys_InitConsole (void)
303 // initialize the windows dedicated server console if needed
304 tevent = CreateEvent(NULL, false, false, NULL);
307 Sys_Error ("Couldn't create event");
309 houtput = GetStdHandle (STD_OUTPUT_HANDLE);
310 hinput = GetStdHandle (STD_INPUT_HANDLE);
312 // LordHavoc: can't check cls.state because it hasn't been initialized yet
313 // if (cls.state == ca_dedicated)
314 if (COM_CheckParm("-dedicated"))
316 if ((houtput == 0) || (houtput == INVALID_HANDLE_VALUE))
319 houtput = GetStdHandle (STD_OUTPUT_HANDLE);
320 hinput = GetStdHandle (STD_INPUT_HANDLE);
322 if ((houtput == 0) || (houtput == INVALID_HANDLE_VALUE))
323 Sys_Error ("Couldn't create dedicated server console");
329 // give QHOST a chance to hook into the console
330 if ((t = COM_CheckParm ("-HFILE")) > 0)
333 hFile = (HANDLE)atoi (com_argv[t+1]);
336 if ((t = COM_CheckParm ("-HPARENT")) > 0)
339 heventParent = (HANDLE)atoi (com_argv[t+1]);
342 if ((t = COM_CheckParm ("-HCHILD")) > 0)
345 heventChild = (HANDLE)atoi (com_argv[t+1]);
348 InitConProc (hFile, heventParent, heventChild);
351 // because sound is off until we become active
355 void Sys_Init_Commands (void)
357 Cvar_RegisterVariable(&sys_usetimegettime);
361 ==============================================================================
365 ==============================================================================
374 HINSTANCE global_hInstance;
375 const char *argv[MAX_NUM_ARGVS];
376 char program_name[MAX_OSPATH];
378 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
380 MEMORYSTATUS lpBuffer;
382 /* previous instances do not exist in Win32 */
386 global_hInstance = hInstance;
388 lpBuffer.dwLength = sizeof(MEMORYSTATUS);
389 GlobalMemoryStatus (&lpBuffer);
391 program_name[sizeof(program_name)-1] = 0;
392 GetModuleFileNameA(NULL, program_name, sizeof(program_name) - 1);
396 argv[0] = program_name;
398 // FIXME: this tokenizer is rather redundent, call a more general one
399 while (*lpCmdLine && (com_argc < MAX_NUM_ARGVS))
401 while (*lpCmdLine && *lpCmdLine <= ' ')
407 if (*lpCmdLine == '\"')
411 argv[com_argc] = lpCmdLine;
413 while (*lpCmdLine && (*lpCmdLine != '\"'))
419 argv[com_argc] = lpCmdLine;
421 while (*lpCmdLine && *lpCmdLine > ' ')
434 /* return success of application */
438 int main (int argc, const char* argv[])
440 MEMORYSTATUS lpBuffer;
442 global_hInstance = GetModuleHandle (0);
444 lpBuffer.dwLength = sizeof(MEMORYSTATUS);
445 GlobalMemoryStatus (&lpBuffer);
447 program_name[sizeof(program_name)-1] = 0;
448 GetModuleFileNameA(NULL, program_name, sizeof(program_name) - 1);