]> git.xonotic.org Git - xonotic/darkplaces.git/blob - sys_sdl.c
also reject time stepping > 30 minutes (this threshold is < 1 hour to reject daylight...
[xonotic/darkplaces.git] / sys_sdl.c
1 #include "quakedef.h"
2
3 #ifdef WIN32
4 #include <io.h>
5 #include "conio.h"
6 #else
7 #include <unistd.h>
8 #include <fcntl.h>
9 #include <sys/time.h>
10 #endif
11
12 #include <signal.h>
13
14 #include <SDL.h>
15
16 // =======================================================================
17 // General routines
18 // =======================================================================
19
20 void Sys_Shutdown (void)
21 {
22 #ifndef WIN32
23         fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
24 #endif
25         fflush(stdout);
26         SDL_Quit();
27 }
28
29
30 void Sys_Error (const char *error, ...)
31 {
32         va_list argptr;
33         char string[MAX_INPUTLINE];
34
35 // change stdin to non blocking
36 #ifndef WIN32
37         fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
38 #endif
39
40         va_start (argptr,error);
41         dpvsnprintf (string, sizeof (string), error, argptr);
42         va_end (argptr);
43
44         Con_Printf ("Quake Error: %s\n", string);
45
46         Host_Shutdown ();
47         exit (1);
48 }
49
50 void Sys_PrintToTerminal(const char *text)
51 {
52 #ifndef WIN32
53         // BUG: for some reason, NDELAY also affects stdout (1) when used on stdin (0).
54         int origflags = fcntl (1, F_GETFL, 0);
55         fcntl (1, F_SETFL, origflags & ~FNDELAY);
56 #endif
57         while(*text)
58         {
59                 int written = (int)write(1, text, (int)strlen(text));
60                 if(written <= 0)
61                         break; // sorry, I cannot do anything about this error - without an output
62                 text += written;
63         }
64 #ifndef WIN32
65         fcntl (1, F_SETFL, origflags);
66 #endif
67         //fprintf(stdout, "%s", text);
68 }
69
70 double Sys_DoubleTime (void)
71 {
72         static int first = true;
73         static double oldtime = 0.0, curtime = 0.0;
74         double newtime;
75         newtime = (double) SDL_GetTicks() / 1000.0;
76
77
78         if (first)
79         {
80                 first = false;
81                 oldtime = newtime;
82         }
83
84         if (newtime < oldtime)
85         {
86                 // warn if it's significant
87                 if (newtime - oldtime < -0.01)
88                         Con_Printf("Sys_DoubleTime: time stepped backwards (went from %f to %f, difference %f)\n", oldtime, newtime, newtime - oldtime);
89         }
90         else if (newtime > oldtime + 1800)
91         {
92                 Con_Printf("Sys_DoubleTime: time stepped forward (went from %f to %f, difference %f)\n", oldtime, newtime, newtime - oldtime);
93         }
94         else
95                 curtime += newtime - oldtime;
96         oldtime = newtime;
97
98         return curtime;
99 }
100
101 char *Sys_ConsoleInput(void)
102 {
103         if (cls.state == ca_dedicated)
104         {
105                 static char text[MAX_INPUTLINE];
106                 int len = 0;
107 #ifdef WIN32
108                 int c;
109
110                 // read a line out
111                 while (_kbhit ())
112                 {
113                         c = _getch ();
114                         putch (c);
115                         if (c == '\r')
116                         {
117                                 text[len] = 0;
118                                 putch ('\n');
119                                 len = 0;
120                                 return text;
121                         }
122                         if (c == 8)
123                         {
124                                 if (len)
125                                 {
126                                         putch (' ');
127                                         putch (c);
128                                         len--;
129                                         text[len] = 0;
130                                 }
131                                 continue;
132                         }
133                         text[len] = c;
134                         len++;
135                         text[len] = 0;
136                         if (len == sizeof (text))
137                                 len = 0;
138                 }
139 #else
140                 fd_set fdset;
141                 struct timeval timeout;
142                 FD_ZERO(&fdset);
143                 FD_SET(0, &fdset); // stdin
144                 timeout.tv_sec = 0;
145                 timeout.tv_usec = 0;
146                 if (select (1, &fdset, NULL, NULL, &timeout) != -1 && FD_ISSET(0, &fdset))
147                 {
148                         len = read (0, text, sizeof(text));
149                         if (len >= 1)
150                         {
151                                 // rip off the \n and terminate
152                                 text[len-1] = 0;
153                                 return text;
154                         }
155                 }
156 #endif
157         }
158         return NULL;
159 }
160
161 void Sys_Sleep(int microseconds)
162 {
163         SDL_Delay(microseconds / 1000);
164 }
165
166 char *Sys_GetClipboardData (void)
167 {
168 #ifdef WIN32
169         char *data = NULL;
170         char *cliptext;
171
172         if (OpenClipboard (NULL) != 0)
173         {
174                 HANDLE hClipboardData;
175
176                 if ((hClipboardData = GetClipboardData (CF_TEXT)) != 0)
177                 {
178                         if ((cliptext = GlobalLock (hClipboardData)) != 0)
179                         {
180                                 size_t allocsize;
181                                 allocsize = GlobalSize (hClipboardData) + 1;
182                                 data = Z_Malloc (allocsize);
183                                 strlcpy (data, cliptext, allocsize);
184                                 GlobalUnlock (hClipboardData);
185                         }
186                 }
187                 CloseClipboard ();
188         }
189         return data;
190 #else
191         return NULL;
192 #endif
193 }
194
195 void Sys_InitConsole (void)
196 {
197 }
198
199 void Sys_Init_Commands (void)
200 {
201 }
202
203 int main (int argc, char *argv[])
204 {
205         signal(SIGFPE, SIG_IGN);
206
207         com_argc = argc;
208         com_argv = (const char **)argv;
209
210 #ifndef WIN32
211         fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);
212 #endif
213
214         // we don't know which systems we'll want to init, yet...
215         SDL_Init(0);
216
217         Host_Main();
218
219         return 0;
220 }