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