]> git.xonotic.org Git - xonotic/darkplaces.git/blob - sys_unix.c
deduplicate main()
[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 "darkplaces.h"
14
15 sys_t sys;
16
17 // =======================================================================
18 // General routines
19 // =======================================================================
20 void Sys_Shutdown (void)
21 {
22 #ifndef WIN32
23         fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~O_NONBLOCK);
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 #ifndef WIN32
35         fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~O_NONBLOCK);
36 #endif
37         va_start (argptr,error);
38         dpvsnprintf (string, sizeof (string), error, argptr);
39         va_end (argptr);
40
41         Con_Printf(CON_ERROR "Engine Error: %s\n", string);
42
43         //Host_Shutdown ();
44         exit (1);
45 }
46
47 void Sys_Print(const char *text)
48 {
49         if(sys.outfd < 0)
50                 return;
51         // BUG: for some reason, NDELAY also affects stdout (1) when used on stdin (0).
52         // this is because both go to /dev/tty by default!
53         {
54 #ifndef WIN32
55                 int origflags = fcntl (sys.outfd, F_GETFL, 0);
56                 fcntl (sys.outfd, F_SETFL, origflags & ~O_NONBLOCK);
57 #else
58 #define write _write
59 #endif
60                 while(*text)
61                 {
62                         fs_offset_t written = (fs_offset_t)write(sys.outfd, text, (int)strlen(text));
63                         if(written <= 0)
64                                 break; // sorry, I cannot do anything about this error - without an output
65                         text += written;
66                 }
67 #ifndef WIN32
68                 fcntl (sys.outfd, F_SETFL, origflags);
69 #endif
70         }
71         //fprintf(stdout, "%s", text);
72 }
73
74 char *Sys_ConsoleInput(void)
75 {
76         static char text[MAX_INPUTLINE];
77         static unsigned int len = 0;
78 #ifdef WIN32
79         int c;
80
81         // read a line out
82         while (_kbhit ())
83         {
84                 c = _getch ();
85                 if (c == '\r')
86                 {
87                         text[len] = '\0';
88                         _putch ('\n');
89                         len = 0;
90                         return text;
91                 }
92                 if (c == '\b')
93                 {
94                         if (len)
95                         {
96                                 _putch (c);
97                                 _putch (' ');
98                                 _putch (c);
99                                 len--;
100                         }
101                         continue;
102                 }
103                 if (len < sizeof (text) - 1)
104                 {
105                         _putch (c);
106                         text[len] = c;
107                         len++;
108                 }
109         }
110 #else
111         fd_set fdset;
112         struct timeval timeout;
113         FD_ZERO(&fdset);
114         FD_SET(0, &fdset); // stdin
115         timeout.tv_sec = 0;
116         timeout.tv_usec = 0;
117         if (select (1, &fdset, NULL, NULL, &timeout) != -1 && FD_ISSET(0, &fdset))
118         {
119                 len = read (0, text, sizeof(text) - 1);
120                 if (len >= 1)
121                 {
122                         // rip off the \n and terminate
123                         // div0: WHY? console code can deal with \n just fine
124                         // this caused problems with pasting stuff into a terminal window
125                         // so, not ripping off the \n, but STILL keeping a NUL terminator
126                         text[len] = 0;
127                         return text;
128                 }
129         }
130 #endif
131         return NULL;
132 }
133
134 char *Sys_GetClipboardData (void)
135 {
136         return NULL;
137 }
138
139 void Sys_SDL_Init(void)
140 {
141 }
142
143 qbool sys_supportsdlgetticks = false;
144 unsigned int Sys_SDL_GetTicks (void)
145 {
146         Sys_Error("Called Sys_SDL_GetTicks on non-SDL target");
147         return 0;
148 }
149 void Sys_SDL_Delay (unsigned int milliseconds)
150 {
151         Sys_Error("Called Sys_SDL_Delay on non-SDL target");
152 }