home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2006 March / Gamestar_82_2006-03_dvd.iso / DVDStar / Editace / quake4_sdkv10.exe / source / sys / sys_public.h
C/C++ Source or Header  |  2005-11-14  |  27KB  |  962 lines

  1.  
  2. #ifndef __SYS_PUBLIC__
  3. #define __SYS_PUBLIC__
  4.  
  5.  
  6. /*
  7. ===============================================================================
  8.  
  9.     Non-portable system services.
  10.  
  11. ===============================================================================
  12. */
  13.  
  14.  
  15. // Win32
  16. #ifdef _WINDOWS 
  17.  
  18. #define    BUILD_STRING                    "win-x86"
  19. #define BUILD_OS_ID                        0
  20. #define    CPUSTRING                        "x86"
  21. #define CPU_EASYARGS                    1
  22.  
  23. #define ALIGN16( x )                    __declspec(align(16)) x
  24. #define PACKED
  25.  
  26. #define _alloca16( x )                    ((void *)((((int)_alloca( (x)+15 )) + 15) & ~15))
  27.  
  28. #define ID_INLINE                        __forceinline
  29. #define ID_STATIC_TEMPLATE                static
  30.  
  31. #define assertmem( x, y )                assert( _CrtIsValidPointer( x, y, true ) )
  32.  
  33. #endif
  34.  
  35. #ifdef __GNUC__
  36. #define id_attribute(x) __attribute__(x)
  37. #else
  38. #define id_attribute(x)  
  39. #endif
  40.  
  41. // Mac OSX
  42. #if defined(MACOS_X) || defined(__APPLE__)
  43.  
  44. #include "osx/apple_bool.h"
  45.  
  46. #ifdef __ppc__
  47.     #define BUILD_STRING                "MacOSX-ppc"
  48.     #define BUILD_OS_ID                    1
  49.     #define    CPUSTRING                    "ppc"
  50.     #define CPU_EASYARGS                0
  51. #elif defined(__i386__)
  52.     #define BUILD_STRING                "MacOSX-x86"
  53.     #define    CPUSTRING                    "x86"
  54.     #define CPU_EASYARGS                1
  55. #endif
  56.  
  57. //#define ALIGN16( x )                    ( (x) __attribute__ ((aligned (16))) )
  58. #define ALIGN16( x )                    x
  59. #ifdef __MWERKS__
  60. #define PACKED
  61. #else
  62. #define PACKED                            __attribute__((packed))
  63. #endif
  64.  
  65. #define _alloca                            alloca
  66. #define _alloca16( x )                    ((void *)((((int)alloca( (x)+15 )) + 15) & ~15))
  67.  
  68. #define __cdecl
  69. #define ASSERT                            assert
  70.  
  71. #define ID_INLINE                        inline
  72. #define ID_STATIC_TEMPLATE
  73.  
  74. #define assertmem( x, y )
  75.  
  76. #endif
  77.  
  78.  
  79. // Linux
  80. #ifdef __linux__
  81.  
  82. #ifdef __i386__
  83.     #define    BUILD_STRING                "linux-x86"
  84.     #define BUILD_OS_ID                    2
  85.     #define CPUSTRING                    "x86"
  86.     #define CPU_EASYARGS                1
  87. #elif defined(__ppc__)
  88.     #define    BUILD_STRING                "linux-ppc"
  89.     #define CPUSTRING                    "ppc"
  90.     #define CPU_EASYARGS                0
  91. #endif
  92.  
  93. #define _alloca                            alloca
  94. #define _alloca16( x )                    ((void *)((((int)alloca( (x)+15 )) + 15) & ~15))
  95.  
  96. #define ALIGN16( x )                    x
  97. #define PACKED                            __attribute__((packed))
  98.  
  99. #define __cdecl
  100. #define ASSERT                            assert
  101.  
  102. #define ID_INLINE                        inline
  103. #define ID_STATIC_TEMPLATE
  104.  
  105. #define assertmem( x, y )
  106.  
  107. #endif
  108.  
  109. // Xenon
  110. #ifdef _XENON
  111.  
  112. #define    BUILD_STRING                    "Xenon-ppc"
  113. #define BUILD_OS_ID                        3    //fixme if id ever addes in apple i386 or linux ppc defines
  114. #define CPUSTRING                        "ppc"
  115.  
  116. #define _alloca16( x )                    ((void *)((((int)_alloca( (x)+15 )) + 15) & ~15))
  117.  
  118. #define ALIGN16( x )                    x
  119. #define PACKED                            
  120.  
  121. #define __cdecl
  122. #define ASSERT                            assert
  123.  
  124. #define ID_INLINE                        inline
  125. #define ID_STATIC_TEMPLATE
  126.  
  127. #define assertmem( x, y )
  128.  
  129. class idCmdArgs;
  130. extern void Com_DoNothing( const idCmdArgs &args );
  131.  
  132. #ifndef _FINAL
  133.  
  134.     // Memory tracking helpers
  135.     namespace MemTracker {
  136.         // Dummy func so the command system doesnt barf on startup
  137.         extern void Com_DumpAllocInfo( const idCmdArgs &args );
  138.         extern void OnAlloc( void *mem, size_t size );
  139.         extern void OnDelete( void *mem );
  140.         extern void Init();
  141.         extern void BeginFrame();
  142.         extern void EndFrame();
  143.         extern void Enable( bool enabled );
  144.         
  145.         // Tag tracking
  146.         extern int PushTag( int tag ); // Returns a safe int value that can be used to pop the tag
  147.         extern int PushTag( const char* tag );  // Returns a safe int value that can be used to pop the tag
  148.         extern void PopTag( int tag );
  149.         extern void PopTag( const char* tag );
  150.     
  151.         // Helper class for automagic tracking
  152.         class AutoPushPop 
  153.         {
  154.             int mPopVal;
  155.         public:
  156.             AutoPushPop( const char* name ) 
  157.             {
  158.                 mPopVal = PushTag( name );
  159.             }
  160.             
  161.             AutoPushPop( int tag ) 
  162.             {
  163.                 mPopVal = PushTag( tag );
  164.             }
  165.             
  166.             ~AutoPushPop() 
  167.             {
  168.                 PopTag( mPopVal );
  169.             }
  170.             
  171.             // You can force it to pop as well
  172.             void Pop() 
  173.             {
  174.                 PopTag( mPopVal );
  175.                 mPopVal = 0xffffffff;
  176.             }
  177.         };
  178.     
  179.     };
  180.  
  181.     #define MEMTRACKER_INDIRECT(F, X) F(X)
  182.     #define MEMTRACKER_STRINGIZE(X) #X
  183.     #define MEMTRACKER_LINESTR MEMTRACKER_INDIRECT(MEMTRACKER_STRINGIZE, __LINE__)
  184.  
  185.     #define MEM_TRACK(X) \
  186.         MemTracker::AutoPushPop __FILE__##MEMTRACKER_LINESTR##_autoPushPop(X);
  187.     /**/
  188.     
  189.     #define MEM_PUSH_TAG(X) \
  190.         MemTracker::PushTag(X);
  191.     /**/
  192.     
  193.     #define MEM_POP_TAG(X) \
  194.         MemTracker::PopTag(X);
  195.     /**/
  196.  
  197. #else
  198.  
  199.     #define MEM_TRACK(X) \
  200.     /**/
  201.     
  202.     #define MEM_PUSH_TAG(X) \
  203.     /**/
  204.     
  205.     #define MEM_POP_TAG(X) \
  206.     /**/
  207.  
  208. #endif
  209.  
  210. #endif
  211.  
  212. typedef enum {
  213.     CPUID_NONE                            = 0x00000,
  214.     CPUID_UNSUPPORTED                    = 0x00001,    // unsupported (386/486)
  215.     CPUID_GENERIC                        = 0x00002,    // unrecognized processor
  216.     CPUID_INTEL                            = 0x00004,    // Intel
  217.     CPUID_AMD                            = 0x00008,    // AMD
  218.     CPUID_MMX                            = 0x00010,    // Multi Media Extensions
  219.     CPUID_3DNOW                            = 0x00020,    // 3DNow!
  220.     CPUID_SSE                            = 0x00040,    // Streaming SIMD Extensions
  221.     CPUID_SSE2                            = 0x00080,    // Streaming SIMD Extensions 2
  222.     CPUID_SSE3                            = 0x00100,    // Streaming SIMD Extentions 3 aka Prescott's New Instructions
  223.     CPUID_ALTIVEC                        = 0x00200,    // AltiVec
  224.     CPUID_HTT                            = 0x01000,    // Hyper-Threading Technology
  225.     CPUID_CMOV                            = 0x02000,    // Conditional Move (CMOV) and fast floating point comparison (FCOMI) instructions
  226.     CPUID_FTZ                            = 0x04000,    // Flush-To-Zero mode (denormal results are flushed to zero)
  227.     CPUID_DAZ                            = 0x08000,    // Denormals-Are-Zero mode (denormal source operands are set to zero)
  228.     CPUID_EM64T                            = 0x10000,    // 64-bit Memory Extensions
  229.     CPUID_AMD64                            = 0x20000,    // possibly redundant with EM64T, but see comment in Is64BitAMD() to see why I'm not chancing it.
  230. // RAVEN BEGIN
  231.     CPUID_XENON                            = 0x80000    // Xenon PPC processor
  232. // RAVEN END
  233. } cpuid_t;
  234.  
  235. typedef enum {
  236.     FPU_EXCEPTION_INVALID_OPERATION        = 1,
  237.     FPU_EXCEPTION_DENORMALIZED_OPERAND    = 2,
  238.     FPU_EXCEPTION_DIVIDE_BY_ZERO        = 4,
  239.     FPU_EXCEPTION_NUMERIC_OVERFLOW        = 8,
  240.     FPU_EXCEPTION_NUMERIC_UNDERFLOW        = 16,
  241.     FPU_EXCEPTION_INEXACT_RESULT        = 32
  242. } fpuExceptions_t;
  243.  
  244. typedef enum {
  245.     FPU_PRECISION_SINGLE                = 0,
  246.     FPU_PRECISION_DOUBLE                = 1,
  247.     FPU_PRECISION_DOUBLE_EXTENDED        = 2
  248. } fpuPrecision_t;
  249.  
  250. typedef enum {
  251.     FPU_ROUNDING_TO_NEAREST                = 0,
  252.     FPU_ROUNDING_DOWN                    = 1,
  253.     FPU_ROUNDING_UP                        = 2,
  254.     FPU_ROUNDING_TO_ZERO                = 3
  255. } fpuRounding_t;
  256.  
  257. typedef enum {
  258.     SE_NONE,                // evTime is still valid
  259.     SE_KEY,                    // evValue is a key code, evValue2 is the down flag
  260.     SE_CHAR,                // evValue is an ascii char
  261.     SE_MOUSE,                // evValue and evValue2 are reletive signed x / y moves
  262.     SE_JOYSTICK_AXIS,        // evValue is an axis number and evValue2 is the current state (-127 to 127)
  263.     SE_CONSOLE                // evPtr is a char*, from typing something at a non-game console
  264. // RAVEN BEGIN
  265. // rjohnson: debug event overflow stuff
  266.     ,
  267.     SE_MAX
  268. // RAVEN END
  269. } sysEventType_t;
  270.  
  271. typedef enum {
  272.     M_ACTION1,
  273.     M_ACTION2,
  274.     M_ACTION3,
  275.     M_ACTION4,
  276.     M_ACTION5,
  277.     M_ACTION6,
  278.     M_ACTION7,
  279.     M_ACTION8,
  280.     M_DELTAX,
  281.     M_DELTAY,
  282.     M_DELTAZ
  283. } sys_mEvents;
  284.  
  285. // RAVEN BEGIN
  286. // rjohnson: new joystick code
  287. #define MAX_AXIS_RANGE    127
  288. #define JOY_TO_CURSOR_SPEED        ( idMath::M_MS2SEC * common->GetUserCmdMSec() * 1.5f * 140.f )
  289.  
  290. typedef enum {
  291.     AXIS_LEFT_HORIZONTAL,
  292.     AXIS_LEFT_VERTICAL,
  293.     AXIS_RIGHT_HORIZONTAL,
  294.     AXIS_RIGHT_VERTICAL,
  295.     MAX_JOYSTICK_AXIS
  296. } joystickAxis_t;
  297.  
  298. typedef enum {
  299.     J_ACTION_BUTTON_LEFT_SHOULDER,        // K_JOY1
  300.     J_ACTION_BUTTON_RIGHT_SHOULDER,        // K_JOY2
  301.     J_ACTION_BUTTON_A,                    // K_JOY3
  302.     J_ACTION_BUTTON_B,                    // K_JOY4
  303.     J_ACTION_BUTTON_Y,                    // K_JOY5
  304.     J_ACTION_BUTTON_X,                    // K_JOY6
  305.     J_ACTION_BUTTON_START,                // K_JOY7
  306.     J_ACTION_BUTTON_BACK,                // K_JOY8
  307.     J_ACTION_BUTTON_DPAD_UP,            // K_JOY9
  308.     J_ACTION_BUTTON_DPAD_DOWN,            // K_JOY10
  309.     J_ACTION_BUTTON_DPAD_RIGHT,            // K_JOY11
  310.     J_ACTION_BUTTON_DPAD_LEFT,            // K_JOY12
  311.     J_ACTION_BUTTON_AXIS_LEFT,            // K_JOY13
  312.     J_ACTION_BUTTON_AXIS_RIGHT,            // K_JOY14
  313.     J_ACTION_BUTTON_LEFT_TRIGGER,        // K_JOY16
  314.     J_ACTION_BUTTON_RIGHT_TRIGGER,        // K_JOY15
  315.  
  316.     J_DELTA_LEFT_HORIZONTAL,
  317.     J_DELTA_LEFT_VERTICAL,
  318.     J_DELTA_RIGHT_HORIZONTAL,
  319.     J_DELTA_RIGHT_VERTICAL,
  320.     
  321.     J_ACTION_BUTTON_GARBAGE,
  322. } sys_jEvents;
  323. // RAVEN END
  324.  
  325. typedef struct sysEvent_s {
  326.     sysEventType_t    evType;
  327.     int                evValue;
  328.     int                evValue2;
  329.     int                evPtrLength;        // bytes of data pointed to by evPtr, for journaling
  330.     void *            evPtr;                // this must be manually freed if not NULL
  331. } sysEvent_t;
  332.  
  333. typedef struct sysMemoryStats_s {
  334.     int memoryLoad;
  335.     int totalPhysical;
  336.     int availPhysical;
  337.     int totalPageFile;
  338.     int availPageFile;
  339.     int totalVirtual;
  340.     int availVirtual;
  341.     int availExtendedVirtual;
  342. } sysMemoryStats_t;
  343.  
  344. typedef unsigned long address_t;
  345.  
  346. template<class type> class idList;        // for Sys_ListFiles
  347.  
  348.  
  349. void            Sys_Init( void );
  350. void            Sys_Shutdown( void );
  351. void            Sys_Error( const char *error, ...);
  352. void            Sys_Quit( void );
  353.  
  354. bool            Sys_AlreadyRunning( void );
  355.  
  356. // note that this isn't journaled...
  357. char *            Sys_GetClipboardData( void );
  358. void            Sys_SetClipboardData( const char *string );
  359.  
  360. // will go to the various text consoles
  361. // NOT thread safe - never use in the async paths
  362. void            Sys_Printf( const char *msg, ... )id_attribute((format(printf,1,2)));
  363.  
  364. // guaranteed to be thread-safe
  365. void            Sys_DebugPrintf( const char *fmt, ... )id_attribute((format(printf,1,2)));
  366. void            Sys_DebugVPrintf( const char *fmt, va_list arg );
  367.  
  368. // a decent minimum sleep time to avoid going below the process scheduler speeds
  369. #define            SYS_MINSLEEP    20
  370.  
  371. // allow game to yield CPU time
  372. // NOTE: due to SYS_MINSLEEP this is very bad portability karma, and should be completely removed
  373. void            Sys_Sleep( int msec );
  374.  
  375. // returns whether the main rendering window has focus
  376. bool            Sys_IsAppActive( void );
  377.  
  378. // Sys_Milliseconds should only be used for profiling purposes,
  379. // any game related timing information should come from event timestamps
  380. int                Sys_Milliseconds( void );
  381.  
  382. // for accurate performance testing
  383. double            Sys_GetClockTicks( void );
  384. double            Sys_ClockTicksPerSecond( void );
  385.  
  386. // returns a selection of the CPUID_* flags
  387. cpuid_t            Sys_GetProcessorId( void );
  388. const char *    Sys_GetProcessorString( void );
  389.  
  390. // returns true if the FPU stack is empty
  391. bool            Sys_FPU_StackIsEmpty( void );
  392.  
  393. // empties the FPU stack
  394. void            Sys_FPU_ClearStack( void );
  395.  
  396. // returns the FPU state as a string
  397. const char *    Sys_FPU_GetState( void );
  398.  
  399. // enables the given FPU exceptions
  400. void            Sys_FPU_EnableExceptions( int exceptions );
  401.  
  402. // sets the FPU precision
  403. void            Sys_FPU_SetPrecision( int precision );
  404.  
  405. // sets the FPU rounding mode
  406. void            Sys_FPU_SetRounding( int rounding );
  407.  
  408. // sets Flush-To-Zero mode (only available when CPUID_FTZ is set)
  409. void            Sys_FPU_SetFTZ( bool enable );
  410.  
  411. // sets Denormals-Are-Zero mode (only available when CPUID_DAZ is set)
  412. void            Sys_FPU_SetDAZ( bool enable );
  413.  
  414. // returns amount of system ram
  415. int                Sys_GetSystemRam( void );
  416.  
  417. // returns amount of video ram
  418. int                Sys_GetVideoRam( void );
  419.  
  420. // returns amount of drive space in path
  421. int                Sys_GetDriveFreeSpace( const char *path );
  422.  
  423. // returns memory stats
  424. void            Sys_GetCurrentMemoryStatus( sysMemoryStats_t &stats );
  425. void            Sys_GetExeLaunchMemoryStatus( sysMemoryStats_t &stats );
  426.  
  427. // lock and unlock memory
  428. bool            Sys_LockMemory( void *ptr, int bytes );
  429. bool            Sys_UnlockMemory( void *ptr, int bytes );
  430.  
  431. // set amount of physical work memory
  432. void            Sys_SetPhysicalWorkMemory( int minBytes, int maxBytes );
  433.  
  434. // allows retrieving the call stack at execution points
  435. void            Sys_GetCallStack( address_t *callStack, const int callStackSize );
  436. const char *    Sys_GetCallStackStr( const address_t *callStack, const int callStackSize );
  437. const char *    Sys_GetCallStackCurStr( int depth );
  438. const char *    Sys_GetCallStackCurAddressStr( int depth );
  439. void            Sys_ShutdownSymbols( void );
  440.  
  441. #ifdef _LOAD_DLL
  442. // DLL loading, the path should be a fully qualified OS path to the DLL file to be loaded
  443. int                Sys_DLL_Load( const char *dllName );
  444. void *            Sys_DLL_GetProcAddress( int dllHandle, const char *procName );
  445. void            Sys_DLL_Unload( int dllHandle );
  446. #endif // _LOAD_DLL
  447.  
  448. // event generation
  449. void            Sys_GenerateEvents( void );
  450. sysEvent_t        Sys_GetEvent( void );
  451. void            Sys_ClearEvents( void );
  452.  
  453. // input is tied to windows, so it needs to be started up and shut down whenever 
  454. // the main window is recreated
  455. void            Sys_InitInput( void );
  456. void            Sys_ShutdownInput( void );
  457. // keyboard input polling
  458. int                Sys_PollKeyboardInputEvents( void );
  459. int                Sys_ReturnKeyboardInputEvent( const int n, int &ch, bool &state );
  460. void            Sys_EndKeyboardInputEvents( void );
  461. int                Sys_MapKey( unsigned long key, unsigned short wParam );
  462.  
  463. // mouse input polling
  464. int                Sys_PollMouseInputEvents( void );
  465. int                Sys_ReturnMouseInputEvent( const int n, int &action, int &value );
  466. void            Sys_EndMouseInputEvents( void );
  467.  
  468. // RAVEN BEGIN
  469. // ksergent: joystick input polling
  470. int                Sys_PollJoystickInputEvents( void );
  471. bool            Sys_IsJoystickEnabled( void );
  472. bool            Sys_IsJoystickConnected( int index );
  473. int                Sys_ReturnJoystickInputEvent( const int n, int &action, int &value );
  474. void            Sys_EndJoystickInputEvents( void );
  475. // RAVEN END
  476.  
  477. // when the console is down, or the game is about to perform a lengthy
  478. // operation like map loading, the system can release the mouse cursor
  479. // when in windowed mode
  480. bool            Sys_IsWindowVisible( void );
  481. void            Sys_Mkdir( const char *path );
  482.  
  483. // RAVEN BEGIN
  484. // jscott: thread handling
  485. void            Sys_StartAsyncThread( void );
  486. void            Sys_EndAsyncThread( void );
  487.  
  488. // jscott: VTune interface
  489. #ifndef _FINAL
  490. void            Sys_StartProfiling( void );
  491. void            Sys_StopProfiling( void );
  492. #endif
  493. // RAVEN END
  494.  
  495. // NOTE: do we need to guarantee the same output on all platforms?
  496. const char *    Sys_TimeStampToStr( long timeStamp );
  497. const char *    Sys_DefaultCDPath( void );
  498. const char *    Sys_DefaultBasePath( void );
  499. const char *    Sys_DefaultSavePath( void );
  500. const char *    Sys_EXEPath( void );
  501.  
  502. // use fs_debug to verbose Sys_ListFiles
  503. // returns -1 if directory was not found (the list is cleared)
  504. int                Sys_ListFiles( const char *directory, const char *extension, idList<class idStr> &list );
  505.  
  506.  
  507. // RAVEN BEGIN
  508. // rjohnson: added block
  509. bool            Sys_AppShouldSleep        ( void );
  510. // RAVEN END
  511.  
  512. // RAVEN BEGIN
  513. // twhitaker: directx version
  514. // return NULL if the DirectX 9 or greater is not found.  otherwise returns version in the format "9.0c"
  515. const char *    Sys_GetDirectXVersion    ( void );
  516. // RAVEN END
  517.  
  518. /*
  519. ==============================================================
  520.  
  521.     Networking
  522.  
  523. ==============================================================
  524. */
  525.  
  526. typedef enum {
  527.     NA_BAD,                    // an address lookup failed
  528.     NA_LOOPBACK,
  529.     NA_BROADCAST,
  530.     NA_IP
  531. // RAVEN BEGIN
  532. // rjohnson: add fake clients
  533.     ,
  534.     NA_FAKE
  535. // RAVEN END
  536. } netadrtype_t;
  537.  
  538. typedef struct {
  539.     netadrtype_t    type;
  540.     unsigned char    ip[4];
  541.     unsigned short    port;
  542. } netadr_t;
  543.  
  544. #define    PORT_ANY            -1
  545.  
  546. class idPort {
  547. public:
  548.                 idPort();                // this just zeros netSocket and port
  549.                 ~idPort();
  550.  
  551.     // if the InitForPort fails, the idPort.port field will remain 0
  552. // RAVEN BEGIN
  553. // asalmon: option for xbox to create a VDP socket
  554. #ifdef _XBOX
  555.     bool        InitForPort( int portNumber, bool vdp = false );
  556. #else
  557.     bool        InitForPort( int portNumber );
  558. #endif
  559. // RAVEN END
  560.     int            GetPort( void ) const { return port; }
  561. // RAVEN BEGIN
  562. // amccarthy: For Xbox this needs to be an unsigned int
  563. #ifdef _XBOX
  564.     unsigned int GetSocket( void ) const { return netSocket; }
  565. #endif
  566. // RAVEN END
  567.     void        Close();
  568.  
  569.     bool        GetPacket( netadr_t &from, void *data, int &size, int maxSize );
  570.     bool        GetPacketBlocking( netadr_t &from, void *data, int &size, int maxSize, int timeout );
  571.     void        SendPacket( const netadr_t to, const void *data, int size );
  572.  
  573. //RAVEN BEGIN
  574. //asalmon: second version of sendPacket for Xbox avoids netadr_t
  575. #ifdef _XBOX
  576.     bool        SendPacketVDP( const struct sockaddr *to, const void *data, int size );
  577. #endif
  578. //RAVEN END
  579.  
  580.     void        GetTrafficStats(  int &bytesSent, int &packetsSent, int &bytesReceived, int &packetsReceived ) const;
  581.  
  582.     void        SetSilent( bool silent );
  583.     bool        GetSilent( void ) const;
  584.  
  585. private:
  586.     int            packetsRead;
  587.     int            bytesRead;
  588.  
  589.     int            packetsWritten;
  590.     int            bytesWritten;
  591.  
  592.     int            port;            // UDP port
  593. //RAVEN BEGIN
  594. //amccarthy: For Xbox this needs to be an unsigned int
  595. #ifdef _XBOX
  596.     unsigned int            netSocket;        // OS specific socket
  597. #else
  598.     int                        netSocket;
  599. #endif
  600. //RAVEN END
  601.  
  602.     bool        silent;            // don't emit anything for a while
  603. };
  604.  
  605. /*
  606. ===============
  607. idPort::GetTrafficStats
  608. ===============
  609. */
  610. ID_INLINE void idPort::GetTrafficStats(  int &_bytesSent, int &_packetsSent, int &_bytesReceived, int &_packetsReceived ) const {
  611.     _bytesSent = bytesWritten;
  612.     _packetsSent = packetsWritten;
  613.     _bytesReceived = bytesRead;
  614.     _packetsReceived = packetsRead;
  615. }
  616.  
  617. /*
  618. ===============
  619. idPort::SetSilent
  620. ===============
  621. */
  622. ID_INLINE void idPort::SetSilent( bool _silent ) { silent = _silent; }
  623.  
  624. /*
  625. ===============
  626. idPort::GetSilent
  627. ===============
  628. */
  629. ID_INLINE bool idPort::GetSilent( void ) const { return silent; }
  630.  
  631. class idTCP {
  632. public:
  633.                 idTCP();
  634.     virtual        ~idTCP();
  635.  
  636.     // if host is host:port, the value of port is ignored
  637.     bool        Init( const char *host, short port );
  638.     void        Close();
  639.  
  640.     // returns -1 on failure (and closes socket)
  641.     // those are non blocking, can be used for polling
  642.     // there is no buffering, you are not guaranteed to Read or Write everything in a single call
  643.     // (specially on win32, see recv and send documentation)
  644.     int            Read( void *data, int size );
  645.     int            Write( void *data, int size );
  646.  
  647. private:
  648.     netadr_t    address;        // remote address
  649.     int            fd;                // OS specific socket
  650. };
  651.  
  652.                 // parses the port number
  653.                 // can also do DNS resolve if you ask for it.
  654.                 // NOTE: DNS resolve is a slow/blocking call, think before you use
  655.                 // ( could be exploited for server DoS )
  656. bool            Sys_StringToNetAdr( const char *s, netadr_t *a, bool doDNSResolve );
  657. const char *    Sys_NetAdrToString( const netadr_t a );
  658. bool            Sys_IsLANAddress( const netadr_t a );
  659. bool            Sys_CompareNetAdrBase( const netadr_t a, const netadr_t b );
  660.  
  661. void            Sys_InitNetworking( void );
  662. void            Sys_ShutdownNetworking( void );
  663.  
  664. // RAVEN BEGIN
  665. // ddynerman: utility functions
  666. // TTimo: FIXME if exposed, call them Sys_
  667. int Net_GetNumInterfaces( void );
  668. netadr_t Net_GetInterface( int index );
  669.  
  670. // asalmon: Xbox live related functions
  671. #ifdef _XBOX
  672. #define NONCE_SIZE  8
  673. bool            Sys_CreateLiveMatch( void );
  674. bool            Sys_CreateSystemLinkMatch( void );
  675. bool            Sys_VerifyString(const char *string);
  676. #endif
  677. // RAVEN END
  678.  
  679.  
  680. /*
  681. ==============================================================
  682.  
  683.     Multi-threading
  684.  
  685. ==============================================================
  686. */
  687.  
  688. typedef unsigned int (*xthread_t)( void * );
  689.  
  690. typedef enum {
  691.     THREAD_NORMAL,
  692.     THREAD_ABOVE_NORMAL,
  693.     THREAD_HIGHEST
  694. } xthreadPriority;
  695.  
  696. typedef struct {
  697.     const char *    name;
  698.     int                threadHandle;
  699.     unsigned long    threadId;
  700. // RAVEN BEGIN
  701. // ksergent: included to track multiprocessor system
  702. #ifdef _XBOX
  703.     unsigned char cpuID;
  704. #endif
  705. // RAVEN END
  706. } xthreadInfo;
  707.  
  708. const int MAX_THREADS                = 10;
  709. extern xthreadInfo *g_threads[MAX_THREADS];
  710. extern int            g_thread_count;
  711.  
  712. void                Sys_CreateThread( xthread_t function, void *parms, xthreadPriority priority, xthreadInfo &info, const char *name, xthreadInfo *threads[MAX_THREADS], int *thread_count );
  713. void                Sys_DestroyThread( xthreadInfo& info ); // sets threadHandle back to 0
  714.  
  715. // find the name of the calling thread
  716. // if index != NULL, set the index in g_threads array (use -1 for "main" thread)
  717. const char *        Sys_GetThreadName( int *index = 0 );
  718.  
  719. const int MAX_CRITICAL_SECTIONS        = 4;
  720.  
  721. enum {
  722.     CRITICAL_SECTION_ZERO = 0,
  723.     CRITICAL_SECTION_ONE,
  724.     CRITICAL_SECTION_TWO,
  725.     CRITICAL_SECTION_THREE
  726. };
  727.  
  728. void                Sys_EnterCriticalSection( int index = CRITICAL_SECTION_ZERO );
  729. void                Sys_LeaveCriticalSection( int index = CRITICAL_SECTION_ZERO );
  730.  
  731. const int MAX_TRIGGER_EVENTS        = 4;
  732.  
  733. enum {
  734.     TRIGGER_EVENT_ZERO = 0,
  735.     TRIGGER_EVENT_ONE,
  736.     TRIGGER_EVENT_TWO,
  737.     TRIGGER_EVENT_THREE
  738. };
  739.  
  740. void                Sys_WaitForEvent( int index = TRIGGER_EVENT_ZERO );
  741. void                Sys_TriggerEvent( int index = TRIGGER_EVENT_ZERO );
  742.  
  743. /*
  744. ==============================================================
  745.  
  746.     idSys
  747.  
  748. ==============================================================
  749. */
  750.  
  751. class idSys {
  752. public:
  753.     virtual void            DebugPrintf( const char *fmt, ... )id_attribute((format(printf,2,3))) = 0;
  754.     virtual void            DebugVPrintf( const char *fmt, va_list arg ) = 0;
  755.  
  756.     virtual double            GetClockTicks( void ) = 0;
  757.     virtual double            ClockTicksPerSecond( void ) = 0;
  758.     virtual cpuid_t            GetProcessorId( void ) = 0;
  759.     virtual const char *    GetProcessorString( void ) = 0;
  760.     virtual const char *    FPU_GetState( void ) = 0;
  761.     virtual bool            FPU_StackIsEmpty( void ) = 0;
  762.     virtual void            FPU_SetFTZ( bool enable ) = 0;
  763.     virtual void            FPU_SetDAZ( bool enable ) = 0;
  764. // RAVEN BEGIN
  765.     virtual void            FPU_SetPrecision( int flags ) = 0;
  766. // RAVEN END
  767.  
  768.     virtual bool            LockMemory( void *ptr, int bytes ) = 0;
  769.     virtual bool            UnlockMemory( void *ptr, int bytes ) = 0;
  770.  
  771.     virtual void            GetCallStack( address_t *callStack, const int callStackSize ) = 0;
  772.     virtual const char *    GetCallStackStr( const address_t *callStack, const int callStackSize ) = 0;
  773.     virtual const char *    GetCallStackCurStr( int depth ) = 0;
  774.     virtual void            ShutdownSymbols( void ) = 0;
  775.  
  776.     virtual int                DLL_Load( const char *dllName ) = 0;
  777.     virtual void *            DLL_GetProcAddress( int dllHandle, const char *procName ) = 0;
  778.     virtual void            DLL_Unload( int dllHandle ) = 0;
  779.     virtual void            DLL_GetFileName( const char *baseName, char *dllName, int maxLength ) = 0;
  780.  
  781.     virtual sysEvent_t        GenerateMouseButtonEvent( int button, bool down ) = 0;
  782.     virtual sysEvent_t        GenerateMouseMoveEvent( int deltax, int deltay ) = 0;
  783.  
  784. // RAVEN BEGIN
  785.     virtual int                MapKey( unsigned long lParam, unsigned short wParam ) = 0;
  786.     virtual void            AddKeyPress( int key, bool state ) = 0;
  787.     virtual int                GetNumKeyPresses( void ) = 0;
  788.     virtual    bool            GetKeyPress( const int n, int &key, bool &state ) = 0;
  789.  
  790.     virtual void *            CreateWindowEx( const char *className, const char *windowName, int style, int x, int y, int w, int h, void *parent, void *menu, void *instance, void *param, int extStyle = 0 ) = 0;
  791.     virtual void *            GetDC( void *hWnd ) = 0;
  792.     virtual    void            ReleaseDC( void *hWnd, void *hDC ) = 0;
  793.     virtual    void            ShowWindow( void *hWnd, int show ) = 0;
  794.     virtual    void            UpdateWindow( void *hWnd ) = 0;
  795.     virtual bool            IsWindowVisible( void *hWnd ) = 0;
  796.     virtual void            SetForegroundWindow( void *hWnd ) = 0;
  797.     virtual void            SetFocus( void *hWnd ) = 0;
  798.     virtual    void            DestroyWindow( void *hWnd ) = 0;
  799.  
  800.     virtual    void            ShowConsole( int visLevel, bool quitOnClose ) = 0;
  801.     virtual    void            UpdateConsole( void ) = 0;
  802.     virtual bool            IsAppActive( void ) const = 0;
  803.     virtual    int                Milliseconds( void ) = 0;
  804.     virtual void            InitInput( void ) = 0;
  805.     virtual void            ShutdownInput( void ) = 0;
  806.     virtual void            GenerateEvents( void ) = 0;
  807.     virtual void            GrabMouseCursor( bool grabIt ) = 0;
  808.  
  809.     virtual FILE            *FOpen( const char *name, const char *mode ) = 0;
  810.     virtual void            FPrintf( FILE *file, const char *fmt ) = 0;
  811.     virtual int                FTell( FILE *file ) = 0;
  812.     virtual int                FSeek( FILE *file, long offset, int mode ) = 0;
  813.     virtual void            FClose( FILE *file ) = 0;
  814.     virtual int                FRead( void *buffer, int size, int count, FILE *file ) = 0;
  815.     virtual int                FWrite( void *buffer, int size, int count, FILE *file ) = 0;
  816.     virtual    long            FileTimeStamp( FILE *file ) = 0;
  817.     virtual int                FEof( FILE *stream  ) = 0;
  818.     virtual char            *FGets( char *string, int n, FILE *stream ) = 0;
  819.     virtual void            FFlush( FILE *f ) = 0;
  820.     virtual int                SetVBuf( FILE *stream, char *buffer, int mode, size_t size  ) = 0;
  821. // RAVEN END
  822.  
  823.     virtual void            OpenURL( const char *url, bool quit ) = 0;
  824.     virtual void            StartProcess( const char *exePath, bool quit ) = 0;
  825. };
  826.  
  827. extern idSys *                sys;
  828.  
  829. // RAVEN BEGIN
  830. // jnewquist: Scope timing tools
  831. #if defined(_XENON)
  832. class ScopeAutoMeasure {
  833. public:
  834.     ID_INLINE ScopeAutoMeasure(const char *name) {
  835.         mName = name;
  836.         QueryPerformanceCounter( &mStartTime ); 
  837.     }
  838.     ID_INLINE ~ScopeAutoMeasure() {
  839.         LARGE_INTEGER endTime;
  840.         QueryPerformanceCounter( &endTime );
  841.         double time = (double)(endTime.QuadPart - mStartTime.QuadPart) / (Sys_ClockTicksPerSecond() * 0.000001);
  842.         printf("Time %s: %f us\n", mName, time);
  843.     }
  844. protected:
  845.     LARGE_INTEGER    mStartTime;
  846.     const char *    mName;
  847. };
  848.  
  849. #if defined(TIME_CAPTURE) //&& defined(_PROFILE)
  850.  
  851. class TimedScope {
  852. public:
  853.     ID_INLINE TimedScope(const char *name) {
  854.         mName = name;
  855.         mNext = mFirst;
  856.         mFirst = this;
  857.         mTime.QuadPart = 0;
  858.     }
  859.     static void ComputeCost() {
  860.         LARGE_INTEGER TicksPerSecond;
  861.         QueryPerformanceFrequency( &TicksPerSecond );
  862.         sTicksPerMicrosecond = (double)TicksPerSecond.QuadPart * 0.000001;
  863.  
  864.         // get a rough time estimate for the cost of a call to Sys_Milliseconds so that we can remove it from the function costs
  865.         LARGE_INTEGER before;
  866.         QueryPerformanceCounter( &before );
  867.  
  868.         LARGE_INTEGER test;
  869.         for(int i=0; i<1000000; i++)
  870.         {
  871.             QueryPerformanceCounter( &test );
  872.         }
  873.         LARGE_INTEGER after;
  874.         QueryPerformanceCounter( &after );
  875.  
  876.         __int64 Ticks = after.QuadPart - before.QuadPart;
  877.         sQueryPerformanceCounterCost.QuadPart = (double)Ticks/1000000.0f;
  878.     }
  879.  
  880.     // automatically deducts the cost of the Sys_Milliseconds() call used to gather the timing
  881.     ID_INLINE void AddTime(unsigned long long ticks) {
  882.         // add number of microseconds
  883.         mTime.QuadPart += (ticks - sQueryPerformanceCounterCost.QuadPart)/sTicksPerMicrosecond;
  884.     }
  885.     ID_INLINE void AddCall() {
  886.         mCalls++;
  887.     }
  888.     ID_INLINE static void PrintTimes(void) {
  889.         if (!mFirst) {
  890.             return;
  891.         }
  892.         printf("Start Frame\n");
  893.         for (TimedScope* p=mFirst; p; p = p->mNext) {
  894.             printf("\t%20s: %d us\t%d calls\t %f us/call\n", p->mName, p->mTime, p->mCalls, (p->mCalls)?(double)p->mTime.QuadPart/(double)p->mCalls:0.0f);
  895.         }
  896.         printf("End Frame\n\n");
  897.     }
  898.     ID_INLINE static void ClearTimes(void) {
  899.         if (!mFirst) {
  900.             return;
  901.         }
  902.         for (TimedScope* p=mFirst; p; p = p->mNext) {
  903.             p->mTime.QuadPart = 0;
  904.         }
  905.     }
  906.     ID_INLINE static void ClearCalls(void) {
  907.         if (!mFirst) {
  908.             return;
  909.         }
  910.         for (TimedScope* p=mFirst; p; p = p->mNext) {
  911.             p->mCalls = 0;
  912.         }
  913.     }
  914. protected:
  915.     static TimedScope *        mFirst;
  916.     TimedScope *            mNext;
  917.     const char *            mName;
  918.     LARGE_INTEGER            mTime;
  919.     unsigned int            mCalls;
  920.     static LARGE_INTEGER    sQueryPerformanceCounterCost;
  921.     static double            sTicksPerMicrosecond;
  922. };
  923.  
  924. class ScopeAutoTimer {
  925. public:
  926.     ID_INLINE ScopeAutoTimer(TimedScope *scope) {
  927.         QueryPerformanceCounter( &mStartTime );
  928.         //mStartTime = Sys_Milliseconds();
  929.         mScope = scope;
  930.     }
  931.     ID_INLINE ~ScopeAutoTimer() {
  932.         LARGE_INTEGER endTime;
  933.         QueryPerformanceCounter( &endTime );
  934.  
  935.         //unsigned int time = (unsigned int)Sys_Milliseconds() - mStartTime;
  936.         // pass in number of ticks used and TimedScope will adjust it to a number of microseconds
  937.         mScope->AddTime(endTime.QuadPart - mStartTime.QuadPart);
  938.         mScope->AddCall();
  939.     }
  940. protected:
  941.     LARGE_INTEGER    mStartTime;
  942.     TimedScope *    mScope;
  943. };
  944.  
  945. #define TIME_THIS_SCOPE(name) \
  946.     static TimedScope scopeTime(name); \
  947.     ScopeAutoTimer autoTimer(&scopeTime)
  948. #else
  949. #define TIME_THIS_SCOPE(name)
  950. #endif
  951. #endif
  952.  
  953. #define STRINGIZE_INDIRECT(F, X) F(X)
  954. #define STRINGIZE(X) #X
  955. #define __LINESTR__ STRINGIZE_INDIRECT(STRINGIZE, __LINE__)
  956. #define __FILELINEFUNC__ (__FILE__ " " __LINESTR__ " " __FUNCTION__)
  957. #define __FUNCLINE__ ( __FUNCTION__ " " __LINESTR__ )
  958.  
  959. // RAVEN END
  960.  
  961. #endif /* !__SYS_PUBLIC__ */
  962.