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