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