]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - sys_sdl.c
Fixed a large number of WIN64 type conversion warnings.
[xonotic/darkplaces.git] / sys_sdl.c
1
2 #ifdef WIN32
3 #ifdef _MSC_VER
4 #pragma comment(lib, "sdl.lib")
5 #pragma comment(lib, "sdlmain.lib")
6 #endif
7 #include <io.h>
8 #include "conio.h"
9 #else
10 #include <unistd.h>
11 #include <fcntl.h>
12 #include <sys/time.h>
13 #endif
14
15 #ifdef __ANDROID__
16 #include <android/log.h>
17
18 #ifndef FNDELAY
19 #define FNDELAY         O_NDELAY
20 #endif
21 #endif
22
23 #include <signal.h>
24
25 #include <SDL.h>
26
27 #include "quakedef.h"
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) & ~FNDELAY);
40 #endif
41         fflush(stdout);
42         SDL_Quit();
43 }
44
45
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) & ~FNDELAY);
54 #endif
55
56         va_start (argptr,error);
57         dpvsnprintf (string, sizeof (string), error, argptr);
58         va_end (argptr);
59
60         Con_Printf ("Quake Error: %s\n", string);
61
62         Host_Shutdown ();
63         exit (1);
64 }
65
66 static int outfd = 1;
67 void Sys_PrintToTerminal(const char *text)
68 {
69 #ifdef __ANDROID__
70         if (developer.integer > 0)
71         {
72                 __android_log_write(ANDROID_LOG_DEBUG, com_argv[0], text);
73         }
74 #else
75         if(outfd < 0)
76                 return;
77 #ifdef FNDELAY
78         // BUG: for some reason, NDELAY also affects stdout (1) when used on stdin (0).
79         // this is because both go to /dev/tty by default!
80         {
81                 int origflags = fcntl (outfd, F_GETFL, 0);
82                 fcntl (outfd, F_SETFL, origflags & ~FNDELAY);
83 #endif
84 #ifdef WIN32
85 #define write _write
86 #endif
87                 while(*text)
88                 {
89                         fs_offset_t written = (fs_offset_t)write(outfd, text, (int)strlen(text));
90                         if(written <= 0)
91                                 break; // sorry, I cannot do anything about this error - without an output
92                         text += written;
93                 }
94 #ifdef FNDELAY
95                 fcntl (outfd, F_SETFL, origflags);
96         }
97 #endif
98         //fprintf(stdout, "%s", text);
99 #endif
100 }
101
102 char *Sys_ConsoleInput(void)
103 {
104 //      if (cls.state == ca_dedicated)
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         }
159         return NULL;
160 }
161
162 char *Sys_GetClipboardData (void)
163 {
164 #ifdef WIN32
165         char *data = NULL;
166         char *cliptext;
167
168         if (OpenClipboard (NULL) != 0)
169         {
170                 HANDLE hClipboardData;
171
172                 if ((hClipboardData = GetClipboardData (CF_TEXT)) != 0)
173                 {
174                         if ((cliptext = (char *)GlobalLock (hClipboardData)) != 0)
175                         {
176                                 size_t allocsize;
177                                 allocsize = GlobalSize (hClipboardData) + 1;
178                                 data = (char *)Z_Malloc (allocsize);
179                                 strlcpy (data, cliptext, allocsize);
180                                 GlobalUnlock (hClipboardData);
181                         }
182                 }
183                 CloseClipboard ();
184         }
185         return data;
186 #else
187         return NULL;
188 #endif
189 }
190
191 void Sys_InitConsole (void)
192 {
193 }
194
195 int main (int argc, char *argv[])
196 {
197         signal(SIGFPE, SIG_IGN);
198
199 #ifdef __ANDROID__
200         Sys_AllowProfiling(true);
201 #endif
202
203         com_argc = argc;
204         com_argv = (const char **)argv;
205         Sys_ProvideSelfFD();
206
207         // COMMANDLINEOPTION: sdl: -noterminal disables console output on stdout
208         if(COM_CheckParm("-noterminal"))
209                 outfd = -1;
210         // COMMANDLINEOPTION: sdl: -stderr moves console output to stderr
211         else if(COM_CheckParm("-stderr"))
212                 outfd = 2;
213         else
214                 outfd = 1;
215
216 #ifndef WIN32
217         fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);
218 #endif
219
220         // we don't know which systems we'll want to init, yet...
221         SDL_Init(0);
222
223         Host_Main();
224
225         return 0;
226 }
227
228 qboolean sys_supportsdlgetticks = true;
229 unsigned int Sys_SDL_GetTicks (void)
230 {
231         return SDL_GetTicks();
232 }
233 void Sys_SDL_Delay (unsigned int milliseconds)
234 {
235         SDL_Delay(milliseconds);
236 }