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