]> git.xonotic.org Git - xonotic/darkplaces.git/blob - sys_sdl.c
Revert "cmd: Merge cmd_server and cmd_client into cmd_local again"
[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 //      if (cls.state == ca_dedicated)
107         {
108                 static char text[MAX_INPUTLINE];
109                 int len = 0;
110 #ifdef WIN32
111                 int c;
112
113                 // read a line out
114                 while (_kbhit ())
115                 {
116                         c = _getch ();
117                         _putch (c);
118                         if (c == '\r')
119                         {
120                                 text[len] = 0;
121                                 _putch ('\n');
122                                 len = 0;
123                                 return text;
124                         }
125                         if (c == 8)
126                         {
127                                 if (len)
128                                 {
129                                         _putch (' ');
130                                         _putch (c);
131                                         len--;
132                                         text[len] = 0;
133                                 }
134                                 continue;
135                         }
136                         text[len] = c;
137                         len++;
138                         text[len] = 0;
139                         if (len == sizeof (text))
140                                 len = 0;
141                 }
142 #else
143                 fd_set fdset;
144                 struct timeval timeout;
145                 FD_ZERO(&fdset);
146                 FD_SET(0, &fdset); // stdin
147                 timeout.tv_sec = 0;
148                 timeout.tv_usec = 0;
149                 if (select (1, &fdset, NULL, NULL, &timeout) != -1 && FD_ISSET(0, &fdset))
150                 {
151                         len = read (0, text, sizeof(text));
152                         if (len >= 1)
153                         {
154                                 // rip off the \n and terminate
155                                 text[len-1] = 0;
156                                 return text;
157                         }
158                 }
159 #endif
160         }
161         return NULL;
162 }
163
164 char *Sys_GetClipboardData (void)
165 {
166         char *data = NULL;
167         char *cliptext;
168
169         cliptext = SDL_GetClipboardText();
170         if (cliptext != NULL) {
171                 size_t allocsize;
172                 allocsize = min(MAX_INPUTLINE, strlen(cliptext) + 1);
173                 data = (char *)Z_Malloc (allocsize);
174                 strlcpy (data, cliptext, allocsize);
175                 SDL_free(cliptext);
176         }
177
178         return data;
179 }
180
181 void Sys_InitConsole (void)
182 {
183 }
184
185 int main (int argc, char *argv[])
186 {
187         signal(SIGFPE, SIG_IGN);
188
189 #ifdef __ANDROID__
190         Sys_AllowProfiling(true);
191 #endif
192
193         sys.selffd = -1;
194         sys.argc = argc;
195         sys.argv = (const char **)argv;
196
197         // Sys_Error this early in startup might screw with automated
198         // workflows or something if we show the dialog by default.
199         nocrashdialog = true;
200
201         Sys_ProvideSelfFD();
202
203         // COMMANDLINEOPTION: -nocrashdialog disables "Engine Error" crash dialog boxes
204         if(!Sys_CheckParm("-nocrashdialog"))
205                 nocrashdialog = false;
206         // COMMANDLINEOPTION: sdl: -noterminal disables console output on stdout
207         if(Sys_CheckParm("-noterminal"))
208                 sys.outfd = -1;
209         // COMMANDLINEOPTION: sdl: -stderr moves console output to stderr
210         else if(Sys_CheckParm("-stderr"))
211                 sys.outfd = 2;
212         else
213                 sys.outfd = 1;
214
215 #ifndef WIN32
216         fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | O_NONBLOCK);
217 #endif
218
219         // we don't know which systems we'll want to init, yet...
220         SDL_Init(0);
221
222         // used by everything
223         Memory_Init();
224
225         Host_Main();
226
227         Sys_Quit(0);
228         
229         return 0;
230 }
231
232 qbool sys_supportsdlgetticks = true;
233 unsigned int Sys_SDL_GetTicks (void)
234 {
235         return SDL_GetTicks();
236 }
237 void Sys_SDL_Delay (unsigned int milliseconds)
238 {
239         SDL_Delay(milliseconds);
240 }