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;
40 static HANDLE heventParent;
41 static HANDLE heventChild;
46 ===============================================================================
50 ===============================================================================
53 void Sys_Error (const char *error, ...)
56 char text[MAX_INPUTLINE];
57 static int in_sys_error0 = 0;
58 static int in_sys_error1 = 0;
59 static int in_sys_error2 = 0;
60 static int in_sys_error3 = 0;
62 va_start (argptr, error);
63 dpvsnprintf (text, sizeof (text), error, argptr);
66 Con_Printf ("Quake Error: %s\n", text);
68 // close video so the message box is visible, unless we already tried that
69 if (!in_sys_error0 && cls.state != ca_dedicated)
75 if (!in_sys_error3 && cls.state != ca_dedicated)
78 MessageBox(NULL, text, "Quake Error", MB_OK | MB_SETFOREGROUND | MB_ICONSTOP);
87 // shut down QHOST hooks if necessary
97 void Sys_Shutdown (void)
101 CloseHandle (tevent);
104 if (cls.state == ca_dedicated)
108 // shut down QHOST hooks if necessary
113 void Sys_PrintToTerminal(const char *text)
116 extern HANDLE houtput;
118 if ((houtput != 0) && (houtput != INVALID_HANDLE_VALUE))
119 WriteFile(houtput, text, (DWORD) strlen(text), &dummy, NULL);
127 double Sys_DoubleTime (void)
129 static int first = true;
130 static double oldtime = 0.0, curtime = 0.0;
132 // LordHavoc: note to people modifying this code, DWORD is specifically defined as an unsigned 32bit number, therefore the 65536.0 * 65536.0 is fine.
133 if (sys_usetimegettime.integer)
135 static int firsttimegettime = true;
138 // Windows 95/98/ME/NT/2000/XP
140 // reasonable accuracy (millisecond)
142 // wraps around every 47 days or so (but this is non-fatal to us, odd times are rejected, only causes a one frame stutter)
144 // make sure the timer is high precision, otherwise different versions of windows have varying accuracy
145 if (firsttimegettime)
148 firsttimegettime = false;
151 newtime = (double) timeGetTime () / 1000.0;
155 // QueryPerformanceCounter
157 // Windows 95/98/ME/NT/2000/XP
159 // very accurate (CPU cycles)
161 // does not necessarily match realtime too well (tends to get faster and faster in win98)
162 // wraps around occasionally on some platforms (depends on CPU speed and probably other unknown factors)
164 LARGE_INTEGER PerformanceFreq;
165 LARGE_INTEGER PerformanceCount;
167 if (!QueryPerformanceFrequency (&PerformanceFreq))
169 Con_Printf ("No hardware timer available\n");
170 // fall back to timeGetTime
171 Cvar_SetValueQuick(&sys_usetimegettime, true);
172 return Sys_DoubleTime();
174 QueryPerformanceCounter (&PerformanceCount);
177 timescale = 1.0 / ((double) PerformanceFreq.u.LowPart + (double) PerformanceFreq.u.HighPart * 65536.0 * 65536.0);
178 newtime = ((double) PerformanceCount.u.LowPart + (double) PerformanceCount.u.HighPart * 65536.0 * 65536.0) * timescale;
180 timescale = 1.0 / ((double) PerformanceFreq.LowPart + (double) PerformanceFreq.HighPart * 65536.0 * 65536.0);
181 newtime = ((double) PerformanceCount.LowPart + (double) PerformanceCount.HighPart * 65536.0 * 65536.0) * timescale;
191 if (newtime < oldtime)
193 // warn if it's significant
194 if (newtime - oldtime < -0.01)
195 Con_Printf("Sys_DoubleTime: time stepped backwards (went from %f to %f, difference %f)\n", oldtime, newtime, newtime - oldtime);
197 else if (newtime > oldtime + 1800)
199 Con_Printf("Sys_DoubleTime: time stepped forward (went from %f to %f, difference %f)\n", oldtime, newtime, newtime - oldtime);
202 curtime += newtime - oldtime;
209 char *Sys_ConsoleInput (void)
211 static char text[MAX_INPUTLINE];
213 INPUT_RECORD recs[1024];
215 DWORD numread, numevents, dummy;
217 if (cls.state != ca_dedicated)
222 if (!GetNumberOfConsoleInputEvents (hinput, &numevents))
224 cls.state = ca_disconnected;
225 Sys_Error ("Error getting # of console events (error code %x)", (unsigned int)GetLastError());
231 if (!ReadConsoleInput(hinput, recs, 1, &numread))
233 cls.state = ca_disconnected;
234 Sys_Error ("Error reading console input (error code %x)", (unsigned int)GetLastError());
239 cls.state = ca_disconnected;
240 Sys_Error ("Couldn't read console input (error code %x)", (unsigned int)GetLastError());
243 if (recs[0].EventType == KEY_EVENT)
245 if (!recs[0].Event.KeyEvent.bKeyDown)
247 ch = recs[0].Event.KeyEvent.uChar.AsciiChar;
252 WriteFile(houtput, "\r\n", 2, &dummy, NULL);
264 WriteFile(houtput, "\b \b", 3, &dummy, NULL);
272 if (ch >= (int) (unsigned char) ' ')
274 WriteFile(houtput, &ch, 1, &dummy, NULL);
276 len = (len + 1) & 0xff;
289 void Sys_Sleep(int microseconds)
291 Sleep(microseconds / 1000);
294 char *Sys_GetClipboardData (void)
299 if (OpenClipboard (NULL) != 0)
301 HANDLE hClipboardData;
303 if ((hClipboardData = GetClipboardData (CF_TEXT)) != 0)
305 if ((cliptext = GlobalLock (hClipboardData)) != 0)
308 allocsize = GlobalSize (hClipboardData) + 1;
309 data = Z_Malloc (allocsize);
310 strlcpy (data, cliptext, allocsize);
311 GlobalUnlock (hClipboardData);
319 void Sys_InitConsole (void)
324 // initialize the windows dedicated server console if needed
325 tevent = CreateEvent(NULL, false, false, NULL);
328 Sys_Error ("Couldn't create event");
331 houtput = GetStdHandle (STD_OUTPUT_HANDLE);
332 hinput = GetStdHandle (STD_INPUT_HANDLE);
334 // LordHavoc: can't check cls.state because it hasn't been initialized yet
335 // if (cls.state == ca_dedicated)
336 if (COM_CheckParm("-dedicated"))
338 //if ((houtput == 0) || (houtput == INVALID_HANDLE_VALUE)) // LordHavoc: on Windows XP this is never 0 or invalid, but hinput is invalid
340 if (!AllocConsole ())
341 Sys_Error ("Couldn't create dedicated server console (error code %x)", (unsigned int)GetLastError());
342 houtput = GetStdHandle (STD_OUTPUT_HANDLE);
343 hinput = GetStdHandle (STD_INPUT_HANDLE);
345 if ((houtput == 0) || (houtput == INVALID_HANDLE_VALUE))
346 Sys_Error ("Couldn't create dedicated server console");
353 // give QHOST a chance to hook into the console
354 if ((t = COM_CheckParm ("-HFILE")) > 0)
357 hFile = (HANDLE)atoi (com_argv[t+1]);
360 if ((t = COM_CheckParm ("-HPARENT")) > 0)
363 heventParent = (HANDLE)atoi (com_argv[t+1]);
366 if ((t = COM_CheckParm ("-HCHILD")) > 0)
369 heventChild = (HANDLE)atoi (com_argv[t+1]);
372 InitConProc (hFile, heventParent, heventChild);
376 // because sound is off until we become active
380 void Sys_Init_Commands (void)
382 Cvar_RegisterVariable(&sys_usetimegettime);
386 ==============================================================================
390 ==============================================================================
399 HINSTANCE global_hInstance;
400 const char *argv[MAX_NUM_ARGVS];
401 char program_name[MAX_OSPATH];
403 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
405 MEMORYSTATUS lpBuffer;
407 /* previous instances do not exist in Win32 */
411 global_hInstance = hInstance;
413 lpBuffer.dwLength = sizeof(MEMORYSTATUS);
414 GlobalMemoryStatus (&lpBuffer);
416 program_name[sizeof(program_name)-1] = 0;
417 GetModuleFileNameA(NULL, program_name, sizeof(program_name) - 1);
421 argv[0] = program_name;
423 // FIXME: this tokenizer is rather redundent, call a more general one
424 while (*lpCmdLine && (com_argc < MAX_NUM_ARGVS))
426 while (*lpCmdLine && ISWHITESPACE(*lpCmdLine))
432 if (*lpCmdLine == '\"')
436 argv[com_argc] = lpCmdLine;
438 while (*lpCmdLine && (*lpCmdLine != '\"'))
444 argv[com_argc] = lpCmdLine;
446 while (*lpCmdLine && !ISWHITESPACE(*lpCmdLine))
459 /* return success of application */
464 // unused, this file is only used when building windows client and vid_wgl provides WinMain() instead
465 int main (int argc, const char* argv[])
467 MEMORYSTATUS lpBuffer;
469 global_hInstance = GetModuleHandle (0);
471 lpBuffer.dwLength = sizeof(MEMORYSTATUS);
472 GlobalMemoryStatus (&lpBuffer);
474 program_name[sizeof(program_name)-1] = 0;
475 GetModuleFileNameA(NULL, program_name, sizeof(program_name) - 1);