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