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