2
===========================================================================
3
Copyright (C) 1999-2005 Id Software, Inc.
5
This file is part of Quake III Arena source code.
7
Quake III Arena source code is free software; you can redistribute it
8
and/or modify it under the terms of the GNU General Public License as
9
published by the Free Software Foundation; either version 2 of the License,
10
or (at your option) any later version.
12
Quake III Arena source code is distributed in the hope that it will be
13
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
GNU General Public License for more details.
17
You should have received a copy of the GNU General Public License
18
along with Quake III Arena source code; if not, write to the Free Software
19
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
===========================================================================
26
#include <sys/types.h>
35
#ifdef USE_LOCAL_HEADERS
37
# include "SDL_cpuinfo.h"
40
# include <SDL_cpuinfo.h>
44
#include "sys_local.h"
45
#include "sys_loadlib.h"
47
#include "../qcommon/q_shared.h"
48
#include "../qcommon/qcommon.h"
50
static char binaryPath[ MAX_OSPATH ] = { 0 };
51
static char installPath[ MAX_OSPATH ] = { 0 };
58
void Sys_SetBinaryPath(const char *path)
60
Q_strncpyz(binaryPath, path, sizeof(binaryPath));
68
char *Sys_BinaryPath(void)
75
Sys_SetDefaultInstallPath
78
void Sys_SetDefaultInstallPath(const char *path)
80
Q_strncpyz(installPath, path, sizeof(installPath));
85
Sys_DefaultInstallPath
88
char *Sys_DefaultInstallPath(void)
101
char *Sys_DefaultAppPath(void)
103
return Sys_BinaryPath();
110
Restart the input subsystem
113
void Sys_In_Restart_f( void )
123
Handle new console input
126
char *Sys_ConsoleInput(void)
135
Single exit point (regular exit or in case of error)
138
void Sys_Exit( int ex )
149
// Cause a backtrace on error exits
160
void Sys_Quit( void )
168
Sys_GetProcessorFeatures
171
cpuFeatures_t Sys_GetProcessorFeatures( void )
173
cpuFeatures_t features = 0;
176
if( SDL_HasRDTSC( ) ) features |= CF_RDTSC;
177
if( SDL_HasMMX( ) ) features |= CF_MMX;
178
if( SDL_HasMMXExt( ) ) features |= CF_MMX_EXT;
179
if( SDL_Has3DNow( ) ) features |= CF_3DNOW;
180
if( SDL_Has3DNowExt( ) ) features |= CF_3DNOW_EXT;
181
if( SDL_HasSSE( ) ) features |= CF_SSE;
182
if( SDL_HasSSE2( ) ) features |= CF_SSE2;
183
if( SDL_HasAltiVec( ) ) features |= CF_ALTIVEC;
196
Cmd_AddCommand( "in_restart", Sys_In_Restart_f );
197
Cvar_Set( "arch", OS_STRING " " ARCH_STRING );
198
Cvar_Set( "username", Sys_GetCurrentUser( ) );
205
Transform Q3 colour codes to ANSI escape sequences
208
void Sys_AnsiColorPrint( const char *msg )
210
static char buffer[ MAXPRINTMSG ];
212
static int q3ToAnsi[ 8 ] =
226
if( Q_IsColorString( msg ) || *msg == '\n' )
228
// First empty the buffer
231
buffer[ length ] = '\0';
232
fputs( buffer, stderr );
238
// Issue a reset and then the newline
239
fputs( "\033[0m\n", stderr );
244
// Print the color code
245
Com_sprintf( buffer, sizeof( buffer ), "\033[%dm",
246
q3ToAnsi[ ColorIndex( *( msg + 1 ) ) ] );
247
fputs( buffer, stderr );
253
if( length >= MAXPRINTMSG - 1 )
256
buffer[ length ] = *msg;
262
// Empty anything still left in the buffer
265
buffer[ length ] = '\0';
266
fputs( buffer, stderr );
275
void Sys_Print( const char *msg )
286
void Sys_Error( const char *error, ... )
293
va_start (argptr,error);
294
Q_vsnprintf (string, sizeof(string), error, argptr);
297
Sys_ErrorDialog( string );
307
void Sys_Warn( char *warning, ... )
312
va_start (argptr,warning);
313
Q_vsnprintf (string, sizeof(string), warning, argptr);
316
CON_Print( va( "Warning: %s", string ) );
323
returns -1 if not present
326
int Sys_FileTime( char *path )
330
if (stat (path,&buf) == -1)
341
void Sys_UnloadDll( void *dllHandle )
345
Com_Printf("Sys_UnloadDll(NULL)\n");
349
Sys_UnloadLibrary(dllHandle);
357
static void* Sys_TryLibraryLoad(const char* base, const char* gamedir, const char* fname, char* fqpath )
364
fn = FS_BuildOSPath( base, gamedir, fname );
365
Com_Printf( "Sys_LoadDll(%s)... \n", fn );
367
libHandle = Sys_LoadLibrary(fn);
370
Com_Printf( "Sys_LoadDll(%s) failed:\n\"%s\"\n", fn, Sys_LibraryError() );
374
Com_Printf ( "Sys_LoadDll(%s): succeeded ...\n", fn );
375
Q_strncpyz ( fqpath , fn , MAX_QPATH ) ;
384
Used to load a development dll instead of a virtual machine
385
#1 look down current path
386
#2 look in fs_homepath
387
#3 look in fs_basepath
390
void *Sys_LoadDll( const char *name, char *fqpath ,
391
intptr_t (**entryPoint)(int, ...),
392
intptr_t (*systemcalls)(intptr_t, ...) )
395
void (*dllEntry)( intptr_t (*syscallptr)(intptr_t, ...) );
396
char fname[MAX_OSPATH];
404
Q_snprintf (fname, sizeof(fname), "%s" ARCH_STRING DLL_EXT, name);
406
// TODO: use fs_searchpaths from files.c
408
basepath = Cvar_VariableString( "fs_basepath" );
409
homepath = Cvar_VariableString( "fs_homepath" );
410
gamedir = Cvar_VariableString( "fs_game" );
412
libHandle = Sys_TryLibraryLoad(pwdpath, gamedir, fname, fqpath);
414
if(!libHandle && homepath)
415
libHandle = Sys_TryLibraryLoad(homepath, gamedir, fname, fqpath);
417
if(!libHandle && basepath)
418
libHandle = Sys_TryLibraryLoad(basepath, gamedir, fname, fqpath);
421
Com_Printf ( "Sys_LoadDll(%s) failed to load library\n", name );
425
dllEntry = Sys_LoadFunction( libHandle, "dllEntry" );
426
*entryPoint = Sys_LoadFunction( libHandle, "vmMain" );
428
if ( !*entryPoint || !dllEntry )
430
Com_Printf ( "Sys_LoadDll(%s) failed to find vmMain function:\n\"%s\" !\n", name, Sys_LibraryError( ) );
431
Sys_UnloadLibrary(libHandle);
436
Com_Printf ( "Sys_LoadDll(%s) found vmMain function at %p\n", name, *entryPoint );
437
dllEntry( systemcalls );
447
static void Sys_Idle( void )
450
int appState = SDL_GetAppState( );
453
// If we have no input focus at all, sleep a bit
454
if( !( appState & ( SDL_APPMOUSEFOCUS | SDL_APPINPUTFOCUS ) ) )
456
Cvar_SetValue( "com_unfocused", 1 );
460
Cvar_SetValue( "com_unfocused", 0 );
462
// If we're minimised, sleep a bit more
463
if( !( appState & SDL_APPACTIVE ) )
465
Cvar_SetValue( "com_minimized", 1 );
469
Cvar_SetValue( "com_minimized", 0 );
471
if( !com_dedicated->integer && sleep )
474
// Dedicated server idles via NET_Sleep
483
void Sys_ParseArgs( int argc, char **argv )
487
if( !strcmp( argv[1], "--version" ) ||
488
!strcmp( argv[1], "-v" ) )
490
const char* date = __DATE__;
492
fprintf( stdout, Q3_VERSION " dedicated server (%s)\n", date );
494
fprintf( stdout, Q3_VERSION " client (%s)\n", date );
501
#ifndef DEFAULT_BASEDIR
503
# define DEFAULT_BASEDIR Sys_StripAppBundle(Sys_BinaryPath())
505
# define DEFAULT_BASEDIR Sys_BinaryPath()
514
void Sys_SigHandler( int signal )
516
static qboolean signalcaught = qfalse;
520
fprintf( stderr, "DOUBLE SIGNAL FAULT: Received signal %d, exiting...\n",
525
signalcaught = qtrue;
526
fprintf( stderr, "Received signal %d, exiting...\n", signal );
530
SV_Shutdown( "Signal caught" );
533
Sys_Exit( 0 ); // Exit with 0 to avoid recursive signals
541
int main( int argc, char **argv )
544
char commandLine[ MAX_STRING_CHARS ] = { 0 };
550
# if !SDL_VERSION_ATLEAST(MINSDL_MAJOR,MINSDL_MINOR,MINSDL_PATCH)
551
# error A more recent version of SDL is required
555
const SDL_version *ver = SDL_Linked_Version( );
558
#define XSTRING(s) STRING(s)
559
#define MINSDL_VERSION \
560
XSTRING(MINSDL_MAJOR) "." \
561
XSTRING(MINSDL_MINOR) "." \
562
XSTRING(MINSDL_PATCH)
564
if( SDL_VERSIONNUM( ver->major, ver->minor, ver->patch ) <
565
SDL_VERSIONNUM( MINSDL_MAJOR, MINSDL_MINOR, MINSDL_PATCH ) )
567
Sys_Print( "SDL version " MINSDL_VERSION " or greater required\n" );
572
Sys_ParseArgs( argc, argv );
573
Sys_SetBinaryPath( Sys_Dirname( argv[ 0 ] ) );
574
Sys_SetDefaultInstallPath( DEFAULT_BASEDIR );
576
// Concatenate the command line for passing to Com_Init
577
for( i = 1; i < argc; i++ )
579
Q_strcat( commandLine, sizeof( commandLine ), argv[ i ] );
580
Q_strcat( commandLine, sizeof( commandLine ), " " );
583
Com_Init( commandLine );
589
// Windows doesn't have these signals
590
// see CON_CtrlHandler() in con_win32.c
591
signal( SIGHUP, Sys_SigHandler );
592
signal( SIGQUIT, Sys_SigHandler );
593
signal( SIGTRAP, Sys_SigHandler );
594
signal( SIGIOT, Sys_SigHandler );
595
signal( SIGBUS, Sys_SigHandler );
598
signal( SIGILL, Sys_SigHandler );
599
signal( SIGFPE, Sys_SigHandler );
600
signal( SIGSEGV, Sys_SigHandler );
601
signal( SIGTERM, Sys_SigHandler );