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
33 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)"};
35 HANDLE hinput, houtput;
39 static HANDLE heventParent;
40 static HANDLE heventChild;
44 ===============================================================================
48 ===============================================================================
51 void Sys_Error (const char *error, ...)
54 char text[MAX_INPUTLINE];
55 static int in_sys_error0 = 0;
56 static int in_sys_error1 = 0;
57 static int in_sys_error2 = 0;
58 static int in_sys_error3 = 0;
60 va_start (argptr, error);
61 dpvsnprintf (text, sizeof (text), error, argptr);
64 Con_Printf ("Quake Error: %s\n", text);
66 // close video so the message box is visible, unless we already tried that
67 if (!in_sys_error0 && cls.state != ca_dedicated)
73 if (!in_sys_error3 && cls.state != ca_dedicated)
76 MessageBox(NULL, text, "Quake Error", MB_OK | MB_SETFOREGROUND | MB_ICONSTOP);
85 // shut down QHOST hooks if necessary
95 void Sys_Shutdown (void)
100 if (cls.state == ca_dedicated)
103 // shut down QHOST hooks if necessary
107 void Sys_PrintToTerminal(const char *text)
110 extern HANDLE houtput;
112 if ((houtput != 0) && (houtput != INVALID_HANDLE_VALUE))
113 WriteFile(houtput, text, (DWORD) strlen(text), &dummy, NULL);
121 double Sys_DoubleTime (void)
123 static int first = true;
124 static double oldtime = 0.0, curtime = 0.0;
126 // LordHavoc: note to people modifying this code, DWORD is specifically defined as an unsigned 32bit number, therefore the 65536.0 * 65536.0 is fine.
127 if (sys_usetimegettime.integer)
129 static int firsttimegettime = true;
132 // Windows 95/98/ME/NT/2000/XP
134 // reasonable accuracy (millisecond)
136 // wraps around every 47 days or so (but this is non-fatal to us, odd times are rejected, only causes a one frame stutter)
138 // make sure the timer is high precision, otherwise different versions of windows have varying accuracy
139 if (firsttimegettime)
142 firsttimegettime = false;
145 newtime = (double) timeGetTime () / 1000.0;
149 // QueryPerformanceCounter
151 // Windows 95/98/ME/NT/2000/XP
153 // very accurate (CPU cycles)
155 // does not necessarily match realtime too well (tends to get faster and faster in win98)
156 // wraps around occasionally on some platforms (depends on CPU speed and probably other unknown factors)
158 LARGE_INTEGER PerformanceFreq;
159 LARGE_INTEGER PerformanceCount;
161 if (!QueryPerformanceFrequency (&PerformanceFreq))
163 Con_Printf ("No hardware timer available\n");
164 // fall back to timeGetTime
165 Cvar_SetValueQuick(&sys_usetimegettime, true);
166 return Sys_DoubleTime();
168 QueryPerformanceCounter (&PerformanceCount);
171 timescale = 1.0 / ((double) PerformanceFreq.u.LowPart + (double) PerformanceFreq.u.HighPart * 65536.0 * 65536.0);
172 newtime = ((double) PerformanceCount.u.LowPart + (double) PerformanceCount.u.HighPart * 65536.0 * 65536.0) * timescale;
174 timescale = 1.0 / ((double) PerformanceFreq.LowPart + (double) PerformanceFreq.HighPart * 65536.0 * 65536.0);
175 newtime = ((double) PerformanceCount.LowPart + (double) PerformanceCount.HighPart * 65536.0 * 65536.0) * timescale;
185 if (newtime < oldtime)
187 // warn if it's significant
188 if (newtime - oldtime < -0.01)
189 Con_Printf("Sys_DoubleTime: time stepped backwards (went from %f to %f, difference %f)\n", oldtime, newtime, newtime - oldtime);
192 curtime += newtime - oldtime;
199 char *Sys_ConsoleInput (void)
201 static char text[MAX_INPUTLINE];
203 INPUT_RECORD recs[1024];
205 DWORD numread, numevents, dummy;
207 if (cls.state != ca_dedicated)
213 if (!GetNumberOfConsoleInputEvents (hinput, &numevents))
214 Sys_Error ("Error getting # of console events");
219 if (!ReadConsoleInput(hinput, recs, 1, &numread))
220 Sys_Error ("Error reading console input");
223 Sys_Error ("Couldn't read console input");
225 if (recs[0].EventType == KEY_EVENT)
227 if (!recs[0].Event.KeyEvent.bKeyDown)
229 ch = recs[0].Event.KeyEvent.uChar.AsciiChar;
234 WriteFile(houtput, "\r\n", 2, &dummy, NULL);
246 WriteFile(houtput, "\b \b", 3, &dummy, NULL);
256 WriteFile(houtput, &ch, 1, &dummy, NULL);
258 len = (len + 1) & 0xff;
271 void Sys_Sleep(int microseconds)
273 Sleep(microseconds / 1000);
276 char *Sys_GetClipboardData (void)
281 if (OpenClipboard (NULL) != 0)
283 HANDLE hClipboardData;
285 if ((hClipboardData = GetClipboardData (CF_TEXT)) != 0)
287 if ((cliptext = GlobalLock (hClipboardData)) != 0)
290 allocsize = GlobalSize (hClipboardData) + 1;
291 data = Z_Malloc (allocsize);
292 strlcpy (data, cliptext, allocsize);
293 GlobalUnlock (hClipboardData);
301 void Sys_InitConsole (void)
305 // initialize the windows dedicated server console if needed
306 tevent = CreateEvent(NULL, false, false, NULL);
309 Sys_Error ("Couldn't create event");
311 houtput = GetStdHandle (STD_OUTPUT_HANDLE);
312 hinput = GetStdHandle (STD_INPUT_HANDLE);
314 // LordHavoc: can't check cls.state because it hasn't been initialized yet
315 // if (cls.state == ca_dedicated)
316 if (COM_CheckParm("-dedicated"))
318 if ((houtput == 0) || (houtput == INVALID_HANDLE_VALUE))
321 houtput = GetStdHandle (STD_OUTPUT_HANDLE);
322 hinput = GetStdHandle (STD_INPUT_HANDLE);
324 if ((houtput == 0) || (houtput == INVALID_HANDLE_VALUE))
325 Sys_Error ("Couldn't create dedicated server console");
331 // give QHOST a chance to hook into the console
332 if ((t = COM_CheckParm ("-HFILE")) > 0)
335 hFile = (HANDLE)atoi (com_argv[t+1]);
338 if ((t = COM_CheckParm ("-HPARENT")) > 0)
341 heventParent = (HANDLE)atoi (com_argv[t+1]);
344 if ((t = COM_CheckParm ("-HCHILD")) > 0)
347 heventChild = (HANDLE)atoi (com_argv[t+1]);
350 InitConProc (hFile, heventParent, heventChild);
353 // because sound is off until we become active
357 void Sys_Init_Commands (void)
359 Cvar_RegisterVariable(&sys_usetimegettime);
363 ==============================================================================
367 ==============================================================================
376 HINSTANCE global_hInstance;
377 const char *argv[MAX_NUM_ARGVS];
378 char program_name[MAX_OSPATH];
380 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
382 MEMORYSTATUS lpBuffer;
384 /* previous instances do not exist in Win32 */
388 global_hInstance = hInstance;
390 lpBuffer.dwLength = sizeof(MEMORYSTATUS);
391 GlobalMemoryStatus (&lpBuffer);
393 program_name[sizeof(program_name)-1] = 0;
394 GetModuleFileNameA(NULL, program_name, sizeof(program_name) - 1);
398 argv[0] = program_name;
400 // FIXME: this tokenizer is rather redundent, call a more general one
401 while (*lpCmdLine && (com_argc < MAX_NUM_ARGVS))
403 while (*lpCmdLine && *lpCmdLine <= ' ')
409 if (*lpCmdLine == '\"')
413 argv[com_argc] = lpCmdLine;
415 while (*lpCmdLine && (*lpCmdLine != '\"'))
421 argv[com_argc] = lpCmdLine;
423 while (*lpCmdLine && *lpCmdLine > ' ')
436 /* return success of application */
440 int main (int argc, const char* argv[])
442 MEMORYSTATUS lpBuffer;
444 global_hInstance = GetModuleHandle (0);
446 lpBuffer.dwLength = sizeof(MEMORYSTATUS);
447 GlobalMemoryStatus (&lpBuffer);
449 program_name[sizeof(program_name)-1] = 0;
450 GetModuleFileNameA(NULL, program_name, sizeof(program_name) - 1);