17 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)"};
20 cvar_t sys_useclockgettime = {CVAR_SAVE, "sys_useclockgettime", "0", "use POSIX clock_gettime function (which has issues if the system clock speed is far off, as it can't get fixed by NTP) for timing rather than gettimeofday (which has issues if the system time is stepped by ntpdate, or apparently on some Xen installations)"};
26 // =======================================================================
28 // =======================================================================
29 void Sys_Shutdown (void)
32 fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
37 void Sys_Error (const char *error, ...)
40 char string[MAX_INPUTLINE];
42 // change stdin to non blocking
44 fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
47 va_start (argptr,error);
48 dpvsnprintf (string, sizeof (string), error, argptr);
51 Con_Printf ("Quake Error: %s\n", string);
57 void Sys_PrintToTerminal(const char *text)
60 // BUG: for some reason, NDELAY also affects stdout (1) when used on stdin (0).
61 int origflags = fcntl (1, F_GETFL, 0);
62 fcntl (1, F_SETFL, origflags & ~FNDELAY);
66 int written = (int)write(1, text, (int)strlen(text));
68 break; // sorry, I cannot do anything about this error - without an output
72 fcntl (1, F_SETFL, origflags);
74 //fprintf(stdout, "%s", text);
77 double Sys_DoubleTime (void)
79 static int first = true;
80 static double oldtime = 0.0, curtime = 0.0;
84 // LordHavoc: note to people modifying this code, DWORD is specifically defined as an unsigned 32bit number, therefore the 65536.0 * 65536.0 is fine.
85 if (sys_usetimegettime.integer)
87 static int firsttimegettime = true;
90 // Windows 95/98/ME/NT/2000/XP
92 // reasonable accuracy (millisecond)
94 // wraps around every 47 days or so (but this is non-fatal to us, odd times are rejected, only causes a one frame stutter)
96 // make sure the timer is high precision, otherwise different versions of windows have varying accuracy
100 firsttimegettime = false;
103 newtime = (double) timeGetTime () / 1000.0;
107 // QueryPerformanceCounter
109 // Windows 95/98/ME/NT/2000/XP
111 // very accurate (CPU cycles)
113 // does not necessarily match realtime too well (tends to get faster and faster in win98)
114 // wraps around occasionally on some platforms (depends on CPU speed and probably other unknown factors)
116 LARGE_INTEGER PerformanceFreq;
117 LARGE_INTEGER PerformanceCount;
119 if (!QueryPerformanceFrequency (&PerformanceFreq))
121 Con_Printf ("No hardware timer available\n");
122 // fall back to timeGetTime
123 Cvar_SetValueQuick(&sys_usetimegettime, true);
124 return Sys_DoubleTime();
126 QueryPerformanceCounter (&PerformanceCount);
129 timescale = 1.0 / ((double) PerformanceFreq.u.LowPart + (double) PerformanceFreq.u.HighPart * 65536.0 * 65536.0);
130 newtime = ((double) PerformanceCount.u.LowPart + (double) PerformanceCount.u.HighPart * 65536.0 * 65536.0) * timescale;
132 timescale = 1.0 / ((double) PerformanceFreq.LowPart + (double) PerformanceFreq.HighPart * 65536.0 * 65536.0);
133 newtime = ((double) PerformanceCount.LowPart + (double) PerformanceCount.HighPart * 65536.0 * 65536.0) * timescale;
138 if (sys_useclockgettime.integer)
142 clock_gettime(CLOCK_HIGHRES, &ts);
144 clock_gettime(CLOCK_MONOTONIC, &ts);
146 newtime = (double) ts.tv_sec + ts.tv_nsec / 1000000000.0;
152 gettimeofday(&tp, NULL);
153 newtime = (double) tp.tv_sec + tp.tv_usec / 1000000.0;
163 if (newtime < oldtime)
165 // warn if it's significant
166 if (newtime - oldtime < -0.01)
167 Con_Printf("Sys_DoubleTime: time stepped backwards (went from %f to %f, difference %f)\n", oldtime, newtime, newtime - oldtime);
170 curtime += newtime - oldtime;
176 char *Sys_ConsoleInput(void)
178 if (cls.state == ca_dedicated)
180 static char text[MAX_INPUTLINE];
181 static unsigned int len = 0;
207 if (len < sizeof (text) - 1)
216 struct timeval timeout;
218 FD_SET(0, &fdset); // stdin
221 if (select (1, &fdset, NULL, NULL, &timeout) != -1 && FD_ISSET(0, &fdset))
223 len = read (0, text, sizeof(text));
226 // rip off the \n and terminate
236 void Sys_Sleep(int microseconds)
239 Sleep(microseconds / 1000);
241 usleep(microseconds);
245 char *Sys_GetClipboardData (void)
250 void Sys_InitConsole (void)
254 void Sys_Init_Commands (void)
257 Cvar_RegisterVariable(&sys_usetimegettime);
260 Cvar_RegisterVariable(&sys_useclockgettime);
265 int main (int argc, char **argv)
267 signal(SIGFPE, SIG_IGN);
270 com_argv = (const char **)argv;
273 fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);