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