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