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