]> git.xonotic.org Git - xonotic/darkplaces.git/blob - sys_linux.c
fix bad unicode path handling on Windows (fix issue #63)
[xonotic/darkplaces.git] / sys_linux.c
1
2 #ifdef WIN32
3 #include <windows.h>
4 #include <mmsystem.h>
5 #include <io.h>
6 #include <locale.h>
7 #include "conio.h"
8 #else
9 #include <unistd.h>
10 #include <fcntl.h>
11 #include <time.h>
12 #endif
13
14 #include <signal.h>
15
16 #include "quakedef.h"
17
18 // =======================================================================
19 // General routines
20 // =======================================================================
21 void Sys_Shutdown (void)
22 {
23 #ifdef FNDELAY
24         fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
25 #endif
26         fflush(stdout);
27 }
28
29 void Sys_Error (const char *error, ...)
30 {
31         va_list argptr;
32         char string[MAX_INPUTLINE];
33
34 // change stdin to non blocking
35 #ifdef FNDELAY
36         fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
37 #endif
38
39         va_start (argptr,error);
40         dpvsnprintf (string, sizeof (string), error, argptr);
41         va_end (argptr);
42
43         Con_Printf ("Quake Error: %s\n", string);
44
45         Host_Shutdown ();
46         exit (1);
47 }
48
49 static int outfd = 1;
50 void Sys_PrintToTerminal(const char *text)
51 {
52         if(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 (outfd, F_GETFL, 0);
59                 fcntl (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(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 (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 #ifdef WIN32
153         setlocale(LC_ALL, "");
154 #endif
155
156         signal(SIGFPE, SIG_IGN);
157
158         com_argc = argc;
159         com_argv = (const char **)argv;
160         Sys_ProvideSelfFD();
161
162         // COMMANDLINEOPTION: sdl: -noterminal disables console output on stdout
163         if(COM_CheckParm("-noterminal"))
164                 outfd = -1;
165         // COMMANDLINEOPTION: sdl: -stderr moves console output to stderr
166         else if(COM_CheckParm("-stderr"))
167                 outfd = 2;
168         else
169                 outfd = 1;
170
171 #ifdef FNDELAY
172         fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);
173 #endif
174
175         Host_Main();
176
177         return 0;
178 }
179
180 qboolean sys_supportsdlgetticks = false;
181 unsigned int Sys_SDL_GetTicks (void)
182 {
183         Sys_Error("Called Sys_SDL_GetTicks on non-SDL target");
184         return 0;
185 }
186 void Sys_SDL_Delay (unsigned int milliseconds)
187 {
188         Sys_Error("Called Sys_SDL_Delay on non-SDL target");
189 }