2 ===========================================================================
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
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.
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.
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/>.
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.
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.
26 ===========================================================================
29 #ifndef __SYS_PUBLIC__
30 #define __SYS_PUBLIC__
33 ===============================================================================
35 Non-portable system services.
37 ===============================================================================
42 #if defined(WIN32) || defined(_WIN32)
44 #define BUILD_STRING "win-x86"
46 #define CPUSTRING "x86"
47 #define CPU_EASYARGS 1
49 #define ALIGN16( x ) __declspec(align(16)) x
52 #define _alloca16( x ) ((void *)((((int)_alloca( (x)+15 )) + 15) & ~15))
54 #define PATHSEPERATOR_STR "\\"
55 #define PATHSEPERATOR_CHAR '\\'
57 #define ID_INLINE __forceinline
58 #define ID_STATIC_TEMPLATE static
60 #define assertmem( x, y ) assert( _CrtIsValidPointer( x, y, true ) )
65 #if defined(MACOS_X) || defined(__APPLE__)
67 #define BUILD_STRING "MacOSX-universal"
70 #define CPUSTRING "ppc"
71 #define CPU_EASYARGS 0
72 #elif defined(__i386__)
73 #define CPUSTRING "x86"
74 #define CPU_EASYARGS 1
77 #define ALIGN16( x ) x __attribute__ ((aligned (16)))
83 #define PACKED __attribute__((packed))
86 #define _alloca alloca
87 #define _alloca16( x ) ((void *)((((int)alloca( (x)+15 )) + 15) & ~15))
89 #define PATHSEPERATOR_STR "/"
90 #define PATHSEPERATOR_CHAR '/'
95 #define ID_INLINE inline
96 #define ID_STATIC_TEMPLATE
98 #define assertmem( x, y )
107 #define BUILD_STRING "linux-x86"
108 #define BUILD_OS_ID 2
109 #define CPUSTRING "x86"
110 #define CPU_EASYARGS 1
111 #elif defined(__ppc__)
112 #define BUILD_STRING "linux-ppc"
113 #define CPUSTRING "ppc"
114 #define CPU_EASYARGS 0
117 #define _alloca alloca
118 #define _alloca16( x ) ((void *)((((int)alloca( (x)+15 )) + 15) & ~15))
120 #define ALIGN16( x ) x
121 #define PACKED __attribute__((packed))
123 #define PATHSEPERATOR_STR "/"
124 #define PATHSEPERATOR_CHAR '/'
127 #define ASSERT assert
129 #define ID_INLINE inline
130 #define ID_STATIC_TEMPLATE
132 #define assertmem( x, y )
137 #define id_attribute(x) __attribute__(x)
139 #define id_attribute(x)
143 CPUID_NONE = 0x00000,
144 CPUID_UNSUPPORTED = 0x00001, // unsupported (386/486)
145 CPUID_GENERIC = 0x00002, // unrecognized processor
146 CPUID_INTEL = 0x00004, // Intel
147 CPUID_AMD = 0x00008, // AMD
148 CPUID_MMX = 0x00010, // Multi Media Extensions
149 CPUID_3DNOW = 0x00020, // 3DNow!
150 CPUID_SSE = 0x00040, // Streaming SIMD Extensions
151 CPUID_SSE2 = 0x00080, // Streaming SIMD Extensions 2
152 CPUID_SSE3 = 0x00100, // Streaming SIMD Extentions 3 aka Prescott's New Instructions
153 CPUID_ALTIVEC = 0x00200, // AltiVec
154 CPUID_HTT = 0x01000, // Hyper-Threading Technology
155 CPUID_CMOV = 0x02000, // Conditional Move (CMOV) and fast floating point comparison (FCOMI) instructions
156 CPUID_FTZ = 0x04000, // Flush-To-Zero mode (denormal results are flushed to zero)
157 CPUID_DAZ = 0x08000 // Denormals-Are-Zero mode (denormal source operands are set to zero)
161 FPU_EXCEPTION_INVALID_OPERATION = 1,
162 FPU_EXCEPTION_DENORMALIZED_OPERAND = 2,
163 FPU_EXCEPTION_DIVIDE_BY_ZERO = 4,
164 FPU_EXCEPTION_NUMERIC_OVERFLOW = 8,
165 FPU_EXCEPTION_NUMERIC_UNDERFLOW = 16,
166 FPU_EXCEPTION_INEXACT_RESULT = 32
170 FPU_PRECISION_SINGLE = 0,
171 FPU_PRECISION_DOUBLE = 1,
172 FPU_PRECISION_DOUBLE_EXTENDED = 2
176 FPU_ROUNDING_TO_NEAREST = 0,
177 FPU_ROUNDING_DOWN = 1,
179 FPU_ROUNDING_TO_ZERO = 3
193 SE_NONE, // evTime is still valid
194 SE_KEY, // evValue is a key code, evValue2 is the down flag
195 SE_CHAR, // evValue is an ascii char
196 SE_MOUSE, // evValue and evValue2 are reletive signed x / y moves
197 SE_JOYSTICK_AXIS, // evValue is an axis number and evValue2 is the current state (-127 to 127)
198 SE_CONSOLE // evPtr is a char*, from typing something at a non-game console
215 typedef struct sysEvent_s {
216 sysEventType_t evType;
219 int evPtrLength; // bytes of data pointed to by evPtr, for journaling
220 void * evPtr; // this must be manually freed if not NULL
223 typedef struct sysMemoryStats_s {
231 int availExtendedVirtual;
234 typedef unsigned long address_t;
236 template<class type> class idList; // for Sys_ListFiles
239 void Sys_Init( void );
240 void Sys_Shutdown( void );
241 void Sys_Error( const char *error, ...);
242 void Sys_Quit( void );
244 bool Sys_AlreadyRunning( void );
246 // note that this isn't journaled...
247 char * Sys_GetClipboardData( void );
248 void Sys_SetClipboardData( const char *string );
250 // will go to the various text consoles
251 // NOT thread safe - never use in the async paths
252 void Sys_Printf( const char *msg, ... )id_attribute((format(printf,1,2)));
254 // guaranteed to be thread-safe
255 void Sys_DebugPrintf( const char *fmt, ... )id_attribute((format(printf,1,2)));
256 void Sys_DebugVPrintf( const char *fmt, va_list arg );
258 // a decent minimum sleep time to avoid going below the process scheduler speeds
259 #define SYS_MINSLEEP 20
261 // allow game to yield CPU time
262 // NOTE: due to SYS_MINSLEEP this is very bad portability karma, and should be completely removed
263 void Sys_Sleep( int msec );
265 // Sys_Milliseconds should only be used for profiling purposes,
266 // any game related timing information should come from event timestamps
267 int Sys_Milliseconds( void );
269 // for accurate performance testing
270 double Sys_GetClockTicks( void );
271 double Sys_ClockTicksPerSecond( void );
273 // returns a selection of the CPUID_* flags
274 cpuid_t Sys_GetProcessorId( void );
275 const char * Sys_GetProcessorString( void );
277 // returns true if the FPU stack is empty
278 bool Sys_FPU_StackIsEmpty( void );
280 // empties the FPU stack
281 void Sys_FPU_ClearStack( void );
283 // returns the FPU state as a string
284 const char * Sys_FPU_GetState( void );
286 // enables the given FPU exceptions
287 void Sys_FPU_EnableExceptions( int exceptions );
289 // sets the FPU precision
290 void Sys_FPU_SetPrecision( int precision );
292 // sets the FPU rounding mode
293 void Sys_FPU_SetRounding( int rounding );
295 // sets Flush-To-Zero mode (only available when CPUID_FTZ is set)
296 void Sys_FPU_SetFTZ( bool enable );
298 // sets Denormals-Are-Zero mode (only available when CPUID_DAZ is set)
299 void Sys_FPU_SetDAZ( bool enable );
301 // returns amount of system ram
302 int Sys_GetSystemRam( void );
304 // returns amount of video ram
305 int Sys_GetVideoRam( void );
307 // returns amount of drive space in path
308 int Sys_GetDriveFreeSpace( const char *path );
310 // returns memory stats
311 void Sys_GetCurrentMemoryStatus( sysMemoryStats_t &stats );
312 void Sys_GetExeLaunchMemoryStatus( sysMemoryStats_t &stats );
314 // lock and unlock memory
315 bool Sys_LockMemory( void *ptr, int bytes );
316 bool Sys_UnlockMemory( void *ptr, int bytes );
318 // set amount of physical work memory
319 void Sys_SetPhysicalWorkMemory( int minBytes, int maxBytes );
321 // allows retrieving the call stack at execution points
322 void Sys_GetCallStack( address_t *callStack, const int callStackSize );
323 const char * Sys_GetCallStackStr( const address_t *callStack, const int callStackSize );
324 const char * Sys_GetCallStackCurStr( int depth );
325 const char * Sys_GetCallStackCurAddressStr( int depth );
326 void Sys_ShutdownSymbols( void );
328 // DLL loading, the path should be a fully qualified OS path to the DLL file to be loaded
329 int Sys_DLL_Load( const char *dllName );
330 void * Sys_DLL_GetProcAddress( int dllHandle, const char *procName );
331 void Sys_DLL_Unload( int dllHandle );
334 void Sys_GenerateEvents( void );
335 sysEvent_t Sys_GetEvent( void );
336 void Sys_ClearEvents( void );
338 // input is tied to windows, so it needs to be started up and shut down whenever
339 // the main window is recreated
340 void Sys_InitInput( void );
341 void Sys_ShutdownInput( void );
342 void Sys_InitScanTable( void );
343 const unsigned char *Sys_GetScanTable( void );
344 unsigned char Sys_GetConsoleKey( bool shifted );
345 // map a scancode key to a char
346 // does nothing on win32, as SE_KEY == SE_CHAR there
347 // on other OSes, consider the keyboard mapping
348 unsigned char Sys_MapCharForKey( int key );
350 // keyboard input polling
351 int Sys_PollKeyboardInputEvents( void );
352 int Sys_ReturnKeyboardInputEvent( const int n, int &ch, bool &state );
353 void Sys_EndKeyboardInputEvents( void );
355 // mouse input polling
356 int Sys_PollMouseInputEvents( void );
357 int Sys_ReturnMouseInputEvent( const int n, int &action, int &value );
358 void Sys_EndMouseInputEvents( void );
360 // when the console is down, or the game is about to perform a lengthy
361 // operation like map loading, the system can release the mouse cursor
362 // when in windowed mode
363 void Sys_GrabMouseCursor( bool grabIt );
365 void Sys_ShowWindow( bool show );
366 bool Sys_IsWindowVisible( void );
367 void Sys_ShowConsole( int visLevel, bool quitOnClose );
370 void Sys_Mkdir( const char *path );
371 ID_TIME_T Sys_FileTimeStamp( FILE *fp );
372 // NOTE: do we need to guarantee the same output on all platforms?
373 const char * Sys_TimeStampToStr( ID_TIME_T timeStamp );
374 const char * Sys_DefaultCDPath( void );
375 const char * Sys_DefaultBasePath( void );
376 const char * Sys_DefaultSavePath( void );
377 const char * Sys_EXEPath( void );
379 // use fs_debug to verbose Sys_ListFiles
380 // returns -1 if directory was not found (the list is cleared)
381 int Sys_ListFiles( const char *directory, const char *extension, idList<class idStr> &list );
383 // know early if we are performing a fatal error shutdown so the error message doesn't get lost
384 void Sys_SetFatalError( const char *error );
386 // display perference dialog
387 void Sys_DoPreferences( void );
390 ==============================================================
394 ==============================================================
398 NA_BAD, // an address lookup failed
414 idPort(); // this just zeros netSocket and port
417 // if the InitForPort fails, the idPort.port field will remain 0
418 bool InitForPort( int portNumber );
419 int GetPort( void ) const { return bound_to.port; }
420 netadr_t GetAdr( void ) const { return bound_to; }
423 bool GetPacket( netadr_t &from, void *data, int &size, int maxSize );
424 bool GetPacketBlocking( netadr_t &from, void *data, int &size, int maxSize, int timeout );
425 void SendPacket( const netadr_t to, const void *data, int size );
434 netadr_t bound_to; // interface and port
435 int netSocket; // OS specific socket
443 // if host is host:port, the value of port is ignored
444 bool Init( const char *host, short port );
447 // returns -1 on failure (and closes socket)
448 // those are non blocking, can be used for polling
449 // there is no buffering, you are not guaranteed to Read or Write everything in a single call
450 // (specially on win32, see recv and send documentation)
451 int Read( void *data, int size );
452 int Write( void *data, int size );
455 netadr_t address; // remote address
456 int fd; // OS specific socket
459 // parses the port number
460 // can also do DNS resolve if you ask for it.
461 // NOTE: DNS resolve is a slow/blocking call, think before you use
462 // ( could be exploited for server DoS )
463 bool Sys_StringToNetAdr( const char *s, netadr_t *a, bool doDNSResolve );
464 const char * Sys_NetAdrToString( const netadr_t a );
465 bool Sys_IsLANAddress( const netadr_t a );
466 bool Sys_CompareNetAdrBase( const netadr_t a, const netadr_t b );
468 void Sys_InitNetworking( void );
469 void Sys_ShutdownNetworking( void );
473 ==============================================================
477 ==============================================================
480 typedef unsigned int (*xthread_t)( void * );
491 unsigned long threadId;
494 const int MAX_THREADS = 10;
495 extern xthreadInfo *g_threads[MAX_THREADS];
496 extern int g_thread_count;
498 void Sys_CreateThread( xthread_t function, void *parms, xthreadPriority priority, xthreadInfo &info, const char *name, xthreadInfo *threads[MAX_THREADS], int *thread_count );
499 void Sys_DestroyThread( xthreadInfo& info ); // sets threadHandle back to 0
501 // find the name of the calling thread
502 // if index != NULL, set the index in g_threads array (use -1 for "main" thread)
503 const char * Sys_GetThreadName( int *index = 0 );
505 const int MAX_CRITICAL_SECTIONS = 4;
508 CRITICAL_SECTION_ZERO = 0,
509 CRITICAL_SECTION_ONE,
510 CRITICAL_SECTION_TWO,
511 CRITICAL_SECTION_THREE
514 void Sys_EnterCriticalSection( int index = CRITICAL_SECTION_ZERO );
515 void Sys_LeaveCriticalSection( int index = CRITICAL_SECTION_ZERO );
517 const int MAX_TRIGGER_EVENTS = 4;
520 TRIGGER_EVENT_ZERO = 0,
526 void Sys_WaitForEvent( int index = TRIGGER_EVENT_ZERO );
527 void Sys_TriggerEvent( int index = TRIGGER_EVENT_ZERO );
530 ==============================================================
534 ==============================================================
539 virtual void DebugPrintf( const char *fmt, ... )id_attribute((format(printf,2,3))) = 0;
540 virtual void DebugVPrintf( const char *fmt, va_list arg ) = 0;
542 virtual double GetClockTicks( void ) = 0;
543 virtual double ClockTicksPerSecond( void ) = 0;
544 virtual cpuid_t GetProcessorId( void ) = 0;
545 virtual const char * GetProcessorString( void ) = 0;
546 virtual const char * FPU_GetState( void ) = 0;
547 virtual bool FPU_StackIsEmpty( void ) = 0;
548 virtual void FPU_SetFTZ( bool enable ) = 0;
549 virtual void FPU_SetDAZ( bool enable ) = 0;
551 virtual void FPU_EnableExceptions( int exceptions ) = 0;
553 virtual bool LockMemory( void *ptr, int bytes ) = 0;
554 virtual bool UnlockMemory( void *ptr, int bytes ) = 0;
556 virtual void GetCallStack( address_t *callStack, const int callStackSize ) = 0;
557 virtual const char * GetCallStackStr( const address_t *callStack, const int callStackSize ) = 0;
558 virtual const char * GetCallStackCurStr( int depth ) = 0;
559 virtual void ShutdownSymbols( void ) = 0;
561 virtual int DLL_Load( const char *dllName ) = 0;
562 virtual void * DLL_GetProcAddress( int dllHandle, const char *procName ) = 0;
563 virtual void DLL_Unload( int dllHandle ) = 0;
564 virtual void DLL_GetFileName( const char *baseName, char *dllName, int maxLength ) = 0;
566 virtual sysEvent_t GenerateMouseButtonEvent( int button, bool down ) = 0;
567 virtual sysEvent_t GenerateMouseMoveEvent( int deltax, int deltay ) = 0;
569 virtual void OpenURL( const char *url, bool quit ) = 0;
570 virtual void StartProcess( const char *exePath, bool quit ) = 0;
575 bool Sys_LoadOpenAL( void );
576 void Sys_FreeOpenAL( void );
578 #endif /* !__SYS_PUBLIC__ */