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