]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - sys_linux.c
div0 fixed the bug that caused huge console prints to the terminal to be truncated...
[xonotic/darkplaces.git] / sys_linux.c
1
2 #ifdef WIN32
3 #include "conio.h"
4 #else
5 #include <unistd.h>
6 #include <fcntl.h>
7 #include <sys/time.h>
8 #endif
9
10 #include <signal.h>
11
12 #include "quakedef.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                 ssize_t written = write(1, text, 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         // LordHavoc: note to people modifying this code, DWORD is specifically defined as an unsigned 32bit number, therefore the 65536.0 * 65536.0 is fine.
79         if (sys_usetimegettime.integer)
80         {
81                 static int firsttimegettime = true;
82                 // timeGetTime
83                 // platform:
84                 // Windows 95/98/ME/NT/2000/XP
85                 // features:
86                 // reasonable accuracy (millisecond)
87                 // issues:
88                 // wraps around every 47 days or so (but this is non-fatal to us, odd times are rejected, only causes a one frame stutter)
89
90                 // make sure the timer is high precision, otherwise different versions of windows have varying accuracy
91                 if (firsttimegettime)
92                 {
93                         timeBeginPeriod (1);
94                         firsttimegettime = false;
95                 }
96
97                 newtime = (double) timeGetTime () / 1000.0;
98         }
99         else
100         {
101                 // QueryPerformanceCounter
102                 // platform:
103                 // Windows 95/98/ME/NT/2000/XP
104                 // features:
105                 // very accurate (CPU cycles)
106                 // known issues:
107                 // does not necessarily match realtime too well (tends to get faster and faster in win98)
108                 // wraps around occasionally on some platforms (depends on CPU speed and probably other unknown factors)
109                 double timescale;
110                 LARGE_INTEGER PerformanceFreq;
111                 LARGE_INTEGER PerformanceCount;
112
113                 if (!QueryPerformanceFrequency (&PerformanceFreq))
114                 {
115                         Con_Printf ("No hardware timer available\n");
116                         // fall back to timeGetTime
117                         Cvar_SetValueQuick(&sys_usetimegettime, true);
118                         return Sys_DoubleTime();
119                 }
120                 QueryPerformanceCounter (&PerformanceCount);
121
122                 #ifdef __BORLANDC__
123                 timescale = 1.0 / ((double) PerformanceFreq.u.LowPart + (double) PerformanceFreq.u.HighPart * 65536.0 * 65536.0);
124                 newtime = ((double) PerformanceCount.u.LowPart + (double) PerformanceCount.u.HighPart * 65536.0 * 65536.0) * timescale;
125                 #else
126                 timescale = 1.0 / ((double) PerformanceFreq.LowPart + (double) PerformanceFreq.HighPart * 65536.0 * 65536.0);
127                 newtime = ((double) PerformanceCount.LowPart + (double) PerformanceCount.HighPart * 65536.0 * 65536.0) * timescale;
128                 #endif
129         }
130 #else
131         struct timeval tp;
132         gettimeofday(&tp, NULL);
133         newtime = (double) tp.tv_sec + tp.tv_usec / 1000000.0;
134 #endif
135
136         if (first)
137         {
138                 first = false;
139                 oldtime = newtime;
140         }
141
142         if (newtime < oldtime)
143         {
144                 // warn if it's significant
145                 if (newtime - oldtime < -0.01)
146                         Con_Printf("Sys_DoubleTime: time stepped backwards (went from %f to %f, difference %f)\n", oldtime, newtime, newtime - oldtime);
147         }
148         else
149                 curtime += newtime - oldtime;
150         oldtime = newtime;
151
152         return curtime;
153 }
154
155 char *Sys_ConsoleInput(void)
156 {
157         if (cls.state == ca_dedicated)
158         {
159                 static char text[MAX_INPUTLINE];
160                 static unsigned int len = 0;
161 #ifdef WIN32
162                 int c;
163
164                 // read a line out
165                 while (_kbhit ())
166                 {
167                         c = _getch ();
168                         if (c == '\r')
169                         {
170                                 text[len] = '\0';
171                                 putch ('\n');
172                                 len = 0;
173                                 return text;
174                         }
175                         if (c == '\b')
176                         {
177                                 if (len)
178                                 {
179                                         putch (c);
180                                         putch (' ');
181                                         putch (c);
182                                         len--;
183                                 }
184                                 continue;
185                         }
186                         if (len < sizeof (text) - 1)
187                         {
188                                 putch (c);
189                                 text[len] = c;
190                                 len++;
191                         }
192                 }
193 #else
194                 fd_set fdset;
195                 struct timeval timeout;
196                 FD_ZERO(&fdset);
197                 FD_SET(0, &fdset); // stdin
198                 timeout.tv_sec = 0;
199                 timeout.tv_usec = 0;
200                 if (select (1, &fdset, NULL, NULL, &timeout) != -1 && FD_ISSET(0, &fdset))
201                 {
202                         len = read (0, text, sizeof(text));
203                         if (len >= 1)
204                         {
205                                 // rip off the \n and terminate
206                                 text[len-1] = 0;
207                                 return text;
208                         }
209                 }
210 #endif
211         }
212         return NULL;
213 }
214
215 void Sys_Sleep(int milliseconds)
216 {
217         if (milliseconds < 1)
218                 milliseconds = 1;
219 #ifdef WIN32
220         Sleep(milliseconds);
221 #else
222         usleep(milliseconds * 1000);
223 #endif
224 }
225
226 char *Sys_GetClipboardData (void)
227 {
228         return NULL;
229 }
230
231 void Sys_InitConsole (void)
232 {
233 }
234
235 void Sys_Init_Commands (void)
236 {
237 }
238
239 int main (int argc, char **argv)
240 {
241         double frameoldtime, framenewtime;
242
243         signal(SIGFPE, SIG_IGN);
244
245         com_argc = argc;
246         com_argv = (const char **)argv;
247
248 #ifndef WIN32
249         fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);
250 #endif
251
252         Host_Init();
253
254         frameoldtime = Sys_DoubleTime () - 0.1;
255         while (1)
256         {
257                 // find time spent rendering last frame
258                 framenewtime = Sys_DoubleTime ();
259
260                 Host_Frame (framenewtime - frameoldtime);
261
262                 frameoldtime = framenewtime;
263         }
264         return 0;
265 }