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