]> icculus.org git repositories - divverent/darkplaces.git/blob - sys_linux.c
make DP run in wine again
[divverent/darkplaces.git] / sys_linux.c
1 #include "quakedef.h"
2
3 #ifdef WIN32
4 #include <windows.h>
5 #include <mmsystem.h>
6 #include <io.h>
7 #include "conio.h"
8 #else
9 #include <unistd.h>
10 #include <fcntl.h>
11 #include <sys/time.h>
12 #include <time.h>
13 #endif
14
15 #include <signal.h>
16
17
18 cvar_t sys_usenoclockbutbenchmark = {CVAR_SAVE, "sys_usenoclockbutbenchmark", "0", "don't use ANY real timing, and simulate a clock (for benchmarking); the game then runs as fast as possible. Run a QC mod with bots that does some stuff, then does a quit at the end, to benchmark a server. NEVER do this on a public server."};
19 static unsigned long benchmark_time;
20
21 #ifdef WIN32
22 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)"};
23 #else
24 # ifndef MACOSX
25 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 # endif
27 #endif
28
29
30
31 // =======================================================================
32 // General routines
33 // =======================================================================
34 void Sys_Shutdown (void)
35 {
36 #ifndef WIN32
37         fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
38 #endif
39         fflush(stdout);
40 }
41
42 void Sys_Error (const char *error, ...)
43 {
44         va_list argptr;
45         char string[MAX_INPUTLINE];
46
47 // change stdin to non blocking
48 #ifndef WIN32
49         fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
50 #endif
51
52         va_start (argptr,error);
53         dpvsnprintf (string, sizeof (string), error, argptr);
54         va_end (argptr);
55
56         Con_Printf ("Quake Error: %s\n", string);
57
58         Host_Shutdown ();
59         exit (1);
60 }
61
62 void Sys_PrintToTerminal(const char *text)
63 {
64 #ifndef WIN32
65         // BUG: for some reason, NDELAY also affects stdout (1) when used on stdin (0).
66         int origflags = fcntl (1, F_GETFL, 0);
67         fcntl (1, F_SETFL, origflags & ~FNDELAY);
68 #else
69 #define write _write
70 #endif
71         while(*text)
72         {
73                 ssize_t written = write(1, text, strlen(text));
74                 if(written <= 0)
75                         break; // sorry, I cannot do anything about this error - without an output
76                 text += written;
77         }
78 #ifndef WIN32
79         fcntl (1, F_SETFL, origflags);
80 #endif
81         //fprintf(stdout, "%s", text);
82 }
83
84 double Sys_DoubleTime (void)
85 {
86         static int first = true;
87         static double oldtime = 0.0, curtime = 0.0;
88         double newtime;
89         if(sys_usenoclockbutbenchmark.integer)
90         {
91                 benchmark_time += 1;
92                 return ((double) benchmark_time) / 1e6;
93         }
94 #ifdef WIN32
95         // LordHavoc: note to people modifying this code, DWORD is specifically defined as an unsigned 32bit number, therefore the 65536.0 * 65536.0 is fine.
96         if (sys_usetimegettime.integer)
97         {
98                 static int firsttimegettime = true;
99                 // timeGetTime
100                 // platform:
101                 // Windows 95/98/ME/NT/2000/XP
102                 // features:
103                 // reasonable accuracy (millisecond)
104                 // issues:
105                 // wraps around every 47 days or so (but this is non-fatal to us, odd times are rejected, only causes a one frame stutter)
106
107                 // make sure the timer is high precision, otherwise different versions of windows have varying accuracy
108                 if (firsttimegettime)
109                 {
110                         timeBeginPeriod (1);
111                         firsttimegettime = false;
112                 }
113
114                 newtime = (double) timeGetTime () / 1000.0;
115         }
116         else
117         {
118                 // QueryPerformanceCounter
119                 // platform:
120                 // Windows 95/98/ME/NT/2000/XP
121                 // features:
122                 // very accurate (CPU cycles)
123                 // known issues:
124                 // does not necessarily match realtime too well (tends to get faster and faster in win98)
125                 // wraps around occasionally on some platforms (depends on CPU speed and probably other unknown factors)
126                 double timescale;
127                 LARGE_INTEGER PerformanceFreq;
128                 LARGE_INTEGER PerformanceCount;
129
130                 if (!QueryPerformanceFrequency (&PerformanceFreq))
131                 {
132                         Con_Printf ("No hardware timer available\n");
133                         // fall back to timeGetTime
134                         Cvar_SetValueQuick(&sys_usetimegettime, true);
135                         return Sys_DoubleTime();
136                 }
137                 QueryPerformanceCounter (&PerformanceCount);
138
139                 #ifdef __BORLANDC__
140                 timescale = 1.0 / ((double) PerformanceFreq.u.LowPart + (double) PerformanceFreq.u.HighPart * 65536.0 * 65536.0);
141                 newtime = ((double) PerformanceCount.u.LowPart + (double) PerformanceCount.u.HighPart * 65536.0 * 65536.0) * timescale;
142                 #else
143                 timescale = 1.0 / ((double) PerformanceFreq.LowPart + (double) PerformanceFreq.HighPart * 65536.0 * 65536.0);
144                 newtime = ((double) PerformanceCount.LowPart + (double) PerformanceCount.HighPart * 65536.0 * 65536.0) * timescale;
145                 #endif
146         }
147 #else
148 # ifndef MACOSX
149         if (sys_useclockgettime.integer)
150         {
151                 struct timespec ts;
152 #  ifdef SUNOS
153                 clock_gettime(CLOCK_HIGHRES, &ts);
154 #  else
155                 clock_gettime(CLOCK_MONOTONIC, &ts);
156 #  endif
157                 newtime = (double) ts.tv_sec + ts.tv_nsec / 1000000000.0;
158         }
159         else
160 # endif
161         {
162                 struct timeval tp;
163                 gettimeofday(&tp, NULL);
164                 newtime = (double) tp.tv_sec + tp.tv_usec / 1000000.0;
165         }
166 #endif
167
168         if (first)
169         {
170                 first = false;
171                 oldtime = newtime;
172         }
173
174         if (newtime < oldtime)
175         {
176                 // warn if it's significant
177                 if (newtime - oldtime < -0.01)
178                         Con_Printf("Sys_DoubleTime: time stepped backwards (went from %f to %f, difference %f)\n", oldtime, newtime, newtime - oldtime);
179         }
180         else if (newtime > oldtime + 1800)
181         {
182                 Con_Printf("Sys_DoubleTime: time stepped forward (went from %f to %f, difference %f)\n", oldtime, newtime, newtime - oldtime);
183         }
184         else
185                 curtime += newtime - oldtime;
186         oldtime = newtime;
187
188         return curtime;
189 }
190
191 char *Sys_ConsoleInput(void)
192 {
193         //if (cls.state == ca_dedicated)
194         {
195                 static char text[MAX_INPUTLINE];
196                 static unsigned int len = 0;
197 #ifdef WIN32
198                 int c;
199
200                 // read a line out
201                 while (_kbhit ())
202                 {
203                         c = _getch ();
204                         if (c == '\r')
205                         {
206                                 text[len] = '\0';
207                                 _putch ('\n');
208                                 len = 0;
209                                 return text;
210                         }
211                         if (c == '\b')
212                         {
213                                 if (len)
214                                 {
215                                         _putch (c);
216                                         _putch (' ');
217                                         _putch (c);
218                                         len--;
219                                 }
220                                 continue;
221                         }
222                         if (len < sizeof (text) - 1)
223                         {
224                                 _putch (c);
225                                 text[len] = c;
226                                 len++;
227                         }
228                 }
229 #else
230                 fd_set fdset;
231                 struct timeval timeout;
232                 FD_ZERO(&fdset);
233                 FD_SET(0, &fdset); // stdin
234                 timeout.tv_sec = 0;
235                 timeout.tv_usec = 0;
236                 if (select (1, &fdset, NULL, NULL, &timeout) != -1 && FD_ISSET(0, &fdset))
237                 {
238                         len = read (0, text, sizeof(text));
239                         if (len >= 1)
240                         {
241                                 // rip off the \n and terminate
242                                 text[len-1] = 0;
243                                 return text;
244                         }
245                 }
246 #endif
247         }
248         return NULL;
249 }
250
251 void Sys_Sleep(int microseconds)
252 {
253         if(sys_usenoclockbutbenchmark.integer)
254         {
255                 benchmark_time += microseconds;
256                 return;
257         }
258 #ifdef WIN32
259         Sleep(microseconds / 1000);
260 #else
261         usleep(microseconds);
262 #endif
263 }
264
265 char *Sys_GetClipboardData (void)
266 {
267         return NULL;
268 }
269
270 void Sys_InitConsole (void)
271 {
272 }
273
274 void Sys_Init_Commands (void)
275 {
276         Cvar_RegisterVariable(&sys_usenoclockbutbenchmark);
277 #ifdef WIN32
278         Cvar_RegisterVariable(&sys_usetimegettime);
279 #else
280 # ifndef MACOSX
281         Cvar_RegisterVariable(&sys_useclockgettime);
282 # endif
283 #endif
284 }
285
286 int main (int argc, char **argv)
287 {
288         signal(SIGFPE, SIG_IGN);
289
290         com_argc = argc;
291         com_argv = (const char **)argv;
292
293 #ifndef WIN32
294         fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);
295 #endif
296
297         Host_Main();
298
299         return 0;
300 }