]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - sys_linux.c
rewrote memory system entirely (hunk, cache, and zone are gone, memory pools replaced...
[xonotic/darkplaces.git] / sys_linux.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <stdarg.h>
4
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <unistd.h>
8 #include <fcntl.h>
9
10 #include <signal.h>
11 #include <limits.h>
12 #include <sys/ipc.h>
13 #include <sys/shm.h>
14 #include <sys/time.h>
15 #include <sys/wait.h>
16 #include <sys/mman.h>
17 #include <string.h>
18 #include <ctype.h>
19 #include <errno.h>
20 #include <time.h>
21
22 #include "quakedef.h"
23
24 char *basedir = ".";
25 #if CACHEENABLE
26 char *cachedir = "/tmp";
27 #endif
28
29 // =======================================================================
30 // General routines
31 // =======================================================================
32
33 void Sys_DebugNumber(int y, int val)
34 {
35 }
36
37 #if 0
38 static char end1[] =
39 "\x1b[?7h\x1b[40m\x1b[2J\x1b[0;1;41m\x1b[1;1H                QUAKE: The Doomed Dimension \x1b[33mby \x1b[44mid\x1b[41m Software                      \x1b[2;1H  ----------------------------------------------------------------------------  \x1b[3;1H           CALL 1-800-IDGAMES TO ORDER OR FOR TECHNICAL SUPPORT                 \x1b[4;1H             PRICE: $45.00 (PRICES MAY VARY OUTSIDE THE US.)                    \x1b[5;1H                                                                                \x1b[6;1H  \x1b[37mYes! You only have one fourth of this incredible epic. That is because most   \x1b[7;1H   of you have paid us nothing or at most, very little. You could steal the     \x1b[8;1H   game from a friend. But we both know you'll be punished by God if you do.    \x1b[9;1H        \x1b[33mWHY RISK ETERNAL DAMNATION? CALL 1-800-IDGAMES AND BUY NOW!             \x1b[10;1H             \x1b[37mRemember, we love you almost as much as He does.                   \x1b[11;1H                                                                                \x1b[12;1H            \x1b[33mProgramming: \x1b[37mJohn Carmack, Michael Abrash, John Cash                \x1b[13;1H       \x1b[33mDesign: \x1b[37mJohn Romero, Sandy Petersen, American McGee, Tim Willits         \x1b[14;1H                     \x1b[33mArt: \x1b[37mAdrian Carmack, Kevin Cloud                           \x1b[15;1H               \x1b[33mBiz: \x1b[37mJay Wilbur, Mike Wilson, Donna Jackson                      \x1b[16;1H            \x1b[33mProjects: \x1b[37mShawn Green   \x1b[33mSupport: \x1b[37mBarrett Alexander                  \x1b[17;1H              \x1b[33mSound Effects: \x1b[37mTrent Reznor and Nine Inch Nails                   \x1b[18;1H  For other information or details on ordering outside the US, check out the    \x1b[19;1H     files accompanying QUAKE or our website at http://www.idsoftware.com.      \x1b[20;1H    \x1b[0;41mQuake is a trademark of Id Software, inc., (c)1996 Id Software, inc.        \x1b[21;1H     All rights reserved. NIN logo is a"
40 " registered trademark licensed to        \x1b[22;1H                 Nothing Interactive, Inc. All rights reserved.                 \x1b[40m\x1b[23;1H\x1b[0m";
41 static char end2[] =
42 "\x1b[?7h\x1b[40m\x1b[2J\x1b[0;1;41m\x1b[1;1H        QUAKE \x1b[33mby \x1b[44mid\x1b[41m Software                                                    \x1b[2;1H -----------------------------------------------------------------------------  \x1b[3;1H        \x1b[37mWhy did you quit from the registered version of QUAKE? Did the          \x1b[4;1H        scary monsters frighten you? Or did Mr. Sandman tug at your             \x1b[5;1H        little lids? No matter! What is important is you love our               \x1b[6;1H        game, and gave us your money. Congratulations, you are probably         \x1b[7;1H        not a thief.                                                            \x1b[8;1H                                                           Thank You.           \x1b[9;1H        \x1b[33;44mid\x1b[41m Software is:                                                         \x1b[10;1H        PROGRAMMING: \x1b[37mJohn Carmack, Michael Abrash, John Cash                    \x1b[11;1H        \x1b[33mDESIGN: \x1b[37mJohn Romero, Sandy Petersen, American McGee, Tim Willits        \x1b[12;1H        \x1b[33mART: \x1b[37mAdrian Carmack, Kevin Cloud                                        \x1b[13;1H        \x1b[33mBIZ: \x1b[37mJay Wilbur, Mike Wilson     \x1b[33mPROJECTS MAN: \x1b[37mShawn Green              \x1b[14;1H        \x1b[33mBIZ ASSIST: \x1b[37mDonna Jackson        \x1b[33mSUPPORT: \x1b[37mBarrett Alexander             \x1b[15;1H        \x1b[33mSOUND EFFECTS AND MUSIC: \x1b[37mTrent Reznor and Nine Inch Nails               \x1b[16;1H                                                                                \x1b[17;1H        If you need help running QUAKE refer to the text files in the           \x1b[18;1H        QUAKE directory, or our website at http://www.idsoftware.com.           \x1b[19;1H        If all else fails, call our technical support at 1-800-IDGAMES.         \x1b[20;1H      \x1b[0;41mQuake is a trademark of Id Software, inc., (c)1996 Id Software, inc.      \x1b[21;1H        All rights reserved."
43 " NIN logo is a registered trademark licensed        \x1b[22;1H             to Nothing Interactive, Inc. All rights reserved.                  \x1b[23;1H\x1b[40m\x1b[0m";
44 #endif
45
46 void Sys_Quit (void)
47 {
48         Host_Shutdown();
49     fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
50 #if 0
51         if (registered.integer)
52                 printf("%s", end2);
53         else
54                 printf("%s", end1);
55 #endif
56         fflush(stdout);
57         exit(0);
58 }
59
60 void Sys_Error (char *error, ...)
61
62     va_list     argptr;
63     char        string[1024];
64
65 // change stdin to non blocking
66     fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
67     
68     va_start (argptr,error);
69     vsprintf (string,error,argptr);
70     va_end (argptr);
71         fprintf(stderr, "Error: %s\n", string);
72
73         Host_Shutdown ();
74         exit (1);
75
76
77
78 void Sys_Warn (char *warning, ...)
79
80     va_list     argptr;
81     char        string[1024];
82     
83     va_start (argptr,warning);
84     vsprintf (string,warning,argptr);
85     va_end (argptr);
86         fprintf(stderr, "Warning: %s", string);
87
88
89 /*
90 ============
91 Sys_FileTime
92
93 returns -1 if not present
94 ============
95 */
96 int     Sys_FileTime (char *path)
97 {
98         struct  stat    buf;
99         
100         if (stat (path,&buf) == -1)
101                 return -1;
102         
103         return buf.st_mtime;
104 }
105
106
107 void Sys_mkdir (char *path)
108 {
109     mkdir (path, 0777);
110 }
111
112 int Sys_FileOpenRead (char *path, int *handle)
113 {
114         int     h;
115         struct stat     fileinfo;
116     
117         
118         h = open (path, O_RDONLY, 0666);
119         *handle = h;
120         if (h == -1)
121                 return -1;
122         
123         if (fstat (h,&fileinfo) == -1)
124                 Sys_Error ("Error fstating %s", path);
125
126         return fileinfo.st_size;
127 }
128
129 int Sys_FileOpenWrite (char *path)
130 {
131         int     handle;
132
133         umask (0);
134         
135         handle = open(path,O_RDWR | O_CREAT | O_TRUNC, 0666);
136
137         if (handle == -1)
138         {
139                 Con_Printf("Sys_FileOpenWrite: Error opening %s: %s", path, strerror(errno));
140                 return 0;
141         }
142
143         return handle;
144 }
145
146 int Sys_FileWrite (int handle, void *src, int count)
147 {
148         return write (handle, src, count);
149 }
150
151 void Sys_FileClose (int handle)
152 {
153         close (handle);
154 }
155
156 void Sys_FileSeek (int handle, int position)
157 {
158         lseek (handle, position, SEEK_SET);
159 }
160
161 int Sys_FileRead (int handle, void *dest, int count)
162 {
163     return read (handle, dest, count);
164 }
165
166 void Sys_DebugLog(char *file, char *fmt, ...)
167 {
168     va_list argptr; 
169     static char data[1024];
170     int fd;
171     
172     va_start(argptr, fmt);
173     vsprintf(data, fmt, argptr);
174     va_end(argptr);
175 //    fd = open(file, O_WRONLY | O_BINARY | O_CREAT | O_APPEND, 0666);
176     fd = open(file, O_WRONLY | O_CREAT | O_APPEND, 0666);
177     write(fd, data, strlen(data));
178     close(fd);
179 }
180
181 double Sys_DoubleTime (void)
182 {
183         static int first = true;
184         static double oldtime = 0.0, basetime = 0.0;
185         double newtime;
186         struct timeval tp;
187         struct timezone tzp; 
188
189         gettimeofday(&tp, &tzp);
190
191         newtime = (double) ((unsigned long) tp.tv_sec) + tp.tv_usec/1000000.0 - basetime;
192
193         if (first)
194         {
195                 first = false;
196                 basetime = newtime;
197                 newtime = 0.0;
198         }
199
200         if (newtime < oldtime)
201                 Sys_Error("Sys_DoubleTime: time running backwards??\n");
202
203         oldtime = newtime;
204
205         return newtime;
206 }
207
208 // =======================================================================
209 // Sleeps for microseconds
210 // =======================================================================
211
212 static volatile int oktogo;
213
214 void alarm_handler(int x)
215 {
216         oktogo=1;
217 }
218
219 void floating_point_exception_handler(int whatever)
220 {
221 //      Sys_Warn("floating point exception\n");
222         signal(SIGFPE, floating_point_exception_handler);
223 }
224
225 char *Sys_ConsoleInput(void)
226 {
227     static char text[256];
228     int     len;
229         fd_set  fdset;
230     struct timeval timeout;
231
232         if (cls.state == ca_dedicated) {
233                 FD_ZERO(&fdset);
234                 FD_SET(0, &fdset); // stdin
235                 timeout.tv_sec = 0;
236                 timeout.tv_usec = 0;
237                 if (select (1, &fdset, NULL, NULL, &timeout) == -1 || !FD_ISSET(0, &fdset))
238                         return NULL;
239
240                 len = read (0, text, sizeof(text));
241                 if (len < 1)
242                         return NULL;
243                 text[len-1] = 0;    // rip off the /n and terminate
244
245                 return text;
246         }
247         return NULL;
248 }
249
250 void Sys_Sleep(void)
251 {
252         usleep(1);
253 }
254
255 int main (int c, char **v)
256 {
257
258         double oldtime, newtime;
259
260 //      static char cwd[1024];
261
262 //      signal(SIGFPE, floating_point_exception_handler);
263         signal(SIGFPE, SIG_IGN);
264
265         memset(&host_parms, 0, sizeof(host_parms));
266
267         COM_InitArgv(c, v);
268         host_parms.argc = com_argc;
269         host_parms.argv = com_argv;
270
271         host_parms.basedir = basedir;
272
273         fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);
274
275         Sys_Shared_EarlyInit();
276
277         Host_Init();
278
279         Sys_Shared_LateInit();
280
281         oldtime = Sys_DoubleTime () - 0.1;
282         while (1)
283         {
284                 // find time spent rendering last frame
285                 newtime = Sys_DoubleTime ();
286
287                 Host_Frame (newtime - oldtime);
288
289                 oldtime = newtime;
290         }
291         return 0;
292 }