Fix engine not starting on Windows if linked against SDL > 2.0.5
[xonotic/darkplaces.git] / sys_linux.c
1
2 #ifdef WIN32
3 #include <windows.h>
4 #include <mmsystem.h>
5 #include <io.h>
6 #include "conio.h"
7 #else
8 #include <unistd.h>
9 #include <fcntl.h>
10 #include <time.h>
11 #endif
12
13 #include <signal.h>
14
15 #include "quakedef.h"
16
17 // =======================================================================
18 // General routines
19 // =======================================================================
20 void Sys_Shutdown (void)
21 {
22 #ifdef FNDELAY
23         fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
24 #endif
25         fflush(stdout);
26 }
27
28 void Sys_Error (const char *error, ...)
29 {
30         va_list argptr;
31         char string[MAX_INPUTLINE];
32
33 // change stdin to non blocking
34 #ifdef FNDELAY
35         fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
36 #endif
37
38         va_start (argptr,error);
39         dpvsnprintf (string, sizeof (string), error, argptr);
40         va_end (argptr);
41
42         Con_Printf ("Quake Error: %s\n", string);
43
44         Host_Shutdown ();
45         exit (1);
46 }
47
48 static int outfd = 1;
49 void Sys_PrintToTerminal(const char *text)
50 {
51         if(outfd < 0)
52                 return;
53 #ifdef FNDELAY
54         // BUG: for some reason, NDELAY also affects stdout (1) when used on stdin (0).
55         // this is because both go to /dev/tty by default!
56         {
57                 int origflags = fcntl (outfd, F_GETFL, 0);
58                 fcntl (outfd, F_SETFL, origflags & ~FNDELAY);
59 #endif
60 #ifdef WIN32
61 #define write _write
62 #endif
63                 while(*text)
64                 {
65                         fs_offset_t written = (fs_offset_t)write(outfd, 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 #ifdef FNDELAY
71                 fcntl (outfd, F_SETFL, origflags);
72         }
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                 static unsigned int len = 0;
83 #ifdef WIN32
84                 int c;
85
86                 // read a line out
87                 while (_kbhit ())
88                 {
89                         c = _getch ();
90                         if (c == '\r')
91                         {
92                                 text[len] = '\0';
93                                 _putch ('\n');
94                                 len = 0;
95                                 return text;
96                         }
97                         if (c == '\b')
98                         {
99                                 if (len)
100                                 {
101                                         _putch (c);
102                                         _putch (' ');
103                                         _putch (c);
104                                         len--;
105                                 }
106                                 continue;
107                         }
108                         if (len < sizeof (text) - 1)
109                         {
110                                 _putch (c);
111                                 text[len] = c;
112                                 len++;
113                         }
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) - 1);
125                         if (len >= 1)
126                         {
127                                 // rip off the \n and terminate
128                                 // div0: WHY? console code can deal with \n just fine
129                                 // this caused problems with pasting stuff into a terminal window
130                                 // so, not ripping off the \n, but STILL keeping a NUL terminator
131                                 text[len] = 0;
132                                 return text;
133                         }
134                 }
135 #endif
136         }
137         return NULL;
138 }
139
140 char *Sys_GetClipboardData (void)
141 {
142         return NULL;
143 }
144
145 void Sys_InitConsole (void)
146 {
147 }
148
149 int main (int argc, char **argv)
150 {
151         signal(SIGFPE, SIG_IGN);
152
153         com_argc = argc;
154         com_argv = (const char **)argv;
155         Sys_ProvideSelfFD();
156
157         // COMMANDLINEOPTION: sdl: -noterminal disables console output on stdout
158         if(COM_CheckParm("-noterminal"))
159                 outfd = -1;
160         // COMMANDLINEOPTION: sdl: -stderr moves console output to stderr
161         else if(COM_CheckParm("-stderr"))
162                 outfd = 2;
163         else
164                 outfd = 1;
165
166 #ifdef FNDELAY
167         fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);
168 #endif
169
170         Host_Main();
171
172         return 0;
173 }
174
175 qboolean sys_supportsdlgetticks = false;
176 unsigned int Sys_SDL_GetTicks (void)
177 {
178         Sys_Error("Called Sys_SDL_GetTicks on non-SDL target");
179         return 0;
180 }
181 void Sys_SDL_Delay (unsigned int milliseconds)
182 {
183         Sys_Error("Called Sys_SDL_Delay on non-SDL target");
184 }