]> git.xonotic.org Git - xonotic/darkplaces.git/blob - sys_sdl.c
Now with new Travis secret key.
[xonotic/darkplaces.git] / sys_sdl.c
1 #include "darkplaces.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 #ifdef __ANDROID__
13 #include <android/log.h>
14 #endif
15
16 #include <signal.h>
17
18 #include <SDL.h>
19
20 #ifdef WIN32
21 #ifdef _MSC_VER
22 #pragma comment(lib, "sdl2.lib")
23 #pragma comment(lib, "sdl2main.lib")
24 #endif
25 #endif
26
27 sys_t sys;
28
29 // =======================================================================
30 // General routines
31 // =======================================================================
32
33 void Sys_Shutdown (void)
34 {
35 #ifdef __ANDROID__
36         Sys_AllowProfiling(false);
37 #endif
38 #ifndef WIN32
39         fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~O_NONBLOCK);
40 #endif
41         fflush(stdout);
42         SDL_Quit();
43 }
44
45 static qbool nocrashdialog;
46 void Sys_Error (const char *error, ...)
47 {
48         va_list argptr;
49         char string[MAX_INPUTLINE];
50
51 // change stdin to non blocking
52 #ifndef WIN32
53         fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~O_NONBLOCK);
54 #endif
55
56         va_start (argptr,error);
57         dpvsnprintf (string, sizeof (string), error, argptr);
58         va_end (argptr);
59
60         Con_Printf(CON_ERROR "Engine Error: %s\n", string);
61         
62         if(!nocrashdialog)
63                 SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Engine Error", string, NULL);
64
65         //Host_Shutdown ();
66         exit (1);
67 }
68
69 void Sys_PrintToTerminal(const char *text)
70 {
71 #ifdef __ANDROID__
72         if (developer.integer > 0)
73         {
74                 __android_log_write(ANDROID_LOG_DEBUG, sys.argv[0], text);
75         }
76 #else
77         if(sys.outfd < 0)
78                 return;
79 #ifndef WIN32
80         // BUG: for some reason, NDELAY also affects stdout (1) when used on stdin (0).
81         // this is because both go to /dev/tty by default!
82         {
83                 int origflags = fcntl (sys.outfd, F_GETFL, 0);
84                 fcntl (sys.outfd, F_SETFL, origflags & ~O_NONBLOCK);
85 #endif
86 #ifdef WIN32
87 #define write _write
88 #endif
89                 while(*text)
90                 {
91                         fs_offset_t written = (fs_offset_t)write(sys.outfd, text, (int)strlen(text));
92                         if(written <= 0)
93                                 break; // sorry, I cannot do anything about this error - without an output
94                         text += written;
95                 }
96 #ifndef WIN32
97                 fcntl (sys.outfd, F_SETFL, origflags);
98         }
99 #endif
100         //fprintf(stdout, "%s", text);
101 #endif
102 }
103
104 char *Sys_ConsoleInput(void)
105 {
106         static char text[MAX_INPUTLINE];
107         int len = 0;
108 #ifdef WIN32
109         int c;
110
111         // read a line out
112         while (_kbhit ())
113         {
114                 c = _getch ();
115                 _putch (c);
116                 if (c == '\r')
117                 {
118                         text[len] = 0;
119                         _putch ('\n');
120                         len = 0;
121                         return text;
122                 }
123                 if (c == 8)
124                 {
125                         if (len)
126                         {
127                                 _putch (' ');
128                                 _putch (c);
129                                 len--;
130                                 text[len] = 0;
131                         }
132                         continue;
133                 }
134                 text[len] = c;
135                 len++;
136                 text[len] = 0;
137                 if (len == sizeof (text))
138                         len = 0;
139         }
140 #else
141         fd_set fdset;
142         struct timeval timeout;
143         FD_ZERO(&fdset);
144         FD_SET(0, &fdset); // stdin
145         timeout.tv_sec = 0;
146         timeout.tv_usec = 0;
147         if (select (1, &fdset, NULL, NULL, &timeout) != -1 && FD_ISSET(0, &fdset))
148         {
149                 len = read (0, text, sizeof(text));
150                 if (len >= 1)
151                 {
152                         // rip off the \n and terminate
153                         text[len-1] = 0;
154                         return text;
155                 }
156         }
157 #endif
158         return NULL;
159 }
160
161 char *Sys_GetClipboardData (void)
162 {
163         char *data = NULL;
164         char *cliptext;
165
166         cliptext = SDL_GetClipboardText();
167         if (cliptext != NULL) {
168                 size_t allocsize;
169                 allocsize = min(MAX_INPUTLINE, strlen(cliptext) + 1);
170                 data = (char *)Z_Malloc (allocsize);
171                 strlcpy (data, cliptext, allocsize);
172                 SDL_free(cliptext);
173         }
174
175         return data;
176 }
177
178 void Sys_InitConsole (void)
179 {
180 }
181
182 int main (int argc, char *argv[])
183 {
184         signal(SIGFPE, SIG_IGN);
185
186 #ifdef __ANDROID__
187         Sys_AllowProfiling(true);
188 #endif
189
190         sys.selffd = -1;
191         sys.argc = argc;
192         sys.argv = (const char **)argv;
193
194         // Sys_Error this early in startup might screw with automated
195         // workflows or something if we show the dialog by default.
196         nocrashdialog = true;
197
198         Sys_ProvideSelfFD();
199
200         // COMMANDLINEOPTION: -nocrashdialog disables "Engine Error" crash dialog boxes
201         if(!Sys_CheckParm("-nocrashdialog"))
202                 nocrashdialog = false;
203         // COMMANDLINEOPTION: sdl: -noterminal disables console output on stdout
204         if(Sys_CheckParm("-noterminal"))
205                 sys.outfd = -1;
206         // COMMANDLINEOPTION: sdl: -stderr moves console output to stderr
207         else if(Sys_CheckParm("-stderr"))
208                 sys.outfd = 2;
209         else
210                 sys.outfd = 1;
211
212 #ifndef WIN32
213         fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | O_NONBLOCK);
214 #endif
215
216         // we don't know which systems we'll want to init, yet...
217         SDL_Init(0);
218
219         // used by everything
220         Memory_Init();
221
222         Host_Main();
223
224         Sys_Quit(0);
225         
226         return 0;
227 }
228
229 qbool sys_supportsdlgetticks = true;
230 unsigned int Sys_SDL_GetTicks (void)
231 {
232         return SDL_GetTicks();
233 }
234 void Sys_SDL_Delay (unsigned int milliseconds)
235 {
236         SDL_Delay(milliseconds);
237 }