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