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