1 /*
2 ===========================================================================
3 
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
6 
7 This file is part of the Doom 3 GPL Source Code ("Doom 3 Source Code").
8 
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13 
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code.  If not, see <http://www.gnu.org/licenses/>.
21 
22 In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code.  If not, please request a copy in writing from id Software at the address below.
23 
24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
25 
26 ===========================================================================
27 */
28 
29 #ifndef __SYS_PUBLIC__
30 #define __SYS_PUBLIC__
31 
32 class idStr;
33 
34 typedef enum {
35 	CPUID_NONE							= 0x00000,
36 	CPUID_UNSUPPORTED					= 0x00001,	// unsupported (386/486)
37 	CPUID_GENERIC						= 0x00002,	// unrecognized processor
38 	CPUID_MMX							= 0x00010,	// Multi Media Extensions
39 	CPUID_3DNOW							= 0x00020,	// 3DNow!
40 	CPUID_SSE							= 0x00040,	// Streaming SIMD Extensions
41 	CPUID_SSE2							= 0x00080,	// Streaming SIMD Extensions 2
42 	CPUID_SSE3							= 0x00100,	// Streaming SIMD Extentions 3 aka Prescott's New Instructions
43 	CPUID_ALTIVEC						= 0x00200,	// AltiVec
44 } cpuidSimd_t;
45 
46 typedef enum {
47 	AXIS_SIDE,
48 	AXIS_FORWARD,
49 	AXIS_UP,
50 	AXIS_ROLL,
51 	AXIS_YAW,
52 	AXIS_PITCH,
53 	MAX_JOYSTICK_AXIS
54 } joystickAxis_t;
55 
56 typedef enum {
57 	SE_NONE,				// evTime is still valid
58 	SE_KEY,					// evValue is a key code, evValue2 is the down flag
59 	SE_CHAR,				// evValue is an ascii char
60 	SE_MOUSE,				// evValue and evValue2 are reletive signed x / y moves
61 	SE_JOYSTICK_AXIS,		// evValue is an axis number and evValue2 is the current state (-127 to 127)
62 	SE_CONSOLE				// evPtr is a char*, from typing something at a non-game console
63 } sysEventType_t;
64 
65 typedef enum {
66 	M_ACTION1,
67 	M_ACTION2,
68 	M_ACTION3,
69 	M_ACTION4,
70 	M_ACTION5,
71 	M_ACTION6,
72 	M_ACTION7,
73 	M_ACTION8,
74 	M_DELTAX,
75 	M_DELTAY,
76 	M_DELTAZ
77 } sys_mEvents;
78 
79 struct sysEvent_t {
80 	sysEventType_t	evType;
81 	int				evValue;
82 	int				evValue2;
83 	int				evPtrLength;		// bytes of data pointed to by evPtr, for journaling
84 	void *			evPtr;				// this must be manually freed if not NULL
85 };
86 
87 enum sysPath_t {
88 	PATH_BASE,
89 	PATH_CONFIG,
90 	PATH_SAVE,
91 	PATH_EXE
92 };
93 
94 template<class type> class idList;		// for Sys_ListFiles
95 
96 
97 void			Sys_Init( void );
98 void			Sys_Shutdown( void );
99 void			Sys_Error( const char *error, ...);
100 void			Sys_Quit( void );
101 
102 // note that this isn't journaled...
103 char *			Sys_GetClipboardData( void );
104 void			Sys_SetClipboardData( const char *string );
105 
106 // will go to the various text consoles
107 // NOT thread safe - never use in the async paths
108 void			Sys_Printf( const char *msg, ... )id_attribute((format(printf,1,2)));
109 
110 // guaranteed to be thread-safe
111 void			Sys_DebugPrintf( const char *fmt, ... )id_attribute((format(printf,1,2)));
112 void			Sys_DebugVPrintf( const char *fmt, va_list arg );
113 
114 // allow game to yield CPU time
115 // NOTE: due to SDL_TIMESLICE this is very bad portability karma, and should be completely removed
116 void			Sys_Sleep( int msec );
117 
118 // Sys_Milliseconds should only be used for profiling purposes,
119 // any game related timing information should come from event timestamps
120 unsigned int	Sys_Milliseconds( void );
121 
122 // returns a selection of the CPUID_* flags
123 int				Sys_GetProcessorId( void );
124 
125 // sets the FPU precision
126 void			Sys_FPU_SetPrecision();
127 
128 // sets Flush-To-Zero mode
129 void			Sys_FPU_SetFTZ( bool enable );
130 
131 // sets Denormals-Are-Zero mode
132 void			Sys_FPU_SetDAZ( bool enable );
133 
134 // returns amount of system ram
135 int				Sys_GetSystemRam( void );
136 
137 // returns amount of drive space in path
138 int				Sys_GetDriveFreeSpace( const char *path );
139 
140 // lock and unlock memory
141 bool			Sys_LockMemory( void *ptr, int bytes );
142 bool			Sys_UnlockMemory( void *ptr, int bytes );
143 
144 // set amount of physical work memory
145 void			Sys_SetPhysicalWorkMemory( int minBytes, int maxBytes );
146 
147 // DLL loading, the path should be a fully qualified OS path to the DLL file to be loaded
148 uintptr_t		Sys_DLL_Load( const char *dllName );
149 void *			Sys_DLL_GetProcAddress( uintptr_t dllHandle, const char *procName );
150 void			Sys_DLL_Unload( uintptr_t dllHandle );
151 
152 // event generation
153 void			Sys_GenerateEvents( void );
154 sysEvent_t		Sys_GetEvent( void );
155 void			Sys_ClearEvents( void );
156 char			*Sys_ConsoleInput( void );
157 
158 // input is tied to windows, so it needs to be started up and shut down whenever
159 // the main window is recreated
160 void			Sys_InitInput( void );
161 void			Sys_ShutdownInput( void );
162 void			Sys_InitScanTable( void );
163 unsigned char	Sys_GetConsoleKey( bool shifted );
164 // map a scancode key to a char
165 // does nothing on win32, as SE_KEY == SE_CHAR there
166 // on other OSes, consider the keyboard mapping
167 unsigned char	Sys_MapCharForKey( int key );
168 
169 // keyboard input polling
170 int				Sys_PollKeyboardInputEvents( void );
171 int				Sys_ReturnKeyboardInputEvent( const int n, int &ch, bool &state );
172 void			Sys_EndKeyboardInputEvents( void );
173 
174 // mouse input polling
175 int				Sys_PollMouseInputEvents( void );
176 int				Sys_ReturnMouseInputEvent( const int n, int &action, int &value );
177 void			Sys_EndMouseInputEvents( void );
178 
179 // when the console is down, or the game is about to perform a lengthy
180 // operation like map loading, the system can release the mouse cursor
181 // when in windowed mode
182 void			Sys_GrabMouseCursor( bool grabIt );
183 
184 void			Sys_ShowWindow( bool show );
185 bool			Sys_IsWindowVisible( void );
186 void			Sys_ShowConsole( int visLevel, bool quitOnClose );
187 
188 
189 void			Sys_Mkdir( const char *path );
190 ID_TIME_T			Sys_FileTimeStamp( FILE *fp );
191 // NOTE: do we need to guarantee the same output on all platforms?
192 const char *	Sys_TimeStampToStr( ID_TIME_T timeStamp );
193 
194 bool			Sys_GetPath(sysPath_t type, idStr &path);
195 
196 // use fs_debug to verbose Sys_ListFiles
197 // returns -1 if directory was not found (the list is cleared)
198 int				Sys_ListFiles( const char *directory, const char *extension, idList<class idStr> &list );
199 
200 /*
201 ==============================================================
202 
203 	Networking
204 
205 ==============================================================
206 */
207 
208 typedef enum {
209 	NA_BAD,					// an address lookup failed
210 	NA_LOOPBACK,
211 	NA_BROADCAST,
212 	NA_IP
213 } netadrtype_t;
214 
215 typedef struct {
216 	netadrtype_t	type;
217 	unsigned char	ip[4];
218 	unsigned short	port;
219 } netadr_t;
220 
221 #define	PORT_ANY			-1
222 
223 class idPort {
224 public:
225 				idPort();				// this just zeros netSocket and port
226 	virtual		~idPort();
227 
228 	// if the InitForPort fails, the idPort.port field will remain 0
229 	bool		InitForPort( int portNumber );
GetPort(void)230 	int			GetPort( void ) const { return bound_to.port; }
GetAdr(void)231 	netadr_t	GetAdr( void ) const { return bound_to; }
232 	void		Close();
233 
234 	bool		GetPacket( netadr_t &from, void *data, int &size, int maxSize );
235 	bool		GetPacketBlocking( netadr_t &from, void *data, int &size, int maxSize, int timeout );
236 	void		SendPacket( const netadr_t to, const void *data, int size );
237 
238 	int			packetsRead;
239 	int			bytesRead;
240 
241 	int			packetsWritten;
242 	int			bytesWritten;
243 
244 private:
245 	netadr_t	bound_to;		// interface and port
246 	int			netSocket;		// OS specific socket
247 };
248 
249 class idTCP {
250 public:
251 				idTCP();
252 	virtual		~idTCP();
253 
254 	// if host is host:port, the value of port is ignored
255 	bool		Init( const char *host, short port );
256 	void		Close();
257 
258 	// returns -1 on failure (and closes socket)
259 	// those are non blocking, can be used for polling
260 	// there is no buffering, you are not guaranteed to Read or Write everything in a single call
261 	// (specially on win32, see recv and send documentation)
262 	int			Read( void *data, int size );
263 	int			Write( void *data, int size );
264 
265 private:
266 	netadr_t	address;		// remote address
267 	int			fd;				// OS specific socket
268 };
269 
270 				// parses the port number
271 				// can also do DNS resolve if you ask for it.
272 				// NOTE: DNS resolve is a slow/blocking call, think before you use
273 				// ( could be exploited for server DoS )
274 bool			Sys_StringToNetAdr( const char *s, netadr_t *a, bool doDNSResolve );
275 const char *	Sys_NetAdrToString( const netadr_t a );
276 bool			Sys_IsLANAddress( const netadr_t a );
277 bool			Sys_CompareNetAdrBase( const netadr_t a, const netadr_t b );
278 
279 void			Sys_InitNetworking( void );
280 void			Sys_ShutdownNetworking( void );
281 
282 
283 /*
284 ==============================================================
285 
286 	Multi-threading
287 
288 ==============================================================
289 */
290 
291 struct SDL_Thread;
292 
293 typedef int (*xthread_t)( void * );
294 
295 typedef struct {
296 	const char		*name;
297 	SDL_Thread		*threadHandle;
298 	unsigned int	threadId;
299 } xthreadInfo;
300 
301 void				Sys_CreateThread( xthread_t function, void *parms, xthreadInfo &info, const char *name );
302 void				Sys_DestroyThread( xthreadInfo& info ); // sets threadHandle back to 0
303 
304 // find the name of the calling thread
305 // if index != NULL, set the index in threads array (use -1 for "main" thread)
306 const char *		Sys_GetThreadName( int *index = 0 );
307 
308 extern void Sys_InitThreads();
309 extern void Sys_ShutdownThreads();
310 
311 const int MAX_CRITICAL_SECTIONS		= 5;
312 
313 enum {
314 	CRITICAL_SECTION_ZERO = 0,
315 	CRITICAL_SECTION_ONE,
316 	CRITICAL_SECTION_TWO,
317 	CRITICAL_SECTION_THREE,
318 	CRITICAL_SECTION_SYS
319 };
320 
321 void				Sys_EnterCriticalSection( int index = CRITICAL_SECTION_ZERO );
322 void				Sys_LeaveCriticalSection( int index = CRITICAL_SECTION_ZERO );
323 
324 const int MAX_TRIGGER_EVENTS		= 4;
325 
326 enum {
327 	TRIGGER_EVENT_ZERO = 0,
328 	TRIGGER_EVENT_ONE,
329 	TRIGGER_EVENT_TWO,
330 	TRIGGER_EVENT_THREE
331 };
332 
333 void				Sys_WaitForEvent( int index = TRIGGER_EVENT_ZERO );
334 void				Sys_TriggerEvent( int index = TRIGGER_EVENT_ZERO );
335 
336 /*
337 ==============================================================
338 
339 	idSys
340 
341 ==============================================================
342 */
343 
344 class idSys {
345 public:
346 	virtual void			DebugPrintf( const char *fmt, ... )id_attribute((format(printf,2,3))) = 0;
347 	virtual void			DebugVPrintf( const char *fmt, va_list arg ) = 0;
348 
349 	virtual unsigned int	GetMilliseconds( void ) = 0;
350 	virtual int				GetProcessorId( void ) = 0;
351 	virtual void			FPU_SetFTZ( bool enable ) = 0;
352 	virtual void			FPU_SetDAZ( bool enable ) = 0;
353 
354 	virtual bool			LockMemory( void *ptr, int bytes ) = 0;
355 	virtual bool			UnlockMemory( void *ptr, int bytes ) = 0;
356 
357 	virtual uintptr_t		DLL_Load( const char *dllName ) = 0;
358 	virtual void *			DLL_GetProcAddress( uintptr_t dllHandle, const char *procName ) = 0;
359 	virtual void			DLL_Unload( uintptr_t dllHandle ) = 0;
360 	virtual void			DLL_GetFileName( const char *baseName, char *dllName, int maxLength ) = 0;
361 
362 	virtual sysEvent_t		GenerateMouseButtonEvent( int button, bool down ) = 0;
363 	virtual sysEvent_t		GenerateMouseMoveEvent( int deltax, int deltay ) = 0;
364 
365 	virtual void			OpenURL( const char *url, bool quit ) = 0;
366 	virtual void			StartProcess( const char *exePath, bool quit ) = 0;
367 };
368 
369 extern idSys *				sys;
370 
371 #endif /* !__SYS_PUBLIC__ */
372