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