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