fix a compile error
[divverent/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 void Sys_PrintToTerminal(const char *text)
56 {
57 #ifndef WIN32
58         // BUG: for some reason, NDELAY also affects stdout (1) when used on stdin (0).
59         int origflags = fcntl (1, F_GETFL, 0);
60         fcntl (1, F_SETFL, origflags & ~FNDELAY);
61 #else
62 #define write _write
63 #endif
64         while(*text)
65         {
66                 int written = (int)write(1, text, (int)strlen(text));
67                 if(written <= 0)
68                         break; // sorry, I cannot do anything about this error - without an output
69                 text += written;
70         }
71 #ifndef WIN32
72         fcntl (1, F_SETFL, origflags);
73 #endif
74         //fprintf(stdout, "%s", text);
75 }
76
77 char *Sys_ConsoleInput(void)
78 {
79         if (cls.state == ca_dedicated)
80         {
81                 static char text[MAX_INPUTLINE];
82                 int len = 0;
83 #ifdef WIN32
84                 int c;
85
86                 // read a line out
87                 while (_kbhit ())
88                 {
89                         c = _getch ();
90                         _putch (c);
91                         if (c == '\r')
92                         {
93                                 text[len] = 0;
94                                 _putch ('\n');
95                                 len = 0;
96                                 return text;
97                         }
98                         if (c == 8)
99                         {
100                                 if (len)
101                                 {
102                                         _putch (' ');
103                                         _putch (c);
104                                         len--;
105                                         text[len] = 0;
106                                 }
107                                 continue;
108                         }
109                         text[len] = c;
110                         len++;
111                         text[len] = 0;
112                         if (len == sizeof (text))
113                                 len = 0;
114                 }
115 #else
116                 fd_set fdset;
117                 struct timeval timeout;
118                 FD_ZERO(&fdset);
119                 FD_SET(0, &fdset); // stdin
120                 timeout.tv_sec = 0;
121                 timeout.tv_usec = 0;
122                 if (select (1, &fdset, NULL, NULL, &timeout) != -1 && FD_ISSET(0, &fdset))
123                 {
124                         len = read (0, text, sizeof(text));
125                         if (len >= 1)
126                         {
127                                 // rip off the \n and terminate
128                                 text[len-1] = 0;
129                                 return text;
130                         }
131                 }
132 #endif
133         }
134         return NULL;
135 }
136
137 char *Sys_GetClipboardData (void)
138 {
139 #ifdef WIN32
140         char *data = NULL;
141         char *cliptext;
142
143         if (OpenClipboard (NULL) != 0)
144         {
145                 HANDLE hClipboardData;
146
147                 if ((hClipboardData = GetClipboardData (CF_TEXT)) != 0)
148                 {
149                         if ((cliptext = (char *)GlobalLock (hClipboardData)) != 0)
150                         {
151                                 size_t allocsize;
152                                 allocsize = GlobalSize (hClipboardData) + 1;
153                                 data = (char *)Z_Malloc (allocsize);
154                                 strlcpy (data, cliptext, allocsize);
155                                 GlobalUnlock (hClipboardData);
156                         }
157                 }
158                 CloseClipboard ();
159         }
160         return data;
161 #else
162         return NULL;
163 #endif
164 }
165
166 void Sys_InitConsole (void)
167 {
168 }
169
170 int main (int argc, char *argv[])
171 {
172         signal(SIGFPE, SIG_IGN);
173
174         com_argc = argc;
175         com_argv = (const char **)argv;
176         Sys_ProvideSelfFD();
177
178 #ifndef WIN32
179         fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);
180 #endif
181
182         // we don't know which systems we'll want to init, yet...
183         SDL_Init(0);
184
185         Host_Main();
186
187         return 0;
188 }
189
190 qboolean sys_supportsdlgetticks = true;
191 unsigned int Sys_SDL_GetTicks (void)
192 {
193         return SDL_GetTicks();
194 }
195 void Sys_SDL_Delay (unsigned int milliseconds)
196 {
197         SDL_Delay(milliseconds);
198 }