]> icculus.org git repositories - divverent/darkplaces.git/blob - sys_linux.c
removed silly uses of 'long' (now int, or qbyte *, depending on the way it was used...
[divverent/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 void Sys_Quit (void)
38 {
39         Host_Shutdown();
40     fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
41         fflush(stdout);
42         exit(0);
43 }
44
45 void Sys_Error (char *error, ...)
46 {
47     va_list     argptr;
48     char        string[1024];
49
50 // change stdin to non blocking
51     fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
52     
53     va_start (argptr,error);
54     vsprintf (string,error,argptr);
55     va_end (argptr);
56         fprintf(stderr, "Error: %s\n", string);
57
58         Host_Shutdown ();
59         exit (1);
60
61
62
63 void Sys_Warn (char *warning, ...)
64
65     va_list     argptr;
66     char        string[1024];
67     
68     va_start (argptr,warning);
69     vsprintf (string,warning,argptr);
70     va_end (argptr);
71         fprintf(stderr, "Warning: %s", string);
72
73
74 /*
75 ============
76 Sys_FileTime
77
78 returns -1 if not present
79 ============
80 */
81 int     Sys_FileTime (char *path)
82 {
83         struct  stat    buf;
84         
85         if (stat (path,&buf) == -1)
86                 return -1;
87         
88         return buf.st_mtime;
89 }
90
91
92 void Sys_mkdir (char *path)
93 {
94     mkdir (path, 0777);
95 }
96
97 int Sys_FileOpenRead (char *path, int *handle)
98 {
99         int     h;
100         struct stat     fileinfo;
101     
102         
103         h = open (path, O_RDONLY, 0666);
104         *handle = h;
105         if (h == -1)
106                 return -1;
107         
108         if (fstat (h,&fileinfo) == -1)
109                 Sys_Error ("Error fstating %s", path);
110
111         return fileinfo.st_size;
112 }
113
114 int Sys_FileOpenWrite (char *path)
115 {
116         int     handle;
117
118         umask (0);
119
120         handle = open(path,O_RDWR | O_CREAT | O_TRUNC, 0666);
121
122         if (handle == -1)
123         {
124                 Con_Printf("Sys_FileOpenWrite: Error opening %s: %s", path, strerror(errno));
125                 return 0;
126         }
127
128         return handle;
129 }
130
131 int Sys_FileWrite (int handle, void *src, int count)
132 {
133         return write (handle, src, count);
134 }
135
136 void Sys_FileClose (int handle)
137 {
138         close (handle);
139 }
140
141 void Sys_FileSeek (int handle, int position)
142 {
143         lseek (handle, position, SEEK_SET);
144 }
145
146 int Sys_FileRead (int handle, void *dest, int count)
147 {
148     return read (handle, dest, count);
149 }
150
151 void Sys_DebugLog(char *file, char *fmt, ...)
152 {
153     va_list argptr; 
154     static char data[1024];
155     int fd;
156     
157     va_start(argptr, fmt);
158     vsprintf(data, fmt, argptr);
159     va_end(argptr);
160 //    fd = open(file, O_WRONLY | O_BINARY | O_CREAT | O_APPEND, 0666);
161     fd = open(file, O_WRONLY | O_CREAT | O_APPEND, 0666);
162     write(fd, data, strlen(data));
163     close(fd);
164 }
165
166 double Sys_DoubleTime (void)
167 {
168         static int first = true;
169         static double oldtime = 0.0, basetime = 0.0;
170         double newtime;
171         struct timeval tp;
172         struct timezone tzp; 
173
174         gettimeofday(&tp, &tzp);
175
176         newtime = (double) ((unsigned long) tp.tv_sec) + tp.tv_usec/1000000.0 - basetime;
177
178         if (first)
179         {
180                 first = false;
181                 basetime = newtime;
182                 newtime = 0.0;
183         }
184
185         if (newtime < oldtime)
186                 Sys_Error("Sys_DoubleTime: time running backwards??\n");
187
188         oldtime = newtime;
189
190         return newtime;
191 }
192
193 // =======================================================================
194 // Sleeps for microseconds
195 // =======================================================================
196
197 static volatile int oktogo;
198
199 void alarm_handler(int x)
200 {
201         oktogo=1;
202 }
203
204 void floating_point_exception_handler(int whatever)
205 {
206 //      Sys_Warn("floating point exception\n");
207         signal(SIGFPE, floating_point_exception_handler);
208 }
209
210 char *Sys_ConsoleInput(void)
211 {
212     static char text[256];
213     int     len;
214         fd_set  fdset;
215     struct timeval timeout;
216
217         if (cls.state == ca_dedicated) {
218                 FD_ZERO(&fdset);
219                 FD_SET(0, &fdset); // stdin
220                 timeout.tv_sec = 0;
221                 timeout.tv_usec = 0;
222                 if (select (1, &fdset, NULL, NULL, &timeout) == -1 || !FD_ISSET(0, &fdset))
223                         return NULL;
224
225                 len = read (0, text, sizeof(text));
226                 if (len < 1)
227                         return NULL;
228                 text[len-1] = 0;    // rip off the /n and terminate
229
230                 return text;
231         }
232         return NULL;
233 }
234
235 void Sys_Sleep(void)
236 {
237         usleep(1);
238 }
239
240 int main (int c, char **v)
241 {
242
243         double oldtime, newtime;
244
245 //      static char cwd[1024];
246
247 //      signal(SIGFPE, floating_point_exception_handler);
248         signal(SIGFPE, SIG_IGN);
249
250         memset(&host_parms, 0, sizeof(host_parms));
251
252         COM_InitArgv(c, v);
253         host_parms.argc = com_argc;
254         host_parms.argv = com_argv;
255
256         host_parms.basedir = basedir;
257
258         fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);
259
260         Sys_Shared_EarlyInit();
261
262         Host_Init();
263
264         Sys_Shared_LateInit();
265
266         oldtime = Sys_DoubleTime () - 0.1;
267         while (1)
268         {
269                 // find time spent rendering last frame
270                 newtime = Sys_DoubleTime ();
271
272                 Host_Frame (newtime - oldtime);
273
274                 oldtime = newtime;
275         }
276         return 0;
277 }