]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - sys_sdl.c
move two #defined to quakedef.h, and always include quakedef.h first before any other...
[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
91                 curtime += newtime - oldtime;
92         oldtime = newtime;
93
94         return curtime;
95 }
96
97 char *Sys_ConsoleInput(void)
98 {
99         if (cls.state == ca_dedicated)
100         {
101                 static char text[MAX_INPUTLINE];
102                 int len = 0;
103 #ifdef WIN32
104                 int c;
105
106                 // read a line out
107                 while (_kbhit ())
108                 {
109                         c = _getch ();
110                         putch (c);
111                         if (c == '\r')
112                         {
113                                 text[len] = 0;
114                                 putch ('\n');
115                                 len = 0;
116                                 return text;
117                         }
118                         if (c == 8)
119                         {
120                                 if (len)
121                                 {
122                                         putch (' ');
123                                         putch (c);
124                                         len--;
125                                         text[len] = 0;
126                                 }
127                                 continue;
128                         }
129                         text[len] = c;
130                         len++;
131                         text[len] = 0;
132                         if (len == sizeof (text))
133                                 len = 0;
134                 }
135 #else
136                 fd_set fdset;
137                 struct timeval timeout;
138                 FD_ZERO(&fdset);
139                 FD_SET(0, &fdset); // stdin
140                 timeout.tv_sec = 0;
141                 timeout.tv_usec = 0;
142                 if (select (1, &fdset, NULL, NULL, &timeout) != -1 && FD_ISSET(0, &fdset))
143                 {
144                         len = read (0, text, sizeof(text));
145                         if (len >= 1)
146                         {
147                                 // rip off the \n and terminate
148                                 text[len-1] = 0;
149                                 return text;
150                         }
151                 }
152 #endif
153         }
154         return NULL;
155 }
156
157 void Sys_Sleep(int microseconds)
158 {
159         if (microseconds < 1000)
160                 microseconds = 1000;
161         SDL_Delay(microseconds / 1000);
162 }
163
164 char *Sys_GetClipboardData (void)
165 {
166 #ifdef WIN32
167         char *data = NULL;
168         char *cliptext;
169
170         if (OpenClipboard (NULL) != 0)
171         {
172                 HANDLE hClipboardData;
173
174                 if ((hClipboardData = GetClipboardData (CF_TEXT)) != 0)
175                 {
176                         if ((cliptext = GlobalLock (hClipboardData)) != 0)
177                         {
178                                 size_t allocsize;
179                                 allocsize = GlobalSize (hClipboardData) + 1;
180                                 data = Z_Malloc (allocsize);
181                                 strlcpy (data, cliptext, allocsize);
182                                 GlobalUnlock (hClipboardData);
183                         }
184                 }
185                 CloseClipboard ();
186         }
187         return data;
188 #else
189         return NULL;
190 #endif
191 }
192
193 void Sys_InitConsole (void)
194 {
195 }
196
197 void Sys_Init_Commands (void)
198 {
199 }
200
201 int main (int argc, char *argv[])
202 {
203         signal(SIGFPE, SIG_IGN);
204
205         com_argc = argc;
206         com_argv = (const char **)argv;
207
208 #ifndef WIN32
209         fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);
210 #endif
211
212         // we don't know which systems we'll want to init, yet...
213         SDL_Init(0);
214
215         Host_Main();
216
217         return 0;
218 }