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
30 extern void S_BlockSound (void);
32 cvar_t sys_usetimegettime = {CVAR_SAVE, "sys_usetimegettime", "1"};
34 // # of seconds to wait on Sys_Error running dedicated before exiting
35 #define CONSOLE_ERROR_TIMEOUT 60.0
36 // sleep time on pause or minimization
37 #define PAUSE_SLEEP 50
38 // sleep time when not focus
39 #define NOT_FOCUS_SLEEP 20
41 static qboolean sc_return_on_enter = false;
42 HANDLE hinput, houtput;
46 static HANDLE heventParent;
47 static HANDLE heventChild;
51 ===============================================================================
55 ===============================================================================
58 void SleepUntilInput (int time);
60 void Sys_Error (const char *error, ...)
64 static int in_sys_error0 = 0;
65 static int in_sys_error1 = 0;
66 static int in_sys_error2 = 0;
68 va_start (argptr, error);
69 vsnprintf (text, sizeof (text), error, argptr);
72 // close video so the message box is visible, unless we already tried that
73 if (!in_sys_error0 && cls.state != ca_dedicated)
78 MessageBox(NULL, text, "Quake Error", MB_OK | MB_SETFOREGROUND | MB_ICONSTOP);
80 Con_Print ("Quake Error: ");
90 // shut down QHOST hooks if necessary
100 void Sys_Shutdown (void)
103 CloseHandle (tevent);
105 if (cls.state == ca_dedicated)
108 // shut down QHOST hooks if necessary
112 void Sys_PrintToTerminal(const char *text)
115 extern HANDLE houtput;
116 if (cls.state == ca_dedicated)
117 WriteFile(houtput, text, strlen (text), &dummy, NULL);
125 double Sys_DoubleTime (void)
127 static int first = true;
128 static double oldtime = 0.0, curtime = 0.0;
130 // LordHavoc: note to people modifying this code, DWORD is specifically defined as an unsigned 32bit number, therefore the 65536.0 * 65536.0 is fine.
131 if (sys_usetimegettime.integer)
133 static int firsttimegettime = true;
136 // Windows 95/98/ME/NT/2000/XP
138 // reasonable accuracy (millisecond)
140 // wraps around every 47 days or so (but this is non-fatal to us, odd times are rejected, only causes a one frame stutter)
142 // make sure the timer is high precision, otherwise different versions of windows have varying accuracy
143 if (firsttimegettime)
146 firsttimegettime = false;
149 newtime = (double) timeGetTime () / 1000.0;
153 // QueryPerformanceCounter
155 // Windows 95/98/ME/NT/2000/XP
157 // very accurate (CPU cycles)
159 // does not necessarily match realtime too well (tends to get faster and faster in win98)
160 // wraps around occasionally on some platforms (depends on CPU speed and probably other unknown factors)
162 LARGE_INTEGER PerformanceFreq;
163 LARGE_INTEGER PerformanceCount;
165 if (!QueryPerformanceFrequency (&PerformanceFreq))
166 Sys_Error ("No hardware timer available");
167 QueryPerformanceCounter (&PerformanceCount);
170 timescale = 1.0 / ((double) PerformanceFreq.u.LowPart + (double) PerformanceFreq.u.HighPart * 65536.0 * 65536.0);
171 newtime = ((double) PerformanceCount.u.LowPart + (double) PerformanceCount.u.HighPart * 65536.0 * 65536.0) * timescale;
173 timescale = 1.0 / ((double) PerformanceFreq.LowPart + (double) PerformanceFreq.HighPart * 65536.0 * 65536.0);
174 newtime = ((double) PerformanceCount.LowPart + (double) PerformanceCount.HighPart * 65536.0 * 65536.0) * timescale;
184 if (newtime < oldtime)
186 // warn if it's significant
187 if (newtime - oldtime < -0.01)
188 Con_Printf("Sys_DoubleTime: time stepped backwards (went from %f to %f, difference %f)\n", oldtime, newtime, newtime - oldtime);
191 curtime += newtime - oldtime;
198 char *Sys_ConsoleInput (void)
200 static char text[256];
202 INPUT_RECORD recs[1024];
204 DWORD numread, numevents, dummy;
206 if (cls.state != ca_dedicated)
212 if (!GetNumberOfConsoleInputEvents (hinput, &numevents))
213 Sys_Error ("Error getting # of console events");
218 if (!ReadConsoleInput(hinput, recs, 1, &numread))
219 Sys_Error ("Error reading console input");
222 Sys_Error ("Couldn't read console input");
224 if (recs[0].EventType == KEY_EVENT)
226 if (!recs[0].Event.KeyEvent.bKeyDown)
228 ch = recs[0].Event.KeyEvent.uChar.AsciiChar;
233 WriteFile(houtput, "\r\n", 2, &dummy, NULL);
241 else if (sc_return_on_enter)
243 // special case to allow exiting from the error handler on Enter
252 WriteFile(houtput, "\b \b", 3, &dummy, NULL);
262 WriteFile(houtput, &ch, 1, &dummy, NULL);
264 len = (len + 1) & 0xff;
277 void Sys_Sleep(int milliseconds)
279 if (milliseconds < 1)
284 char *Sys_GetClipboardData (void)
289 if (OpenClipboard (NULL) != 0)
291 HANDLE hClipboardData;
293 if ((hClipboardData = GetClipboardData (CF_TEXT)) != 0)
295 if ((cliptext = GlobalLock (hClipboardData)) != 0)
297 data = malloc (GlobalSize(hClipboardData)+1);
298 strcpy (data, cliptext);
299 GlobalUnlock (hClipboardData);
308 ==============================================================================
312 ==============================================================================
316 void SleepUntilInput (int time)
318 MsgWaitForMultipleObjects(1, &tevent, false, time, QS_ALLINPUT);
327 HINSTANCE global_hInstance;
328 const char *argv[MAX_NUM_ARGVS];
329 char program_name[MAX_OSPATH];
331 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
333 double frameoldtime, framenewtime;
334 MEMORYSTATUS lpBuffer;
337 /* previous instances do not exist in Win32 */
341 global_hInstance = hInstance;
343 lpBuffer.dwLength = sizeof(MEMORYSTATUS);
344 GlobalMemoryStatus (&lpBuffer);
347 program_name[sizeof(program_name)-1] = 0;
348 GetModuleFileNameA(NULL, program_name, sizeof(program_name) - 1);
349 argv[0] = program_name;
351 // FIXME: this tokenizer is rather redundent, call a more general one
352 while (*lpCmdLine && (com_argc < MAX_NUM_ARGVS))
354 while (*lpCmdLine && *lpCmdLine <= 32)
359 if (*lpCmdLine == '\"')
362 argv[com_argc] = lpCmdLine;
365 while (*lpCmdLine && (*lpCmdLine != '\"'))
374 if (*lpCmdLine == '\"')
380 argv[com_argc] = lpCmdLine;
383 while (*lpCmdLine && *lpCmdLine > 32)
396 Sys_Shared_EarlyInit();
398 Cvar_RegisterVariable(&sys_usetimegettime);
400 tevent = CreateEvent(NULL, false, false, NULL);
403 Sys_Error ("Couldn't create event");
405 // LordHavoc: can't check cls.state because it hasn't been initialized yet
406 // if (cls.state == ca_dedicated)
407 if (COM_CheckParm("-dedicated"))
409 if (!AllocConsole ())
410 Sys_Error ("Couldn't create dedicated server console");
412 hinput = GetStdHandle (STD_INPUT_HANDLE);
413 houtput = GetStdHandle (STD_OUTPUT_HANDLE);
415 // give QHOST a chance to hook into the console
416 if ((t = COM_CheckParm ("-HFILE")) > 0)
419 hFile = (HANDLE)atoi (com_argv[t+1]);
422 if ((t = COM_CheckParm ("-HPARENT")) > 0)
425 heventParent = (HANDLE)atoi (com_argv[t+1]);
428 if ((t = COM_CheckParm ("-HCHILD")) > 0)
431 heventChild = (HANDLE)atoi (com_argv[t+1]);
434 InitConProc (hFile, heventParent, heventChild);
437 // because sound is off until we become active
442 Sys_Shared_LateInit();
444 frameoldtime = Sys_DoubleTime ();
446 /* main window message loop */
449 if (cls.state != ca_dedicated)
451 // yield the CPU for a little while when paused, minimized, or not the focus
452 if ((cl.paused && !vid_activewindow) || vid_hidden)
454 SleepUntilInput (PAUSE_SLEEP);
455 scr_skipupdate = 1; // no point in bothering to draw
457 else if (!vid_activewindow)
458 SleepUntilInput (NOT_FOCUS_SLEEP);
461 framenewtime = Sys_DoubleTime ();
462 Host_Frame (framenewtime - frameoldtime);
463 frameoldtime = framenewtime;
466 /* return success of application */