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