]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - sys_linux.c
cleaned up and merged a lot of sys_*.c code into sys_shared.c
[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_Quit (void)
34 {
35         Host_Shutdown();
36         fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
37         fflush(stdout);
38         exit(0);
39 }
40
41 void Sys_Error (const char *error, ...)
42 {
43         va_list argptr;
44         char string[1024];
45
46 // change stdin to non blocking
47         fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
48
49         va_start (argptr,error);
50         vsprintf (string,error,argptr);
51         va_end (argptr);
52         fprintf(stderr, "Error: %s\n", string);
53
54         Host_Shutdown ();
55         exit (1);
56
57 }
58
59 void Sys_Warn (const char *warning, ...)
60 {
61         va_list argptr;
62         char string[1024];
63
64         va_start (argptr,warning);
65         vsprintf (string,warning,argptr);
66         va_end (argptr);
67         fprintf(stderr, "Warning: %s", string);
68 }
69
70 double Sys_DoubleTime (void)
71 {
72         static int first = true;
73         static double oldtime = 0.0, curtime = 0.0;
74         double newtime;
75         struct timeval tp;
76
77         gettimeofday(&tp, NULL);
78
79         newtime = (double) tp.tv_sec + tp.tv_usec / 1000000.0;
80
81         if (first)
82         {
83                 first = false;
84                 oldtime = newtime;
85         }
86
87         if (newtime < oldtime)
88         {
89                 // warn if it's significant
90                 if (newtime - oldtime < -0.01)
91                         Con_Printf("Sys_DoubleTime: time stepped backwards (went from %f to %f, difference %f)\n", oldtime, newtime, newtime - oldtime);
92         }
93         else
94                 curtime += newtime - oldtime;
95         oldtime = newtime;
96
97         return curtime;
98 }
99
100 // =======================================================================
101 // Sleeps for microseconds
102 // =======================================================================
103
104 static volatile int oktogo;
105
106 void alarm_handler(int x)
107 {
108         oktogo=1;
109 }
110
111 void floating_point_exception_handler(int whatever)
112 {
113         signal(SIGFPE, floating_point_exception_handler);
114 }
115
116 char *Sys_ConsoleInput(void)
117 {
118         static char text[256];
119         int len;
120         fd_set fdset;
121         struct timeval timeout;
122
123         if (cls.state == ca_dedicated)
124         {
125                 FD_ZERO(&fdset);
126                 FD_SET(0, &fdset); // stdin
127                 timeout.tv_sec = 0;
128                 timeout.tv_usec = 0;
129                 if (select (1, &fdset, NULL, NULL, &timeout) == -1 || !FD_ISSET(0, &fdset))
130                         return NULL;
131
132                 len = read (0, text, sizeof(text));
133                 if (len < 1)
134                         return NULL;
135                 text[len-1] = 0;    // rip off the /n and terminate
136
137                 return text;
138         }
139         return NULL;
140 }
141
142 void Sys_Sleep(void)
143 {
144         usleep(1);
145 }
146
147 int main (int c, char **v)
148 {
149         double frameoldtime, framenewtime;
150
151         signal(SIGFPE, SIG_IGN);
152
153         memset(&host_parms, 0, sizeof(host_parms));
154
155         host_parms.argc = c;
156         host_parms.argv = v;
157         host_parms.basedir = basedir;
158
159         fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);
160
161         Sys_Shared_EarlyInit();
162
163         Host_Init();
164
165         Sys_Shared_LateInit();
166
167         frameoldtime = Sys_DoubleTime () - 0.1;
168         while (1)
169         {
170                 // find time spent rendering last frame
171                 framenewtime = Sys_DoubleTime ();
172
173                 Host_Frame (framenewtime - frameoldtime);
174
175                 frameoldtime = framenewtime;
176         }
177         return 0;
178 }