]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - sys_linux.c
a61b79e7bc4c6daf6de7672b8d002b35363b4d54
[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 sys_t sys;
18
19 // =======================================================================
20 // General routines
21 // =======================================================================
22 void Sys_Shutdown (void)
23 {
24 #ifdef FNDELAY
25         fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
26 #endif
27         fflush(stdout);
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 #ifdef FNDELAY
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_Errorf ("Engine Error: %s\n", string);
45
46         Host_Shutdown ();
47         exit (1);
48 }
49
50 void Sys_PrintToTerminal(const char *text)
51 {
52         if(sys.outfd < 0)
53                 return;
54 #ifdef FNDELAY
55         // BUG: for some reason, NDELAY also affects stdout (1) when used on stdin (0).
56         // this is because both go to /dev/tty by default!
57         {
58                 int origflags = fcntl (sys.outfd, F_GETFL, 0);
59                 fcntl (sys.outfd, F_SETFL, origflags & ~FNDELAY);
60 #endif
61 #ifdef WIN32
62 #define write _write
63 #endif
64                 while(*text)
65                 {
66                         fs_offset_t written = (fs_offset_t)write(sys.outfd, 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 #ifdef FNDELAY
72                 fcntl (sys.outfd, F_SETFL, origflags);
73         }
74 #endif
75         //fprintf(stdout, "%s", text);
76 }
77
78 char *Sys_ConsoleInput(void)
79 {
80         //if (cls.state == ca_dedicated)
81         {
82                 static char text[MAX_INPUTLINE];
83                 static unsigned int len = 0;
84 #ifdef WIN32
85                 int c;
86
87                 // read a line out
88                 while (_kbhit ())
89                 {
90                         c = _getch ();
91                         if (c == '\r')
92                         {
93                                 text[len] = '\0';
94                                 _putch ('\n');
95                                 len = 0;
96                                 return text;
97                         }
98                         if (c == '\b')
99                         {
100                                 if (len)
101                                 {
102                                         _putch (c);
103                                         _putch (' ');
104                                         _putch (c);
105                                         len--;
106                                 }
107                                 continue;
108                         }
109                         if (len < sizeof (text) - 1)
110                         {
111                                 _putch (c);
112                                 text[len] = c;
113                                 len++;
114                         }
115                 }
116 #else
117                 fd_set fdset;
118                 struct timeval timeout;
119                 FD_ZERO(&fdset);
120                 FD_SET(0, &fdset); // stdin
121                 timeout.tv_sec = 0;
122                 timeout.tv_usec = 0;
123                 if (select (1, &fdset, NULL, NULL, &timeout) != -1 && FD_ISSET(0, &fdset))
124                 {
125                         len = read (0, text, sizeof(text) - 1);
126                         if (len >= 1)
127                         {
128                                 // rip off the \n and terminate
129                                 // div0: WHY? console code can deal with \n just fine
130                                 // this caused problems with pasting stuff into a terminal window
131                                 // so, not ripping off the \n, but STILL keeping a NUL terminator
132                                 text[len] = 0;
133                                 return text;
134                         }
135                 }
136 #endif
137         }
138         return NULL;
139 }
140
141 char *Sys_GetClipboardData (void)
142 {
143         return NULL;
144 }
145
146 void Sys_InitConsole (void)
147 {
148 }
149
150 int main (int argc, char **argv)
151 {
152         signal(SIGFPE, SIG_IGN);
153         sys.selffd = -1;
154         sys.argc = argc;
155         sys.argv = (const char **)argv;
156         Sys_ProvideSelfFD();
157
158         // COMMANDLINEOPTION: sdl: -noterminal disables console output on stdout
159         if(COM_CheckParm("-noterminal"))
160                 sys.outfd = -1;
161         // COMMANDLINEOPTION: sdl: -stderr moves console output to stderr
162         else if(COM_CheckParm("-stderr"))
163                 sys.outfd = 2;
164         else
165                 sys.outfd = 1;
166
167 #ifdef FNDELAY
168         fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);
169 #endif
170
171         Host_Main();
172
173         return 0;
174 }
175
176 qboolean sys_supportsdlgetticks = false;
177 unsigned int Sys_SDL_GetTicks (void)
178 {
179         Sys_Error("Called Sys_SDL_GetTicks on non-SDL target");
180         return 0;
181 }
182 void Sys_SDL_Delay (unsigned int milliseconds)
183 {
184         Sys_Error("Called Sys_SDL_Delay on non-SDL target");
185 }